isl_local_space.c: add missing include
[isl.git] / isl_union_map.c
blob4f5127b5827eec3c877be382d9812f0bcc8a5acb
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 model = isl_space_params(model);
258 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
259 if (!data.exp)
260 goto error;
262 data.res = isl_union_map_alloc(isl_space_copy(data.exp->dim),
263 umap->table.n);
264 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
265 &align_entry, &data) < 0)
266 goto error;
268 isl_reordering_free(data.exp);
269 isl_union_map_free(umap);
270 isl_space_free(model);
271 return data.res;
272 error:
273 isl_reordering_free(data.exp);
274 isl_union_map_free(umap);
275 isl_union_map_free(data.res);
276 isl_space_free(model);
277 return NULL;
280 __isl_give isl_union_set *isl_union_set_align_params(
281 __isl_take isl_union_set *uset, __isl_take isl_space *model)
283 return isl_union_map_align_params(uset, model);
286 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
287 __isl_take isl_union_map *umap2)
289 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
290 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
292 umap1 = isl_union_map_cow(umap1);
294 if (!umap1 || !umap2)
295 goto error;
297 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
298 goto error;
300 isl_union_map_free(umap2);
302 return umap1;
303 error:
304 isl_union_map_free(umap1);
305 isl_union_map_free(umap2);
306 return NULL;
309 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
310 __isl_take isl_union_set *uset2)
312 return isl_union_map_union(uset1, uset2);
315 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
317 if (!umap)
318 return NULL;
320 umap->ref++;
321 return umap;
324 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
326 return isl_union_map_copy(uset);
329 __isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
331 if (!umap)
332 return NULL;
334 if (--umap->ref > 0)
335 return NULL;
337 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
338 &free_umap_entry, NULL);
339 isl_hash_table_clear(&umap->table);
340 isl_space_free(umap->dim);
341 free(umap);
342 return NULL;
345 __isl_null isl_union_set *isl_union_set_free(__isl_take isl_union_set *uset)
347 return isl_union_map_free(uset);
350 /* Do "umap" and "space" have the same parameters?
352 isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
353 __isl_keep isl_space *space)
355 isl_space *umap_space;
357 umap_space = isl_union_map_peek_space(umap);
358 return isl_space_has_equal_params(umap_space, space);
361 /* Do "uset" and "space" have the same parameters?
363 isl_bool isl_union_set_space_has_equal_params(__isl_keep isl_union_set *uset,
364 __isl_keep isl_space *space)
366 return isl_union_map_space_has_equal_params(uset_to_umap(uset), space);
369 static int has_space(const void *entry, const void *val)
371 isl_map *map = (isl_map *)entry;
372 isl_space *space = (isl_space *) val;
374 return isl_space_is_equal(map->dim, space);
377 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
378 __isl_take isl_map *map)
380 uint32_t hash;
381 struct isl_hash_table_entry *entry;
382 isl_bool aligned;
384 if (!map || !umap)
385 goto error;
387 if (isl_map_plain_is_empty(map)) {
388 isl_map_free(map);
389 return umap;
392 aligned = isl_map_space_has_equal_params(map, umap->dim);
393 if (aligned < 0)
394 goto error;
395 if (!aligned) {
396 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
397 map = isl_map_align_params(map, isl_union_map_get_space(umap));
400 umap = isl_union_map_cow(umap);
402 if (!map || !umap)
403 goto error;
405 hash = isl_space_get_hash(map->dim);
406 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
407 &has_space, map->dim, 1);
408 if (!entry)
409 goto error;
411 if (!entry->data)
412 entry->data = map;
413 else {
414 entry->data = isl_map_union(entry->data, isl_map_copy(map));
415 if (!entry->data)
416 goto error;
417 isl_map_free(map);
420 return umap;
421 error:
422 isl_map_free(map);
423 isl_union_map_free(umap);
424 return NULL;
427 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
428 __isl_take isl_set *set)
430 return isl_union_map_add_map(uset, set_to_map(set));
433 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
435 isl_space *dim;
436 isl_union_map *umap;
438 if (!map)
439 return NULL;
441 dim = isl_map_get_space(map);
442 dim = isl_space_params(dim);
443 umap = isl_union_map_empty(dim);
444 umap = isl_union_map_add_map(umap, map);
446 return umap;
449 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
451 return isl_union_map_from_map(set_to_map(set));
454 __isl_give isl_union_map *isl_union_map_from_basic_map(
455 __isl_take isl_basic_map *bmap)
457 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
460 __isl_give isl_union_set *isl_union_set_from_basic_set(
461 __isl_take isl_basic_set *bset)
463 return isl_union_map_from_basic_map(bset);
466 struct isl_union_map_foreach_data
468 isl_stat (*fn)(__isl_take isl_map *map, void *user);
469 void *user;
472 static isl_stat call_on_copy(void **entry, void *user)
474 isl_map *map = *entry;
475 struct isl_union_map_foreach_data *data;
476 data = (struct isl_union_map_foreach_data *)user;
478 return data->fn(isl_map_copy(map), data->user);
481 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
483 return umap ? umap->table.n : 0;
486 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
488 return uset ? uset->table.n : 0;
491 isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
492 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
494 struct isl_union_map_foreach_data data = { fn, user };
496 if (!umap)
497 return isl_stat_error;
499 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
500 &call_on_copy, &data);
503 /* Internal data structure for isl_union_map_every_map.
505 * "test" is the user-specified callback function.
506 * "user" is the user-specified callback function argument.
508 * "failed" is initialized to 0 and set to 1 if "test" fails
509 * on any map.
511 struct isl_union_map_every_data {
512 isl_bool (*test)(__isl_keep isl_map *map, void *user);
513 void *user;
514 int failed;
517 /* Call data->test on "map".
518 * If this fails, then set data->failed and abort.
520 static isl_stat call_every(void **entry, void *user)
522 isl_map *map = *entry;
523 struct isl_union_map_every_data *data = user;
524 isl_bool r;
526 r = data->test(map, data->user);
527 if (r < 0)
528 return isl_stat_error;
529 if (r)
530 return isl_stat_ok;
531 data->failed = 1;
532 return isl_stat_error;
535 /* Does "test" succeed on every map in "umap"?
537 isl_bool isl_union_map_every_map(__isl_keep isl_union_map *umap,
538 isl_bool (*test)(__isl_keep isl_map *map, void *user), void *user)
540 struct isl_union_map_every_data data = { test, user, 0 };
541 isl_stat r;
543 if (!umap)
544 return isl_bool_error;
546 r = isl_hash_table_foreach(isl_union_map_get_ctx(umap), &umap->table,
547 &call_every, &data);
548 if (r >= 0)
549 return isl_bool_true;
550 if (data.failed)
551 return isl_bool_false;
552 return isl_bool_error;
555 static isl_stat copy_map(void **entry, void *user)
557 isl_map *map = *entry;
558 isl_map **map_p = user;
560 *map_p = isl_map_copy(map);
562 return isl_stat_error;
565 __isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
567 isl_ctx *ctx;
568 isl_map *map = NULL;
570 if (!umap)
571 return NULL;
572 ctx = isl_union_map_get_ctx(umap);
573 if (umap->table.n != 1)
574 isl_die(ctx, isl_error_invalid,
575 "union map needs to contain elements in exactly "
576 "one space", goto error);
578 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
580 isl_union_map_free(umap);
582 return map;
583 error:
584 isl_union_map_free(umap);
585 return NULL;
588 __isl_give isl_set *isl_set_from_union_set(__isl_take isl_union_set *uset)
590 return isl_map_from_union_map(uset);
593 /* Extract the map in "umap" that lives in the given space (ignoring
594 * parameters).
596 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
597 __isl_take isl_space *space)
599 uint32_t hash;
600 struct isl_hash_table_entry *entry;
602 space = isl_space_drop_dims(space, isl_dim_param,
603 0, isl_space_dim(space, isl_dim_param));
604 space = isl_space_align_params(space, isl_union_map_get_space(umap));
605 if (!umap || !space)
606 goto error;
608 hash = isl_space_get_hash(space);
609 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
610 &has_space, space, 0);
611 if (!entry)
612 return isl_map_empty(space);
613 isl_space_free(space);
614 return isl_map_copy(entry->data);
615 error:
616 isl_space_free(space);
617 return NULL;
620 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
621 __isl_take isl_space *dim)
623 return set_from_map(isl_union_map_extract_map(uset, dim));
626 /* Check if umap contains a map in the given space.
628 isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
629 __isl_keep isl_space *space)
631 uint32_t hash;
632 struct isl_hash_table_entry *entry;
634 if (!umap || !space)
635 return isl_bool_error;
637 hash = isl_space_get_hash(space);
638 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
639 &has_space, space, 0);
640 return !!entry;
643 isl_bool isl_union_set_contains(__isl_keep isl_union_set *uset,
644 __isl_keep isl_space *space)
646 return isl_union_map_contains(uset, space);
649 isl_stat isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
650 isl_stat (*fn)(__isl_take isl_set *set, void *user), void *user)
652 return isl_union_map_foreach_map(uset,
653 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
656 struct isl_union_set_foreach_point_data {
657 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
658 void *user;
661 static isl_stat foreach_point(__isl_take isl_set *set, void *user)
663 struct isl_union_set_foreach_point_data *data = user;
664 isl_stat r;
666 r = isl_set_foreach_point(set, data->fn, data->user);
667 isl_set_free(set);
669 return r;
672 isl_stat isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
673 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
675 struct isl_union_set_foreach_point_data data = { fn, user };
676 return isl_union_set_foreach_set(uset, &foreach_point, &data);
679 /* Data structure that specifies how gen_bin_op should
680 * construct results from the inputs.
682 * If "subtract" is set, then a map in the first input is copied to the result
683 * if there is no corresponding map in the second input.
684 * Otherwise, a map in the first input with no corresponding map
685 * in the second input is ignored.
686 * If "filter" is not NULL, then it specifies which maps in the first
687 * input may have a matching map in the second input.
688 * In particular, it makes sure that "match_space" can be called
689 * on the space of the map.
690 * "match_space" specifies how to transform the space of a map
691 * in the first input to the space of the corresponding map
692 * in the second input.
693 * "fn_map" specifies how the matching maps, one from each input,
694 * should be combined to form a map in the result.
696 struct isl_bin_op_control {
697 int subtract;
698 isl_bool (*filter)(__isl_keep isl_map *map);
699 __isl_give isl_space *(*match_space)(__isl_take isl_space *space);
700 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map1,
701 __isl_take isl_map *map2);
704 /* Internal data structure for gen_bin_op.
705 * "control" specifies how the maps in the result should be constructed.
706 * "umap2" is a pointer to the second argument.
707 * "res" collects the results.
709 struct isl_union_map_gen_bin_data {
710 struct isl_bin_op_control *control;
711 isl_union_map *umap2;
712 isl_union_map *res;
715 /* Add a copy of "map" to "res" and return the result.
717 static __isl_give isl_union_map *bin_add_map(__isl_take isl_union_map *res,
718 __isl_keep isl_map *map)
720 return isl_union_map_add_map(res, isl_map_copy(map));
723 /* Combine "map1" and "map2", add the result to "res" and return the result.
724 * Check whether the result is empty before adding it to "res".
726 static __isl_give isl_union_map *bin_add_pair(__isl_take isl_union_map *res,
727 __isl_keep isl_map *map1, __isl_keep isl_map *map2,
728 struct isl_union_map_gen_bin_data *data)
730 isl_bool empty;
731 isl_map *map;
733 map = data->control->fn_map(isl_map_copy(map1), isl_map_copy(map2));
734 empty = isl_map_is_empty(map);
735 if (empty < 0 || empty) {
736 isl_map_free(map);
737 if (empty < 0)
738 return isl_union_map_free(res);
739 return res;
741 return isl_union_map_add_map(res, map);
744 /* Dummy match_space function that simply returns the input space.
746 static __isl_give isl_space *identity(__isl_take isl_space *space)
748 return space;
751 /* Look for the map in data->umap2 that corresponds to "map", if any.
752 * Return (isl_bool_true, matching map) if there is one,
753 * (isl_bool_false, NULL) if there is no matching map and
754 * (isl_bool_error, NULL) on error.
756 * If not NULL, then data->control->filter specifies whether "map"
757 * can have any matching map. If so,
758 * data->control->match_space specifies which map in data->umap2
759 * corresponds to "map".
761 static __isl_keep isl_maybe_isl_map bin_try_get_match(
762 struct isl_union_map_gen_bin_data *data, __isl_keep isl_map *map)
764 uint32_t hash;
765 struct isl_hash_table_entry *entry2;
766 isl_space *space;
767 isl_maybe_isl_map res = { isl_bool_error, NULL };
769 if (data->control->filter) {
770 res.valid = data->control->filter(map);
771 if (res.valid < 0 || !res.valid)
772 return res;
773 res.valid = isl_bool_error;
776 space = isl_map_get_space(map);
777 if (data->control->match_space != &identity)
778 space = data->control->match_space(space);
779 if (!space)
780 return res;
781 hash = isl_space_get_hash(space);
782 entry2 = isl_hash_table_find(isl_union_map_get_ctx(data->umap2),
783 &data->umap2->table, hash,
784 &has_space, space, 0);
785 isl_space_free(space);
786 res.valid = entry2 != NULL;
787 if (entry2)
788 res.value = entry2->data;
790 return res;
793 /* isl_hash_table_foreach callback for gen_bin_op.
794 * Look for the map in data->umap2 that corresponds
795 * to the map that "entry" points to, apply the binary operation and
796 * add the result to data->res.
798 * If no corresponding map can be found, then the effect depends
799 * on data->control->subtract. If it is set, then the current map
800 * is added directly to the result. Otherwise, it is ignored.
802 static isl_stat gen_bin_entry(void **entry, void *user)
804 struct isl_union_map_gen_bin_data *data = user;
805 isl_map *map = *entry;
806 isl_maybe_isl_map m;
808 m = bin_try_get_match(data, map);
809 if (m.valid < 0)
810 return isl_stat_error;
811 if (!m.valid && !data->control->subtract)
812 return isl_stat_ok;
814 if (!m.valid)
815 data->res = bin_add_map(data->res, map);
816 else
817 data->res = bin_add_pair(data->res, map, m.value, data);
818 if (!data->res)
819 return isl_stat_error;
821 return isl_stat_ok;
824 /* Apply a binary operation to "umap1" and "umap2" based on "control".
825 * Run over all maps in "umap1" and look for the corresponding map in "umap2"
826 * in gen_bin_entry.
828 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
829 __isl_take isl_union_map *umap2, struct isl_bin_op_control *control)
831 struct isl_union_map_gen_bin_data data = { control, NULL, NULL };
833 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
834 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
836 if (!umap1 || !umap2)
837 goto error;
839 data.umap2 = umap2;
840 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
841 umap1->table.n);
842 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
843 &gen_bin_entry, &data) < 0)
844 goto error;
846 isl_union_map_free(umap1);
847 isl_union_map_free(umap2);
848 return data.res;
849 error:
850 isl_union_map_free(umap1);
851 isl_union_map_free(umap2);
852 isl_union_map_free(data.res);
853 return NULL;
856 __isl_give isl_union_map *isl_union_map_subtract(
857 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
859 struct isl_bin_op_control control = {
860 .subtract = 1,
861 .match_space = &identity,
862 .fn_map = &isl_map_subtract,
865 return gen_bin_op(umap1, umap2, &control);
868 __isl_give isl_union_set *isl_union_set_subtract(
869 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
871 return isl_union_map_subtract(uset1, uset2);
874 struct isl_union_map_gen_bin_set_data {
875 isl_set *set;
876 isl_union_map *res;
879 static isl_stat intersect_params_entry(void **entry, void *user)
881 struct isl_union_map_gen_bin_set_data *data = user;
882 isl_map *map = *entry;
883 int empty;
885 map = isl_map_copy(map);
886 map = isl_map_intersect_params(map, isl_set_copy(data->set));
888 empty = isl_map_is_empty(map);
889 if (empty < 0) {
890 isl_map_free(map);
891 return isl_stat_error;
894 data->res = isl_union_map_add_map(data->res, map);
896 return isl_stat_ok;
899 static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
900 __isl_take isl_set *set, isl_stat (*fn)(void **, void *))
902 struct isl_union_map_gen_bin_set_data data = { NULL, NULL };
904 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
905 set = isl_set_align_params(set, isl_union_map_get_space(umap));
907 if (!umap || !set)
908 goto error;
910 data.set = set;
911 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
912 umap->table.n);
913 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
914 fn, &data) < 0)
915 goto error;
917 isl_union_map_free(umap);
918 isl_set_free(set);
919 return data.res;
920 error:
921 isl_union_map_free(umap);
922 isl_set_free(set);
923 isl_union_map_free(data.res);
924 return NULL;
927 /* Intersect "umap" with the parameter domain "set".
929 * If "set" does not have any constraints, then we can return immediately.
931 __isl_give isl_union_map *isl_union_map_intersect_params(
932 __isl_take isl_union_map *umap, __isl_take isl_set *set)
934 int is_universe;
936 is_universe = isl_set_plain_is_universe(set);
937 if (is_universe < 0)
938 goto error;
939 if (is_universe) {
940 isl_set_free(set);
941 return umap;
944 return gen_bin_set_op(umap, set, &intersect_params_entry);
945 error:
946 isl_union_map_free(umap);
947 isl_set_free(set);
948 return NULL;
951 __isl_give isl_union_set *isl_union_set_intersect_params(
952 __isl_take isl_union_set *uset, __isl_take isl_set *set)
954 return isl_union_map_intersect_params(uset, set);
957 static __isl_give isl_union_map *union_map_intersect_params(
958 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
960 return isl_union_map_intersect_params(umap,
961 isl_set_from_union_set(uset));
964 static __isl_give isl_union_map *union_map_gist_params(
965 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
967 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
970 struct isl_union_map_match_bin_data {
971 isl_union_map *umap2;
972 isl_union_map *res;
973 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
976 static isl_stat match_bin_entry(void **entry, void *user)
978 struct isl_union_map_match_bin_data *data = user;
979 uint32_t hash;
980 struct isl_hash_table_entry *entry2;
981 isl_map *map = *entry;
982 int empty;
984 hash = isl_space_get_hash(map->dim);
985 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
986 hash, &has_space, map->dim, 0);
987 if (!entry2)
988 return isl_stat_ok;
990 map = isl_map_copy(map);
991 map = data->fn(map, isl_map_copy(entry2->data));
993 empty = isl_map_is_empty(map);
994 if (empty < 0) {
995 isl_map_free(map);
996 return isl_stat_error;
998 if (empty) {
999 isl_map_free(map);
1000 return isl_stat_ok;
1003 data->res = isl_union_map_add_map(data->res, map);
1005 return isl_stat_ok;
1008 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
1009 __isl_take isl_union_map *umap2,
1010 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
1012 struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
1014 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1015 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1017 if (!umap1 || !umap2)
1018 goto error;
1020 data.umap2 = umap2;
1021 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1022 umap1->table.n);
1023 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1024 &match_bin_entry, &data) < 0)
1025 goto error;
1027 isl_union_map_free(umap1);
1028 isl_union_map_free(umap2);
1029 return data.res;
1030 error:
1031 isl_union_map_free(umap1);
1032 isl_union_map_free(umap2);
1033 isl_union_map_free(data.res);
1034 return NULL;
1037 __isl_give isl_union_map *isl_union_map_intersect(
1038 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1040 return match_bin_op(umap1, umap2, &isl_map_intersect);
1043 /* Compute the intersection of the two union_sets.
1044 * As a special case, if exactly one of the two union_sets
1045 * is a parameter domain, then intersect the parameter domain
1046 * of the other one with this set.
1048 __isl_give isl_union_set *isl_union_set_intersect(
1049 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1051 int p1, p2;
1053 p1 = isl_union_set_is_params(uset1);
1054 p2 = isl_union_set_is_params(uset2);
1055 if (p1 < 0 || p2 < 0)
1056 goto error;
1057 if (!p1 && p2)
1058 return union_map_intersect_params(uset1, uset2);
1059 if (p1 && !p2)
1060 return union_map_intersect_params(uset2, uset1);
1061 return isl_union_map_intersect(uset1, uset2);
1062 error:
1063 isl_union_set_free(uset1);
1064 isl_union_set_free(uset2);
1065 return NULL;
1068 static isl_stat gist_params_entry(void **entry, void *user)
1070 struct isl_union_map_gen_bin_set_data *data = user;
1071 isl_map *map = *entry;
1072 int empty;
1074 map = isl_map_copy(map);
1075 map = isl_map_gist_params(map, isl_set_copy(data->set));
1077 empty = isl_map_is_empty(map);
1078 if (empty < 0) {
1079 isl_map_free(map);
1080 return isl_stat_error;
1083 data->res = isl_union_map_add_map(data->res, map);
1085 return isl_stat_ok;
1088 __isl_give isl_union_map *isl_union_map_gist_params(
1089 __isl_take isl_union_map *umap, __isl_take isl_set *set)
1091 return gen_bin_set_op(umap, set, &gist_params_entry);
1094 __isl_give isl_union_set *isl_union_set_gist_params(
1095 __isl_take isl_union_set *uset, __isl_take isl_set *set)
1097 return isl_union_map_gist_params(uset, set);
1100 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
1101 __isl_take isl_union_map *context)
1103 return match_bin_op(umap, context, &isl_map_gist);
1106 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
1107 __isl_take isl_union_set *context)
1109 if (isl_union_set_is_params(context))
1110 return union_map_gist_params(uset, context);
1111 return isl_union_map_gist(uset, context);
1114 /* For each map in "umap", remove the constraints in the corresponding map
1115 * of "context".
1116 * Each map in "context" is assumed to consist of a single disjunct and
1117 * to have explicit representations for all local variables.
1119 __isl_give isl_union_map *isl_union_map_plain_gist(
1120 __isl_take isl_union_map *umap, __isl_take isl_union_map *context)
1122 return match_bin_op(umap, context, &isl_map_plain_gist);
1125 /* For each set in "uset", remove the constraints in the corresponding set
1126 * of "context".
1127 * Each set in "context" is assumed to consist of a single disjunct and
1128 * to have explicit representations for all local variables.
1130 __isl_give isl_union_set *isl_union_set_plain_gist(
1131 __isl_take isl_union_set *uset, __isl_take isl_union_set *context)
1133 return isl_union_map_plain_gist(uset, context);
1136 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
1137 __isl_take isl_map *set2)
1139 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
1142 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
1143 __isl_take isl_map *set2)
1145 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
1148 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
1149 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1151 return match_bin_op(uset1, uset2, &lex_lt_set);
1154 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
1155 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1157 return match_bin_op(uset1, uset2, &lex_le_set);
1160 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
1161 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1163 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
1166 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
1167 __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
1169 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
1172 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
1173 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1175 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
1178 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
1179 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1181 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
1184 /* Intersect the domain of "umap" with "uset".
1186 static __isl_give isl_union_map *union_map_intersect_domain(
1187 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1189 struct isl_bin_op_control control = {
1190 .match_space = &isl_space_domain,
1191 .fn_map = &isl_map_intersect_domain,
1194 return gen_bin_op(umap, uset, &control);
1197 /* Intersect the domain of "umap" with "uset".
1198 * If "uset" is a parameters domain, then intersect the parameter
1199 * domain of "umap" with this set.
1201 __isl_give isl_union_map *isl_union_map_intersect_domain(
1202 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1204 if (isl_union_set_is_params(uset))
1205 return union_map_intersect_params(umap, uset);
1206 else
1207 return union_map_intersect_domain(umap, uset);
1210 /* Remove the elements of "uset" from the domain of "umap".
1212 __isl_give isl_union_map *isl_union_map_subtract_domain(
1213 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1215 struct isl_bin_op_control control = {
1216 .subtract = 1,
1217 .match_space = &isl_space_domain,
1218 .fn_map = &isl_map_subtract_domain,
1221 return gen_bin_op(umap, dom, &control);
1224 /* Remove the elements of "uset" from the range of "umap".
1226 __isl_give isl_union_map *isl_union_map_subtract_range(
1227 __isl_take isl_union_map *umap, __isl_take isl_union_set *dom)
1229 struct isl_bin_op_control control = {
1230 .subtract = 1,
1231 .match_space = &isl_space_range,
1232 .fn_map = &isl_map_subtract_range,
1235 return gen_bin_op(umap, dom, &control);
1238 /* Compute the gist of "umap" with respect to the domain "uset".
1240 static __isl_give isl_union_map *union_map_gist_domain(
1241 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1243 struct isl_bin_op_control control = {
1244 .match_space = &isl_space_domain,
1245 .fn_map = &isl_map_gist_domain,
1248 return gen_bin_op(umap, uset, &control);
1251 /* Compute the gist of "umap" with respect to the domain "uset".
1252 * If "uset" is a parameters domain, then compute the gist
1253 * with respect to this parameter domain.
1255 __isl_give isl_union_map *isl_union_map_gist_domain(
1256 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1258 if (isl_union_set_is_params(uset))
1259 return union_map_gist_params(umap, uset);
1260 else
1261 return union_map_gist_domain(umap, uset);
1264 /* Compute the gist of "umap" with respect to the range "uset".
1266 __isl_give isl_union_map *isl_union_map_gist_range(
1267 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1269 struct isl_bin_op_control control = {
1270 .match_space = &isl_space_range,
1271 .fn_map = &isl_map_gist_range,
1274 return gen_bin_op(umap, uset, &control);
1277 __isl_give isl_union_map *isl_union_map_intersect_range(
1278 __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
1280 struct isl_bin_op_control control = {
1281 .match_space = &isl_space_range,
1282 .fn_map = &isl_map_intersect_range,
1285 return gen_bin_op(umap, uset, &control);
1288 /* Intersect each map in "umap" in a space A -> [B -> C]
1289 * with the corresponding map in "factor" in the space A -> C and
1290 * collect the results.
1292 __isl_give isl_union_map *isl_union_map_intersect_range_factor_range(
1293 __isl_take isl_union_map *umap, __isl_take isl_union_map *factor)
1295 struct isl_bin_op_control control = {
1296 .filter = &isl_map_range_is_wrapping,
1297 .match_space = &isl_space_range_factor_range,
1298 .fn_map = &isl_map_intersect_range_factor_range,
1301 return gen_bin_op(umap, factor, &control);
1304 struct isl_union_map_bin_data {
1305 isl_union_map *umap2;
1306 isl_union_map *res;
1307 isl_map *map;
1308 isl_stat (*fn)(void **entry, void *user);
1311 static isl_stat apply_range_entry(void **entry, void *user)
1313 struct isl_union_map_bin_data *data = user;
1314 isl_map *map2 = *entry;
1315 isl_bool empty;
1317 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1318 map2->dim, isl_dim_in))
1319 return isl_stat_ok;
1321 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1323 empty = isl_map_is_empty(map2);
1324 if (empty < 0) {
1325 isl_map_free(map2);
1326 return isl_stat_error;
1328 if (empty) {
1329 isl_map_free(map2);
1330 return isl_stat_ok;
1333 data->res = isl_union_map_add_map(data->res, map2);
1335 return isl_stat_ok;
1338 static isl_stat bin_entry(void **entry, void *user)
1340 struct isl_union_map_bin_data *data = user;
1341 isl_map *map = *entry;
1343 data->map = map;
1344 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1345 data->fn, data) < 0)
1346 return isl_stat_error;
1348 return isl_stat_ok;
1351 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1352 __isl_take isl_union_map *umap2,
1353 isl_stat (*fn)(void **entry, void *user))
1355 struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
1357 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1358 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1360 if (!umap1 || !umap2)
1361 goto error;
1363 data.umap2 = umap2;
1364 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1365 umap1->table.n);
1366 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1367 &bin_entry, &data) < 0)
1368 goto error;
1370 isl_union_map_free(umap1);
1371 isl_union_map_free(umap2);
1372 return data.res;
1373 error:
1374 isl_union_map_free(umap1);
1375 isl_union_map_free(umap2);
1376 isl_union_map_free(data.res);
1377 return NULL;
1380 __isl_give isl_union_map *isl_union_map_apply_range(
1381 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1383 return bin_op(umap1, umap2, &apply_range_entry);
1386 __isl_give isl_union_map *isl_union_map_apply_domain(
1387 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1389 umap1 = isl_union_map_reverse(umap1);
1390 umap1 = isl_union_map_apply_range(umap1, umap2);
1391 return isl_union_map_reverse(umap1);
1394 __isl_give isl_union_set *isl_union_set_apply(
1395 __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
1397 return isl_union_map_apply_range(uset, umap);
1400 static isl_stat map_lex_lt_entry(void **entry, void *user)
1402 struct isl_union_map_bin_data *data = user;
1403 isl_map *map2 = *entry;
1405 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1406 map2->dim, isl_dim_out))
1407 return isl_stat_ok;
1409 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1411 data->res = isl_union_map_add_map(data->res, map2);
1413 return isl_stat_ok;
1416 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1417 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1419 return bin_op(umap1, umap2, &map_lex_lt_entry);
1422 static isl_stat map_lex_le_entry(void **entry, void *user)
1424 struct isl_union_map_bin_data *data = user;
1425 isl_map *map2 = *entry;
1427 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1428 map2->dim, isl_dim_out))
1429 return isl_stat_ok;
1431 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1433 data->res = isl_union_map_add_map(data->res, map2);
1435 return isl_stat_ok;
1438 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
1439 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1441 return bin_op(umap1, umap2, &map_lex_le_entry);
1444 static isl_stat product_entry(void **entry, void *user)
1446 struct isl_union_map_bin_data *data = user;
1447 isl_map *map2 = *entry;
1449 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1451 data->res = isl_union_map_add_map(data->res, map2);
1453 return isl_stat_ok;
1456 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1457 __isl_take isl_union_map *umap2)
1459 return bin_op(umap1, umap2, &product_entry);
1462 static isl_stat set_product_entry(void **entry, void *user)
1464 struct isl_union_map_bin_data *data = user;
1465 isl_set *set2 = *entry;
1467 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1469 data->res = isl_union_set_add_set(data->res, set2);
1471 return isl_stat_ok;
1474 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
1475 __isl_take isl_union_set *uset2)
1477 return bin_op(uset1, uset2, &set_product_entry);
1480 static isl_stat domain_product_entry(void **entry, void *user)
1482 struct isl_union_map_bin_data *data = user;
1483 isl_map *map2 = *entry;
1485 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1486 map2->dim, isl_dim_out))
1487 return isl_stat_ok;
1489 map2 = isl_map_domain_product(isl_map_copy(data->map),
1490 isl_map_copy(map2));
1492 data->res = isl_union_map_add_map(data->res, map2);
1494 return isl_stat_ok;
1497 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1499 __isl_give isl_union_map *isl_union_map_domain_product(
1500 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1502 return bin_op(umap1, umap2, &domain_product_entry);
1505 static isl_stat range_product_entry(void **entry, void *user)
1507 struct isl_union_map_bin_data *data = user;
1508 isl_map *map2 = *entry;
1510 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1511 map2->dim, isl_dim_in))
1512 return isl_stat_ok;
1514 map2 = isl_map_range_product(isl_map_copy(data->map),
1515 isl_map_copy(map2));
1517 data->res = isl_union_map_add_map(data->res, map2);
1519 return isl_stat_ok;
1522 __isl_give isl_union_map *isl_union_map_range_product(
1523 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1525 return bin_op(umap1, umap2, &range_product_entry);
1528 /* If data->map A -> B and "map2" C -> D have the same range space,
1529 * then add (A, C) -> (B * D) to data->res.
1531 static isl_stat flat_domain_product_entry(void **entry, void *user)
1533 struct isl_union_map_bin_data *data = user;
1534 isl_map *map2 = *entry;
1536 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1537 map2->dim, isl_dim_out))
1538 return isl_stat_ok;
1540 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1541 isl_map_copy(map2));
1543 data->res = isl_union_map_add_map(data->res, map2);
1545 return isl_stat_ok;
1548 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1550 __isl_give isl_union_map *isl_union_map_flat_domain_product(
1551 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1553 return bin_op(umap1, umap2, &flat_domain_product_entry);
1556 static isl_stat flat_range_product_entry(void **entry, void *user)
1558 struct isl_union_map_bin_data *data = user;
1559 isl_map *map2 = *entry;
1561 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1562 map2->dim, isl_dim_in))
1563 return isl_stat_ok;
1565 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1566 isl_map_copy(map2));
1568 data->res = isl_union_map_add_map(data->res, map2);
1570 return isl_stat_ok;
1573 __isl_give isl_union_map *isl_union_map_flat_range_product(
1574 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1576 return bin_op(umap1, umap2, &flat_range_product_entry);
1579 /* Data structure that specifies how un_op should modify
1580 * the maps in the union map.
1582 * If "inplace" is set, then the maps in the input union map
1583 * are modified in place. This means that "fn_map" should not
1584 * change the meaning of the map or that the union map only
1585 * has a single reference.
1586 * If "total" is set, then all maps need to be modified and
1587 * the results need to live in the same space.
1588 * Otherwise, a new union map is constructed to store the results.
1589 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1590 * are taken into account. "filter_user" is passed as the second argument
1591 * to "filter". No filter can be set if "inplace" or
1592 * "total" is set.
1593 * "fn_map" specifies how the maps (selected by "filter")
1594 * should be transformed.
1596 struct isl_un_op_control {
1597 int inplace;
1598 int total;
1599 isl_bool (*filter)(__isl_keep isl_map *map, void *user);
1600 void *filter_user;
1601 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1604 /* Data structure for wrapping the data for un_op_filter_drop_user.
1605 * "filter" is the function that is being wrapped.
1607 struct isl_un_op_drop_user_data {
1608 isl_bool (*filter)(__isl_keep isl_map *map);
1611 /* Wrapper for isl_un_op_control filters that do not require
1612 * a second argument.
1613 * Simply call data->filter without the second argument.
1615 static isl_bool un_op_filter_drop_user(__isl_keep isl_map *map, void *user)
1617 struct isl_un_op_drop_user_data *data = user;
1618 return data->filter(map);
1621 /* Internal data structure for "un_op".
1622 * "control" specifies how the maps in the union map should be modified.
1623 * "res" collects the results.
1625 struct isl_union_map_un_data {
1626 struct isl_un_op_control *control;
1627 isl_union_map *res;
1630 /* isl_hash_table_foreach callback for un_op.
1631 * Handle the map that "entry" points to.
1633 * If control->filter is set, then check if this map satisfies the filter.
1634 * If so (or if control->filter is not set), modify the map
1635 * by calling control->fn_map and either add the result to data->res or
1636 * replace the original entry by the result (if control->inplace is set).
1638 static isl_stat un_entry(void **entry, void *user)
1640 struct isl_union_map_un_data *data = user;
1641 isl_map *map = *entry;
1643 if (data->control->filter) {
1644 isl_bool ok;
1646 ok = data->control->filter(map, data->control->filter_user);
1647 if (ok < 0)
1648 return isl_stat_error;
1649 if (!ok)
1650 return isl_stat_ok;
1653 map = data->control->fn_map(isl_map_copy(map));
1654 if (!map)
1655 return isl_stat_error;
1656 if (data->control->inplace) {
1657 isl_map_free(*entry);
1658 *entry = map;
1659 } else {
1660 data->res = isl_union_map_add_map(data->res, map);
1661 if (!data->res)
1662 return isl_stat_error;
1665 return isl_stat_ok;
1668 /* Modify the maps in "umap" based on "control".
1669 * If control->inplace is set, then modify the maps in "umap" in-place.
1670 * Otherwise, create a new union map to hold the results.
1671 * If control->total is set, then perform an inplace computation
1672 * if "umap" is only referenced once. Otherwise, create a new union map
1673 * to store the results.
1675 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1676 struct isl_un_op_control *control)
1678 struct isl_union_map_un_data data = { control };
1680 if (!umap)
1681 return NULL;
1682 if ((control->inplace || control->total) && control->filter)
1683 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
1684 "inplace/total modification cannot be filtered",
1685 return isl_union_map_free(umap));
1687 if (control->total && umap->ref == 1)
1688 control->inplace = 1;
1689 if (control->inplace) {
1690 data.res = umap;
1691 } else {
1692 isl_space *space;
1694 space = isl_union_map_get_space(umap);
1695 data.res = isl_union_map_alloc(space, umap->table.n);
1697 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1698 &umap->table, &un_entry, &data) < 0)
1699 data.res = isl_union_map_free(data.res);
1701 if (control->inplace)
1702 return data.res;
1703 isl_union_map_free(umap);
1704 return data.res;
1707 __isl_give isl_union_map *isl_union_map_from_range(
1708 __isl_take isl_union_set *uset)
1710 struct isl_un_op_control control = {
1711 .fn_map = &isl_map_from_range,
1713 return un_op(uset, &control);
1716 __isl_give isl_union_map *isl_union_map_from_domain(
1717 __isl_take isl_union_set *uset)
1719 return isl_union_map_reverse(isl_union_map_from_range(uset));
1722 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
1723 __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
1725 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1726 isl_union_map_from_range(range));
1729 /* Modify the maps in "umap" by applying "fn" on them.
1730 * "fn" should apply to all maps in "umap" and should not modify the space.
1732 static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1733 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1735 struct isl_un_op_control control = {
1736 .total = 1,
1737 .fn_map = fn,
1740 return un_op(umap, &control);
1743 /* Compute the affine hull of "map" and return the result as an isl_map.
1745 static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1747 return isl_map_from_basic_map(isl_map_affine_hull(map));
1750 __isl_give isl_union_map *isl_union_map_affine_hull(
1751 __isl_take isl_union_map *umap)
1753 return total(umap, &isl_map_affine_hull_map);
1756 __isl_give isl_union_set *isl_union_set_affine_hull(
1757 __isl_take isl_union_set *uset)
1759 return isl_union_map_affine_hull(uset);
1762 /* Wrapper around isl_set_combined_lineality_space
1763 * that returns the combined lineality space in the form of an isl_set
1764 * instead of an isl_basic_set.
1766 static __isl_give isl_set *combined_lineality_space(__isl_take isl_set *set)
1768 return isl_set_from_basic_set(isl_set_combined_lineality_space(set));
1771 /* For each set in "uset", compute the (linear) hull
1772 * of the lineality spaces of its basic sets and
1773 * collect and return the results.
1775 __isl_give isl_union_set *isl_union_set_combined_lineality_space(
1776 __isl_take isl_union_set *uset)
1778 struct isl_un_op_control control = {
1779 .fn_map = &combined_lineality_space,
1781 return un_op(uset, &control);
1784 /* Compute the polyhedral hull of "map" and return the result as an isl_map.
1786 static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1788 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1791 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
1792 __isl_take isl_union_map *umap)
1794 return total(umap, &isl_map_polyhedral_hull_map);
1797 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
1798 __isl_take isl_union_set *uset)
1800 return isl_union_map_polyhedral_hull(uset);
1803 /* Compute a superset of the convex hull of "map" that is described
1804 * by only translates of the constraints in the constituents of "map" and
1805 * return the result as an isl_map.
1807 static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1809 return isl_map_from_basic_map(isl_map_simple_hull(map));
1812 __isl_give isl_union_map *isl_union_map_simple_hull(
1813 __isl_take isl_union_map *umap)
1815 return total(umap, &isl_map_simple_hull_map);
1818 __isl_give isl_union_set *isl_union_set_simple_hull(
1819 __isl_take isl_union_set *uset)
1821 return isl_union_map_simple_hull(uset);
1824 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1825 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1827 struct isl_un_op_control control = {
1828 .inplace = 1,
1829 .fn_map = fn,
1832 return un_op(umap, &control);
1835 /* Remove redundant constraints in each of the basic maps of "umap".
1836 * Since removing redundant constraints does not change the meaning
1837 * or the space, the operation can be performed in-place.
1839 __isl_give isl_union_map *isl_union_map_remove_redundancies(
1840 __isl_take isl_union_map *umap)
1842 return inplace(umap, &isl_map_remove_redundancies);
1845 /* Remove redundant constraints in each of the basic sets of "uset".
1847 __isl_give isl_union_set *isl_union_set_remove_redundancies(
1848 __isl_take isl_union_set *uset)
1850 return isl_union_map_remove_redundancies(uset);
1853 __isl_give isl_union_map *isl_union_map_coalesce(
1854 __isl_take isl_union_map *umap)
1856 return inplace(umap, &isl_map_coalesce);
1859 __isl_give isl_union_set *isl_union_set_coalesce(
1860 __isl_take isl_union_set *uset)
1862 return isl_union_map_coalesce(uset);
1865 __isl_give isl_union_map *isl_union_map_detect_equalities(
1866 __isl_take isl_union_map *umap)
1868 return inplace(umap, &isl_map_detect_equalities);
1871 __isl_give isl_union_set *isl_union_set_detect_equalities(
1872 __isl_take isl_union_set *uset)
1874 return isl_union_map_detect_equalities(uset);
1877 __isl_give isl_union_map *isl_union_map_compute_divs(
1878 __isl_take isl_union_map *umap)
1880 return inplace(umap, &isl_map_compute_divs);
1883 __isl_give isl_union_set *isl_union_set_compute_divs(
1884 __isl_take isl_union_set *uset)
1886 return isl_union_map_compute_divs(uset);
1889 __isl_give isl_union_map *isl_union_map_lexmin(
1890 __isl_take isl_union_map *umap)
1892 return total(umap, &isl_map_lexmin);
1895 __isl_give isl_union_set *isl_union_set_lexmin(
1896 __isl_take isl_union_set *uset)
1898 return isl_union_map_lexmin(uset);
1901 __isl_give isl_union_map *isl_union_map_lexmax(
1902 __isl_take isl_union_map *umap)
1904 return total(umap, &isl_map_lexmax);
1907 __isl_give isl_union_set *isl_union_set_lexmax(
1908 __isl_take isl_union_set *uset)
1910 return isl_union_map_lexmax(uset);
1913 /* Return the universe in the space of "map".
1915 static __isl_give isl_map *universe(__isl_take isl_map *map)
1917 isl_space *space;
1919 space = isl_map_get_space(map);
1920 isl_map_free(map);
1921 return isl_map_universe(space);
1924 __isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1926 struct isl_un_op_control control = {
1927 .fn_map = &universe,
1929 return un_op(umap, &control);
1932 __isl_give isl_union_set *isl_union_set_universe(__isl_take isl_union_set *uset)
1934 return isl_union_map_universe(uset);
1937 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1939 struct isl_un_op_control control = {
1940 .fn_map = &isl_map_reverse,
1942 return un_op(umap, &control);
1945 /* Compute the parameter domain of the given union map.
1947 __isl_give isl_set *isl_union_map_params(__isl_take isl_union_map *umap)
1949 struct isl_un_op_control control = {
1950 .fn_map = &isl_map_params,
1952 int empty;
1954 empty = isl_union_map_is_empty(umap);
1955 if (empty < 0)
1956 goto error;
1957 if (empty) {
1958 isl_space *space;
1959 space = isl_union_map_get_space(umap);
1960 isl_union_map_free(umap);
1961 return isl_set_empty(space);
1963 return isl_set_from_union_set(un_op(umap, &control));
1964 error:
1965 isl_union_map_free(umap);
1966 return NULL;
1969 /* Compute the parameter domain of the given union set.
1971 __isl_give isl_set *isl_union_set_params(__isl_take isl_union_set *uset)
1973 return isl_union_map_params(uset);
1976 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1978 struct isl_un_op_control control = {
1979 .fn_map = &isl_map_domain,
1981 return un_op(umap, &control);
1984 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1986 struct isl_un_op_control control = {
1987 .fn_map = &isl_map_range,
1989 return un_op(umap, &control);
1992 __isl_give isl_union_map *isl_union_map_domain_map(
1993 __isl_take isl_union_map *umap)
1995 struct isl_un_op_control control = {
1996 .fn_map = &isl_map_domain_map,
1998 return un_op(umap, &control);
2001 /* Construct an isl_pw_multi_aff that maps "map" to its domain and
2002 * add the result to "res".
2004 static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
2006 isl_union_pw_multi_aff **res = user;
2007 isl_multi_aff *ma;
2008 isl_pw_multi_aff *pma;
2010 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
2011 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
2012 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2014 return *res ? isl_stat_ok : isl_stat_error;
2018 /* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
2019 * to its domain.
2021 __isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
2022 __isl_take isl_union_map *umap)
2024 isl_union_pw_multi_aff *res;
2026 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
2027 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
2028 res = isl_union_pw_multi_aff_free(res);
2030 isl_union_map_free(umap);
2031 return res;
2034 __isl_give isl_union_map *isl_union_map_range_map(
2035 __isl_take isl_union_map *umap)
2037 struct isl_un_op_control control = {
2038 .fn_map = &isl_map_range_map,
2040 return un_op(umap, &control);
2043 /* Given a collection of wrapped maps of the form A[B -> C],
2044 * return the collection of maps A[B -> C] -> B.
2046 __isl_give isl_union_map *isl_union_set_wrapped_domain_map(
2047 __isl_take isl_union_set *uset)
2049 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2050 struct isl_un_op_control control = {
2051 .filter = &un_op_filter_drop_user,
2052 .filter_user = &data,
2053 .fn_map = &isl_set_wrapped_domain_map,
2055 return un_op(uset, &control);
2058 /* Does "map" relate elements from the same space?
2060 static isl_bool equal_tuples(__isl_keep isl_map *map, void *user)
2062 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
2063 map->dim, isl_dim_out);
2066 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
2068 struct isl_un_op_control control = {
2069 .filter = &equal_tuples,
2070 .fn_map = &isl_map_deltas,
2072 return un_op(umap, &control);
2075 __isl_give isl_union_map *isl_union_map_deltas_map(
2076 __isl_take isl_union_map *umap)
2078 struct isl_un_op_control control = {
2079 .filter = &equal_tuples,
2080 .fn_map = &isl_map_deltas_map,
2082 return un_op(umap, &control);
2085 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
2087 struct isl_un_op_control control = {
2088 .fn_map = &isl_set_identity,
2090 return un_op(uset, &control);
2093 /* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2095 static isl_stat identity_upma(__isl_take isl_set *set, void *user)
2097 isl_union_pw_multi_aff **res = user;
2098 isl_space *space;
2099 isl_pw_multi_aff *pma;
2101 space = isl_space_map_from_set(isl_set_get_space(set));
2102 pma = isl_pw_multi_aff_identity(space);
2103 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2104 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2106 return *res ? isl_stat_ok : isl_stat_error;
2109 /* Return an identity function on "uset" in the form
2110 * of an isl_union_pw_multi_aff.
2112 __isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2113 __isl_take isl_union_set *uset)
2115 isl_union_pw_multi_aff *res;
2117 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2118 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2119 res = isl_union_pw_multi_aff_free(res);
2121 isl_union_set_free(uset);
2122 return res;
2125 /* For each map in "umap" of the form [A -> B] -> C,
2126 * construct the map A -> C and collect the results.
2128 __isl_give isl_union_map *isl_union_map_domain_factor_domain(
2129 __isl_take isl_union_map *umap)
2131 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2132 struct isl_un_op_control control = {
2133 .filter = &un_op_filter_drop_user,
2134 .filter_user = &data,
2135 .fn_map = &isl_map_domain_factor_domain,
2137 return un_op(umap, &control);
2140 /* For each map in "umap" of the form [A -> B] -> C,
2141 * construct the map B -> C and collect the results.
2143 __isl_give isl_union_map *isl_union_map_domain_factor_range(
2144 __isl_take isl_union_map *umap)
2146 struct isl_un_op_drop_user_data data = { &isl_map_domain_is_wrapping };
2147 struct isl_un_op_control control = {
2148 .filter = &un_op_filter_drop_user,
2149 .filter_user = &data,
2150 .fn_map = &isl_map_domain_factor_range,
2152 return un_op(umap, &control);
2155 /* For each map in "umap" of the form A -> [B -> C],
2156 * construct the map A -> B and collect the results.
2158 __isl_give isl_union_map *isl_union_map_range_factor_domain(
2159 __isl_take isl_union_map *umap)
2161 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2162 struct isl_un_op_control control = {
2163 .filter = &un_op_filter_drop_user,
2164 .filter_user = &data,
2165 .fn_map = &isl_map_range_factor_domain,
2167 return un_op(umap, &control);
2170 /* For each map in "umap" of the form A -> [B -> C],
2171 * construct the map A -> C and collect the results.
2173 __isl_give isl_union_map *isl_union_map_range_factor_range(
2174 __isl_take isl_union_map *umap)
2176 struct isl_un_op_drop_user_data data = { &isl_map_range_is_wrapping };
2177 struct isl_un_op_control control = {
2178 .filter = &un_op_filter_drop_user,
2179 .filter_user = &data,
2180 .fn_map = &isl_map_range_factor_range,
2182 return un_op(umap, &control);
2185 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2186 * construct the map A -> C and collect the results.
2188 __isl_give isl_union_map *isl_union_map_factor_domain(
2189 __isl_take isl_union_map *umap)
2191 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2192 struct isl_un_op_control control = {
2193 .filter = &un_op_filter_drop_user,
2194 .filter_user = &data,
2195 .fn_map = &isl_map_factor_domain,
2197 return un_op(umap, &control);
2200 /* For each map in "umap" of the form [A -> B] -> [C -> D],
2201 * construct the map B -> D and collect the results.
2203 __isl_give isl_union_map *isl_union_map_factor_range(
2204 __isl_take isl_union_map *umap)
2206 struct isl_un_op_drop_user_data data = { &isl_map_is_product };
2207 struct isl_un_op_control control = {
2208 .filter = &un_op_filter_drop_user,
2209 .filter_user = &data,
2210 .fn_map = &isl_map_factor_range,
2212 return un_op(umap, &control);
2215 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
2217 struct isl_un_op_drop_user_data data = { &isl_set_is_wrapping };
2218 struct isl_un_op_control control = {
2219 .filter = &un_op_filter_drop_user,
2220 .filter_user = &data,
2221 .fn_map = &isl_set_unwrap,
2223 return un_op(uset, &control);
2226 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
2228 struct isl_un_op_control control = {
2229 .fn_map = &isl_map_wrap,
2231 return un_op(umap, &control);
2234 struct isl_union_map_is_subset_data {
2235 isl_union_map *umap2;
2236 isl_bool is_subset;
2239 static isl_stat is_subset_entry(void **entry, void *user)
2241 struct isl_union_map_is_subset_data *data = user;
2242 uint32_t hash;
2243 struct isl_hash_table_entry *entry2;
2244 isl_map *map = *entry;
2246 hash = isl_space_get_hash(map->dim);
2247 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2248 hash, &has_space, map->dim, 0);
2249 if (!entry2) {
2250 int empty = isl_map_is_empty(map);
2251 if (empty < 0)
2252 return isl_stat_error;
2253 if (empty)
2254 return isl_stat_ok;
2255 data->is_subset = 0;
2256 return isl_stat_error;
2259 data->is_subset = isl_map_is_subset(map, entry2->data);
2260 if (data->is_subset < 0 || !data->is_subset)
2261 return isl_stat_error;
2263 return isl_stat_ok;
2266 isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2267 __isl_keep isl_union_map *umap2)
2269 struct isl_union_map_is_subset_data data = { NULL, isl_bool_true };
2271 umap1 = isl_union_map_copy(umap1);
2272 umap2 = isl_union_map_copy(umap2);
2273 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2274 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2276 if (!umap1 || !umap2)
2277 goto error;
2279 data.umap2 = umap2;
2280 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2281 &is_subset_entry, &data) < 0 &&
2282 data.is_subset)
2283 goto error;
2285 isl_union_map_free(umap1);
2286 isl_union_map_free(umap2);
2288 return data.is_subset;
2289 error:
2290 isl_union_map_free(umap1);
2291 isl_union_map_free(umap2);
2292 return isl_bool_error;
2295 isl_bool isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
2296 __isl_keep isl_union_set *uset2)
2298 return isl_union_map_is_subset(uset1, uset2);
2301 isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2302 __isl_keep isl_union_map *umap2)
2304 isl_bool is_subset;
2306 if (!umap1 || !umap2)
2307 return isl_bool_error;
2308 is_subset = isl_union_map_is_subset(umap1, umap2);
2309 if (is_subset != isl_bool_true)
2310 return is_subset;
2311 is_subset = isl_union_map_is_subset(umap2, umap1);
2312 return is_subset;
2315 isl_bool isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
2316 __isl_keep isl_union_set *uset2)
2318 return isl_union_map_is_equal(uset1, uset2);
2321 isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2322 __isl_keep isl_union_map *umap2)
2324 isl_bool is_subset;
2326 if (!umap1 || !umap2)
2327 return isl_bool_error;
2328 is_subset = isl_union_map_is_subset(umap1, umap2);
2329 if (is_subset != isl_bool_true)
2330 return is_subset;
2331 is_subset = isl_union_map_is_subset(umap2, umap1);
2332 if (is_subset == isl_bool_error)
2333 return is_subset;
2334 return !is_subset;
2337 isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
2338 __isl_keep isl_union_set *uset2)
2340 return isl_union_map_is_strict_subset(uset1, uset2);
2343 /* Internal data structure for isl_union_map_is_disjoint.
2344 * umap2 is the union map with which we are comparing.
2345 * is_disjoint is initialized to 1 and is set to 0 as soon
2346 * as the union maps turn out not to be disjoint.
2348 struct isl_union_map_is_disjoint_data {
2349 isl_union_map *umap2;
2350 isl_bool is_disjoint;
2353 /* Check if "map" is disjoint from data->umap2 and abort
2354 * the search if it is not.
2356 static isl_stat is_disjoint_entry(void **entry, void *user)
2358 struct isl_union_map_is_disjoint_data *data = user;
2359 uint32_t hash;
2360 struct isl_hash_table_entry *entry2;
2361 isl_map *map = *entry;
2363 hash = isl_space_get_hash(map->dim);
2364 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2365 hash, &has_space, map->dim, 0);
2366 if (!entry2)
2367 return isl_stat_ok;
2369 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2370 if (data->is_disjoint < 0 || !data->is_disjoint)
2371 return isl_stat_error;
2373 return isl_stat_ok;
2376 /* Are "umap1" and "umap2" disjoint?
2378 isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2379 __isl_keep isl_union_map *umap2)
2381 struct isl_union_map_is_disjoint_data data = { NULL, isl_bool_true };
2383 umap1 = isl_union_map_copy(umap1);
2384 umap2 = isl_union_map_copy(umap2);
2385 umap1 = isl_union_map_align_params(umap1,
2386 isl_union_map_get_space(umap2));
2387 umap2 = isl_union_map_align_params(umap2,
2388 isl_union_map_get_space(umap1));
2390 if (!umap1 || !umap2)
2391 goto error;
2393 data.umap2 = umap2;
2394 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2395 &is_disjoint_entry, &data) < 0 &&
2396 data.is_disjoint)
2397 goto error;
2399 isl_union_map_free(umap1);
2400 isl_union_map_free(umap2);
2402 return data.is_disjoint;
2403 error:
2404 isl_union_map_free(umap1);
2405 isl_union_map_free(umap2);
2406 return isl_bool_error;
2409 /* Are "uset1" and "uset2" disjoint?
2411 isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_set *uset1,
2412 __isl_keep isl_union_set *uset2)
2414 return isl_union_map_is_disjoint(uset1, uset2);
2417 static isl_stat sample_entry(void **entry, void *user)
2419 isl_basic_map **sample = (isl_basic_map **)user;
2420 isl_map *map = *entry;
2422 *sample = isl_map_sample(isl_map_copy(map));
2423 if (!*sample)
2424 return isl_stat_error;
2425 if (!isl_basic_map_plain_is_empty(*sample))
2426 return isl_stat_error;
2427 return isl_stat_ok;
2430 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2432 isl_basic_map *sample = NULL;
2434 if (!umap)
2435 return NULL;
2437 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2438 &sample_entry, &sample) < 0 &&
2439 !sample)
2440 goto error;
2442 if (!sample)
2443 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2445 isl_union_map_free(umap);
2447 return sample;
2448 error:
2449 isl_union_map_free(umap);
2450 return NULL;
2453 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
2455 return bset_from_bmap(isl_union_map_sample(uset));
2458 /* Return an element in "uset" in the form of an isl_point.
2459 * Return a void isl_point if "uset" is empty.
2461 __isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_set *uset)
2463 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2466 struct isl_forall_data {
2467 isl_bool res;
2468 isl_bool (*fn)(__isl_keep isl_map *map);
2471 static isl_stat forall_entry(void **entry, void *user)
2473 struct isl_forall_data *data = user;
2474 isl_map *map = *entry;
2476 data->res = data->fn(map);
2477 if (data->res < 0)
2478 return isl_stat_error;
2480 if (!data->res)
2481 return isl_stat_error;
2483 return isl_stat_ok;
2486 static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2487 isl_bool (*fn)(__isl_keep isl_map *map))
2489 struct isl_forall_data data = { isl_bool_true, fn };
2491 if (!umap)
2492 return isl_bool_error;
2494 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2495 &forall_entry, &data) < 0 && data.res)
2496 return isl_bool_error;
2498 return data.res;
2501 struct isl_forall_user_data {
2502 isl_bool res;
2503 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2504 void *user;
2507 static isl_stat forall_user_entry(void **entry, void *user)
2509 struct isl_forall_user_data *data = user;
2510 isl_map *map = *entry;
2512 data->res = data->fn(map, data->user);
2513 if (data->res < 0)
2514 return isl_stat_error;
2516 if (!data->res)
2517 return isl_stat_error;
2519 return isl_stat_ok;
2522 /* Check if fn(map, user) returns true for all maps "map" in umap.
2524 static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2525 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2527 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2529 if (!umap)
2530 return isl_bool_error;
2532 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2533 &forall_user_entry, &data) < 0 && data.res)
2534 return isl_bool_error;
2536 return data.res;
2539 /* Is "umap" obviously empty?
2541 isl_bool isl_union_map_plain_is_empty(__isl_keep isl_union_map *umap)
2543 if (!umap)
2544 return isl_bool_error;
2545 return isl_union_map_n_map(umap) == 0;
2548 isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2550 return union_map_forall(umap, &isl_map_is_empty);
2553 isl_bool isl_union_set_is_empty(__isl_keep isl_union_set *uset)
2555 return isl_union_map_is_empty(uset);
2558 static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2560 isl_bool is_subset;
2561 isl_space *dim;
2562 isl_map *id;
2564 if (!map)
2565 return isl_bool_error;
2567 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2568 map->dim, isl_dim_out))
2569 return isl_bool_false;
2571 dim = isl_map_get_space(map);
2572 id = isl_map_identity(dim);
2574 is_subset = isl_map_is_subset(map, id);
2576 isl_map_free(id);
2578 return is_subset;
2581 /* Given an isl_union_map that consists of a single map, check
2582 * if it is single-valued.
2584 static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2586 isl_map *map;
2587 isl_bool sv;
2589 umap = isl_union_map_copy(umap);
2590 map = isl_map_from_union_map(umap);
2591 sv = isl_map_is_single_valued(map);
2592 isl_map_free(map);
2594 return sv;
2597 /* Internal data structure for single_valued_on_domain.
2599 * "umap" is the union map to be tested.
2600 * "sv" is set to 1 as long as "umap" may still be single-valued.
2602 struct isl_union_map_is_sv_data {
2603 isl_union_map *umap;
2604 isl_bool sv;
2607 /* Check if the data->umap is single-valued on "set".
2609 * If data->umap consists of a single map on "set", then test it
2610 * as an isl_map.
2612 * Otherwise, compute
2614 * M \circ M^-1
2616 * check if the result is a subset of the identity mapping and
2617 * store the result in data->sv.
2619 * Terminate as soon as data->umap has been determined not to
2620 * be single-valued.
2622 static isl_stat single_valued_on_domain(__isl_take isl_set *set, void *user)
2624 struct isl_union_map_is_sv_data *data = user;
2625 isl_union_map *umap, *test;
2627 umap = isl_union_map_copy(data->umap);
2628 umap = isl_union_map_intersect_domain(umap,
2629 isl_union_set_from_set(set));
2631 if (isl_union_map_n_map(umap) == 1) {
2632 data->sv = single_map_is_single_valued(umap);
2633 isl_union_map_free(umap);
2634 } else {
2635 test = isl_union_map_reverse(isl_union_map_copy(umap));
2636 test = isl_union_map_apply_range(test, umap);
2638 data->sv = union_map_forall(test, &is_subset_of_identity);
2640 isl_union_map_free(test);
2643 if (data->sv < 0 || !data->sv)
2644 return isl_stat_error;
2645 return isl_stat_ok;
2648 /* Check if the given map is single-valued.
2650 * If the union map consists of a single map, then test it as an isl_map.
2651 * Otherwise, check if the union map is single-valued on each of its
2652 * domain spaces.
2654 isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2656 isl_union_map *universe;
2657 isl_union_set *domain;
2658 struct isl_union_map_is_sv_data data;
2660 if (isl_union_map_n_map(umap) == 1)
2661 return single_map_is_single_valued(umap);
2663 universe = isl_union_map_universe(isl_union_map_copy(umap));
2664 domain = isl_union_map_domain(universe);
2666 data.sv = isl_bool_true;
2667 data.umap = umap;
2668 if (isl_union_set_foreach_set(domain,
2669 &single_valued_on_domain, &data) < 0 && data.sv)
2670 data.sv = isl_bool_error;
2671 isl_union_set_free(domain);
2673 return data.sv;
2676 isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2678 isl_bool in;
2680 umap = isl_union_map_copy(umap);
2681 umap = isl_union_map_reverse(umap);
2682 in = isl_union_map_is_single_valued(umap);
2683 isl_union_map_free(umap);
2685 return in;
2688 /* Is "map" obviously not an identity relation because
2689 * it maps elements from one space to another space?
2690 * Update *non_identity accordingly.
2692 * In particular, if the domain and range spaces are the same,
2693 * then the map is not considered to obviously not be an identity relation.
2694 * Otherwise, the map is considered to obviously not be an identity relation
2695 * if it is is non-empty.
2697 * If "map" is determined to obviously not be an identity relation,
2698 * then the search is aborted.
2700 static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2702 isl_bool *non_identity = user;
2703 isl_bool equal;
2704 isl_space *space;
2706 space = isl_map_get_space(map);
2707 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2708 if (equal >= 0 && !equal)
2709 *non_identity = isl_bool_not(isl_map_is_empty(map));
2710 else
2711 *non_identity = isl_bool_not(equal);
2712 isl_space_free(space);
2713 isl_map_free(map);
2715 if (*non_identity < 0 || *non_identity)
2716 return isl_stat_error;
2718 return isl_stat_ok;
2721 /* Is "umap" obviously not an identity relation because
2722 * it maps elements from one space to another space?
2724 * As soon as a map has been found that maps elements to a different space,
2725 * non_identity is changed and the search is aborted.
2727 static isl_bool isl_union_map_plain_is_not_identity(
2728 __isl_keep isl_union_map *umap)
2730 isl_bool non_identity;
2732 non_identity = isl_bool_false;
2733 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2734 &non_identity) < 0 &&
2735 non_identity == isl_bool_false)
2736 return isl_bool_error;
2738 return non_identity;
2741 /* Does "map" only map elements to themselves?
2742 * Update *identity accordingly.
2744 * If "map" is determined not to be an identity relation,
2745 * then the search is aborted.
2747 static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2749 isl_bool *identity = user;
2751 *identity = isl_map_is_identity(map);
2752 isl_map_free(map);
2754 if (*identity < 0 || !*identity)
2755 return isl_stat_error;
2757 return isl_stat_ok;
2760 /* Does "umap" only map elements to themselves?
2762 * First check if there are any maps that map elements to different spaces.
2763 * If not, then check that all the maps (between identical spaces)
2764 * are identity relations.
2766 isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2768 isl_bool non_identity;
2769 isl_bool identity;
2771 non_identity = isl_union_map_plain_is_not_identity(umap);
2772 if (non_identity < 0 || non_identity)
2773 return isl_bool_not(non_identity);
2775 identity = isl_bool_true;
2776 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2777 identity == isl_bool_true)
2778 return isl_bool_error;
2780 return identity;
2783 /* Represents a map that has a fixed value (v) for one of its
2784 * range dimensions.
2785 * The map in this structure is not reference counted, so it
2786 * is only valid while the isl_union_map from which it was
2787 * obtained is still alive.
2789 struct isl_fixed_map {
2790 isl_int v;
2791 isl_map *map;
2794 static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2795 int n)
2797 int i;
2798 struct isl_fixed_map *v;
2800 v = isl_calloc_array(ctx, struct isl_fixed_map, n);
2801 if (!v)
2802 return NULL;
2803 for (i = 0; i < n; ++i)
2804 isl_int_init(v[i].v);
2805 return v;
2808 static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2810 int i;
2812 if (!v)
2813 return;
2814 for (i = 0; i < n; ++i)
2815 isl_int_clear(v[i].v);
2816 free(v);
2819 /* Compare the "v" field of two isl_fixed_map structs.
2821 static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2823 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2824 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2826 return isl_int_cmp(e1->v, e2->v);
2829 /* Internal data structure used while checking whether all maps
2830 * in a union_map have a fixed value for a given output dimension.
2831 * v is the list of maps, with the fixed value for the dimension
2832 * n is the number of maps considered so far
2833 * pos is the output dimension under investigation
2835 struct isl_fixed_dim_data {
2836 struct isl_fixed_map *v;
2837 int n;
2838 int pos;
2841 static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2843 struct isl_fixed_dim_data *data = user;
2845 data->v[data->n].map = map;
2846 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2847 &data->v[data->n++].v);
2850 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2851 int first, int n_range);
2853 /* Given a list of the maps, with their fixed values at output dimension "pos",
2854 * check whether the ranges of the maps form an obvious partition.
2856 * We first sort the maps according to their fixed values.
2857 * If all maps have a different value, then we know the ranges form
2858 * a partition.
2859 * Otherwise, we collect the maps with the same fixed value and
2860 * check whether each such collection is obviously injective
2861 * based on later dimensions.
2863 static int separates(struct isl_fixed_map *v, int n,
2864 __isl_take isl_space *dim, int pos, int n_range)
2866 int i;
2868 if (!v)
2869 goto error;
2871 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2873 for (i = 0; i + 1 < n; ++i) {
2874 int j, k;
2875 isl_union_map *part;
2876 int injective;
2878 for (j = i + 1; j < n; ++j)
2879 if (isl_int_ne(v[i].v, v[j].v))
2880 break;
2882 if (j == i + 1)
2883 continue;
2885 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2886 for (k = i; k < j; ++k)
2887 part = isl_union_map_add_map(part,
2888 isl_map_copy(v[k].map));
2890 injective = plain_injective_on_range(part, pos + 1, n_range);
2891 if (injective < 0)
2892 goto error;
2893 if (!injective)
2894 break;
2896 i = j - 1;
2899 isl_space_free(dim);
2900 free_isl_fixed_map_array(v, n);
2901 return i + 1 >= n;
2902 error:
2903 isl_space_free(dim);
2904 free_isl_fixed_map_array(v, n);
2905 return -1;
2908 /* Check whether the maps in umap have obviously distinct ranges.
2909 * In particular, check for an output dimension in the range
2910 * [first,n_range) for which all maps have a fixed value
2911 * and then check if these values, possibly along with fixed values
2912 * at later dimensions, entail distinct ranges.
2914 static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2915 int first, int n_range)
2917 isl_ctx *ctx;
2918 int n;
2919 struct isl_fixed_dim_data data = { NULL };
2921 ctx = isl_union_map_get_ctx(umap);
2923 n = isl_union_map_n_map(umap);
2924 if (!umap)
2925 goto error;
2927 if (n <= 1) {
2928 isl_union_map_free(umap);
2929 return isl_bool_true;
2932 if (first >= n_range) {
2933 isl_union_map_free(umap);
2934 return isl_bool_false;
2937 data.v = alloc_isl_fixed_map_array(ctx, n);
2938 if (!data.v)
2939 goto error;
2941 for (data.pos = first; data.pos < n_range; ++data.pos) {
2942 isl_bool fixed;
2943 int injective;
2944 isl_space *dim;
2946 data.n = 0;
2947 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2948 if (fixed < 0)
2949 goto error;
2950 if (!fixed)
2951 continue;
2952 dim = isl_union_map_get_space(umap);
2953 injective = separates(data.v, n, dim, data.pos, n_range);
2954 isl_union_map_free(umap);
2955 return injective;
2958 free_isl_fixed_map_array(data.v, n);
2959 isl_union_map_free(umap);
2961 return isl_bool_false;
2962 error:
2963 free_isl_fixed_map_array(data.v, n);
2964 isl_union_map_free(umap);
2965 return isl_bool_error;
2968 /* Check whether the maps in umap that map to subsets of "ran"
2969 * have obviously distinct ranges.
2971 static isl_bool plain_injective_on_range_wrap(__isl_keep isl_set *ran,
2972 void *user)
2974 isl_union_map *umap = user;
2976 umap = isl_union_map_copy(umap);
2977 umap = isl_union_map_intersect_range(umap,
2978 isl_union_set_from_set(isl_set_copy(ran)));
2979 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2982 /* Check if the given union_map is obviously injective.
2984 * In particular, we first check if all individual maps are obviously
2985 * injective and then check if all the ranges of these maps are
2986 * obviously disjoint.
2988 isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2990 isl_bool in;
2991 isl_union_map *univ;
2992 isl_union_set *ran;
2994 in = union_map_forall(umap, &isl_map_plain_is_injective);
2995 if (in < 0)
2996 return isl_bool_error;
2997 if (!in)
2998 return isl_bool_false;
3000 univ = isl_union_map_universe(isl_union_map_copy(umap));
3001 ran = isl_union_map_range(univ);
3003 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
3005 isl_union_set_free(ran);
3007 return in;
3010 isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
3012 isl_bool sv;
3014 sv = isl_union_map_is_single_valued(umap);
3015 if (sv < 0 || !sv)
3016 return sv;
3018 return isl_union_map_is_injective(umap);
3021 __isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3023 struct isl_un_op_drop_user_data data = { &isl_map_can_zip };
3024 struct isl_un_op_control control = {
3025 .filter = &un_op_filter_drop_user,
3026 .filter_user = &data,
3027 .fn_map = &isl_map_zip,
3029 return un_op(umap, &control);
3032 /* Given a union map, take the maps of the form A -> (B -> C) and
3033 * return the union of the corresponding maps (A -> B) -> C.
3035 __isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3037 struct isl_un_op_drop_user_data data = { &isl_map_can_uncurry };
3038 struct isl_un_op_control control = {
3039 .filter = &un_op_filter_drop_user,
3040 .filter_user = &data,
3041 .fn_map = &isl_map_uncurry,
3043 return un_op(umap, &control);
3046 /* Given a union map, take the maps of the form (A -> B) -> C and
3047 * return the union of the corresponding maps A -> (B -> C).
3049 __isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3051 struct isl_un_op_drop_user_data data = { &isl_map_can_curry };
3052 struct isl_un_op_control control = {
3053 .filter = &un_op_filter_drop_user,
3054 .filter_user = &data,
3055 .fn_map = &isl_map_curry,
3057 return un_op(umap, &control);
3060 /* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3061 * return the union of the corresponding maps A -> (B -> (C -> D)).
3063 __isl_give isl_union_map *isl_union_map_range_curry(
3064 __isl_take isl_union_map *umap)
3066 struct isl_un_op_drop_user_data data = { &isl_map_can_range_curry };
3067 struct isl_un_op_control control = {
3068 .filter = &un_op_filter_drop_user,
3069 .filter_user = &data,
3070 .fn_map = &isl_map_range_curry,
3072 return un_op(umap, &control);
3075 __isl_give isl_union_set *isl_union_set_lift(__isl_take isl_union_set *uset)
3077 struct isl_un_op_control control = {
3078 .fn_map = &isl_set_lift,
3080 return un_op(uset, &control);
3083 static isl_stat coefficients_entry(void **entry, void *user)
3085 isl_set *set = *entry;
3086 isl_union_set **res = user;
3088 set = isl_set_copy(set);
3089 set = isl_set_from_basic_set(isl_set_coefficients(set));
3090 *res = isl_union_set_add_set(*res, set);
3092 return isl_stat_ok;
3095 __isl_give isl_union_set *isl_union_set_coefficients(
3096 __isl_take isl_union_set *uset)
3098 isl_ctx *ctx;
3099 isl_space *dim;
3100 isl_union_set *res;
3102 if (!uset)
3103 return NULL;
3105 ctx = isl_union_set_get_ctx(uset);
3106 dim = isl_space_set_alloc(ctx, 0, 0);
3107 res = isl_union_map_alloc(dim, uset->table.n);
3108 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3109 &coefficients_entry, &res) < 0)
3110 goto error;
3112 isl_union_set_free(uset);
3113 return res;
3114 error:
3115 isl_union_set_free(uset);
3116 isl_union_set_free(res);
3117 return NULL;
3120 static isl_stat solutions_entry(void **entry, void *user)
3122 isl_set *set = *entry;
3123 isl_union_set **res = user;
3125 set = isl_set_copy(set);
3126 set = isl_set_from_basic_set(isl_set_solutions(set));
3127 if (!*res)
3128 *res = isl_union_set_from_set(set);
3129 else
3130 *res = isl_union_set_add_set(*res, set);
3132 if (!*res)
3133 return isl_stat_error;
3135 return isl_stat_ok;
3138 __isl_give isl_union_set *isl_union_set_solutions(
3139 __isl_take isl_union_set *uset)
3141 isl_union_set *res = NULL;
3143 if (!uset)
3144 return NULL;
3146 if (uset->table.n == 0) {
3147 res = isl_union_set_empty(isl_union_set_get_space(uset));
3148 isl_union_set_free(uset);
3149 return res;
3152 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3153 &solutions_entry, &res) < 0)
3154 goto error;
3156 isl_union_set_free(uset);
3157 return res;
3158 error:
3159 isl_union_set_free(uset);
3160 isl_union_set_free(res);
3161 return NULL;
3164 /* Is the domain space of "map" equal to "space"?
3166 static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3168 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3169 space, isl_dim_out);
3172 /* Is the range space of "map" equal to "space"?
3174 static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3176 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3177 space, isl_dim_out);
3180 /* Is the set space of "map" equal to "space"?
3182 static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3184 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3185 space, isl_dim_out);
3188 /* Internal data structure for preimage_pw_multi_aff.
3190 * "pma" is the function under which the preimage should be taken.
3191 * "space" is the space of "pma".
3192 * "res" collects the results.
3193 * "fn" computes the preimage for a given map.
3194 * "match" returns true if "fn" can be called.
3196 struct isl_union_map_preimage_data {
3197 isl_space *space;
3198 isl_pw_multi_aff *pma;
3199 isl_union_map *res;
3200 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3201 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3202 __isl_take isl_pw_multi_aff *pma);
3205 /* Call data->fn to compute the preimage of the domain or range of *entry
3206 * under the function represented by data->pma, provided the domain/range
3207 * space of *entry matches the target space of data->pma
3208 * (as given by data->match), and add the result to data->res.
3210 static isl_stat preimage_entry(void **entry, void *user)
3212 int m;
3213 isl_map *map = *entry;
3214 struct isl_union_map_preimage_data *data = user;
3215 isl_bool empty;
3217 m = data->match(map, data->space);
3218 if (m < 0)
3219 return isl_stat_error;
3220 if (!m)
3221 return isl_stat_ok;
3223 map = isl_map_copy(map);
3224 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3226 empty = isl_map_is_empty(map);
3227 if (empty < 0 || empty) {
3228 isl_map_free(map);
3229 return empty < 0 ? isl_stat_error : isl_stat_ok;
3232 data->res = isl_union_map_add_map(data->res, map);
3234 return isl_stat_ok;
3237 /* Compute the preimage of the domain or range of "umap" under the function
3238 * represented by "pma".
3239 * In other words, plug in "pma" in the domain or range of "umap".
3240 * The function "fn" performs the actual preimage computation on a map,
3241 * while "match" determines to which maps the function should be applied.
3243 static __isl_give isl_union_map *preimage_pw_multi_aff(
3244 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3245 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3246 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3247 __isl_take isl_pw_multi_aff *pma))
3249 isl_ctx *ctx;
3250 isl_space *space;
3251 struct isl_union_map_preimage_data data;
3253 umap = isl_union_map_align_params(umap,
3254 isl_pw_multi_aff_get_space(pma));
3255 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3257 if (!umap || !pma)
3258 goto error;
3260 ctx = isl_union_map_get_ctx(umap);
3261 space = isl_union_map_get_space(umap);
3262 data.space = isl_pw_multi_aff_get_space(pma);
3263 data.pma = pma;
3264 data.res = isl_union_map_alloc(space, umap->table.n);
3265 data.match = match;
3266 data.fn = fn;
3267 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3268 &data) < 0)
3269 data.res = isl_union_map_free(data.res);
3271 isl_space_free(data.space);
3272 isl_union_map_free(umap);
3273 isl_pw_multi_aff_free(pma);
3274 return data.res;
3275 error:
3276 isl_union_map_free(umap);
3277 isl_pw_multi_aff_free(pma);
3278 return NULL;
3281 /* Compute the preimage of the domain of "umap" under the function
3282 * represented by "pma".
3283 * In other words, plug in "pma" in the domain of "umap".
3284 * The result contains maps that live in the same spaces as the maps of "umap"
3285 * with domain space equal to the target space of "pma",
3286 * except that the domain has been replaced by the domain space of "pma".
3288 __isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3289 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3291 return preimage_pw_multi_aff(umap, pma, &domain_match,
3292 &isl_map_preimage_domain_pw_multi_aff);
3295 /* Compute the preimage of the range of "umap" under the function
3296 * represented by "pma".
3297 * In other words, plug in "pma" in the range of "umap".
3298 * The result contains maps that live in the same spaces as the maps of "umap"
3299 * with range space equal to the target space of "pma",
3300 * except that the range has been replaced by the domain space of "pma".
3302 __isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3303 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3305 return preimage_pw_multi_aff(umap, pma, &range_match,
3306 &isl_map_preimage_range_pw_multi_aff);
3309 /* Compute the preimage of "uset" under the function represented by "pma".
3310 * In other words, plug in "pma" in "uset".
3311 * The result contains sets that live in the same spaces as the sets of "uset"
3312 * with space equal to the target space of "pma",
3313 * except that the space has been replaced by the domain space of "pma".
3315 __isl_give isl_union_set *isl_union_set_preimage_pw_multi_aff(
3316 __isl_take isl_union_set *uset, __isl_take isl_pw_multi_aff *pma)
3318 return preimage_pw_multi_aff(uset, pma, &set_match,
3319 &isl_set_preimage_pw_multi_aff);
3322 /* Compute the preimage of the domain of "umap" under the function
3323 * represented by "ma".
3324 * In other words, plug in "ma" in the domain of "umap".
3325 * The result contains maps that live in the same spaces as the maps of "umap"
3326 * with domain space equal to the target space of "ma",
3327 * except that the domain has been replaced by the domain space of "ma".
3329 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3330 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3332 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3333 isl_pw_multi_aff_from_multi_aff(ma));
3336 /* Compute the preimage of the range of "umap" under the function
3337 * represented by "ma".
3338 * In other words, plug in "ma" in the range of "umap".
3339 * The result contains maps that live in the same spaces as the maps of "umap"
3340 * with range space equal to the target space of "ma",
3341 * except that the range has been replaced by the domain space of "ma".
3343 __isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3344 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3346 return isl_union_map_preimage_range_pw_multi_aff(umap,
3347 isl_pw_multi_aff_from_multi_aff(ma));
3350 /* Compute the preimage of "uset" under the function represented by "ma".
3351 * In other words, plug in "ma" in "uset".
3352 * The result contains sets that live in the same spaces as the sets of "uset"
3353 * with space equal to the target space of "ma",
3354 * except that the space has been replaced by the domain space of "ma".
3356 __isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3357 __isl_take isl_union_set *uset, __isl_take isl_multi_aff *ma)
3359 return isl_union_set_preimage_pw_multi_aff(uset,
3360 isl_pw_multi_aff_from_multi_aff(ma));
3363 /* Internal data structure for preimage_multi_pw_aff.
3365 * "mpa" is the function under which the preimage should be taken.
3366 * "space" is the space of "mpa".
3367 * "res" collects the results.
3368 * "fn" computes the preimage for a given map.
3369 * "match" returns true if "fn" can be called.
3371 struct isl_union_map_preimage_mpa_data {
3372 isl_space *space;
3373 isl_multi_pw_aff *mpa;
3374 isl_union_map *res;
3375 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3376 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3377 __isl_take isl_multi_pw_aff *mpa);
3380 /* Call data->fn to compute the preimage of the domain or range of *entry
3381 * under the function represented by data->mpa, provided the domain/range
3382 * space of *entry matches the target space of data->mpa
3383 * (as given by data->match), and add the result to data->res.
3385 static isl_stat preimage_mpa_entry(void **entry, void *user)
3387 int m;
3388 isl_map *map = *entry;
3389 struct isl_union_map_preimage_mpa_data *data = user;
3390 isl_bool empty;
3392 m = data->match(map, data->space);
3393 if (m < 0)
3394 return isl_stat_error;
3395 if (!m)
3396 return isl_stat_ok;
3398 map = isl_map_copy(map);
3399 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3401 empty = isl_map_is_empty(map);
3402 if (empty < 0 || empty) {
3403 isl_map_free(map);
3404 return empty < 0 ? isl_stat_error : isl_stat_ok;
3407 data->res = isl_union_map_add_map(data->res, map);
3409 return isl_stat_ok;
3412 /* Compute the preimage of the domain or range of "umap" under the function
3413 * represented by "mpa".
3414 * In other words, plug in "mpa" in the domain or range of "umap".
3415 * The function "fn" performs the actual preimage computation on a map,
3416 * while "match" determines to which maps the function should be applied.
3418 static __isl_give isl_union_map *preimage_multi_pw_aff(
3419 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3420 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3421 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3422 __isl_take isl_multi_pw_aff *mpa))
3424 isl_ctx *ctx;
3425 isl_space *space;
3426 struct isl_union_map_preimage_mpa_data data;
3428 umap = isl_union_map_align_params(umap,
3429 isl_multi_pw_aff_get_space(mpa));
3430 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3432 if (!umap || !mpa)
3433 goto error;
3435 ctx = isl_union_map_get_ctx(umap);
3436 space = isl_union_map_get_space(umap);
3437 data.space = isl_multi_pw_aff_get_space(mpa);
3438 data.mpa = mpa;
3439 data.res = isl_union_map_alloc(space, umap->table.n);
3440 data.match = match;
3441 data.fn = fn;
3442 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3443 &data) < 0)
3444 data.res = isl_union_map_free(data.res);
3446 isl_space_free(data.space);
3447 isl_union_map_free(umap);
3448 isl_multi_pw_aff_free(mpa);
3449 return data.res;
3450 error:
3451 isl_union_map_free(umap);
3452 isl_multi_pw_aff_free(mpa);
3453 return NULL;
3456 /* Compute the preimage of the domain of "umap" under the function
3457 * represented by "mpa".
3458 * In other words, plug in "mpa" in the domain of "umap".
3459 * The result contains maps that live in the same spaces as the maps of "umap"
3460 * with domain space equal to the target space of "mpa",
3461 * except that the domain has been replaced by the domain space of "mpa".
3463 __isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3464 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3466 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3467 &isl_map_preimage_domain_multi_pw_aff);
3470 /* Internal data structure for preimage_upma.
3472 * "umap" is the map of which the preimage should be computed.
3473 * "res" collects the results.
3474 * "fn" computes the preimage for a given piecewise multi-affine function.
3476 struct isl_union_map_preimage_upma_data {
3477 isl_union_map *umap;
3478 isl_union_map *res;
3479 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3480 __isl_take isl_pw_multi_aff *pma);
3483 /* Call data->fn to compute the preimage of the domain or range of data->umap
3484 * under the function represented by pma and add the result to data->res.
3486 static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3488 struct isl_union_map_preimage_upma_data *data = user;
3489 isl_union_map *umap;
3491 umap = isl_union_map_copy(data->umap);
3492 umap = data->fn(umap, pma);
3493 data->res = isl_union_map_union(data->res, umap);
3495 return data->res ? isl_stat_ok : isl_stat_error;
3498 /* Compute the preimage of the domain or range of "umap" under the function
3499 * represented by "upma".
3500 * In other words, plug in "upma" in the domain or range of "umap".
3501 * The function "fn" performs the actual preimage computation
3502 * on a piecewise multi-affine function.
3504 static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3505 __isl_take isl_union_map *umap,
3506 __isl_take isl_union_pw_multi_aff *upma,
3507 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3508 __isl_take isl_pw_multi_aff *pma))
3510 struct isl_union_map_preimage_upma_data data;
3512 data.umap = umap;
3513 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3514 data.fn = fn;
3515 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3516 &preimage_upma, &data) < 0)
3517 data.res = isl_union_map_free(data.res);
3519 isl_union_map_free(umap);
3520 isl_union_pw_multi_aff_free(upma);
3522 return data.res;
3525 /* Compute the preimage of the domain of "umap" under the function
3526 * represented by "upma".
3527 * In other words, plug in "upma" in the domain of "umap".
3528 * The result contains maps that live in the same spaces as the maps of "umap"
3529 * with domain space equal to one of the target spaces of "upma",
3530 * except that the domain has been replaced by one of the domain spaces that
3531 * correspond to that target space of "upma".
3533 __isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3534 __isl_take isl_union_map *umap,
3535 __isl_take isl_union_pw_multi_aff *upma)
3537 return preimage_union_pw_multi_aff(umap, upma,
3538 &isl_union_map_preimage_domain_pw_multi_aff);
3541 /* Compute the preimage of the range of "umap" under the function
3542 * represented by "upma".
3543 * In other words, plug in "upma" in the range of "umap".
3544 * The result contains maps that live in the same spaces as the maps of "umap"
3545 * with range space equal to one of the target spaces of "upma",
3546 * except that the range has been replaced by one of the domain spaces that
3547 * correspond to that target space of "upma".
3549 __isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3550 __isl_take isl_union_map *umap,
3551 __isl_take isl_union_pw_multi_aff *upma)
3553 return preimage_union_pw_multi_aff(umap, upma,
3554 &isl_union_map_preimage_range_pw_multi_aff);
3557 /* Compute the preimage of "uset" under the function represented by "upma".
3558 * In other words, plug in "upma" in the range of "uset".
3559 * The result contains sets that live in the same spaces as the sets of "uset"
3560 * with space equal to one of the target spaces of "upma",
3561 * except that the space has been replaced by one of the domain spaces that
3562 * correspond to that target space of "upma".
3564 __isl_give isl_union_set *isl_union_set_preimage_union_pw_multi_aff(
3565 __isl_take isl_union_set *uset,
3566 __isl_take isl_union_pw_multi_aff *upma)
3568 return preimage_union_pw_multi_aff(uset, upma,
3569 &isl_union_set_preimage_pw_multi_aff);
3572 /* Reset the user pointer on all identifiers of parameters and tuples
3573 * of the spaces of "umap".
3575 __isl_give isl_union_map *isl_union_map_reset_user(
3576 __isl_take isl_union_map *umap)
3578 umap = isl_union_map_cow(umap);
3579 if (!umap)
3580 return NULL;
3581 umap->dim = isl_space_reset_user(umap->dim);
3582 if (!umap->dim)
3583 return isl_union_map_free(umap);
3584 return total(umap, &isl_map_reset_user);
3587 /* Reset the user pointer on all identifiers of parameters and tuples
3588 * of the spaces of "uset".
3590 __isl_give isl_union_set *isl_union_set_reset_user(
3591 __isl_take isl_union_set *uset)
3593 return isl_union_map_reset_user(uset);
3596 /* Remove all existentially quantified variables and integer divisions
3597 * from "umap" using Fourier-Motzkin elimination.
3599 __isl_give isl_union_map *isl_union_map_remove_divs(
3600 __isl_take isl_union_map *umap)
3602 return total(umap, &isl_map_remove_divs);
3605 /* Remove all existentially quantified variables and integer divisions
3606 * from "uset" using Fourier-Motzkin elimination.
3608 __isl_give isl_union_set *isl_union_set_remove_divs(
3609 __isl_take isl_union_set *uset)
3611 return isl_union_map_remove_divs(uset);
3614 /* Internal data structure for isl_union_map_project_out.
3615 * "type", "first" and "n" are the arguments for the isl_map_project_out
3616 * call.
3617 * "res" collects the results.
3619 struct isl_union_map_project_out_data {
3620 enum isl_dim_type type;
3621 unsigned first;
3622 unsigned n;
3624 isl_union_map *res;
3627 /* Turn the data->n dimensions of type data->type, starting at data->first
3628 * into existentially quantified variables and add the result to data->res.
3630 static isl_stat project_out(__isl_take isl_map *map, void *user)
3632 struct isl_union_map_project_out_data *data = user;
3634 map = isl_map_project_out(map, data->type, data->first, data->n);
3635 data->res = isl_union_map_add_map(data->res, map);
3637 return isl_stat_ok;
3640 /* Turn the "n" dimensions of type "type", starting at "first"
3641 * into existentially quantified variables.
3642 * Since the space of an isl_union_map only contains parameters,
3643 * type is required to be equal to isl_dim_param.
3645 __isl_give isl_union_map *isl_union_map_project_out(
3646 __isl_take isl_union_map *umap,
3647 enum isl_dim_type type, unsigned first, unsigned n)
3649 isl_space *space;
3650 struct isl_union_map_project_out_data data = { type, first, n };
3652 if (!umap)
3653 return NULL;
3655 if (type != isl_dim_param)
3656 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3657 "can only project out parameters",
3658 return isl_union_map_free(umap));
3660 space = isl_union_map_get_space(umap);
3661 space = isl_space_drop_dims(space, type, first, n);
3662 data.res = isl_union_map_empty(space);
3663 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3664 data.res = isl_union_map_free(data.res);
3666 isl_union_map_free(umap);
3668 return data.res;
3671 /* Project out all parameters from "umap" by existentially quantifying
3672 * over them.
3674 __isl_give isl_union_map *isl_union_map_project_out_all_params(
3675 __isl_take isl_union_map *umap)
3677 unsigned n;
3679 if (!umap)
3680 return NULL;
3681 n = isl_union_map_dim(umap, isl_dim_param);
3682 return isl_union_map_project_out(umap, isl_dim_param, 0, n);
3685 /* Turn the "n" dimensions of type "type", starting at "first"
3686 * into existentially quantified variables.
3687 * Since the space of an isl_union_set only contains parameters,
3688 * "type" is required to be equal to isl_dim_param.
3690 __isl_give isl_union_set *isl_union_set_project_out(
3691 __isl_take isl_union_set *uset,
3692 enum isl_dim_type type, unsigned first, unsigned n)
3694 return isl_union_map_project_out(uset, type, first, n);
3697 /* Internal data structure for isl_union_map_involves_dims.
3698 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3700 struct isl_union_map_involves_dims_data {
3701 unsigned first;
3702 unsigned n;
3705 /* Does "map" _not_ involve the data->n parameters starting at data->first?
3707 static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3709 struct isl_union_map_involves_dims_data *data = user;
3710 isl_bool involves;
3712 involves = isl_map_involves_dims(map,
3713 isl_dim_param, data->first, data->n);
3714 if (involves < 0)
3715 return isl_bool_error;
3716 return !involves;
3719 /* Does "umap" involve any of the n parameters starting at first?
3720 * "type" is required to be set to isl_dim_param.
3722 * "umap" involves any of those parameters if any of its maps
3723 * involve the parameters. In other words, "umap" does not
3724 * involve any of the parameters if all its maps to not
3725 * involve the parameters.
3727 isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3728 enum isl_dim_type type, unsigned first, unsigned n)
3730 struct isl_union_map_involves_dims_data data = { first, n };
3731 isl_bool excludes;
3733 if (type != isl_dim_param)
3734 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3735 "can only reference parameters", return isl_bool_error);
3737 excludes = union_map_forall_user(umap, &map_excludes, &data);
3739 if (excludes < 0)
3740 return isl_bool_error;
3742 return !excludes;
3745 /* Internal data structure for isl_union_map_reset_range_space.
3746 * "range" is the space from which to set the range space.
3747 * "res" collects the results.
3749 struct isl_union_map_reset_range_space_data {
3750 isl_space *range;
3751 isl_union_map *res;
3754 /* Replace the range space of "map" by the range space of data->range and
3755 * add the result to data->res.
3757 static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3759 struct isl_union_map_reset_range_space_data *data = user;
3760 isl_space *space;
3762 space = isl_map_get_space(map);
3763 space = isl_space_domain(space);
3764 space = isl_space_extend_domain_with_range(space,
3765 isl_space_copy(data->range));
3766 map = isl_map_reset_space(map, space);
3767 data->res = isl_union_map_add_map(data->res, map);
3769 return data->res ? isl_stat_ok : isl_stat_error;
3772 /* Replace the range space of all the maps in "umap" by
3773 * the range space of "space".
3775 * This assumes that all maps have the same output dimension.
3776 * This function should therefore not be made publicly available.
3778 * Since the spaces of the maps change, so do their hash value.
3779 * We therefore need to create a new isl_union_map.
3781 __isl_give isl_union_map *isl_union_map_reset_range_space(
3782 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3784 struct isl_union_map_reset_range_space_data data = { space };
3786 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3787 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3788 data.res = isl_union_map_free(data.res);
3790 isl_space_free(space);
3791 isl_union_map_free(umap);
3792 return data.res;
3795 /* Check that "umap" and "space" have the same number of parameters.
3797 static isl_stat check_union_map_space_equal_dim(__isl_keep isl_union_map *umap,
3798 __isl_keep isl_space *space)
3800 unsigned dim1, dim2;
3802 if (!umap || !space)
3803 return isl_stat_error;
3804 dim1 = isl_union_map_dim(umap, isl_dim_param);
3805 dim2 = isl_space_dim(space, isl_dim_param);
3806 if (dim1 == dim2)
3807 return isl_stat_ok;
3808 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,
3809 "number of parameters does not match", return isl_stat_error);
3812 /* Internal data structure for isl_union_map_reset_equal_dim_space.
3813 * "space" is the target space.
3814 * "res" collects the results.
3816 struct isl_union_map_reset_params_data {
3817 isl_space *space;
3818 isl_union_map *res;
3821 /* Replace the parameters of "map" by those of data->space and
3822 * add the result to data->res.
3824 static isl_stat reset_params(__isl_take isl_map *map, void *user)
3826 struct isl_union_map_reset_params_data *data = user;
3827 isl_space *space;
3829 space = isl_map_get_space(map);
3830 space = isl_space_replace_params(space, data->space);
3831 map = isl_map_reset_equal_dim_space(map, space);
3832 data->res = isl_union_map_add_map(data->res, map);
3834 return data->res ? isl_stat_ok : isl_stat_error;
3837 /* Replace the space of "umap" by "space", without modifying
3838 * the dimension of "umap", i.e., the number of parameters of "umap".
3840 * Since the hash values of the maps in the union map depend
3841 * on the parameters, a new union map needs to be constructed.
3843 __isl_give isl_union_map *isl_union_map_reset_equal_dim_space(
3844 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3846 struct isl_union_map_reset_params_data data = { space };
3847 isl_bool equal;
3848 isl_space *umap_space;
3850 umap_space = isl_union_map_peek_space(umap);
3851 equal = isl_space_is_equal(umap_space, space);
3852 if (equal < 0)
3853 goto error;
3854 if (equal) {
3855 isl_space_free(space);
3856 return umap;
3858 if (check_union_map_space_equal_dim(umap, space) < 0)
3859 goto error;
3861 data.res = isl_union_map_empty(isl_space_copy(space));
3862 if (isl_union_map_foreach_map(umap, &reset_params, &data) < 0)
3863 data.res = isl_union_map_free(data.res);
3865 isl_space_free(space);
3866 isl_union_map_free(umap);
3867 return data.res;
3868 error:
3869 isl_union_map_free(umap);
3870 isl_space_free(space);
3871 return NULL;
3874 /* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3875 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3876 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3877 * to the domain and the range of each map.
3878 * "res" collects the results.
3880 struct isl_union_order_at_data {
3881 isl_multi_union_pw_aff *mupa;
3882 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3883 __isl_take isl_multi_pw_aff *mpa2);
3884 isl_union_map *res;
3887 /* Intersect "map" with the result of applying data->order to
3888 * the functions in data->mupa that apply to the domain and the range
3889 * of "map" and add the result to data->res.
3891 static isl_stat order_at(__isl_take isl_map *map, void *user)
3893 struct isl_union_order_at_data *data = user;
3894 isl_space *space;
3895 isl_multi_pw_aff *mpa1, *mpa2;
3896 isl_map *order;
3898 space = isl_space_domain(isl_map_get_space(map));
3899 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3900 space = isl_space_range(isl_map_get_space(map));
3901 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3902 order = data->order(mpa1, mpa2);
3903 map = isl_map_intersect(map, order);
3904 data->res = isl_union_map_add_map(data->res, map);
3906 return data->res ? isl_stat_ok : isl_stat_error;
3909 /* If "mupa" has a non-trivial explicit domain, then intersect
3910 * domain and range of "umap" with this explicit domain.
3911 * If the explicit domain only describes constraints on the parameters,
3912 * then the intersection only needs to be performed once.
3914 static __isl_give isl_union_map *intersect_explicit_domain(
3915 __isl_take isl_union_map *umap, __isl_keep isl_multi_union_pw_aff *mupa)
3917 isl_bool non_trivial, is_params;
3918 isl_union_set *dom;
3920 non_trivial = isl_multi_union_pw_aff_has_non_trivial_domain(mupa);
3921 if (non_trivial < 0)
3922 return isl_union_map_free(umap);
3923 if (!non_trivial)
3924 return umap;
3925 mupa = isl_multi_union_pw_aff_copy(mupa);
3926 dom = isl_multi_union_pw_aff_domain(mupa);
3927 is_params = isl_union_set_is_params(dom);
3928 if (is_params < 0) {
3929 isl_union_set_free(dom);
3930 return isl_union_map_free(umap);
3932 if (is_params) {
3933 isl_set *set;
3935 set = isl_union_set_params(dom);
3936 umap = isl_union_map_intersect_params(umap, set);
3937 return umap;
3939 umap = isl_union_map_intersect_domain(umap, isl_union_set_copy(dom));
3940 umap = isl_union_map_intersect_range(umap, dom);
3941 return umap;
3944 /* Intersect each map in "umap" with the result of calling "order"
3945 * on the functions is "mupa" that apply to the domain and the range
3946 * of the map.
3948 static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3949 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3950 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3951 __isl_take isl_multi_pw_aff *mpa2))
3953 struct isl_union_order_at_data data;
3955 umap = isl_union_map_align_params(umap,
3956 isl_multi_union_pw_aff_get_space(mupa));
3957 mupa = isl_multi_union_pw_aff_align_params(mupa,
3958 isl_union_map_get_space(umap));
3959 umap = intersect_explicit_domain(umap, mupa);
3960 data.mupa = mupa;
3961 data.order = order;
3962 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3963 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
3964 data.res = isl_union_map_free(data.res);
3966 isl_multi_union_pw_aff_free(mupa);
3967 isl_union_map_free(umap);
3968 return data.res;
3971 /* Return the subset of "umap" where the domain and the range
3972 * have equal "mupa" values.
3974 __isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
3975 __isl_take isl_union_map *umap,
3976 __isl_take isl_multi_union_pw_aff *mupa)
3978 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3979 &isl_multi_pw_aff_eq_map);
3982 /* Return the subset of "umap" where the domain has a lexicographically
3983 * smaller "mupa" value than the range.
3985 __isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
3986 __isl_take isl_union_map *umap,
3987 __isl_take isl_multi_union_pw_aff *mupa)
3989 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3990 &isl_multi_pw_aff_lex_lt_map);
3993 /* Return the subset of "umap" where the domain has a lexicographically
3994 * greater "mupa" value than the range.
3996 __isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
3997 __isl_take isl_union_map *umap,
3998 __isl_take isl_multi_union_pw_aff *mupa)
4000 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
4001 &isl_multi_pw_aff_lex_gt_map);
4004 /* Return the union of the elements in the list "list".
4006 __isl_give isl_union_set *isl_union_set_list_union(
4007 __isl_take isl_union_set_list *list)
4009 int i, n;
4010 isl_ctx *ctx;
4011 isl_space *space;
4012 isl_union_set *res;
4014 if (!list)
4015 return NULL;
4017 ctx = isl_union_set_list_get_ctx(list);
4018 space = isl_space_params_alloc(ctx, 0);
4019 res = isl_union_set_empty(space);
4021 n = isl_union_set_list_n_union_set(list);
4022 for (i = 0; i < n; ++i) {
4023 isl_union_set *uset_i;
4025 uset_i = isl_union_set_list_get_union_set(list, i);
4026 res = isl_union_set_union(res, uset_i);
4029 isl_union_set_list_free(list);
4030 return res;
4033 /* Update *hash with the hash value of "map".
4035 static isl_stat add_hash(__isl_take isl_map *map, void *user)
4037 uint32_t *hash = user;
4038 uint32_t map_hash;
4040 map_hash = isl_map_get_hash(map);
4041 isl_hash_hash(*hash, map_hash);
4043 isl_map_free(map);
4044 return isl_stat_ok;
4047 /* Return a hash value that digests "umap".
4049 uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
4051 uint32_t hash;
4053 if (!umap)
4054 return 0;
4056 hash = isl_hash_init();
4057 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
4058 return 0;
4060 return hash;
4063 /* Return a hash value that digests "uset".
4065 uint32_t isl_union_set_get_hash(__isl_keep isl_union_set *uset)
4067 return isl_union_map_get_hash(uset);
4070 /* Add the number of basic sets in "set" to "n".
4072 static isl_stat add_n(__isl_take isl_set *set, void *user)
4074 int *n = user;
4076 *n += isl_set_n_basic_set(set);
4077 isl_set_free(set);
4079 return isl_stat_ok;
4082 /* Return the total number of basic sets in "uset".
4084 int isl_union_set_n_basic_set(__isl_keep isl_union_set *uset)
4086 int n = 0;
4088 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
4089 return -1;
4091 return n;
4094 /* Add the basic sets in "set" to "list".
4096 static isl_stat add_list(__isl_take isl_set *set, void *user)
4098 isl_basic_set_list **list = user;
4099 isl_basic_set_list *list_i;
4101 list_i = isl_set_get_basic_set_list(set);
4102 *list = isl_basic_set_list_concat(*list, list_i);
4103 isl_set_free(set);
4105 if (!*list)
4106 return isl_stat_error;
4107 return isl_stat_ok;
4110 /* Return a list containing all the basic sets in "uset".
4112 * First construct a list of the appropriate size and
4113 * then add all the elements.
4115 __isl_give isl_basic_set_list *isl_union_set_get_basic_set_list(
4116 __isl_keep isl_union_set *uset)
4118 int n;
4119 isl_ctx *ctx;
4120 isl_basic_set_list *list;
4122 if (!uset)
4123 return NULL;
4124 ctx = isl_union_set_get_ctx(uset);
4125 n = isl_union_set_n_basic_set(uset);
4126 if (n < 0)
4127 return NULL;
4128 list = isl_basic_set_list_alloc(ctx, n);
4129 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
4130 list = isl_basic_set_list_free(list);
4132 return list;
4135 /* Internal data structure for isl_union_map_remove_map_if.
4136 * "fn" and "user" are the arguments to isl_union_map_remove_map_if.
4138 struct isl_union_map_remove_map_if_data {
4139 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
4140 void *user;
4143 /* isl_un_op_control filter that negates the result of data->fn
4144 * called on "map".
4146 static isl_bool not(__isl_keep isl_map *map, void *user)
4148 struct isl_union_map_remove_map_if_data *data = user;
4150 return isl_bool_not(data->fn(map, data->user));
4153 /* Dummy isl_un_op_control transformation callback that
4154 * simply returns the input.
4156 static __isl_give isl_map *map_id(__isl_take isl_map *map)
4158 return map;
4161 /* Call "fn" on every map in "umap" and remove those maps
4162 * for which the callback returns true.
4164 * Use un_op to keep only those maps that are not filtered out,
4165 * applying an identity transformation on them.
4167 __isl_give isl_union_map *isl_union_map_remove_map_if(
4168 __isl_take isl_union_map *umap,
4169 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
4171 struct isl_union_map_remove_map_if_data data = { fn, user };
4172 struct isl_un_op_control control = {
4173 .filter = &not,
4174 .filter_user = &data,
4175 .fn_map = &map_id,
4177 return un_op(umap, &control);