Bug Summary

File:tools/polly/lib/External/isl/isl_union_map.c
Warning:line 874, column 2
Use of memory after it is freed

Annotated Source Code

1/*
2 * Copyright 2010-2011 INRIA Saclay
3 * Copyright 2013-2014 Ecole Normale Superieure
4 * Copyright 2014 INRIA Rocquencourt
5 * Copyright 2016 Sven Verdoolaege
6 *
7 * Use of this software is governed by the MIT license
8 *
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
14 */
15
16#define ISL_DIM_H
17#include <isl_map_private.h>
18#include <isl_union_map_private.h>
19#include <isl/ctx.h>
20#include <isl/hash.h>
21#include <isl/aff.h>
22#include <isl/map.h>
23#include <isl/set.h>
24#include <isl_space_private.h>
25#include <isl/union_set.h>
26#include <isl/deprecated/union_map_int.h>
27
28#include <bset_from_bmap.c>
29#include <set_to_map.c>
30#include <set_from_map.c>
31
32/* Return the number of parameters of "umap", where "type"
33 * is required to be set to isl_dim_param.
34 */
35unsigned isl_union_map_dim(__isl_keep isl_union_map *umap,
36 enum isl_dim_type type)
37{
38 if (!umap)
39 return 0;
40
41 if (type != isl_dim_param)
42 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only reference parameters", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_union_map.c"
, 43); return 0; } while (0)
43 "can only reference parameters", return 0)do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only reference parameters", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_union_map.c"
, 43); return 0; } while (0)
;
44
45 return isl_space_dim(umap->dim, type);
46}
47
48/* Return the number of parameters of "uset", where "type"
49 * is required to be set to isl_dim_param.
50 */
51unsigned isl_union_set_dim(__isl_keep isl_union_setisl_union_map *uset,
52 enum isl_dim_type type)
53{
54 return isl_union_map_dim(uset, type);
55}
56
57/* Return the id of the specified dimension.
58 */
59__isl_give isl_id *isl_union_map_get_dim_id(__isl_keep isl_union_map *umap,
60 enum isl_dim_type type, unsigned pos)
61{
62 if (!umap)
63 return NULL((void*)0);
64
65 if (type != isl_dim_param)
66 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only reference parameters", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_union_map.c"
, 67); return ((void*)0); } while (0)
67 "can only reference parameters", return NULL)do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only reference parameters", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_union_map.c"
, 67); return ((void*)0); } while (0)
;
68
69 return isl_space_get_dim_id(umap->dim, type, pos);
70}
71
72/* Is this union set a parameter domain?
73 */
74isl_bool isl_union_set_is_params(__isl_keep isl_union_setisl_union_map *uset)
75{
76 isl_setisl_map *set;
77 isl_bool params;
78
79 if (!uset)
2
Assuming 'uset' is non-null
3
Taking false branch
80 return isl_bool_error;
81 if (uset->table.n != 1)
4
Assuming the condition is false
5
Taking false branch
82 return isl_bool_false;
83
84 set = isl_set_from_union_set(isl_union_set_copy(uset));
6
Calling 'isl_set_from_union_set'
17
Returning; memory was released via 1st parameter
85 params = isl_set_is_params(set);
86 isl_set_free(set);
87 return params;
88}
89
90static __isl_give isl_union_map *isl_union_map_alloc(
91 __isl_take isl_space *space, int size)
92{
93 isl_union_map *umap;
94
95 space = isl_space_params(space);
96 if (!space)
97 return NULL((void*)0);
98
99 umap = isl_calloc_type(space->ctx, isl_union_map)((isl_union_map *)isl_calloc_or_die(space->ctx, 1, sizeof(
isl_union_map)))
;
100 if (!umap) {
101 isl_space_free(space);
102 return NULL((void*)0);
103 }
104
105 umap->ref = 1;
106 umap->dim = space;
107 if (isl_hash_table_init(space->ctx, &umap->table, size) < 0)
108 return isl_union_map_free(umap);
109
110 return umap;
111}
112
113__isl_give isl_union_map *isl_union_map_empty(__isl_take isl_space *dim)
114{
115 return isl_union_map_alloc(dim, 16);
116}
117
118__isl_give isl_union_setisl_union_map *isl_union_set_empty(__isl_take isl_space *dim)
119{
120 return isl_union_map_empty(dim);
121}
122
123isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
124{
125 return umap ? umap->dim->ctx : NULL((void*)0);
126}
127
128isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_setisl_union_map *uset)
129{
130 return uset ? uset->dim->ctx : NULL((void*)0);
131}
132
133/* Return the space of "umap".
134 */
135__isl_keep isl_space *isl_union_map_peek_space(__isl_keep isl_union_map *umap)
136{
137 return umap ? umap->dim : NULL((void*)0);
138}
139
140__isl_give isl_space *isl_union_map_get_space(__isl_keep isl_union_map *umap)
141{
142 return isl_space_copy(isl_union_map_peek_space(umap));
143}
144
145/* Return the position of the parameter with the given name
146 * in "umap".
147 * Return -1 if no such dimension can be found.
148 */
149int isl_union_map_find_dim_by_name(__isl_keep isl_union_map *umap,
150 enum isl_dim_type type, const char *name)
151{
152 if (!umap)
153 return -1;
154 return isl_space_find_dim_by_name(umap->dim, type, name);
155}
156
157__isl_give isl_space *isl_union_set_get_space(__isl_keep isl_union_setisl_union_map *uset)
158{
159 return isl_union_map_get_space(uset);
160}
161
162static isl_stat free_umap_entry(void **entry, void *user)
163{
164 isl_map *map = *entry;
165 isl_map_free(map);
166 return isl_stat_ok;
167}
168
169static isl_stat add_map(__isl_take isl_map *map, void *user)
170{
171 isl_union_map **umap = (isl_union_map **)user;
172
173 *umap = isl_union_map_add_map(*umap, map);
174
175 return isl_stat_ok;
176}
177
178__isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
179{
180 isl_union_map *dup;
181
182 if (!umap)
183 return NULL((void*)0);
184
185 dup = isl_union_map_empty(isl_space_copy(umap->dim));
186 if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
187 goto error;
188 return dup;
189error:
190 isl_union_map_free(dup);
191 return NULL((void*)0);
192}
193
194__isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
195{
196 if (!umap)
197 return NULL((void*)0);
198
199 if (umap->ref == 1)
200 return umap;
201 umap->ref--;
202 return isl_union_map_dup(umap);
203}
204
205struct isl_union_align {
206 isl_reordering *exp;
207 isl_union_map *res;
208};
209
210static isl_stat align_entry(void **entry, void *user)
211{
212 isl_map *map = *entry;
213 isl_reordering *exp;
214 struct isl_union_align *data = user;
215
216 exp = isl_reordering_extend_space(isl_reordering_copy(data->exp),
217 isl_map_get_space(map));
218
219 data->res = isl_union_map_add_map(data->res,
220 isl_map_realign(isl_map_copy(map), exp));
221
222 return isl_stat_ok;
223}
224
225/* Align the parameters of umap along those of model.
226 * The result has the parameters of model first, in the same order
227 * as they appear in model, followed by any remaining parameters of
228 * umap that do not appear in model.
229 */
230__isl_give isl_union_map *isl_union_map_align_params(
231 __isl_take isl_union_map *umap, __isl_take isl_space *model)
232{
233 struct isl_union_align data = { NULL((void*)0), NULL((void*)0) };
234 isl_bool equal_params;
235
236 if (!umap || !model)
237 goto error;
238
239 equal_params = isl_space_has_equal_params(umap->dim, model);
240 if (equal_params < 0)
241 goto error;
242 if (equal_params) {
243 isl_space_free(model);
244 return umap;
245 }
246
247 model = isl_space_params(model);
248 data.exp = isl_parameter_alignment_reordering(umap->dim, model);
249 if (!data.exp)
250 goto error;
251
252 data.res = isl_union_map_alloc(isl_space_copy(data.exp->dim),
253 umap->table.n);
254 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
255 &align_entry, &data) < 0)
256 goto error;
257
258 isl_reordering_free(data.exp);
259 isl_union_map_free(umap);
260 isl_space_free(model);
261 return data.res;
262error:
263 isl_reordering_free(data.exp);
264 isl_union_map_free(umap);
265 isl_union_map_free(data.res);
266 isl_space_free(model);
267 return NULL((void*)0);
268}
269
270__isl_give isl_union_setisl_union_map *isl_union_set_align_params(
271 __isl_take isl_union_setisl_union_map *uset, __isl_take isl_space *model)
272{
273 return isl_union_map_align_params(uset, model);
274}
275
276__isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
277 __isl_take isl_union_map *umap2)
278{
279 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
280 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
281
282 umap1 = isl_union_map_cow(umap1);
283
284 if (!umap1 || !umap2)
285 goto error;
286
287 if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
288 goto error;
289
290 isl_union_map_free(umap2);
291
292 return umap1;
293error:
294 isl_union_map_free(umap1);
295 isl_union_map_free(umap2);
296 return NULL((void*)0);
297}
298
299__isl_give isl_union_setisl_union_map *isl_union_set_union(__isl_take isl_union_setisl_union_map *uset1,
300 __isl_take isl_union_setisl_union_map *uset2)
301{
302 return isl_union_map_union(uset1, uset2);
303}
304
305__isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
306{
307 if (!umap)
308 return NULL((void*)0);
309
310 umap->ref++;
311 return umap;
312}
313
314__isl_give isl_union_setisl_union_map *isl_union_set_copy(__isl_keep isl_union_setisl_union_map *uset)
315{
316 return isl_union_map_copy(uset);
317}
318
319__isl_null isl_union_map *isl_union_map_free(__isl_take isl_union_map *umap)
320{
321 if (!umap)
11
Taking false branch
322 return NULL((void*)0);
323
324 if (--umap->ref > 0)
12
Assuming the condition is false
13
Taking false branch
325 return NULL((void*)0);
326
327 isl_hash_table_foreach(umap->dim->ctx, &umap->table,
328 &free_umap_entry, NULL((void*)0));
329 isl_hash_table_clear(&umap->table);
330 isl_space_free(umap->dim);
331 free(umap);
14
Memory is released
332 return NULL((void*)0);
333}
334
335__isl_null isl_union_setisl_union_map *isl_union_set_free(__isl_take isl_union_setisl_union_map *uset)
336{
337 return isl_union_map_free(uset);
338}
339
340/* Do "umap" and "space" have the same parameters?
341 */
342isl_bool isl_union_map_space_has_equal_params(__isl_keep isl_union_map *umap,
343 __isl_keep isl_space *space)
344{
345 isl_space *umap_space;
346
347 umap_space = isl_union_map_peek_space(umap);
348 return isl_space_has_equal_params(umap_space, space);
349}
350
351static int has_dim(const void *entry, const void *val)
352{
353 isl_map *map = (isl_map *)entry;
354 isl_space *dim = (isl_space *)val;
355
356 return isl_space_is_equal(map->dim, dim);
357}
358
359__isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
360 __isl_take isl_map *map)
361{
362 uint32_t hash;
363 struct isl_hash_table_entry *entry;
364 isl_bool aligned;
365
366 if (!map || !umap)
367 goto error;
368
369 if (isl_map_plain_is_empty(map)) {
370 isl_map_free(map);
371 return umap;
372 }
373
374 aligned = isl_map_space_has_equal_params(map, umap->dim);
375 if (aligned < 0)
376 goto error;
377 if (!aligned) {
378 umap = isl_union_map_align_params(umap, isl_map_get_space(map));
379 map = isl_map_align_params(map, isl_union_map_get_space(umap));
380 }
381
382 umap = isl_union_map_cow(umap);
383
384 if (!map || !umap)
385 goto error;
386
387 hash = isl_space_get_hash(map->dim);
388 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
389 &has_dim, map->dim, 1);
390 if (!entry)
391 goto error;
392
393 if (!entry->data)
394 entry->data = map;
395 else {
396 entry->data = isl_map_union(entry->data, isl_map_copy(map));
397 if (!entry->data)
398 goto error;
399 isl_map_free(map);
400 }
401
402 return umap;
403error:
404 isl_map_free(map);
405 isl_union_map_free(umap);
406 return NULL((void*)0);
407}
408
409__isl_give isl_union_setisl_union_map *isl_union_set_add_set(__isl_take isl_union_setisl_union_map *uset,
410 __isl_take isl_setisl_map *set)
411{
412 return isl_union_map_add_map(uset, set_to_map(set));
413}
414
415__isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
416{
417 isl_space *dim;
418 isl_union_map *umap;
419
420 if (!map)
421 return NULL((void*)0);
422
423 dim = isl_map_get_space(map);
424 dim = isl_space_params(dim);
425 umap = isl_union_map_empty(dim);
426 umap = isl_union_map_add_map(umap, map);
427
428 return umap;
429}
430
431__isl_give isl_union_setisl_union_map *isl_union_set_from_set(__isl_take isl_setisl_map *set)
432{
433 return isl_union_map_from_map(set_to_map(set));
434}
435
436__isl_give isl_union_map *isl_union_map_from_basic_map(
437 __isl_take isl_basic_map *bmap)
438{
439 return isl_union_map_from_map(isl_map_from_basic_map(bmap));
440}
441
442__isl_give isl_union_setisl_union_map *isl_union_set_from_basic_set(
443 __isl_take isl_basic_setisl_basic_map *bset)
444{
445 return isl_union_map_from_basic_map(bset);
446}
447
448struct isl_union_map_foreach_data
449{
450 isl_stat (*fn)(__isl_take isl_map *map, void *user);
451 void *user;
452};
453
454static isl_stat call_on_copy(void **entry, void *user)
455{
456 isl_map *map = *entry;
457 struct isl_union_map_foreach_data *data;
458 data = (struct isl_union_map_foreach_data *)user;
459
460 return data->fn(isl_map_copy(map), data->user);
461}
462
463int isl_union_map_n_map(__isl_keep isl_union_map *umap)
464{
465 return umap ? umap->table.n : 0;
466}
467
468int isl_union_set_n_set(__isl_keep isl_union_setisl_union_map *uset)
469{
470 return uset ? uset->table.n : 0;
471}
472
473isl_stat isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
474 isl_stat (*fn)(__isl_take isl_map *map, void *user), void *user)
475{
476 struct isl_union_map_foreach_data data = { fn, user };
477
478 if (!umap)
479 return isl_stat_error;
480
481 return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
482 &call_on_copy, &data);
483}
484
485static isl_stat copy_map(void **entry, void *user)
486{
487 isl_map *map = *entry;
488 isl_map **map_p = user;
489
490 *map_p = isl_map_copy(map);
491
492 return isl_stat_error;
493}
494
495__isl_give isl_map *isl_map_from_union_map(__isl_take isl_union_map *umap)
496{
497 isl_ctx *ctx;
498 isl_map *map = NULL((void*)0);
499
500 if (!umap)
8
Taking false branch
501 return NULL((void*)0);
502 ctx = isl_union_map_get_ctx(umap);
503 if (umap->table.n != 1)
9
Taking false branch
504 isl_die(ctx, isl_error_invalid,do { isl_handle_error(ctx, isl_error_invalid, "union map needs to contain elements in exactly "
"one space", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_union_map.c"
, 506); goto error; } while (0)
505 "union map needs to contain elements in exactly "do { isl_handle_error(ctx, isl_error_invalid, "union map needs to contain elements in exactly "
"one space", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_union_map.c"
, 506); goto error; } while (0)
506 "one space", goto error)do { isl_handle_error(ctx, isl_error_invalid, "union map needs to contain elements in exactly "
"one space", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_union_map.c"
, 506); goto error; } while (0)
;
507
508 isl_hash_table_foreach(ctx, &umap->table, &copy_map, &map);
509
510 isl_union_map_free(umap);
10
Calling 'isl_union_map_free'
15
Returning; memory was released via 1st parameter
511
512 return map;
513error:
514 isl_union_map_free(umap);
515 return NULL((void*)0);
516}
517
518__isl_give isl_setisl_map *isl_set_from_union_set(__isl_take isl_union_setisl_union_map *uset)
519{
520 return isl_map_from_union_map(uset);
7
Calling 'isl_map_from_union_map'
16
Returning; memory was released via 1st parameter
521}
522
523/* Extract the map in "umap" that lives in the given space (ignoring
524 * parameters).
525 */
526__isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
527 __isl_take isl_space *space)
528{
529 uint32_t hash;
530 struct isl_hash_table_entry *entry;
531
532 space = isl_space_drop_dims(space, isl_dim_param,
533 0, isl_space_dim(space, isl_dim_param));
534 space = isl_space_align_params(space, isl_union_map_get_space(umap));
535 if (!umap || !space)
536 goto error;
537
538 hash = isl_space_get_hash(space);
539 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
540 &has_dim, space, 0);
541 if (!entry)
542 return isl_map_empty(space);
543 isl_space_free(space);
544 return isl_map_copy(entry->data);
545error:
546 isl_space_free(space);
547 return NULL((void*)0);
548}
549
550__isl_give isl_setisl_map *isl_union_set_extract_set(__isl_keep isl_union_setisl_union_map *uset,
551 __isl_take isl_space *dim)
552{
553 return set_from_map(isl_union_map_extract_map(uset, dim));
554}
555
556/* Check if umap contains a map in the given space.
557 */
558isl_bool isl_union_map_contains(__isl_keep isl_union_map *umap,
559 __isl_keep isl_space *space)
560{
561 uint32_t hash;
562 struct isl_hash_table_entry *entry;
563
564 if (!umap || !space)
565 return isl_bool_error;
566
567 hash = isl_space_get_hash(space);
568 entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
569 &has_dim, space, 0);
570 return !!entry;
571}
572
573isl_bool isl_union_set_contains(__isl_keep isl_union_setisl_union_map *uset,
574 __isl_keep isl_space *space)
575{
576 return isl_union_map_contains(uset, space);
577}
578
579isl_stat isl_union_set_foreach_set(__isl_keep isl_union_setisl_union_map *uset,
580 isl_stat (*fn)(__isl_take isl_setisl_map *set, void *user), void *user)
581{
582 return isl_union_map_foreach_map(uset,
583 (isl_stat(*)(__isl_take isl_map *, void*))fn, user);
584}
585
586struct isl_union_set_foreach_point_data {
587 isl_stat (*fn)(__isl_take isl_point *pnt, void *user);
588 void *user;
589};
590
591static isl_stat foreach_point(__isl_take isl_setisl_map *set, void *user)
592{
593 struct isl_union_set_foreach_point_data *data = user;
594 isl_stat r;
595
596 r = isl_set_foreach_point(set, data->fn, data->user);
597 isl_set_free(set);
598
599 return r;
600}
601
602isl_stat isl_union_set_foreach_point(__isl_keep isl_union_setisl_union_map *uset,
603 isl_stat (*fn)(__isl_take isl_point *pnt, void *user), void *user)
604{
605 struct isl_union_set_foreach_point_data data = { fn, user };
606 return isl_union_set_foreach_set(uset, &foreach_point, &data);
607}
608
609struct isl_union_map_gen_bin_data {
610 isl_union_map *umap2;
611 isl_union_map *res;
612};
613
614static isl_stat subtract_entry(void **entry, void *user)
615{
616 struct isl_union_map_gen_bin_data *data = user;
617 uint32_t hash;
618 struct isl_hash_table_entry *entry2;
619 isl_map *map = *entry;
620
621 hash = isl_space_get_hash(map->dim);
622 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
623 hash, &has_dim, map->dim, 0);
624 map = isl_map_copy(map);
625 if (entry2) {
626 int empty;
627 map = isl_map_subtract(map, isl_map_copy(entry2->data));
628
629 empty = isl_map_is_empty(map);
630 if (empty < 0) {
631 isl_map_free(map);
632 return isl_stat_error;
633 }
634 if (empty) {
635 isl_map_free(map);
636 return isl_stat_ok;
637 }
638 }
639 data->res = isl_union_map_add_map(data->res, map);
640
641 return isl_stat_ok;
642}
643
644static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
645 __isl_take isl_union_map *umap2, isl_stat (*fn)(void **, void *))
646{
647 struct isl_union_map_gen_bin_data data = { NULL((void*)0), NULL((void*)0) };
648
649 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
650 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
651
652 if (!umap1 || !umap2)
653 goto error;
654
655 data.umap2 = umap2;
656 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
657 umap1->table.n);
658 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
659 fn, &data) < 0)
660 goto error;
661
662 isl_union_map_free(umap1);
663 isl_union_map_free(umap2);
664 return data.res;
665error:
666 isl_union_map_free(umap1);
667 isl_union_map_free(umap2);
668 isl_union_map_free(data.res);
669 return NULL((void*)0);
670}
671
672__isl_give isl_union_map *isl_union_map_subtract(
673 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
674{
675 return gen_bin_op(umap1, umap2, &subtract_entry);
676}
677
678__isl_give isl_union_setisl_union_map *isl_union_set_subtract(
679 __isl_take isl_union_setisl_union_map *uset1, __isl_take isl_union_setisl_union_map *uset2)
680{
681 return isl_union_map_subtract(uset1, uset2);
682}
683
684struct isl_union_map_gen_bin_set_data {
685 isl_setisl_map *set;
686 isl_union_map *res;
687};
688
689static isl_stat intersect_params_entry(void **entry, void *user)
690{
691 struct isl_union_map_gen_bin_set_data *data = user;
692 isl_map *map = *entry;
693 int empty;
694
695 map = isl_map_copy(map);
696 map = isl_map_intersect_params(map, isl_set_copy(data->set));
697
698 empty = isl_map_is_empty(map);
699 if (empty < 0) {
700 isl_map_free(map);
701 return isl_stat_error;
702 }
703
704 data->res = isl_union_map_add_map(data->res, map);
705
706 return isl_stat_ok;
707}
708
709static __isl_give isl_union_map *gen_bin_set_op(__isl_take isl_union_map *umap,
710 __isl_take isl_setisl_map *set, isl_stat (*fn)(void **, void *))
711{
712 struct isl_union_map_gen_bin_set_data data = { NULL((void*)0), NULL((void*)0) };
713
714 umap = isl_union_map_align_params(umap, isl_set_get_space(set));
715 set = isl_set_align_params(set, isl_union_map_get_space(umap));
716
717 if (!umap || !set)
718 goto error;
719
720 data.set = set;
721 data.res = isl_union_map_alloc(isl_space_copy(umap->dim),
722 umap->table.n);
723 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
724 fn, &data) < 0)
725 goto error;
726
727 isl_union_map_free(umap);
728 isl_set_free(set);
729 return data.res;
730error:
731 isl_union_map_free(umap);
732 isl_set_free(set);
733 isl_union_map_free(data.res);
734 return NULL((void*)0);
735}
736
737/* Intersect "umap" with the parameter domain "set".
738 *
739 * If "set" does not have any constraints, then we can return immediately.
740 */
741__isl_give isl_union_map *isl_union_map_intersect_params(
742 __isl_take isl_union_map *umap, __isl_take isl_setisl_map *set)
743{
744 int is_universe;
745
746 is_universe = isl_set_plain_is_universe(set);
747 if (is_universe < 0)
748 goto error;
749 if (is_universe) {
750 isl_set_free(set);
751 return umap;
752 }
753
754 return gen_bin_set_op(umap, set, &intersect_params_entry);
755error:
756 isl_union_map_free(umap);
757 isl_set_free(set);
758 return NULL((void*)0);
759}
760
761__isl_give isl_union_setisl_union_map *isl_union_set_intersect_params(
762 __isl_take isl_union_setisl_union_map *uset, __isl_take isl_setisl_map *set)
763{
764 return isl_union_map_intersect_params(uset, set);
765}
766
767static __isl_give isl_union_map *union_map_intersect_params(
768 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *uset)
769{
770 return isl_union_map_intersect_params(umap,
771 isl_set_from_union_set(uset));
772}
773
774static __isl_give isl_union_map *union_map_gist_params(
775 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *uset)
776{
777 return isl_union_map_gist_params(umap, isl_set_from_union_set(uset));
778}
779
780struct isl_union_map_match_bin_data {
781 isl_union_map *umap2;
782 isl_union_map *res;
783 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
784};
785
786static isl_stat match_bin_entry(void **entry, void *user)
787{
788 struct isl_union_map_match_bin_data *data = user;
789 uint32_t hash;
790 struct isl_hash_table_entry *entry2;
791 isl_map *map = *entry;
792 int empty;
793
794 hash = isl_space_get_hash(map->dim);
795 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
796 hash, &has_dim, map->dim, 0);
797 if (!entry2)
798 return isl_stat_ok;
799
800 map = isl_map_copy(map);
801 map = data->fn(map, isl_map_copy(entry2->data));
802
803 empty = isl_map_is_empty(map);
804 if (empty < 0) {
805 isl_map_free(map);
806 return isl_stat_error;
807 }
808 if (empty) {
809 isl_map_free(map);
810 return isl_stat_ok;
811 }
812
813 data->res = isl_union_map_add_map(data->res, map);
814
815 return isl_stat_ok;
816}
817
818static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
819 __isl_take isl_union_map *umap2,
820 __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
821{
822 struct isl_union_map_match_bin_data data = { NULL((void*)0), NULL((void*)0), fn };
823
824 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
825 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
826
827 if (!umap1 || !umap2)
828 goto error;
829
830 data.umap2 = umap2;
831 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
832 umap1->table.n);
833 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
834 &match_bin_entry, &data) < 0)
835 goto error;
836
837 isl_union_map_free(umap1);
838 isl_union_map_free(umap2);
839 return data.res;
840error:
841 isl_union_map_free(umap1);
842 isl_union_map_free(umap2);
843 isl_union_map_free(data.res);
844 return NULL((void*)0);
845}
846
847__isl_give isl_union_map *isl_union_map_intersect(
848 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
849{
850 return match_bin_op(umap1, umap2, &isl_map_intersect);
851}
852
853/* Compute the intersection of the two union_sets.
854 * As a special case, if exactly one of the two union_sets
855 * is a parameter domain, then intersect the parameter domain
856 * of the other one with this set.
857 */
858__isl_give isl_union_setisl_union_map *isl_union_set_intersect(
859 __isl_take isl_union_setisl_union_map *uset1, __isl_take isl_union_setisl_union_map *uset2)
860{
861 int p1, p2;
862
863 p1 = isl_union_set_is_params(uset1);
864 p2 = isl_union_set_is_params(uset2);
1
Calling 'isl_union_set_is_params'
18
Returning; memory was released via 1st parameter
865 if (p1 < 0 || p2 < 0)
866 goto error;
19
Control jumps to line 873
867 if (!p1 && p2)
868 return union_map_intersect_params(uset1, uset2);
869 if (p1 && !p2)
870 return union_map_intersect_params(uset2, uset1);
871 return isl_union_map_intersect(uset1, uset2);
872error:
873 isl_union_set_free(uset1);
874 isl_union_set_free(uset2);
20
Use of memory after it is freed
875 return NULL((void*)0);
876}
877
878static isl_stat gist_params_entry(void **entry, void *user)
879{
880 struct isl_union_map_gen_bin_set_data *data = user;
881 isl_map *map = *entry;
882 int empty;
883
884 map = isl_map_copy(map);
885 map = isl_map_gist_params(map, isl_set_copy(data->set));
886
887 empty = isl_map_is_empty(map);
888 if (empty < 0) {
889 isl_map_free(map);
890 return isl_stat_error;
891 }
892
893 data->res = isl_union_map_add_map(data->res, map);
894
895 return isl_stat_ok;
896}
897
898__isl_give isl_union_map *isl_union_map_gist_params(
899 __isl_take isl_union_map *umap, __isl_take isl_setisl_map *set)
900{
901 return gen_bin_set_op(umap, set, &gist_params_entry);
902}
903
904__isl_give isl_union_setisl_union_map *isl_union_set_gist_params(
905 __isl_take isl_union_setisl_union_map *uset, __isl_take isl_setisl_map *set)
906{
907 return isl_union_map_gist_params(uset, set);
908}
909
910__isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
911 __isl_take isl_union_map *context)
912{
913 return match_bin_op(umap, context, &isl_map_gist);
914}
915
916__isl_give isl_union_setisl_union_map *isl_union_set_gist(__isl_take isl_union_setisl_union_map *uset,
917 __isl_take isl_union_setisl_union_map *context)
918{
919 if (isl_union_set_is_params(context))
920 return union_map_gist_params(uset, context);
921 return isl_union_map_gist(uset, context);
922}
923
924static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
925 __isl_take isl_map *set2)
926{
927 return isl_set_lex_le_set(set_from_map(set1), set_from_map(set2));
928}
929
930static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
931 __isl_take isl_map *set2)
932{
933 return isl_set_lex_lt_set(set_from_map(set1), set_from_map(set2));
934}
935
936__isl_give isl_union_map *isl_union_set_lex_lt_union_set(
937 __isl_take isl_union_setisl_union_map *uset1, __isl_take isl_union_setisl_union_map *uset2)
938{
939 return match_bin_op(uset1, uset2, &lex_lt_set);
940}
941
942__isl_give isl_union_map *isl_union_set_lex_le_union_set(
943 __isl_take isl_union_setisl_union_map *uset1, __isl_take isl_union_setisl_union_map *uset2)
944{
945 return match_bin_op(uset1, uset2, &lex_le_set);
946}
947
948__isl_give isl_union_map *isl_union_set_lex_gt_union_set(
949 __isl_take isl_union_setisl_union_map *uset1, __isl_take isl_union_setisl_union_map *uset2)
950{
951 return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
952}
953
954__isl_give isl_union_map *isl_union_set_lex_ge_union_set(
955 __isl_take isl_union_setisl_union_map *uset1, __isl_take isl_union_setisl_union_map *uset2)
956{
957 return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
958}
959
960__isl_give isl_union_map *isl_union_map_lex_gt_union_map(
961 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
962{
963 return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
964}
965
966__isl_give isl_union_map *isl_union_map_lex_ge_union_map(
967 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
968{
969 return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
970}
971
972static isl_stat intersect_domain_entry(void **entry, void *user)
973{
974 struct isl_union_map_gen_bin_data *data = user;
975 uint32_t hash;
976 struct isl_hash_table_entry *entry2;
977 isl_space *dim;
978 isl_map *map = *entry;
979 isl_bool empty;
980
981 dim = isl_map_get_space(map);
982 dim = isl_space_domain(dim);
983 hash = isl_space_get_hash(dim);
984 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
985 hash, &has_dim, dim, 0);
986 isl_space_free(dim);
987 if (!entry2)
988 return isl_stat_ok;
989
990 map = isl_map_copy(map);
991 map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
992
993 empty = isl_map_is_empty(map);
994 if (empty < 0) {
995 isl_map_free(map);
996 return isl_stat_error;
997 }
998 if (empty) {
999 isl_map_free(map);
1000 return isl_stat_ok;
1001 }
1002
1003 data->res = isl_union_map_add_map(data->res, map);
1004
1005 return isl_stat_ok;
1006}
1007
1008/* Intersect the domain of "umap" with "uset".
1009 * If "uset" is a parameters domain, then intersect the parameter
1010 * domain of "umap" with this set.
1011 */
1012__isl_give isl_union_map *isl_union_map_intersect_domain(
1013 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *uset)
1014{
1015 if (isl_union_set_is_params(uset))
1016 return union_map_intersect_params(umap, uset);
1017 return gen_bin_op(umap, uset, &intersect_domain_entry);
1018}
1019
1020/* Remove the elements of data->umap2 from the domain of *entry
1021 * and add the result to data->res.
1022 */
1023static isl_stat subtract_domain_entry(void **entry, void *user)
1024{
1025 struct isl_union_map_gen_bin_data *data = user;
1026 uint32_t hash;
1027 struct isl_hash_table_entry *entry2;
1028 isl_space *dim;
1029 isl_map *map = *entry;
1030 isl_bool empty;
1031
1032 dim = isl_map_get_space(map);
1033 dim = isl_space_domain(dim);
1034 hash = isl_space_get_hash(dim);
1035 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1036 hash, &has_dim, dim, 0);
1037 isl_space_free(dim);
1038
1039 map = isl_map_copy(map);
1040
1041 if (!entry2) {
1042 data->res = isl_union_map_add_map(data->res, map);
1043 return isl_stat_ok;
1044 }
1045
1046 map = isl_map_subtract_domain(map, isl_set_copy(entry2->data));
1047
1048 empty = isl_map_is_empty(map);
1049 if (empty < 0) {
1050 isl_map_free(map);
1051 return isl_stat_error;
1052 }
1053 if (empty) {
1054 isl_map_free(map);
1055 return isl_stat_ok;
1056 }
1057
1058 data->res = isl_union_map_add_map(data->res, map);
1059
1060 return isl_stat_ok;
1061}
1062
1063/* Remove the elements of "uset" from the domain of "umap".
1064 */
1065__isl_give isl_union_map *isl_union_map_subtract_domain(
1066 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *dom)
1067{
1068 return gen_bin_op(umap, dom, &subtract_domain_entry);
1069}
1070
1071/* Remove the elements of data->umap2 from the range of *entry
1072 * and add the result to data->res.
1073 */
1074static isl_stat subtract_range_entry(void **entry, void *user)
1075{
1076 struct isl_union_map_gen_bin_data *data = user;
1077 uint32_t hash;
1078 struct isl_hash_table_entry *entry2;
1079 isl_space *space;
1080 isl_map *map = *entry;
1081 isl_bool empty;
1082
1083 space = isl_map_get_space(map);
1084 space = isl_space_range(space);
1085 hash = isl_space_get_hash(space);
1086 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1087 hash, &has_dim, space, 0);
1088 isl_space_free(space);
1089
1090 map = isl_map_copy(map);
1091
1092 if (!entry2) {
1093 data->res = isl_union_map_add_map(data->res, map);
1094 return isl_stat_ok;
1095 }
1096
1097 map = isl_map_subtract_range(map, isl_set_copy(entry2->data));
1098
1099 empty = isl_map_is_empty(map);
1100 if (empty < 0) {
1101 isl_map_free(map);
1102 return isl_stat_error;
1103 }
1104 if (empty) {
1105 isl_map_free(map);
1106 return isl_stat_ok;
1107 }
1108
1109 data->res = isl_union_map_add_map(data->res, map);
1110
1111 return isl_stat_ok;
1112}
1113
1114/* Remove the elements of "uset" from the range of "umap".
1115 */
1116__isl_give isl_union_map *isl_union_map_subtract_range(
1117 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *dom)
1118{
1119 return gen_bin_op(umap, dom, &subtract_range_entry);
1120}
1121
1122static isl_stat gist_domain_entry(void **entry, void *user)
1123{
1124 struct isl_union_map_gen_bin_data *data = user;
1125 uint32_t hash;
1126 struct isl_hash_table_entry *entry2;
1127 isl_space *dim;
1128 isl_map *map = *entry;
1129 isl_bool empty;
1130
1131 dim = isl_map_get_space(map);
1132 dim = isl_space_domain(dim);
1133 hash = isl_space_get_hash(dim);
1134 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1135 hash, &has_dim, dim, 0);
1136 isl_space_free(dim);
1137 if (!entry2)
1138 return isl_stat_ok;
1139
1140 map = isl_map_copy(map);
1141 map = isl_map_gist_domain(map, isl_set_copy(entry2->data));
1142
1143 empty = isl_map_is_empty(map);
1144 if (empty < 0) {
1145 isl_map_free(map);
1146 return isl_stat_error;
1147 }
1148
1149 data->res = isl_union_map_add_map(data->res, map);
1150
1151 return isl_stat_ok;
1152}
1153
1154/* Compute the gist of "umap" with respect to the domain "uset".
1155 * If "uset" is a parameters domain, then compute the gist
1156 * with respect to this parameter domain.
1157 */
1158__isl_give isl_union_map *isl_union_map_gist_domain(
1159 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *uset)
1160{
1161 if (isl_union_set_is_params(uset))
1162 return union_map_gist_params(umap, uset);
1163 return gen_bin_op(umap, uset, &gist_domain_entry);
1164}
1165
1166static isl_stat gist_range_entry(void **entry, void *user)
1167{
1168 struct isl_union_map_gen_bin_data *data = user;
1169 uint32_t hash;
1170 struct isl_hash_table_entry *entry2;
1171 isl_space *space;
1172 isl_map *map = *entry;
1173 isl_bool empty;
1174
1175 space = isl_map_get_space(map);
1176 space = isl_space_range(space);
1177 hash = isl_space_get_hash(space);
1178 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1179 hash, &has_dim, space, 0);
1180 isl_space_free(space);
1181 if (!entry2)
1182 return isl_stat_ok;
1183
1184 map = isl_map_copy(map);
1185 map = isl_map_gist_range(map, isl_set_copy(entry2->data));
1186
1187 empty = isl_map_is_empty(map);
1188 if (empty < 0) {
1189 isl_map_free(map);
1190 return isl_stat_error;
1191 }
1192
1193 data->res = isl_union_map_add_map(data->res, map);
1194
1195 return isl_stat_ok;
1196}
1197
1198/* Compute the gist of "umap" with respect to the range "uset".
1199 */
1200__isl_give isl_union_map *isl_union_map_gist_range(
1201 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *uset)
1202{
1203 return gen_bin_op(umap, uset, &gist_range_entry);
1204}
1205
1206static isl_stat intersect_range_entry(void **entry, void *user)
1207{
1208 struct isl_union_map_gen_bin_data *data = user;
1209 uint32_t hash;
1210 struct isl_hash_table_entry *entry2;
1211 isl_space *dim;
1212 isl_map *map = *entry;
1213 isl_bool empty;
1214
1215 dim = isl_map_get_space(map);
1216 dim = isl_space_range(dim);
1217 hash = isl_space_get_hash(dim);
1218 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1219 hash, &has_dim, dim, 0);
1220 isl_space_free(dim);
1221 if (!entry2)
1222 return isl_stat_ok;
1223
1224 map = isl_map_copy(map);
1225 map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
1226
1227 empty = isl_map_is_empty(map);
1228 if (empty < 0) {
1229 isl_map_free(map);
1230 return isl_stat_error;
1231 }
1232 if (empty) {
1233 isl_map_free(map);
1234 return isl_stat_ok;
1235 }
1236
1237 data->res = isl_union_map_add_map(data->res, map);
1238
1239 return isl_stat_ok;
1240}
1241
1242__isl_give isl_union_map *isl_union_map_intersect_range(
1243 __isl_take isl_union_map *umap, __isl_take isl_union_setisl_union_map *uset)
1244{
1245 return gen_bin_op(umap, uset, &intersect_range_entry);
1246}
1247
1248struct isl_union_map_bin_data {
1249 isl_union_map *umap2;
1250 isl_union_map *res;
1251 isl_map *map;
1252 isl_stat (*fn)(void **entry, void *user);
1253};
1254
1255static isl_stat apply_range_entry(void **entry, void *user)
1256{
1257 struct isl_union_map_bin_data *data = user;
1258 isl_map *map2 = *entry;
1259 isl_bool empty;
1260
1261 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1262 map2->dim, isl_dim_in))
1263 return isl_stat_ok;
1264
1265 map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
1266
1267 empty = isl_map_is_empty(map2);
1268 if (empty < 0) {
1269 isl_map_free(map2);
1270 return isl_stat_error;
1271 }
1272 if (empty) {
1273 isl_map_free(map2);
1274 return isl_stat_ok;
1275 }
1276
1277 data->res = isl_union_map_add_map(data->res, map2);
1278
1279 return isl_stat_ok;
1280}
1281
1282static isl_stat bin_entry(void **entry, void *user)
1283{
1284 struct isl_union_map_bin_data *data = user;
1285 isl_map *map = *entry;
1286
1287 data->map = map;
1288 if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
1289 data->fn, data) < 0)
1290 return isl_stat_error;
1291
1292 return isl_stat_ok;
1293}
1294
1295static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
1296 __isl_take isl_union_map *umap2,
1297 isl_stat (*fn)(void **entry, void *user))
1298{
1299 struct isl_union_map_bin_data data = { NULL((void*)0), NULL((void*)0), NULL((void*)0), fn };
1300
1301 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
1302 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
1303
1304 if (!umap1 || !umap2)
1305 goto error;
1306
1307 data.umap2 = umap2;
1308 data.res = isl_union_map_alloc(isl_space_copy(umap1->dim),
1309 umap1->table.n);
1310 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1311 &bin_entry, &data) < 0)
1312 goto error;
1313
1314 isl_union_map_free(umap1);
1315 isl_union_map_free(umap2);
1316 return data.res;
1317error:
1318 isl_union_map_free(umap1);
1319 isl_union_map_free(umap2);
1320 isl_union_map_free(data.res);
1321 return NULL((void*)0);
1322}
1323
1324__isl_give isl_union_map *isl_union_map_apply_range(
1325 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1326{
1327 return bin_op(umap1, umap2, &apply_range_entry);
1328}
1329
1330__isl_give isl_union_map *isl_union_map_apply_domain(
1331 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1332{
1333 umap1 = isl_union_map_reverse(umap1);
1334 umap1 = isl_union_map_apply_range(umap1, umap2);
1335 return isl_union_map_reverse(umap1);
1336}
1337
1338__isl_give isl_union_setisl_union_map *isl_union_set_apply(
1339 __isl_take isl_union_setisl_union_map *uset, __isl_take isl_union_map *umap)
1340{
1341 return isl_union_map_apply_range(uset, umap);
1342}
1343
1344static isl_stat map_lex_lt_entry(void **entry, void *user)
1345{
1346 struct isl_union_map_bin_data *data = user;
1347 isl_map *map2 = *entry;
1348
1349 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1350 map2->dim, isl_dim_out))
1351 return isl_stat_ok;
1352
1353 map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
1354
1355 data->res = isl_union_map_add_map(data->res, map2);
1356
1357 return isl_stat_ok;
1358}
1359
1360__isl_give isl_union_map *isl_union_map_lex_lt_union_map(
1361 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1362{
1363 return bin_op(umap1, umap2, &map_lex_lt_entry);
1364}
1365
1366static isl_stat map_lex_le_entry(void **entry, void *user)
1367{
1368 struct isl_union_map_bin_data *data = user;
1369 isl_map *map2 = *entry;
1370
1371 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1372 map2->dim, isl_dim_out))
1373 return isl_stat_ok;
1374
1375 map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
1376
1377 data->res = isl_union_map_add_map(data->res, map2);
1378
1379 return isl_stat_ok;
1380}
1381
1382__isl_give isl_union_map *isl_union_map_lex_le_union_map(
1383 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1384{
1385 return bin_op(umap1, umap2, &map_lex_le_entry);
1386}
1387
1388static isl_stat product_entry(void **entry, void *user)
1389{
1390 struct isl_union_map_bin_data *data = user;
1391 isl_map *map2 = *entry;
1392
1393 map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
1394
1395 data->res = isl_union_map_add_map(data->res, map2);
1396
1397 return isl_stat_ok;
1398}
1399
1400__isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
1401 __isl_take isl_union_map *umap2)
1402{
1403 return bin_op(umap1, umap2, &product_entry);
1404}
1405
1406static isl_stat set_product_entry(void **entry, void *user)
1407{
1408 struct isl_union_map_bin_data *data = user;
1409 isl_setisl_map *set2 = *entry;
1410
1411 set2 = isl_set_product(isl_set_copy(data->map), isl_set_copy(set2));
1412
1413 data->res = isl_union_set_add_set(data->res, set2);
1414
1415 return isl_stat_ok;
1416}
1417
1418__isl_give isl_union_setisl_union_map *isl_union_set_product(__isl_take isl_union_setisl_union_map *uset1,
1419 __isl_take isl_union_setisl_union_map *uset2)
1420{
1421 return bin_op(uset1, uset2, &set_product_entry);
1422}
1423
1424static isl_stat domain_product_entry(void **entry, void *user)
1425{
1426 struct isl_union_map_bin_data *data = user;
1427 isl_map *map2 = *entry;
1428
1429 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1430 map2->dim, isl_dim_out))
1431 return isl_stat_ok;
1432
1433 map2 = isl_map_domain_product(isl_map_copy(data->map),
1434 isl_map_copy(map2));
1435
1436 data->res = isl_union_map_add_map(data->res, map2);
1437
1438 return isl_stat_ok;
1439}
1440
1441/* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
1442 */
1443__isl_give isl_union_map *isl_union_map_domain_product(
1444 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1445{
1446 return bin_op(umap1, umap2, &domain_product_entry);
1447}
1448
1449static isl_stat range_product_entry(void **entry, void *user)
1450{
1451 struct isl_union_map_bin_data *data = user;
1452 isl_map *map2 = *entry;
1453
1454 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1455 map2->dim, isl_dim_in))
1456 return isl_stat_ok;
1457
1458 map2 = isl_map_range_product(isl_map_copy(data->map),
1459 isl_map_copy(map2));
1460
1461 data->res = isl_union_map_add_map(data->res, map2);
1462
1463 return isl_stat_ok;
1464}
1465
1466__isl_give isl_union_map *isl_union_map_range_product(
1467 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1468{
1469 return bin_op(umap1, umap2, &range_product_entry);
1470}
1471
1472/* If data->map A -> B and "map2" C -> D have the same range space,
1473 * then add (A, C) -> (B * D) to data->res.
1474 */
1475static isl_stat flat_domain_product_entry(void **entry, void *user)
1476{
1477 struct isl_union_map_bin_data *data = user;
1478 isl_map *map2 = *entry;
1479
1480 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_out,
1481 map2->dim, isl_dim_out))
1482 return isl_stat_ok;
1483
1484 map2 = isl_map_flat_domain_product(isl_map_copy(data->map),
1485 isl_map_copy(map2));
1486
1487 data->res = isl_union_map_add_map(data->res, map2);
1488
1489 return isl_stat_ok;
1490}
1491
1492/* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D).
1493 */
1494__isl_give isl_union_map *isl_union_map_flat_domain_product(
1495 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1496{
1497 return bin_op(umap1, umap2, &flat_domain_product_entry);
1498}
1499
1500static isl_stat flat_range_product_entry(void **entry, void *user)
1501{
1502 struct isl_union_map_bin_data *data = user;
1503 isl_map *map2 = *entry;
1504
1505 if (!isl_space_tuple_is_equal(data->map->dim, isl_dim_in,
1506 map2->dim, isl_dim_in))
1507 return isl_stat_ok;
1508
1509 map2 = isl_map_flat_range_product(isl_map_copy(data->map),
1510 isl_map_copy(map2));
1511
1512 data->res = isl_union_map_add_map(data->res, map2);
1513
1514 return isl_stat_ok;
1515}
1516
1517__isl_give isl_union_map *isl_union_map_flat_range_product(
1518 __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
1519{
1520 return bin_op(umap1, umap2, &flat_range_product_entry);
1521}
1522
1523static __isl_give isl_union_setisl_union_map *cond_un_op(__isl_take isl_union_map *umap,
1524 isl_stat (*fn)(void **, void *))
1525{
1526 isl_union_setisl_union_map *res;
1527
1528 if (!umap)
1529 return NULL((void*)0);
1530
1531 res = isl_union_map_alloc(isl_space_copy(umap->dim), umap->table.n);
1532 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1533 goto error;
1534
1535 isl_union_map_free(umap);
1536 return res;
1537error:
1538 isl_union_map_free(umap);
1539 isl_union_set_free(res);
1540 return NULL((void*)0);
1541}
1542
1543static isl_stat from_range_entry(void **entry, void *user)
1544{
1545 isl_map *set = *entry;
1546 isl_union_setisl_union_map **res = user;
1547
1548 *res = isl_union_map_add_map(*res,
1549 isl_map_from_range(isl_set_copy(set)));
1550
1551 return isl_stat_ok;
1552}
1553
1554__isl_give isl_union_map *isl_union_map_from_range(
1555 __isl_take isl_union_setisl_union_map *uset)
1556{
1557 return cond_un_op(uset, &from_range_entry);
1558}
1559
1560__isl_give isl_union_map *isl_union_map_from_domain(
1561 __isl_take isl_union_setisl_union_map *uset)
1562{
1563 return isl_union_map_reverse(isl_union_map_from_range(uset));
1564}
1565
1566__isl_give isl_union_map *isl_union_map_from_domain_and_range(
1567 __isl_take isl_union_setisl_union_map *domain, __isl_take isl_union_setisl_union_map *range)
1568{
1569 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1570 isl_union_map_from_range(range));
1571}
1572
1573static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1574 isl_stat (*fn)(void **, void *))
1575{
1576 umap = isl_union_map_cow(umap);
1577 if (!umap)
1578 return NULL((void*)0);
1579
1580 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL((void*)0)) < 0)
1581 goto error;
1582
1583 return umap;
1584error:
1585 isl_union_map_free(umap);
1586 return NULL((void*)0);
1587}
1588
1589static isl_stat affine_entry(void **entry, void *user)
1590{
1591 isl_map **map = (isl_map **)entry;
1592
1593 *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
1594
1595 return *map ? isl_stat_ok : isl_stat_error;
1596}
1597
1598__isl_give isl_union_map *isl_union_map_affine_hull(
1599 __isl_take isl_union_map *umap)
1600{
1601 return un_op(umap, &affine_entry);
1602}
1603
1604__isl_give isl_union_setisl_union_map *isl_union_set_affine_hull(
1605 __isl_take isl_union_setisl_union_map *uset)
1606{
1607 return isl_union_map_affine_hull(uset);
1608}
1609
1610static isl_stat polyhedral_entry(void **entry, void *user)
1611{
1612 isl_map **map = (isl_map **)entry;
1613
1614 *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
1615
1616 return *map ? isl_stat_ok : isl_stat_error;
1617}
1618
1619__isl_give isl_union_map *isl_union_map_polyhedral_hull(
1620 __isl_take isl_union_map *umap)
1621{
1622 return un_op(umap, &polyhedral_entry);
1623}
1624
1625__isl_give isl_union_setisl_union_map *isl_union_set_polyhedral_hull(
1626 __isl_take isl_union_setisl_union_map *uset)
1627{
1628 return isl_union_map_polyhedral_hull(uset);
1629}
1630
1631static isl_stat simple_entry(void **entry, void *user)
1632{
1633 isl_map **map = (isl_map **)entry;
1634
1635 *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
1636
1637 return *map ? isl_stat_ok : isl_stat_error;
1638}
1639
1640__isl_give isl_union_map *isl_union_map_simple_hull(
1641 __isl_take isl_union_map *umap)
1642{
1643 return un_op(umap, &simple_entry);
1644}
1645
1646__isl_give isl_union_setisl_union_map *isl_union_set_simple_hull(
1647 __isl_take isl_union_setisl_union_map *uset)
1648{
1649 return isl_union_map_simple_hull(uset);
1650}
1651
1652static isl_stat inplace_entry(void **entry, void *user)
1653{
1654 __isl_give isl_map *(*fn)(__isl_take isl_map *);
1655 isl_map **map = (isl_map **)entry;
1656 isl_map *copy;
1657
1658 fn = *(__isl_give isl_map *(**)(__isl_take isl_map *)) user;
1659 copy = fn(isl_map_copy(*map));
1660 if (!copy)
1661 return isl_stat_error;
1662
1663 isl_map_free(*map);
1664 *map = copy;
1665
1666 return isl_stat_ok;
1667}
1668
1669static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1670 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1671{
1672 if (!umap)
1673 return NULL((void*)0);
1674
1675 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1676 &inplace_entry, &fn) < 0)
1677 goto error;
1678
1679 return umap;
1680error:
1681 isl_union_map_free(umap);
1682 return NULL((void*)0);
1683}
1684
1685/* Remove redundant constraints in each of the basic maps of "umap".
1686 * Since removing redundant constraints does not change the meaning
1687 * or the space, the operation can be performed in-place.
1688 */
1689__isl_give isl_union_map *isl_union_map_remove_redundancies(
1690 __isl_take isl_union_map *umap)
1691{
1692 return inplace(umap, &isl_map_remove_redundancies);
1693}
1694
1695/* Remove redundant constraints in each of the basic sets of "uset".
1696 */
1697__isl_give isl_union_setisl_union_map *isl_union_set_remove_redundancies(
1698 __isl_take isl_union_setisl_union_map *uset)
1699{
1700 return isl_union_map_remove_redundancies(uset);
1701}
1702
1703__isl_give isl_union_map *isl_union_map_coalesce(
1704 __isl_take isl_union_map *umap)
1705{
1706 return inplace(umap, &isl_map_coalesce);
1707}
1708
1709__isl_give isl_union_setisl_union_map *isl_union_set_coalesce(
1710 __isl_take isl_union_setisl_union_map *uset)
1711{
1712 return isl_union_map_coalesce(uset);
1713}
1714
1715__isl_give isl_union_map *isl_union_map_detect_equalities(
1716 __isl_take isl_union_map *umap)
1717{
1718 return inplace(umap, &isl_map_detect_equalities);
1719}
1720
1721__isl_give isl_union_setisl_union_map *isl_union_set_detect_equalities(
1722 __isl_take isl_union_setisl_union_map *uset)
1723{
1724 return isl_union_map_detect_equalities(uset);
1725}
1726
1727__isl_give isl_union_map *isl_union_map_compute_divs(
1728 __isl_take isl_union_map *umap)
1729{
1730 return inplace(umap, &isl_map_compute_divs);
1731}
1732
1733__isl_give isl_union_setisl_union_map *isl_union_set_compute_divs(
1734 __isl_take isl_union_setisl_union_map *uset)
1735{
1736 return isl_union_map_compute_divs(uset);
1737}
1738
1739static isl_stat lexmin_entry(void **entry, void *user)
1740{
1741 isl_map **map = (isl_map **)entry;
1742
1743 *map = isl_map_lexmin(*map);
1744
1745 return *map ? isl_stat_ok : isl_stat_error;
1746}
1747
1748__isl_give isl_union_map *isl_union_map_lexmin(
1749 __isl_take isl_union_map *umap)
1750{
1751 return un_op(umap, &lexmin_entry);
1752}
1753
1754__isl_give isl_union_setisl_union_map *isl_union_set_lexmin(
1755 __isl_take isl_union_setisl_union_map *uset)
1756{
1757 return isl_union_map_lexmin(uset);
1758}
1759
1760static isl_stat lexmax_entry(void **entry, void *user)
1761{
1762 isl_map **map = (isl_map **)entry;
1763
1764 *map = isl_map_lexmax(*map);
1765
1766 return *map ? isl_stat_ok : isl_stat_error;
1767}
1768
1769__isl_give isl_union_map *isl_union_map_lexmax(
1770 __isl_take isl_union_map *umap)
1771{
1772 return un_op(umap, &lexmax_entry);
1773}
1774
1775__isl_give isl_union_setisl_union_map *isl_union_set_lexmax(
1776 __isl_take isl_union_setisl_union_map *uset)
1777{
1778 return isl_union_map_lexmax(uset);
1779}
1780
1781static isl_stat universe_entry(void **entry, void *user)
1782{
1783 isl_map *map = *entry;
1784 isl_union_map **res = user;
1785
1786 map = isl_map_universe(isl_map_get_space(map));
1787 *res = isl_union_map_add_map(*res, map);
1788
1789 return isl_stat_ok;
1790}
1791
1792__isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1793{
1794 return cond_un_op(umap, &universe_entry);
1795}
1796
1797__isl_give isl_union_setisl_union_map *isl_union_set_universe(__isl_take isl_union_setisl_union_map *uset)
1798{
1799 return isl_union_map_universe(uset);
1800}
1801
1802static isl_stat reverse_entry(void **entry, void *user)
1803{
1804 isl_map *map = *entry;
1805 isl_union_map **res = user;
1806
1807 *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1808
1809 return isl_stat_ok;
1810}
1811
1812__isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1813{
1814 return cond_un_op(umap, &reverse_entry);
1815}
1816
1817static isl_stat params_entry(void **entry, void *user)
1818{
1819 isl_map *map = *entry;
1820 isl_union_setisl_union_map **res = user;
1821
1822 *res = isl_union_set_add_set(*res, isl_map_params(isl_map_copy(map)));
1823
1824 return isl_stat_ok;
1825}
1826
1827/* Compute the parameter domain of the given union map.
1828 */
1829__isl_give isl_setisl_map *isl_union_map_params(__isl_take isl_union_map *umap)
1830{
1831 int empty;
1832
1833 empty = isl_union_map_is_empty(umap);
1834 if (empty < 0)
1835 goto error;
1836 if (empty) {
1837 isl_space *space;
1838 space = isl_union_map_get_space(umap);
1839 isl_union_map_free(umap);
1840 return isl_set_empty(space);
1841 }
1842 return isl_set_from_union_set(cond_un_op(umap, &params_entry));
1843error:
1844 isl_union_map_free(umap);
1845 return NULL((void*)0);
1846}
1847
1848/* Compute the parameter domain of the given union set.
1849 */
1850__isl_give isl_setisl_map *isl_union_set_params(__isl_take isl_union_setisl_union_map *uset)
1851{
1852 return isl_union_map_params(uset);
1853}
1854
1855static isl_stat domain_entry(void **entry, void *user)
1856{
1857 isl_map *map = *entry;
1858 isl_union_setisl_union_map **res = user;
1859
1860 *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1861
1862 return isl_stat_ok;
1863}
1864
1865__isl_give isl_union_setisl_union_map *isl_union_map_domain(__isl_take isl_union_map *umap)
1866{
1867 return cond_un_op(umap, &domain_entry);
1868}
1869
1870static isl_stat range_entry(void **entry, void *user)
1871{
1872 isl_map *map = *entry;
1873 isl_union_setisl_union_map **res = user;
1874
1875 *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1876
1877 return isl_stat_ok;
1878}
1879
1880__isl_give isl_union_setisl_union_map *isl_union_map_range(__isl_take isl_union_map *umap)
1881{
1882 return cond_un_op(umap, &range_entry);
1883}
1884
1885static isl_stat domain_map_entry(void **entry, void *user)
1886{
1887 isl_map *map = *entry;
1888 isl_union_setisl_union_map **res = user;
1889
1890 *res = isl_union_map_add_map(*res,
1891 isl_map_domain_map(isl_map_copy(map)));
1892
1893 return isl_stat_ok;
1894}
1895
1896__isl_give isl_union_map *isl_union_map_domain_map(
1897 __isl_take isl_union_map *umap)
1898{
1899 return cond_un_op(umap, &domain_map_entry);
1900}
1901
1902/* Construct an isl_pw_multi_aff that maps "map" to its domain and
1903 * add the result to "res".
1904 */
1905static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
1906{
1907 isl_union_pw_multi_aff **res = user;
1908 isl_multi_aff *ma;
1909 isl_pw_multi_aff *pma;
1910
1911 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
1912 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
1913 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
1914
1915 return *res ? isl_stat_ok : isl_stat_error;
1916
1917}
1918
1919/* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
1920 * to its domain.
1921 */
1922__isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
1923 __isl_take isl_union_map *umap)
1924{
1925 isl_union_pw_multi_aff *res;
1926
1927 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
1928 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
1929 res = isl_union_pw_multi_aff_free(res);
1930
1931 isl_union_map_free(umap);
1932 return res;
1933}
1934
1935static isl_stat range_map_entry(void **entry, void *user)
1936{
1937 isl_map *map = *entry;
1938 isl_union_setisl_union_map **res = user;
1939
1940 *res = isl_union_map_add_map(*res,
1941 isl_map_range_map(isl_map_copy(map)));
1942
1943 return isl_stat_ok;
1944}
1945
1946__isl_give isl_union_map *isl_union_map_range_map(
1947 __isl_take isl_union_map *umap)
1948{
1949 return cond_un_op(umap, &range_map_entry);
1950}
1951
1952/* Check if "set" is of the form A[B -> C].
1953 * If so, add A[B -> C] -> B to "res".
1954 */
1955static isl_stat wrapped_domain_map_entry(void **entry, void *user)
1956{
1957 isl_setisl_map *set = *entry;
1958 isl_union_setisl_union_map **res = user;
1959 int wrapping;
1960
1961 wrapping = isl_set_is_wrapping(set);
1962 if (wrapping < 0)
1963 return isl_stat_error;
1964 if (!wrapping)
1965 return isl_stat_ok;
1966
1967 *res = isl_union_map_add_map(*res,
1968 isl_set_wrapped_domain_map(isl_set_copy(set)));
1969
1970 return isl_stat_ok;
1971}
1972
1973/* Given a collection of wrapped maps of the form A[B -> C],
1974 * return the collection of maps A[B -> C] -> B.
1975 */
1976__isl_give isl_union_map *isl_union_set_wrapped_domain_map(
1977 __isl_take isl_union_setisl_union_map *uset)
1978{
1979 return cond_un_op(uset, &wrapped_domain_map_entry);
1980}
1981
1982static isl_stat deltas_entry(void **entry, void *user)
1983{
1984 isl_map *map = *entry;
1985 isl_union_setisl_union_map **res = user;
1986
1987 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
1988 map->dim, isl_dim_out))
1989 return isl_stat_ok;
1990
1991 *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1992
1993 return isl_stat_ok;
1994}
1995
1996__isl_give isl_union_setisl_union_map *isl_union_map_deltas(__isl_take isl_union_map *umap)
1997{
1998 return cond_un_op(umap, &deltas_entry);
1999}
2000
2001static isl_stat deltas_map_entry(void **entry, void *user)
2002{
2003 isl_map *map = *entry;
2004 isl_union_map **res = user;
2005
2006 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2007 map->dim, isl_dim_out))
2008 return isl_stat_ok;
2009
2010 *res = isl_union_map_add_map(*res,
2011 isl_map_deltas_map(isl_map_copy(map)));
2012
2013 return isl_stat_ok;
2014}
2015
2016__isl_give isl_union_map *isl_union_map_deltas_map(
2017 __isl_take isl_union_map *umap)
2018{
2019 return cond_un_op(umap, &deltas_map_entry);
2020}
2021
2022static isl_stat identity_entry(void **entry, void *user)
2023{
2024 isl_setisl_map *set = *entry;
2025 isl_union_map **res = user;
2026
2027 *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
2028
2029 return isl_stat_ok;
2030}
2031
2032__isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_setisl_union_map *uset)
2033{
2034 return cond_un_op(uset, &identity_entry);
2035}
2036
2037/* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
2038 */
2039static isl_stat identity_upma(__isl_take isl_setisl_map *set, void *user)
2040{
2041 isl_union_pw_multi_aff **res = user;
2042 isl_space *space;
2043 isl_pw_multi_aff *pma;
2044
2045 space = isl_space_map_from_set(isl_set_get_space(set));
2046 pma = isl_pw_multi_aff_identity(space);
2047 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2048 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2049
2050 return *res ? isl_stat_ok : isl_stat_error;
2051}
2052
2053/* Return an identity function on "uset" in the form
2054 * of an isl_union_pw_multi_aff.
2055 */
2056__isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2057 __isl_take isl_union_setisl_union_map *uset)
2058{
2059 isl_union_pw_multi_aff *res;
2060
2061 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2062 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2063 res = isl_union_pw_multi_aff_free(res);
2064
2065 isl_union_set_free(uset);
2066 return res;
2067}
2068
2069/* If "map" is of the form [A -> B] -> C, then add A -> C to "res".
2070 */
2071static isl_stat domain_factor_domain_entry(void **entry, void *user)
2072{
2073 isl_map *map = *entry;
2074 isl_union_map **res = user;
2075
2076 if (!isl_map_domain_is_wrapping(map))
2077 return isl_stat_ok;
2078
2079 *res = isl_union_map_add_map(*res,
2080 isl_map_domain_factor_domain(isl_map_copy(map)));
2081
2082 return *res ? isl_stat_ok : isl_stat_error;
2083}
2084
2085/* For each map in "umap" of the form [A -> B] -> C,
2086 * construct the map A -> C and collect the results.
2087 */
2088__isl_give isl_union_map *isl_union_map_domain_factor_domain(
2089 __isl_take isl_union_map *umap)
2090{
2091 return cond_un_op(umap, &domain_factor_domain_entry);
2092}
2093
2094/* If "map" is of the form [A -> B] -> C, then add B -> C to "res".
2095 */
2096static isl_stat domain_factor_range_entry(void **entry, void *user)
2097{
2098 isl_map *map = *entry;
2099 isl_union_map **res = user;
2100
2101 if (!isl_map_domain_is_wrapping(map))
2102 return isl_stat_ok;
2103
2104 *res = isl_union_map_add_map(*res,
2105 isl_map_domain_factor_range(isl_map_copy(map)));
2106
2107 return *res ? isl_stat_ok : isl_stat_error;
2108}
2109
2110/* For each map in "umap" of the form [A -> B] -> C,
2111 * construct the map B -> C and collect the results.
2112 */
2113__isl_give isl_union_map *isl_union_map_domain_factor_range(
2114 __isl_take isl_union_map *umap)
2115{
2116 return cond_un_op(umap, &domain_factor_range_entry);
2117}
2118
2119/* If "map" is of the form A -> [B -> C], then add A -> B to "res".
2120 */
2121static isl_stat range_factor_domain_entry(void **entry, void *user)
2122{
2123 isl_map *map = *entry;
2124 isl_union_map **res = user;
2125
2126 if (!isl_map_range_is_wrapping(map))
2127 return isl_stat_ok;
2128
2129 *res = isl_union_map_add_map(*res,
2130 isl_map_range_factor_domain(isl_map_copy(map)));
2131
2132 return *res ? isl_stat_ok : isl_stat_error;
2133}
2134
2135/* For each map in "umap" of the form A -> [B -> C],
2136 * construct the map A -> B and collect the results.
2137 */
2138__isl_give isl_union_map *isl_union_map_range_factor_domain(
2139 __isl_take isl_union_map *umap)
2140{
2141 return cond_un_op(umap, &range_factor_domain_entry);
2142}
2143
2144/* If "map" is of the form A -> [B -> C], then add A -> C to "res".
2145 */
2146static isl_stat range_factor_range_entry(void **entry, void *user)
2147{
2148 isl_map *map = *entry;
2149 isl_union_map **res = user;
2150
2151 if (!isl_map_range_is_wrapping(map))
2152 return isl_stat_ok;
2153
2154 *res = isl_union_map_add_map(*res,
2155 isl_map_range_factor_range(isl_map_copy(map)));
2156
2157 return *res ? isl_stat_ok : isl_stat_error;
2158}
2159
2160/* For each map in "umap" of the form A -> [B -> C],
2161 * construct the map A -> C and collect the results.
2162 */
2163__isl_give isl_union_map *isl_union_map_range_factor_range(
2164 __isl_take isl_union_map *umap)
2165{
2166 return cond_un_op(umap, &range_factor_range_entry);
2167}
2168
2169/* If "map" is of the form [A -> B] -> [C -> D], then add A -> C to "res".
2170 */
2171static isl_stat factor_domain_entry(void **entry, void *user)
2172{
2173 isl_map *map = *entry;
2174 isl_union_map **res = user;
2175
2176 if (!isl_map_domain_is_wrapping(map) || !isl_map_range_is_wrapping(map))
2177 return isl_stat_ok;
2178
2179 *res = isl_union_map_add_map(*res,
2180 isl_map_factor_domain(isl_map_copy(map)));
2181
2182 return *res ? isl_stat_ok : isl_stat_error;
2183}
2184
2185/* For each map in "umap" of the form [A -> B] -> [C -> D],
2186 * construct the map A -> C and collect the results.
2187 */
2188__isl_give isl_union_map *isl_union_map_factor_domain(
2189 __isl_take isl_union_map *umap)
2190{
2191 return cond_un_op(umap, &factor_domain_entry);
2192}
2193
2194/* If "map" is of the form [A -> B] -> [C -> D], then add B -> D to "res".
2195 */
2196static isl_stat factor_range_entry(void **entry, void *user)
2197{
2198 isl_map *map = *entry;
2199 isl_union_map **res = user;
2200
2201 if (!isl_map_domain_is_wrapping(map) || !isl_map_range_is_wrapping(map))
2202 return isl_stat_ok;
2203
2204 *res = isl_union_map_add_map(*res,
2205 isl_map_factor_range(isl_map_copy(map)));
2206
2207 return *res ? isl_stat_ok : isl_stat_error;
2208}
2209
2210/* For each map in "umap" of the form [A -> B] -> [C -> D],
2211 * construct the map B -> D and collect the results.
2212 */
2213__isl_give isl_union_map *isl_union_map_factor_range(
2214 __isl_take isl_union_map *umap)
2215{
2216 return cond_un_op(umap, &factor_range_entry);
2217}
2218
2219static isl_stat unwrap_entry(void **entry, void *user)
2220{
2221 isl_setisl_map *set = *entry;
2222 isl_union_setisl_union_map **res = user;
2223
2224 if (!isl_set_is_wrapping(set))
2225 return isl_stat_ok;
2226
2227 *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
2228
2229 return isl_stat_ok;
2230}
2231
2232__isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_setisl_union_map *uset)
2233{
2234 return cond_un_op(uset, &unwrap_entry);
2235}
2236
2237static isl_stat wrap_entry(void **entry, void *user)
2238{
2239 isl_map *map = *entry;
2240 isl_union_setisl_union_map **res = user;
2241
2242 *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
2243
2244 return isl_stat_ok;
2245}
2246
2247__isl_give isl_union_setisl_union_map *isl_union_map_wrap(__isl_take isl_union_map *umap)
2248{
2249 return cond_un_op(umap, &wrap_entry);
2250}
2251
2252struct isl_union_map_is_subset_data {
2253 isl_union_map *umap2;
2254 isl_bool is_subset;
2255};
2256
2257static isl_stat is_subset_entry(void **entry, void *user)
2258{
2259 struct isl_union_map_is_subset_data *data = user;
2260 uint32_t hash;
2261 struct isl_hash_table_entry *entry2;
2262 isl_map *map = *entry;
2263
2264 hash = isl_space_get_hash(map->dim);
2265 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2266 hash, &has_dim, map->dim, 0);
2267 if (!entry2) {
2268 int empty = isl_map_is_empty(map);
2269 if (empty < 0)
2270 return isl_stat_error;
2271 if (empty)
2272 return isl_stat_ok;
2273 data->is_subset = 0;
2274 return isl_stat_error;
2275 }
2276
2277 data->is_subset = isl_map_is_subset(map, entry2->data);
2278 if (data->is_subset < 0 || !data->is_subset)
2279 return isl_stat_error;
2280
2281 return isl_stat_ok;
2282}
2283
2284isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2285 __isl_keep isl_union_map *umap2)
2286{
2287 struct isl_union_map_is_subset_data data = { NULL((void*)0), isl_bool_true };
2288
2289 umap1 = isl_union_map_copy(umap1);
2290 umap2 = isl_union_map_copy(umap2);
2291 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2292 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2293
2294 if (!umap1 || !umap2)
2295 goto error;
2296
2297 data.umap2 = umap2;
2298 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2299 &is_subset_entry, &data) < 0 &&
2300 data.is_subset)
2301 goto error;
2302
2303 isl_union_map_free(umap1);
2304 isl_union_map_free(umap2);
2305
2306 return data.is_subset;
2307error:
2308 isl_union_map_free(umap1);
2309 isl_union_map_free(umap2);
2310 return isl_bool_error;
2311}
2312
2313isl_bool isl_union_set_is_subset(__isl_keep isl_union_setisl_union_map *uset1,
2314 __isl_keep isl_union_setisl_union_map *uset2)
2315{
2316 return isl_union_map_is_subset(uset1, uset2);
2317}
2318
2319isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2320 __isl_keep isl_union_map *umap2)
2321{
2322 isl_bool is_subset;
2323
2324 if (!umap1 || !umap2)
2325 return isl_bool_error;
2326 is_subset = isl_union_map_is_subset(umap1, umap2);
2327 if (is_subset != isl_bool_true)
2328 return is_subset;
2329 is_subset = isl_union_map_is_subset(umap2, umap1);
2330 return is_subset;
2331}
2332
2333isl_bool isl_union_set_is_equal(__isl_keep isl_union_setisl_union_map *uset1,
2334 __isl_keep isl_union_setisl_union_map *uset2)
2335{
2336 return isl_union_map_is_equal(uset1, uset2);
2337}
2338
2339isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2340 __isl_keep isl_union_map *umap2)
2341{
2342 isl_bool is_subset;
2343
2344 if (!umap1 || !umap2)
2345 return isl_bool_error;
2346 is_subset = isl_union_map_is_subset(umap1, umap2);
2347 if (is_subset != isl_bool_true)
2348 return is_subset;
2349 is_subset = isl_union_map_is_subset(umap2, umap1);
2350 if (is_subset == isl_bool_error)
2351 return is_subset;
2352 return !is_subset;
2353}
2354
2355isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_setisl_union_map *uset1,
2356 __isl_keep isl_union_setisl_union_map *uset2)
2357{
2358 return isl_union_map_is_strict_subset(uset1, uset2);
2359}
2360
2361/* Internal data structure for isl_union_map_is_disjoint.
2362 * umap2 is the union map with which we are comparing.
2363 * is_disjoint is initialized to 1 and is set to 0 as soon
2364 * as the union maps turn out not to be disjoint.
2365 */
2366struct isl_union_map_is_disjoint_data {
2367 isl_union_map *umap2;
2368 isl_bool is_disjoint;
2369};
2370
2371/* Check if "map" is disjoint from data->umap2 and abort
2372 * the search if it is not.
2373 */
2374static isl_stat is_disjoint_entry(void **entry, void *user)
2375{
2376 struct isl_union_map_is_disjoint_data *data = user;
2377 uint32_t hash;
2378 struct isl_hash_table_entry *entry2;
2379 isl_map *map = *entry;
2380
2381 hash = isl_space_get_hash(map->dim);
2382 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2383 hash, &has_dim, map->dim, 0);
2384 if (!entry2)
2385 return isl_stat_ok;
2386
2387 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2388 if (data->is_disjoint < 0 || !data->is_disjoint)
2389 return isl_stat_error;
2390
2391 return isl_stat_ok;
2392}
2393
2394/* Are "umap1" and "umap2" disjoint?
2395 */
2396isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2397 __isl_keep isl_union_map *umap2)
2398{
2399 struct isl_union_map_is_disjoint_data data = { NULL((void*)0), isl_bool_true };
2400
2401 umap1 = isl_union_map_copy(umap1);
2402 umap2 = isl_union_map_copy(umap2);
2403 umap1 = isl_union_map_align_params(umap1,
2404 isl_union_map_get_space(umap2));
2405 umap2 = isl_union_map_align_params(umap2,
2406 isl_union_map_get_space(umap1));
2407
2408 if (!umap1 || !umap2)
2409 goto error;
2410
2411 data.umap2 = umap2;
2412 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2413 &is_disjoint_entry, &data) < 0 &&
2414 data.is_disjoint)
2415 goto error;
2416
2417 isl_union_map_free(umap1);
2418 isl_union_map_free(umap2);
2419
2420 return data.is_disjoint;
2421error:
2422 isl_union_map_free(umap1);
2423 isl_union_map_free(umap2);
2424 return isl_bool_error;
2425}
2426
2427/* Are "uset1" and "uset2" disjoint?
2428 */
2429isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_setisl_union_map *uset1,
2430 __isl_keep isl_union_setisl_union_map *uset2)
2431{
2432 return isl_union_map_is_disjoint(uset1, uset2);
2433}
2434
2435static isl_stat sample_entry(void **entry, void *user)
2436{
2437 isl_basic_map **sample = (isl_basic_map **)user;
2438 isl_map *map = *entry;
2439
2440 *sample = isl_map_sample(isl_map_copy(map));
2441 if (!*sample)
2442 return isl_stat_error;
2443 if (!isl_basic_map_plain_is_empty(*sample))
2444 return isl_stat_error;
2445 return isl_stat_ok;
2446}
2447
2448__isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2449{
2450 isl_basic_map *sample = NULL((void*)0);
2451
2452 if (!umap)
2453 return NULL((void*)0);
2454
2455 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2456 &sample_entry, &sample) < 0 &&
2457 !sample)
2458 goto error;
2459
2460 if (!sample)
2461 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2462
2463 isl_union_map_free(umap);
2464
2465 return sample;
2466error:
2467 isl_union_map_free(umap);
2468 return NULL((void*)0);
2469}
2470
2471__isl_give isl_basic_setisl_basic_map *isl_union_set_sample(__isl_take isl_union_setisl_union_map *uset)
2472{
2473 return bset_from_bmap(isl_union_map_sample(uset));
2474}
2475
2476/* Return an element in "uset" in the form of an isl_point.
2477 * Return a void isl_point if "uset" is empty.
2478 */
2479__isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_setisl_union_map *uset)
2480{
2481 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2482}
2483
2484struct isl_forall_data {
2485 isl_bool res;
2486 isl_bool (*fn)(__isl_keep isl_map *map);
2487};
2488
2489static isl_stat forall_entry(void **entry, void *user)
2490{
2491 struct isl_forall_data *data = user;
2492 isl_map *map = *entry;
2493
2494 data->res = data->fn(map);
2495 if (data->res < 0)
2496 return isl_stat_error;
2497
2498 if (!data->res)
2499 return isl_stat_error;
2500
2501 return isl_stat_ok;
2502}
2503
2504static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2505 isl_bool (*fn)(__isl_keep isl_map *map))
2506{
2507 struct isl_forall_data data = { isl_bool_true, fn };
2508
2509 if (!umap)
2510 return isl_bool_error;
2511
2512 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2513 &forall_entry, &data) < 0 && data.res)
2514 return isl_bool_error;
2515
2516 return data.res;
2517}
2518
2519struct isl_forall_user_data {
2520 isl_bool res;
2521 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2522 void *user;
2523};
2524
2525static isl_stat forall_user_entry(void **entry, void *user)
2526{
2527 struct isl_forall_user_data *data = user;
2528 isl_map *map = *entry;
2529
2530 data->res = data->fn(map, data->user);
2531 if (data->res < 0)
2532 return isl_stat_error;
2533
2534 if (!data->res)
2535 return isl_stat_error;
2536
2537 return isl_stat_ok;
2538}
2539
2540/* Check if fn(map, user) returns true for all maps "map" in umap.
2541 */
2542static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2543 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2544{
2545 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2546
2547 if (!umap)
2548 return isl_bool_error;
2549
2550 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2551 &forall_user_entry, &data) < 0 && data.res)
2552 return isl_bool_error;
2553
2554 return data.res;
2555}
2556
2557isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2558{
2559 return union_map_forall(umap, &isl_map_is_empty);
2560}
2561
2562isl_bool isl_union_set_is_empty(__isl_keep isl_union_setisl_union_map *uset)
2563{
2564 return isl_union_map_is_empty(uset);
2565}
2566
2567static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2568{
2569 isl_bool is_subset;
2570 isl_space *dim;
2571 isl_map *id;
2572
2573 if (!map)
2574 return isl_bool_error;
2575
2576 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2577 map->dim, isl_dim_out))
2578 return isl_bool_false;
2579
2580 dim = isl_map_get_space(map);
2581 id = isl_map_identity(dim);
2582
2583 is_subset = isl_map_is_subset(map, id);
2584
2585 isl_map_free(id);
2586
2587 return is_subset;
2588}
2589
2590/* Given an isl_union_map that consists of a single map, check
2591 * if it is single-valued.
2592 */
2593static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2594{
2595 isl_map *map;
2596 isl_bool sv;
2597
2598 umap = isl_union_map_copy(umap);
2599 map = isl_map_from_union_map(umap);
2600 sv = isl_map_is_single_valued(map);
2601 isl_map_free(map);
2602
2603 return sv;
2604}
2605
2606/* Internal data structure for single_valued_on_domain.
2607 *
2608 * "umap" is the union map to be tested.
2609 * "sv" is set to 1 as long as "umap" may still be single-valued.
2610 */
2611struct isl_union_map_is_sv_data {
2612 isl_union_map *umap;
2613 isl_bool sv;
2614};
2615
2616/* Check if the data->umap is single-valued on "set".
2617 *
2618 * If data->umap consists of a single map on "set", then test it
2619 * as an isl_map.
2620 *
2621 * Otherwise, compute
2622 *
2623 * M \circ M^-1
2624 *
2625 * check if the result is a subset of the identity mapping and
2626 * store the result in data->sv.
2627 *
2628 * Terminate as soon as data->umap has been determined not to
2629 * be single-valued.
2630 */
2631static isl_stat single_valued_on_domain(__isl_take isl_setisl_map *set, void *user)
2632{
2633 struct isl_union_map_is_sv_data *data = user;
2634 isl_union_map *umap, *test;
2635
2636 umap = isl_union_map_copy(data->umap);
2637 umap = isl_union_map_intersect_domain(umap,
2638 isl_union_set_from_set(set));
2639
2640 if (isl_union_map_n_map(umap) == 1) {
2641 data->sv = single_map_is_single_valued(umap);
2642 isl_union_map_free(umap);
2643 } else {
2644 test = isl_union_map_reverse(isl_union_map_copy(umap));
2645 test = isl_union_map_apply_range(test, umap);
2646
2647 data->sv = union_map_forall(test, &is_subset_of_identity);
2648
2649 isl_union_map_free(test);
2650 }
2651
2652 if (data->sv < 0 || !data->sv)
2653 return isl_stat_error;
2654 return isl_stat_ok;
2655}
2656
2657/* Check if the given map is single-valued.
2658 *
2659 * If the union map consists of a single map, then test it as an isl_map.
2660 * Otherwise, check if the union map is single-valued on each of its
2661 * domain spaces.
2662 */
2663isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2664{
2665 isl_union_map *universe;
2666 isl_union_setisl_union_map *domain;
2667 struct isl_union_map_is_sv_data data;
2668
2669 if (isl_union_map_n_map(umap) == 1)
2670 return single_map_is_single_valued(umap);
2671
2672 universe = isl_union_map_universe(isl_union_map_copy(umap));
2673 domain = isl_union_map_domain(universe);
2674
2675 data.sv = isl_bool_true;
2676 data.umap = umap;
2677 if (isl_union_set_foreach_set(domain,
2678 &single_valued_on_domain, &data) < 0 && data.sv)
2679 data.sv = isl_bool_error;
2680 isl_union_set_free(domain);
2681
2682 return data.sv;
2683}
2684
2685isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2686{
2687 isl_bool in;
2688
2689 umap = isl_union_map_copy(umap);
2690 umap = isl_union_map_reverse(umap);
2691 in = isl_union_map_is_single_valued(umap);
2692 isl_union_map_free(umap);
2693
2694 return in;
2695}
2696
2697/* Is "map" obviously not an identity relation because
2698 * it maps elements from one space to another space?
2699 * Update *non_identity accordingly.
2700 *
2701 * In particular, if the domain and range spaces are the same,
2702 * then the map is not considered to obviously not be an identity relation.
2703 * Otherwise, the map is considered to obviously not be an identity relation
2704 * if it is is non-empty.
2705 *
2706 * If "map" is determined to obviously not be an identity relation,
2707 * then the search is aborted.
2708 */
2709static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2710{
2711 isl_bool *non_identity = user;
2712 isl_bool equal;
2713 isl_space *space;
2714
2715 space = isl_map_get_space(map);
2716 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2717 if (equal >= 0 && !equal)
2718 *non_identity = isl_bool_not(isl_map_is_empty(map));
2719 else
2720 *non_identity = isl_bool_not(equal);
2721 isl_space_free(space);
2722 isl_map_free(map);
2723
2724 if (*non_identity < 0 || *non_identity)
2725 return isl_stat_error;
2726
2727 return isl_stat_ok;
2728}
2729
2730/* Is "umap" obviously not an identity relation because
2731 * it maps elements from one space to another space?
2732 *
2733 * As soon as a map has been found that maps elements to a different space,
2734 * non_identity is changed and the search is aborted.
2735 */
2736static isl_bool isl_union_map_plain_is_not_identity(
2737 __isl_keep isl_union_map *umap)
2738{
2739 isl_bool non_identity;
2740
2741 non_identity = isl_bool_false;
2742 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2743 &non_identity) < 0 &&
2744 non_identity == isl_bool_false)
2745 return isl_bool_error;
2746
2747 return non_identity;
2748}
2749
2750/* Does "map" only map elements to themselves?
2751 * Update *identity accordingly.
2752 *
2753 * If "map" is determined not to be an identity relation,
2754 * then the search is aborted.
2755 */
2756static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2757{
2758 isl_bool *identity = user;
2759
2760 *identity = isl_map_is_identity(map);
2761 isl_map_free(map);
2762
2763 if (*identity < 0 || !*identity)
2764 return isl_stat_error;
2765
2766 return isl_stat_ok;
2767}
2768
2769/* Does "umap" only map elements to themselves?
2770 *
2771 * First check if there are any maps that map elements to different spaces.
2772 * If not, then check that all the maps (between identical spaces)
2773 * are identity relations.
2774 */
2775isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2776{
2777 isl_bool non_identity;
2778 isl_bool identity;
2779
2780 non_identity = isl_union_map_plain_is_not_identity(umap);
2781 if (non_identity < 0 || non_identity)
2782 return isl_bool_not(non_identity);
2783
2784 identity = isl_bool_true;
2785 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2786 identity == isl_bool_true)
2787 return isl_bool_error;
2788
2789 return identity;
2790}
2791
2792/* Represents a map that has a fixed value (v) for one of its
2793 * range dimensions.
2794 * The map in this structure is not reference counted, so it
2795 * is only valid while the isl_union_map from which it was
2796 * obtained is still alive.
2797 */
2798struct isl_fixed_map {
2799 isl_int v;
2800 isl_map *map;
2801};
2802
2803static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2804 int n)
2805{
2806 int i;
2807 struct isl_fixed_map *v;
2808
2809 v = isl_calloc_array(ctx, struct isl_fixed_map, n)((struct isl_fixed_map *)isl_calloc_or_die(ctx, n, sizeof(struct
isl_fixed_map)))
;
2810 if (!v)
2811 return NULL((void*)0);
2812 for (i = 0; i < n; ++i)
2813 isl_int_init(v[i].v)isl_sioimath_init((v[i].v));
2814 return v;
2815}
2816
2817static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2818{
2819 int i;
2820
2821 if (!v)
2822 return;
2823 for (i = 0; i < n; ++i)
2824 isl_int_clear(v[i].v)isl_sioimath_clear((v[i].v));
2825 free(v);
2826}
2827
2828/* Compare the "v" field of two isl_fixed_map structs.
2829 */
2830static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2831{
2832 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2833 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2834
2835 return isl_int_cmp(e1->v, e2->v)isl_sioimath_cmp(*(e1->v), *(e2->v));
2836}
2837
2838/* Internal data structure used while checking whether all maps
2839 * in a union_map have a fixed value for a given output dimension.
2840 * v is the list of maps, with the fixed value for the dimension
2841 * n is the number of maps considered so far
2842 * pos is the output dimension under investigation
2843 */
2844struct isl_fixed_dim_data {
2845 struct isl_fixed_map *v;
2846 int n;
2847 int pos;
2848};
2849
2850static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2851{
2852 struct isl_fixed_dim_data *data = user;
2853
2854 data->v[data->n].map = map;
2855 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2856 &data->v[data->n++].v);
2857}
2858
2859static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2860 int first, int n_range);
2861
2862/* Given a list of the maps, with their fixed values at output dimension "pos",
2863 * check whether the ranges of the maps form an obvious partition.
2864 *
2865 * We first sort the maps according to their fixed values.
2866 * If all maps have a different value, then we know the ranges form
2867 * a partition.
2868 * Otherwise, we collect the maps with the same fixed value and
2869 * check whether each such collection is obviously injective
2870 * based on later dimensions.
2871 */
2872static int separates(struct isl_fixed_map *v, int n,
2873 __isl_take isl_space *dim, int pos, int n_range)
2874{
2875 int i;
2876
2877 if (!v)
2878 goto error;
2879
2880 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2881
2882 for (i = 0; i + 1 < n; ++i) {
2883 int j, k;
2884 isl_union_map *part;
2885 int injective;
2886
2887 for (j = i + 1; j < n; ++j)
2888 if (isl_int_ne(v[i].v, v[j].v)(isl_sioimath_cmp(*(v[i].v), *(v[j].v)) != 0))
2889 break;
2890
2891 if (j == i + 1)
2892 continue;
2893
2894 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2895 for (k = i; k < j; ++k)
2896 part = isl_union_map_add_map(part,
2897 isl_map_copy(v[k].map));
2898
2899 injective = plain_injective_on_range(part, pos + 1, n_range);
2900 if (injective < 0)
2901 goto error;
2902 if (!injective)
2903 break;
2904
2905 i = j - 1;
2906 }
2907
2908 isl_space_free(dim);
2909 free_isl_fixed_map_array(v, n);
2910 return i + 1 >= n;
2911error:
2912 isl_space_free(dim);
2913 free_isl_fixed_map_array(v, n);
2914 return -1;
2915}
2916
2917/* Check whether the maps in umap have obviously distinct ranges.
2918 * In particular, check for an output dimension in the range
2919 * [first,n_range) for which all maps have a fixed value
2920 * and then check if these values, possibly along with fixed values
2921 * at later dimensions, entail distinct ranges.
2922 */
2923static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2924 int first, int n_range)
2925{
2926 isl_ctx *ctx;
2927 int n;
2928 struct isl_fixed_dim_data data = { NULL((void*)0) };
2929
2930 ctx = isl_union_map_get_ctx(umap);
2931
2932 n = isl_union_map_n_map(umap);
2933 if (!umap)
2934 goto error;
2935
2936 if (n <= 1) {
2937 isl_union_map_free(umap);
2938 return isl_bool_true;
2939 }
2940
2941 if (first >= n_range) {
2942 isl_union_map_free(umap);
2943 return isl_bool_false;
2944 }
2945
2946 data.v = alloc_isl_fixed_map_array(ctx, n);
2947 if (!data.v)
2948 goto error;
2949
2950 for (data.pos = first; data.pos < n_range; ++data.pos) {
2951 isl_bool fixed;
2952 int injective;
2953 isl_space *dim;
2954
2955 data.n = 0;
2956 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2957 if (fixed < 0)
2958 goto error;
2959 if (!fixed)
2960 continue;
2961 dim = isl_union_map_get_space(umap);
2962 injective = separates(data.v, n, dim, data.pos, n_range);
2963 isl_union_map_free(umap);
2964 return injective;
2965 }
2966
2967 free_isl_fixed_map_array(data.v, n);
2968 isl_union_map_free(umap);
2969
2970 return isl_bool_false;
2971error:
2972 free_isl_fixed_map_array(data.v, n);
2973 isl_union_map_free(umap);
2974 return isl_bool_error;
2975}
2976
2977/* Check whether the maps in umap that map to subsets of "ran"
2978 * have obviously distinct ranges.
2979 */
2980static isl_bool plain_injective_on_range_wrap(__isl_keep isl_setisl_map *ran,
2981 void *user)
2982{
2983 isl_union_map *umap = user;
2984
2985 umap = isl_union_map_copy(umap);
2986 umap = isl_union_map_intersect_range(umap,
2987 isl_union_set_from_set(isl_set_copy(ran)));
2988 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2989}
2990
2991/* Check if the given union_map is obviously injective.
2992 *
2993 * In particular, we first check if all individual maps are obviously
2994 * injective and then check if all the ranges of these maps are
2995 * obviously disjoint.
2996 */
2997isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2998{
2999 isl_bool in;
3000 isl_union_map *univ;
3001 isl_union_setisl_union_map *ran;
3002
3003 in = union_map_forall(umap, &isl_map_plain_is_injective);
3004 if (in < 0)
3005 return isl_bool_error;
3006 if (!in)
3007 return isl_bool_false;
3008
3009 univ = isl_union_map_universe(isl_union_map_copy(umap));
3010 ran = isl_union_map_range(univ);
3011
3012 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
3013
3014 isl_union_set_free(ran);
3015
3016 return in;
3017}
3018
3019isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
3020{
3021 isl_bool sv;
3022
3023 sv = isl_union_map_is_single_valued(umap);
3024 if (sv < 0 || !sv)
3025 return sv;
3026
3027 return isl_union_map_is_injective(umap);
3028}
3029
3030static isl_stat zip_entry(void **entry, void *user)
3031{
3032 isl_map *map = *entry;
3033 isl_union_map **res = user;
3034
3035 if (!isl_map_can_zip(map))
3036 return isl_stat_ok;
3037
3038 *res = isl_union_map_add_map(*res, isl_map_zip(isl_map_copy(map)));
3039
3040 return isl_stat_ok;
3041}
3042
3043__isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
3044{
3045 return cond_un_op(umap, &zip_entry);
3046}
3047
3048static isl_stat uncurry_entry(void **entry, void *user)
3049{
3050 isl_map *map = *entry;
3051 isl_union_map **res = user;
3052
3053 if (!isl_map_can_uncurry(map))
3054 return isl_stat_ok;
3055
3056 *res = isl_union_map_add_map(*res, isl_map_uncurry(isl_map_copy(map)));
3057
3058 return isl_stat_ok;
3059}
3060
3061/* Given a union map, take the maps of the form A -> (B -> C) and
3062 * return the union of the corresponding maps (A -> B) -> C.
3063 */
3064__isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
3065{
3066 return cond_un_op(umap, &uncurry_entry);
3067}
3068
3069static isl_stat curry_entry(void **entry, void *user)
3070{
3071 isl_map *map = *entry;
3072 isl_union_map **res = user;
3073
3074 if (!isl_map_can_curry(map))
3075 return isl_stat_ok;
3076
3077 *res = isl_union_map_add_map(*res, isl_map_curry(isl_map_copy(map)));
3078
3079 return isl_stat_ok;
3080}
3081
3082/* Given a union map, take the maps of the form (A -> B) -> C and
3083 * return the union of the corresponding maps A -> (B -> C).
3084 */
3085__isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
3086{
3087 return cond_un_op(umap, &curry_entry);
3088}
3089
3090/* If *entry is of the form A -> ((B -> C) -> D), then apply
3091 * isl_map_range_curry to it and add the result to *res.
3092 */
3093static isl_stat range_curry_entry(void **entry, void *user)
3094{
3095 isl_map *map = *entry;
3096 isl_union_map **res = user;
3097
3098 if (!isl_map_can_range_curry(map))
3099 return isl_stat_ok;
3100
3101 map = isl_map_range_curry(isl_map_copy(map));
3102 *res = isl_union_map_add_map(*res, map);
3103
3104 return isl_stat_ok;
3105}
3106
3107/* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
3108 * return the union of the corresponding maps A -> (B -> (C -> D)).
3109 */
3110__isl_give isl_union_map *isl_union_map_range_curry(
3111 __isl_take isl_union_map *umap)
3112{
3113 return cond_un_op(umap, &range_curry_entry);
3114}
3115
3116static isl_stat lift_entry(void **entry, void *user)
3117{
3118 isl_setisl_map *set = *entry;
3119 isl_union_setisl_union_map **res = user;
3120
3121 *res = isl_union_set_add_set(*res, isl_set_lift(isl_set_copy(set)));
3122
3123 return isl_stat_ok;
3124}
3125
3126__isl_give isl_union_setisl_union_map *isl_union_set_lift(__isl_take isl_union_setisl_union_map *uset)
3127{
3128 return cond_un_op(uset, &lift_entry);
3129}
3130
3131static isl_stat coefficients_entry(void **entry, void *user)
3132{
3133 isl_setisl_map *set = *entry;
3134 isl_union_setisl_union_map **res = user;
3135
3136 set = isl_set_copy(set);
3137 set = isl_set_from_basic_set(isl_set_coefficients(set));
3138 *res = isl_union_set_add_set(*res, set);
3139
3140 return isl_stat_ok;
3141}
3142
3143__isl_give isl_union_setisl_union_map *isl_union_set_coefficients(
3144 __isl_take isl_union_setisl_union_map *uset)
3145{
3146 isl_ctx *ctx;
3147 isl_space *dim;
3148 isl_union_setisl_union_map *res;
3149
3150 if (!uset)
3151 return NULL((void*)0);
3152
3153 ctx = isl_union_set_get_ctx(uset);
3154 dim = isl_space_set_alloc(ctx, 0, 0);
3155 res = isl_union_map_alloc(dim, uset->table.n);
3156 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3157 &coefficients_entry, &res) < 0)
3158 goto error;
3159
3160 isl_union_set_free(uset);
3161 return res;
3162error:
3163 isl_union_set_free(uset);
3164 isl_union_set_free(res);
3165 return NULL((void*)0);
3166}
3167
3168static isl_stat solutions_entry(void **entry, void *user)
3169{
3170 isl_setisl_map *set = *entry;
3171 isl_union_setisl_union_map **res = user;
3172
3173 set = isl_set_copy(set);
3174 set = isl_set_from_basic_set(isl_set_solutions(set));
3175 if (!*res)
3176 *res = isl_union_set_from_set(set);
3177 else
3178 *res = isl_union_set_add_set(*res, set);
3179
3180 if (!*res)
3181 return isl_stat_error;
3182
3183 return isl_stat_ok;
3184}
3185
3186__isl_give isl_union_setisl_union_map *isl_union_set_solutions(
3187 __isl_take isl_union_setisl_union_map *uset)
3188{
3189 isl_union_setisl_union_map *res = NULL((void*)0);
3190
3191 if (!uset)
3192 return NULL((void*)0);
3193
3194 if (uset->table.n == 0) {
3195 res = isl_union_set_empty(isl_union_set_get_space(uset));
3196 isl_union_set_free(uset);
3197 return res;
3198 }
3199
3200 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3201 &solutions_entry, &res) < 0)
3202 goto error;
3203
3204 isl_union_set_free(uset);
3205 return res;
3206error:
3207 isl_union_set_free(uset);
3208 isl_union_set_free(res);
3209 return NULL((void*)0);
3210}
3211
3212/* Is the domain space of "map" equal to "space"?
3213 */
3214static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3215{
3216 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3217 space, isl_dim_out);
3218}
3219
3220/* Is the range space of "map" equal to "space"?
3221 */
3222static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3223{
3224 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3225 space, isl_dim_out);
3226}
3227
3228/* Is the set space of "map" equal to "space"?
3229 */
3230static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3231{
3232 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3233 space, isl_dim_out);
3234}
3235
3236/* Internal data structure for preimage_pw_multi_aff.
3237 *
3238 * "pma" is the function under which the preimage should be taken.
3239 * "space" is the space of "pma".
3240 * "res" collects the results.
3241 * "fn" computes the preimage for a given map.
3242 * "match" returns true if "fn" can be called.
3243 */
3244struct isl_union_map_preimage_data {
3245 isl_space *space;
3246 isl_pw_multi_aff *pma;
3247 isl_union_map *res;
3248 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3249 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3250 __isl_take isl_pw_multi_aff *pma);
3251};
3252
3253/* Call data->fn to compute the preimage of the domain or range of *entry
3254 * under the function represented by data->pma, provided the domain/range
3255 * space of *entry matches the target space of data->pma
3256 * (as given by data->match), and add the result to data->res.
3257 */
3258static isl_stat preimage_entry(void **entry, void *user)
3259{
3260 int m;
3261 isl_map *map = *entry;
3262 struct isl_union_map_preimage_data *data = user;
3263 isl_bool empty;
3264
3265 m = data->match(map, data->space);
3266 if (m < 0)
3267 return isl_stat_error;
3268 if (!m)
3269 return isl_stat_ok;
3270
3271 map = isl_map_copy(map);
3272 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3273
3274 empty = isl_map_is_empty(map);
3275 if (empty < 0 || empty) {
3276 isl_map_free(map);
3277 return empty < 0 ? isl_stat_error : isl_stat_ok;
3278 }
3279
3280 data->res = isl_union_map_add_map(data->res, map);
3281
3282 return isl_stat_ok;
3283}
3284
3285/* Compute the preimage of the domain or range of "umap" under the function
3286 * represented by "pma".
3287 * In other words, plug in "pma" in the domain or range of "umap".
3288 * The function "fn" performs the actual preimage computation on a map,
3289 * while "match" determines to which maps the function should be applied.
3290 */
3291static __isl_give isl_union_map *preimage_pw_multi_aff(
3292 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3293 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3294 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3295 __isl_take isl_pw_multi_aff *pma))
3296{
3297 isl_ctx *ctx;
3298 isl_space *space;
3299 struct isl_union_map_preimage_data data;
3300
3301 umap = isl_union_map_align_params(umap,
3302 isl_pw_multi_aff_get_space(pma));
3303 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3304
3305 if (!umap || !pma)
3306 goto error;
3307
3308 ctx = isl_union_map_get_ctx(umap);
3309 space = isl_union_map_get_space(umap);
3310 data.space = isl_pw_multi_aff_get_space(pma);
3311 data.pma = pma;
3312 data.res = isl_union_map_alloc(space, umap->table.n);
3313 data.match = match;
3314 data.fn = fn;
3315 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3316 &data) < 0)
3317 data.res = isl_union_map_free(data.res);
3318
3319 isl_space_free(data.space);
3320 isl_union_map_free(umap);
3321 isl_pw_multi_aff_free(pma);
3322 return data.res;
3323error:
3324 isl_union_map_free(umap);
3325 isl_pw_multi_aff_free(pma);
3326 return NULL((void*)0);
3327}
3328
3329/* Compute the preimage of the domain of "umap" under the function
3330 * represented by "pma".
3331 * In other words, plug in "pma" in the domain of "umap".
3332 * The result contains maps that live in the same spaces as the maps of "umap"
3333 * with domain space equal to the target space of "pma",
3334 * except that the domain has been replaced by the domain space of "pma".
3335 */
3336__isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3337 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3338{
3339 return preimage_pw_multi_aff(umap, pma, &domain_match,
3340 &isl_map_preimage_domain_pw_multi_aff);
3341}
3342
3343/* Compute the preimage of the range of "umap" under the function
3344 * represented by "pma".
3345 * In other words, plug in "pma" in the range of "umap".
3346 * The result contains maps that live in the same spaces as the maps of "umap"
3347 * with range space equal to the target space of "pma",
3348 * except that the range has been replaced by the domain space of "pma".
3349 */
3350__isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3351 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3352{
3353 return preimage_pw_multi_aff(umap, pma, &range_match,
3354 &isl_map_preimage_range_pw_multi_aff);
3355}
3356
3357/* Compute the preimage of "uset" under the function represented by "pma".
3358 * In other words, plug in "pma" in "uset".
3359 * The result contains sets that live in the same spaces as the sets of "uset"
3360 * with space equal to the target space of "pma",
3361 * except that the space has been replaced by the domain space of "pma".
3362 */
3363__isl_give isl_union_setisl_union_map *isl_union_set_preimage_pw_multi_aff(
3364 __isl_take isl_union_setisl_union_map *uset, __isl_take isl_pw_multi_aff *pma)
3365{
3366 return preimage_pw_multi_aff(uset, pma, &set_match,
3367 &isl_set_preimage_pw_multi_aff);
3368}
3369
3370/* Compute the preimage of the domain of "umap" under the function
3371 * represented by "ma".
3372 * In other words, plug in "ma" in the domain of "umap".
3373 * The result contains maps that live in the same spaces as the maps of "umap"
3374 * with domain space equal to the target space of "ma",
3375 * except that the domain has been replaced by the domain space of "ma".
3376 */
3377__isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3378 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3379{
3380 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3381 isl_pw_multi_aff_from_multi_aff(ma));
3382}
3383
3384/* Compute the preimage of the range of "umap" under the function
3385 * represented by "ma".
3386 * In other words, plug in "ma" in the range of "umap".
3387 * The result contains maps that live in the same spaces as the maps of "umap"
3388 * with range space equal to the target space of "ma",
3389 * except that the range has been replaced by the domain space of "ma".
3390 */
3391__isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3392 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3393{
3394 return isl_union_map_preimage_range_pw_multi_aff(umap,
3395 isl_pw_multi_aff_from_multi_aff(ma));
3396}
3397
3398/* Compute the preimage of "uset" under the function represented by "ma".
3399 * In other words, plug in "ma" in "uset".
3400 * The result contains sets that live in the same spaces as the sets of "uset"
3401 * with space equal to the target space of "ma",
3402 * except that the space has been replaced by the domain space of "ma".
3403 */
3404__isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3405 __isl_take isl_union_setisl_union_map *uset, __isl_take isl_multi_aff *ma)
3406{
3407 return isl_union_set_preimage_pw_multi_aff(uset,
3408 isl_pw_multi_aff_from_multi_aff(ma));
3409}
3410
3411/* Internal data structure for preimage_multi_pw_aff.
3412 *
3413 * "mpa" is the function under which the preimage should be taken.
3414 * "space" is the space of "mpa".
3415 * "res" collects the results.
3416 * "fn" computes the preimage for a given map.
3417 * "match" returns true if "fn" can be called.
3418 */
3419struct isl_union_map_preimage_mpa_data {
3420 isl_space *space;
3421 isl_multi_pw_aff *mpa;
3422 isl_union_map *res;
3423 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3424 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3425 __isl_take isl_multi_pw_aff *mpa);
3426};
3427
3428/* Call data->fn to compute the preimage of the domain or range of *entry
3429 * under the function represented by data->mpa, provided the domain/range
3430 * space of *entry matches the target space of data->mpa
3431 * (as given by data->match), and add the result to data->res.
3432 */
3433static isl_stat preimage_mpa_entry(void **entry, void *user)
3434{
3435 int m;
3436 isl_map *map = *entry;
3437 struct isl_union_map_preimage_mpa_data *data = user;
3438 isl_bool empty;
3439
3440 m = data->match(map, data->space);
3441 if (m < 0)
3442 return isl_stat_error;
3443 if (!m)
3444 return isl_stat_ok;
3445
3446 map = isl_map_copy(map);
3447 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3448
3449 empty = isl_map_is_empty(map);
3450 if (empty < 0 || empty) {
3451 isl_map_free(map);
3452 return empty < 0 ? isl_stat_error : isl_stat_ok;
3453 }
3454
3455 data->res = isl_union_map_add_map(data->res, map);
3456
3457 return isl_stat_ok;
3458}
3459
3460/* Compute the preimage of the domain or range of "umap" under the function
3461 * represented by "mpa".
3462 * In other words, plug in "mpa" in the domain or range of "umap".
3463 * The function "fn" performs the actual preimage computation on a map,
3464 * while "match" determines to which maps the function should be applied.
3465 */
3466static __isl_give isl_union_map *preimage_multi_pw_aff(
3467 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3468 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3469 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3470 __isl_take isl_multi_pw_aff *mpa))
3471{
3472 isl_ctx *ctx;
3473 isl_space *space;
3474 struct isl_union_map_preimage_mpa_data data;
3475
3476 umap = isl_union_map_align_params(umap,
3477 isl_multi_pw_aff_get_space(mpa));
3478 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3479
3480 if (!umap || !mpa)
3481 goto error;
3482
3483 ctx = isl_union_map_get_ctx(umap);
3484 space = isl_union_map_get_space(umap);
3485 data.space = isl_multi_pw_aff_get_space(mpa);
3486 data.mpa = mpa;
3487 data.res = isl_union_map_alloc(space, umap->table.n);
3488 data.match = match;
3489 data.fn = fn;
3490 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3491 &data) < 0)
3492 data.res = isl_union_map_free(data.res);
3493
3494 isl_space_free(data.space);
3495 isl_union_map_free(umap);
3496 isl_multi_pw_aff_free(mpa);
3497 return data.res;
3498error:
3499 isl_union_map_free(umap);
3500 isl_multi_pw_aff_free(mpa);
3501 return NULL((void*)0);
3502}
3503
3504/* Compute the preimage of the domain of "umap" under the function
3505 * represented by "mpa".
3506 * In other words, plug in "mpa" in the domain of "umap".
3507 * The result contains maps that live in the same spaces as the maps of "umap"
3508 * with domain space equal to the target space of "mpa",
3509 * except that the domain has been replaced by the domain space of "mpa".
3510 */
3511__isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3512 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3513{
3514 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3515 &isl_map_preimage_domain_multi_pw_aff);
3516}
3517
3518/* Internal data structure for preimage_upma.
3519 *
3520 * "umap" is the map of which the preimage should be computed.
3521 * "res" collects the results.
3522 * "fn" computes the preimage for a given piecewise multi-affine function.
3523 */
3524struct isl_union_map_preimage_upma_data {
3525 isl_union_map *umap;
3526 isl_union_map *res;
3527 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3528 __isl_take isl_pw_multi_aff *pma);
3529};
3530
3531/* Call data->fn to compute the preimage of the domain or range of data->umap
3532 * under the function represented by pma and add the result to data->res.
3533 */
3534static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3535{
3536 struct isl_union_map_preimage_upma_data *data = user;
3537 isl_union_map *umap;
3538
3539 umap = isl_union_map_copy(data->umap);
3540 umap = data->fn(umap, pma);
3541 data->res = isl_union_map_union(data->res, umap);
3542
3543 return data->res ? isl_stat_ok : isl_stat_error;
3544}
3545
3546/* Compute the preimage of the domain or range of "umap" under the function
3547 * represented by "upma".
3548 * In other words, plug in "upma" in the domain or range of "umap".
3549 * The function "fn" performs the actual preimage computation
3550 * on a piecewise multi-affine function.
3551 */
3552static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3553 __isl_take isl_union_map *umap,
3554 __isl_take isl_union_pw_multi_aff *upma,
3555 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3556 __isl_take isl_pw_multi_aff *pma))
3557{
3558 struct isl_union_map_preimage_upma_data data;
3559
3560 data.umap = umap;
3561 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3562 data.fn = fn;
3563 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3564 &preimage_upma, &data) < 0)
3565 data.res = isl_union_map_free(data.res);
3566
3567 isl_union_map_free(umap);
3568 isl_union_pw_multi_aff_free(upma);
3569
3570 return data.res;
3571}
3572
3573/* Compute the preimage of the domain of "umap" under the function
3574 * represented by "upma".
3575 * In other words, plug in "upma" in the domain of "umap".
3576 * The result contains maps that live in the same spaces as the maps of "umap"
3577 * with domain space equal to one of the target spaces of "upma",
3578 * except that the domain has been replaced by one of the the domain spaces that
3579 * corresponds to that target space of "upma".
3580 */
3581__isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3582 __isl_take isl_union_map *umap,
3583 __isl_take isl_union_pw_multi_aff *upma)
3584{
3585 return preimage_union_pw_multi_aff(umap, upma,
3586 &isl_union_map_preimage_domain_pw_multi_aff);
3587}
3588
3589/* Compute the preimage of the range of "umap" under the function
3590 * represented by "upma".
3591 * In other words, plug in "upma" in the range of "umap".
3592 * The result contains maps that live in the same spaces as the maps of "umap"
3593 * with range space equal to one of the target spaces of "upma",
3594 * except that the range has been replaced by one of the the domain spaces that
3595 * corresponds to that target space of "upma".
3596 */
3597__isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3598 __isl_take isl_union_map *umap,
3599 __isl_take isl_union_pw_multi_aff *upma)
3600{
3601 return preimage_union_pw_multi_aff(umap, upma,
3602 &isl_union_map_preimage_range_pw_multi_aff);
3603}
3604
3605/* Compute the preimage of "uset" under the function represented by "upma".
3606 * In other words, plug in "upma" in the range of "uset".
3607 * The result contains sets that live in the same spaces as the sets of "uset"
3608 * with space equal to one of the target spaces of "upma",
3609 * except that the space has been replaced by one of the the domain spaces that
3610 * corresponds to that target space of "upma".
3611 */
3612__isl_give isl_union_setisl_union_map *isl_union_set_preimage_union_pw_multi_aff(
3613 __isl_take isl_union_setisl_union_map *uset,
3614 __isl_take isl_union_pw_multi_aff *upma)
3615{
3616 return preimage_union_pw_multi_aff(uset, upma,
3617 &isl_union_set_preimage_pw_multi_aff);
3618}
3619
3620/* Reset the user pointer on all identifiers of parameters and tuples
3621 * of the space of *entry.
3622 */
3623static isl_stat reset_user(void **entry, void *user)
3624{
3625 isl_map **map = (isl_map **)entry;
3626
3627 *map = isl_map_reset_user(*map);
3628
3629 return *map ? isl_stat_ok : isl_stat_error;
3630}
3631
3632/* Reset the user pointer on all identifiers of parameters and tuples
3633 * of the spaces of "umap".
3634 */
3635__isl_give isl_union_map *isl_union_map_reset_user(
3636 __isl_take isl_union_map *umap)
3637{
3638 umap = isl_union_map_cow(umap);
3639 if (!umap)
3640 return NULL((void*)0);
3641 umap->dim = isl_space_reset_user(umap->dim);
3642 if (!umap->dim)
3643 return isl_union_map_free(umap);
3644 umap = un_op(umap, &reset_user);
3645
3646 return umap;
3647}
3648
3649/* Reset the user pointer on all identifiers of parameters and tuples
3650 * of the spaces of "uset".
3651 */
3652__isl_give isl_union_setisl_union_map *isl_union_set_reset_user(
3653 __isl_take isl_union_setisl_union_map *uset)
3654{
3655 return isl_union_map_reset_user(uset);
3656}
3657
3658/* Internal data structure for isl_union_map_project_out.
3659 * "type", "first" and "n" are the arguments for the isl_map_project_out
3660 * call.
3661 * "res" collects the results.
3662 */
3663struct isl_union_map_project_out_data {
3664 enum isl_dim_type type;
3665 unsigned first;
3666 unsigned n;
3667
3668 isl_union_map *res;
3669};
3670
3671/* Turn the data->n dimensions of type data->type, starting at data->first
3672 * into existentially quantified variables and add the result to data->res.
3673 */
3674static isl_stat project_out(__isl_take isl_map *map, void *user)
3675{
3676 struct isl_union_map_project_out_data *data = user;
3677
3678 map = isl_map_project_out(map, data->type, data->first, data->n);
3679 data->res = isl_union_map_add_map(data->res, map);
3680
3681 return isl_stat_ok;
3682}
3683
3684/* Turn the "n" dimensions of type "type", starting at "first"
3685 * into existentially quantified variables.
3686 * Since the space of an isl_union_map only contains parameters,
3687 * type is required to be equal to isl_dim_param.
3688 */
3689__isl_give isl_union_map *isl_union_map_project_out(
3690 __isl_take isl_union_map *umap,
3691 enum isl_dim_type type, unsigned first, unsigned n)
3692{
3693 isl_space *space;
3694 struct isl_union_map_project_out_data data = { type, first, n };
3695
3696 if (!umap)
3697 return NULL((void*)0);
3698
3699 if (type != isl_dim_param)
3700 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only project out parameters", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_union_map.c"
, 3702); return isl_union_map_free(umap); } while (0)
3701 "can only project out parameters",do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only project out parameters", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_union_map.c"
, 3702); return isl_union_map_free(umap); } while (0)
3702 return isl_union_map_free(umap))do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only project out parameters", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_union_map.c"
, 3702); return isl_union_map_free(umap); } while (0)
;
3703
3704 space = isl_union_map_get_space(umap);
3705 space = isl_space_drop_dims(space, type, first, n);
3706 data.res = isl_union_map_empty(space);
3707 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3708 data.res = isl_union_map_free(data.res);
3709
3710 isl_union_map_free(umap);
3711
3712 return data.res;
3713}
3714
3715/* Turn the "n" dimensions of type "type", starting at "first"
3716 * into existentially quantified variables.
3717 * Since the space of an isl_union_set only contains parameters,
3718 * "type" is required to be equal to isl_dim_param.
3719 */
3720__isl_give isl_union_setisl_union_map *isl_union_set_project_out(
3721 __isl_take isl_union_setisl_union_map *uset,
3722 enum isl_dim_type type, unsigned first, unsigned n)
3723{
3724 return isl_union_map_project_out(uset, type, first, n);
3725}
3726
3727/* Internal data structure for isl_union_map_involves_dims.
3728 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3729 */
3730struct isl_union_map_involves_dims_data {
3731 unsigned first;
3732 unsigned n;
3733};
3734
3735/* Does "map" _not_ involve the data->n parameters starting at data->first?
3736 */
3737static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3738{
3739 struct isl_union_map_involves_dims_data *data = user;
3740 isl_bool involves;
3741
3742 involves = isl_map_involves_dims(map,
3743 isl_dim_param, data->first, data->n);
3744 if (involves < 0)
3745 return isl_bool_error;
3746 return !involves;
3747}
3748
3749/* Does "umap" involve any of the n parameters starting at first?
3750 * "type" is required to be set to isl_dim_param.
3751 *
3752 * "umap" involves any of those parameters if any of its maps
3753 * involve the parameters. In other words, "umap" does not
3754 * involve any of the parameters if all its maps to not
3755 * involve the parameters.
3756 */
3757isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3758 enum isl_dim_type type, unsigned first, unsigned n)
3759{
3760 struct isl_union_map_involves_dims_data data = { first, n };
3761 isl_bool excludes;
3762
3763 if (type != isl_dim_param)
3764 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only reference parameters", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_union_map.c"
, 3765); return isl_bool_error; } while (0)
3765 "can only reference parameters", return isl_bool_error)do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "can only reference parameters", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn298304/tools/polly/lib/External/isl/isl_union_map.c"
, 3765); return isl_bool_error; } while (0)
;
3766
3767 excludes = union_map_forall_user(umap, &map_excludes, &data);
3768
3769 if (excludes < 0)
3770 return isl_bool_error;
3771
3772 return !excludes;
3773}
3774
3775/* Internal data structure for isl_union_map_reset_range_space.
3776 * "range" is the space from which to set the range space.
3777 * "res" collects the results.
3778 */
3779struct isl_union_map_reset_range_space_data {
3780 isl_space *range;
3781 isl_union_map *res;
3782};
3783
3784/* Replace the range space of "map" by the range space of data->range and
3785 * add the result to data->res.
3786 */
3787static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3788{
3789 struct isl_union_map_reset_range_space_data *data = user;
3790 isl_space *space;
3791
3792 space = isl_map_get_space(map);
3793 space = isl_space_domain(space);
3794 space = isl_space_extend_domain_with_range(space,
3795 isl_space_copy(data->range));
3796 map = isl_map_reset_space(map, space);
3797 data->res = isl_union_map_add_map(data->res, map);
3798
3799 return data->res ? isl_stat_ok : isl_stat_error;
3800}
3801
3802/* Replace the range space of all the maps in "umap" by
3803 * the range space of "space".
3804 *
3805 * This assumes that all maps have the same output dimension.
3806 * This function should therefore not be made publicly available.
3807 *
3808 * Since the spaces of the maps change, so do their hash value.
3809 * We therefore need to create a new isl_union_map.
3810 */
3811__isl_give isl_union_map *isl_union_map_reset_range_space(
3812 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3813{
3814 struct isl_union_map_reset_range_space_data data = { space };
3815
3816 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3817 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3818 data.res = isl_union_map_free(data.res);
3819
3820 isl_space_free(space);
3821 isl_union_map_free(umap);
3822 return data.res;
3823}
3824
3825/* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3826 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3827 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3828 * to the domain and the range of each map.
3829 * "res" collects the results.
3830 */
3831struct isl_union_order_at_data {
3832 isl_multi_union_pw_aff *mupa;
3833 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3834 __isl_take isl_multi_pw_aff *mpa2);
3835 isl_union_map *res;
3836};
3837
3838/* Intersect "map" with the result of applying data->order to
3839 * the functions in data->mupa that apply to the domain and the range
3840 * of "map" and add the result to data->res.
3841 */
3842static isl_stat order_at(__isl_take isl_map *map, void *user)
3843{
3844 struct isl_union_order_at_data *data = user;
3845 isl_space *space;
3846 isl_multi_pw_aff *mpa1, *mpa2;
3847 isl_map *order;
3848
3849 space = isl_space_domain(isl_map_get_space(map));
3850 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3851 space = isl_space_range(isl_map_get_space(map));
3852 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3853 order = data->order(mpa1, mpa2);
3854 map = isl_map_intersect(map, order);
3855 data->res = isl_union_map_add_map(data->res, map);
3856
3857 return data->res ? isl_stat_ok : isl_stat_error;
3858}
3859
3860/* Intersect each map in "umap" with the result of calling "order"
3861 * on the functions is "mupa" that apply to the domain and the range
3862 * of the map.
3863 */
3864static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3865 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3866 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3867 __isl_take isl_multi_pw_aff *mpa2))
3868{
3869 struct isl_union_order_at_data data;
3870
3871 umap = isl_union_map_align_params(umap,
3872 isl_multi_union_pw_aff_get_space(mupa));
3873 mupa = isl_multi_union_pw_aff_align_params(mupa,
3874 isl_union_map_get_space(umap));
3875 data.mupa = mupa;
3876 data.order = order;
3877 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3878 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
3879 data.res = isl_union_map_free(data.res);
3880
3881 isl_multi_union_pw_aff_free(mupa);
3882 isl_union_map_free(umap);
3883 return data.res;
3884}
3885
3886/* Return the subset of "umap" where the domain and the range
3887 * have equal "mupa" values.
3888 */
3889__isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
3890 __isl_take isl_union_map *umap,
3891 __isl_take isl_multi_union_pw_aff *mupa)
3892{
3893 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3894 &isl_multi_pw_aff_eq_map);
3895}
3896
3897/* Return the subset of "umap" where the domain has a lexicographically
3898 * smaller "mupa" value than the range.
3899 */
3900__isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
3901 __isl_take isl_union_map *umap,
3902 __isl_take isl_multi_union_pw_aff *mupa)
3903{
3904 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3905 &isl_multi_pw_aff_lex_lt_map);
3906}
3907
3908/* Return the subset of "umap" where the domain has a lexicographically
3909 * greater "mupa" value than the range.
3910 */
3911__isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
3912 __isl_take isl_union_map *umap,
3913 __isl_take isl_multi_union_pw_aff *mupa)
3914{
3915 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3916 &isl_multi_pw_aff_lex_gt_map);
3917}
3918
3919/* Return the union of the elements in the list "list".
3920 */
3921__isl_give isl_union_setisl_union_map *isl_union_set_list_union(
3922 __isl_take isl_union_set_listisl_union_map_list *list)
3923{
3924 int i, n;
3925 isl_ctx *ctx;
3926 isl_space *space;
3927 isl_union_setisl_union_map *res;
3928
3929 if (!list)
3930 return NULL((void*)0);
3931
3932 ctx = isl_union_set_list_get_ctx(list);
3933 space = isl_space_params_alloc(ctx, 0);
3934 res = isl_union_set_empty(space);
3935
3936 n = isl_union_set_list_n_union_set(list);
3937 for (i = 0; i < n; ++i) {
3938 isl_union_setisl_union_map *uset_i;
3939
3940 uset_i = isl_union_set_list_get_union_set(list, i);
3941 res = isl_union_set_union(res, uset_i);
3942 }
3943
3944 isl_union_set_list_free(list);
3945 return res;
3946}
3947
3948/* Update *hash with the hash value of "map".
3949 */
3950static isl_stat add_hash(__isl_take isl_map *map, void *user)
3951{
3952 uint32_t *hash = user;
3953 uint32_t map_hash;
3954
3955 map_hash = isl_map_get_hash(map);
3956 isl_hash_hash(*hash, map_hash)do { do { *hash *= 16777619; *hash ^= (map_hash) & 0xFF; }
while(0); do { *hash *= 16777619; *hash ^= ((map_hash) >>
8) & 0xFF; } while(0); do { *hash *= 16777619; *hash ^= (
(map_hash) >> 16) & 0xFF; } while(0); do { *hash *=
16777619; *hash ^= ((map_hash) >> 24) & 0xFF; } while
(0); } while(0)
;
3957
3958 isl_map_free(map);
3959 return isl_stat_ok;
3960}
3961
3962/* Return a hash value that digests "umap".
3963 */
3964uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
3965{
3966 uint32_t hash;
3967
3968 if (!umap)
3969 return 0;
3970
3971 hash = isl_hash_init()(2166136261u);
3972 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
3973 return 0;
3974
3975 return hash;
3976}
3977
3978/* Return a hash value that digests "uset".
3979 */
3980uint32_t isl_union_set_get_hash(__isl_keep isl_union_setisl_union_map *uset)
3981{
3982 return isl_union_map_get_hash(uset);
3983}