667fa62c686d4ae1a82b3505312e70e62abe7c23
[platform/upstream/isl.git] / isl_union_map.c
1 /*
2  * Copyright 2010      INRIA Saclay
3  *
4  * Use of this software is governed by the GNU LGPLv2.1 license
5  *
6  * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7  * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
8  * 91893 Orsay, France 
9  */
10
11 #include <isl/ctx.h>
12 #include <isl/hash.h>
13 #include <isl/map.h>
14 #include <isl/set.h>
15 #include <isl_dim_private.h>
16 #include <isl_map_private.h>
17 #include <isl_union_map_private.h>
18 #include <isl/union_set.h>
19
20 static __isl_give isl_union_map *isl_union_map_alloc(__isl_take isl_dim *dim,
21         int size)
22 {
23         isl_union_map *umap;
24
25         if (!dim)
26                 return NULL;
27
28         umap = isl_calloc_type(dim->ctx, isl_union_map);
29         if (!umap)
30                 return NULL;
31
32         umap->ref = 1;
33         umap->dim = dim;
34         if (isl_hash_table_init(dim->ctx, &umap->table, size) < 0)
35                 goto error;
36
37         return umap;
38 error:
39         isl_dim_free(dim);
40         isl_union_map_free(umap);
41         return NULL;
42 }
43
44 __isl_give isl_union_map *isl_union_map_empty(__isl_take isl_dim *dim)
45 {
46         return isl_union_map_alloc(dim, 16);
47 }
48
49 __isl_give isl_union_set *isl_union_set_empty(__isl_take isl_dim *dim)
50 {
51         return isl_union_map_empty(dim);
52 }
53
54 isl_ctx *isl_union_map_get_ctx(__isl_keep isl_union_map *umap)
55 {
56         return umap ? umap->dim->ctx : NULL;
57 }
58
59 isl_ctx *isl_union_set_get_ctx(__isl_keep isl_union_set *uset)
60 {
61         return uset ? uset->dim->ctx : NULL;
62 }
63
64 __isl_give isl_dim *isl_union_map_get_dim(__isl_keep isl_union_map *umap)
65 {
66         if (!umap)
67                 return NULL;
68         return isl_dim_copy(umap->dim);
69 }
70
71 __isl_give isl_dim *isl_union_set_get_dim(__isl_keep isl_union_set *uset)
72 {
73         return isl_union_map_get_dim(uset);
74 }
75
76 static int free_umap_entry(void **entry, void *user)
77 {
78         isl_map *map = *entry;
79         isl_map_free(map);
80         return 0;
81 }
82
83 static int add_map(__isl_take isl_map *map, void *user)
84 {
85         isl_union_map **umap = (isl_union_map **)user;
86
87         *umap = isl_union_map_add_map(*umap, map);
88
89         return 0;
90 }
91
92 __isl_give isl_union_map *isl_union_map_dup(__isl_keep isl_union_map *umap)
93 {
94         isl_union_map *dup;
95
96         if (!umap)
97                 return NULL;
98
99         dup = isl_union_map_empty(isl_dim_copy(umap->dim));
100         if (isl_union_map_foreach_map(umap, &add_map, &dup) < 0)
101                 goto error;
102         return dup;
103 error:
104         isl_union_map_free(dup);
105         return NULL;
106 }
107
108 __isl_give isl_union_map *isl_union_map_cow(__isl_take isl_union_map *umap)
109 {
110         if (!umap)
111                 return NULL;
112
113         if (umap->ref == 1)
114                 return umap;
115         umap->ref--;
116         return isl_union_map_dup(umap);
117 }
118
119 struct isl_union_align {
120         isl_reordering *exp;
121         isl_union_map *res;
122 };
123
124 static int align_entry(void **entry, void *user)
125 {
126         isl_map *map = *entry;
127         isl_reordering *exp;
128         struct isl_union_align *data = user;
129
130         exp = isl_reordering_extend_dim(isl_reordering_copy(data->exp),
131                                     isl_map_get_dim(map));
132
133         data->res = isl_union_map_add_map(data->res,
134                                         isl_map_realign(isl_map_copy(map), exp));
135
136         return 0;
137 }
138
139 /* Align the parameters of umap along those of model.
140  * The result has the parameters of model first, in the same order
141  * as they appear in model, followed by any remaining parameters of
142  * umap that do not appear in model.
143  */
144 __isl_give isl_union_map *isl_union_map_align_params(
145         __isl_take isl_union_map *umap, __isl_take isl_dim *model)
146 {
147         struct isl_union_align data = { NULL, NULL };
148
149         if (!umap || !model)
150                 goto error;
151
152         if (isl_dim_match(umap->dim, isl_dim_param, model, isl_dim_param)) {
153                 isl_dim_free(model);
154                 return umap;
155         }
156
157         data.exp = isl_parameter_alignment_reordering(umap->dim, model);
158         if (!data.exp)
159                 goto error;
160
161         data.res = isl_union_map_alloc(isl_dim_copy(data.exp->dim),
162                                         umap->table.n);
163         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
164                                         &align_entry, &data) < 0)
165                 goto error;
166
167         isl_reordering_free(data.exp);
168         isl_union_map_free(umap);
169         isl_dim_free(model);
170         return data.res;
171 error:
172         isl_reordering_free(data.exp);
173         isl_union_map_free(umap);
174         isl_union_map_free(data.res);
175         isl_dim_free(model);
176         return NULL;
177 }
178
179 __isl_give isl_union_set *isl_union_set_align_params(
180         __isl_take isl_union_set *uset, __isl_take isl_dim *model)
181 {
182         return isl_union_map_align_params(uset, model);
183 }
184
185 __isl_give isl_union_map *isl_union_map_union(__isl_take isl_union_map *umap1,
186         __isl_take isl_union_map *umap2)
187 {
188         umap1 = isl_union_map_align_params(umap1, isl_union_map_get_dim(umap2));
189         umap2 = isl_union_map_align_params(umap2, isl_union_map_get_dim(umap1));
190
191         umap1 = isl_union_map_cow(umap1);
192
193         if (!umap1 || !umap2)
194                 goto error;
195
196         if (isl_union_map_foreach_map(umap2, &add_map, &umap1) < 0)
197                 goto error;
198
199         isl_union_map_free(umap2);
200
201         return umap1;
202 error:
203         isl_union_map_free(umap1);
204         isl_union_map_free(umap2);
205         return NULL;
206 }
207
208 __isl_give isl_union_set *isl_union_set_union(__isl_take isl_union_set *uset1,
209         __isl_take isl_union_set *uset2)
210 {
211         return isl_union_map_union(uset1, uset2);
212 }
213
214 __isl_give isl_union_map *isl_union_map_copy(__isl_keep isl_union_map *umap)
215 {
216         if (!umap)
217                 return NULL;
218
219         umap->ref++;
220         return umap;
221 }
222
223 __isl_give isl_union_set *isl_union_set_copy(__isl_keep isl_union_set *uset)
224 {
225         return isl_union_map_copy(uset);
226 }
227
228 void isl_union_map_free(__isl_take isl_union_map *umap)
229 {
230         if (!umap)
231                 return;
232
233         if (--umap->ref > 0)
234                 return;
235
236         isl_hash_table_foreach(umap->dim->ctx, &umap->table,
237                                &free_umap_entry, NULL);
238         isl_hash_table_clear(&umap->table);
239         isl_dim_free(umap->dim);
240         free(umap);
241 }
242
243 void isl_union_set_free(__isl_take isl_union_set *uset)
244 {
245         isl_union_map_free(uset);
246 }
247
248 static int has_dim(const void *entry, const void *val)
249 {
250         isl_map *map = (isl_map *)entry;
251         isl_dim *dim = (isl_dim *)val;
252
253         return isl_dim_equal(map->dim, dim);
254 }
255
256 __isl_give isl_union_map *isl_union_map_add_map(__isl_take isl_union_map *umap,
257         __isl_take isl_map *map)
258 {
259         uint32_t hash;
260         struct isl_hash_table_entry *entry;
261
262         if (isl_map_fast_is_empty(map)) {
263                 isl_map_free(map);
264                 return umap;
265         }
266
267         umap = isl_union_map_cow(umap);
268
269         if (!map || !umap)
270                 goto error;
271
272         isl_assert(map->ctx, isl_dim_match(map->dim, isl_dim_param, umap->dim,
273                                            isl_dim_param), goto error);
274
275         hash = isl_dim_get_hash(map->dim);
276         entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
277                                     &has_dim, map->dim, 1);
278         if (!entry)
279                 goto error;
280
281         if (!entry->data)
282                 entry->data = map;
283         else {
284                 entry->data = isl_map_union(entry->data, isl_map_copy(map));
285                 if (!entry->data)
286                         goto error;
287                 isl_map_free(map);
288         }
289
290         return umap;
291 error:
292         isl_map_free(map);
293         isl_union_map_free(umap);
294         return NULL;
295 }
296
297 __isl_give isl_union_set *isl_union_set_add_set(__isl_take isl_union_set *uset,
298         __isl_take isl_set *set)
299 {
300         return isl_union_map_add_map(uset, (isl_map *)set);
301 }
302
303 __isl_give isl_union_map *isl_union_map_from_map(__isl_take isl_map *map)
304 {
305         isl_dim *dim;
306         isl_union_map *umap;
307
308         if (!map)
309                 return NULL;
310
311         dim = isl_map_get_dim(map);
312         dim = isl_dim_drop(dim, isl_dim_in, 0, isl_dim_size(dim, isl_dim_in));
313         dim = isl_dim_drop(dim, isl_dim_out, 0, isl_dim_size(dim, isl_dim_out));
314         umap = isl_union_map_empty(dim);
315         umap = isl_union_map_add_map(umap, map);
316
317         return umap;
318 }
319
320 __isl_give isl_union_set *isl_union_set_from_set(__isl_take isl_set *set)
321 {
322         return isl_union_map_from_map((isl_map *)set);
323 }
324
325 struct isl_union_map_foreach_data
326 {
327         int (*fn)(__isl_take isl_map *map, void *user);
328         void *user;
329 };
330
331 static int call_on_copy(void **entry, void *user)
332 {
333         isl_map *map = *entry;
334         struct isl_union_map_foreach_data *data;
335         data = (struct isl_union_map_foreach_data *)user;
336
337         return data->fn(isl_map_copy(map), data->user);
338 }
339
340 int isl_union_map_n_map(__isl_keep isl_union_map *umap)
341 {
342         return umap ? umap->table.n : 0;
343 }
344
345 int isl_union_set_n_set(__isl_keep isl_union_set *uset)
346 {
347         return uset ? uset->table.n : 0;
348 }
349
350 int isl_union_map_foreach_map(__isl_keep isl_union_map *umap,
351         int (*fn)(__isl_take isl_map *map, void *user), void *user)
352 {
353         struct isl_union_map_foreach_data data = { fn, user };
354
355         if (!umap)
356                 return -1;
357
358         return isl_hash_table_foreach(umap->dim->ctx, &umap->table,
359                                       &call_on_copy, &data);
360 }
361
362 __isl_give isl_map *isl_union_map_extract_map(__isl_keep isl_union_map *umap,
363         __isl_take isl_dim *dim)
364 {
365         uint32_t hash;
366         struct isl_hash_table_entry *entry;
367
368         if (!umap || !dim)
369                 goto error;
370
371         hash = isl_dim_get_hash(dim);
372         entry = isl_hash_table_find(umap->dim->ctx, &umap->table, hash,
373                                     &has_dim, dim, 0);
374         if (!entry)
375                 return isl_map_empty(dim);
376         isl_dim_free(dim);
377         return isl_map_copy(entry->data);
378 error:
379         isl_dim_free(dim);
380         return NULL;
381 }
382
383 __isl_give isl_set *isl_union_set_extract_set(__isl_keep isl_union_set *uset,
384         __isl_take isl_dim *dim)
385 {
386         return (isl_set *)isl_union_map_extract_map(uset, dim);
387 }
388
389 int isl_union_set_foreach_set(__isl_keep isl_union_set *uset,
390         int (*fn)(__isl_take isl_set *set, void *user), void *user)
391 {
392         return isl_union_map_foreach_map(uset,
393                 (int(*)(__isl_take isl_map *, void*))fn, user);
394 }
395
396 struct isl_union_set_foreach_point_data {
397         int (*fn)(__isl_take isl_point *pnt, void *user);
398         void *user;
399 };
400
401 static int foreach_point(__isl_take isl_set *set, void *user)
402 {
403         struct isl_union_set_foreach_point_data *data = user;
404         int r;
405
406         r = isl_set_foreach_point(set, data->fn, data->user);
407         isl_set_free(set);
408
409         return r;
410 }
411
412 int isl_union_set_foreach_point(__isl_keep isl_union_set *uset,
413         int (*fn)(__isl_take isl_point *pnt, void *user), void *user)
414 {
415         struct isl_union_set_foreach_point_data data = { fn, user };
416         return isl_union_set_foreach_set(uset, &foreach_point, &data);
417 }
418
419 struct isl_union_map_gen_bin_data {
420         isl_union_map *umap2;
421         isl_union_map *res;
422 };
423
424 static int subtract_entry(void **entry, void *user)
425 {
426         struct isl_union_map_gen_bin_data *data = user;
427         uint32_t hash;
428         struct isl_hash_table_entry *entry2;
429         isl_map *map = *entry;
430
431         hash = isl_dim_get_hash(map->dim);
432         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
433                                      hash, &has_dim, map->dim, 0);
434         map = isl_map_copy(map);
435         if (entry2) {
436                 int empty;
437                 map = isl_map_subtract(map, isl_map_copy(entry2->data));
438
439                 empty = isl_map_is_empty(map);
440                 if (empty < 0) {
441                         isl_map_free(map);
442                         return -1;
443                 }
444                 if (empty) {
445                         isl_map_free(map);
446                         return 0;
447                 }
448         }
449         data->res = isl_union_map_add_map(data->res, map);
450
451         return 0;
452 }
453
454 static __isl_give isl_union_map *gen_bin_op(__isl_take isl_union_map *umap1,
455         __isl_take isl_union_map *umap2, int (*fn)(void **, void *))
456 {
457         struct isl_union_map_gen_bin_data data = { NULL, NULL };
458
459         umap1 = isl_union_map_align_params(umap1, isl_union_map_get_dim(umap2));
460         umap2 = isl_union_map_align_params(umap2, isl_union_map_get_dim(umap1));
461
462         if (!umap1 || !umap2)
463                 goto error;
464
465         data.umap2 = umap2;
466         data.res = isl_union_map_alloc(isl_dim_copy(umap1->dim),
467                                        umap1->table.n);
468         if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
469                                    fn, &data) < 0)
470                 goto error;
471
472         isl_union_map_free(umap1);
473         isl_union_map_free(umap2);
474         return data.res;
475 error:
476         isl_union_map_free(umap1);
477         isl_union_map_free(umap2);
478         isl_union_map_free(data.res);
479         return NULL;
480 }
481
482 __isl_give isl_union_map *isl_union_map_subtract(
483         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
484 {
485         return gen_bin_op(umap1, umap2, &subtract_entry);
486 }
487
488 __isl_give isl_union_set *isl_union_set_subtract(
489         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
490 {
491         return isl_union_map_subtract(uset1, uset2);
492 }
493
494 struct isl_union_map_match_bin_data {
495         isl_union_map *umap2;
496         isl_union_map *res;
497         __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*);
498 };
499
500 static int match_bin_entry(void **entry, void *user)
501 {
502         struct isl_union_map_match_bin_data *data = user;
503         uint32_t hash;
504         struct isl_hash_table_entry *entry2;
505         isl_map *map = *entry;
506         int empty;
507
508         hash = isl_dim_get_hash(map->dim);
509         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
510                                      hash, &has_dim, map->dim, 0);
511         if (!entry2)
512                 return 0;
513
514         map = isl_map_copy(map);
515         map = data->fn(map, isl_map_copy(entry2->data));
516
517         empty = isl_map_is_empty(map);
518         if (empty < 0) {
519                 isl_map_free(map);
520                 return -1;
521         }
522         if (empty) {
523                 isl_map_free(map);
524                 return 0;
525         }
526
527         data->res = isl_union_map_add_map(data->res, map);
528
529         return 0;
530 }
531
532 static __isl_give isl_union_map *match_bin_op(__isl_take isl_union_map *umap1,
533         __isl_take isl_union_map *umap2,
534         __isl_give isl_map *(*fn)(__isl_take isl_map*, __isl_take isl_map*))
535 {
536         struct isl_union_map_match_bin_data data = { NULL, NULL, fn };
537
538         umap1 = isl_union_map_align_params(umap1, isl_union_map_get_dim(umap2));
539         umap2 = isl_union_map_align_params(umap2, isl_union_map_get_dim(umap1));
540
541         if (!umap1 || !umap2)
542                 goto error;
543
544         data.umap2 = umap2;
545         data.res = isl_union_map_alloc(isl_dim_copy(umap1->dim),
546                                        umap1->table.n);
547         if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
548                                    &match_bin_entry, &data) < 0)
549                 goto error;
550
551         isl_union_map_free(umap1);
552         isl_union_map_free(umap2);
553         return data.res;
554 error:
555         isl_union_map_free(umap1);
556         isl_union_map_free(umap2);
557         isl_union_map_free(data.res);
558         return NULL;
559 }
560
561 __isl_give isl_union_map *isl_union_map_intersect(
562         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
563 {
564         return match_bin_op(umap1, umap2, &isl_map_intersect);
565 }
566
567 __isl_give isl_union_set *isl_union_set_intersect(
568         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
569 {
570         return isl_union_map_intersect(uset1, uset2);
571 }
572
573 __isl_give isl_union_map *isl_union_map_gist(__isl_take isl_union_map *umap,
574         __isl_take isl_union_map *context)
575 {
576         return match_bin_op(umap, context, &isl_map_gist);
577 }
578
579 __isl_give isl_union_set *isl_union_set_gist(__isl_take isl_union_set *uset,
580         __isl_take isl_union_set *context)
581 {
582         return isl_union_map_gist(uset, context);
583 }
584
585 static __isl_give isl_map *lex_le_set(__isl_take isl_map *set1,
586         __isl_take isl_map *set2)
587 {
588         return isl_set_lex_le_set((isl_set *)set1, (isl_set *)set2);
589 }
590
591 static __isl_give isl_map *lex_lt_set(__isl_take isl_map *set1,
592         __isl_take isl_map *set2)
593 {
594         return isl_set_lex_lt_set((isl_set *)set1, (isl_set *)set2);
595 }
596
597 __isl_give isl_union_map *isl_union_set_lex_lt_union_set(
598         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
599 {
600         return match_bin_op(uset1, uset2, &lex_lt_set);
601 }
602
603 __isl_give isl_union_map *isl_union_set_lex_le_union_set(
604         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
605 {
606         return match_bin_op(uset1, uset2, &lex_le_set);
607 }
608
609 __isl_give isl_union_map *isl_union_set_lex_gt_union_set(
610         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
611 {
612         return isl_union_map_reverse(isl_union_set_lex_lt_union_set(uset2, uset1));
613 }
614
615 __isl_give isl_union_map *isl_union_set_lex_ge_union_set(
616         __isl_take isl_union_set *uset1, __isl_take isl_union_set *uset2)
617 {
618         return isl_union_map_reverse(isl_union_set_lex_le_union_set(uset2, uset1));
619 }
620
621 __isl_give isl_union_map *isl_union_map_lex_gt_union_map(
622         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
623 {
624         return isl_union_map_reverse(isl_union_map_lex_lt_union_map(umap2, umap1));
625 }
626
627 __isl_give isl_union_map *isl_union_map_lex_ge_union_map(
628         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
629 {
630         return isl_union_map_reverse(isl_union_map_lex_le_union_map(umap2, umap1));
631 }
632
633 static int intersect_domain_entry(void **entry, void *user)
634 {
635         struct isl_union_map_gen_bin_data *data = user;
636         uint32_t hash;
637         struct isl_hash_table_entry *entry2;
638         isl_dim *dim;
639         isl_map *map = *entry;
640         int empty;
641
642         dim = isl_map_get_dim(map);
643         dim = isl_dim_domain(dim);
644         hash = isl_dim_get_hash(dim);
645         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
646                                      hash, &has_dim, dim, 0);
647         isl_dim_free(dim);
648         if (!entry2)
649                 return 0;
650
651         map = isl_map_copy(map);
652         map = isl_map_intersect_domain(map, isl_set_copy(entry2->data));
653
654         empty = isl_map_is_empty(map);
655         if (empty < 0) {
656                 isl_map_free(map);
657                 return -1;
658         }
659         if (empty) {
660                 isl_map_free(map);
661                 return 0;
662         }
663
664         data->res = isl_union_map_add_map(data->res, map);
665
666         return 0;
667 }
668
669 __isl_give isl_union_map *isl_union_map_intersect_domain(
670         __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
671 {
672         return gen_bin_op(umap, uset, &intersect_domain_entry);
673 }
674
675 static int intersect_range_entry(void **entry, void *user)
676 {
677         struct isl_union_map_gen_bin_data *data = user;
678         uint32_t hash;
679         struct isl_hash_table_entry *entry2;
680         isl_dim *dim;
681         isl_map *map = *entry;
682         int empty;
683
684         dim = isl_map_get_dim(map);
685         dim = isl_dim_range(dim);
686         hash = isl_dim_get_hash(dim);
687         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
688                                      hash, &has_dim, dim, 0);
689         isl_dim_free(dim);
690         if (!entry2)
691                 return 0;
692
693         map = isl_map_copy(map);
694         map = isl_map_intersect_range(map, isl_set_copy(entry2->data));
695
696         empty = isl_map_is_empty(map);
697         if (empty < 0) {
698                 isl_map_free(map);
699                 return -1;
700         }
701         if (empty) {
702                 isl_map_free(map);
703                 return 0;
704         }
705
706         data->res = isl_union_map_add_map(data->res, map);
707
708         return 0;
709 }
710
711 __isl_give isl_union_map *isl_union_map_intersect_range(
712         __isl_take isl_union_map *umap, __isl_take isl_union_set *uset)
713 {
714         return gen_bin_op(umap, uset, &intersect_range_entry);
715 }
716
717 struct isl_union_map_bin_data {
718         isl_union_map *umap2;
719         isl_union_map *res;
720         isl_map *map;
721         int (*fn)(void **entry, void *user);
722 };
723
724 static int apply_range_entry(void **entry, void *user)
725 {
726         struct isl_union_map_bin_data *data = user;
727         isl_map *map2 = *entry;
728         int empty;
729
730         if (!isl_dim_tuple_match(data->map->dim, isl_dim_out,
731                                  map2->dim, isl_dim_in))
732                 return 0;
733
734         map2 = isl_map_apply_range(isl_map_copy(data->map), isl_map_copy(map2));
735
736         empty = isl_map_is_empty(map2);
737         if (empty < 0) {
738                 isl_map_free(map2);
739                 return -1;
740         }
741         if (empty) {
742                 isl_map_free(map2);
743                 return 0;
744         }
745
746         data->res = isl_union_map_add_map(data->res, map2);
747
748         return 0;
749 }
750
751 static int bin_entry(void **entry, void *user)
752 {
753         struct isl_union_map_bin_data *data = user;
754         isl_map *map = *entry;
755
756         data->map = map;
757         if (isl_hash_table_foreach(data->umap2->dim->ctx, &data->umap2->table,
758                                    data->fn, data) < 0)
759                 return -1;
760
761         return 0;
762 }
763
764 static __isl_give isl_union_map *bin_op(__isl_take isl_union_map *umap1,
765         __isl_take isl_union_map *umap2, int (*fn)(void **entry, void *user))
766 {
767         struct isl_union_map_bin_data data = { NULL, NULL, NULL, fn };
768
769         umap1 = isl_union_map_align_params(umap1, isl_union_map_get_dim(umap2));
770         umap2 = isl_union_map_align_params(umap2, isl_union_map_get_dim(umap1));
771
772         if (!umap1 || !umap2)
773                 goto error;
774
775         data.umap2 = umap2;
776         data.res = isl_union_map_alloc(isl_dim_copy(umap1->dim),
777                                        umap1->table.n);
778         if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
779                                    &bin_entry, &data) < 0)
780                 goto error;
781
782         isl_union_map_free(umap1);
783         isl_union_map_free(umap2);
784         return data.res;
785 error:
786         isl_union_map_free(umap1);
787         isl_union_map_free(umap2);
788         isl_union_map_free(data.res);
789         return NULL;
790 }
791
792 __isl_give isl_union_map *isl_union_map_apply_range(
793         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
794 {
795         return bin_op(umap1, umap2, &apply_range_entry);
796 }
797
798 __isl_give isl_union_map *isl_union_map_apply_domain(
799         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
800 {
801         umap1 = isl_union_map_reverse(umap1);
802         umap1 = isl_union_map_apply_range(umap1, umap2);
803         return isl_union_map_reverse(umap1);
804 }
805
806 __isl_give isl_union_set *isl_union_set_apply(
807         __isl_take isl_union_set *uset, __isl_take isl_union_map *umap)
808 {
809         return isl_union_map_apply_range(uset, umap);
810 }
811
812 static int map_lex_lt_entry(void **entry, void *user)
813 {
814         struct isl_union_map_bin_data *data = user;
815         isl_map *map2 = *entry;
816
817         if (!isl_dim_tuple_match(data->map->dim, isl_dim_out,
818                                  map2->dim, isl_dim_out))
819                 return 0;
820
821         map2 = isl_map_lex_lt_map(isl_map_copy(data->map), isl_map_copy(map2));
822
823         data->res = isl_union_map_add_map(data->res, map2);
824
825         return 0;
826 }
827
828 __isl_give isl_union_map *isl_union_map_lex_lt_union_map(
829         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
830 {
831         return bin_op(umap1, umap2, &map_lex_lt_entry);
832 }
833
834 static int map_lex_le_entry(void **entry, void *user)
835 {
836         struct isl_union_map_bin_data *data = user;
837         isl_map *map2 = *entry;
838
839         if (!isl_dim_tuple_match(data->map->dim, isl_dim_out,
840                                  map2->dim, isl_dim_out))
841                 return 0;
842
843         map2 = isl_map_lex_le_map(isl_map_copy(data->map), isl_map_copy(map2));
844
845         data->res = isl_union_map_add_map(data->res, map2);
846
847         return 0;
848 }
849
850 __isl_give isl_union_map *isl_union_map_lex_le_union_map(
851         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
852 {
853         return bin_op(umap1, umap2, &map_lex_le_entry);
854 }
855
856 static int product_entry(void **entry, void *user)
857 {
858         struct isl_union_map_bin_data *data = user;
859         isl_map *map2 = *entry;
860
861         map2 = isl_map_product(isl_map_copy(data->map), isl_map_copy(map2));
862
863         data->res = isl_union_map_add_map(data->res, map2);
864
865         return 0;
866 }
867
868 __isl_give isl_union_map *isl_union_map_product(__isl_take isl_union_map *umap1,
869         __isl_take isl_union_map *umap2)
870 {
871         return bin_op(umap1, umap2, &product_entry);
872 }
873
874 __isl_give isl_union_set *isl_union_set_product(__isl_take isl_union_set *uset1,
875         __isl_take isl_union_set *uset2)
876 {
877         return isl_union_map_product(uset1, uset2);
878 }
879
880 static int range_product_entry(void **entry, void *user)
881 {
882         struct isl_union_map_bin_data *data = user;
883         isl_map *map2 = *entry;
884
885         map2 = isl_map_range_product(isl_map_copy(data->map),
886                                      isl_map_copy(map2));
887
888         data->res = isl_union_map_add_map(data->res, map2);
889
890         return 0;
891 }
892
893 __isl_give isl_union_map *isl_union_map_range_product(
894         __isl_take isl_union_map *umap1, __isl_take isl_union_map *umap2)
895 {
896         return bin_op(umap1, umap2, &range_product_entry);
897 }
898
899 __isl_give isl_union_map *isl_union_map_from_range(
900         __isl_take isl_union_set *uset)
901 {
902         return uset;
903 }
904
905 __isl_give isl_union_map *isl_union_map_from_domain(
906         __isl_take isl_union_set *uset)
907 {
908         return isl_union_map_reverse(isl_union_map_from_range(uset));
909 }
910
911 __isl_give isl_union_map *isl_union_map_from_domain_and_range(
912         __isl_take isl_union_set *domain, __isl_take isl_union_set *range)
913 {
914         return isl_union_map_apply_range(isl_union_map_from_domain(domain),
915                                          isl_union_map_from_range(range));
916 }
917
918 static __isl_give isl_union_map *un_op(__isl_take isl_union_map *umap,
919         int (*fn)(void **, void *))
920 {
921         umap = isl_union_map_cow(umap);
922         if (!umap)
923                 return NULL;
924
925         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, NULL) < 0)
926                 goto error;
927
928         return umap;
929 error:
930         isl_union_map_free(umap);
931         return NULL;
932 }
933
934 static int affine_entry(void **entry, void *user)
935 {
936         isl_map **map = (isl_map **)entry;
937
938         *map = isl_map_from_basic_map(isl_map_affine_hull(*map));
939
940         return *map ? 0 : -1;
941 }
942
943 __isl_give isl_union_map *isl_union_map_affine_hull(
944         __isl_take isl_union_map *umap)
945 {
946         return un_op(umap, &affine_entry);
947 }
948
949 __isl_give isl_union_set *isl_union_set_affine_hull(
950         __isl_take isl_union_set *uset)
951 {
952         return isl_union_map_affine_hull(uset);
953 }
954
955 static int polyhedral_entry(void **entry, void *user)
956 {
957         isl_map **map = (isl_map **)entry;
958
959         *map = isl_map_from_basic_map(isl_map_polyhedral_hull(*map));
960
961         return *map ? 0 : -1;
962 }
963
964 __isl_give isl_union_map *isl_union_map_polyhedral_hull(
965         __isl_take isl_union_map *umap)
966 {
967         return un_op(umap, &polyhedral_entry);
968 }
969
970 __isl_give isl_union_set *isl_union_set_polyhedral_hull(
971         __isl_take isl_union_set *uset)
972 {
973         return isl_union_map_polyhedral_hull(uset);
974 }
975
976 static int simple_entry(void **entry, void *user)
977 {
978         isl_map **map = (isl_map **)entry;
979
980         *map = isl_map_from_basic_map(isl_map_simple_hull(*map));
981
982         return *map ? 0 : -1;
983 }
984
985 __isl_give isl_union_map *isl_union_map_simple_hull(
986         __isl_take isl_union_map *umap)
987 {
988         return un_op(umap, &simple_entry);
989 }
990
991 __isl_give isl_union_set *isl_union_set_simple_hull(
992         __isl_take isl_union_set *uset)
993 {
994         return isl_union_map_simple_hull(uset);
995 }
996
997 static int inplace_entry(void **entry, void *user)
998 {
999         __isl_give isl_map *(*fn)(__isl_take isl_map *) = user;
1000         isl_map **map = (isl_map **)entry;
1001         isl_map *copy;
1002
1003         copy = fn(isl_map_copy(*map));
1004         if (!copy)
1005                 return -1;
1006
1007         isl_map_free(*map);
1008         *map = copy;
1009
1010         return 0;
1011 }
1012
1013 static __isl_give isl_union_map *inplace(__isl_take isl_union_map *umap,
1014         __isl_give isl_map *(*fn)(__isl_take isl_map *))
1015 {
1016         if (!umap)
1017                 return NULL;
1018
1019         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1020                                     &inplace_entry, fn) < 0)
1021                 goto error;
1022
1023         return umap;
1024 error:
1025         isl_union_map_free(umap);
1026         return NULL;
1027 }
1028
1029 __isl_give isl_union_map *isl_union_map_coalesce(
1030         __isl_take isl_union_map *umap)
1031 {
1032         return inplace(umap, &isl_map_coalesce);
1033 }
1034
1035 __isl_give isl_union_set *isl_union_set_coalesce(
1036         __isl_take isl_union_set *uset)
1037 {
1038         return isl_union_map_coalesce(uset);
1039 }
1040
1041 __isl_give isl_union_map *isl_union_map_detect_equalities(
1042         __isl_take isl_union_map *umap)
1043 {
1044         return inplace(umap, &isl_map_detect_equalities);
1045 }
1046
1047 __isl_give isl_union_set *isl_union_set_detect_equalities(
1048         __isl_take isl_union_set *uset)
1049 {
1050         return isl_union_map_detect_equalities(uset);
1051 }
1052
1053 __isl_give isl_union_map *isl_union_map_compute_divs(
1054         __isl_take isl_union_map *umap)
1055 {
1056         return inplace(umap, &isl_map_compute_divs);
1057 }
1058
1059 __isl_give isl_union_set *isl_union_set_compute_divs(
1060         __isl_take isl_union_set *uset)
1061 {
1062         return isl_union_map_compute_divs(uset);
1063 }
1064
1065 static int lexmin_entry(void **entry, void *user)
1066 {
1067         isl_map **map = (isl_map **)entry;
1068
1069         *map = isl_map_lexmin(*map);
1070
1071         return *map ? 0 : -1;
1072 }
1073
1074 __isl_give isl_union_map *isl_union_map_lexmin(
1075         __isl_take isl_union_map *umap)
1076 {
1077         return un_op(umap, &lexmin_entry);
1078 }
1079
1080 __isl_give isl_union_set *isl_union_set_lexmin(
1081         __isl_take isl_union_set *uset)
1082 {
1083         return isl_union_map_lexmin(uset);
1084 }
1085
1086 static int lexmax_entry(void **entry, void *user)
1087 {
1088         isl_map **map = (isl_map **)entry;
1089
1090         *map = isl_map_lexmax(*map);
1091
1092         return *map ? 0 : -1;
1093 }
1094
1095 __isl_give isl_union_map *isl_union_map_lexmax(
1096         __isl_take isl_union_map *umap)
1097 {
1098         return un_op(umap, &lexmax_entry);
1099 }
1100
1101 __isl_give isl_union_set *isl_union_set_lexmax(
1102         __isl_take isl_union_set *uset)
1103 {
1104         return isl_union_map_lexmax(uset);
1105 }
1106
1107 static __isl_give isl_union_set *cond_un_op(__isl_take isl_union_map *umap,
1108         int (*fn)(void **, void *))
1109 {
1110         isl_union_set *res;
1111
1112         if (!umap)
1113                 return NULL;
1114
1115         res = isl_union_map_alloc(isl_dim_copy(umap->dim), umap->table.n);
1116         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table, fn, &res) < 0)
1117                 goto error;
1118
1119         isl_union_map_free(umap);
1120         return res;
1121 error:
1122         isl_union_map_free(umap);
1123         isl_union_set_free(res);
1124         return NULL;
1125 }
1126
1127 static int reverse_entry(void **entry, void *user)
1128 {
1129         isl_map *map = *entry;
1130         isl_union_map **res = user;
1131
1132         *res = isl_union_map_add_map(*res, isl_map_reverse(isl_map_copy(map)));
1133
1134         return 0;
1135 }
1136
1137 __isl_give isl_union_map *isl_union_map_reverse(__isl_take isl_union_map *umap)
1138 {
1139         return cond_un_op(umap, &reverse_entry);
1140 }
1141
1142 static int domain_entry(void **entry, void *user)
1143 {
1144         isl_map *map = *entry;
1145         isl_union_set **res = user;
1146
1147         *res = isl_union_set_add_set(*res, isl_map_domain(isl_map_copy(map)));
1148
1149         return 0;
1150 }
1151
1152 __isl_give isl_union_set *isl_union_map_domain(__isl_take isl_union_map *umap)
1153 {
1154         return cond_un_op(umap, &domain_entry);
1155 }
1156
1157 static int range_entry(void **entry, void *user)
1158 {
1159         isl_map *map = *entry;
1160         isl_union_set **res = user;
1161
1162         *res = isl_union_set_add_set(*res, isl_map_range(isl_map_copy(map)));
1163
1164         return 0;
1165 }
1166
1167 __isl_give isl_union_set *isl_union_map_range(__isl_take isl_union_map *umap)
1168 {
1169         return cond_un_op(umap, &range_entry);
1170 }
1171
1172 static int domain_map_entry(void **entry, void *user)
1173 {
1174         isl_map *map = *entry;
1175         isl_union_set **res = user;
1176
1177         *res = isl_union_map_add_map(*res,
1178                                         isl_map_domain_map(isl_map_copy(map)));
1179
1180         return 0;
1181 }
1182
1183 __isl_give isl_union_map *isl_union_map_domain_map(
1184         __isl_take isl_union_map *umap)
1185 {
1186         return cond_un_op(umap, &domain_map_entry);
1187 }
1188
1189 static int range_map_entry(void **entry, void *user)
1190 {
1191         isl_map *map = *entry;
1192         isl_union_set **res = user;
1193
1194         *res = isl_union_map_add_map(*res,
1195                                         isl_map_range_map(isl_map_copy(map)));
1196
1197         return 0;
1198 }
1199
1200 __isl_give isl_union_map *isl_union_map_range_map(
1201         __isl_take isl_union_map *umap)
1202 {
1203         return cond_un_op(umap, &range_map_entry);
1204 }
1205
1206 static int deltas_entry(void **entry, void *user)
1207 {
1208         isl_map *map = *entry;
1209         isl_union_set **res = user;
1210
1211         if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
1212                 return 0;
1213
1214         *res = isl_union_set_add_set(*res, isl_map_deltas(isl_map_copy(map)));
1215
1216         return 0;
1217 }
1218
1219 __isl_give isl_union_set *isl_union_map_deltas(__isl_take isl_union_map *umap)
1220 {
1221         return cond_un_op(umap, &deltas_entry);
1222 }
1223
1224 static int identity_entry(void **entry, void *user)
1225 {
1226         isl_set *set = *entry;
1227         isl_union_map **res = user;
1228
1229         *res = isl_union_map_add_map(*res, isl_set_identity(isl_set_copy(set)));
1230
1231         return 0;
1232 }
1233
1234 __isl_give isl_union_map *isl_union_set_identity(__isl_take isl_union_set *uset)
1235 {
1236         return cond_un_op(uset, &identity_entry);
1237 }
1238
1239 static int unwrap_entry(void **entry, void *user)
1240 {
1241         isl_set *set = *entry;
1242         isl_union_set **res = user;
1243
1244         if (!isl_set_is_wrapping(set))
1245                 return 0;
1246
1247         *res = isl_union_map_add_map(*res, isl_set_unwrap(isl_set_copy(set)));
1248
1249         return 0;
1250 }
1251
1252 __isl_give isl_union_map *isl_union_set_unwrap(__isl_take isl_union_set *uset)
1253 {
1254         return cond_un_op(uset, &unwrap_entry);
1255 }
1256
1257 static int wrap_entry(void **entry, void *user)
1258 {
1259         isl_map *map = *entry;
1260         isl_union_set **res = user;
1261
1262         *res = isl_union_set_add_set(*res, isl_map_wrap(isl_map_copy(map)));
1263
1264         return 0;
1265 }
1266
1267 __isl_give isl_union_set *isl_union_map_wrap(__isl_take isl_union_map *umap)
1268 {
1269         return cond_un_op(umap, &wrap_entry);
1270 }
1271
1272 struct isl_union_map_is_subset_data {
1273         isl_union_map *umap2;
1274         int is_subset;
1275 };
1276
1277 static int is_subset_entry(void **entry, void *user)
1278 {
1279         struct isl_union_map_is_subset_data *data = user;
1280         uint32_t hash;
1281         struct isl_hash_table_entry *entry2;
1282         isl_map *map = *entry;
1283
1284         hash = isl_dim_get_hash(map->dim);
1285         entry2 = isl_hash_table_find(data->umap2->dim->ctx, &data->umap2->table,
1286                                      hash, &has_dim, map->dim, 0);
1287         if (!entry2) {
1288                 data->is_subset = 0;
1289                 return -1;
1290         }
1291
1292         data->is_subset = isl_map_is_subset(map, entry2->data);
1293         if (data->is_subset < 0 || !data->is_subset)
1294                 return -1;
1295
1296         return 0;
1297 }
1298
1299 int isl_union_map_is_subset(__isl_keep isl_union_map *umap1,
1300         __isl_keep isl_union_map *umap2)
1301 {
1302         struct isl_union_map_is_subset_data data = { NULL, 1 };
1303
1304         umap1 = isl_union_map_copy(umap1);
1305         umap2 = isl_union_map_copy(umap2);
1306         umap1 = isl_union_map_align_params(umap1, isl_union_map_get_dim(umap2));
1307         umap2 = isl_union_map_align_params(umap2, isl_union_map_get_dim(umap1));
1308
1309         if (!umap1 || !umap2)
1310                 goto error;
1311
1312         data.umap2 = umap2;
1313         if (isl_hash_table_foreach(umap1->dim->ctx, &umap1->table,
1314                                    &is_subset_entry, &data) < 0 &&
1315             data.is_subset)
1316                 goto error;
1317
1318         isl_union_map_free(umap1);
1319         isl_union_map_free(umap2);
1320
1321         return data.is_subset;
1322 error:
1323         isl_union_map_free(umap1);
1324         isl_union_map_free(umap2);
1325         return -1;
1326 }
1327
1328 int isl_union_set_is_subset(__isl_keep isl_union_set *uset1,
1329         __isl_keep isl_union_set *uset2)
1330 {
1331         return isl_union_map_is_subset(uset1, uset2);
1332 }
1333
1334 int isl_union_map_is_equal(__isl_keep isl_union_map *umap1,
1335         __isl_keep isl_union_map *umap2)
1336 {
1337         int is_subset;
1338
1339         if (!umap1 || !umap2)
1340                 return -1;
1341         is_subset = isl_union_map_is_subset(umap1, umap2);
1342         if (is_subset != 1)
1343                 return is_subset;
1344         is_subset = isl_union_map_is_subset(umap2, umap1);
1345         return is_subset;
1346 }
1347
1348 int isl_union_set_is_equal(__isl_keep isl_union_set *uset1,
1349         __isl_keep isl_union_set *uset2)
1350 {
1351         return isl_union_map_is_equal(uset1, uset2);
1352 }
1353
1354 int isl_union_map_is_strict_subset(__isl_keep isl_union_map *umap1,
1355         __isl_keep isl_union_map *umap2)
1356 {
1357         int is_subset;
1358
1359         if (!umap1 || !umap2)
1360                 return -1;
1361         is_subset = isl_union_map_is_subset(umap1, umap2);
1362         if (is_subset != 1)
1363                 return is_subset;
1364         is_subset = isl_union_map_is_subset(umap2, umap1);
1365         if (is_subset == -1)
1366                 return is_subset;
1367         return !is_subset;
1368 }
1369
1370 int isl_union_set_is_strict_subset(__isl_keep isl_union_set *uset1,
1371         __isl_keep isl_union_set *uset2)
1372 {
1373         return isl_union_map_is_strict_subset(uset1, uset2);
1374 }
1375
1376 static int sample_entry(void **entry, void *user)
1377 {
1378         isl_basic_map **sample = (isl_basic_map **)user;
1379         isl_map *map = *entry;
1380
1381         *sample = isl_map_sample(isl_map_copy(map));
1382         if (!*sample)
1383                 return -1;
1384         if (!isl_basic_map_fast_is_empty(*sample))
1385                 return -1;
1386         return 0;
1387 }
1388
1389 __isl_give isl_basic_map *isl_union_map_sample(__isl_take isl_union_map *umap)
1390 {
1391         isl_basic_map *sample = NULL;
1392
1393         if (!umap)
1394                 return NULL;
1395
1396         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1397                                    &sample_entry, &sample) < 0 &&
1398             !sample)
1399                 goto error;
1400
1401         if (!sample)
1402                 sample = isl_basic_map_empty(isl_union_map_get_dim(umap));
1403
1404         isl_union_map_free(umap);
1405
1406         return sample;
1407 error:
1408         isl_union_map_free(umap);
1409         return NULL;
1410 }
1411
1412 __isl_give isl_basic_set *isl_union_set_sample(__isl_take isl_union_set *uset)
1413 {
1414         return (isl_basic_set *)isl_union_map_sample(uset);
1415 }
1416
1417 static int empty_entry(void **entry, void *user)
1418 {
1419         int *empty = user;
1420         isl_map *map = *entry;
1421
1422         if (isl_map_is_empty(map))
1423                 return 0;
1424
1425         *empty = 0;
1426
1427         return -1;
1428 }
1429
1430 __isl_give int isl_union_map_is_empty(__isl_keep isl_union_map *umap)
1431 {
1432         int empty = 1;
1433
1434         if (!umap)
1435                 return -1;
1436
1437         if (isl_hash_table_foreach(umap->dim->ctx, &umap->table,
1438                                    &empty_entry, &empty) < 0 && empty)
1439                 return -1;
1440
1441         return empty;
1442 }
1443
1444 int isl_union_set_is_empty(__isl_keep isl_union_set *uset)
1445 {
1446         return isl_union_map_is_empty(uset);
1447 }