Bug Summary

File:tools/polly/lib/External/isl/isl_union_map.c
Warning:line 873, 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-2017 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~svn306458/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~svn306458/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~svn306458/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~svn306458/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~svn306458/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~svn306458/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~svn306458/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);
1
Calling 'isl_union_set_is_params'
18
Returning; memory was released via 1st parameter
864 p2 = isl_union_set_is_params(uset2);
865 if (p1 < 0 || p2 < 0)
19
Assuming 'p1' is < 0
866 goto error;
20
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);
21
Use of memory after it is freed
874 isl_union_set_free(uset2);
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
1523/* Data structure that specifies how un_op should modify
1524 * the maps in the union map.
1525 *
1526 * If "inplace" is set, then the maps in the input union map
1527 * are modified in place. This means that "fn_map" should not
1528 * change the meaning of the map or that the union map only
1529 * has a single reference.
1530 * If "total" is set, then all maps need to be modified and
1531 * the results need to live in the same space.
1532 * Otherwise, a new union map is constructed to store the results.
1533 * If "filter" is not NULL, then only the input maps that satisfy "filter"
1534 * are taken into account. No filter can be set if "inplace" or
1535 * "total" is set.
1536 * "fn_map" specifies how the maps (selected by "filter")
1537 * should be transformed.
1538 */
1539struct isl_un_op_control {
1540 int inplace;
1541 int total;
1542 isl_bool (*filter)(__isl_keep isl_map *map);
1543 __isl_give isl_map *(*fn_map)(__isl_take isl_map *map);
1544};
1545
1546/* Internal data structure for "un_op".
1547 * "control" specifies how the maps in the union map should be modified.
1548 * "res" collects the results.
1549 */
1550struct isl_union_map_un_data {
1551 struct isl_un_op_control *control;
1552 isl_union_map *res;
1553};
1554
1555/* isl_hash_table_foreach callback for un_op.
1556 * Handle the map that "entry" points to.
1557 *
1558 * If control->filter is set, then check if this map satisfies the filter.
1559 * If so (or if control->filter is not set), modify the map
1560 * by calling control->fn_map and either add the result to data->res or
1561 * replace the original entry by the result (if control->inplace is set).
1562 */
1563static isl_stat un_entry(void **entry, void *user)
1564{
1565 struct isl_union_map_un_data *data = user;
1566 isl_map *map = *entry;
1567
1568 if (data->control->filter) {
1569 isl_bool ok;
1570
1571 ok = data->control->filter(map);
1572 if (ok < 0)
1573 return isl_stat_error;
1574 if (!ok)
1575 return isl_stat_ok;
1576 }
1577
1578 map = data->control->fn_map(isl_map_copy(map));
1579 if (!map)
1580 return isl_stat_error;
1581 if (data->control->inplace) {
1582 isl_map_free(*entry);
1583 *entry = map;
1584 } else {
1585 data->res = isl_union_map_add_map(data->res, map);
1586 if (!data->res)
1587 return isl_stat_error;
1588 }
1589
1590 return isl_stat_ok;
1591}
1592
1593/* Modify the maps in "umap" based on "control".
1594 * If control->inplace is set, then modify the maps in "umap" in-place.
1595 * Otherwise, create a new union map to hold the results.
1596 * If control->total is set, then perform an inplace computation
1597 * if "umap" is only referenced once. Otherwise, create a new union map
1598 * to store the results.
1599 */
1600static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
1601 struct isl_un_op_control *control)
1602{
1603 struct isl_union_map_un_data data = { control };
1604
1605 if (!umap)
1606 return NULL((void*)0);
1607 if ((control->inplace || control->total) && control->filter)
1608 isl_die(isl_union_map_get_ctx(umap), isl_error_invalid,do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "inplace/total modification cannot be filtered", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_union_map.c"
, 1610); return isl_union_map_free(umap); } while (0)
1609 "inplace/total modification cannot be filtered",do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "inplace/total modification cannot be filtered", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_union_map.c"
, 1610); return isl_union_map_free(umap); } while (0)
1610 return isl_union_map_free(umap))do { isl_handle_error(isl_union_map_get_ctx(umap), isl_error_invalid
, "inplace/total modification cannot be filtered", "/tmp/buildd/llvm-toolchain-snapshot-5.0~svn306458/tools/polly/lib/External/isl/isl_union_map.c"
, 1610); return isl_union_map_free(umap); } while (0)
;
1611
1612 if (control->total && umap->ref == 1)
1613 control->inplace = 1;
1614 if (control->inplace) {
1615 data.res = umap;
1616 } else {
1617 isl_space *space;
1618
1619 space = isl_union_map_get_space(umap);
1620 data.res = isl_union_map_alloc(space, umap->table.n);
1621 }
1622 if (isl_hash_table_foreach(isl_union_map_get_ctx(umap),
1623 &umap->table, &un_entry, &data) < 0)
1624 data.res = isl_union_map_free(data.res);
1625
1626 if (control->inplace)
1627 return data.res;
1628 isl_union_map_free(umap);
1629 return data.res;
1630}
1631
1632__isl_give isl_union_map *isl_union_map_from_range(
1633 __isl_take isl_union_setisl_union_map *uset)
1634{
1635 struct isl_un_op_control control = {
1636 .fn_map = &isl_map_from_range,
1637 };
1638 return un_op(uset, &control);
1639}
1640
1641__isl_give isl_union_map *isl_union_map_from_domain(
1642 __isl_take isl_union_setisl_union_map *uset)
1643{
1644 return isl_union_map_reverse(isl_union_map_from_range(uset));
1645}
1646
1647__isl_give isl_union_map *isl_union_map_from_domain_and_range(
1648 __isl_take isl_union_setisl_union_map *domain, __isl_take isl_union_setisl_union_map *range)
1649{
1650 return isl_union_map_apply_range(isl_union_map_from_domain(domain),
1651 isl_union_map_from_range(range));
1652}
1653
1654/* Modify the maps in "umap" by applying "fn" on them.
1655 * "fn" should apply to all maps in "umap" and should not modify the space.
1656 */
1657static __isl_give isl_union_map *total(__isl_take isl_union_map *umap,
1658 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1659{
1660 struct isl_un_op_control control = {
1661 .total = 1,
1662 .fn_map = fn,
1663 };
1664
1665 return un_op(umap, &control);
1666}
1667
1668/* Compute the affine hull of "map" and return the result as an isl_map.
1669 */
1670static __isl_give isl_map *isl_map_affine_hull_map(__isl_take isl_map *map)
1671{
1672 return isl_map_from_basic_map(isl_map_affine_hull(map));
1673}
1674
1675__isl_give isl_union_map *isl_union_map_affine_hull(
1676 __isl_take isl_union_map *umap)
1677{
1678 return total(umap, &isl_map_affine_hull_map);
1679}
1680
1681__isl_give isl_union_setisl_union_map *isl_union_set_affine_hull(
1682 __isl_take isl_union_setisl_union_map *uset)
1683{
1684 return isl_union_map_affine_hull(uset);
1685}
1686
1687/* Compute the polyhedral hull of "map" and return the result as an isl_map.
1688 */
1689static __isl_give isl_map *isl_map_polyhedral_hull_map(__isl_take isl_map *map)
1690{
1691 return isl_map_from_basic_map(isl_map_polyhedral_hull(map));
1692}
1693
1694__isl_give isl_union_map *isl_union_map_polyhedral_hull(
1695 __isl_take isl_union_map *umap)
1696{
1697 return total(umap, &isl_map_polyhedral_hull_map);
1698}
1699
1700__isl_give isl_union_setisl_union_map *isl_union_set_polyhedral_hull(
1701 __isl_take isl_union_setisl_union_map *uset)
1702{
1703 return isl_union_map_polyhedral_hull(uset);
1704}
1705
1706/* Compute a superset of the convex hull of "map" that is described
1707 * by only translates of the constraints in the constituents of "map" and
1708 * return the result as an isl_map.
1709 */
1710static __isl_give isl_map *isl_map_simple_hull_map(__isl_take isl_map *map)
1711{
1712 return isl_map_from_basic_map(isl_map_simple_hull(map));
1713}
1714
1715__isl_give isl_union_map *isl_union_map_simple_hull(
1716 __isl_take isl_union_map *umap)
1717{
1718 return total(umap, &isl_map_simple_hull_map);
1719}
1720
1721__isl_give isl_union_setisl_union_map *isl_union_set_simple_hull(
1722 __isl_take isl_union_setisl_union_map *uset)
1723{
1724 return isl_union_map_simple_hull(uset);
1725}
1726
1727static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1728 __isl_give isl_map *(*fn)(__isl_take isl_map *))
1729{
1730 struct isl_un_op_control control = {
1731 .inplace = 1,
1732 .fn_map = fn,
1733 };
1734
1735 return un_op(umap, &control);
1736}
1737
1738/* Remove redundant constraints in each of the basic maps of "umap".
1739 * Since removing redundant constraints does not change the meaning
1740 * or the space, the operation can be performed in-place.
1741 */
1742__isl_give isl_union_map *isl_union_map_remove_redundancies(
1743 __isl_take isl_union_map *umap)
1744{
1745 return inplace(umap, &isl_map_remove_redundancies);
1746}
1747
1748/* Remove redundant constraints in each of the basic sets of "uset".
1749 */
1750__isl_give isl_union_setisl_union_map *isl_union_set_remove_redundancies(
1751 __isl_take isl_union_setisl_union_map *uset)
1752{
1753 return isl_union_map_remove_redundancies(uset);
1754}
1755
1756__isl_give isl_union_map *isl_union_map_coalesce(
1757 __isl_take isl_union_map *umap)
1758{
1759 return inplace(umap, &isl_map_coalesce);
1760}
1761
1762__isl_give isl_union_setisl_union_map *isl_union_set_coalesce(
1763 __isl_take isl_union_setisl_union_map *uset)
1764{
1765 return isl_union_map_coalesce(uset);
1766}
1767
1768__isl_give isl_union_map *isl_union_map_detect_equalities(
1769 __isl_take isl_union_map *umap)
1770{
1771 return inplace(umap, &isl_map_detect_equalities);
1772}
1773
1774__isl_give isl_union_setisl_union_map *isl_union_set_detect_equalities(
1775 __isl_take isl_union_setisl_union_map *uset)
1776{
1777 return isl_union_map_detect_equalities(uset);
1778}
1779
1780__isl_give isl_union_map *isl_union_map_compute_divs(
1781 __isl_take isl_union_map *umap)
1782{
1783 return inplace(umap, &isl_map_compute_divs);
1784}
1785
1786__isl_give isl_union_setisl_union_map *isl_union_set_compute_divs(
1787 __isl_take isl_union_setisl_union_map *uset)
1788{
1789 return isl_union_map_compute_divs(uset);
1790}
1791
1792__isl_give isl_union_map *isl_union_map_lexmin(
1793 __isl_take isl_union_map *umap)
1794{
1795 return total(umap, &isl_map_lexmin);
1796}
1797
1798__isl_give isl_union_setisl_union_map *isl_union_set_lexmin(
1799 __isl_take isl_union_setisl_union_map *uset)
1800{
1801 return isl_union_map_lexmin(uset);
1802}
1803
1804__isl_give isl_union_map *isl_union_map_lexmax(
1805 __isl_take isl_union_map *umap)
1806{
1807 return total(umap, &isl_map_lexmax);
1808}
1809
1810__isl_give isl_union_setisl_union_map *isl_union_set_lexmax(
1811 __isl_take isl_union_setisl_union_map *uset)
1812{
1813 return isl_union_map_lexmax(uset);
1814}
1815
1816/* Return the universe in the space of "map".
1817 */
1818static __isl_give isl_map *universe(__isl_take isl_map *map)
1819{
1820 isl_space *space;
1821
1822 space = isl_map_get_space(map);
1823 isl_map_free(map);
1824 return isl_map_universe(space);
1825}
1826
1827__isl_give isl_union_map *isl_union_map_universe(__isl_take isl_union_map *umap)
1828{
1829 struct isl_un_op_control control = {
1830 .fn_map = &universe,
1831 };
1832 return un_op(umap, &control);
1833}
1834
1835__isl_give isl_union_setisl_union_map *isl_union_set_universe(__isl_take isl_union_setisl_union_map *uset)
1836{
1837 return isl_union_map_universe(uset);
1838}
1839
1840__isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1841{
1842 struct isl_un_op_control control = {
1843 .fn_map = &isl_map_reverse,
1844 };
1845 return un_op(umap, &control);
1846}
1847
1848/* Compute the parameter domain of the given union map.
1849 */
1850__isl_give isl_setisl_map *isl_union_map_params(__isl_take isl_union_map *umap)
1851{
1852 struct isl_un_op_control control = {
1853 .fn_map = &isl_map_params,
1854 };
1855 int empty;
1856
1857 empty = isl_union_map_is_empty(umap);
1858 if (empty < 0)
1859 goto error;
1860 if (empty) {
1861 isl_space *space;
1862 space = isl_union_map_get_space(umap);
1863 isl_union_map_free(umap);
1864 return isl_set_empty(space);
1865 }
1866 return isl_set_from_union_set(un_op(umap, &control));
1867error:
1868 isl_union_map_free(umap);
1869 return NULL((void*)0);
1870}
1871
1872/* Compute the parameter domain of the given union set.
1873 */
1874__isl_give isl_setisl_map *isl_union_set_params(__isl_take isl_union_setisl_union_map *uset)
1875{
1876 return isl_union_map_params(uset);
1877}
1878
1879__isl_give isl_union_setisl_union_map *isl_union_map_domain(__isl_take isl_union_map *umap)
1880{
1881 struct isl_un_op_control control = {
1882 .fn_map = &isl_map_domain,
1883 };
1884 return un_op(umap, &control);
1885}
1886
1887__isl_give isl_union_setisl_union_map *isl_union_map_range(__isl_take isl_union_map *umap)
1888{
1889 struct isl_un_op_control control = {
1890 .fn_map = &isl_map_range,
1891 };
1892 return un_op(umap, &control);
1893}
1894
1895__isl_give isl_union_map *isl_union_map_domain_map(
1896 __isl_take isl_union_map *umap)
1897{
1898 struct isl_un_op_control control = {
1899 .fn_map = &isl_map_domain_map,
1900 };
1901 return un_op(umap, &control);
1902}
1903
1904/* Construct an isl_pw_multi_aff that maps "map" to its domain and
1905 * add the result to "res".
1906 */
1907static isl_stat domain_map_upma(__isl_take isl_map *map, void *user)
1908{
1909 isl_union_pw_multi_aff **res = user;
1910 isl_multi_aff *ma;
1911 isl_pw_multi_aff *pma;
1912
1913 ma = isl_multi_aff_domain_map(isl_map_get_space(map));
1914 pma = isl_pw_multi_aff_alloc(isl_map_wrap(map), ma);
1915 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
1916
1917 return *res ? isl_stat_ok : isl_stat_error;
1918
1919}
1920
1921/* Return an isl_union_pw_multi_aff that maps a wrapped copy of "umap"
1922 * to its domain.
1923 */
1924__isl_give isl_union_pw_multi_aff *isl_union_map_domain_map_union_pw_multi_aff(
1925 __isl_take isl_union_map *umap)
1926{
1927 isl_union_pw_multi_aff *res;
1928
1929 res = isl_union_pw_multi_aff_empty(isl_union_map_get_space(umap));
1930 if (isl_union_map_foreach_map(umap, &domain_map_upma, &res) < 0)
1931 res = isl_union_pw_multi_aff_free(res);
1932
1933 isl_union_map_free(umap);
1934 return res;
1935}
1936
1937__isl_give isl_union_map *isl_union_map_range_map(
1938 __isl_take isl_union_map *umap)
1939{
1940 struct isl_un_op_control control = {
1941 .fn_map = &isl_map_range_map,
1942 };
1943 return un_op(umap, &control);
1944}
1945
1946/* Given a collection of wrapped maps of the form A[B -> C],
1947 * return the collection of maps A[B -> C] -> B.
1948 */
1949__isl_give isl_union_map *isl_union_set_wrapped_domain_map(
1950 __isl_take isl_union_setisl_union_map *uset)
1951{
1952 struct isl_un_op_control control = {
1953 .filter = &isl_set_is_wrapping,
1954 .fn_map = &isl_set_wrapped_domain_map,
1955 };
1956 return un_op(uset, &control);
1957}
1958
1959/* Does "map" relate elements from the same space?
1960 */
1961static isl_bool equal_tuples(__isl_keep isl_map *map)
1962{
1963 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
1964 map->dim, isl_dim_out);
1965}
1966
1967__isl_give isl_union_setisl_union_map *isl_union_map_deltas(__isl_take isl_union_map *umap)
1968{
1969 struct isl_un_op_control control = {
1970 .filter = &equal_tuples,
1971 .fn_map = &isl_map_deltas,
1972 };
1973 return un_op(umap, &control);
1974}
1975
1976__isl_give isl_union_map *isl_union_map_deltas_map(
1977 __isl_take isl_union_map *umap)
1978{
1979 struct isl_un_op_control control = {
1980 .filter = &equal_tuples,
1981 .fn_map = &isl_map_deltas_map,
1982 };
1983 return un_op(umap, &control);
1984}
1985
1986__isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_setisl_union_map *uset)
1987{
1988 struct isl_un_op_control control = {
1989 .fn_map = &isl_set_identity,
1990 };
1991 return un_op(uset, &control);
1992}
1993
1994/* Construct an identity isl_pw_multi_aff on "set" and add it to *res.
1995 */
1996static isl_stat identity_upma(__isl_take isl_setisl_map *set, void *user)
1997{
1998 isl_union_pw_multi_aff **res = user;
1999 isl_space *space;
2000 isl_pw_multi_aff *pma;
2001
2002 space = isl_space_map_from_set(isl_set_get_space(set));
2003 pma = isl_pw_multi_aff_identity(space);
2004 pma = isl_pw_multi_aff_intersect_domain(pma, set);
2005 *res = isl_union_pw_multi_aff_add_pw_multi_aff(*res, pma);
2006
2007 return *res ? isl_stat_ok : isl_stat_error;
2008}
2009
2010/* Return an identity function on "uset" in the form
2011 * of an isl_union_pw_multi_aff.
2012 */
2013__isl_give isl_union_pw_multi_aff *isl_union_set_identity_union_pw_multi_aff(
2014 __isl_take isl_union_setisl_union_map *uset)
2015{
2016 isl_union_pw_multi_aff *res;
2017
2018 res = isl_union_pw_multi_aff_empty(isl_union_set_get_space(uset));
2019 if (isl_union_set_foreach_set(uset, &identity_upma, &res) < 0)
2020 res = isl_union_pw_multi_aff_free(res);
2021
2022 isl_union_set_free(uset);
2023 return res;
2024}
2025
2026/* For each map in "umap" of the form [A -> B] -> C,
2027 * construct the map A -> C and collect the results.
2028 */
2029__isl_give isl_union_map *isl_union_map_domain_factor_domain(
2030 __isl_take isl_union_map *umap)
2031{
2032 struct isl_un_op_control control = {
2033 .filter = &isl_map_domain_is_wrapping,
2034 .fn_map = &isl_map_domain_factor_domain,
2035 };
2036 return un_op(umap, &control);
2037}
2038
2039/* For each map in "umap" of the form [A -> B] -> C,
2040 * construct the map B -> C and collect the results.
2041 */
2042__isl_give isl_union_map *isl_union_map_domain_factor_range(
2043 __isl_take isl_union_map *umap)
2044{
2045 struct isl_un_op_control control = {
2046 .filter = &isl_map_domain_is_wrapping,
2047 .fn_map = &isl_map_domain_factor_range,
2048 };
2049 return un_op(umap, &control);
2050}
2051
2052/* For each map in "umap" of the form A -> [B -> C],
2053 * construct the map A -> B and collect the results.
2054 */
2055__isl_give isl_union_map *isl_union_map_range_factor_domain(
2056 __isl_take isl_union_map *umap)
2057{
2058 struct isl_un_op_control control = {
2059 .filter = &isl_map_range_is_wrapping,
2060 .fn_map = &isl_map_range_factor_domain,
2061 };
2062 return un_op(umap, &control);
2063}
2064
2065/* For each map in "umap" of the form A -> [B -> C],
2066 * construct the map A -> C and collect the results.
2067 */
2068__isl_give isl_union_map *isl_union_map_range_factor_range(
2069 __isl_take isl_union_map *umap)
2070{
2071 struct isl_un_op_control control = {
2072 .filter = &isl_map_range_is_wrapping,
2073 .fn_map = &isl_map_range_factor_range,
2074 };
2075 return un_op(umap, &control);
2076}
2077
2078/* For each map in "umap" of the form [A -> B] -> [C -> D],
2079 * construct the map A -> C and collect the results.
2080 */
2081__isl_give isl_union_map *isl_union_map_factor_domain(
2082 __isl_take isl_union_map *umap)
2083{
2084 struct isl_un_op_control control = {
2085 .filter = &isl_map_is_product,
2086 .fn_map = &isl_map_factor_domain,
2087 };
2088 return un_op(umap, &control);
2089}
2090
2091/* For each map in "umap" of the form [A -> B] -> [C -> D],
2092 * construct the map B -> D and collect the results.
2093 */
2094__isl_give isl_union_map *isl_union_map_factor_range(
2095 __isl_take isl_union_map *umap)
2096{
2097 struct isl_un_op_control control = {
2098 .filter = &isl_map_is_product,
2099 .fn_map = &isl_map_factor_range,
2100 };
2101 return un_op(umap, &control);
2102}
2103
2104__isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_setisl_union_map *uset)
2105{
2106 struct isl_un_op_control control = {
2107 .filter = &isl_set_is_wrapping,
2108 .fn_map = &isl_set_unwrap,
2109 };
2110 return un_op(uset, &control);
2111}
2112
2113__isl_give isl_union_setisl_union_map *isl_union_map_wrap(__isl_take isl_union_map *umap)
2114{
2115 struct isl_un_op_control control = {
2116 .fn_map = &isl_map_wrap,
2117 };
2118 return un_op(umap, &control);
2119}
2120
2121struct isl_union_map_is_subset_data {
2122 isl_union_map *umap2;
2123 isl_bool is_subset;
2124};
2125
2126static isl_stat is_subset_entry(void **entry, void *user)
2127{
2128 struct isl_union_map_is_subset_data *data = user;
2129 uint32_t hash;
2130 struct isl_hash_table_entry *entry2;
2131 isl_map *map = *entry;
2132
2133 hash = isl_space_get_hash(map->dim);
2134 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2135 hash, &has_dim, map->dim, 0);
2136 if (!entry2) {
2137 int empty = isl_map_is_empty(map);
2138 if (empty < 0)
2139 return isl_stat_error;
2140 if (empty)
2141 return isl_stat_ok;
2142 data->is_subset = 0;
2143 return isl_stat_error;
2144 }
2145
2146 data->is_subset = isl_map_is_subset(map, entry2->data);
2147 if (data->is_subset < 0 || !data->is_subset)
2148 return isl_stat_error;
2149
2150 return isl_stat_ok;
2151}
2152
2153isl_bool isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
2154 __isl_keep isl_union_map *umap2)
2155{
2156 struct isl_union_map_is_subset_data data = { NULL((void*)0), isl_bool_true };
2157
2158 umap1 = isl_union_map_copy(umap1);
2159 umap2 = isl_union_map_copy(umap2);
2160 umap1 = isl_union_map_align_params(umap1, isl_union_map_get_space(umap2));
2161 umap2 = isl_union_map_align_params(umap2, isl_union_map_get_space(umap1));
2162
2163 if (!umap1 || !umap2)
2164 goto error;
2165
2166 data.umap2 = umap2;
2167 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2168 &is_subset_entry, &data) < 0 &&
2169 data.is_subset)
2170 goto error;
2171
2172 isl_union_map_free(umap1);
2173 isl_union_map_free(umap2);
2174
2175 return data.is_subset;
2176error:
2177 isl_union_map_free(umap1);
2178 isl_union_map_free(umap2);
2179 return isl_bool_error;
2180}
2181
2182isl_bool isl_union_set_is_subset(__isl_keep isl_union_setisl_union_map *uset1,
2183 __isl_keep isl_union_setisl_union_map *uset2)
2184{
2185 return isl_union_map_is_subset(uset1, uset2);
2186}
2187
2188isl_bool isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
2189 __isl_keep isl_union_map *umap2)
2190{
2191 isl_bool is_subset;
2192
2193 if (!umap1 || !umap2)
2194 return isl_bool_error;
2195 is_subset = isl_union_map_is_subset(umap1, umap2);
2196 if (is_subset != isl_bool_true)
2197 return is_subset;
2198 is_subset = isl_union_map_is_subset(umap2, umap1);
2199 return is_subset;
2200}
2201
2202isl_bool isl_union_set_is_equal(__isl_keep isl_union_setisl_union_map *uset1,
2203 __isl_keep isl_union_setisl_union_map *uset2)
2204{
2205 return isl_union_map_is_equal(uset1, uset2);
2206}
2207
2208isl_bool isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
2209 __isl_keep isl_union_map *umap2)
2210{
2211 isl_bool is_subset;
2212
2213 if (!umap1 || !umap2)
2214 return isl_bool_error;
2215 is_subset = isl_union_map_is_subset(umap1, umap2);
2216 if (is_subset != isl_bool_true)
2217 return is_subset;
2218 is_subset = isl_union_map_is_subset(umap2, umap1);
2219 if (is_subset == isl_bool_error)
2220 return is_subset;
2221 return !is_subset;
2222}
2223
2224isl_bool isl_union_set_is_strict_subset(__isl_keep isl_union_setisl_union_map *uset1,
2225 __isl_keep isl_union_setisl_union_map *uset2)
2226{
2227 return isl_union_map_is_strict_subset(uset1, uset2);
2228}
2229
2230/* Internal data structure for isl_union_map_is_disjoint.
2231 * umap2 is the union map with which we are comparing.
2232 * is_disjoint is initialized to 1 and is set to 0 as soon
2233 * as the union maps turn out not to be disjoint.
2234 */
2235struct isl_union_map_is_disjoint_data {
2236 isl_union_map *umap2;
2237 isl_bool is_disjoint;
2238};
2239
2240/* Check if "map" is disjoint from data->umap2 and abort
2241 * the search if it is not.
2242 */
2243static isl_stat is_disjoint_entry(void **entry, void *user)
2244{
2245 struct isl_union_map_is_disjoint_data *data = user;
2246 uint32_t hash;
2247 struct isl_hash_table_entry *entry2;
2248 isl_map *map = *entry;
2249
2250 hash = isl_space_get_hash(map->dim);
2251 entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
2252 hash, &has_dim, map->dim, 0);
2253 if (!entry2)
2254 return isl_stat_ok;
2255
2256 data->is_disjoint = isl_map_is_disjoint(map, entry2->data);
2257 if (data->is_disjoint < 0 || !data->is_disjoint)
2258 return isl_stat_error;
2259
2260 return isl_stat_ok;
2261}
2262
2263/* Are "umap1" and "umap2" disjoint?
2264 */
2265isl_bool isl_union_map_is_disjoint(__isl_keep isl_union_map *umap1,
2266 __isl_keep isl_union_map *umap2)
2267{
2268 struct isl_union_map_is_disjoint_data data = { NULL((void*)0), isl_bool_true };
2269
2270 umap1 = isl_union_map_copy(umap1);
2271 umap2 = isl_union_map_copy(umap2);
2272 umap1 = isl_union_map_align_params(umap1,
2273 isl_union_map_get_space(umap2));
2274 umap2 = isl_union_map_align_params(umap2,
2275 isl_union_map_get_space(umap1));
2276
2277 if (!umap1 || !umap2)
2278 goto error;
2279
2280 data.umap2 = umap2;
2281 if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
2282 &is_disjoint_entry, &data) < 0 &&
2283 data.is_disjoint)
2284 goto error;
2285
2286 isl_union_map_free(umap1);
2287 isl_union_map_free(umap2);
2288
2289 return data.is_disjoint;
2290error:
2291 isl_union_map_free(umap1);
2292 isl_union_map_free(umap2);
2293 return isl_bool_error;
2294}
2295
2296/* Are "uset1" and "uset2" disjoint?
2297 */
2298isl_bool isl_union_set_is_disjoint(__isl_keep isl_union_setisl_union_map *uset1,
2299 __isl_keep isl_union_setisl_union_map *uset2)
2300{
2301 return isl_union_map_is_disjoint(uset1, uset2);
2302}
2303
2304static isl_stat sample_entry(void **entry, void *user)
2305{
2306 isl_basic_map **sample = (isl_basic_map **)user;
2307 isl_map *map = *entry;
2308
2309 *sample = isl_map_sample(isl_map_copy(map));
2310 if (!*sample)
2311 return isl_stat_error;
2312 if (!isl_basic_map_plain_is_empty(*sample))
2313 return isl_stat_error;
2314 return isl_stat_ok;
2315}
2316
2317__isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
2318{
2319 isl_basic_map *sample = NULL((void*)0);
2320
2321 if (!umap)
2322 return NULL((void*)0);
2323
2324 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2325 &sample_entry, &sample) < 0 &&
2326 !sample)
2327 goto error;
2328
2329 if (!sample)
2330 sample = isl_basic_map_empty(isl_union_map_get_space(umap));
2331
2332 isl_union_map_free(umap);
2333
2334 return sample;
2335error:
2336 isl_union_map_free(umap);
2337 return NULL((void*)0);
2338}
2339
2340__isl_give isl_basic_setisl_basic_map *isl_union_set_sample(__isl_take isl_union_setisl_union_map *uset)
2341{
2342 return bset_from_bmap(isl_union_map_sample(uset));
2343}
2344
2345/* Return an element in "uset" in the form of an isl_point.
2346 * Return a void isl_point if "uset" is empty.
2347 */
2348__isl_give isl_point *isl_union_set_sample_point(__isl_take isl_union_setisl_union_map *uset)
2349{
2350 return isl_basic_set_sample_point(isl_union_set_sample(uset));
2351}
2352
2353struct isl_forall_data {
2354 isl_bool res;
2355 isl_bool (*fn)(__isl_keep isl_map *map);
2356};
2357
2358static isl_stat forall_entry(void **entry, void *user)
2359{
2360 struct isl_forall_data *data = user;
2361 isl_map *map = *entry;
2362
2363 data->res = data->fn(map);
2364 if (data->res < 0)
2365 return isl_stat_error;
2366
2367 if (!data->res)
2368 return isl_stat_error;
2369
2370 return isl_stat_ok;
2371}
2372
2373static isl_bool union_map_forall(__isl_keep isl_union_map *umap,
2374 isl_bool (*fn)(__isl_keep isl_map *map))
2375{
2376 struct isl_forall_data data = { isl_bool_true, fn };
2377
2378 if (!umap)
2379 return isl_bool_error;
2380
2381 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2382 &forall_entry, &data) < 0 && data.res)
2383 return isl_bool_error;
2384
2385 return data.res;
2386}
2387
2388struct isl_forall_user_data {
2389 isl_bool res;
2390 isl_bool (*fn)(__isl_keep isl_map *map, void *user);
2391 void *user;
2392};
2393
2394static isl_stat forall_user_entry(void **entry, void *user)
2395{
2396 struct isl_forall_user_data *data = user;
2397 isl_map *map = *entry;
2398
2399 data->res = data->fn(map, data->user);
2400 if (data->res < 0)
2401 return isl_stat_error;
2402
2403 if (!data->res)
2404 return isl_stat_error;
2405
2406 return isl_stat_ok;
2407}
2408
2409/* Check if fn(map, user) returns true for all maps "map" in umap.
2410 */
2411static isl_bool union_map_forall_user(__isl_keep isl_union_map *umap,
2412 isl_bool (*fn)(__isl_keep isl_map *map, void *user), void *user)
2413{
2414 struct isl_forall_user_data data = { isl_bool_true, fn, user };
2415
2416 if (!umap)
2417 return isl_bool_error;
2418
2419 if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
2420 &forall_user_entry, &data) < 0 && data.res)
2421 return isl_bool_error;
2422
2423 return data.res;
2424}
2425
2426isl_bool isl_union_map_is_empty(__isl_keep isl_union_map *umap)
2427{
2428 return union_map_forall(umap, &isl_map_is_empty);
2429}
2430
2431isl_bool isl_union_set_is_empty(__isl_keep isl_union_setisl_union_map *uset)
2432{
2433 return isl_union_map_is_empty(uset);
2434}
2435
2436static isl_bool is_subset_of_identity(__isl_keep isl_map *map)
2437{
2438 isl_bool is_subset;
2439 isl_space *dim;
2440 isl_map *id;
2441
2442 if (!map)
2443 return isl_bool_error;
2444
2445 if (!isl_space_tuple_is_equal(map->dim, isl_dim_in,
2446 map->dim, isl_dim_out))
2447 return isl_bool_false;
2448
2449 dim = isl_map_get_space(map);
2450 id = isl_map_identity(dim);
2451
2452 is_subset = isl_map_is_subset(map, id);
2453
2454 isl_map_free(id);
2455
2456 return is_subset;
2457}
2458
2459/* Given an isl_union_map that consists of a single map, check
2460 * if it is single-valued.
2461 */
2462static isl_bool single_map_is_single_valued(__isl_keep isl_union_map *umap)
2463{
2464 isl_map *map;
2465 isl_bool sv;
2466
2467 umap = isl_union_map_copy(umap);
2468 map = isl_map_from_union_map(umap);
2469 sv = isl_map_is_single_valued(map);
2470 isl_map_free(map);
2471
2472 return sv;
2473}
2474
2475/* Internal data structure for single_valued_on_domain.
2476 *
2477 * "umap" is the union map to be tested.
2478 * "sv" is set to 1 as long as "umap" may still be single-valued.
2479 */
2480struct isl_union_map_is_sv_data {
2481 isl_union_map *umap;
2482 isl_bool sv;
2483};
2484
2485/* Check if the data->umap is single-valued on "set".
2486 *
2487 * If data->umap consists of a single map on "set", then test it
2488 * as an isl_map.
2489 *
2490 * Otherwise, compute
2491 *
2492 * M \circ M^-1
2493 *
2494 * check if the result is a subset of the identity mapping and
2495 * store the result in data->sv.
2496 *
2497 * Terminate as soon as data->umap has been determined not to
2498 * be single-valued.
2499 */
2500static isl_stat single_valued_on_domain(__isl_take isl_setisl_map *set, void *user)
2501{
2502 struct isl_union_map_is_sv_data *data = user;
2503 isl_union_map *umap, *test;
2504
2505 umap = isl_union_map_copy(data->umap);
2506 umap = isl_union_map_intersect_domain(umap,
2507 isl_union_set_from_set(set));
2508
2509 if (isl_union_map_n_map(umap) == 1) {
2510 data->sv = single_map_is_single_valued(umap);
2511 isl_union_map_free(umap);
2512 } else {
2513 test = isl_union_map_reverse(isl_union_map_copy(umap));
2514 test = isl_union_map_apply_range(test, umap);
2515
2516 data->sv = union_map_forall(test, &is_subset_of_identity);
2517
2518 isl_union_map_free(test);
2519 }
2520
2521 if (data->sv < 0 || !data->sv)
2522 return isl_stat_error;
2523 return isl_stat_ok;
2524}
2525
2526/* Check if the given map is single-valued.
2527 *
2528 * If the union map consists of a single map, then test it as an isl_map.
2529 * Otherwise, check if the union map is single-valued on each of its
2530 * domain spaces.
2531 */
2532isl_bool isl_union_map_is_single_valued(__isl_keep isl_union_map *umap)
2533{
2534 isl_union_map *universe;
2535 isl_union_setisl_union_map *domain;
2536 struct isl_union_map_is_sv_data data;
2537
2538 if (isl_union_map_n_map(umap) == 1)
2539 return single_map_is_single_valued(umap);
2540
2541 universe = isl_union_map_universe(isl_union_map_copy(umap));
2542 domain = isl_union_map_domain(universe);
2543
2544 data.sv = isl_bool_true;
2545 data.umap = umap;
2546 if (isl_union_set_foreach_set(domain,
2547 &single_valued_on_domain, &data) < 0 && data.sv)
2548 data.sv = isl_bool_error;
2549 isl_union_set_free(domain);
2550
2551 return data.sv;
2552}
2553
2554isl_bool isl_union_map_is_injective(__isl_keep isl_union_map *umap)
2555{
2556 isl_bool in;
2557
2558 umap = isl_union_map_copy(umap);
2559 umap = isl_union_map_reverse(umap);
2560 in = isl_union_map_is_single_valued(umap);
2561 isl_union_map_free(umap);
2562
2563 return in;
2564}
2565
2566/* Is "map" obviously not an identity relation because
2567 * it maps elements from one space to another space?
2568 * Update *non_identity accordingly.
2569 *
2570 * In particular, if the domain and range spaces are the same,
2571 * then the map is not considered to obviously not be an identity relation.
2572 * Otherwise, the map is considered to obviously not be an identity relation
2573 * if it is is non-empty.
2574 *
2575 * If "map" is determined to obviously not be an identity relation,
2576 * then the search is aborted.
2577 */
2578static isl_stat map_plain_is_not_identity(__isl_take isl_map *map, void *user)
2579{
2580 isl_bool *non_identity = user;
2581 isl_bool equal;
2582 isl_space *space;
2583
2584 space = isl_map_get_space(map);
2585 equal = isl_space_tuple_is_equal(space, isl_dim_in, space, isl_dim_out);
2586 if (equal >= 0 && !equal)
2587 *non_identity = isl_bool_not(isl_map_is_empty(map));
2588 else
2589 *non_identity = isl_bool_not(equal);
2590 isl_space_free(space);
2591 isl_map_free(map);
2592
2593 if (*non_identity < 0 || *non_identity)
2594 return isl_stat_error;
2595
2596 return isl_stat_ok;
2597}
2598
2599/* Is "umap" obviously not an identity relation because
2600 * it maps elements from one space to another space?
2601 *
2602 * As soon as a map has been found that maps elements to a different space,
2603 * non_identity is changed and the search is aborted.
2604 */
2605static isl_bool isl_union_map_plain_is_not_identity(
2606 __isl_keep isl_union_map *umap)
2607{
2608 isl_bool non_identity;
2609
2610 non_identity = isl_bool_false;
2611 if (isl_union_map_foreach_map(umap, &map_plain_is_not_identity,
2612 &non_identity) < 0 &&
2613 non_identity == isl_bool_false)
2614 return isl_bool_error;
2615
2616 return non_identity;
2617}
2618
2619/* Does "map" only map elements to themselves?
2620 * Update *identity accordingly.
2621 *
2622 * If "map" is determined not to be an identity relation,
2623 * then the search is aborted.
2624 */
2625static isl_stat map_is_identity(__isl_take isl_map *map, void *user)
2626{
2627 isl_bool *identity = user;
2628
2629 *identity = isl_map_is_identity(map);
2630 isl_map_free(map);
2631
2632 if (*identity < 0 || !*identity)
2633 return isl_stat_error;
2634
2635 return isl_stat_ok;
2636}
2637
2638/* Does "umap" only map elements to themselves?
2639 *
2640 * First check if there are any maps that map elements to different spaces.
2641 * If not, then check that all the maps (between identical spaces)
2642 * are identity relations.
2643 */
2644isl_bool isl_union_map_is_identity(__isl_keep isl_union_map *umap)
2645{
2646 isl_bool non_identity;
2647 isl_bool identity;
2648
2649 non_identity = isl_union_map_plain_is_not_identity(umap);
2650 if (non_identity < 0 || non_identity)
2651 return isl_bool_not(non_identity);
2652
2653 identity = isl_bool_true;
2654 if (isl_union_map_foreach_map(umap, &map_is_identity, &identity) < 0 &&
2655 identity == isl_bool_true)
2656 return isl_bool_error;
2657
2658 return identity;
2659}
2660
2661/* Represents a map that has a fixed value (v) for one of its
2662 * range dimensions.
2663 * The map in this structure is not reference counted, so it
2664 * is only valid while the isl_union_map from which it was
2665 * obtained is still alive.
2666 */
2667struct isl_fixed_map {
2668 isl_int v;
2669 isl_map *map;
2670};
2671
2672static struct isl_fixed_map *alloc_isl_fixed_map_array(isl_ctx *ctx,
2673 int n)
2674{
2675 int i;
2676 struct isl_fixed_map *v;
2677
2678 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)))
;
2679 if (!v)
2680 return NULL((void*)0);
2681 for (i = 0; i < n; ++i)
2682 isl_int_init(v[i].v)isl_sioimath_init((v[i].v));
2683 return v;
2684}
2685
2686static void free_isl_fixed_map_array(struct isl_fixed_map *v, int n)
2687{
2688 int i;
2689
2690 if (!v)
2691 return;
2692 for (i = 0; i < n; ++i)
2693 isl_int_clear(v[i].v)isl_sioimath_clear((v[i].v));
2694 free(v);
2695}
2696
2697/* Compare the "v" field of two isl_fixed_map structs.
2698 */
2699static int qsort_fixed_map_cmp(const void *p1, const void *p2)
2700{
2701 const struct isl_fixed_map *e1 = (const struct isl_fixed_map *) p1;
2702 const struct isl_fixed_map *e2 = (const struct isl_fixed_map *) p2;
2703
2704 return isl_int_cmp(e1->v, e2->v)isl_sioimath_cmp(*(e1->v), *(e2->v));
2705}
2706
2707/* Internal data structure used while checking whether all maps
2708 * in a union_map have a fixed value for a given output dimension.
2709 * v is the list of maps, with the fixed value for the dimension
2710 * n is the number of maps considered so far
2711 * pos is the output dimension under investigation
2712 */
2713struct isl_fixed_dim_data {
2714 struct isl_fixed_map *v;
2715 int n;
2716 int pos;
2717};
2718
2719static isl_bool fixed_at_pos(__isl_keep isl_map *map, void *user)
2720{
2721 struct isl_fixed_dim_data *data = user;
2722
2723 data->v[data->n].map = map;
2724 return isl_map_plain_is_fixed(map, isl_dim_out, data->pos,
2725 &data->v[data->n++].v);
2726}
2727
2728static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2729 int first, int n_range);
2730
2731/* Given a list of the maps, with their fixed values at output dimension "pos",
2732 * check whether the ranges of the maps form an obvious partition.
2733 *
2734 * We first sort the maps according to their fixed values.
2735 * If all maps have a different value, then we know the ranges form
2736 * a partition.
2737 * Otherwise, we collect the maps with the same fixed value and
2738 * check whether each such collection is obviously injective
2739 * based on later dimensions.
2740 */
2741static int separates(struct isl_fixed_map *v, int n,
2742 __isl_take isl_space *dim, int pos, int n_range)
2743{
2744 int i;
2745
2746 if (!v)
2747 goto error;
2748
2749 qsort(v, n, sizeof(*v), &qsort_fixed_map_cmp);
2750
2751 for (i = 0; i + 1 < n; ++i) {
2752 int j, k;
2753 isl_union_map *part;
2754 int injective;
2755
2756 for (j = i + 1; j < n; ++j)
2757 if (isl_int_ne(v[i].v, v[j].v)(isl_sioimath_cmp(*(v[i].v), *(v[j].v)) != 0))
2758 break;
2759
2760 if (j == i + 1)
2761 continue;
2762
2763 part = isl_union_map_alloc(isl_space_copy(dim), j - i);
2764 for (k = i; k < j; ++k)
2765 part = isl_union_map_add_map(part,
2766 isl_map_copy(v[k].map));
2767
2768 injective = plain_injective_on_range(part, pos + 1, n_range);
2769 if (injective < 0)
2770 goto error;
2771 if (!injective)
2772 break;
2773
2774 i = j - 1;
2775 }
2776
2777 isl_space_free(dim);
2778 free_isl_fixed_map_array(v, n);
2779 return i + 1 >= n;
2780error:
2781 isl_space_free(dim);
2782 free_isl_fixed_map_array(v, n);
2783 return -1;
2784}
2785
2786/* Check whether the maps in umap have obviously distinct ranges.
2787 * In particular, check for an output dimension in the range
2788 * [first,n_range) for which all maps have a fixed value
2789 * and then check if these values, possibly along with fixed values
2790 * at later dimensions, entail distinct ranges.
2791 */
2792static isl_bool plain_injective_on_range(__isl_take isl_union_map *umap,
2793 int first, int n_range)
2794{
2795 isl_ctx *ctx;
2796 int n;
2797 struct isl_fixed_dim_data data = { NULL((void*)0) };
2798
2799 ctx = isl_union_map_get_ctx(umap);
2800
2801 n = isl_union_map_n_map(umap);
2802 if (!umap)
2803 goto error;
2804
2805 if (n <= 1) {
2806 isl_union_map_free(umap);
2807 return isl_bool_true;
2808 }
2809
2810 if (first >= n_range) {
2811 isl_union_map_free(umap);
2812 return isl_bool_false;
2813 }
2814
2815 data.v = alloc_isl_fixed_map_array(ctx, n);
2816 if (!data.v)
2817 goto error;
2818
2819 for (data.pos = first; data.pos < n_range; ++data.pos) {
2820 isl_bool fixed;
2821 int injective;
2822 isl_space *dim;
2823
2824 data.n = 0;
2825 fixed = union_map_forall_user(umap, &fixed_at_pos, &data);
2826 if (fixed < 0)
2827 goto error;
2828 if (!fixed)
2829 continue;
2830 dim = isl_union_map_get_space(umap);
2831 injective = separates(data.v, n, dim, data.pos, n_range);
2832 isl_union_map_free(umap);
2833 return injective;
2834 }
2835
2836 free_isl_fixed_map_array(data.v, n);
2837 isl_union_map_free(umap);
2838
2839 return isl_bool_false;
2840error:
2841 free_isl_fixed_map_array(data.v, n);
2842 isl_union_map_free(umap);
2843 return isl_bool_error;
2844}
2845
2846/* Check whether the maps in umap that map to subsets of "ran"
2847 * have obviously distinct ranges.
2848 */
2849static isl_bool plain_injective_on_range_wrap(__isl_keep isl_setisl_map *ran,
2850 void *user)
2851{
2852 isl_union_map *umap = user;
2853
2854 umap = isl_union_map_copy(umap);
2855 umap = isl_union_map_intersect_range(umap,
2856 isl_union_set_from_set(isl_set_copy(ran)));
2857 return plain_injective_on_range(umap, 0, isl_set_dim(ran, isl_dim_set));
2858}
2859
2860/* Check if the given union_map is obviously injective.
2861 *
2862 * In particular, we first check if all individual maps are obviously
2863 * injective and then check if all the ranges of these maps are
2864 * obviously disjoint.
2865 */
2866isl_bool isl_union_map_plain_is_injective(__isl_keep isl_union_map *umap)
2867{
2868 isl_bool in;
2869 isl_union_map *univ;
2870 isl_union_setisl_union_map *ran;
2871
2872 in = union_map_forall(umap, &isl_map_plain_is_injective);
2873 if (in < 0)
2874 return isl_bool_error;
2875 if (!in)
2876 return isl_bool_false;
2877
2878 univ = isl_union_map_universe(isl_union_map_copy(umap));
2879 ran = isl_union_map_range(univ);
2880
2881 in = union_map_forall_user(ran, &plain_injective_on_range_wrap, umap);
2882
2883 isl_union_set_free(ran);
2884
2885 return in;
2886}
2887
2888isl_bool isl_union_map_is_bijective(__isl_keep isl_union_map *umap)
2889{
2890 isl_bool sv;
2891
2892 sv = isl_union_map_is_single_valued(umap);
2893 if (sv < 0 || !sv)
2894 return sv;
2895
2896 return isl_union_map_is_injective(umap);
2897}
2898
2899__isl_give isl_union_map *isl_union_map_zip(__isl_take isl_union_map *umap)
2900{
2901 struct isl_un_op_control control = {
2902 .filter = &isl_map_can_zip,
2903 .fn_map = &isl_map_zip,
2904 };
2905 return un_op(umap, &control);
2906}
2907
2908/* Given a union map, take the maps of the form A -> (B -> C) and
2909 * return the union of the corresponding maps (A -> B) -> C.
2910 */
2911__isl_give isl_union_map *isl_union_map_uncurry(__isl_take isl_union_map *umap)
2912{
2913 struct isl_un_op_control control = {
2914 .filter = &isl_map_can_uncurry,
2915 .fn_map = &isl_map_uncurry,
2916 };
2917 return un_op(umap, &control);
2918}
2919
2920/* Given a union map, take the maps of the form (A -> B) -> C and
2921 * return the union of the corresponding maps A -> (B -> C).
2922 */
2923__isl_give isl_union_map *isl_union_map_curry(__isl_take isl_union_map *umap)
2924{
2925 struct isl_un_op_control control = {
2926 .filter = &isl_map_can_curry,
2927 .fn_map = &isl_map_curry,
2928 };
2929 return un_op(umap, &control);
2930}
2931
2932/* Given a union map, take the maps of the form A -> ((B -> C) -> D) and
2933 * return the union of the corresponding maps A -> (B -> (C -> D)).
2934 */
2935__isl_give isl_union_map *isl_union_map_range_curry(
2936 __isl_take isl_union_map *umap)
2937{
2938 struct isl_un_op_control control = {
2939 .filter = &isl_map_can_range_curry,
2940 .fn_map = &isl_map_range_curry,
2941 };
2942 return un_op(umap, &control);
2943}
2944
2945__isl_give isl_union_setisl_union_map *isl_union_set_lift(__isl_take isl_union_setisl_union_map *uset)
2946{
2947 struct isl_un_op_control control = {
2948 .fn_map = &isl_set_lift,
2949 };
2950 return un_op(uset, &control);
2951}
2952
2953static isl_stat coefficients_entry(void **entry, void *user)
2954{
2955 isl_setisl_map *set = *entry;
2956 isl_union_setisl_union_map **res = user;
2957
2958 set = isl_set_copy(set);
2959 set = isl_set_from_basic_set(isl_set_coefficients(set));
2960 *res = isl_union_set_add_set(*res, set);
2961
2962 return isl_stat_ok;
2963}
2964
2965__isl_give isl_union_setisl_union_map *isl_union_set_coefficients(
2966 __isl_take isl_union_setisl_union_map *uset)
2967{
2968 isl_ctx *ctx;
2969 isl_space *dim;
2970 isl_union_setisl_union_map *res;
2971
2972 if (!uset)
2973 return NULL((void*)0);
2974
2975 ctx = isl_union_set_get_ctx(uset);
2976 dim = isl_space_set_alloc(ctx, 0, 0);
2977 res = isl_union_map_alloc(dim, uset->table.n);
2978 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
2979 &coefficients_entry, &res) < 0)
2980 goto error;
2981
2982 isl_union_set_free(uset);
2983 return res;
2984error:
2985 isl_union_set_free(uset);
2986 isl_union_set_free(res);
2987 return NULL((void*)0);
2988}
2989
2990static isl_stat solutions_entry(void **entry, void *user)
2991{
2992 isl_setisl_map *set = *entry;
2993 isl_union_setisl_union_map **res = user;
2994
2995 set = isl_set_copy(set);
2996 set = isl_set_from_basic_set(isl_set_solutions(set));
2997 if (!*res)
2998 *res = isl_union_set_from_set(set);
2999 else
3000 *res = isl_union_set_add_set(*res, set);
3001
3002 if (!*res)
3003 return isl_stat_error;
3004
3005 return isl_stat_ok;
3006}
3007
3008__isl_give isl_union_setisl_union_map *isl_union_set_solutions(
3009 __isl_take isl_union_setisl_union_map *uset)
3010{
3011 isl_union_setisl_union_map *res = NULL((void*)0);
3012
3013 if (!uset)
3014 return NULL((void*)0);
3015
3016 if (uset->table.n == 0) {
3017 res = isl_union_set_empty(isl_union_set_get_space(uset));
3018 isl_union_set_free(uset);
3019 return res;
3020 }
3021
3022 if (isl_hash_table_foreach(uset->dim->ctx, &uset->table,
3023 &solutions_entry, &res) < 0)
3024 goto error;
3025
3026 isl_union_set_free(uset);
3027 return res;
3028error:
3029 isl_union_set_free(uset);
3030 isl_union_set_free(res);
3031 return NULL((void*)0);
3032}
3033
3034/* Is the domain space of "map" equal to "space"?
3035 */
3036static int domain_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3037{
3038 return isl_space_tuple_is_equal(map->dim, isl_dim_in,
3039 space, isl_dim_out);
3040}
3041
3042/* Is the range space of "map" equal to "space"?
3043 */
3044static int range_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3045{
3046 return isl_space_tuple_is_equal(map->dim, isl_dim_out,
3047 space, isl_dim_out);
3048}
3049
3050/* Is the set space of "map" equal to "space"?
3051 */
3052static int set_match(__isl_keep isl_map *map, __isl_keep isl_space *space)
3053{
3054 return isl_space_tuple_is_equal(map->dim, isl_dim_set,
3055 space, isl_dim_out);
3056}
3057
3058/* Internal data structure for preimage_pw_multi_aff.
3059 *
3060 * "pma" is the function under which the preimage should be taken.
3061 * "space" is the space of "pma".
3062 * "res" collects the results.
3063 * "fn" computes the preimage for a given map.
3064 * "match" returns true if "fn" can be called.
3065 */
3066struct isl_union_map_preimage_data {
3067 isl_space *space;
3068 isl_pw_multi_aff *pma;
3069 isl_union_map *res;
3070 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3071 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3072 __isl_take isl_pw_multi_aff *pma);
3073};
3074
3075/* Call data->fn to compute the preimage of the domain or range of *entry
3076 * under the function represented by data->pma, provided the domain/range
3077 * space of *entry matches the target space of data->pma
3078 * (as given by data->match), and add the result to data->res.
3079 */
3080static isl_stat preimage_entry(void **entry, void *user)
3081{
3082 int m;
3083 isl_map *map = *entry;
3084 struct isl_union_map_preimage_data *data = user;
3085 isl_bool empty;
3086
3087 m = data->match(map, data->space);
3088 if (m < 0)
3089 return isl_stat_error;
3090 if (!m)
3091 return isl_stat_ok;
3092
3093 map = isl_map_copy(map);
3094 map = data->fn(map, isl_pw_multi_aff_copy(data->pma));
3095
3096 empty = isl_map_is_empty(map);
3097 if (empty < 0 || empty) {
3098 isl_map_free(map);
3099 return empty < 0 ? isl_stat_error : isl_stat_ok;
3100 }
3101
3102 data->res = isl_union_map_add_map(data->res, map);
3103
3104 return isl_stat_ok;
3105}
3106
3107/* Compute the preimage of the domain or range of "umap" under the function
3108 * represented by "pma".
3109 * In other words, plug in "pma" in the domain or range of "umap".
3110 * The function "fn" performs the actual preimage computation on a map,
3111 * while "match" determines to which maps the function should be applied.
3112 */
3113static __isl_give isl_union_map *preimage_pw_multi_aff(
3114 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma,
3115 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3116 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3117 __isl_take isl_pw_multi_aff *pma))
3118{
3119 isl_ctx *ctx;
3120 isl_space *space;
3121 struct isl_union_map_preimage_data data;
3122
3123 umap = isl_union_map_align_params(umap,
3124 isl_pw_multi_aff_get_space(pma));
3125 pma = isl_pw_multi_aff_align_params(pma, isl_union_map_get_space(umap));
3126
3127 if (!umap || !pma)
3128 goto error;
3129
3130 ctx = isl_union_map_get_ctx(umap);
3131 space = isl_union_map_get_space(umap);
3132 data.space = isl_pw_multi_aff_get_space(pma);
3133 data.pma = pma;
3134 data.res = isl_union_map_alloc(space, umap->table.n);
3135 data.match = match;
3136 data.fn = fn;
3137 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_entry,
3138 &data) < 0)
3139 data.res = isl_union_map_free(data.res);
3140
3141 isl_space_free(data.space);
3142 isl_union_map_free(umap);
3143 isl_pw_multi_aff_free(pma);
3144 return data.res;
3145error:
3146 isl_union_map_free(umap);
3147 isl_pw_multi_aff_free(pma);
3148 return NULL((void*)0);
3149}
3150
3151/* Compute the preimage of the domain of "umap" under the function
3152 * represented by "pma".
3153 * In other words, plug in "pma" in the domain of "umap".
3154 * The result contains maps that live in the same spaces as the maps of "umap"
3155 * with domain space equal to the target space of "pma",
3156 * except that the domain has been replaced by the domain space of "pma".
3157 */
3158__isl_give isl_union_map *isl_union_map_preimage_domain_pw_multi_aff(
3159 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3160{
3161 return preimage_pw_multi_aff(umap, pma, &domain_match,
3162 &isl_map_preimage_domain_pw_multi_aff);
3163}
3164
3165/* Compute the preimage of the range of "umap" under the function
3166 * represented by "pma".
3167 * In other words, plug in "pma" in the range of "umap".
3168 * The result contains maps that live in the same spaces as the maps of "umap"
3169 * with range space equal to the target space of "pma",
3170 * except that the range has been replaced by the domain space of "pma".
3171 */
3172__isl_give isl_union_map *isl_union_map_preimage_range_pw_multi_aff(
3173 __isl_take isl_union_map *umap, __isl_take isl_pw_multi_aff *pma)
3174{
3175 return preimage_pw_multi_aff(umap, pma, &range_match,
3176 &isl_map_preimage_range_pw_multi_aff);
3177}
3178
3179/* Compute the preimage of "uset" under the function represented by "pma".
3180 * In other words, plug in "pma" in "uset".
3181 * The result contains sets that live in the same spaces as the sets of "uset"
3182 * with space equal to the target space of "pma",
3183 * except that the space has been replaced by the domain space of "pma".
3184 */
3185__isl_give isl_union_setisl_union_map *isl_union_set_preimage_pw_multi_aff(
3186 __isl_take isl_union_setisl_union_map *uset, __isl_take isl_pw_multi_aff *pma)
3187{
3188 return preimage_pw_multi_aff(uset, pma, &set_match,
3189 &isl_set_preimage_pw_multi_aff);
3190}
3191
3192/* Compute the preimage of the domain of "umap" under the function
3193 * represented by "ma".
3194 * In other words, plug in "ma" in the domain of "umap".
3195 * The result contains maps that live in the same spaces as the maps of "umap"
3196 * with domain space equal to the target space of "ma",
3197 * except that the domain has been replaced by the domain space of "ma".
3198 */
3199__isl_give isl_union_map *isl_union_map_preimage_domain_multi_aff(
3200 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3201{
3202 return isl_union_map_preimage_domain_pw_multi_aff(umap,
3203 isl_pw_multi_aff_from_multi_aff(ma));
3204}
3205
3206/* Compute the preimage of the range of "umap" under the function
3207 * represented by "ma".
3208 * In other words, plug in "ma" in the range of "umap".
3209 * The result contains maps that live in the same spaces as the maps of "umap"
3210 * with range space equal to the target space of "ma",
3211 * except that the range has been replaced by the domain space of "ma".
3212 */
3213__isl_give isl_union_map *isl_union_map_preimage_range_multi_aff(
3214 __isl_take isl_union_map *umap, __isl_take isl_multi_aff *ma)
3215{
3216 return isl_union_map_preimage_range_pw_multi_aff(umap,
3217 isl_pw_multi_aff_from_multi_aff(ma));
3218}
3219
3220/* Compute the preimage of "uset" under the function represented by "ma".
3221 * In other words, plug in "ma" in "uset".
3222 * The result contains sets that live in the same spaces as the sets of "uset"
3223 * with space equal to the target space of "ma",
3224 * except that the space has been replaced by the domain space of "ma".
3225 */
3226__isl_give isl_union_map *isl_union_set_preimage_multi_aff(
3227 __isl_take isl_union_setisl_union_map *uset, __isl_take isl_multi_aff *ma)
3228{
3229 return isl_union_set_preimage_pw_multi_aff(uset,
3230 isl_pw_multi_aff_from_multi_aff(ma));
3231}
3232
3233/* Internal data structure for preimage_multi_pw_aff.
3234 *
3235 * "mpa" is the function under which the preimage should be taken.
3236 * "space" is the space of "mpa".
3237 * "res" collects the results.
3238 * "fn" computes the preimage for a given map.
3239 * "match" returns true if "fn" can be called.
3240 */
3241struct isl_union_map_preimage_mpa_data {
3242 isl_space *space;
3243 isl_multi_pw_aff *mpa;
3244 isl_union_map *res;
3245 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space);
3246 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3247 __isl_take isl_multi_pw_aff *mpa);
3248};
3249
3250/* Call data->fn to compute the preimage of the domain or range of *entry
3251 * under the function represented by data->mpa, provided the domain/range
3252 * space of *entry matches the target space of data->mpa
3253 * (as given by data->match), and add the result to data->res.
3254 */
3255static isl_stat preimage_mpa_entry(void **entry, void *user)
3256{
3257 int m;
3258 isl_map *map = *entry;
3259 struct isl_union_map_preimage_mpa_data *data = user;
3260 isl_bool empty;
3261
3262 m = data->match(map, data->space);
3263 if (m < 0)
3264 return isl_stat_error;
3265 if (!m)
3266 return isl_stat_ok;
3267
3268 map = isl_map_copy(map);
3269 map = data->fn(map, isl_multi_pw_aff_copy(data->mpa));
3270
3271 empty = isl_map_is_empty(map);
3272 if (empty < 0 || empty) {
3273 isl_map_free(map);
3274 return empty < 0 ? isl_stat_error : isl_stat_ok;
3275 }
3276
3277 data->res = isl_union_map_add_map(data->res, map);
3278
3279 return isl_stat_ok;
3280}
3281
3282/* Compute the preimage of the domain or range of "umap" under the function
3283 * represented by "mpa".
3284 * In other words, plug in "mpa" in the domain or range of "umap".
3285 * The function "fn" performs the actual preimage computation on a map,
3286 * while "match" determines to which maps the function should be applied.
3287 */
3288static __isl_give isl_union_map *preimage_multi_pw_aff(
3289 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa,
3290 int (*match)(__isl_keep isl_map *map, __isl_keep isl_space *space),
3291 __isl_give isl_map *(*fn)(__isl_take isl_map *map,
3292 __isl_take isl_multi_pw_aff *mpa))
3293{
3294 isl_ctx *ctx;
3295 isl_space *space;
3296 struct isl_union_map_preimage_mpa_data data;
3297
3298 umap = isl_union_map_align_params(umap,
3299 isl_multi_pw_aff_get_space(mpa));
3300 mpa = isl_multi_pw_aff_align_params(mpa, isl_union_map_get_space(umap));
3301
3302 if (!umap || !mpa)
3303 goto error;
3304
3305 ctx = isl_union_map_get_ctx(umap);
3306 space = isl_union_map_get_space(umap);
3307 data.space = isl_multi_pw_aff_get_space(mpa);
3308 data.mpa = mpa;
3309 data.res = isl_union_map_alloc(space, umap->table.n);
3310 data.match = match;
3311 data.fn = fn;
3312 if (isl_hash_table_foreach(ctx, &umap->table, &preimage_mpa_entry,
3313 &data) < 0)
3314 data.res = isl_union_map_free(data.res);
3315
3316 isl_space_free(data.space);
3317 isl_union_map_free(umap);
3318 isl_multi_pw_aff_free(mpa);
3319 return data.res;
3320error:
3321 isl_union_map_free(umap);
3322 isl_multi_pw_aff_free(mpa);
3323 return NULL((void*)0);
3324}
3325
3326/* Compute the preimage of the domain of "umap" under the function
3327 * represented by "mpa".
3328 * In other words, plug in "mpa" in the domain of "umap".
3329 * The result contains maps that live in the same spaces as the maps of "umap"
3330 * with domain space equal to the target space of "mpa",
3331 * except that the domain has been replaced by the domain space of "mpa".
3332 */
3333__isl_give isl_union_map *isl_union_map_preimage_domain_multi_pw_aff(
3334 __isl_take isl_union_map *umap, __isl_take isl_multi_pw_aff *mpa)
3335{
3336 return preimage_multi_pw_aff(umap, mpa, &domain_match,
3337 &isl_map_preimage_domain_multi_pw_aff);
3338}
3339
3340/* Internal data structure for preimage_upma.
3341 *
3342 * "umap" is the map of which the preimage should be computed.
3343 * "res" collects the results.
3344 * "fn" computes the preimage for a given piecewise multi-affine function.
3345 */
3346struct isl_union_map_preimage_upma_data {
3347 isl_union_map *umap;
3348 isl_union_map *res;
3349 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3350 __isl_take isl_pw_multi_aff *pma);
3351};
3352
3353/* Call data->fn to compute the preimage of the domain or range of data->umap
3354 * under the function represented by pma and add the result to data->res.
3355 */
3356static isl_stat preimage_upma(__isl_take isl_pw_multi_aff *pma, void *user)
3357{
3358 struct isl_union_map_preimage_upma_data *data = user;
3359 isl_union_map *umap;
3360
3361 umap = isl_union_map_copy(data->umap);
3362 umap = data->fn(umap, pma);
3363 data->res = isl_union_map_union(data->res, umap);
3364
3365 return data->res ? isl_stat_ok : isl_stat_error;
3366}
3367
3368/* Compute the preimage of the domain or range of "umap" under the function
3369 * represented by "upma".
3370 * In other words, plug in "upma" in the domain or range of "umap".
3371 * The function "fn" performs the actual preimage computation
3372 * on a piecewise multi-affine function.
3373 */
3374static __isl_give isl_union_map *preimage_union_pw_multi_aff(
3375 __isl_take isl_union_map *umap,
3376 __isl_take isl_union_pw_multi_aff *upma,
3377 __isl_give isl_union_map *(*fn)(__isl_take isl_union_map *umap,
3378 __isl_take isl_pw_multi_aff *pma))
3379{
3380 struct isl_union_map_preimage_upma_data data;
3381
3382 data.umap = umap;
3383 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3384 data.fn = fn;
3385 if (isl_union_pw_multi_aff_foreach_pw_multi_aff(upma,
3386 &preimage_upma, &data) < 0)
3387 data.res = isl_union_map_free(data.res);
3388
3389 isl_union_map_free(umap);
3390 isl_union_pw_multi_aff_free(upma);
3391
3392 return data.res;
3393}
3394
3395/* Compute the preimage of the domain of "umap" under the function
3396 * represented by "upma".
3397 * In other words, plug in "upma" in the domain of "umap".
3398 * The result contains maps that live in the same spaces as the maps of "umap"
3399 * with domain space equal to one of the target spaces of "upma",
3400 * except that the domain has been replaced by one of the the domain spaces that
3401 * corresponds to that target space of "upma".
3402 */
3403__isl_give isl_union_map *isl_union_map_preimage_domain_union_pw_multi_aff(
3404 __isl_take isl_union_map *umap,
3405 __isl_take isl_union_pw_multi_aff *upma)
3406{
3407 return preimage_union_pw_multi_aff(umap, upma,
3408 &isl_union_map_preimage_domain_pw_multi_aff);
3409}
3410
3411/* Compute the preimage of the range of "umap" under the function
3412 * represented by "upma".
3413 * In other words, plug in "upma" in the range of "umap".
3414 * The result contains maps that live in the same spaces as the maps of "umap"
3415 * with range space equal to one of the target spaces of "upma",
3416 * except that the range has been replaced by one of the the domain spaces that
3417 * corresponds to that target space of "upma".
3418 */
3419__isl_give isl_union_map *isl_union_map_preimage_range_union_pw_multi_aff(
3420 __isl_take isl_union_map *umap,
3421 __isl_take isl_union_pw_multi_aff *upma)
3422{
3423 return preimage_union_pw_multi_aff(umap, upma,
3424 &isl_union_map_preimage_range_pw_multi_aff);
3425}
3426
3427/* Compute the preimage of "uset" under the function represented by "upma".
3428 * In other words, plug in "upma" in the range of "uset".
3429 * The result contains sets that live in the same spaces as the sets of "uset"
3430 * with space equal to one of the target spaces of "upma",
3431 * except that the space has been replaced by one of the the domain spaces that
3432 * corresponds to that target space of "upma".
3433 */
3434__isl_give isl_union_setisl_union_map *isl_union_set_preimage_union_pw_multi_aff(
3435 __isl_take isl_union_setisl_union_map *uset,
3436 __isl_take isl_union_pw_multi_aff *upma)
3437{
3438 return preimage_union_pw_multi_aff(uset, upma,
3439 &isl_union_set_preimage_pw_multi_aff);
3440}
3441
3442/* Reset the user pointer on all identifiers of parameters and tuples
3443 * of the spaces of "umap".
3444 */
3445__isl_give isl_union_map *isl_union_map_reset_user(
3446 __isl_take isl_union_map *umap)
3447{
3448 umap = isl_union_map_cow(umap);
3449 if (!umap)
3450 return NULL((void*)0);
3451 umap->dim = isl_space_reset_user(umap->dim);
3452 if (!umap->dim)
3453 return isl_union_map_free(umap);
3454 return total(umap, &isl_map_reset_user);
3455}
3456
3457/* Reset the user pointer on all identifiers of parameters and tuples
3458 * of the spaces of "uset".
3459 */
3460__isl_give isl_union_setisl_union_map *isl_union_set_reset_user(
3461 __isl_take isl_union_setisl_union_map *uset)
3462{
3463 return isl_union_map_reset_user(uset);
3464}
3465
3466/* Remove all existentially quantified variables and integer divisions
3467 * from "umap" using Fourier-Motzkin elimination.
3468 */
3469__isl_give isl_union_map *isl_union_map_remove_divs(
3470 __isl_take isl_union_map *umap)
3471{
3472 return total(umap, &isl_map_remove_divs);
3473}
3474
3475/* Remove all existentially quantified variables and integer divisions
3476 * from "uset" using Fourier-Motzkin elimination.
3477 */
3478__isl_give isl_union_setisl_union_map *isl_union_set_remove_divs(
3479 __isl_take isl_union_setisl_union_map *uset)
3480{
3481 return isl_union_map_remove_divs(uset);
3482}
3483
3484/* Internal data structure for isl_union_map_project_out.
3485 * "type", "first" and "n" are the arguments for the isl_map_project_out
3486 * call.
3487 * "res" collects the results.
3488 */
3489struct isl_union_map_project_out_data {
3490 enum isl_dim_type type;
3491 unsigned first;
3492 unsigned n;
3493
3494 isl_union_map *res;
3495};
3496
3497/* Turn the data->n dimensions of type data->type, starting at data->first
3498 * into existentially quantified variables and add the result to data->res.
3499 */
3500static isl_stat project_out(__isl_take isl_map *map, void *user)
3501{
3502 struct isl_union_map_project_out_data *data = user;
3503
3504 map = isl_map_project_out(map, data->type, data->first, data->n);
3505 data->res = isl_union_map_add_map(data->res, map);
3506
3507 return isl_stat_ok;
3508}
3509
3510/* Turn the "n" dimensions of type "type", starting at "first"
3511 * into existentially quantified variables.
3512 * Since the space of an isl_union_map only contains parameters,
3513 * type is required to be equal to isl_dim_param.
3514 */
3515__isl_give isl_union_map *isl_union_map_project_out(
3516 __isl_take isl_union_map *umap,
3517 enum isl_dim_type type, unsigned first, unsigned n)
3518{
3519 isl_space *space;
3520 struct isl_union_map_project_out_data data = { type, first, n };
3521
3522 if (!umap)
3523 return NULL((void*)0);
3524
3525 if (type != isl_dim_param)
3526 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~svn306458/tools/polly/lib/External/isl/isl_union_map.c"
, 3528); return isl_union_map_free(umap); } while (0)
3527 "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~svn306458/tools/polly/lib/External/isl/isl_union_map.c"
, 3528); return isl_union_map_free(umap); } while (0)
3528 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~svn306458/tools/polly/lib/External/isl/isl_union_map.c"
, 3528); return isl_union_map_free(umap); } while (0)
;
3529
3530 space = isl_union_map_get_space(umap);
3531 space = isl_space_drop_dims(space, type, first, n);
3532 data.res = isl_union_map_empty(space);
3533 if (isl_union_map_foreach_map(umap, &project_out, &data) < 0)
3534 data.res = isl_union_map_free(data.res);
3535
3536 isl_union_map_free(umap);
3537
3538 return data.res;
3539}
3540
3541/* Turn the "n" dimensions of type "type", starting at "first"
3542 * into existentially quantified variables.
3543 * Since the space of an isl_union_set only contains parameters,
3544 * "type" is required to be equal to isl_dim_param.
3545 */
3546__isl_give isl_union_setisl_union_map *isl_union_set_project_out(
3547 __isl_take isl_union_setisl_union_map *uset,
3548 enum isl_dim_type type, unsigned first, unsigned n)
3549{
3550 return isl_union_map_project_out(uset, type, first, n);
3551}
3552
3553/* Internal data structure for isl_union_map_involves_dims.
3554 * "first" and "n" are the arguments for the isl_map_involves_dims calls.
3555 */
3556struct isl_union_map_involves_dims_data {
3557 unsigned first;
3558 unsigned n;
3559};
3560
3561/* Does "map" _not_ involve the data->n parameters starting at data->first?
3562 */
3563static isl_bool map_excludes(__isl_keep isl_map *map, void *user)
3564{
3565 struct isl_union_map_involves_dims_data *data = user;
3566 isl_bool involves;
3567
3568 involves = isl_map_involves_dims(map,
3569 isl_dim_param, data->first, data->n);
3570 if (involves < 0)
3571 return isl_bool_error;
3572 return !involves;
3573}
3574
3575/* Does "umap" involve any of the n parameters starting at first?
3576 * "type" is required to be set to isl_dim_param.
3577 *
3578 * "umap" involves any of those parameters if any of its maps
3579 * involve the parameters. In other words, "umap" does not
3580 * involve any of the parameters if all its maps to not
3581 * involve the parameters.
3582 */
3583isl_bool isl_union_map_involves_dims(__isl_keep isl_union_map *umap,
3584 enum isl_dim_type type, unsigned first, unsigned n)
3585{
3586 struct isl_union_map_involves_dims_data data = { first, n };
3587 isl_bool excludes;
3588
3589 if (type != isl_dim_param)
3590 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~svn306458/tools/polly/lib/External/isl/isl_union_map.c"
, 3591); return isl_bool_error; } while (0)
3591 "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~svn306458/tools/polly/lib/External/isl/isl_union_map.c"
, 3591); return isl_bool_error; } while (0)
;
3592
3593 excludes = union_map_forall_user(umap, &map_excludes, &data);
3594
3595 if (excludes < 0)
3596 return isl_bool_error;
3597
3598 return !excludes;
3599}
3600
3601/* Internal data structure for isl_union_map_reset_range_space.
3602 * "range" is the space from which to set the range space.
3603 * "res" collects the results.
3604 */
3605struct isl_union_map_reset_range_space_data {
3606 isl_space *range;
3607 isl_union_map *res;
3608};
3609
3610/* Replace the range space of "map" by the range space of data->range and
3611 * add the result to data->res.
3612 */
3613static isl_stat reset_range_space(__isl_take isl_map *map, void *user)
3614{
3615 struct isl_union_map_reset_range_space_data *data = user;
3616 isl_space *space;
3617
3618 space = isl_map_get_space(map);
3619 space = isl_space_domain(space);
3620 space = isl_space_extend_domain_with_range(space,
3621 isl_space_copy(data->range));
3622 map = isl_map_reset_space(map, space);
3623 data->res = isl_union_map_add_map(data->res, map);
3624
3625 return data->res ? isl_stat_ok : isl_stat_error;
3626}
3627
3628/* Replace the range space of all the maps in "umap" by
3629 * the range space of "space".
3630 *
3631 * This assumes that all maps have the same output dimension.
3632 * This function should therefore not be made publicly available.
3633 *
3634 * Since the spaces of the maps change, so do their hash value.
3635 * We therefore need to create a new isl_union_map.
3636 */
3637__isl_give isl_union_map *isl_union_map_reset_range_space(
3638 __isl_take isl_union_map *umap, __isl_take isl_space *space)
3639{
3640 struct isl_union_map_reset_range_space_data data = { space };
3641
3642 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3643 if (isl_union_map_foreach_map(umap, &reset_range_space, &data) < 0)
3644 data.res = isl_union_map_free(data.res);
3645
3646 isl_space_free(space);
3647 isl_union_map_free(umap);
3648 return data.res;
3649}
3650
3651/* Internal data structure for isl_union_map_order_at_multi_union_pw_aff.
3652 * "mupa" is the function from which the isl_multi_pw_affs are extracted.
3653 * "order" is applied to the extracted isl_multi_pw_affs that correspond
3654 * to the domain and the range of each map.
3655 * "res" collects the results.
3656 */
3657struct isl_union_order_at_data {
3658 isl_multi_union_pw_aff *mupa;
3659 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3660 __isl_take isl_multi_pw_aff *mpa2);
3661 isl_union_map *res;
3662};
3663
3664/* Intersect "map" with the result of applying data->order to
3665 * the functions in data->mupa that apply to the domain and the range
3666 * of "map" and add the result to data->res.
3667 */
3668static isl_stat order_at(__isl_take isl_map *map, void *user)
3669{
3670 struct isl_union_order_at_data *data = user;
3671 isl_space *space;
3672 isl_multi_pw_aff *mpa1, *mpa2;
3673 isl_map *order;
3674
3675 space = isl_space_domain(isl_map_get_space(map));
3676 mpa1 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3677 space = isl_space_range(isl_map_get_space(map));
3678 mpa2 = isl_multi_union_pw_aff_extract_multi_pw_aff(data->mupa, space);
3679 order = data->order(mpa1, mpa2);
3680 map = isl_map_intersect(map, order);
3681 data->res = isl_union_map_add_map(data->res, map);
3682
3683 return data->res ? isl_stat_ok : isl_stat_error;
3684}
3685
3686/* Intersect each map in "umap" with the result of calling "order"
3687 * on the functions is "mupa" that apply to the domain and the range
3688 * of the map.
3689 */
3690static __isl_give isl_union_map *isl_union_map_order_at_multi_union_pw_aff(
3691 __isl_take isl_union_map *umap, __isl_take isl_multi_union_pw_aff *mupa,
3692 __isl_give isl_map *(*order)(__isl_take isl_multi_pw_aff *mpa1,
3693 __isl_take isl_multi_pw_aff *mpa2))
3694{
3695 struct isl_union_order_at_data data;
3696
3697 umap = isl_union_map_align_params(umap,
3698 isl_multi_union_pw_aff_get_space(mupa));
3699 mupa = isl_multi_union_pw_aff_align_params(mupa,
3700 isl_union_map_get_space(umap));
3701 data.mupa = mupa;
3702 data.order = order;
3703 data.res = isl_union_map_empty(isl_union_map_get_space(umap));
3704 if (isl_union_map_foreach_map(umap, &order_at, &data) < 0)
3705 data.res = isl_union_map_free(data.res);
3706
3707 isl_multi_union_pw_aff_free(mupa);
3708 isl_union_map_free(umap);
3709 return data.res;
3710}
3711
3712/* Return the subset of "umap" where the domain and the range
3713 * have equal "mupa" values.
3714 */
3715__isl_give isl_union_map *isl_union_map_eq_at_multi_union_pw_aff(
3716 __isl_take isl_union_map *umap,
3717 __isl_take isl_multi_union_pw_aff *mupa)
3718{
3719 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3720 &isl_multi_pw_aff_eq_map);
3721}
3722
3723/* Return the subset of "umap" where the domain has a lexicographically
3724 * smaller "mupa" value than the range.
3725 */
3726__isl_give isl_union_map *isl_union_map_lex_lt_at_multi_union_pw_aff(
3727 __isl_take isl_union_map *umap,
3728 __isl_take isl_multi_union_pw_aff *mupa)
3729{
3730 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3731 &isl_multi_pw_aff_lex_lt_map);
3732}
3733
3734/* Return the subset of "umap" where the domain has a lexicographically
3735 * greater "mupa" value than the range.
3736 */
3737__isl_give isl_union_map *isl_union_map_lex_gt_at_multi_union_pw_aff(
3738 __isl_take isl_union_map *umap,
3739 __isl_take isl_multi_union_pw_aff *mupa)
3740{
3741 return isl_union_map_order_at_multi_union_pw_aff(umap, mupa,
3742 &isl_multi_pw_aff_lex_gt_map);
3743}
3744
3745/* Return the union of the elements in the list "list".
3746 */
3747__isl_give isl_union_setisl_union_map *isl_union_set_list_union(
3748 __isl_take isl_union_set_listisl_union_map_list *list)
3749{
3750 int i, n;
3751 isl_ctx *ctx;
3752 isl_space *space;
3753 isl_union_setisl_union_map *res;
3754
3755 if (!list)
3756 return NULL((void*)0);
3757
3758 ctx = isl_union_set_list_get_ctx(list);
3759 space = isl_space_params_alloc(ctx, 0);
3760 res = isl_union_set_empty(space);
3761
3762 n = isl_union_set_list_n_union_set(list);
3763 for (i = 0; i < n; ++i) {
3764 isl_union_setisl_union_map *uset_i;
3765
3766 uset_i = isl_union_set_list_get_union_set(list, i);
3767 res = isl_union_set_union(res, uset_i);
3768 }
3769
3770 isl_union_set_list_free(list);
3771 return res;
3772}
3773
3774/* Update *hash with the hash value of "map".
3775 */
3776static isl_stat add_hash(__isl_take isl_map *map, void *user)
3777{
3778 uint32_t *hash = user;
3779 uint32_t map_hash;
3780
3781 map_hash = isl_map_get_hash(map);
3782 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)
;
3783
3784 isl_map_free(map);
3785 return isl_stat_ok;
3786}
3787
3788/* Return a hash value that digests "umap".
3789 */
3790uint32_t isl_union_map_get_hash(__isl_keep isl_union_map *umap)
3791{
3792 uint32_t hash;
3793
3794 if (!umap)
3795 return 0;
3796
3797 hash = isl_hash_init()(2166136261u);
3798 if (isl_union_map_foreach_map(umap, &add_hash, &hash) < 0)
3799 return 0;
3800
3801 return hash;
3802}
3803
3804/* Return a hash value that digests "uset".
3805 */
3806uint32_t isl_union_set_get_hash(__isl_keep isl_union_setisl_union_map *uset)
3807{
3808 return isl_union_map_get_hash(uset);
3809}
3810
3811/* Add the number of basic sets in "set" to "n".
3812 */
3813static isl_stat add_n(__isl_take isl_setisl_map *set, void *user)
3814{
3815 int *n = user;
3816
3817 *n += isl_set_n_basic_set(set);
3818 isl_set_free(set);
3819
3820 return isl_stat_ok;
3821}
3822
3823/* Return the total number of basic sets in "uset".
3824 */
3825int isl_union_set_n_basic_set(__isl_keep isl_union_setisl_union_map *uset)
3826{
3827 int n = 0;
3828
3829 if (isl_union_set_foreach_set(uset, &add_n, &n) < 0)
3830 return -1;
3831
3832 return n;
3833}
3834
3835/* Add the basic sets in "set" to "list".
3836 */
3837static isl_stat add_list(__isl_take isl_setisl_map *set, void *user)
3838{
3839 isl_basic_set_listisl_basic_map_list **list = user;
3840 isl_basic_set_listisl_basic_map_list *list_i;
3841
3842 list_i = isl_set_get_basic_set_list(set);
3843 *list = isl_basic_set_list_concat(*list, list_i);
3844 isl_set_free(set);
3845
3846 if (!*list)
3847 return isl_stat_error;
3848 return isl_stat_ok;
3849}
3850
3851/* Return a list containing all the basic sets in "uset".
3852 *
3853 * First construct a list of the appropriate size and
3854 * then add all the elements.
3855 */
3856__isl_give isl_basic_set_listisl_basic_map_list *isl_union_set_get_basic_set_list(
3857 __isl_keep isl_union_setisl_union_map *uset)
3858{
3859 int n;
3860 isl_ctx *ctx;
3861 isl_basic_set_listisl_basic_map_list *list;
3862
3863 if (!uset)
3864 return NULL((void*)0);
3865 ctx = isl_union_set_get_ctx(uset);
3866 n = isl_union_set_n_basic_set(uset);
3867 if (n < 0)
3868 return NULL((void*)0);
3869 list = isl_basic_set_list_alloc(ctx, n);
3870 if (isl_union_set_foreach_set(uset, &add_list, &list) < 0)
3871 list = isl_basic_set_list_free(list);
3872
3873 return list;
3874}