Add test coverage for isl_[union_]set_get_basic_set_list
[isl.git] / isl_union_map.c
blob1041fe9cf30d356ff6876a3d1b74cc780fc1f369
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 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl/ctx.h>
19 #include <isl/hash.h>
20 #include <isl_aff_private.h>
21 #include <isl/map.h>
22 #include <isl/set.h>
23 #include <isl_space_private.h>
24 #include <isl/union_set.h>
25 #include <isl_maybe_map.h>
27 #include <bset_from_bmap.c>
28 #include <set_to_map.c>
29 #include <set_from_map.c>
30 #include <uset_to_umap.c>
31 #include <uset_from_umap.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 /* Is this union map actually a parameter domain?
92 * Users should never call this function. Outside of isl,
93 * a union map can never be a parameter domain.
95 isl_bool isl_union_map_is_params(__isl_keep isl_union_map *umap)
97 return isl_union_set_is_params(uset_from_umap(umap));
100 static __isl_give isl_union_map *isl_union_map_alloc(
101 __isl_take isl_space *space, int size)
103 isl_union_map *umap;
105 space = isl_space_params(space);
106 if (!space)
107 return NULL;
109 umap = isl_calloc_type(space->ctx, isl_union_map);
110 if (!umap) {
111 isl_space_free(space);
112 return NULL;
115 umap->ref = 1;
116 umap->dim = space;
117 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
118 return isl_union_map_free(umap);
120 return umap;
123 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *space)
125 return isl_union_map_alloc(space, 16);
128 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_space *space)
130 return isl_union_map_empty(space);
133 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
135 return umap ? umap->dim->ctx : NULL;
138 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
140 return uset ? uset->dim->ctx : NULL;
143 /* Return the space of "umap".
145 __isl_keep isl_space *isl_union_map_peek_space(__isl_keep isl_union_map *umap)
147 return umap ? umap->dim : NULL;
150 __isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
152 return isl_space_copy(isl_union_map_peek_space(umap));
155 /* Return the position of the parameter with the given name
156 * in "umap".
157 * Return -1 if no such dimension can be found.
159 int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
160 enum isl_dim_type type, const char *name)
162 if (!umap)
163 return -1;
164 return isl_space_find_dim_by_name(umap->dim, type, name);
167 __isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_set *uset)
169 return isl_union_map_get_space(uset);
172 static isl_stat free_umap_entry(void **entry, void *user)
174 isl_map *map = *entry;
175 isl_map_free(map);
176 return isl_stat_ok;
179 static isl_stat add_map(__isl_take isl_map *map, void *user)
181 isl_union_map **umap = (isl_union_map **)user;
183 *umap = isl_union_map_add_map(*umap, map);
185 return isl_stat_ok;
188 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
190 isl_union_map *dup;
192 if (!umap)
193 return NULL;
195 dup = isl_union_map_empty(isl_space_copy(umap->dim));
196 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
197 goto error;
198 return dup;
199 error:
200 isl_union_map_free(dup);
201 return NULL;
204 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
206 if (!umap)
207 return NULL;
209 if (umap->ref == 1)
210 return umap;
211 umap->ref--;
212 return isl_union_map_dup(umap);
215 struct isl_union_align {
216 isl_reordering *exp;
217 isl_union_map *res;
220 static isl_stat align_entry(void **entry, void *user)
222 isl_map *map = *entry;
223 isl_reordering *exp;
224 struct isl_union_align *data = user;
226 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
227 isl_map_get_space(map));
229 data->res = isl_union_map_add_map(data->res,
230 isl_map_realign(isl_map_copy(map), exp));
232 return isl_stat_ok;
235 /* Align the parameters of umap along those of model.
236 * The result has the parameters of model first, in the same order
237 * as they appear in model, followed by any remaining parameters of
238 * umap that do not appear in model.
240 __isl_give isl_union_map *isl_union_map_align_params(
241 __isl_take isl_union_map *umap, __isl_take isl_space *model)
243 struct isl_union_align data = { NULL, NULL };
244 isl_bool equal_params;
246 if (!umap || !model)
247 goto error;
249 equal_params = isl_space_has_equal_params(umap->dim, model);
250 if (equal_params < 0)
251 goto error;
252 if (equal_params) {
253 isl_space_free(model);
254 return umap;
257 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
258 if (!data.exp)
259 goto error;
261 data.res = isl_union_map_alloc(isl_reordering_get_space(data.exp),
262 umap->table.n);
263 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
264 &align_entry, &data) < 0)
265 goto error;
267 isl_reordering_free(data.exp);
268 isl_union_map_free(umap);
269 isl_space_free(model);
270 return data.res;
271 error:
272 isl_reordering_free(data.exp);
273 isl_union_map_free(umap);
274 isl_union_map_free(data.res);
275 isl_space_free(model);
276 return NULL;
279 __isl_give isl_union_set *isl_union_set_align_params(
280 __isl_take isl_union_set *uset, __isl_take isl_space *model)
282 return isl_union_map_align_params(uset, model);
285 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
286 __isl_take isl_union_map *umap2)
288 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
289 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
291 umap1 = isl_union_map_cow(umap1);
293 if (!umap1 || !umap2)
294 goto error;
296 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
297 goto error;
299 isl_union_map_free(umap2);
301 return umap1;
302 error:
303 isl_union_map_free(umap1);
304 isl_union_map_free(umap2);
305 return NULL;
308 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
309 __isl_take isl_union_set *uset2)
311 return isl_union_map_union(uset1, uset2);
314 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
316 if (!umap)
317 return NULL;
319 umap->ref++;
320 return umap;
323 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
325 return isl_union_map_copy(uset);
328 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
330 if (!umap)
331 return NULL;
333 if (--umap->ref > 0)
334 return NULL;
336 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
337 &free_umap_entry, NULL);
338 isl_hash_table_clear(&umap->table);
339 isl_space_free(umap->dim);
340 free(umap);
341 return NULL;
344 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
346 return isl_union_map_free(uset);
349 /* Do "umap" and "space" have the same parameters?
351 isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
352 __isl_keep isl_space *space)
354 isl_space *umap_space;
356 umap_space = isl_union_map_peek_space(umap);
357 return isl_space_has_equal_params(umap_space, space);
360 /* Do "uset" and "space" have the same parameters?
362 isl_bool isl_union_set_space_has_equal_params(__isl_keep isl_union_set *uset,
363 __isl_keep isl_space *space)
365 return isl_union_map_space_has_equal_params(uset_to_umap(uset), space);
368 static int has_space(const void *entry, const void *val)
370 isl_map *map = (isl_map *)entry;
371 isl_space *space = (isl_space *) val;
373 return isl_space_is_equal(map->dim, space);
376 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
377 __isl_take isl_map *map)
379 uint32_t hash;
380 struct isl_hash_table_entry *entry;
381 isl_bool aligned;
383 if (!map || !umap)
384 goto error;
386 if (isl_map_plain_is_empty(map)) {
387 isl_map_free(map);
388 return umap;
391 aligned = isl_map_space_has_equal_params(map, umap->dim);
392 if (aligned < 0)
393 goto error;
394 if (!aligned) {
395 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
396 map = isl_map_align_params(map, isl_union_map_get_space(umap));
399 umap = isl_union_map_cow(umap);
401 if (!map || !umap)
402 goto error;
404 hash = isl_space_get_hash(map->dim);
405 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
406 &has_space, map->dim, 1);
407 if (!entry)
408 goto error;
410 if (!entry->data)
411 entry->data = map;
412 else {
413 entry->data = isl_map_union(entry->data, isl_map_copy(map));
414 if (!entry->data)
415 goto error;
416 isl_map_free(map);
419 return umap;
420 error:
421 isl_map_free(map);
422 isl_union_map_free(umap);
423 return NULL;
426 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
427 __isl_take isl_set *set)
429 return isl_union_map_add_map(uset, set_to_map(set));
432 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
434 isl_space *dim;
435 isl_union_map *umap;
437 if (!map)
438 return NULL;
440 dim = isl_map_get_space(map);
441 dim = isl_space_params(dim);
442 umap = isl_union_map_empty(dim);
443 umap = isl_union_map_add_map(umap, map);
445 return umap;
448 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
450 return isl_union_map_from_map(set_to_map(set));
453 __isl_give isl_union_map *isl_union_map_from_basic_map(
454 __isl_take isl_basic_map *bmap)
456 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
459 __isl_give isl_union_set *isl_union_set_from_basic_set(
460 __isl_take isl_basic_set *bset)
462 return isl_union_map_from_basic_map(bset);
465 struct isl_union_map_foreach_data
467 isl_stat (*fn)(__isl_take isl_map *map, void *user);
468 void *user;
471 static isl_stat call_on_copy(void **entry, void *user)
473 isl_map *map = *entry;
474 struct isl_union_map_foreach_data *data;
475 data = (struct isl_union_map_foreach_data *)user;
477 return data->fn(isl_map_copy(map), data->user);
480 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
482 return umap ? umap->table.n : 0;
485 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
487 return uset ? uset->table.n : 0;
490 isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
491 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
493 struct isl_union_map_foreach_data data = { fn, user };
495 if (!umap)
496 return isl_stat_error;
498 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
499 &call_on_copy, &data);
502 /* Internal data structure for isl_union_map_every_map.
504 * "test" is the user-specified callback function.
505 * "user" is the user-specified callback function argument.
507 * "failed" is initialized to 0 and set to 1 if "test" fails
508 * on any map.
510 struct isl_union_map_every_data {
511 isl_bool (*test)(__isl_keep isl_map *map, void *user);
512 void *user;
513 int failed;
516 /* Call data->test on "map".
517 * If this fails, then set data->failed and abort.
519 static isl_stat call_every(void **entry, void *user)
521 isl_map *map = *entry;
522 struct isl_union_map_every_data *data = user;
523 isl_bool r;
525 r = data->test(map, data->user);
526 if (r < 0)
527 return isl_stat_error;
528 if (r)
529 return isl_stat_ok;
530 data->failed = 1;
531 return isl_stat_error;
534 /* Does "test" succeed on every map in "umap"?
536 isl_bool isl_union_map_every_map(__isl_keep isl_union_map *umap,
537 isl_bool (*test)(__isl_keep isl_map *map, void *user), void *user)
539 struct isl_union_map_every_data data = { test, user, 0 };
540 isl_stat r;
542 if (!umap)
543 return isl_bool_error;
545 r = isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
546 &call_every, &data);
547 if (r >= 0)
548 return isl_bool_true;
549 if (data.failed)
550 return isl_bool_false;
551 return isl_bool_error;
554 static isl_stat copy_map(void **entry, void *user)
556 isl_map *map = *entry;
557 isl_map **map_p = user;
559 *map_p = isl_map_copy(map);
561 return isl_stat_error;
564 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
566 isl_ctx *ctx;
567 isl_map *map = NULL;
569 if (!umap)
570 return NULL;
571 ctx = isl_union_map_get_ctx(umap);
572 if (umap->table.n != 1)
573 isl_die(ctx, isl_error_invalid,
574 "union map needs to contain elements in exactly "
575 "one space", goto error);
577 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
579 isl_union_map_free(umap);
581 return map;
582 error:
583 isl_union_map_free(umap);
584 return NULL;
587 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
589 return isl_map_from_union_map(uset);
592 /* Extract the map in "umap" that lives in the given space (ignoring
593 * parameters).
595 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
596 __isl_take isl_space *space)
598 uint32_t hash;
599 struct isl_hash_table_entry *entry;
601 space = isl_space_drop_dims(space, isl_dim_param,
602 0, isl_space_dim(space, isl_dim_param));
603 space = isl_space_align_params(space, isl_union_map_get_space(umap));
604 if (!umap || !space)
605 goto error;
607 hash = isl_space_get_hash(space);
608 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
609 &has_space, space, 0);
610 if (!entry)
611 return isl_map_empty(space);
612 isl_space_free(space);
613 return isl_map_copy(entry->data);
614 error:
615 isl_space_free(space);
616 return NULL;
619 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
620 __isl_take isl_space *dim)
622 return set_from_map(isl_union_map_extract_map(uset, dim));
625 /* Check if umap contains a map in the given space.
627 isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
628 __isl_keep isl_space *space)
630 uint32_t hash;
631 struct isl_hash_table_entry *entry;
633 if (!umap || !space)
634 return isl_bool_error;
636 hash = isl_space_get_hash(space);
637 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
638 &has_space, space, 0);
639 return !!entry;
642 isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
643 __isl_keep isl_space *space)
645 return isl_union_map_contains(uset, space);
648 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
649 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
651 return isl_union_map_foreach_map(uset,
652 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
655 struct isl_union_set_foreach_point_data {
656 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
657 void *user;
660 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
662 struct isl_union_set_foreach_point_data *data = user;
663 isl_stat r;
665 r = isl_set_foreach_point(set, data->fn, data->user);
666 isl_set_free(set);
668 return r;
671 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
672 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
674 struct isl_union_set_foreach_point_data data = { fn, user };
675 return isl_union_set_foreach_set(uset, &foreach_point, &data);
678 /* Data structure that specifies how gen_bin_op should
679 * construct results from the inputs.
681 * If "subtract" is set, then a map in the first input is copied to the result
682 * if there is no corresponding map in the second input.
683 * Otherwise, a map in the first input with no corresponding map
684 * in the second input is ignored.
685 * If "filter" is not NULL, then it specifies which maps in the first
686 * input may have a matching map in the second input.
687 * In particular, it makes sure that "match_space" can be called
688 * on the space of the map.
689 * "match_space" specifies how to transform the space of a map
690 * in the first input to the space of the corresponding map
691 * in the second input.
692 * "fn_map" specifies how the matching maps, one from each input,
693 * should be combined to form a map in the result.
695 struct isl_bin_op_control {
696 int subtract;
697 isl_bool (*filter)(__isl_keep isl_map *map);
698 __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
699 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
700 __isl_take isl_map *map2);
703 /* Internal data structure for gen_bin_op.
704 * "control" specifies how the maps in the result should be constructed.
705 * "umap2" is a pointer to the second argument.
706 * "res" collects the results.
708 struct isl_union_map_gen_bin_data {
709 struct isl_bin_op_control *control;
710 isl_union_map *umap2;
711 isl_union_map *res;
714 /* Add a copy of "map" to "res" and return the result.
716 static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
717 __isl_keep isl_map *map)
719 return isl_union_map_add_map(res, isl_map_copy(map));
722 /* Combine "map1" and "map2", add the result to "res" and return the result.
723 * Check whether the result is empty before adding it to "res".
725 static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
726 __isl_keep isl_map *map1, __isl_keep isl_map *map2,
727 struct isl_union_map_gen_bin_data *data)
729 isl_bool empty;
730 isl_map *map;
732 map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
733 empty = isl_map_is_empty(map);
734 if (empty < 0 || empty) {
735 isl_map_free(map);
736 if (empty < 0)
737 return isl_union_map_free(res);
738 return res;
740 return isl_union_map_add_map(res, map);
743 /* Dummy match_space function that simply returns the input space.
745 static __isl_give isl_space *identity(__isl_take isl_space *space)
747 return space;
750 /* Look for the map in data->umap2 that corresponds to "map", if any.
751 * Return (isl_bool_true, matching map) if there is one,
752 * (isl_bool_false, NULL) if there is no matching map and
753 * (isl_bool_error, NULL) on error.
755 * If not NULL, then data->control->filter specifies whether "map"
756 * can have any matching map. If so,
757 * data->control->match_space specifies which map in data->umap2
758 * corresponds to "map".
760 static __isl_keep isl_maybe_isl_map bin_try_get_match(
761 struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
763 uint32_t hash;
764 struct isl_hash_table_entry *entry2;
765 isl_space *space;
766 isl_maybe_isl_map res = { isl_bool_error, NULL };
768 if (data->control->filter) {
769 res.valid = data->control->filter(map);
770 if (res.valid < 0 || !res.valid)
771 return res;
772 res.valid = isl_bool_error;
775 space = isl_map_get_space(map);
776 if (data->control->match_space != &identity)
777 space = data->control->match_space(space);
778 if (!space)
779 return res;
780 hash = isl_space_get_hash(space);
781 entry2 = isl_hash_table_find(isl_union_map_get_ctx(data->umap2),
782 &data->umap2->table, hash,
783 &has_space, space, 0);
784 isl_space_free(space);
785 res.valid = entry2 != NULL;
786 if (entry2)
787 res.value = entry2->data;
789 return res;
792 /* isl_hash_table_foreach callback for gen_bin_op.
793 * Look for the map in data->umap2 that corresponds
794 * to the map that "entry" points to, apply the binary operation and
795 * add the result to data->res.
797 * If no corresponding map can be found, then the effect depends
798 * on data->control->subtract. If it is set, then the current map
799 * is added directly to the result. Otherwise, it is ignored.
801 static isl_stat gen_bin_entry(void **entry, void *user)
803 struct isl_union_map_gen_bin_data *data = user;
804 isl_map *map = *entry;
805 isl_maybe_isl_map m;
807 m = bin_try_get_match(data, map);
808 if (m.valid < 0)
809 return isl_stat_error;
810 if (!m.valid && !data->control->subtract)
811 return isl_stat_ok;
813 if (!m.valid)
814 data->res = bin_add_map(data->res, map);
815 else
816 data->res = bin_add_pair(data->res, map, m.value, data);
817 if (!data->res)
818 return isl_stat_error;
820 return isl_stat_ok;
823 /* Apply a binary operation to "umap1" and "umap2" based on "control".
824 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
825 * in gen_bin_entry.
827 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
828 __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
830 struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
832 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
833 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
835 if (!umap1 || !umap2)
836 goto error;
838 data.umap2 = umap2;
839 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
840 umap1->table.n);
841 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
842 &gen_bin_entry, &data) < 0)
843 goto error;
845 isl_union_map_free(umap1);
846 isl_union_map_free(umap2);
847 return data.res;
848 error:
849 isl_union_map_free(umap1);
850 isl_union_map_free(umap2);
851 isl_union_map_free(data.res);
852 return NULL;
855 __isl_give isl_union_map *isl_union_map_subtract(
856 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
858 struct isl_bin_op_control control = {
859 .subtract = 1,
860 .match_space = &identity,
861 .fn_map = &isl_map_subtract,
864 return gen_bin_op(umap1, umap2, &control);
867 __isl_give isl_union_set *isl_union_set_subtract(
868 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
870 return isl_union_map_subtract(uset1, uset2);
873 struct isl_union_map_gen_bin_set_data {
874 isl_set *set;
875 isl_union_map *res;
878 static isl_stat intersect_params_entry(void **entry, void *user)
880 struct isl_union_map_gen_bin_set_data *data = user;
881 isl_map *map = *entry;
882 int empty;
884 map = isl_map_copy(map);
885 map = isl_map_intersect_params(map, isl_set_copy(data->set));
887 empty = isl_map_is_empty(map);
888 if (empty < 0) {
889 isl_map_free(map);
890 return isl_stat_error;
893 data->res = isl_union_map_add_map(data->res, map);
895 return isl_stat_ok;
898 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
899 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
901 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
903 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
904 set = isl_set_align_params(set, isl_union_map_get_space(umap));
906 if (!umap || !set)
907 goto error;
909 data.set = set;
910 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
911 umap->table.n);
912 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
913 fn, &data) < 0)
914 goto error;
916 isl_union_map_free(umap);
917 isl_set_free(set);
918 return data.res;
919 error:
920 isl_union_map_free(umap);
921 isl_set_free(set);
922 isl_union_map_free(data.res);
923 return NULL;
926 /* Intersect "umap" with the parameter domain "set".
928 * If "set" does not have any constraints, then we can return immediately.
930 __isl_give isl_union_map *isl_union_map_intersect_params(
931 __isl_take isl_union_map *umap, __isl_take isl_set *set)
933 int is_universe;
935 is_universe = isl_set_plain_is_universe(set);
936 if (is_universe < 0)
937 goto error;
938 if (is_universe) {
939 isl_set_free(set);
940 return umap;
943 return gen_bin_set_op(umap, set, &intersect_params_entry);
944 error:
945 isl_union_map_free(umap);
946 isl_set_free(set);
947 return NULL;
950 __isl_give isl_union_set *isl_union_set_intersect_params(
951 __isl_take isl_union_set *uset, __isl_take isl_set *set)
953 return isl_union_map_intersect_params(uset, set);
956 static __isl_give isl_union_map *union_map_intersect_params(
957 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
959 return isl_union_map_intersect_params(umap,
960 isl_set_from_union_set(uset));
963 static __isl_give isl_union_map *union_map_gist_params(
964 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
966 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
969 struct isl_union_map_match_bin_data {
970 isl_union_map *umap2;
971 isl_union_map *res;
972 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
975 static isl_stat match_bin_entry(void **entry, void *user)
977 struct isl_union_map_match_bin_data *data = user;
978 uint32_t hash;
979 struct isl_hash_table_entry *entry2;
980 isl_map *map = *entry;
981 int empty;
983 hash = isl_space_get_hash(map->dim);
984 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
985 hash, &has_space, map->dim, 0);
986 if (!entry2)
987 return isl_stat_ok;
989 map = isl_map_copy(map);
990 map = data->fn(map, isl_map_copy(entry2->data));
992 empty = isl_map_is_empty(map);
993 if (empty < 0) {
994 isl_map_free(map);
995 return isl_stat_error;
997 if (empty) {
998 isl_map_free(map);
999 return isl_stat_ok;
1002 data->res = isl_union_map_add_map(data->res, map);
1004 return isl_stat_ok;
1007 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
1008 __isl_take isl_union_map *umap2,
1009 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
1011 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
1013 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1014 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1016 if (!umap1 || !umap2)
1017 goto error;
1019 data.umap2 = umap2;
1020 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1021 umap1->table.n);
1022 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1023 &match_bin_entry, &data) < 0)
1024 goto error;
1026 isl_union_map_free(umap1);
1027 isl_union_map_free(umap2);
1028 return data.res;
1029 error:
1030 isl_union_map_free(umap1);
1031 isl_union_map_free(umap2);
1032 isl_union_map_free(data.res);
1033 return NULL;
1036 __isl_give isl_union_map *isl_union_map_intersect(
1037 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1039 return match_bin_op(umap1, umap2, &isl_map_intersect);
1042 /* Compute the intersection of the two union_sets.
1043 * As a special case, if exactly one of the two union_sets
1044 * is a parameter domain, then intersect the parameter domain
1045 * of the other one with this set.
1047 __isl_give isl_union_set *isl_union_set_intersect(
1048 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1050 int p1, p2;
1052 p1 = isl_union_set_is_params(uset1);
1053 p2 = isl_union_set_is_params(uset2);
1054 if (p1 < 0 || p2 < 0)
1055 goto error;
1056 if (!p1 && p2)
1057 return union_map_intersect_params(uset1, uset2);
1058 if (p1 && !p2)
1059 return union_map_intersect_params(uset2, uset1);
1060 return isl_union_map_intersect(uset1, uset2);
1061 error:
1062 isl_union_set_free(uset1);
1063 isl_union_set_free(uset2);
1064 return NULL;
1067 static isl_stat gist_params_entry(void **entry, void *user)
1069 struct isl_union_map_gen_bin_set_data *data = user;
1070 isl_map *map = *entry;
1071 int empty;
1073 map = isl_map_copy(map);
1074 map = isl_map_gist_params(map, isl_set_copy(data->set));
1076 empty = isl_map_is_empty(map);
1077 if (empty < 0) {
1078 isl_map_free(map);
1079 return isl_stat_error;
1082 data->res = isl_union_map_add_map(data->res, map);
1084 return isl_stat_ok;
1087 __isl_give isl_union_map *isl_union_map_gist_params(
1088 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1090 return gen_bin_set_op(umap, set, &gist_params_entry);
1093 __isl_give isl_union_set *isl_union_set_gist_params(
1094 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1096 return isl_union_map_gist_params(uset, set);
1099 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1100 __isl_take isl_union_map *context)
1102 return match_bin_op(umap, context, &isl_map_gist);
1105 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
1106 __isl_take isl_union_set *context)
1108 if (isl_union_set_is_params(context))
1109 return union_map_gist_params(uset, context);
1110 return isl_union_map_gist(uset, context);
1113 /* For each map in "umap", remove the constraints in the corresponding map
1114 * of "context".
1115 * Each map in "context" is assumed to consist of a single disjunct and
1116 * to have explicit representations for all local variables.
1118 __isl_give isl_union_map *isl_union_map_plain_gist(
1119 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1121 return match_bin_op(umap, context, &isl_map_plain_gist);
1124 /* For each set in "uset", remove the constraints in the corresponding set
1125 * of "context".
1126 * Each set in "context" is assumed to consist of a single disjunct and
1127 * to have explicit representations for all local variables.
1129 __isl_give isl_union_set *isl_union_set_plain_gist(
1130 __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
1132 return isl_union_map_plain_gist(uset, context);
1135 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1136 __isl_take isl_map *set2)
1138 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1141 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1142 __isl_take isl_map *set2)
1144 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1147 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1148 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1150 return match_bin_op(uset1, uset2, &lex_lt_set);
1153 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
1154 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1156 return match_bin_op(uset1, uset2, &lex_le_set);
1159 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1160 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1162 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1165 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1166 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1168 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1171 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1172 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1174 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1177 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1178 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1180 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1183 /* Intersect the domain of "umap" with "uset".
1185 static __isl_give isl_union_map *union_map_intersect_domain(
1186 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1188 struct isl_bin_op_control control = {
1189 .match_space = &isl_space_domain,
1190 .fn_map = &isl_map_intersect_domain,
1193 return gen_bin_op(umap, uset, &control);
1196 /* Intersect the domain of "umap" with "uset".
1197 * If "uset" is a parameters domain, then intersect the parameter
1198 * domain of "umap" with this set.
1200 __isl_give isl_union_map *isl_union_map_intersect_domain(
1201 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1203 if (isl_union_set_is_params(uset))
1204 return union_map_intersect_params(umap, uset);
1205 else
1206 return union_map_intersect_domain(umap, uset);
1209 /* Remove the elements of "uset" from the domain of "umap".
1211 __isl_give isl_union_map *isl_union_map_subtract_domain(
1212 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1214 struct isl_bin_op_control control = {
1215 .subtract = 1,
1216 .match_space = &isl_space_domain,
1217 .fn_map = &isl_map_subtract_domain,
1220 return gen_bin_op(umap, dom, &control);
1223 /* Remove the elements of "uset" from the range of "umap".
1225 __isl_give isl_union_map *isl_union_map_subtract_range(
1226 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1228 struct isl_bin_op_control control = {
1229 .subtract = 1,
1230 .match_space = &isl_space_range,
1231 .fn_map = &isl_map_subtract_range,
1234 return gen_bin_op(umap, dom, &control);
1237 /* Compute the gist of "umap" with respect to the domain "uset".
1239 static __isl_give isl_union_map *union_map_gist_domain(
1240 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1242 struct isl_bin_op_control control = {
1243 .match_space = &isl_space_domain,
1244 .fn_map = &isl_map_gist_domain,
1247 return gen_bin_op(umap, uset, &control);
1250 /* Compute the gist of "umap" with respect to the domain "uset".
1251 * If "uset" is a parameters domain, then compute the gist
1252 * with respect to this parameter domain.
1254 __isl_give isl_union_map *isl_union_map_gist_domain(
1255 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1257 if (isl_union_set_is_params(uset))
1258 return union_map_gist_params(umap, uset);
1259 else
1260 return union_map_gist_domain(umap, uset);
1263 /* Compute the gist of "umap" with respect to the range "uset".
1265 __isl_give isl_union_map *isl_union_map_gist_range(
1266 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1268 struct isl_bin_op_control control = {
1269 .match_space = &isl_space_range,
1270 .fn_map = &isl_map_gist_range,
1273 return gen_bin_op(umap, uset, &control);
1276 __isl_give isl_union_map *isl_union_map_intersect_range(
1277 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1279 struct isl_bin_op_control control = {
1280 .match_space = &isl_space_range,
1281 .fn_map = &isl_map_intersect_range,
1284 return gen_bin_op(umap, uset, &control);
1287 /* Intersect each map in "umap" in a space A -> [B -> C]
1288 * with the corresponding map in "factor" in the space A -> C and
1289 * collect the results.
1291 __isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1292 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1294 struct isl_bin_op_control control = {
1295 .filter = &isl_map_range_is_wrapping,
1296 .match_space = &isl_space_range_factor_range,
1297 .fn_map = &isl_map_intersect_range_factor_range,
1300 return gen_bin_op(umap, factor, &control);
1303 struct isl_union_map_bin_data {
1304 isl_union_map *umap2;
1305 isl_union_map *res;
1306 isl_map *map;
1307 isl_stat (*fn)(void **entry, void *user);
1310 static isl_stat apply_range_entry(void **entry, void *user)
1312 struct isl_union_map_bin_data *data = user;
1313 isl_map *map2 = *entry;
1314 isl_bool empty;
1316 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1317 map2->dim, isl_dim_in))
1318 return isl_stat_ok;
1320 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1322 empty = isl_map_is_empty(map2);
1323 if (empty < 0) {
1324 isl_map_free(map2);
1325 return isl_stat_error;
1327 if (empty) {
1328 isl_map_free(map2);
1329 return isl_stat_ok;
1332 data->res = isl_union_map_add_map(data->res, map2);
1334 return isl_stat_ok;
1337 static isl_stat bin_entry(void **entry, void *user)
1339 struct isl_union_map_bin_data *data = user;
1340 isl_map *map = *entry;
1342 data->map = map;
1343 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1344 data->fn, data) < 0)
1345 return isl_stat_error;
1347 return isl_stat_ok;
1350 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1351 __isl_take isl_union_map *umap2,
1352 isl_stat (*fn)(void **entry, void *user))
1354 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1356 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1357 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1359 if (!umap1 || !umap2)
1360 goto error;
1362 data.umap2 = umap2;
1363 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1364 umap1->table.n);
1365 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1366 &bin_entry, &data) < 0)
1367 goto error;
1369 isl_union_map_free(umap1);
1370 isl_union_map_free(umap2);
1371 return data.res;
1372 error:
1373 isl_union_map_free(umap1);
1374 isl_union_map_free(umap2);
1375 isl_union_map_free(data.res);
1376 return NULL;
1379 __isl_give isl_union_map *isl_union_map_apply_range(
1380 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1382 return bin_op(umap1, umap2, &apply_range_entry);
1385 __isl_give isl_union_map *isl_union_map_apply_domain(
1386 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1388 umap1 = isl_union_map_reverse(umap1);
1389 umap1 = isl_union_map_apply_range(umap1, umap2);
1390 return isl_union_map_reverse(umap1);
1393 __isl_give isl_union_set *isl_union_set_apply(
1394 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1396 return isl_union_map_apply_range(uset, umap);
1399 static isl_stat map_lex_lt_entry(void **entry, void *user)
1401 struct isl_union_map_bin_data *data = user;
1402 isl_map *map2 = *entry;
1404 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1405 map2->dim, isl_dim_out))
1406 return isl_stat_ok;
1408 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1410 data->res = isl_union_map_add_map(data->res, map2);
1412 return isl_stat_ok;
1415 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1416 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1418 return bin_op(umap1, umap2, &map_lex_lt_entry);
1421 static isl_stat map_lex_le_entry(void **entry, void *user)
1423 struct isl_union_map_bin_data *data = user;
1424 isl_map *map2 = *entry;
1426 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1427 map2->dim, isl_dim_out))
1428 return isl_stat_ok;
1430 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1432 data->res = isl_union_map_add_map(data->res, map2);
1434 return isl_stat_ok;
1437 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1438 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1440 return bin_op(umap1, umap2, &map_lex_le_entry);
1443 static isl_stat product_entry(void **entry, void *user)
1445 struct isl_union_map_bin_data *data = user;
1446 isl_map *map2 = *entry;
1448 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1450 data->res = isl_union_map_add_map(data->res, map2);
1452 return isl_stat_ok;
1455 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1456 __isl_take isl_union_map *umap2)
1458 return bin_op(umap1, umap2, &product_entry);
1461 static isl_stat set_product_entry(void **entry, void *user)
1463 struct isl_union_map_bin_data *data = user;
1464 isl_set *set2 = *entry;
1466 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1468 data->res = isl_union_set_add_set(data->res, set2);
1470 return isl_stat_ok;
1473 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1474 __isl_take isl_union_set *uset2)
1476 return bin_op(uset1, uset2, &set_product_entry);
1479 static isl_stat domain_product_entry(void **entry, void *user)
1481 struct isl_union_map_bin_data *data = user;
1482 isl_map *map2 = *entry;
1484 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1485 map2->dim, isl_dim_out))
1486 return isl_stat_ok;
1488 map2 = isl_map_domain_product(isl_map_copy(data->map),
1489 isl_map_copy(map2));
1491 data->res = isl_union_map_add_map(data->res, map2);
1493 return isl_stat_ok;
1496 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1498 __isl_give isl_union_map *isl_union_map_domain_product(
1499 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1501 return bin_op(umap1, umap2, &domain_product_entry);
1504 static isl_stat range_product_entry(void **entry, void *user)
1506 struct isl_union_map_bin_data *data = user;
1507 isl_map *map2 = *entry;
1509 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1510 map2->dim, isl_dim_in))
1511 return isl_stat_ok;
1513 map2 = isl_map_range_product(isl_map_copy(data->map),
1514 isl_map_copy(map2));
1516 data->res = isl_union_map_add_map(data->res, map2);
1518 return isl_stat_ok;
1521 __isl_give isl_union_map *isl_union_map_range_product(
1522 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1524 return bin_op(umap1, umap2, &range_product_entry);
1527 /* If data->map A -> B and "map2" C -> D have the same range space,
1528 * then add (A, C) -> (B * D) to data->res.
1530 static isl_stat flat_domain_product_entry(void **entry, void *user)
1532 struct isl_union_map_bin_data *data = user;
1533 isl_map *map2 = *entry;
1535 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1536 map2->dim, isl_dim_out))
1537 return isl_stat_ok;
1539 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1540 isl_map_copy(map2));
1542 data->res = isl_union_map_add_map(data->res, map2);
1544 return isl_stat_ok;
1547 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1549 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1550 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1552 return bin_op(umap1, umap2, &flat_domain_product_entry);
1555 static isl_stat flat_range_product_entry(void **entry, void *user)
1557 struct isl_union_map_bin_data *data = user;
1558 isl_map *map2 = *entry;
1560 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1561 map2->dim, isl_dim_in))
1562 return isl_stat_ok;
1564 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1565 isl_map_copy(map2));
1567 data->res = isl_union_map_add_map(data->res, map2);
1569 return isl_stat_ok;
1572 __isl_give isl_union_map *isl_union_map_flat_range_product(
1573 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1575 return bin_op(umap1, umap2, &flat_range_product_entry);
1578 /* Data structure that specifies how un_op should modify
1579 * the maps in the union map.
1581 * If "inplace" is set, then the maps in the input union map
1582 * are modified in place. This means that "fn_map" should not
1583 * change the meaning of the map or that the union map only
1584 * has a single reference.
1585 * If "total" is set, then all maps need to be modified and
1586 * the results need to live in the same space.
1587 * Otherwise, a new union map is constructed to store the results.
1588 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1589 * are taken into account. "filter_user" is passed as the second argument
1590 * to "filter". No filter can be set if "inplace" or
1591 * "total" is set.
1592 * "fn_map" specifies how the maps (selected by "filter")
1593 * should be transformed.
1595 struct isl_un_op_control {
1596 int inplace;
1597 int total;
1598 isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1599 void *filter_user;
1600 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1603 /* Data structure for wrapping the data for un_op_filter_drop_user.
1604 * "filter" is the function that is being wrapped.
1606 struct isl_un_op_drop_user_data {
1607 isl_bool (*filter)(__isl_keep isl_map *map);
1610 /* Wrapper for isl_un_op_control filters that do not require
1611 * a second argument.
1612 * Simply call data->filter without the second argument.
1614 static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1616 struct isl_un_op_drop_user_data *data = user;
1617 return data->filter(map);
1620 /* Internal data structure for "un_op".
1621 * "control" specifies how the maps in the union map should be modified.
1622 * "res" collects the results.
1624 struct isl_union_map_un_data {
1625 struct isl_un_op_control *control;
1626 isl_union_map *res;
1629 /* isl_hash_table_foreach callback for un_op.
1630 * Handle the map that "entry" points to.
1632 * If control->filter is set, then check if this map satisfies the filter.
1633 * If so (or if control->filter is not set), modify the map
1634 * by calling control->fn_map and either add the result to data->res or
1635 * replace the original entry by the result (if control->inplace is set).
1637 static isl_stat un_entry(void **entry, void *user)
1639 struct isl_union_map_un_data *data = user;
1640 isl_map *map = *entry;
1642 if (data->control->filter) {
1643 isl_bool ok;
1645 ok = data->control->filter(map, data->control->filter_user);
1646 if (ok < 0)
1647 return isl_stat_error;
1648 if (!ok)
1649 return isl_stat_ok;
1652 map = data->control->fn_map(isl_map_copy(map));
1653 if (!map)
1654 return isl_stat_error;
1655 if (data->control->inplace) {
1656 isl_map_free(*entry);
1657 *entry = map;
1658 } else {
1659 data->res = isl_union_map_add_map(data->res, map);
1660 if (!data->res)
1661 return isl_stat_error;
1664 return isl_stat_ok;
1667 /* Modify the maps in "umap" based on "control".
1668 * If control->inplace is set, then modify the maps in "umap" in-place.
1669 * Otherwise, create a new union map to hold the results.
1670 * If control->total is set, then perform an inplace computation
1671 * if "umap" is only referenced once. Otherwise, create a new union map
1672 * to store the results.
1674 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1675 struct isl_un_op_control *control)
1677 struct isl_union_map_un_data data = { control };
1679 if (!umap)
1680 return NULL;
1681 if ((control->inplace || control->total) && control->filter)
1682 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
1683 "inplace/total modification cannot be filtered",
1684 return isl_union_map_free(umap));
1686 if (control->total && umap->ref == 1)
1687 control->inplace = 1;
1688 if (control->inplace) {
1689 data.res = umap;
1690 } else {
1691 isl_space *space;
1693 space = isl_union_map_get_space(umap);
1694 data.res = isl_union_map_alloc(space, umap->table.n);
1696 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1697 &umap->table, &un_entry, &data) < 0)
1698 data.res = isl_union_map_free(data.res);
1700 if (control->inplace)
1701 return data.res;
1702 isl_union_map_free(umap);
1703 return data.res;
1706 __isl_give isl_union_map *isl_union_map_from_range(
1707 __isl_take isl_union_set *uset)
1709 struct isl_un_op_control control = {
1710 .fn_map = &isl_map_from_range,
1712 return un_op(uset, &control);
1715 __isl_give isl_union_map *isl_union_map_from_domain(
1716 __isl_take isl_union_set *uset)
1718 return isl_union_map_reverse(isl_union_map_from_range(uset));
1721 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1722 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1724 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1725 isl_union_map_from_range(range));
1728 /* Modify the maps in "umap" by applying "fn" on them.
1729 * "fn" should apply to all maps in "umap" and should not modify the space.
1731 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1732 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1734 struct isl_un_op_control control = {
1735 .total = 1,
1736 .fn_map = fn,
1739 return un_op(umap, &control);
1742 /* Compute the affine hull of "map" and return the result as an isl_map.
1744 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1746 return isl_map_from_basic_map(isl_map_affine_hull(map));
1749 __isl_give isl_union_map *isl_union_map_affine_hull(
1750 __isl_take isl_union_map *umap)
1752 return total(umap, &isl_map_affine_hull_map);
1755 __isl_give isl_union_set *isl_union_set_affine_hull(
1756 __isl_take isl_union_set *uset)
1758 return isl_union_map_affine_hull(uset);
1761 /* Wrapper around isl_set_combined_lineality_space
1762 * that returns the combined lineality space in the form of an isl_set
1763 * instead of an isl_basic_set.
1765 static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
1767 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
1770 /* For each set in "uset", compute the (linear) hull
1771 * of the lineality spaces of its basic sets and
1772 * collect and return the results.
1774 __isl_give isl_union_set *isl_union_set_combined_lineality_space(
1775 __isl_take isl_union_set *uset)
1777 struct isl_un_op_control control = {
1778 .fn_map = &combined_lineality_space,
1780 return un_op(uset, &control);
1783 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1785 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1787 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1790 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1791 __isl_take isl_union_map *umap)
1793 return total(umap, &isl_map_polyhedral_hull_map);
1796 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1797 __isl_take isl_union_set *uset)
1799 return isl_union_map_polyhedral_hull(uset);
1802 /* Compute a superset of the convex hull of "map" that is described
1803 * by only translates of the constraints in the constituents of "map" and
1804 * return the result as an isl_map.
1806 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1808 return isl_map_from_basic_map(isl_map_simple_hull(map));
1811 __isl_give isl_union_map *isl_union_map_simple_hull(
1812 __isl_take isl_union_map *umap)
1814 return total(umap, &isl_map_simple_hull_map);
1817 __isl_give isl_union_set *isl_union_set_simple_hull(
1818 __isl_take isl_union_set *uset)
1820 return isl_union_map_simple_hull(uset);
1823 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1824 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1826 struct isl_un_op_control control = {
1827 .inplace = 1,
1828 .fn_map = fn,
1831 return un_op(umap, &control);
1834 /* Remove redundant constraints in each of the basic maps of "umap".
1835 * Since removing redundant constraints does not change the meaning
1836 * or the space, the operation can be performed in-place.
1838 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1839 __isl_take isl_union_map *umap)
1841 return inplace(umap, &isl_map_remove_redundancies);
1844 /* Remove redundant constraints in each of the basic sets of "uset".
1846 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1847 __isl_take isl_union_set *uset)
1849 return isl_union_map_remove_redundancies(uset);
1852 __isl_give isl_union_map *isl_union_map_coalesce(
1853 __isl_take isl_union_map *umap)
1855 return inplace(umap, &isl_map_coalesce);
1858 __isl_give isl_union_set *isl_union_set_coalesce(
1859 __isl_take isl_union_set *uset)
1861 return isl_union_map_coalesce(uset);
1864 __isl_give isl_union_map *isl_union_map_detect_equalities(
1865 __isl_take isl_union_map *umap)
1867 return inplace(umap, &isl_map_detect_equalities);
1870 __isl_give isl_union_set *isl_union_set_detect_equalities(
1871 __isl_take isl_union_set *uset)
1873 return isl_union_map_detect_equalities(uset);
1876 __isl_give isl_union_map *isl_union_map_compute_divs(
1877 __isl_take isl_union_map *umap)
1879 return inplace(umap, &isl_map_compute_divs);
1882 __isl_give isl_union_set *isl_union_set_compute_divs(
1883 __isl_take isl_union_set *uset)
1885 return isl_union_map_compute_divs(uset);
1888 __isl_give isl_union_map *isl_union_map_lexmin(
1889 __isl_take isl_union_map *umap)
1891 return total(umap, &isl_map_lexmin);
1894 __isl_give isl_union_set *isl_union_set_lexmin(
1895 __isl_take isl_union_set *uset)
1897 return isl_union_map_lexmin(uset);
1900 __isl_give isl_union_map *isl_union_map_lexmax(
1901 __isl_take isl_union_map *umap)
1903 return total(umap, &isl_map_lexmax);
1906 __isl_give isl_union_set *isl_union_set_lexmax(
1907 __isl_take isl_union_set *uset)
1909 return isl_union_map_lexmax(uset);
1912 /* Return the universe in the space of "map".
1914 static __isl_give isl_map *universe(__isl_take isl_map *map)
1916 isl_space *space;
1918 space = isl_map_get_space(map);
1919 isl_map_free(map);
1920 return isl_map_universe(space);
1923 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1925 struct isl_un_op_control control = {
1926 .fn_map = &universe,
1928 return un_op(umap, &control);
1931 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1933 return isl_union_map_universe(uset);
1936 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1938 struct isl_un_op_control control = {
1939 .fn_map = &isl_map_reverse,
1941 return un_op(umap, &control);
1944 /* Compute the parameter domain of the given union map.
1946 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1948 struct isl_un_op_control control = {
1949 .fn_map = &isl_map_params,
1951 int empty;
1953 empty = isl_union_map_is_empty(umap);
1954 if (empty < 0)
1955 goto error;
1956 if (empty) {
1957 isl_space *space;
1958 space = isl_union_map_get_space(umap);
1959 isl_union_map_free(umap);
1960 return isl_set_empty(space);
1962 return isl_set_from_union_set(un_op(umap, &control));
1963 error:
1964 isl_union_map_free(umap);
1965 return NULL;
1968 /* Compute the parameter domain of the given union set.
1970 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1972 return isl_union_map_params(uset);
1975 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1977 struct isl_un_op_control control = {
1978 .fn_map = &isl_map_domain,
1980 return un_op(umap, &control);
1983 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1985 struct isl_un_op_control control = {
1986 .fn_map = &isl_map_range,
1988 return un_op(umap, &control);
1991 __isl_give isl_union_map *isl_union_map_domain_map(
1992 __isl_take isl_union_map *umap)
1994 struct isl_un_op_control control = {
1995 .fn_map = &isl_map_domain_map,
1997 return un_op(umap, &control);
2000 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
2001 * add the result to "res".
2003 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
2005 isl_union_pw_multi_aff **res = user;
2006 isl_multi_aff *ma;
2007 isl_pw_multi_aff *pma;
2009 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
2010 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
2011 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2013 return *res ? isl_stat_ok : isl_stat_error;
2017 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2018 * to its domain.
2020 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2021 __isl_take isl_union_map *umap)
2023 isl_union_pw_multi_aff *res;
2025 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2026 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2027 res = isl_union_pw_multi_aff_free(res);
2029 isl_union_map_free(umap);
2030 return res;
2033 __isl_give isl_union_map *isl_union_map_range_map(
2034 __isl_take isl_union_map *umap)
2036 struct isl_un_op_control control = {
2037 .fn_map = &isl_map_range_map,
2039 return un_op(umap, &control);
2042 /* Given a collection of wrapped maps of the form A[B -> C],
2043 * return the collection of maps A[B -> C] -> B.
2045 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2046 __isl_take isl_union_set *uset)
2048 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2049 struct isl_un_op_control control = {
2050 .filter = &un_op_filter_drop_user,
2051 .filter_user = &data,
2052 .fn_map = &isl_set_wrapped_domain_map,
2054 return un_op(uset, &control);
2057 /* Does "map" relate elements from the same space?
2059 static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2061 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2062 map->dim, isl_dim_out);
2065 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2067 struct isl_un_op_control control = {
2068 .filter = &equal_tuples,
2069 .fn_map = &isl_map_deltas,
2071 return un_op(umap, &control);
2074 __isl_give isl_union_map *isl_union_map_deltas_map(
2075 __isl_take isl_union_map *umap)
2077 struct isl_un_op_control control = {
2078 .filter = &equal_tuples,
2079 .fn_map = &isl_map_deltas_map,
2081 return un_op(umap, &control);
2084 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2086 struct isl_un_op_control control = {
2087 .fn_map = &isl_set_identity,
2089 return un_op(uset, &control);
2092 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2094 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2096 isl_union_pw_multi_aff **res = user;
2097 isl_space *space;
2098 isl_pw_multi_aff *pma;
2100 space = isl_space_map_from_set(isl_set_get_space(set));
2101 pma = isl_pw_multi_aff_identity(space);
2102 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2103 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2105 return *res ? isl_stat_ok : isl_stat_error;
2108 /* Return an identity function on "uset" in the form
2109 * of an isl_union_pw_multi_aff.
2111 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2112 __isl_take isl_union_set *uset)
2114 isl_union_pw_multi_aff *res;
2116 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2117 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2118 res = isl_union_pw_multi_aff_free(res);
2120 isl_union_set_free(uset);
2121 return res;
2124 /* For each map in "umap" of the form [A -> B] -> C,
2125 * construct the map A -> C and collect the results.
2127 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2128 __isl_take isl_union_map *umap)
2130 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2131 struct isl_un_op_control control = {
2132 .filter = &un_op_filter_drop_user,
2133 .filter_user = &data,
2134 .fn_map = &isl_map_domain_factor_domain,
2136 return un_op(umap, &control);
2139 /* For each map in "umap" of the form [A -> B] -> C,
2140 * construct the map B -> C and collect the results.
2142 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2143 __isl_take isl_union_map *umap)
2145 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2146 struct isl_un_op_control control = {
2147 .filter = &un_op_filter_drop_user,
2148 .filter_user = &data,
2149 .fn_map = &isl_map_domain_factor_range,
2151 return un_op(umap, &control);
2154 /* For each map in "umap" of the form A -> [B -> C],
2155 * construct the map A -> B and collect the results.
2157 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2158 __isl_take isl_union_map *umap)
2160 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2161 struct isl_un_op_control control = {
2162 .filter = &un_op_filter_drop_user,
2163 .filter_user = &data,
2164 .fn_map = &isl_map_range_factor_domain,
2166 return un_op(umap, &control);
2169 /* For each map in "umap" of the form A -> [B -> C],
2170 * construct the map A -> C and collect the results.
2172 __isl_give isl_union_map *isl_union_map_range_factor_range(
2173 __isl_take isl_union_map *umap)
2175 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2176 struct isl_un_op_control control = {
2177 .filter = &un_op_filter_drop_user,
2178 .filter_user = &data,
2179 .fn_map = &isl_map_range_factor_range,
2181 return un_op(umap, &control);
2184 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2185 * construct the map A -> C and collect the results.
2187 __isl_give isl_union_map *isl_union_map_factor_domain(
2188 __isl_take isl_union_map *umap)
2190 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2191 struct isl_un_op_control control = {
2192 .filter = &un_op_filter_drop_user,
2193 .filter_user = &data,
2194 .fn_map = &isl_map_factor_domain,
2196 return un_op(umap, &control);
2199 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2200 * construct the map B -> D and collect the results.
2202 __isl_give isl_union_map *isl_union_map_factor_range(
2203 __isl_take isl_union_map *umap)
2205 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2206 struct isl_un_op_control control = {
2207 .filter = &un_op_filter_drop_user,
2208 .filter_user = &data,
2209 .fn_map = &isl_map_factor_range,
2211 return un_op(umap, &control);
2214 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2216 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2217 struct isl_un_op_control control = {
2218 .filter = &un_op_filter_drop_user,
2219 .filter_user = &data,
2220 .fn_map = &isl_set_unwrap,
2222 return un_op(uset, &control);
2225 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2227 struct isl_un_op_control control = {
2228 .fn_map = &isl_map_wrap,
2230 return un_op(umap, &control);
2233 struct isl_union_map_is_subset_data {
2234 isl_union_map *umap2;
2235 isl_bool is_subset;
2238 static isl_stat is_subset_entry(void **entry, void *user)
2240 struct isl_union_map_is_subset_data *data = user;
2241 uint32_t hash;
2242 struct isl_hash_table_entry *entry2;
2243 isl_map *map = *entry;
2245 hash = isl_space_get_hash(map->dim);
2246 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2247 hash, &has_space, map->dim, 0);
2248 if (!entry2) {
2249 int empty = isl_map_is_empty(map);
2250 if (empty < 0)
2251 return isl_stat_error;
2252 if (empty)
2253 return isl_stat_ok;
2254 data->is_subset = 0;
2255 return isl_stat_error;
2258 data->is_subset = isl_map_is_subset(map, entry2->data);
2259 if (data->is_subset < 0 || !data->is_subset)
2260 return isl_stat_error;
2262 return isl_stat_ok;
2265 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2266 __isl_keep isl_union_map *umap2)
2268 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2270 umap1 = isl_union_map_copy(umap1);
2271 umap2 = isl_union_map_copy(umap2);
2272 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2273 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2275 if (!umap1 || !umap2)
2276 goto error;
2278 data.umap2 = umap2;
2279 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2280 &is_subset_entry, &data) < 0 &&
2281 data.is_subset)
2282 goto error;
2284 isl_union_map_free(umap1);
2285 isl_union_map_free(umap2);
2287 return data.is_subset;
2288 error:
2289 isl_union_map_free(umap1);
2290 isl_union_map_free(umap2);
2291 return isl_bool_error;
2294 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2295 __isl_keep isl_union_set *uset2)
2297 return isl_union_map_is_subset(uset1, uset2);
2300 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2301 __isl_keep isl_union_map *umap2)
2303 isl_bool is_subset;
2305 if (!umap1 || !umap2)
2306 return isl_bool_error;
2307 is_subset = isl_union_map_is_subset(umap1, umap2);
2308 if (is_subset != isl_bool_true)
2309 return is_subset;
2310 is_subset = isl_union_map_is_subset(umap2, umap1);
2311 return is_subset;
2314 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2315 __isl_keep isl_union_set *uset2)
2317 return isl_union_map_is_equal(uset1, uset2);
2320 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2321 __isl_keep isl_union_map *umap2)
2323 isl_bool is_subset;
2325 if (!umap1 || !umap2)
2326 return isl_bool_error;
2327 is_subset = isl_union_map_is_subset(umap1, umap2);
2328 if (is_subset != isl_bool_true)
2329 return is_subset;
2330 is_subset = isl_union_map_is_subset(umap2, umap1);
2331 if (is_subset == isl_bool_error)
2332 return is_subset;
2333 return !is_subset;
2336 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2337 __isl_keep isl_union_set *uset2)
2339 return isl_union_map_is_strict_subset(uset1, uset2);
2342 /* Internal data structure for isl_union_map_is_disjoint.
2343 * umap2 is the union map with which we are comparing.
2344 * is_disjoint is initialized to 1 and is set to 0 as soon
2345 * as the union maps turn out not to be disjoint.
2347 struct isl_union_map_is_disjoint_data {
2348 isl_union_map *umap2;
2349 isl_bool is_disjoint;
2352 /* Check if "map" is disjoint from data->umap2 and abort
2353 * the search if it is not.
2355 static isl_stat is_disjoint_entry(void **entry, void *user)
2357 struct isl_union_map_is_disjoint_data *data = user;
2358 uint32_t hash;
2359 struct isl_hash_table_entry *entry2;
2360 isl_map *map = *entry;
2362 hash = isl_space_get_hash(map->dim);
2363 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2364 hash, &has_space, map->dim, 0);
2365 if (!entry2)
2366 return isl_stat_ok;
2368 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2369 if (data->is_disjoint < 0 || !data->is_disjoint)
2370 return isl_stat_error;
2372 return isl_stat_ok;
2375 /* Are "umap1" and "umap2" disjoint?
2377 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2378 __isl_keep isl_union_map *umap2)
2380 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2382 umap1 = isl_union_map_copy(umap1);
2383 umap2 = isl_union_map_copy(umap2);
2384 umap1 = isl_union_map_align_params(umap1,
2385 isl_union_map_get_space(umap2));
2386 umap2 = isl_union_map_align_params(umap2,
2387 isl_union_map_get_space(umap1));
2389 if (!umap1 || !umap2)
2390 goto error;
2392 data.umap2 = umap2;
2393 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2394 &is_disjoint_entry, &data) < 0 &&
2395 data.is_disjoint)
2396 goto error;
2398 isl_union_map_free(umap1);
2399 isl_union_map_free(umap2);
2401 return data.is_disjoint;
2402 error:
2403 isl_union_map_free(umap1);
2404 isl_union_map_free(umap2);
2405 return isl_bool_error;
2408 /* Are "uset1" and "uset2" disjoint?
2410 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2411 __isl_keep isl_union_set *uset2)
2413 return isl_union_map_is_disjoint(uset1, uset2);
2416 static isl_stat sample_entry(void **entry, void *user)
2418 isl_basic_map **sample = (isl_basic_map **)user;
2419 isl_map *map = *entry;
2421 *sample = isl_map_sample(isl_map_copy(map));
2422 if (!*sample)
2423 return isl_stat_error;
2424 if (!isl_basic_map_plain_is_empty(*sample))
2425 return isl_stat_error;
2426 return isl_stat_ok;
2429 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2431 isl_basic_map *sample = NULL;
2433 if (!umap)
2434 return NULL;
2436 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2437 &sample_entry, &sample) < 0 &&
2438 !sample)
2439 goto error;
2441 if (!sample)
2442 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2444 isl_union_map_free(umap);
2446 return sample;
2447 error:
2448 isl_union_map_free(umap);
2449 return NULL;
2452 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2454 return bset_from_bmap(isl_union_map_sample(uset));
2457 /* Return an element in "uset" in the form of an isl_point.
2458 * Return a void isl_point if "uset" is empty.
2460 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2462 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2465 struct isl_forall_data {
2466 isl_bool res;
2467 isl_bool (*fn)(__isl_keep isl_map *map);
2470 static isl_stat forall_entry(void **entry, void *user)
2472 struct isl_forall_data *data = user;
2473 isl_map *map = *entry;
2475 data->res = data->fn(map);
2476 if (data->res < 0)
2477 return isl_stat_error;
2479 if (!data->res)
2480 return isl_stat_error;
2482 return isl_stat_ok;
2485 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2486 isl_bool (*fn)(__isl_keep isl_map *map))
2488 struct isl_forall_data data = { isl_bool_true, fn };
2490 if (!umap)
2491 return isl_bool_error;
2493 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2494 &forall_entry, &data) < 0 && data.res)
2495 return isl_bool_error;
2497 return data.res;
2500 struct isl_forall_user_data {
2501 isl_bool res;
2502 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2503 void *user;
2506 static isl_stat forall_user_entry(void **entry, void *user)
2508 struct isl_forall_user_data *data = user;
2509 isl_map *map = *entry;
2511 data->res = data->fn(map, data->user);
2512 if (data->res < 0)
2513 return isl_stat_error;
2515 if (!data->res)
2516 return isl_stat_error;
2518 return isl_stat_ok;
2521 /* Check if fn(map, user) returns true for all maps "map" in umap.
2523 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2524 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2526 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2528 if (!umap)
2529 return isl_bool_error;
2531 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2532 &forall_user_entry, &data) < 0 && data.res)
2533 return isl_bool_error;
2535 return data.res;
2538 /* Is "umap" obviously empty?
2540 isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2542 if (!umap)
2543 return isl_bool_error;
2544 return isl_union_map_n_map(umap) == 0;
2547 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2549 return union_map_forall(umap, &isl_map_is_empty);
2552 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2554 return isl_union_map_is_empty(uset);
2557 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2559 isl_bool is_subset;
2560 isl_space *dim;
2561 isl_map *id;
2563 if (!map)
2564 return isl_bool_error;
2566 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2567 map->dim, isl_dim_out))
2568 return isl_bool_false;
2570 dim = isl_map_get_space(map);
2571 id = isl_map_identity(dim);
2573 is_subset = isl_map_is_subset(map, id);
2575 isl_map_free(id);
2577 return is_subset;
2580 /* Given an isl_union_map that consists of a single map, check
2581 * if it is single-valued.
2583 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2585 isl_map *map;
2586 isl_bool sv;
2588 umap = isl_union_map_copy(umap);
2589 map = isl_map_from_union_map(umap);
2590 sv = isl_map_is_single_valued(map);
2591 isl_map_free(map);
2593 return sv;
2596 /* Internal data structure for single_valued_on_domain.
2598 * "umap" is the union map to be tested.
2599 * "sv" is set to 1 as long as "umap" may still be single-valued.
2601 struct isl_union_map_is_sv_data {
2602 isl_union_map *umap;
2603 isl_bool sv;
2606 /* Check if the data->umap is single-valued on "set".
2608 * If data->umap consists of a single map on "set", then test it
2609 * as an isl_map.
2611 * Otherwise, compute
2613 * M \circ M^-1
2615 * check if the result is a subset of the identity mapping and
2616 * store the result in data->sv.
2618 * Terminate as soon as data->umap has been determined not to
2619 * be single-valued.
2621 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2623 struct isl_union_map_is_sv_data *data = user;
2624 isl_union_map *umap, *test;
2626 umap = isl_union_map_copy(data->umap);
2627 umap = isl_union_map_intersect_domain(umap,
2628 isl_union_set_from_set(set));
2630 if (isl_union_map_n_map(umap) == 1) {
2631 data->sv = single_map_is_single_valued(umap);
2632 isl_union_map_free(umap);
2633 } else {
2634 test = isl_union_map_reverse(isl_union_map_copy(umap));
2635 test = isl_union_map_apply_range(test, umap);
2637 data->sv = union_map_forall(test, &is_subset_of_identity);
2639 isl_union_map_free(test);
2642 if (data->sv < 0 || !data->sv)
2643 return isl_stat_error;
2644 return isl_stat_ok;
2647 /* Check if the given map is single-valued.
2649 * If the union map consists of a single map, then test it as an isl_map.
2650 * Otherwise, check if the union map is single-valued on each of its
2651 * domain spaces.
2653 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2655 isl_union_map *universe;
2656 isl_union_set *domain;
2657 struct isl_union_map_is_sv_data data;
2659 if (isl_union_map_n_map(umap) == 1)
2660 return single_map_is_single_valued(umap);
2662 universe = isl_union_map_universe(isl_union_map_copy(umap));
2663 domain = isl_union_map_domain(universe);
2665 data.sv = isl_bool_true;
2666 data.umap = umap;
2667 if (isl_union_set_foreach_set(domain,
2668 &single_valued_on_domain, &data) < 0 && data.sv)
2669 data.sv = isl_bool_error;
2670 isl_union_set_free(domain);
2672 return data.sv;
2675 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2677 isl_bool in;
2679 umap = isl_union_map_copy(umap);
2680 umap = isl_union_map_reverse(umap);
2681 in = isl_union_map_is_single_valued(umap);
2682 isl_union_map_free(umap);
2684 return in;
2687 /* Is "map" obviously not an identity relation because
2688 * it maps elements from one space to another space?
2689 * Update *non_identity accordingly.
2691 * In particular, if the domain and range spaces are the same,
2692 * then the map is not considered to obviously not be an identity relation.
2693 * Otherwise, the map is considered to obviously not be an identity relation
2694 * if it is is non-empty.
2696 * If "map" is determined to obviously not be an identity relation,
2697 * then the search is aborted.
2699 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2701 isl_bool *non_identity = user;
2702 isl_bool equal;
2703 isl_space *space;
2705 space = isl_map_get_space(map);
2706 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2707 if (equal >= 0 && !equal)
2708 *non_identity = isl_bool_not(isl_map_is_empty(map));
2709 else
2710 *non_identity = isl_bool_not(equal);
2711 isl_space_free(space);
2712 isl_map_free(map);
2714 if (*non_identity < 0 || *non_identity)
2715 return isl_stat_error;
2717 return isl_stat_ok;
2720 /* Is "umap" obviously not an identity relation because
2721 * it maps elements from one space to another space?
2723 * As soon as a map has been found that maps elements to a different space,
2724 * non_identity is changed and the search is aborted.
2726 static isl_bool isl_union_map_plain_is_not_identity(
2727 __isl_keep isl_union_map *umap)
2729 isl_bool non_identity;
2731 non_identity = isl_bool_false;
2732 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2733 &non_identity) < 0 &&
2734 non_identity == isl_bool_false)
2735 return isl_bool_error;
2737 return non_identity;
2740 /* Does "map" only map elements to themselves?
2741 * Update *identity accordingly.
2743 * If "map" is determined not to be an identity relation,
2744 * then the search is aborted.
2746 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2748 isl_bool *identity = user;
2750 *identity = isl_map_is_identity(map);
2751 isl_map_free(map);
2753 if (*identity < 0 || !*identity)
2754 return isl_stat_error;
2756 return isl_stat_ok;
2759 /* Does "umap" only map elements to themselves?
2761 * First check if there are any maps that map elements to different spaces.
2762 * If not, then check that all the maps (between identical spaces)
2763 * are identity relations.
2765 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2767 isl_bool non_identity;
2768 isl_bool identity;
2770 non_identity = isl_union_map_plain_is_not_identity(umap);
2771 if (non_identity < 0 || non_identity)
2772 return isl_bool_not(non_identity);
2774 identity = isl_bool_true;
2775 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2776 identity == isl_bool_true)
2777 return isl_bool_error;
2779 return identity;
2782 /* Represents a map that has a fixed value (v) for one of its
2783 * range dimensions.
2784 * The map in this structure is not reference counted, so it
2785 * is only valid while the isl_union_map from which it was
2786 * obtained is still alive.
2788 struct isl_fixed_map {
2789 isl_int v;
2790 isl_map *map;
2793 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2794 int n)
2796 int i;
2797 struct isl_fixed_map *v;
2799 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2800 if (!v)
2801 return NULL;
2802 for (i = 0; i < n; ++i)
2803 isl_int_init(v[i].v);
2804 return v;
2807 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2809 int i;
2811 if (!v)
2812 return;
2813 for (i = 0; i < n; ++i)
2814 isl_int_clear(v[i].v);
2815 free(v);
2818 /* Compare the "v" field of two isl_fixed_map structs.
2820 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2822 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2823 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2825 return isl_int_cmp(e1->v, e2->v);
2828 /* Internal data structure used while checking whether all maps
2829 * in a union_map have a fixed value for a given output dimension.
2830 * v is the list of maps, with the fixed value for the dimension
2831 * n is the number of maps considered so far
2832 * pos is the output dimension under investigation
2834 struct isl_fixed_dim_data {
2835 struct isl_fixed_map *v;
2836 int n;
2837 int pos;
2840 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2842 struct isl_fixed_dim_data *data = user;
2844 data->v[data->n].map = map;
2845 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2846 &data->v[data->n++].v);
2849 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2850 int first, int n_range);
2852 /* Given a list of the maps, with their fixed values at output dimension "pos",
2853 * check whether the ranges of the maps form an obvious partition.
2855 * We first sort the maps according to their fixed values.
2856 * If all maps have a different value, then we know the ranges form
2857 * a partition.
2858 * Otherwise, we collect the maps with the same fixed value and
2859 * check whether each such collection is obviously injective
2860 * based on later dimensions.
2862 static int separates(struct isl_fixed_map *v, int n,
2863 __isl_take isl_space *dim, int pos, int n_range)
2865 int i;
2867 if (!v)
2868 goto error;
2870 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2872 for (i = 0; i + 1 < n; ++i) {
2873 int j, k;
2874 isl_union_map *part;
2875 int injective;
2877 for (j = i + 1; j < n; ++j)
2878 if (isl_int_ne(v[i].v, v[j].v))
2879 break;
2881 if (j == i + 1)
2882 continue;
2884 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2885 for (k = i; k < j; ++k)
2886 part = isl_union_map_add_map(part,
2887 isl_map_copy(v[k].map));
2889 injective = plain_injective_on_range(part, pos + 1, n_range);
2890 if (injective < 0)
2891 goto error;
2892 if (!injective)
2893 break;
2895 i = j - 1;
2898 isl_space_free(dim);
2899 free_isl_fixed_map_array(v, n);
2900 return i + 1 >= n;
2901 error:
2902 isl_space_free(dim);
2903 free_isl_fixed_map_array(v, n);
2904 return -1;
2907 /* Check whether the maps in umap have obviously distinct ranges.
2908 * In particular, check for an output dimension in the range
2909 * [first,n_range) for which all maps have a fixed value
2910 * and then check if these values, possibly along with fixed values
2911 * at later dimensions, entail distinct ranges.
2913 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2914 int first, int n_range)
2916 isl_ctx *ctx;
2917 int n;
2918 struct isl_fixed_dim_data data = { NULL };
2920 ctx = isl_union_map_get_ctx(umap);
2922 n = isl_union_map_n_map(umap);
2923 if (!umap)
2924 goto error;
2926 if (n <= 1) {
2927 isl_union_map_free(umap);
2928 return isl_bool_true;
2931 if (first >= n_range) {
2932 isl_union_map_free(umap);
2933 return isl_bool_false;
2936 data.v = alloc_isl_fixed_map_array(ctx, n);
2937 if (!data.v)
2938 goto error;
2940 for (data.pos = first; data.pos < n_range; ++data.pos) {
2941 isl_bool fixed;
2942 int injective;
2943 isl_space *dim;
2945 data.n = 0;
2946 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2947 if (fixed < 0)
2948 goto error;
2949 if (!fixed)
2950 continue;
2951 dim = isl_union_map_get_space(umap);
2952 injective = separates(data.v, n, dim, data.pos, n_range);
2953 isl_union_map_free(umap);
2954 return injective;
2957 free_isl_fixed_map_array(data.v, n);
2958 isl_union_map_free(umap);
2960 return isl_bool_false;
2961 error:
2962 free_isl_fixed_map_array(data.v, n);
2963 isl_union_map_free(umap);
2964 return isl_bool_error;
2967 /* Check whether the maps in umap that map to subsets of "ran"
2968 * have obviously distinct ranges.
2970 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
2971 void *user)
2973 isl_union_map *umap = user;
2975 umap = isl_union_map_copy(umap);
2976 umap = isl_union_map_intersect_range(umap,
2977 isl_union_set_from_set(isl_set_copy(ran)));
2978 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2981 /* Check if the given union_map is obviously injective.
2983 * In particular, we first check if all individual maps are obviously
2984 * injective and then check if all the ranges of these maps are
2985 * obviously disjoint.
2987 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2989 isl_bool in;
2990 isl_union_map *univ;
2991 isl_union_set *ran;
2993 in = union_map_forall(umap, &isl_map_plain_is_injective);
2994 if (in < 0)
2995 return isl_bool_error;
2996 if (!in)
2997 return isl_bool_false;
2999 univ = isl_union_map_universe(isl_union_map_copy(umap));
3000 ran = isl_union_map_range(univ);
3002 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
3004 isl_union_set_free(ran);
3006 return in;
3009 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
3011 isl_bool sv;
3013 sv = isl_union_map_is_single_valued(umap);
3014 if (sv < 0 || !sv)
3015 return sv;
3017 return isl_union_map_is_injective(umap);
3020 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3022 struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3023 struct isl_un_op_control control = {
3024 .filter = &un_op_filter_drop_user,
3025 .filter_user = &data,
3026 .fn_map = &isl_map_zip,
3028 return un_op(umap, &control);
3031 /* Given a union map, take the maps of the form A -> (B -> C) and
3032 * return the union of the corresponding maps (A -> B) -> C.
3034 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3036 struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3037 struct isl_un_op_control control = {
3038 .filter = &un_op_filter_drop_user,
3039 .filter_user = &data,
3040 .fn_map = &isl_map_uncurry,
3042 return un_op(umap, &control);
3045 /* Given a union map, take the maps of the form (A -> B) -> C and
3046 * return the union of the corresponding maps A -> (B -> C).
3048 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3050 struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3051 struct isl_un_op_control control = {
3052 .filter = &un_op_filter_drop_user,
3053 .filter_user = &data,
3054 .fn_map = &isl_map_curry,
3056 return un_op(umap, &control);
3059 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3060 * return the union of the corresponding maps A -> (B -> (C -> D)).
3062 __isl_give isl_union_map *isl_union_map_range_curry(
3063 __isl_take isl_union_map *umap)
3065 struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3066 struct isl_un_op_control control = {
3067 .filter = &un_op_filter_drop_user,
3068 .filter_user = &data,
3069 .fn_map = &isl_map_range_curry,
3071 return un_op(umap, &control);
3074 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3076 struct isl_un_op_control control = {
3077 .fn_map = &isl_set_lift,
3079 return un_op(uset, &control);
3082 static isl_stat coefficients_entry(void **entry, void *user)
3084 isl_set *set = *entry;
3085 isl_union_set **res = user;
3087 set = isl_set_copy(set);
3088 set = isl_set_from_basic_set(isl_set_coefficients(set));
3089 *res = isl_union_set_add_set(*res, set);
3091 return isl_stat_ok;
3094 __isl_give isl_union_set *isl_union_set_coefficients(
3095 __isl_take isl_union_set *uset)
3097 isl_ctx *ctx;
3098 isl_space *dim;
3099 isl_union_set *res;
3101 if (!uset)
3102 return NULL;
3104 ctx = isl_union_set_get_ctx(uset);
3105 dim = isl_space_set_alloc(ctx, 0, 0);
3106 res = isl_union_map_alloc(dim, uset->table.n);
3107 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3108 &coefficients_entry, &res) < 0)
3109 goto error;
3111 isl_union_set_free(uset);
3112 return res;
3113 error:
3114 isl_union_set_free(uset);
3115 isl_union_set_free(res);
3116 return NULL;
3119 static isl_stat solutions_entry(void **entry, void *user)
3121 isl_set *set = *entry;
3122 isl_union_set **res = user;
3124 set = isl_set_copy(set);
3125 set = isl_set_from_basic_set(isl_set_solutions(set));
3126 if (!*res)
3127 *res = isl_union_set_from_set(set);
3128 else
3129 *res = isl_union_set_add_set(*res, set);
3131 if (!*res)
3132 return isl_stat_error;
3134 return isl_stat_ok;
3137 __isl_give isl_union_set *isl_union_set_solutions(
3138 __isl_take isl_union_set *uset)
3140 isl_union_set *res = NULL;
3142 if (!uset)
3143 return NULL;
3145 if (uset->table.n == 0) {
3146 res = isl_union_set_empty(isl_union_set_get_space(uset));
3147 isl_union_set_free(uset);
3148 return res;
3151 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3152 &solutions_entry, &res) < 0)
3153 goto error;
3155 isl_union_set_free(uset);
3156 return res;
3157 error:
3158 isl_union_set_free(uset);
3159 isl_union_set_free(res);
3160 return NULL;
3163 /* Is the domain space of "map" equal to "space"?
3165 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3167 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3168 space, isl_dim_out);
3171 /* Is the range space of "map" equal to "space"?
3173 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3175 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3176 space, isl_dim_out);
3179 /* Is the set space of "map" equal to "space"?
3181 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3183 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3184 space, isl_dim_out);
3187 /* Internal data structure for preimage_pw_multi_aff.
3189 * "pma" is the function under which the preimage should be taken.
3190 * "space" is the space of "pma".
3191 * "res" collects the results.
3192 * "fn" computes the preimage for a given map.
3193 * "match" returns true if "fn" can be called.
3195 struct isl_union_map_preimage_data {
3196 isl_space *space;
3197 isl_pw_multi_aff *pma;
3198 isl_union_map *res;
3199 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3200 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3201 __isl_take isl_pw_multi_aff *pma);
3204 /* Call data->fn to compute the preimage of the domain or range of *entry
3205 * under the function represented by data->pma, provided the domain/range
3206 * space of *entry matches the target space of data->pma
3207 * (as given by data->match), and add the result to data->res.
3209 static isl_stat preimage_entry(void **entry, void *user)
3211 int m;
3212 isl_map *map = *entry;
3213 struct isl_union_map_preimage_data *data = user;
3214 isl_bool empty;
3216 m = data->match(map, data->space);
3217 if (m < 0)
3218 return isl_stat_error;
3219 if (!m)
3220 return isl_stat_ok;
3222 map = isl_map_copy(map);
3223 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3225 empty = isl_map_is_empty(map);
3226 if (empty < 0 || empty) {
3227 isl_map_free(map);
3228 return empty < 0 ? isl_stat_error : isl_stat_ok;
3231 data->res = isl_union_map_add_map(data->res, map);
3233 return isl_stat_ok;
3236 /* Compute the preimage of the domain or range of "umap" under the function
3237 * represented by "pma".
3238 * In other words, plug in "pma" in the domain or range of "umap".
3239 * The function "fn" performs the actual preimage computation on a map,
3240 * while "match" determines to which maps the function should be applied.
3242 static __isl_give isl_union_map *preimage_pw_multi_aff(
3243 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3244 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3245 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3246 __isl_take isl_pw_multi_aff *pma))
3248 isl_ctx *ctx;
3249 isl_space *space;
3250 struct isl_union_map_preimage_data data;
3252 umap = isl_union_map_align_params(umap,
3253 isl_pw_multi_aff_get_space(pma));
3254 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3256 if (!umap || !pma)
3257 goto error;
3259 ctx = isl_union_map_get_ctx(umap);
3260 space = isl_union_map_get_space(umap);
3261 data.space = isl_pw_multi_aff_get_space(pma);
3262 data.pma = pma;
3263 data.res = isl_union_map_alloc(space, umap->table.n);
3264 data.match = match;
3265 data.fn = fn;
3266 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3267 &data) < 0)
3268 data.res = isl_union_map_free(data.res);
3270 isl_space_free(data.space);
3271 isl_union_map_free(umap);
3272 isl_pw_multi_aff_free(pma);
3273 return data.res;
3274 error:
3275 isl_union_map_free(umap);
3276 isl_pw_multi_aff_free(pma);
3277 return NULL;
3280 /* Compute the preimage of the domain of "umap" under the function
3281 * represented by "pma".
3282 * In other words, plug in "pma" in the domain of "umap".
3283 * The result contains maps that live in the same spaces as the maps of "umap"
3284 * with domain space equal to the target space of "pma",
3285 * except that the domain has been replaced by the domain space of "pma".
3287 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3288 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3290 return preimage_pw_multi_aff(umap, pma, &domain_match,
3291 &isl_map_preimage_domain_pw_multi_aff);
3294 /* Compute the preimage of the range of "umap" under the function
3295 * represented by "pma".
3296 * In other words, plug in "pma" in the range of "umap".
3297 * The result contains maps that live in the same spaces as the maps of "umap"
3298 * with range space equal to the target space of "pma",
3299 * except that the range has been replaced by the domain space of "pma".
3301 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3302 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3304 return preimage_pw_multi_aff(umap, pma, &range_match,
3305 &isl_map_preimage_range_pw_multi_aff);
3308 /* Compute the preimage of "uset" under the function represented by "pma".
3309 * In other words, plug in "pma" in "uset".
3310 * The result contains sets that live in the same spaces as the sets of "uset"
3311 * with space equal to the target space of "pma",
3312 * except that the space has been replaced by the domain space of "pma".
3314 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3315 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3317 return preimage_pw_multi_aff(uset, pma, &set_match,
3318 &isl_set_preimage_pw_multi_aff);
3321 /* Compute the preimage of the domain of "umap" under the function
3322 * represented by "ma".
3323 * In other words, plug in "ma" in the domain of "umap".
3324 * The result contains maps that live in the same spaces as the maps of "umap"
3325 * with domain space equal to the target space of "ma",
3326 * except that the domain has been replaced by the domain space of "ma".
3328 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3329 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3331 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3332 isl_pw_multi_aff_from_multi_aff(ma));
3335 /* Compute the preimage of the range of "umap" under the function
3336 * represented by "ma".
3337 * In other words, plug in "ma" in the range of "umap".
3338 * The result contains maps that live in the same spaces as the maps of "umap"
3339 * with range space equal to the target space of "ma",
3340 * except that the range has been replaced by the domain space of "ma".
3342 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3343 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3345 return isl_union_map_preimage_range_pw_multi_aff(umap,
3346 isl_pw_multi_aff_from_multi_aff(ma));
3349 /* Compute the preimage of "uset" under the function represented by "ma".
3350 * In other words, plug in "ma" in "uset".
3351 * The result contains sets that live in the same spaces as the sets of "uset"
3352 * with space equal to the target space of "ma",
3353 * except that the space has been replaced by the domain space of "ma".
3355 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3356 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3358 return isl_union_set_preimage_pw_multi_aff(uset,
3359 isl_pw_multi_aff_from_multi_aff(ma));
3362 /* Internal data structure for preimage_multi_pw_aff.
3364 * "mpa" is the function under which the preimage should be taken.
3365 * "space" is the space of "mpa".
3366 * "res" collects the results.
3367 * "fn" computes the preimage for a given map.
3368 * "match" returns true if "fn" can be called.
3370 struct isl_union_map_preimage_mpa_data {
3371 isl_space *space;
3372 isl_multi_pw_aff *mpa;
3373 isl_union_map *res;
3374 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3375 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3376 __isl_take isl_multi_pw_aff *mpa);
3379 /* Call data->fn to compute the preimage of the domain or range of *entry
3380 * under the function represented by data->mpa, provided the domain/range
3381 * space of *entry matches the target space of data->mpa
3382 * (as given by data->match), and add the result to data->res.
3384 static isl_stat preimage_mpa_entry(void **entry, void *user)
3386 int m;
3387 isl_map *map = *entry;
3388 struct isl_union_map_preimage_mpa_data *data = user;
3389 isl_bool empty;
3391 m = data->match(map, data->space);
3392 if (m < 0)
3393 return isl_stat_error;
3394 if (!m)
3395 return isl_stat_ok;
3397 map = isl_map_copy(map);
3398 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3400 empty = isl_map_is_empty(map);
3401 if (empty < 0 || empty) {
3402 isl_map_free(map);
3403 return empty < 0 ? isl_stat_error : isl_stat_ok;
3406 data->res = isl_union_map_add_map(data->res, map);
3408 return isl_stat_ok;
3411 /* Compute the preimage of the domain or range of "umap" under the function
3412 * represented by "mpa".
3413 * In other words, plug in "mpa" in the domain or range of "umap".
3414 * The function "fn" performs the actual preimage computation on a map,
3415 * while "match" determines to which maps the function should be applied.
3417 static __isl_give isl_union_map *preimage_multi_pw_aff(
3418 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3419 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3420 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3421 __isl_take isl_multi_pw_aff *mpa))
3423 isl_ctx *ctx;
3424 isl_space *space;
3425 struct isl_union_map_preimage_mpa_data data;
3427 umap = isl_union_map_align_params(umap,
3428 isl_multi_pw_aff_get_space(mpa));
3429 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3431 if (!umap || !mpa)
3432 goto error;
3434 ctx = isl_union_map_get_ctx(umap);
3435 space = isl_union_map_get_space(umap);
3436 data.space = isl_multi_pw_aff_get_space(mpa);
3437 data.mpa = mpa;
3438 data.res = isl_union_map_alloc(space, umap->table.n);
3439 data.match = match;
3440 data.fn = fn;
3441 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3442 &data) < 0)
3443 data.res = isl_union_map_free(data.res);
3445 isl_space_free(data.space);
3446 isl_union_map_free(umap);
3447 isl_multi_pw_aff_free(mpa);
3448 return data.res;
3449 error:
3450 isl_union_map_free(umap);
3451 isl_multi_pw_aff_free(mpa);
3452 return NULL;
3455 /* Compute the preimage of the domain of "umap" under the function
3456 * represented by "mpa".
3457 * In other words, plug in "mpa" in the domain of "umap".
3458 * The result contains maps that live in the same spaces as the maps of "umap"
3459 * with domain space equal to the target space of "mpa",
3460 * except that the domain has been replaced by the domain space of "mpa".
3462 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3463 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3465 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3466 &isl_map_preimage_domain_multi_pw_aff);
3469 /* Internal data structure for preimage_upma.
3471 * "umap" is the map of which the preimage should be computed.
3472 * "res" collects the results.
3473 * "fn" computes the preimage for a given piecewise multi-affine function.
3475 struct isl_union_map_preimage_upma_data {
3476 isl_union_map *umap;
3477 isl_union_map *res;
3478 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3479 __isl_take isl_pw_multi_aff *pma);
3482 /* Call data->fn to compute the preimage of the domain or range of data->umap
3483 * under the function represented by pma and add the result to data->res.
3485 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3487 struct isl_union_map_preimage_upma_data *data = user;
3488 isl_union_map *umap;
3490 umap = isl_union_map_copy(data->umap);
3491 umap = data->fn(umap, pma);
3492 data->res = isl_union_map_union(data->res, umap);
3494 return data->res ? isl_stat_ok : isl_stat_error;
3497 /* Compute the preimage of the domain or range of "umap" under the function
3498 * represented by "upma".
3499 * In other words, plug in "upma" in the domain or range of "umap".
3500 * The function "fn" performs the actual preimage computation
3501 * on a piecewise multi-affine function.
3503 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3504 __isl_take isl_union_map *umap,
3505 __isl_take isl_union_pw_multi_aff *upma,
3506 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3507 __isl_take isl_pw_multi_aff *pma))
3509 struct isl_union_map_preimage_upma_data data;
3511 data.umap = umap;
3512 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3513 data.fn = fn;
3514 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3515 &preimage_upma, &data) < 0)
3516 data.res = isl_union_map_free(data.res);
3518 isl_union_map_free(umap);
3519 isl_union_pw_multi_aff_free(upma);
3521 return data.res;
3524 /* Compute the preimage of the domain of "umap" under the function
3525 * represented by "upma".
3526 * In other words, plug in "upma" in the domain of "umap".
3527 * The result contains maps that live in the same spaces as the maps of "umap"
3528 * with domain space equal to one of the target spaces of "upma",
3529 * except that the domain has been replaced by one of the domain spaces that
3530 * correspond to that target space of "upma".
3532 __isl_give isl_union_map *isl_union_map_preimage_domain_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_domain_pw_multi_aff);
3540 /* Compute the preimage of the range of "umap" under the function
3541 * represented by "upma".
3542 * In other words, plug in "upma" in the range of "umap".
3543 * The result contains maps that live in the same spaces as the maps of "umap"
3544 * with range space equal to one of the target spaces of "upma",
3545 * except that the range has been replaced by one of the domain spaces that
3546 * correspond to that target space of "upma".
3548 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3549 __isl_take isl_union_map *umap,
3550 __isl_take isl_union_pw_multi_aff *upma)
3552 return preimage_union_pw_multi_aff(umap, upma,
3553 &isl_union_map_preimage_range_pw_multi_aff);
3556 /* Compute the preimage of "uset" under the function represented by "upma".
3557 * In other words, plug in "upma" in the range of "uset".
3558 * The result contains sets that live in the same spaces as the sets of "uset"
3559 * with space equal to one of the target spaces of "upma",
3560 * except that the space has been replaced by one of the domain spaces that
3561 * correspond to that target space of "upma".
3563 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3564 __isl_take isl_union_set *uset,
3565 __isl_take isl_union_pw_multi_aff *upma)
3567 return preimage_union_pw_multi_aff(uset, upma,
3568 &isl_union_set_preimage_pw_multi_aff);
3571 /* Reset the user pointer on all identifiers of parameters and tuples
3572 * of the spaces of "umap".
3574 __isl_give isl_union_map *isl_union_map_reset_user(
3575 __isl_take isl_union_map *umap)
3577 umap = isl_union_map_cow(umap);
3578 if (!umap)
3579 return NULL;
3580 umap->dim = isl_space_reset_user(umap->dim);
3581 if (!umap->dim)
3582 return isl_union_map_free(umap);
3583 return total(umap, &isl_map_reset_user);
3586 /* Reset the user pointer on all identifiers of parameters and tuples
3587 * of the spaces of "uset".
3589 __isl_give isl_union_set *isl_union_set_reset_user(
3590 __isl_take isl_union_set *uset)
3592 return isl_union_map_reset_user(uset);
3595 /* Remove all existentially quantified variables and integer divisions
3596 * from "umap" using Fourier-Motzkin elimination.
3598 __isl_give isl_union_map *isl_union_map_remove_divs(
3599 __isl_take isl_union_map *umap)
3601 return total(umap, &isl_map_remove_divs);
3604 /* Remove all existentially quantified variables and integer divisions
3605 * from "uset" using Fourier-Motzkin elimination.
3607 __isl_give isl_union_set *isl_union_set_remove_divs(
3608 __isl_take isl_union_set *uset)
3610 return isl_union_map_remove_divs(uset);
3613 /* Internal data structure for isl_union_map_project_out.
3614 * "type", "first" and "n" are the arguments for the isl_map_project_out
3615 * call.
3616 * "res" collects the results.
3618 struct isl_union_map_project_out_data {
3619 enum isl_dim_type type;
3620 unsigned first;
3621 unsigned n;
3623 isl_union_map *res;
3626 /* Turn the data->n dimensions of type data->type, starting at data->first
3627 * into existentially quantified variables and add the result to data->res.
3629 static isl_stat project_out(__isl_take isl_map *map, void *user)
3631 struct isl_union_map_project_out_data *data = user;
3633 map = isl_map_project_out(map, data->type, data->first, data->n);
3634 data->res = isl_union_map_add_map(data->res, map);
3636 return isl_stat_ok;
3639 /* Turn the "n" dimensions of type "type", starting at "first"
3640 * into existentially quantified variables.
3641 * Since the space of an isl_union_map only contains parameters,
3642 * type is required to be equal to isl_dim_param.
3644 __isl_give isl_union_map *isl_union_map_project_out(
3645 __isl_take isl_union_map *umap,
3646 enum isl_dim_type type, unsigned first, unsigned n)
3648 isl_space *space;
3649 struct isl_union_map_project_out_data data = { type, first, n };
3651 if (!umap)
3652 return NULL;
3654 if (type != isl_dim_param)
3655 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3656 "can only project out parameters",
3657 return isl_union_map_free(umap));
3659 space = isl_union_map_get_space(umap);
3660 space = isl_space_drop_dims(space, type, first, n);
3661 data.res = isl_union_map_empty(space);
3662 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3663 data.res = isl_union_map_free(data.res);
3665 isl_union_map_free(umap);
3667 return data.res;
3670 /* Project out all parameters from "umap" by existentially quantifying
3671 * over them.
3673 __isl_give isl_union_map *isl_union_map_project_out_all_params(
3674 __isl_take isl_union_map *umap)
3676 unsigned n;
3678 if (!umap)
3679 return NULL;
3680 n = isl_union_map_dim(umap, isl_dim_param);
3681 return isl_union_map_project_out(umap, isl_dim_param, 0, n);
3684 /* Turn the "n" dimensions of type "type", starting at "first"
3685 * into existentially quantified variables.
3686 * Since the space of an isl_union_set only contains parameters,
3687 * "type" is required to be equal to isl_dim_param.
3689 __isl_give isl_union_set *isl_union_set_project_out(
3690 __isl_take isl_union_set *uset,
3691 enum isl_dim_type type, unsigned first, unsigned n)
3693 return isl_union_map_project_out(uset, type, first, n);
3696 /* Internal data structure for isl_union_map_involves_dims.
3697 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3699 struct isl_union_map_involves_dims_data {
3700 unsigned first;
3701 unsigned n;
3704 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3706 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3708 struct isl_union_map_involves_dims_data *data = user;
3709 isl_bool involves;
3711 involves = isl_map_involves_dims(map,
3712 isl_dim_param, data->first, data->n);
3713 if (involves < 0)
3714 return isl_bool_error;
3715 return !involves;
3718 /* Does "umap" involve any of the n parameters starting at first?
3719 * "type" is required to be set to isl_dim_param.
3721 * "umap" involves any of those parameters if any of its maps
3722 * involve the parameters. In other words, "umap" does not
3723 * involve any of the parameters if all its maps to not
3724 * involve the parameters.
3726 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3727 enum isl_dim_type type, unsigned first, unsigned n)
3729 struct isl_union_map_involves_dims_data data = { first, n };
3730 isl_bool excludes;
3732 if (type != isl_dim_param)
3733 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3734 "can only reference parameters", return isl_bool_error);
3736 excludes = union_map_forall_user(umap, &map_excludes, &data);
3738 if (excludes < 0)
3739 return isl_bool_error;
3741 return !excludes;
3744 /* Internal data structure for isl_union_map_reset_range_space.
3745 * "range" is the space from which to set the range space.
3746 * "res" collects the results.
3748 struct isl_union_map_reset_range_space_data {
3749 isl_space *range;
3750 isl_union_map *res;
3753 /* Replace the range space of "map" by the range space of data->range and
3754 * add the result to data->res.
3756 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3758 struct isl_union_map_reset_range_space_data *data = user;
3759 isl_space *space;
3761 space = isl_map_get_space(map);
3762 space = isl_space_domain(space);
3763 space = isl_space_extend_domain_with_range(space,
3764 isl_space_copy(data->range));
3765 map = isl_map_reset_space(map, space);
3766 data->res = isl_union_map_add_map(data->res, map);
3768 return data->res ? isl_stat_ok : isl_stat_error;
3771 /* Replace the range space of all the maps in "umap" by
3772 * the range space of "space".
3774 * This assumes that all maps have the same output dimension.
3775 * This function should therefore not be made publicly available.
3777 * Since the spaces of the maps change, so do their hash value.
3778 * We therefore need to create a new isl_union_map.
3780 __isl_give isl_union_map *isl_union_map_reset_range_space(
3781 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3783 struct isl_union_map_reset_range_space_data data = { space };
3785 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3786 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3787 data.res = isl_union_map_free(data.res);
3789 isl_space_free(space);
3790 isl_union_map_free(umap);
3791 return data.res;
3794 /* Check that "umap" and "space" have the same number of parameters.
3796 static isl_stat check_union_map_space_equal_dim(__isl_keep isl_union_map *umap,
3797 __isl_keep isl_space *space)
3799 unsigned dim1, dim2;
3801 if (!umap || !space)
3802 return isl_stat_error;
3803 dim1 = isl_union_map_dim(umap, isl_dim_param);
3804 dim2 = isl_space_dim(space, isl_dim_param);
3805 if (dim1 == dim2)
3806 return isl_stat_ok;
3807 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3808 "number of parameters does not match", return isl_stat_error);
3811 /* Internal data structure for isl_union_map_reset_equal_dim_space.
3812 * "space" is the target space.
3813 * "res" collects the results.
3815 struct isl_union_map_reset_params_data {
3816 isl_space *space;
3817 isl_union_map *res;
3820 /* Replace the parameters of "map" by those of data->space and
3821 * add the result to data->res.
3823 static isl_stat reset_params(__isl_take isl_map *map, void *user)
3825 struct isl_union_map_reset_params_data *data = user;
3826 isl_space *space;
3828 space = isl_map_get_space(map);
3829 space = isl_space_replace_params(space, data->space);
3830 map = isl_map_reset_equal_dim_space(map, space);
3831 data->res = isl_union_map_add_map(data->res, map);
3833 return data->res ? isl_stat_ok : isl_stat_error;
3836 /* Replace the space of "umap" by "space", without modifying
3837 * the dimension of "umap", i.e., the number of parameters of "umap".
3839 * Since the hash values of the maps in the union map depend
3840 * on the parameters, a new union map needs to be constructed.
3842 __isl_give isl_union_map *isl_union_map_reset_equal_dim_space(
3843 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3845 struct isl_union_map_reset_params_data data = { space };
3846 isl_bool equal;
3847 isl_space *umap_space;
3849 umap_space = isl_union_map_peek_space(umap);
3850 equal = isl_space_is_equal(umap_space, space);
3851 if (equal < 0)
3852 goto error;
3853 if (equal) {
3854 isl_space_free(space);
3855 return umap;
3857 if (check_union_map_space_equal_dim(umap, space) < 0)
3858 goto error;
3860 data.res = isl_union_map_empty(isl_space_copy(space));
3861 if (isl_union_map_foreach_map(umap, &reset_params, &data) < 0)
3862 data.res = isl_union_map_free(data.res);
3864 isl_space_free(space);
3865 isl_union_map_free(umap);
3866 return data.res;
3867 error:
3868 isl_union_map_free(umap);
3869 isl_space_free(space);
3870 return NULL;
3873 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3874 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3875 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3876 * to the domain and the range of each map.
3877 * "res" collects the results.
3879 struct isl_union_order_at_data {
3880 isl_multi_union_pw_aff *mupa;
3881 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3882 __isl_take isl_multi_pw_aff *mpa2);
3883 isl_union_map *res;
3886 /* Intersect "map" with the result of applying data->order to
3887 * the functions in data->mupa that apply to the domain and the range
3888 * of "map" and add the result to data->res.
3890 static isl_stat order_at(__isl_take isl_map *map, void *user)
3892 struct isl_union_order_at_data *data = user;
3893 isl_space *space;
3894 isl_multi_pw_aff *mpa1, *mpa2;
3895 isl_map *order;
3897 space = isl_space_domain(isl_map_get_space(map));
3898 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3899 space = isl_space_range(isl_map_get_space(map));
3900 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3901 order = data->order(mpa1, mpa2);
3902 map = isl_map_intersect(map, order);
3903 data->res = isl_union_map_add_map(data->res, map);
3905 return data->res ? isl_stat_ok : isl_stat_error;
3908 /* If "mupa" has a non-trivial explicit domain, then intersect
3909 * domain and range of "umap" with this explicit domain.
3910 * If the explicit domain only describes constraints on the parameters,
3911 * then the intersection only needs to be performed once.
3913 static __isl_give isl_union_map *intersect_explicit_domain(
3914 __isl_take isl_union_map *umap, __isl_keep isl_multi_union_pw_aff *mupa)
3916 isl_bool non_trivial, is_params;
3917 isl_union_set *dom;
3919 non_trivial = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
3920 if (non_trivial < 0)
3921 return isl_union_map_free(umap);
3922 if (!non_trivial)
3923 return umap;
3924 mupa = isl_multi_union_pw_aff_copy(mupa);
3925 dom = isl_multi_union_pw_aff_domain(mupa);
3926 is_params = isl_union_set_is_params(dom);
3927 if (is_params < 0) {
3928 isl_union_set_free(dom);
3929 return isl_union_map_free(umap);
3931 if (is_params) {
3932 isl_set *set;
3934 set = isl_union_set_params(dom);
3935 umap = isl_union_map_intersect_params(umap, set);
3936 return umap;
3938 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(dom));
3939 umap = isl_union_map_intersect_range(umap, dom);
3940 return umap;
3943 /* Intersect each map in "umap" with the result of calling "order"
3944 * on the functions is "mupa" that apply to the domain and the range
3945 * of the map.
3947 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3948 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3949 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3950 __isl_take isl_multi_pw_aff *mpa2))
3952 struct isl_union_order_at_data data;
3954 umap = isl_union_map_align_params(umap,
3955 isl_multi_union_pw_aff_get_space(mupa));
3956 mupa = isl_multi_union_pw_aff_align_params(mupa,
3957 isl_union_map_get_space(umap));
3958 umap = intersect_explicit_domain(umap, mupa);
3959 data.mupa = mupa;
3960 data.order = order;
3961 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3962 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
3963 data.res = isl_union_map_free(data.res);
3965 isl_multi_union_pw_aff_free(mupa);
3966 isl_union_map_free(umap);
3967 return data.res;
3970 /* Return the subset of "umap" where the domain and the range
3971 * have equal "mupa" values.
3973 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
3974 __isl_take isl_union_map *umap,
3975 __isl_take isl_multi_union_pw_aff *mupa)
3977 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3978 &isl_multi_pw_aff_eq_map);
3981 /* Return the subset of "umap" where the domain has a lexicographically
3982 * smaller "mupa" value than the range.
3984 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
3985 __isl_take isl_union_map *umap,
3986 __isl_take isl_multi_union_pw_aff *mupa)
3988 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3989 &isl_multi_pw_aff_lex_lt_map);
3992 /* Return the subset of "umap" where the domain has a lexicographically
3993 * greater "mupa" value than the range.
3995 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
3996 __isl_take isl_union_map *umap,
3997 __isl_take isl_multi_union_pw_aff *mupa)
3999 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4000 &isl_multi_pw_aff_lex_gt_map);
4003 /* Return the union of the elements in the list "list".
4005 __isl_give isl_union_set *isl_union_set_list_union(
4006 __isl_take isl_union_set_list *list)
4008 int i, n;
4009 isl_ctx *ctx;
4010 isl_space *space;
4011 isl_union_set *res;
4013 if (!list)
4014 return NULL;
4016 ctx = isl_union_set_list_get_ctx(list);
4017 space = isl_space_params_alloc(ctx, 0);
4018 res = isl_union_set_empty(space);
4020 n = isl_union_set_list_n_union_set(list);
4021 for (i = 0; i < n; ++i) {
4022 isl_union_set *uset_i;
4024 uset_i = isl_union_set_list_get_union_set(list, i);
4025 res = isl_union_set_union(res, uset_i);
4028 isl_union_set_list_free(list);
4029 return res;
4032 /* Update *hash with the hash value of "map".
4034 static isl_stat add_hash(__isl_take isl_map *map, void *user)
4036 uint32_t *hash = user;
4037 uint32_t map_hash;
4039 map_hash = isl_map_get_hash(map);
4040 isl_hash_hash(*hash, map_hash);
4042 isl_map_free(map);
4043 return isl_stat_ok;
4046 /* Return a hash value that digests "umap".
4048 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
4050 uint32_t hash;
4052 if (!umap)
4053 return 0;
4055 hash = isl_hash_init();
4056 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
4057 return 0;
4059 return hash;
4062 /* Return a hash value that digests "uset".
4064 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
4066 return isl_union_map_get_hash(uset);
4069 /* Add the number of basic sets in "set" to "n".
4071 static isl_stat add_n(__isl_take isl_set *set, void *user)
4073 int *n = user;
4075 *n += isl_set_n_basic_set(set);
4076 isl_set_free(set);
4078 return isl_stat_ok;
4081 /* Return the total number of basic sets in "uset".
4083 int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
4085 int n = 0;
4087 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
4088 return -1;
4090 return n;
4093 /* Add the basic sets in "set" to "list".
4095 static isl_stat add_list(__isl_take isl_set *set, void *user)
4097 isl_basic_set_list **list = user;
4098 isl_basic_set_list *list_i;
4100 list_i = isl_set_get_basic_set_list(set);
4101 *list = isl_basic_set_list_concat(*list, list_i);
4102 isl_set_free(set);
4104 if (!*list)
4105 return isl_stat_error;
4106 return isl_stat_ok;
4109 /* Return a list containing all the basic sets in "uset".
4111 * First construct a list of the appropriate size and
4112 * then add all the elements.
4114 __isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
4115 __isl_keep isl_union_set *uset)
4117 int n;
4118 isl_ctx *ctx;
4119 isl_basic_set_list *list;
4121 if (!uset)
4122 return NULL;
4123 ctx = isl_union_set_get_ctx(uset);
4124 n = isl_union_set_n_basic_set(uset);
4125 if (n < 0)
4126 return NULL;
4127 list = isl_basic_set_list_alloc(ctx, n);
4128 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
4129 list = isl_basic_set_list_free(list);
4131 return list;
4134 /* Internal data structure for isl_union_map_remove_map_if.
4135 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4137 struct isl_union_map_remove_map_if_data {
4138 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
4139 void *user;
4142 /* isl_un_op_control filter that negates the result of data->fn
4143 * called on "map".
4145 static isl_bool not(__isl_keep isl_map *map, void *user)
4147 struct isl_union_map_remove_map_if_data *data = user;
4149 return isl_bool_not(data->fn(map, data->user));
4152 /* Dummy isl_un_op_control transformation callback that
4153 * simply returns the input.
4155 static __isl_give isl_map *map_id(__isl_take isl_map *map)
4157 return map;
4160 /* Call "fn" on every map in "umap" and remove those maps
4161 * for which the callback returns true.
4163 * Use un_op to keep only those maps that are not filtered out,
4164 * applying an identity transformation on them.
4166 __isl_give isl_union_map *isl_union_map_remove_map_if(
4167 __isl_take isl_union_map *umap,
4168 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4170 struct isl_union_map_remove_map_if_data data = { fn, user };
4171 struct isl_un_op_control control = {
4172 .filter = &not,
4173 .filter_user = &data,
4174 .fn_map = &map_id,
4176 return un_op(umap, &control);