isl_map_intersect: be careful when intersecting with empty parameter domain
[platform/upstream/isl.git] / isl_map.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
7  * Written by Sven Verdoolaege, K.U.Leuven, Departement
8  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
11  */
12
13 #include <string.h>
14 #include <isl_ctx_private.h>
15 #include <isl_map_private.h>
16 #include <isl/blk.h>
17 #include "isl_dim_private.h"
18 #include "isl_equalities.h"
19 #include <isl_list_private.h>
20 #include <isl/lp.h>
21 #include <isl/seq.h>
22 #include <isl/set.h>
23 #include <isl/map.h>
24 #include "isl_map_piplib.h"
25 #include <isl_reordering.h>
26 #include "isl_sample.h"
27 #include "isl_tab.h"
28 #include <isl/vec.h>
29 #include <isl_mat_private.h>
30 #include <isl_dim_map.h>
31 #include <isl_local_space_private.h>
32 #include <isl_aff_private.h>
33
34 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
35 {
36         switch (type) {
37         case isl_dim_param:     return dim->nparam;
38         case isl_dim_in:        return dim->n_in;
39         case isl_dim_out:       return dim->n_out;
40         case isl_dim_all:       return dim->nparam + dim->n_in + dim->n_out;
41         default:                return 0;
42         }
43 }
44
45 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
46 {
47         switch (type) {
48         case isl_dim_param:     return 1;
49         case isl_dim_in:        return 1 + dim->nparam;
50         case isl_dim_out:       return 1 + dim->nparam + dim->n_in;
51         default:                return 0;
52         }
53 }
54
55 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
56                                 enum isl_dim_type type)
57 {
58         if (!bmap)
59                 return 0;
60         switch (type) {
61         case isl_dim_cst:       return 1;
62         case isl_dim_param:
63         case isl_dim_in:
64         case isl_dim_out:       return isl_dim_size(bmap->dim, type);
65         case isl_dim_div:       return bmap->n_div;
66         case isl_dim_all:       return isl_basic_map_total_dim(bmap);
67         default:                return 0;
68         }
69 }
70
71 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
72 {
73         return map ? n(map->dim, type) : 0;
74 }
75
76 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
77 {
78         return set ? n(set->dim, type) : 0;
79 }
80
81 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
82                                         enum isl_dim_type type)
83 {
84         struct isl_dim *dim = bmap->dim;
85         switch (type) {
86         case isl_dim_cst:       return 0;
87         case isl_dim_param:     return 1;
88         case isl_dim_in:        return 1 + dim->nparam;
89         case isl_dim_out:       return 1 + dim->nparam + dim->n_in;
90         case isl_dim_div:       return 1 + dim->nparam + dim->n_in + dim->n_out;
91         default:                return 0;
92         }
93 }
94
95 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
96                                         enum isl_dim_type type)
97 {
98         return isl_basic_map_offset(bset, type);
99 }
100
101 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
102 {
103         return pos(map->dim, type);
104 }
105
106 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
107                                 enum isl_dim_type type)
108 {
109         return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
110 }
111
112 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
113 {
114         return isl_basic_set_dim(bset, isl_dim_set);
115 }
116
117 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
118 {
119         return isl_basic_set_dim(bset, isl_dim_param);
120 }
121
122 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
123 {
124         return isl_dim_total(bset->dim) + bset->n_div;
125 }
126
127 unsigned isl_set_n_dim(const struct isl_set *set)
128 {
129         return isl_set_dim(set, isl_dim_set);
130 }
131
132 unsigned isl_set_n_param(const struct isl_set *set)
133 {
134         return isl_set_dim(set, isl_dim_param);
135 }
136
137 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
138 {
139         return bmap ? bmap->dim->n_in : 0;
140 }
141
142 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
143 {
144         return bmap ? bmap->dim->n_out : 0;
145 }
146
147 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
148 {
149         return bmap ? bmap->dim->nparam : 0;
150 }
151
152 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
153 {
154         return bmap ? bmap->n_div : 0;
155 }
156
157 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
158 {
159         return bmap ? isl_dim_total(bmap->dim) + bmap->n_div : 0;
160 }
161
162 unsigned isl_map_n_in(const struct isl_map *map)
163 {
164         return map ? map->dim->n_in : 0;
165 }
166
167 unsigned isl_map_n_out(const struct isl_map *map)
168 {
169         return map ? map->dim->n_out : 0;
170 }
171
172 unsigned isl_map_n_param(const struct isl_map *map)
173 {
174         return map ? map->dim->nparam : 0;
175 }
176
177 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
178 {
179         int m;
180         if (!map || !set)
181                 return -1;
182         m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
183         if (m < 0 || !m)
184                 return m;
185         return isl_dim_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
186 }
187
188 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
189                 struct isl_basic_set *bset)
190 {
191         int m;
192         if (!bmap || !bset)
193                 return -1;
194         m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
195         if (m < 0 || !m)
196                 return m;
197         return isl_dim_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
198 }
199
200 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
201 {
202         int m;
203         if (!map || !set)
204                 return -1;
205         m = isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
206         if (m < 0 || !m)
207                 return m;
208         return isl_dim_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
209 }
210
211 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
212                 struct isl_basic_set *bset)
213 {
214         int m;
215         if (!bmap || !bset)
216                 return -1;
217         m = isl_dim_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
218         if (m < 0 || !m)
219                 return m;
220         return isl_dim_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
221 }
222
223 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
224 {
225         return bmap ? bmap->ctx : NULL;
226 }
227
228 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
229 {
230         return bset ? bset->ctx : NULL;
231 }
232
233 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
234 {
235         return map ? map->ctx : NULL;
236 }
237
238 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
239 {
240         return set ? set->ctx : NULL;
241 }
242
243 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
244 {
245         if (!bmap)
246                 return NULL;
247         return isl_dim_copy(bmap->dim);
248 }
249
250 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
251 {
252         if (!bset)
253                 return NULL;
254         return isl_dim_copy(bset->dim);
255 }
256
257 __isl_give isl_local_space *isl_basic_map_get_local_space(
258         __isl_keep isl_basic_map *bmap)
259 {
260         int i;
261         isl_local_space *ls;
262         unsigned total;
263
264         if (!bmap)
265                 return NULL;
266
267         total = isl_basic_map_total_dim(bmap);
268         ls = isl_local_space_alloc(isl_dim_copy(bmap->dim), bmap->n_div);
269         if (!ls)
270                 return NULL;
271
272         for (i = 0; i < bmap->n_div; ++i)
273                 isl_seq_cpy(ls->div->row[i], bmap->div[i], 2 + total);
274
275         return ls;
276 }
277
278 __isl_give isl_local_space *isl_basic_set_get_local_space(
279         __isl_keep isl_basic_set *bset)
280 {
281         return isl_basic_map_get_local_space(bset);
282 }
283
284 __isl_give isl_basic_map *isl_basic_map_from_local_space(
285         __isl_take isl_local_space *ls)
286 {
287         int i;
288         int n_div;
289         isl_basic_map *bmap;
290
291         if (!ls)
292                 return NULL;
293
294         n_div = isl_local_space_dim(ls, isl_dim_div);
295         bmap = isl_basic_map_alloc_dim(isl_local_space_get_dim(ls),
296                                         n_div, 0, 2 * n_div);
297
298         for (i = 0; i < n_div; ++i)
299                 if (isl_basic_map_alloc_div(bmap) < 0)
300                         goto error;
301
302         for (i = 0; i < n_div; ++i) {
303                 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
304                 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
305                         goto error;
306         }
307                                         
308         isl_local_space_free(ls);
309         return bmap;
310 error:
311         isl_local_space_free(ls);
312         isl_basic_map_free(bmap);
313         return NULL;
314 }
315
316 __isl_give isl_basic_set *isl_basic_set_from_local_space(
317         __isl_take isl_local_space *ls)
318 {
319         return isl_basic_map_from_local_space(ls);
320 }
321
322 struct isl_dim *isl_map_get_dim(struct isl_map *map)
323 {
324         if (!map)
325                 return NULL;
326         return isl_dim_copy(map->dim);
327 }
328
329 struct isl_dim *isl_set_get_dim(struct isl_set *set)
330 {
331         if (!set)
332                 return NULL;
333         return isl_dim_copy(set->dim);
334 }
335
336 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
337         __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
338 {
339         bmap = isl_basic_map_cow(bmap);
340         if (!bmap)
341                 return NULL;
342         bmap->dim = isl_dim_set_tuple_name(bmap->dim, type, s);
343         if (!bmap->dim)
344                 goto error;
345         bmap = isl_basic_map_finalize(bmap);
346         return bmap;
347 error:
348         isl_basic_map_free(bmap);
349         return NULL;
350 }
351
352 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
353         __isl_take isl_basic_set *bset, const char *s)
354 {
355         return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
356 }
357
358 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
359         enum isl_dim_type type)
360 {
361         return bmap ? isl_dim_get_tuple_name(bmap->dim, type) : NULL;
362 }
363
364 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
365         enum isl_dim_type type, const char *s)
366 {
367         int i;
368
369         map = isl_map_cow(map);
370         if (!map)
371                 return NULL;
372
373         map->dim = isl_dim_set_tuple_name(map->dim, type, s);
374         if (!map->dim)
375                 goto error;
376
377         for (i = 0; i < map->n; ++i) {
378                 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
379                 if (!map->p[i])
380                         goto error;
381         }
382
383         return map;
384 error:
385         isl_map_free(map);
386         return NULL;
387 }
388
389 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
390         enum isl_dim_type type)
391 {
392         return map ? isl_dim_get_tuple_name(map->dim, type) : NULL;
393 }
394
395 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
396         const char *s)
397 {
398         return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
399 }
400
401 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
402         enum isl_dim_type type, __isl_take isl_id *id)
403 {
404         map = isl_map_cow(map);
405         if (!map)
406                 return isl_id_free(id);
407
408         map->dim = isl_dim_set_tuple_id(map->dim, type, id);
409
410         return isl_map_reset_dim(map, isl_dim_copy(map->dim));
411 }
412
413 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
414         __isl_take isl_id *id)
415 {
416         return isl_map_set_tuple_id(set, isl_dim_set, id);
417 }
418
419 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
420         enum isl_dim_type type)
421 {
422         map = isl_map_cow(map);
423         if (!map)
424                 return NULL;
425
426         map->dim = isl_dim_reset_tuple_id(map->dim, type);
427
428         return isl_map_reset_dim(map, isl_dim_copy(map->dim));
429 }
430
431 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
432         enum isl_dim_type type)
433 {
434         return map ? isl_dim_get_tuple_id(map->dim, type) : NULL;
435 }
436
437 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
438 {
439         return isl_map_get_tuple_id(set, isl_dim_set);
440 }
441
442 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
443 {
444         return bset ? isl_dim_get_tuple_name(bset->dim, isl_dim_set) : NULL;
445 }
446
447 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
448 {
449         return set ? isl_dim_get_tuple_name(set->dim, isl_dim_set) : NULL;
450 }
451
452 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
453         enum isl_dim_type type, unsigned pos)
454 {
455         return bmap ? isl_dim_get_name(bmap->dim, type, pos) : NULL;
456 }
457
458 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
459         enum isl_dim_type type, unsigned pos)
460 {
461         return bset ? isl_dim_get_name(bset->dim, type, pos) : NULL;
462 }
463
464 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
465         enum isl_dim_type type, unsigned pos)
466 {
467         return map ? isl_dim_get_name(map->dim, type, pos) : NULL;
468 }
469
470 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
471         enum isl_dim_type type, unsigned pos)
472 {
473         return set ? isl_dim_get_name(set->dim, type, pos) : NULL;
474 }
475
476 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
477         __isl_take isl_basic_map *bmap,
478         enum isl_dim_type type, unsigned pos, const char *s)
479 {
480         if (!bmap)
481                 return NULL;
482         bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
483         if (!bmap->dim)
484                 goto error;
485         return bmap;
486 error:
487         isl_basic_map_free(bmap);
488         return NULL;
489 }
490
491 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
492         enum isl_dim_type type, unsigned pos, const char *s)
493 {
494         int i;
495
496         if (!map)
497                 return NULL;
498
499         map->dim = isl_dim_set_name(map->dim, type, pos, s);
500         if (!map->dim)
501                 goto error;
502
503         for (i = 0; i < map->n; ++i) {
504                 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
505                 if (!map->p[i])
506                         goto error;
507         }
508
509         return map;
510 error:
511         isl_map_free(map);
512         return NULL;
513 }
514
515 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
516         __isl_take isl_basic_set *bset,
517         enum isl_dim_type type, unsigned pos, const char *s)
518 {
519         return (isl_basic_set *)isl_basic_map_set_dim_name(
520                 (isl_basic_map *)bset, type, pos, s);
521 }
522
523 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
524         enum isl_dim_type type, unsigned pos, const char *s)
525 {
526         return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
527 }
528
529 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
530         enum isl_dim_type type, unsigned pos)
531 {
532         return map ? isl_dim_get_dim_id(map->dim, type, pos) : NULL;
533 }
534
535 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
536         enum isl_dim_type type, unsigned pos)
537 {
538         return isl_map_get_dim_id(set, type, pos);
539 }
540
541 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
542         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
543 {
544         map = isl_map_cow(map);
545         if (!map)
546                 return isl_id_free(id);
547
548         map->dim = isl_dim_set_dim_id(map->dim, type, pos, id);
549
550         return isl_map_reset_dim(map, isl_dim_copy(map->dim));
551 }
552
553 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
554         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
555 {
556         return isl_map_set_dim_id(set, type, pos, id);
557 }
558
559 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
560         __isl_keep isl_id *id)
561 {
562         if (!map)
563                 return -1;
564         return isl_dim_find_dim_by_id(map->dim, type, id);
565 }
566
567 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
568         __isl_keep isl_id *id)
569 {
570         return isl_map_find_dim_by_id(set, type, id);
571 }
572
573 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
574 {
575         if (!bmap)
576                 return -1;
577         return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
578 }
579
580 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
581 {
582         return isl_basic_map_is_rational(bset);
583 }
584
585 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
586                 struct isl_basic_map *bmap, unsigned extra,
587                 unsigned n_eq, unsigned n_ineq)
588 {
589         int i;
590         size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
591
592         bmap->ctx = ctx;
593         isl_ctx_ref(ctx);
594
595         bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
596         if (isl_blk_is_error(bmap->block))
597                 goto error;
598
599         bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
600         if (!bmap->ineq)
601                 goto error;
602
603         if (extra == 0) {
604                 bmap->block2 = isl_blk_empty();
605                 bmap->div = NULL;
606         } else {
607                 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
608                 if (isl_blk_is_error(bmap->block2))
609                         goto error;
610
611                 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
612                 if (!bmap->div)
613                         goto error;
614         }
615
616         for (i = 0; i < n_ineq + n_eq; ++i)
617                 bmap->ineq[i] = bmap->block.data + i * row_size;
618
619         for (i = 0; i < extra; ++i)
620                 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
621
622         bmap->ref = 1;
623         bmap->flags = 0;
624         bmap->c_size = n_eq + n_ineq;
625         bmap->eq = bmap->ineq + n_ineq;
626         bmap->extra = extra;
627         bmap->n_eq = 0;
628         bmap->n_ineq = 0;
629         bmap->n_div = 0;
630         bmap->sample = NULL;
631
632         return bmap;
633 error:
634         isl_basic_map_free(bmap);
635         return NULL;
636 }
637
638 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
639                 unsigned nparam, unsigned dim, unsigned extra,
640                 unsigned n_eq, unsigned n_ineq)
641 {
642         struct isl_basic_map *bmap;
643         bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
644         return (struct isl_basic_set *)bmap;
645 }
646
647 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
648                 unsigned extra, unsigned n_eq, unsigned n_ineq)
649 {
650         struct isl_basic_map *bmap;
651         if (!dim)
652                 return NULL;
653         isl_assert(dim->ctx, dim->n_in == 0, goto error);
654         bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
655         return (struct isl_basic_set *)bmap;
656 error:
657         isl_dim_free(dim);
658         return NULL;
659 }
660
661 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
662                 unsigned extra, unsigned n_eq, unsigned n_ineq)
663 {
664         struct isl_basic_map *bmap;
665
666         if (!dim)
667                 return NULL;
668         bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
669         if (!bmap)
670                 goto error;
671         bmap->dim = dim;
672
673         return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
674 error:
675         isl_dim_free(dim);
676         return NULL;
677 }
678
679 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
680                 unsigned nparam, unsigned in, unsigned out, unsigned extra,
681                 unsigned n_eq, unsigned n_ineq)
682 {
683         struct isl_basic_map *bmap;
684         struct isl_dim *dim;
685
686         dim = isl_dim_alloc(ctx, nparam, in, out);
687         if (!dim)
688                 return NULL;
689
690         bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
691         return bmap;
692 }
693
694 static void dup_constraints(
695                 struct isl_basic_map *dst, struct isl_basic_map *src)
696 {
697         int i;
698         unsigned total = isl_basic_map_total_dim(src);
699
700         for (i = 0; i < src->n_eq; ++i) {
701                 int j = isl_basic_map_alloc_equality(dst);
702                 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
703         }
704
705         for (i = 0; i < src->n_ineq; ++i) {
706                 int j = isl_basic_map_alloc_inequality(dst);
707                 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
708         }
709
710         for (i = 0; i < src->n_div; ++i) {
711                 int j = isl_basic_map_alloc_div(dst);
712                 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
713         }
714         ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
715 }
716
717 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
718 {
719         struct isl_basic_map *dup;
720
721         if (!bmap)
722                 return NULL;
723         dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
724                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
725         if (!dup)
726                 return NULL;
727         dup_constraints(dup, bmap);
728         dup->flags = bmap->flags;
729         dup->sample = isl_vec_copy(bmap->sample);
730         return dup;
731 }
732
733 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
734 {
735         struct isl_basic_map *dup;
736
737         dup = isl_basic_map_dup((struct isl_basic_map *)bset);
738         return (struct isl_basic_set *)dup;
739 }
740
741 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
742 {
743         if (!bset)
744                 return NULL;
745
746         if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
747                 bset->ref++;
748                 return bset;
749         }
750         return isl_basic_set_dup(bset);
751 }
752
753 struct isl_set *isl_set_copy(struct isl_set *set)
754 {
755         if (!set)
756                 return NULL;
757
758         set->ref++;
759         return set;
760 }
761
762 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
763 {
764         if (!bmap)
765                 return NULL;
766
767         if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
768                 bmap->ref++;
769                 return bmap;
770         }
771         bmap = isl_basic_map_dup(bmap);
772         if (bmap)
773                 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
774         return bmap;
775 }
776
777 struct isl_map *isl_map_copy(struct isl_map *map)
778 {
779         if (!map)
780                 return NULL;
781
782         map->ref++;
783         return map;
784 }
785
786 void isl_basic_map_free(struct isl_basic_map *bmap)
787 {
788         if (!bmap)
789                 return;
790
791         if (--bmap->ref > 0)
792                 return;
793
794         isl_ctx_deref(bmap->ctx);
795         free(bmap->div);
796         isl_blk_free(bmap->ctx, bmap->block2);
797         free(bmap->ineq);
798         isl_blk_free(bmap->ctx, bmap->block);
799         isl_vec_free(bmap->sample);
800         isl_dim_free(bmap->dim);
801         free(bmap);
802 }
803
804 void isl_basic_set_free(struct isl_basic_set *bset)
805 {
806         isl_basic_map_free((struct isl_basic_map *)bset);
807 }
808
809 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
810 {
811         return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
812 }
813
814 __isl_give isl_map *isl_map_align_params_map_map_and(
815         __isl_take isl_map *map1, __isl_take isl_map *map2,
816         __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
817                                     __isl_take isl_map *map2))
818 {
819         if (!map1 || !map2)
820                 goto error;
821         if (isl_dim_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
822                 return fn(map1, map2);
823         if (!isl_dim_has_named_params(map1->dim) ||
824             !isl_dim_has_named_params(map2->dim))
825                 isl_die(map1->ctx, isl_error_invalid,
826                         "unaligned unnamed parameters", goto error);
827         map1 = isl_map_align_params(map1, isl_map_get_dim(map2));
828         map2 = isl_map_align_params(map2, isl_map_get_dim(map1));
829         return fn(map1, map2);
830 error:
831         isl_map_free(map1);
832         isl_map_free(map2);
833         return NULL;
834 }
835
836 static int align_params_map_map_and_test(__isl_keep isl_map *map1,
837         __isl_keep isl_map *map2,
838         int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
839 {
840         int r;
841
842         if (!map1 || !map2)
843                 return -1;
844         if (isl_dim_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
845                 return fn(map1, map2);
846         if (!isl_dim_has_named_params(map1->dim) ||
847             !isl_dim_has_named_params(map2->dim))
848                 isl_die(map1->ctx, isl_error_invalid,
849                         "unaligned unnamed parameters", return -1);
850         map1 = isl_map_copy(map1);
851         map2 = isl_map_copy(map2);
852         map1 = isl_map_align_params(map1, isl_map_get_dim(map2));
853         map2 = isl_map_align_params(map2, isl_map_get_dim(map1));
854         r = fn(map1, map2);
855         isl_map_free(map1);
856         isl_map_free(map2);
857         return r;
858 }
859
860 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
861 {
862         struct isl_ctx *ctx;
863         if (!bmap)
864                 return -1;
865         ctx = bmap->ctx;
866         isl_assert(ctx, room_for_con(bmap, 1), return -1);
867         isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
868                         return -1);
869         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
870         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
871         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
872         ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
873         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
874         if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
875                 isl_int *t;
876                 int j = isl_basic_map_alloc_inequality(bmap);
877                 if (j < 0)
878                         return -1;
879                 t = bmap->ineq[j];
880                 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
881                 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
882                 bmap->eq[-1] = t;
883                 bmap->n_eq++;
884                 bmap->n_ineq--;
885                 bmap->eq--;
886                 return 0;
887         }
888         isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
889                       bmap->extra - bmap->n_div);
890         return bmap->n_eq++;
891 }
892
893 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
894 {
895         return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
896 }
897
898 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
899 {
900         if (!bmap)
901                 return -1;
902         isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
903         bmap->n_eq -= n;
904         return 0;
905 }
906
907 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
908 {
909         return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
910 }
911
912 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
913 {
914         isl_int *t;
915         if (!bmap)
916                 return -1;
917         isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
918
919         if (pos != bmap->n_eq - 1) {
920                 t = bmap->eq[pos];
921                 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
922                 bmap->eq[bmap->n_eq - 1] = t;
923         }
924         bmap->n_eq--;
925         return 0;
926 }
927
928 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
929 {
930         return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
931 }
932
933 void isl_basic_map_inequality_to_equality(
934                 struct isl_basic_map *bmap, unsigned pos)
935 {
936         isl_int *t;
937
938         t = bmap->ineq[pos];
939         bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
940         bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
941         bmap->eq[-1] = t;
942         bmap->n_eq++;
943         bmap->n_ineq--;
944         bmap->eq--;
945         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
946         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
947         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
948         ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
949 }
950
951 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
952 {
953         return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
954 }
955
956 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
957 {
958         struct isl_ctx *ctx;
959         if (!bmap)
960                 return -1;
961         ctx = bmap->ctx;
962         isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
963         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
964         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
965         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
966         ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
967         isl_seq_clr(bmap->ineq[bmap->n_ineq] +
968                       1 + isl_basic_map_total_dim(bmap),
969                       bmap->extra - bmap->n_div);
970         return bmap->n_ineq++;
971 }
972
973 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
974 {
975         return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
976 }
977
978 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
979 {
980         if (!bmap)
981                 return -1;
982         isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
983         bmap->n_ineq -= n;
984         return 0;
985 }
986
987 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
988 {
989         return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
990 }
991
992 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
993 {
994         isl_int *t;
995         if (!bmap)
996                 return -1;
997         isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
998
999         if (pos != bmap->n_ineq - 1) {
1000                 t = bmap->ineq[pos];
1001                 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1002                 bmap->ineq[bmap->n_ineq - 1] = t;
1003                 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1004         }
1005         bmap->n_ineq--;
1006         return 0;
1007 }
1008
1009 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1010 {
1011         return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1012 }
1013
1014 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1015         isl_int *eq)
1016 {
1017         int k;
1018
1019         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1020         if (!bmap)
1021                 return NULL;
1022         k = isl_basic_map_alloc_equality(bmap);
1023         if (k < 0)
1024                 goto error;
1025         isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1026         return bmap;
1027 error:
1028         isl_basic_map_free(bmap);
1029         return NULL;
1030 }
1031
1032 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1033         isl_int *eq)
1034 {
1035         return (isl_basic_set *)
1036                 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1037 }
1038
1039 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1040         isl_int *ineq)
1041 {
1042         int k;
1043
1044         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1045         if (!bmap)
1046                 return NULL;
1047         k = isl_basic_map_alloc_inequality(bmap);
1048         if (k < 0)
1049                 goto error;
1050         isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1051         return bmap;
1052 error:
1053         isl_basic_map_free(bmap);
1054         return NULL;
1055 }
1056
1057 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1058         isl_int *ineq)
1059 {
1060         return (isl_basic_set *)
1061                 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1062 }
1063
1064 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1065 {
1066         if (!bmap)
1067                 return -1;
1068         isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1069         isl_seq_clr(bmap->div[bmap->n_div] +
1070                       1 + 1 + isl_basic_map_total_dim(bmap),
1071                       bmap->extra - bmap->n_div);
1072         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1073         return bmap->n_div++;
1074 }
1075
1076 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1077 {
1078         return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1079 }
1080
1081 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1082 {
1083         if (!bmap)
1084                 return -1;
1085         isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1086         bmap->n_div -= n;
1087         return 0;
1088 }
1089
1090 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1091 {
1092         return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1093 }
1094
1095 /* Copy constraint from src to dst, putting the vars of src at offset
1096  * dim_off in dst and the divs of src at offset div_off in dst.
1097  * If both sets are actually map, then dim_off applies to the input
1098  * variables.
1099  */
1100 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1101                             struct isl_basic_map *src_map, isl_int *src,
1102                             unsigned in_off, unsigned out_off, unsigned div_off)
1103 {
1104         unsigned src_nparam = isl_basic_map_n_param(src_map);
1105         unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1106         unsigned src_in = isl_basic_map_n_in(src_map);
1107         unsigned dst_in = isl_basic_map_n_in(dst_map);
1108         unsigned src_out = isl_basic_map_n_out(src_map);
1109         unsigned dst_out = isl_basic_map_n_out(dst_map);
1110         isl_int_set(dst[0], src[0]);
1111         isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1112         if (dst_nparam > src_nparam)
1113                 isl_seq_clr(dst+1+src_nparam,
1114                                 dst_nparam - src_nparam);
1115         isl_seq_clr(dst+1+dst_nparam, in_off);
1116         isl_seq_cpy(dst+1+dst_nparam+in_off,
1117                     src+1+src_nparam,
1118                     isl_min(dst_in-in_off, src_in));
1119         if (dst_in-in_off > src_in)
1120                 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1121                                 dst_in - in_off - src_in);
1122         isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1123         isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1124                     src+1+src_nparam+src_in,
1125                     isl_min(dst_out-out_off, src_out));
1126         if (dst_out-out_off > src_out)
1127                 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1128                                 dst_out - out_off - src_out);
1129         isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1130         isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1131                     src+1+src_nparam+src_in+src_out,
1132                     isl_min(dst_map->extra-div_off, src_map->n_div));
1133         if (dst_map->n_div-div_off > src_map->n_div)
1134                 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1135                                 div_off+src_map->n_div,
1136                                 dst_map->n_div - div_off - src_map->n_div);
1137 }
1138
1139 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1140                      struct isl_basic_map *src_map, isl_int *src,
1141                      unsigned in_off, unsigned out_off, unsigned div_off)
1142 {
1143         isl_int_set(dst[0], src[0]);
1144         copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1145 }
1146
1147 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1148                 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1149 {
1150         int i;
1151         unsigned div_off;
1152
1153         if (!bmap1 || !bmap2)
1154                 goto error;
1155
1156         div_off = bmap1->n_div;
1157
1158         for (i = 0; i < bmap2->n_eq; ++i) {
1159                 int i1 = isl_basic_map_alloc_equality(bmap1);
1160                 if (i1 < 0)
1161                         goto error;
1162                 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1163                                 i_pos, o_pos, div_off);
1164         }
1165
1166         for (i = 0; i < bmap2->n_ineq; ++i) {
1167                 int i1 = isl_basic_map_alloc_inequality(bmap1);
1168                 if (i1 < 0)
1169                         goto error;
1170                 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1171                                 i_pos, o_pos, div_off);
1172         }
1173
1174         for (i = 0; i < bmap2->n_div; ++i) {
1175                 int i1 = isl_basic_map_alloc_div(bmap1);
1176                 if (i1 < 0)
1177                         goto error;
1178                 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1179                          i_pos, o_pos, div_off);
1180         }
1181
1182         isl_basic_map_free(bmap2);
1183
1184         return bmap1;
1185
1186 error:
1187         isl_basic_map_free(bmap1);
1188         isl_basic_map_free(bmap2);
1189         return NULL;
1190 }
1191
1192 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1193                 struct isl_basic_set *bset2, unsigned pos)
1194 {
1195         return (struct isl_basic_set *)
1196                 add_constraints((struct isl_basic_map *)bset1,
1197                                 (struct isl_basic_map *)bset2, 0, pos);
1198 }
1199
1200 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
1201                 struct isl_dim *dim, unsigned extra,
1202                 unsigned n_eq, unsigned n_ineq)
1203 {
1204         struct isl_basic_map *ext;
1205         unsigned flags;
1206         int dims_ok;
1207
1208         if (!dim)
1209                 goto error;
1210
1211         if (!base)
1212                 goto error;
1213
1214         dims_ok = isl_dim_equal(base->dim, dim) &&
1215                   base->extra >= base->n_div + extra;
1216
1217         if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1218                        room_for_ineq(base, n_ineq)) {
1219                 isl_dim_free(dim);
1220                 return base;
1221         }
1222
1223         isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1224         isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1225         isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1226         extra += base->extra;
1227         n_eq += base->n_eq;
1228         n_ineq += base->n_ineq;
1229
1230         ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1231         dim = NULL;
1232         if (!ext)
1233                 goto error;
1234
1235         if (dims_ok)
1236                 ext->sample = isl_vec_copy(base->sample);
1237         flags = base->flags;
1238         ext = add_constraints(ext, base, 0, 0);
1239         if (ext) {
1240                 ext->flags = flags;
1241                 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1242         }
1243
1244         return ext;
1245
1246 error:
1247         isl_dim_free(dim);
1248         isl_basic_map_free(base);
1249         return NULL;
1250 }
1251
1252 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1253                 struct isl_dim *dim, unsigned extra,
1254                 unsigned n_eq, unsigned n_ineq)
1255 {
1256         return (struct isl_basic_set *)
1257                 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1258                                                         extra, n_eq, n_ineq);
1259 }
1260
1261 struct isl_basic_map *isl_basic_map_extend_constraints(
1262                 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1263 {
1264         if (!base)
1265                 return NULL;
1266         return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1267                                         0, n_eq, n_ineq);
1268 }
1269
1270 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1271                 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1272                 unsigned n_eq, unsigned n_ineq)
1273 {
1274         struct isl_basic_map *bmap;
1275         struct isl_dim *dim;
1276
1277         if (!base)
1278                 return NULL;
1279         dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1280         if (!dim)
1281                 goto error;
1282
1283         bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1284         return bmap;
1285 error:
1286         isl_basic_map_free(base);
1287         return NULL;
1288 }
1289
1290 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1291                 unsigned nparam, unsigned dim, unsigned extra,
1292                 unsigned n_eq, unsigned n_ineq)
1293 {
1294         return (struct isl_basic_set *)
1295                 isl_basic_map_extend((struct isl_basic_map *)base,
1296                                         nparam, 0, dim, extra, n_eq, n_ineq);
1297 }
1298
1299 struct isl_basic_set *isl_basic_set_extend_constraints(
1300                 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1301 {
1302         return (struct isl_basic_set *)
1303                 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1304                                                     n_eq, n_ineq);
1305 }
1306
1307 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1308 {
1309         return (struct isl_basic_set *)
1310                 isl_basic_map_cow((struct isl_basic_map *)bset);
1311 }
1312
1313 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1314 {
1315         if (!bmap)
1316                 return NULL;
1317
1318         if (bmap->ref > 1) {
1319                 bmap->ref--;
1320                 bmap = isl_basic_map_dup(bmap);
1321         }
1322         if (bmap)
1323                 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1324         return bmap;
1325 }
1326
1327 struct isl_set *isl_set_cow(struct isl_set *set)
1328 {
1329         if (!set)
1330                 return NULL;
1331
1332         if (set->ref == 1)
1333                 return set;
1334         set->ref--;
1335         return isl_set_dup(set);
1336 }
1337
1338 struct isl_map *isl_map_cow(struct isl_map *map)
1339 {
1340         if (!map)
1341                 return NULL;
1342
1343         if (map->ref == 1)
1344                 return map;
1345         map->ref--;
1346         return isl_map_dup(map);
1347 }
1348
1349 static void swap_vars(struct isl_blk blk, isl_int *a,
1350                         unsigned a_len, unsigned b_len)
1351 {
1352         isl_seq_cpy(blk.data, a+a_len, b_len);
1353         isl_seq_cpy(blk.data+b_len, a, a_len);
1354         isl_seq_cpy(a, blk.data, b_len+a_len);
1355 }
1356
1357 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1358         __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1359 {
1360         int i;
1361         struct isl_blk blk;
1362
1363         if (!bmap)
1364                 goto error;
1365
1366         isl_assert(bmap->ctx,
1367                 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1368
1369         if (n1 == 0 || n2 == 0)
1370                 return bmap;
1371
1372         bmap = isl_basic_map_cow(bmap);
1373         if (!bmap)
1374                 return NULL;
1375
1376         blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1377         if (isl_blk_is_error(blk))
1378                 goto error;
1379
1380         for (i = 0; i < bmap->n_eq; ++i)
1381                 swap_vars(blk,
1382                           bmap->eq[i] + pos, n1, n2);
1383
1384         for (i = 0; i < bmap->n_ineq; ++i)
1385                 swap_vars(blk,
1386                           bmap->ineq[i] + pos, n1, n2);
1387
1388         for (i = 0; i < bmap->n_div; ++i)
1389                 swap_vars(blk,
1390                           bmap->div[i]+1 + pos, n1, n2);
1391
1392         isl_blk_free(bmap->ctx, blk);
1393
1394         ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1395         bmap = isl_basic_map_gauss(bmap, NULL);
1396         return isl_basic_map_finalize(bmap);
1397 error:
1398         isl_basic_map_free(bmap);
1399         return NULL;
1400 }
1401
1402 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1403         __isl_take isl_basic_set *bset, unsigned n)
1404 {
1405         unsigned dim;
1406         unsigned nparam;
1407
1408         nparam = isl_basic_set_n_param(bset);
1409         dim = isl_basic_set_n_dim(bset);
1410         isl_assert(bset->ctx, n <= dim, goto error);
1411
1412         return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1413 error:
1414         isl_basic_set_free(bset);
1415         return NULL;
1416 }
1417
1418 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1419 {
1420         int i = 0;
1421         unsigned total;
1422         if (!bmap)
1423                 goto error;
1424         total = isl_basic_map_total_dim(bmap);
1425         isl_basic_map_free_div(bmap, bmap->n_div);
1426         isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1427         if (bmap->n_eq > 0)
1428                 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1429         else {
1430                 i = isl_basic_map_alloc_equality(bmap);
1431                 if (i < 0)
1432                         goto error;
1433         }
1434         isl_int_set_si(bmap->eq[i][0], 1);
1435         isl_seq_clr(bmap->eq[i]+1, total);
1436         ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1437         isl_vec_free(bmap->sample);
1438         bmap->sample = NULL;
1439         return isl_basic_map_finalize(bmap);
1440 error:
1441         isl_basic_map_free(bmap);
1442         return NULL;
1443 }
1444
1445 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1446 {
1447         return (struct isl_basic_set *)
1448                 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1449 }
1450
1451 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1452 {
1453         int i;
1454         unsigned off = isl_dim_total(bmap->dim);
1455         isl_int *t = bmap->div[a];
1456         bmap->div[a] = bmap->div[b];
1457         bmap->div[b] = t;
1458
1459         for (i = 0; i < bmap->n_eq; ++i)
1460                 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1461
1462         for (i = 0; i < bmap->n_ineq; ++i)
1463                 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1464
1465         for (i = 0; i < bmap->n_div; ++i)
1466                 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1467         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1468 }
1469
1470 /* Eliminate the specified n dimensions starting at first from the
1471  * constraints using Fourier-Motzkin.  The dimensions themselves
1472  * are not removed.
1473  */
1474 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1475         enum isl_dim_type type, unsigned first, unsigned n)
1476 {
1477         int i;
1478
1479         if (!map)
1480                 return NULL;
1481         if (n == 0)
1482                 return map;
1483
1484         map = isl_map_cow(map);
1485         if (!map)
1486                 return NULL;
1487         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1488         first += pos(map->dim, type) - 1;
1489         
1490         for (i = 0; i < map->n; ++i) {
1491                 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1492                 if (!map->p[i])
1493                         goto error;
1494         }
1495         return map;
1496 error:
1497         isl_map_free(map);
1498         return NULL;
1499 }
1500
1501 /* Eliminate the specified n dimensions starting at first from the
1502  * constraints using Fourier-Motzkin.  The dimensions themselves
1503  * are not removed.
1504  */
1505 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1506         enum isl_dim_type type, unsigned first, unsigned n)
1507 {
1508         return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1509 }
1510
1511 /* Eliminate the specified n dimensions starting at first from the
1512  * constraints using Fourier-Motzkin.  The dimensions themselves
1513  * are not removed.
1514  */
1515 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1516         unsigned first, unsigned n)
1517 {
1518         return isl_set_eliminate(set, isl_dim_set, first, n);
1519 }
1520
1521 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1522         __isl_take isl_basic_map *bmap)
1523 {
1524         bmap = isl_basic_map_eliminate_vars(bmap, isl_dim_total(bmap->dim),
1525                                                 bmap->n_div);
1526         if (!bmap)
1527                 return NULL;
1528         bmap->n_div = 0;
1529         return isl_basic_map_finalize(bmap);
1530 }
1531
1532 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1533         __isl_take isl_basic_set *bset)
1534 {
1535         return (struct isl_basic_set *)isl_basic_map_remove_divs(
1536                         (struct isl_basic_map *)bset);
1537 }
1538
1539 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1540 {
1541         int i;
1542
1543         if (!map)
1544                 return NULL;
1545         if (map->n == 0)
1546                 return map;
1547
1548         map = isl_map_cow(map);
1549         if (!map)
1550                 return NULL;
1551         
1552         for (i = 0; i < map->n; ++i) {
1553                 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1554                 if (!map->p[i])
1555                         goto error;
1556         }
1557         return map;
1558 error:
1559         isl_map_free(map);
1560         return NULL;
1561 }
1562
1563 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1564 {
1565         return isl_map_remove_divs(set);
1566 }
1567
1568 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1569         enum isl_dim_type type, unsigned first, unsigned n)
1570 {
1571         if (!bmap)
1572                 return NULL;
1573         isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1574                         goto error);
1575         if (n == 0)
1576                 return bmap;
1577         bmap = isl_basic_map_eliminate_vars(bmap,
1578                         isl_basic_map_offset(bmap, type) - 1 + first, n);
1579         if (!bmap)
1580                 return bmap;
1581         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1582                 return bmap;
1583         bmap = isl_basic_map_drop(bmap, type, first, n);
1584         return bmap;
1585 error:
1586         isl_basic_map_free(bmap);
1587         return NULL;
1588 }
1589
1590 /* Return true if the definition of the given div (recursively) involves
1591  * any of the given variables.
1592  */
1593 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1594         unsigned first, unsigned n)
1595 {
1596         int i;
1597         unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1598
1599         if (isl_int_is_zero(bmap->div[div][0]))
1600                 return 0;
1601         if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1602                 return 1;
1603
1604         for (i = bmap->n_div - 1; i >= 0; --i) {
1605                 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1606                         continue;
1607                 if (div_involves_vars(bmap, i, first, n))
1608                         return 1;
1609         }
1610
1611         return 0;
1612 }
1613
1614 /* Remove all divs (recursively) involving any of the given dimensions
1615  * in their definitions.
1616  */
1617 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
1618         __isl_take isl_basic_map *bmap,
1619         enum isl_dim_type type, unsigned first, unsigned n)
1620 {
1621         int i;
1622
1623         if (!bmap)
1624                 return NULL;
1625         isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1626                         goto error);
1627         first += isl_basic_map_offset(bmap, type);
1628
1629         for (i = bmap->n_div - 1; i >= 0; --i) {
1630                 if (!div_involves_vars(bmap, i, first, n))
1631                         continue;
1632                 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1633                 if (!bmap)
1634                         return NULL;
1635                 i = bmap->n_div;
1636         }
1637
1638         return bmap;
1639 error:
1640         isl_basic_map_free(bmap);
1641         return NULL;
1642 }
1643
1644 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
1645         enum isl_dim_type type, unsigned first, unsigned n)
1646 {
1647         int i;
1648
1649         if (!map)
1650                 return NULL;
1651         if (map->n == 0)
1652                 return map;
1653
1654         map = isl_map_cow(map);
1655         if (!map)
1656                 return NULL;
1657
1658         for (i = 0; i < map->n; ++i) {
1659                 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
1660                                                                 type, first, n);
1661                 if (!map->p[i])
1662                         goto error;
1663         }
1664         return map;
1665 error:
1666         isl_map_free(map);
1667         return NULL;
1668 }
1669
1670 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
1671         enum isl_dim_type type, unsigned first, unsigned n)
1672 {
1673         return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
1674                                                               type, first, n);
1675 }
1676
1677 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
1678         enum isl_dim_type type, unsigned first, unsigned n)
1679 {
1680         int i;
1681
1682         if (!bmap)
1683                 return -1;
1684
1685         if (first + n > isl_basic_map_dim(bmap, type))
1686                 isl_die(bmap->ctx, isl_error_invalid,
1687                         "index out of bounds", return -1);
1688
1689         first += isl_basic_map_offset(bmap, type);
1690         for (i = 0; i < bmap->n_eq; ++i)
1691                 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
1692                         return 1;
1693         for (i = 0; i < bmap->n_ineq; ++i)
1694                 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
1695                         return 1;
1696
1697         return 0;
1698 }
1699
1700 int isl_map_involves_dims(__isl_keep isl_map *map,
1701         enum isl_dim_type type, unsigned first, unsigned n)
1702 {
1703         int i;
1704
1705         if (!map)
1706                 return -1;
1707
1708         if (first + n > isl_map_dim(map, type))
1709                 isl_die(map->ctx, isl_error_invalid,
1710                         "index out of bounds", return -1);
1711
1712         for (i = 0; i < map->n; ++i) {
1713                 int involves = isl_basic_map_involves_dims(map->p[i],
1714                                                             type, first, n);
1715                 if (involves < 0 || !involves)
1716                         return involves;
1717         }
1718
1719         return 1;
1720 }
1721
1722 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
1723         enum isl_dim_type type, unsigned first, unsigned n)
1724 {
1725         return isl_basic_map_involves_dims(bset, type, first, n);
1726 }
1727
1728 int isl_set_involves_dims(__isl_keep isl_set *set,
1729         enum isl_dim_type type, unsigned first, unsigned n)
1730 {
1731         return isl_map_involves_dims(set, type, first, n);
1732 }
1733
1734 /* Return true if the definition of the given div is unknown or depends
1735  * on unknown divs.
1736  */
1737 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
1738 {
1739         int i;
1740         unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1741
1742         if (isl_int_is_zero(bmap->div[div][0]))
1743                 return 1;
1744
1745         for (i = bmap->n_div - 1; i >= 0; --i) {
1746                 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1747                         continue;
1748                 if (div_is_unknown(bmap, i))
1749                         return 1;
1750         }
1751
1752         return 0;
1753 }
1754
1755 /* Remove all divs that are unknown or defined in terms of unknown divs.
1756  */
1757 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
1758         __isl_take isl_basic_map *bmap)
1759 {
1760         int i;
1761
1762         if (!bmap)
1763                 return NULL;
1764
1765         for (i = bmap->n_div - 1; i >= 0; --i) {
1766                 if (!div_is_unknown(bmap, i))
1767                         continue;
1768                 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
1769         }
1770
1771         return bmap;
1772 }
1773
1774 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
1775 {
1776         int i;
1777
1778         if (!map)
1779                 return NULL;
1780         if (map->n == 0)
1781                 return map;
1782
1783         map = isl_map_cow(map);
1784         if (!map)
1785                 return NULL;
1786
1787         for (i = 0; i < map->n; ++i) {
1788                 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
1789                 if (!map->p[i])
1790                         goto error;
1791         }
1792         return map;
1793 error:
1794         isl_map_free(map);
1795         return NULL;
1796 }
1797
1798 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
1799 {
1800         return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
1801 }
1802
1803 __isl_give isl_basic_set *isl_basic_set_remove_dims(
1804         __isl_take isl_basic_set *bset,
1805         enum isl_dim_type type, unsigned first, unsigned n)
1806 {
1807         return (isl_basic_set *)
1808             isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
1809 }
1810
1811 struct isl_map *isl_map_remove_dims(struct isl_map *map,
1812         enum isl_dim_type type, unsigned first, unsigned n)
1813 {
1814         int i;
1815
1816         if (n == 0)
1817                 return map;
1818
1819         map = isl_map_cow(map);
1820         if (!map)
1821                 return NULL;
1822         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1823         
1824         for (i = 0; i < map->n; ++i) {
1825                 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1826                         isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1827                 if (!map->p[i])
1828                         goto error;
1829         }
1830         map = isl_map_drop(map, type, first, n);
1831         return map;
1832 error:
1833         isl_map_free(map);
1834         return NULL;
1835 }
1836
1837 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
1838         enum isl_dim_type type, unsigned first, unsigned n)
1839 {
1840         return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
1841 }
1842
1843 /* Project out n inputs starting at first using Fourier-Motzkin */
1844 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1845         unsigned first, unsigned n)
1846 {
1847         return isl_map_remove_dims(map, isl_dim_in, first, n);
1848 }
1849
1850 static void dump_term(struct isl_basic_map *bmap,
1851                         isl_int c, int pos, FILE *out)
1852 {
1853         const char *name;
1854         unsigned in = isl_basic_map_n_in(bmap);
1855         unsigned dim = in + isl_basic_map_n_out(bmap);
1856         unsigned nparam = isl_basic_map_n_param(bmap);
1857         if (!pos)
1858                 isl_int_print(out, c, 0);
1859         else {
1860                 if (!isl_int_is_one(c))
1861                         isl_int_print(out, c, 0);
1862                 if (pos < 1 + nparam) {
1863                         name = isl_dim_get_name(bmap->dim,
1864                                                 isl_dim_param, pos - 1);
1865                         if (name)
1866                                 fprintf(out, "%s", name);
1867                         else
1868                                 fprintf(out, "p%d", pos - 1);
1869                 } else if (pos < 1 + nparam + in)
1870                         fprintf(out, "i%d", pos - 1 - nparam);
1871                 else if (pos < 1 + nparam + dim)
1872                         fprintf(out, "o%d", pos - 1 - nparam - in);
1873                 else
1874                         fprintf(out, "e%d", pos - 1 - nparam - dim);
1875         }
1876 }
1877
1878 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1879                                 int sign, FILE *out)
1880 {
1881         int i;
1882         int first;
1883         unsigned len = 1 + isl_basic_map_total_dim(bmap);
1884         isl_int v;
1885
1886         isl_int_init(v);
1887         for (i = 0, first = 1; i < len; ++i) {
1888                 if (isl_int_sgn(c[i]) * sign <= 0)
1889                         continue;
1890                 if (!first)
1891                         fprintf(out, " + ");
1892                 first = 0;
1893                 isl_int_abs(v, c[i]);
1894                 dump_term(bmap, v, i, out);
1895         }
1896         isl_int_clear(v);
1897         if (first)
1898                 fprintf(out, "0");
1899 }
1900
1901 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1902                                 const char *op, FILE *out, int indent)
1903 {
1904         int i;
1905
1906         fprintf(out, "%*s", indent, "");
1907
1908         dump_constraint_sign(bmap, c, 1, out);
1909         fprintf(out, " %s ", op);
1910         dump_constraint_sign(bmap, c, -1, out);
1911
1912         fprintf(out, "\n");
1913
1914         for (i = bmap->n_div; i < bmap->extra; ++i) {
1915                 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1916                         continue;
1917                 fprintf(out, "%*s", indent, "");
1918                 fprintf(out, "ERROR: unused div coefficient not zero\n");
1919                 abort();
1920         }
1921 }
1922
1923 static void dump_constraints(struct isl_basic_map *bmap,
1924                                 isl_int **c, unsigned n,
1925                                 const char *op, FILE *out, int indent)
1926 {
1927         int i;
1928
1929         for (i = 0; i < n; ++i)
1930                 dump_constraint(bmap, c[i], op, out, indent);
1931 }
1932
1933 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1934 {
1935         int j;
1936         int first = 1;
1937         unsigned total = isl_basic_map_total_dim(bmap);
1938
1939         for (j = 0; j < 1 + total; ++j) {
1940                 if (isl_int_is_zero(exp[j]))
1941                         continue;
1942                 if (!first && isl_int_is_pos(exp[j]))
1943                         fprintf(out, "+");
1944                 dump_term(bmap, exp[j], j, out);
1945                 first = 0;
1946         }
1947 }
1948
1949 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1950 {
1951         int i;
1952
1953         dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1954         dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1955
1956         for (i = 0; i < bmap->n_div; ++i) {
1957                 fprintf(out, "%*s", indent, "");
1958                 fprintf(out, "e%d = [(", i);
1959                 dump_affine(bmap, bmap->div[i]+1, out);
1960                 fprintf(out, ")/");
1961                 isl_int_print(out, bmap->div[i][0], 0);
1962                 fprintf(out, "]\n");
1963         }
1964 }
1965
1966 void isl_basic_set_print_internal(struct isl_basic_set *bset,
1967         FILE *out, int indent)
1968 {
1969         if (!bset) {
1970                 fprintf(out, "null basic set\n");
1971                 return;
1972         }
1973
1974         fprintf(out, "%*s", indent, "");
1975         fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1976                         bset->ref, bset->dim->nparam, bset->dim->n_out,
1977                         bset->extra, bset->flags);
1978         dump((struct isl_basic_map *)bset, out, indent);
1979 }
1980
1981 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
1982         FILE *out, int indent)
1983 {
1984         if (!bmap) {
1985                 fprintf(out, "null basic map\n");
1986                 return;
1987         }
1988
1989         fprintf(out, "%*s", indent, "");
1990         fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1991                         "flags: %x, n_name: %d\n",
1992                 bmap->ref,
1993                 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1994                 bmap->extra, bmap->flags, bmap->dim->n_id);
1995         dump(bmap, out, indent);
1996 }
1997
1998 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1999 {
2000         unsigned total;
2001         if (!bmap)
2002                 return -1;
2003         total = isl_basic_map_total_dim(bmap);
2004         isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2005         isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2006         isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2007         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2008         return 0;
2009 }
2010
2011 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2012 {
2013         struct isl_set *set;
2014
2015         if (!dim)
2016                 return NULL;
2017         isl_assert(dim->ctx, dim->n_in == 0, goto error);
2018         isl_assert(dim->ctx, n >= 0, goto error);
2019         set = isl_alloc(dim->ctx, struct isl_set,
2020                         sizeof(struct isl_set) +
2021                         (n - 1) * sizeof(struct isl_basic_set *));
2022         if (!set)
2023                 goto error;
2024
2025         set->ctx = dim->ctx;
2026         isl_ctx_ref(set->ctx);
2027         set->ref = 1;
2028         set->size = n;
2029         set->n = 0;
2030         set->dim = dim;
2031         set->flags = flags;
2032         return set;
2033 error:
2034         isl_dim_free(dim);
2035         return NULL;
2036 }
2037
2038 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2039                 unsigned nparam, unsigned dim, int n, unsigned flags)
2040 {
2041         struct isl_set *set;
2042         struct isl_dim *dims;
2043
2044         dims = isl_dim_alloc(ctx, nparam, 0, dim);
2045         if (!dims)
2046                 return NULL;
2047
2048         set = isl_set_alloc_dim(dims, n, flags);
2049         return set;
2050 }
2051
2052 /* Make sure "map" has room for at least "n" more basic maps.
2053  */
2054 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2055 {
2056         int i;
2057         struct isl_map *grown = NULL;
2058
2059         if (!map)
2060                 return NULL;
2061         isl_assert(map->ctx, n >= 0, goto error);
2062         if (map->n + n <= map->size)
2063                 return map;
2064         grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
2065         if (!grown)
2066                 goto error;
2067         for (i = 0; i < map->n; ++i) {
2068                 grown->p[i] = isl_basic_map_copy(map->p[i]);
2069                 if (!grown->p[i])
2070                         goto error;
2071                 grown->n++;
2072         }
2073         isl_map_free(map);
2074         return grown;
2075 error:
2076         isl_map_free(grown);
2077         isl_map_free(map);
2078         return NULL;
2079 }
2080
2081 /* Make sure "set" has room for at least "n" more basic sets.
2082  */
2083 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2084 {
2085         return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2086 }
2087
2088 struct isl_set *isl_set_dup(struct isl_set *set)
2089 {
2090         int i;
2091         struct isl_set *dup;
2092
2093         if (!set)
2094                 return NULL;
2095
2096         dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
2097         if (!dup)
2098                 return NULL;
2099         for (i = 0; i < set->n; ++i)
2100                 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2101         return dup;
2102 }
2103
2104 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2105 {
2106         return isl_map_from_basic_map(bset);
2107 }
2108
2109 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2110 {
2111         struct isl_map *map;
2112
2113         if (!bmap)
2114                 return NULL;
2115
2116         map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2117         return isl_map_add_basic_map(map, bmap);
2118 }
2119
2120 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2121                                                 __isl_take isl_basic_set *bset)
2122 {
2123         return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2124                                                 (struct isl_basic_map *)bset);
2125 }
2126
2127 void isl_set_free(struct isl_set *set)
2128 {
2129         int i;
2130
2131         if (!set)
2132                 return;
2133
2134         if (--set->ref > 0)
2135                 return;
2136
2137         isl_ctx_deref(set->ctx);
2138         for (i = 0; i < set->n; ++i)
2139                 isl_basic_set_free(set->p[i]);
2140         isl_dim_free(set->dim);
2141         free(set);
2142 }
2143
2144 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2145 {
2146         int i;
2147
2148         if (!set) {
2149                 fprintf(out, "null set\n");
2150                 return;
2151         }
2152
2153         fprintf(out, "%*s", indent, "");
2154         fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2155                         set->ref, set->n, set->dim->nparam, set->dim->n_out,
2156                         set->flags);
2157         for (i = 0; i < set->n; ++i) {
2158                 fprintf(out, "%*s", indent, "");
2159                 fprintf(out, "basic set %d:\n", i);
2160                 isl_basic_set_print_internal(set->p[i], out, indent+4);
2161         }
2162 }
2163
2164 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2165 {
2166         int i;
2167
2168         if (!map) {
2169                 fprintf(out, "null map\n");
2170                 return;
2171         }
2172
2173         fprintf(out, "%*s", indent, "");
2174         fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2175                      "flags: %x, n_name: %d\n",
2176                         map->ref, map->n, map->dim->nparam, map->dim->n_in,
2177                         map->dim->n_out, map->flags, map->dim->n_id);
2178         for (i = 0; i < map->n; ++i) {
2179                 fprintf(out, "%*s", indent, "");
2180                 fprintf(out, "basic map %d:\n", i);
2181                 isl_basic_map_print_internal(map->p[i], out, indent+4);
2182         }
2183 }
2184
2185 struct isl_basic_map *isl_basic_map_intersect_domain(
2186                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2187 {
2188         struct isl_basic_map *bmap_domain;
2189
2190         if (!bmap || !bset)
2191                 goto error;
2192
2193         isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2194                                         bset->dim, isl_dim_param), goto error);
2195
2196         if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2197                 isl_assert(bset->ctx,
2198                     isl_basic_map_compatible_domain(bmap, bset), goto error);
2199
2200         bmap = isl_basic_map_cow(bmap);
2201         if (!bmap)
2202                 goto error;
2203         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2204                         bset->n_div, bset->n_eq, bset->n_ineq);
2205         bmap_domain = isl_basic_map_from_domain(bset);
2206         bmap = add_constraints(bmap, bmap_domain, 0, 0);
2207
2208         bmap = isl_basic_map_simplify(bmap);
2209         return isl_basic_map_finalize(bmap);
2210 error:
2211         isl_basic_map_free(bmap);
2212         isl_basic_set_free(bset);
2213         return NULL;
2214 }
2215
2216 struct isl_basic_map *isl_basic_map_intersect_range(
2217                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2218 {
2219         struct isl_basic_map *bmap_range;
2220
2221         if (!bmap || !bset)
2222                 goto error;
2223
2224         isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
2225                                         bset->dim, isl_dim_param), goto error);
2226
2227         if (isl_dim_size(bset->dim, isl_dim_set) != 0)
2228                 isl_assert(bset->ctx,
2229                     isl_basic_map_compatible_range(bmap, bset), goto error);
2230
2231         if (isl_basic_set_is_universe(bset)) {
2232                 isl_basic_set_free(bset);
2233                 return bmap;
2234         }
2235
2236         bmap = isl_basic_map_cow(bmap);
2237         if (!bmap)
2238                 goto error;
2239         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
2240                         bset->n_div, bset->n_eq, bset->n_ineq);
2241         bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2242         bmap = add_constraints(bmap, bmap_range, 0, 0);
2243
2244         bmap = isl_basic_map_simplify(bmap);
2245         return isl_basic_map_finalize(bmap);
2246 error:
2247         isl_basic_map_free(bmap);
2248         isl_basic_set_free(bset);
2249         return NULL;
2250 }
2251
2252 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2253 {
2254         int i;
2255         unsigned total;
2256         isl_int s;
2257
2258         total = 1 + isl_basic_map_total_dim(bmap);
2259         if (total != vec->size)
2260                 return -1;
2261
2262         isl_int_init(s);
2263
2264         for (i = 0; i < bmap->n_eq; ++i) {
2265                 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2266                 if (!isl_int_is_zero(s)) {
2267                         isl_int_clear(s);
2268                         return 0;
2269                 }
2270         }
2271
2272         for (i = 0; i < bmap->n_ineq; ++i) {
2273                 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2274                 if (isl_int_is_neg(s)) {
2275                         isl_int_clear(s);
2276                         return 0;
2277                 }
2278         }
2279
2280         isl_int_clear(s);
2281
2282         return 1;
2283 }
2284
2285 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2286 {
2287         return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2288 }
2289
2290 struct isl_basic_map *isl_basic_map_intersect(
2291                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2292 {
2293         struct isl_vec *sample = NULL;
2294
2295         if (!bmap1 || !bmap2)
2296                 goto error;
2297
2298         isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
2299                                      bmap2->dim, isl_dim_param), goto error);
2300         if (isl_dim_total(bmap1->dim) ==
2301                                 isl_dim_size(bmap1->dim, isl_dim_param) &&
2302             isl_dim_total(bmap2->dim) !=
2303                                 isl_dim_size(bmap2->dim, isl_dim_param))
2304                 return isl_basic_map_intersect(bmap2, bmap1);
2305
2306         if (isl_dim_total(bmap2->dim) !=
2307                                         isl_dim_size(bmap2->dim, isl_dim_param))
2308                 isl_assert(bmap1->ctx,
2309                             isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
2310
2311         if (bmap1->sample &&
2312             isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2313             isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2314                 sample = isl_vec_copy(bmap1->sample);
2315         else if (bmap2->sample &&
2316             isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2317             isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2318                 sample = isl_vec_copy(bmap2->sample);
2319
2320         bmap1 = isl_basic_map_cow(bmap1);
2321         if (!bmap1)
2322                 goto error;
2323         bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
2324                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2325         bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2326
2327         if (!bmap1)
2328                 isl_vec_free(sample);
2329         else if (sample) {
2330                 isl_vec_free(bmap1->sample);
2331                 bmap1->sample = sample;
2332         }
2333
2334         bmap1 = isl_basic_map_simplify(bmap1);
2335         return isl_basic_map_finalize(bmap1);
2336 error:
2337         if (sample)
2338                 isl_vec_free(sample);
2339         isl_basic_map_free(bmap1);
2340         isl_basic_map_free(bmap2);
2341         return NULL;
2342 }
2343
2344 struct isl_basic_set *isl_basic_set_intersect(
2345                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2346 {
2347         return (struct isl_basic_set *)
2348                 isl_basic_map_intersect(
2349                         (struct isl_basic_map *)bset1,
2350                         (struct isl_basic_map *)bset2);
2351 }
2352
2353 /* Special case of isl_map_intersect, where both map1 and map2
2354  * are convex, without any divs and such that either map1 or map2
2355  * contains a single constraint.  This constraint is then simply
2356  * added to the other map.
2357  */
2358 static __isl_give isl_map *map_intersect_add_constraint(
2359         __isl_take isl_map *map1, __isl_take isl_map *map2)
2360 {
2361         isl_assert(map1->ctx, map1->n == 1, goto error);
2362         isl_assert(map2->ctx, map1->n == 1, goto error);
2363         isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2364         isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2365
2366         if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2367                 return isl_map_intersect(map2, map1);
2368
2369         isl_assert(map2->ctx,
2370                     map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2371
2372         map1 = isl_map_cow(map1);
2373         if (!map1)
2374                 goto error;
2375         if (isl_map_plain_is_empty(map1)) {
2376                 isl_map_free(map2);
2377                 return map1;
2378         }
2379         map1->p[0] = isl_basic_map_cow(map1->p[0]);
2380         if (map2->p[0]->n_eq == 1)
2381                 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2382         else
2383                 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2384                                                         map2->p[0]->ineq[0]);
2385
2386         map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2387         map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2388         if (!map1->p[0])
2389                 goto error;
2390
2391         if (isl_basic_map_plain_is_empty(map1->p[0])) {
2392                 isl_basic_map_free(map1->p[0]);
2393                 map1->n = 0;
2394         }
2395
2396         isl_map_free(map2);
2397
2398         return map1;
2399 error:
2400         isl_map_free(map1);
2401         isl_map_free(map2);
2402         return NULL;
2403 }
2404
2405 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2406         __isl_take isl_map *map2)
2407 {
2408         unsigned flags = 0;
2409         struct isl_map *result;
2410         int i, j;
2411
2412         if (!map1 || !map2)
2413                 goto error;
2414
2415         if (isl_map_plain_is_empty(map1) &&
2416             isl_dim_equal(map1->dim, map2->dim)) {
2417                 isl_map_free(map2);
2418                 return map1;
2419         }
2420         if (isl_map_plain_is_empty(map2) &&
2421             isl_dim_equal(map1->dim, map2->dim)) {
2422                 isl_map_free(map1);
2423                 return map2;
2424         }
2425
2426         if (map1->n == 1 && map2->n == 1 &&
2427             map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2428             isl_dim_equal(map1->dim, map2->dim) &&
2429             (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2430              map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2431                 return map_intersect_add_constraint(map1, map2);
2432         if (isl_dim_total(map1->dim) ==
2433                                 isl_dim_size(map1->dim, isl_dim_param) &&
2434             isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2435                 return isl_map_intersect(map2, map1);
2436
2437         if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2438                 isl_assert(map1->ctx,
2439                             isl_dim_equal(map1->dim, map2->dim), goto error);
2440
2441         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2442             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2443                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2444
2445         result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2446                                 map1->n * map2->n, flags);
2447         if (!result)
2448                 goto error;
2449         for (i = 0; i < map1->n; ++i)
2450                 for (j = 0; j < map2->n; ++j) {
2451                         struct isl_basic_map *part;
2452                         part = isl_basic_map_intersect(
2453                                     isl_basic_map_copy(map1->p[i]),
2454                                     isl_basic_map_copy(map2->p[j]));
2455                         if (isl_basic_map_is_empty(part))
2456                                 isl_basic_map_free(part);
2457                         else
2458                                 result = isl_map_add_basic_map(result, part);
2459                         if (!result)
2460                                 goto error;
2461                 }
2462         isl_map_free(map1);
2463         isl_map_free(map2);
2464         return result;
2465 error:
2466         isl_map_free(map1);
2467         isl_map_free(map2);
2468         return NULL;
2469 }
2470
2471 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2472         __isl_take isl_map *map2)
2473 {
2474         return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2475 }
2476
2477 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2478 {
2479         return (struct isl_set *)
2480                 isl_map_intersect((struct isl_map *)set1,
2481                                   (struct isl_map *)set2);
2482 }
2483
2484 /* The current implementation of isl_map_intersect accepts intersections
2485  * with parameter domains, so we can just call that for now.
2486  */
2487 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2488                 __isl_take isl_set *params)
2489 {
2490         return isl_map_intersect(map, params);
2491 }
2492
2493 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2494         __isl_take isl_map *map2)
2495 {
2496         return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2497 }
2498
2499 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2500                 __isl_take isl_set *params)
2501 {
2502         return isl_map_intersect_params(set, params);
2503 }
2504
2505 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2506 {
2507         struct isl_dim *dim;
2508         struct isl_basic_set *bset;
2509         unsigned in;
2510
2511         if (!bmap)
2512                 return NULL;
2513         bmap = isl_basic_map_cow(bmap);
2514         if (!bmap)
2515                 return NULL;
2516         dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2517         in = isl_basic_map_n_in(bmap);
2518         bset = isl_basic_set_from_basic_map(bmap);
2519         bset = isl_basic_set_swap_vars(bset, in);
2520         return isl_basic_map_from_basic_set(bset, dim);
2521 }
2522
2523 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2524                 enum isl_dim_type type, unsigned pos, unsigned n)
2525 {
2526         struct isl_dim *res_dim;
2527         struct isl_basic_map *res;
2528         struct isl_dim_map *dim_map;
2529         unsigned total, off;
2530         enum isl_dim_type t;
2531
2532         if (n == 0)
2533                 return bmap;
2534
2535         if (!bmap)
2536                 return NULL;
2537
2538         res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2539
2540         total = isl_basic_map_total_dim(bmap) + n;
2541         dim_map = isl_dim_map_alloc(bmap->ctx, total);
2542         off = 0;
2543         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2544                 if (t != type) {
2545                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
2546                 } else {
2547                         unsigned size = isl_basic_map_dim(bmap, t);
2548                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2549                                                 0, pos, off);
2550                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2551                                                 pos, size - pos, off + pos + n);
2552                 }
2553                 off += isl_dim_size(res_dim, t);
2554         }
2555         isl_dim_map_div(dim_map, bmap, off);
2556
2557         res = isl_basic_map_alloc_dim(res_dim,
2558                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
2559         if (isl_basic_map_is_rational(bmap))
2560                 res = isl_basic_map_set_rational(res);
2561         res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2562         return isl_basic_map_finalize(res);
2563 }
2564
2565 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2566                 enum isl_dim_type type, unsigned n)
2567 {
2568         if (!bmap)
2569                 return NULL;
2570         return isl_basic_map_insert(bmap, type,
2571                                         isl_basic_map_dim(bmap, type), n);
2572 }
2573
2574 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2575                 enum isl_dim_type type, unsigned n)
2576 {
2577         if (!bset)
2578                 return NULL;
2579         isl_assert(bset->ctx, type != isl_dim_in, goto error);
2580         return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2581 error:
2582         isl_basic_set_free(bset);
2583         return NULL;
2584 }
2585
2586 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2587                 enum isl_dim_type type, unsigned pos, unsigned n)
2588 {
2589         int i;
2590
2591         if (n == 0)
2592                 return map;
2593
2594         map = isl_map_cow(map);
2595         if (!map)
2596                 return NULL;
2597
2598         map->dim = isl_dim_insert(map->dim, type, pos, n);
2599         if (!map->dim)
2600                 goto error;
2601
2602         for (i = 0; i < map->n; ++i) {
2603                 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2604                 if (!map->p[i])
2605                         goto error;
2606         }
2607
2608         return map;
2609 error:
2610         isl_map_free(map);
2611         return NULL;
2612 }
2613
2614 __isl_give isl_set *isl_set_insert(__isl_take isl_set *set,
2615                 enum isl_dim_type type, unsigned pos, unsigned n)
2616 {
2617         return (isl_set *)isl_map_insert((isl_map *)set, type, pos, n);
2618 }
2619
2620 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
2621                 enum isl_dim_type type, unsigned n)
2622 {
2623         if (!map)
2624                 return NULL;
2625         return isl_map_insert(map, type, isl_map_dim(map, type), n);
2626 }
2627
2628 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
2629                 enum isl_dim_type type, unsigned n)
2630 {
2631         if (!set)
2632                 return NULL;
2633         isl_assert(set->ctx, type != isl_dim_in, goto error);
2634         return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
2635 error:
2636         isl_set_free(set);
2637         return NULL;
2638 }
2639
2640 __isl_give isl_basic_map *isl_basic_map_move_dims(
2641         __isl_take isl_basic_map *bmap,
2642         enum isl_dim_type dst_type, unsigned dst_pos,
2643         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2644 {
2645         struct isl_dim_map *dim_map;
2646         struct isl_basic_map *res;
2647         enum isl_dim_type t;
2648         unsigned total, off;
2649
2650         if (!bmap)
2651                 return NULL;
2652         if (n == 0)
2653                 return bmap;
2654
2655         isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2656                 goto error);
2657
2658         if (dst_type == src_type && dst_pos == src_pos)
2659                 return bmap;
2660
2661         isl_assert(bmap->ctx, dst_type != src_type, goto error);
2662
2663         if (pos(bmap->dim, dst_type) + dst_pos ==
2664             pos(bmap->dim, src_type) + src_pos +
2665                                             ((src_type < dst_type) ? n : 0)) {
2666                 bmap = isl_basic_map_cow(bmap);
2667                 if (!bmap)
2668                         return NULL;
2669
2670                 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2671                                                 src_type, src_pos, n);
2672                 if (!bmap->dim)
2673                         goto error;
2674
2675                 bmap = isl_basic_map_finalize(bmap);
2676
2677                 return bmap;
2678         }
2679
2680         total = isl_basic_map_total_dim(bmap);
2681         dim_map = isl_dim_map_alloc(bmap->ctx, total);
2682
2683         off = 0;
2684         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2685                 unsigned size = isl_dim_size(bmap->dim, t);
2686                 if (t == dst_type) {
2687                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2688                                             0, dst_pos, off);
2689                         off += dst_pos;
2690                         isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2691                                             src_pos, n, off);
2692                         off += n;
2693                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2694                                             dst_pos, size - dst_pos, off);
2695                         off += size - dst_pos;
2696                 } else if (t == src_type) {
2697                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2698                                             0, src_pos, off);
2699                         off += src_pos;
2700                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2701                                         src_pos + n, size - src_pos - n, off);
2702                         off += size - src_pos - n;
2703                 } else {
2704                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
2705                         off += size;
2706                 }
2707         }
2708         isl_dim_map_div(dim_map, bmap, off);
2709
2710         res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2711                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
2712         bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2713
2714         bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2715                                         src_type, src_pos, n);
2716         if (!bmap->dim)
2717                 goto error;
2718
2719         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2720         bmap = isl_basic_map_gauss(bmap, NULL);
2721         bmap = isl_basic_map_finalize(bmap);
2722
2723         return bmap;
2724 error:
2725         isl_basic_map_free(bmap);
2726         return NULL;
2727 }
2728
2729 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2730         enum isl_dim_type dst_type, unsigned dst_pos,
2731         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2732 {
2733         return (isl_basic_set *)isl_basic_map_move_dims(
2734                 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2735 }
2736
2737 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2738         enum isl_dim_type dst_type, unsigned dst_pos,
2739         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2740 {
2741         if (!set)
2742                 return NULL;
2743         isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2744         return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2745                                         src_type, src_pos, n);
2746 error:
2747         isl_set_free(set);
2748         return NULL;
2749 }
2750
2751 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2752         enum isl_dim_type dst_type, unsigned dst_pos,
2753         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2754 {
2755         int i;
2756
2757         if (!map)
2758                 return NULL;
2759         if (n == 0)
2760                 return map;
2761
2762         isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2763                 goto error);
2764
2765         if (dst_type == src_type && dst_pos == src_pos)
2766                 return map;
2767
2768         isl_assert(map->ctx, dst_type != src_type, goto error);
2769
2770         map = isl_map_cow(map);
2771         if (!map)
2772                 return NULL;
2773
2774         map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2775         if (!map->dim)
2776                 goto error;
2777
2778         for (i = 0; i < map->n; ++i) {
2779                 map->p[i] = isl_basic_map_move_dims(map->p[i],
2780                                                 dst_type, dst_pos,
2781                                                 src_type, src_pos, n);
2782                 if (!map->p[i])
2783                         goto error;
2784         }
2785
2786         return map;
2787 error:
2788         isl_map_free(map);
2789         return NULL;
2790 }
2791
2792 /* Move the specified dimensions to the last columns right before
2793  * the divs.  Don't change the dimension specification of bmap.
2794  * That's the responsibility of the caller.
2795  */
2796 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2797         enum isl_dim_type type, unsigned first, unsigned n)
2798 {
2799         struct isl_dim_map *dim_map;
2800         struct isl_basic_map *res;
2801         enum isl_dim_type t;
2802         unsigned total, off;
2803
2804         if (!bmap)
2805                 return NULL;
2806         if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2807                 return bmap;
2808
2809         total = isl_basic_map_total_dim(bmap);
2810         dim_map = isl_dim_map_alloc(bmap->ctx, total);
2811
2812         off = 0;
2813         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2814                 unsigned size = isl_dim_size(bmap->dim, t);
2815                 if (t == type) {
2816                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2817                                             0, first, off);
2818                         off += first;
2819                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2820                                             first, n, total - bmap->n_div - n);
2821                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2822                                             first + n, size - (first + n), off);
2823                         off += size - (first + n);
2824                 } else {
2825                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
2826                         off += size;
2827                 }
2828         }
2829         isl_dim_map_div(dim_map, bmap, off + n);
2830
2831         res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2832                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
2833         res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
2834         return res;
2835 }
2836
2837 /* Turn the n dimensions of type type, starting at first
2838  * into existentially quantified variables.
2839  */
2840 __isl_give isl_basic_map *isl_basic_map_project_out(
2841                 __isl_take isl_basic_map *bmap,
2842                 enum isl_dim_type type, unsigned first, unsigned n)
2843 {
2844         int i;
2845         size_t row_size;
2846         isl_int **new_div;
2847         isl_int *old;
2848
2849         if (n == 0)
2850                 return bmap;
2851
2852         if (!bmap)
2853                 return NULL;
2854
2855         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2856                 return isl_basic_map_remove_dims(bmap, type, first, n);
2857
2858         isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2859                         goto error);
2860
2861         bmap = move_last(bmap, type, first, n);
2862         bmap = isl_basic_map_cow(bmap);
2863         if (!bmap)
2864                 return NULL;
2865
2866         row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2867         old = bmap->block2.data;
2868         bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2869                                         (bmap->extra + n) * (1 + row_size));
2870         if (!bmap->block2.data)
2871                 goto error;
2872         new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2873         if (!new_div)
2874                 goto error;
2875         for (i = 0; i < n; ++i) {
2876                 new_div[i] = bmap->block2.data +
2877                                 (bmap->extra + i) * (1 + row_size);
2878                 isl_seq_clr(new_div[i], 1 + row_size);
2879         }
2880         for (i = 0; i < bmap->extra; ++i)
2881                 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2882         free(bmap->div);
2883         bmap->div = new_div;
2884         bmap->n_div += n;
2885         bmap->extra += n;
2886
2887         bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2888         if (!bmap->dim)
2889                 goto error;
2890         bmap = isl_basic_map_simplify(bmap);
2891         bmap = isl_basic_map_drop_redundant_divs(bmap);
2892         return isl_basic_map_finalize(bmap);
2893 error:
2894         isl_basic_map_free(bmap);
2895         return NULL;
2896 }
2897
2898 /* Turn the n dimensions of type type, starting at first
2899  * into existentially quantified variables.
2900  */
2901 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2902                 enum isl_dim_type type, unsigned first, unsigned n)
2903 {
2904         return (isl_basic_set *)isl_basic_map_project_out(
2905                         (isl_basic_map *)bset, type, first, n);
2906 }
2907
2908 /* Turn the n dimensions of type type, starting at first
2909  * into existentially quantified variables.
2910  */
2911 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2912                 enum isl_dim_type type, unsigned first, unsigned n)
2913 {
2914         int i;
2915
2916         if (!map)
2917                 return NULL;
2918
2919         if (n == 0)
2920                 return map;
2921
2922         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2923
2924         map = isl_map_cow(map);
2925         if (!map)
2926                 return NULL;
2927
2928         map->dim = isl_dim_drop(map->dim, type, first, n);
2929         if (!map->dim)
2930                 goto error;
2931
2932         for (i = 0; i < map->n; ++i) {
2933                 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2934                 if (!map->p[i])
2935                         goto error;
2936         }
2937
2938         return map;
2939 error:
2940         isl_map_free(map);
2941         return NULL;
2942 }
2943
2944 /* Turn the n dimensions of type type, starting at first
2945  * into existentially quantified variables.
2946  */
2947 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2948                 enum isl_dim_type type, unsigned first, unsigned n)
2949 {
2950         return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2951 }
2952
2953 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2954 {
2955         int i, j;
2956
2957         for (i = 0; i < n; ++i) {
2958                 j = isl_basic_map_alloc_div(bmap);
2959                 if (j < 0)
2960                         goto error;
2961                 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2962         }
2963         return bmap;
2964 error:
2965         isl_basic_map_free(bmap);
2966         return NULL;
2967 }
2968
2969 struct isl_basic_map *isl_basic_map_apply_range(
2970                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2971 {
2972         struct isl_dim *dim_result = NULL;
2973         struct isl_basic_map *bmap;
2974         unsigned n_in, n_out, n, nparam, total, pos;
2975         struct isl_dim_map *dim_map1, *dim_map2;
2976
2977         if (!bmap1 || !bmap2)
2978                 goto error;
2979
2980         dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2981                                   isl_dim_copy(bmap2->dim));
2982
2983         n_in = isl_basic_map_n_in(bmap1);
2984         n_out = isl_basic_map_n_out(bmap2);
2985         n = isl_basic_map_n_out(bmap1);
2986         nparam = isl_basic_map_n_param(bmap1);
2987
2988         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2989         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2990         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2991         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2992         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2993         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2994         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2995         isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2996         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2997         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2998         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2999
3000         bmap = isl_basic_map_alloc_dim(dim_result,
3001                         bmap1->n_div + bmap2->n_div + n,
3002                         bmap1->n_eq + bmap2->n_eq,
3003                         bmap1->n_ineq + bmap2->n_ineq);
3004         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3005         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3006         bmap = add_divs(bmap, n);
3007         bmap = isl_basic_map_simplify(bmap);
3008         bmap = isl_basic_map_drop_redundant_divs(bmap);
3009         return isl_basic_map_finalize(bmap);
3010 error:
3011         isl_basic_map_free(bmap1);
3012         isl_basic_map_free(bmap2);
3013         return NULL;
3014 }
3015
3016 struct isl_basic_set *isl_basic_set_apply(
3017                 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3018 {
3019         if (!bset || !bmap)
3020                 goto error;
3021
3022         isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3023                     goto error);
3024
3025         return (struct isl_basic_set *)
3026                 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3027 error:
3028         isl_basic_set_free(bset);
3029         isl_basic_map_free(bmap);
3030         return NULL;
3031 }
3032
3033 struct isl_basic_map *isl_basic_map_apply_domain(
3034                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3035 {
3036         if (!bmap1 || !bmap2)
3037                 goto error;
3038
3039         isl_assert(bmap1->ctx,
3040             isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3041         isl_assert(bmap1->ctx,
3042             isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3043             goto error);
3044
3045         bmap1 = isl_basic_map_reverse(bmap1);
3046         bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3047         return isl_basic_map_reverse(bmap1);
3048 error:
3049         isl_basic_map_free(bmap1);
3050         isl_basic_map_free(bmap2);
3051         return NULL;
3052 }
3053
3054 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3055  * A \cap B -> f(A) + f(B)
3056  */
3057 struct isl_basic_map *isl_basic_map_sum(
3058                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3059 {
3060         unsigned n_in, n_out, nparam, total, pos;
3061         struct isl_basic_map *bmap = NULL;
3062         struct isl_dim_map *dim_map1, *dim_map2;
3063         int i;
3064
3065         if (!bmap1 || !bmap2)
3066                 goto error;
3067
3068         isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
3069                 goto error);
3070
3071         nparam = isl_basic_map_n_param(bmap1);
3072         n_in = isl_basic_map_n_in(bmap1);
3073         n_out = isl_basic_map_n_out(bmap1);
3074
3075         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3076         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3077         dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3078         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3079         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3080         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3081         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3082         isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3083         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3084         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3085         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3086
3087         bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
3088                         bmap1->n_div + bmap2->n_div + 2 * n_out,
3089                         bmap1->n_eq + bmap2->n_eq + n_out,
3090                         bmap1->n_ineq + bmap2->n_ineq);
3091         for (i = 0; i < n_out; ++i) {
3092                 int j = isl_basic_map_alloc_equality(bmap);
3093                 if (j < 0)
3094                         goto error;
3095                 isl_seq_clr(bmap->eq[j], 1+total);
3096                 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3097                 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3098                 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3099         }
3100         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3101         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3102         bmap = add_divs(bmap, 2 * n_out);
3103
3104         bmap = isl_basic_map_simplify(bmap);
3105         return isl_basic_map_finalize(bmap);
3106 error:
3107         isl_basic_map_free(bmap);
3108         isl_basic_map_free(bmap1);
3109         isl_basic_map_free(bmap2);
3110         return NULL;
3111 }
3112
3113 /* Given two maps A -> f(A) and B -> g(B), construct a map
3114  * A \cap B -> f(A) + f(B)
3115  */
3116 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3117 {
3118         struct isl_map *result;
3119         int i, j;
3120
3121         if (!map1 || !map2)
3122                 goto error;
3123
3124         isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3125
3126         result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3127                                 map1->n * map2->n, 0);
3128         if (!result)
3129                 goto error;
3130         for (i = 0; i < map1->n; ++i)
3131                 for (j = 0; j < map2->n; ++j) {
3132                         struct isl_basic_map *part;
3133                         part = isl_basic_map_sum(
3134                                     isl_basic_map_copy(map1->p[i]),
3135                                     isl_basic_map_copy(map2->p[j]));
3136                         if (isl_basic_map_is_empty(part))
3137                                 isl_basic_map_free(part);
3138                         else
3139                                 result = isl_map_add_basic_map(result, part);
3140                         if (!result)
3141                                 goto error;
3142                 }
3143         isl_map_free(map1);
3144         isl_map_free(map2);
3145         return result;
3146 error:
3147         isl_map_free(map1);
3148         isl_map_free(map2);
3149         return NULL;
3150 }
3151
3152 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3153         __isl_take isl_set *set2)
3154 {
3155         return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3156 }
3157
3158 /* Given a basic map A -> f(A), construct A -> -f(A).
3159  */
3160 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3161 {
3162         int i, j;
3163         unsigned off, n;
3164
3165         bmap = isl_basic_map_cow(bmap);
3166         if (!bmap)
3167                 return NULL;
3168
3169         n = isl_basic_map_dim(bmap, isl_dim_out);
3170         off = isl_basic_map_offset(bmap, isl_dim_out);
3171         for (i = 0; i < bmap->n_eq; ++i)
3172                 for (j = 0; j < n; ++j)
3173                         isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3174         for (i = 0; i < bmap->n_ineq; ++i)
3175                 for (j = 0; j < n; ++j)
3176                         isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3177         for (i = 0; i < bmap->n_div; ++i)
3178                 for (j = 0; j < n; ++j)
3179                         isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3180         return isl_basic_map_finalize(bmap);
3181 }
3182
3183 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3184 {
3185         return isl_basic_map_neg(bset);
3186 }
3187
3188 /* Given a map A -> f(A), construct A -> -f(A).
3189  */
3190 struct isl_map *isl_map_neg(struct isl_map *map)
3191 {
3192         int i;
3193
3194         map = isl_map_cow(map);
3195         if (!map)
3196                 return NULL;
3197
3198         for (i = 0; i < map->n; ++i) {
3199                 map->p[i] = isl_basic_map_neg(map->p[i]);
3200                 if (!map->p[i])
3201                         goto error;
3202         }
3203
3204         return map;
3205 error:
3206         isl_map_free(map);
3207         return NULL;
3208 }
3209
3210 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3211 {
3212         return (isl_set *)isl_map_neg((isl_map *)set);
3213 }
3214
3215 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3216  * A -> floor(f(A)/d).
3217  */
3218 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3219                 isl_int d)
3220 {
3221         unsigned n_in, n_out, nparam, total, pos;
3222         struct isl_basic_map *result = NULL;
3223         struct isl_dim_map *dim_map;
3224         int i;
3225
3226         if (!bmap)
3227                 return NULL;
3228
3229         nparam = isl_basic_map_n_param(bmap);
3230         n_in = isl_basic_map_n_in(bmap);
3231         n_out = isl_basic_map_n_out(bmap);
3232
3233         total = nparam + n_in + n_out + bmap->n_div + n_out;
3234         dim_map = isl_dim_map_alloc(bmap->ctx, total);
3235         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3236         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3237         isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3238         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3239
3240         result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
3241                         bmap->n_div + n_out,
3242                         bmap->n_eq, bmap->n_ineq + 2 * n_out);
3243         result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3244         result = add_divs(result, n_out);
3245         for (i = 0; i < n_out; ++i) {
3246                 int j;
3247                 j = isl_basic_map_alloc_inequality(result);
3248                 if (j < 0)
3249                         goto error;
3250                 isl_seq_clr(result->ineq[j], 1+total);
3251                 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3252                 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3253                 j = isl_basic_map_alloc_inequality(result);
3254                 if (j < 0)
3255                         goto error;
3256                 isl_seq_clr(result->ineq[j], 1+total);
3257                 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3258                 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3259                 isl_int_sub_ui(result->ineq[j][0], d, 1);
3260         }
3261
3262         result = isl_basic_map_simplify(result);
3263         return isl_basic_map_finalize(result);
3264 error:
3265         isl_basic_map_free(result);
3266         return NULL;
3267 }
3268
3269 /* Given a map A -> f(A) and an integer d, construct a map
3270  * A -> floor(f(A)/d).
3271  */
3272 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3273 {
3274         int i;
3275
3276         map = isl_map_cow(map);
3277         if (!map)
3278                 return NULL;
3279
3280         ISL_F_CLR(map, ISL_MAP_DISJOINT);
3281         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3282         for (i = 0; i < map->n; ++i) {
3283                 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3284                 if (!map->p[i])
3285                         goto error;
3286         }
3287
3288         return map;
3289 error:
3290         isl_map_free(map);
3291         return NULL;
3292 }
3293
3294 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3295 {
3296         int i;
3297         unsigned nparam;
3298         unsigned n_in;
3299
3300         i = isl_basic_map_alloc_equality(bmap);
3301         if (i < 0)
3302                 goto error;
3303         nparam = isl_basic_map_n_param(bmap);
3304         n_in = isl_basic_map_n_in(bmap);
3305         isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3306         isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3307         isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3308         return isl_basic_map_finalize(bmap);
3309 error:
3310         isl_basic_map_free(bmap);
3311         return NULL;
3312 }
3313
3314 /* Add a constraints to "bmap" expressing i_pos < o_pos
3315  */
3316 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3317 {
3318         int i;
3319         unsigned nparam;
3320         unsigned n_in;
3321
3322         i = isl_basic_map_alloc_inequality(bmap);
3323         if (i < 0)
3324                 goto error;
3325         nparam = isl_basic_map_n_param(bmap);
3326         n_in = isl_basic_map_n_in(bmap);
3327         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3328         isl_int_set_si(bmap->ineq[i][0], -1);
3329         isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3330         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3331         return isl_basic_map_finalize(bmap);
3332 error:
3333         isl_basic_map_free(bmap);
3334         return NULL;
3335 }
3336
3337 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3338  */
3339 static __isl_give isl_basic_map *var_less_or_equal(
3340         __isl_take isl_basic_map *bmap, unsigned pos)
3341 {
3342         int i;
3343         unsigned nparam;
3344         unsigned n_in;
3345
3346         i = isl_basic_map_alloc_inequality(bmap);
3347         if (i < 0)
3348                 goto error;
3349         nparam = isl_basic_map_n_param(bmap);
3350         n_in = isl_basic_map_n_in(bmap);
3351         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3352         isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3353         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3354         return isl_basic_map_finalize(bmap);
3355 error:
3356         isl_basic_map_free(bmap);
3357         return NULL;
3358 }
3359
3360 /* Add a constraints to "bmap" expressing i_pos > o_pos
3361  */
3362 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3363 {
3364         int i;
3365         unsigned nparam;
3366         unsigned n_in;
3367
3368         i = isl_basic_map_alloc_inequality(bmap);
3369         if (i < 0)
3370                 goto error;
3371         nparam = isl_basic_map_n_param(bmap);
3372         n_in = isl_basic_map_n_in(bmap);
3373         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3374         isl_int_set_si(bmap->ineq[i][0], -1);
3375         isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3376         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3377         return isl_basic_map_finalize(bmap);
3378 error:
3379         isl_basic_map_free(bmap);
3380         return NULL;
3381 }
3382
3383 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3384  */
3385 static __isl_give isl_basic_map *var_more_or_equal(
3386         __isl_take isl_basic_map *bmap, unsigned pos)
3387 {
3388         int i;
3389         unsigned nparam;
3390         unsigned n_in;
3391
3392         i = isl_basic_map_alloc_inequality(bmap);
3393         if (i < 0)
3394                 goto error;
3395         nparam = isl_basic_map_n_param(bmap);
3396         n_in = isl_basic_map_n_in(bmap);
3397         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3398         isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3399         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3400         return isl_basic_map_finalize(bmap);
3401 error:
3402         isl_basic_map_free(bmap);
3403         return NULL;
3404 }
3405
3406 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
3407 {
3408         int i;
3409         struct isl_basic_map *bmap;
3410         bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
3411         if (!bmap)
3412                 return NULL;
3413         for (i = 0; i < n_equal && bmap; ++i)
3414                 bmap = var_equal(bmap, i);
3415         return isl_basic_map_finalize(bmap);
3416 }
3417
3418 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3419  */
3420 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
3421 {
3422         int i;
3423         struct isl_basic_map *bmap;
3424         bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3425         if (!bmap)
3426                 return NULL;
3427         for (i = 0; i < pos && bmap; ++i)
3428                 bmap = var_equal(bmap, i);
3429         if (bmap)
3430                 bmap = var_less(bmap, pos);
3431         return isl_basic_map_finalize(bmap);
3432 }
3433
3434 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3435  */
3436 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3437         __isl_take isl_dim *dim, unsigned pos)
3438 {
3439         int i;
3440         isl_basic_map *bmap;
3441
3442         bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3443         for (i = 0; i < pos; ++i)
3444                 bmap = var_equal(bmap, i);
3445         bmap = var_less_or_equal(bmap, pos);
3446         return isl_basic_map_finalize(bmap);
3447 }
3448
3449 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3450  */
3451 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
3452 {
3453         int i;
3454         struct isl_basic_map *bmap;
3455         bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3456         if (!bmap)
3457                 return NULL;
3458         for (i = 0; i < pos && bmap; ++i)
3459                 bmap = var_equal(bmap, i);
3460         if (bmap)
3461                 bmap = var_more(bmap, pos);
3462         return isl_basic_map_finalize(bmap);
3463 }
3464
3465 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3466  */
3467 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3468         __isl_take isl_dim *dim, unsigned pos)
3469 {
3470         int i;
3471         isl_basic_map *bmap;
3472
3473         bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3474         for (i = 0; i < pos; ++i)
3475                 bmap = var_equal(bmap, i);
3476         bmap = var_more_or_equal(bmap, pos);
3477         return isl_basic_map_finalize(bmap);
3478 }
3479
3480 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3481         unsigned n, int equal)
3482 {
3483         struct isl_map *map;
3484         int i;
3485
3486         if (n == 0 && equal)
3487                 return isl_map_universe(dims);
3488
3489         map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3490
3491         for (i = 0; i + 1 < n; ++i)
3492                 map = isl_map_add_basic_map(map,
3493                                   isl_basic_map_less_at(isl_dim_copy(dims), i));
3494         if (n > 0) {
3495                 if (equal)
3496                         map = isl_map_add_basic_map(map,
3497                               isl_basic_map_less_or_equal_at(dims, n - 1));
3498                 else
3499                         map = isl_map_add_basic_map(map,
3500                               isl_basic_map_less_at(dims, n - 1));
3501         } else
3502                 isl_dim_free(dims);
3503
3504         return map;
3505 }
3506
3507 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3508 {
3509         if (!dims)
3510                 return NULL;
3511         return map_lex_lte_first(dims, dims->n_out, equal);
3512 }
3513
3514 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3515 {
3516         return map_lex_lte_first(dim, n, 0);
3517 }
3518
3519 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3520 {
3521         return map_lex_lte_first(dim, n, 1);
3522 }
3523
3524 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3525 {
3526         return map_lex_lte(isl_dim_map_from_set(set_dim), 0);
3527 }
3528
3529 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3530 {
3531         return map_lex_lte(isl_dim_map_from_set(set_dim), 1);
3532 }
3533
3534 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3535         unsigned n, int equal)
3536 {
3537         struct isl_map *map;
3538         int i;
3539
3540         if (n == 0 && equal)
3541                 return isl_map_universe(dims);
3542
3543         map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3544
3545         for (i = 0; i + 1 < n; ++i)
3546                 map = isl_map_add_basic_map(map,
3547                                   isl_basic_map_more_at(isl_dim_copy(dims), i));
3548         if (n > 0) {
3549                 if (equal)
3550                         map = isl_map_add_basic_map(map,
3551                               isl_basic_map_more_or_equal_at(dims, n - 1));
3552                 else
3553                         map = isl_map_add_basic_map(map,
3554                               isl_basic_map_more_at(dims, n - 1));
3555         } else
3556                 isl_dim_free(dims);
3557
3558         return map;
3559 }
3560
3561 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3562 {
3563         if (!dims)
3564                 return NULL;
3565         return map_lex_gte_first(dims, dims->n_out, equal);
3566 }
3567
3568 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3569 {
3570         return map_lex_gte_first(dim, n, 0);
3571 }
3572
3573 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3574 {
3575         return map_lex_gte_first(dim, n, 1);
3576 }
3577
3578 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3579 {
3580         return map_lex_gte(isl_dim_map_from_set(set_dim), 0);
3581 }
3582
3583 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3584 {
3585         return map_lex_gte(isl_dim_map_from_set(set_dim), 1);
3586 }
3587
3588 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
3589         __isl_take isl_set *set2)
3590 {
3591         isl_map *map;
3592         map = isl_map_lex_le(isl_set_get_dim(set1));
3593         map = isl_map_intersect_domain(map, set1);
3594         map = isl_map_intersect_range(map, set2);
3595         return map;
3596 }
3597
3598 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
3599         __isl_take isl_set *set2)
3600 {
3601         isl_map *map;
3602         map = isl_map_lex_lt(isl_set_get_dim(set1));
3603         map = isl_map_intersect_domain(map, set1);
3604         map = isl_map_intersect_range(map, set2);
3605         return map;
3606 }
3607
3608 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
3609         __isl_take isl_set *set2)
3610 {
3611         isl_map *map;
3612         map = isl_map_lex_ge(isl_set_get_dim(set1));
3613         map = isl_map_intersect_domain(map, set1);
3614         map = isl_map_intersect_range(map, set2);
3615         return map;
3616 }
3617
3618 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
3619         __isl_take isl_set *set2)
3620 {
3621         isl_map *map;
3622         map = isl_map_lex_gt(isl_set_get_dim(set1));
3623         map = isl_map_intersect_domain(map, set1);
3624         map = isl_map_intersect_range(map, set2);
3625         return map;
3626 }
3627
3628 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
3629         __isl_take isl_map *map2)
3630 {
3631         isl_map *map;
3632         map = isl_map_lex_le(isl_dim_range(isl_map_get_dim(map1)));
3633         map = isl_map_apply_domain(map, isl_map_reverse(map1));
3634         map = isl_map_apply_range(map, isl_map_reverse(map2));
3635         return map;
3636 }
3637
3638 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
3639         __isl_take isl_map *map2)
3640 {
3641         isl_map *map;
3642         map = isl_map_lex_lt(isl_dim_range(isl_map_get_dim(map1)));
3643         map = isl_map_apply_domain(map, isl_map_reverse(map1));
3644         map = isl_map_apply_range(map, isl_map_reverse(map2));
3645         return map;
3646 }
3647
3648 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
3649         __isl_take isl_map *map2)
3650 {
3651         isl_map *map;
3652         map = isl_map_lex_ge(isl_dim_range(isl_map_get_dim(map1)));
3653         map = isl_map_apply_domain(map, isl_map_reverse(map1));
3654         map = isl_map_apply_range(map, isl_map_reverse(map2));
3655         return map;
3656 }
3657
3658 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
3659         __isl_take isl_map *map2)
3660 {
3661         isl_map *map;
3662         map = isl_map_lex_gt(isl_dim_range(isl_map_get_dim(map1)));
3663         map = isl_map_apply_domain(map, isl_map_reverse(map1));
3664         map = isl_map_apply_range(map, isl_map_reverse(map2));
3665         return map;
3666 }
3667
3668 struct isl_basic_map *isl_basic_map_from_basic_set(
3669                 struct isl_basic_set *bset, struct isl_dim *dim)
3670 {
3671         struct isl_basic_map *bmap;
3672
3673         bset = isl_basic_set_cow(bset);
3674         if (!bset || !dim)
3675                 goto error;
3676
3677         isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3678         isl_dim_free(bset->dim);
3679         bmap = (struct isl_basic_map *) bset;
3680         bmap->dim = dim;
3681         return isl_basic_map_finalize(bmap);
3682 error:
3683         isl_basic_set_free(bset);
3684         isl_dim_free(dim);
3685         return NULL;
3686 }
3687
3688 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3689 {
3690         if (!bmap)
3691                 goto error;
3692         if (bmap->dim->n_in == 0)
3693                 return (struct isl_basic_set *)bmap;
3694         bmap = isl_basic_map_cow(bmap);
3695         if (!bmap)
3696                 goto error;
3697         bmap->dim = isl_dim_as_set_dim(bmap->dim);
3698         if (!bmap->dim)
3699                 goto error;
3700         bmap = isl_basic_map_finalize(bmap);
3701         return (struct isl_basic_set *)bmap;
3702 error:
3703         isl_basic_map_free(bmap);
3704         return NULL;
3705 }
3706
3707 /* For a div d = floor(f/m), add the constraints
3708  *
3709  *              f - m d >= 0
3710  *              -(f-(n-1)) + m d >= 0
3711  *
3712  * Note that the second constraint is the negation of
3713  *
3714  *              f - m d >= n
3715  */
3716 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3717         unsigned pos, isl_int *div)
3718 {
3719         int i, j;
3720         unsigned total = isl_basic_map_total_dim(bmap);
3721
3722         i = isl_basic_map_alloc_inequality(bmap);
3723         if (i < 0)
3724                 return -1;
3725         isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3726         isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3727
3728         j = isl_basic_map_alloc_inequality(bmap);
3729         if (j < 0)
3730                 return -1;
3731         isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3732         isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3733         isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3734         return j;
3735 }
3736
3737 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3738         unsigned pos, isl_int *div)
3739 {
3740         return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3741                                                         pos, div);
3742 }
3743
3744 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3745 {
3746         unsigned total = isl_basic_map_total_dim(bmap);
3747         unsigned div_pos = total - bmap->n_div + div;
3748
3749         return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3750                                                         bmap->div[div]);
3751 }
3752
3753 struct isl_basic_set *isl_basic_map_underlying_set(
3754                 struct isl_basic_map *bmap)
3755 {
3756         if (!bmap)
3757                 goto error;
3758         if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
3759             bmap->n_div == 0 &&
3760             !isl_dim_is_named_or_nested(bmap->dim, isl_dim_in) &&
3761             !isl_dim_is_named_or_nested(bmap->dim, isl_dim_out))
3762                 return (struct isl_basic_set *)bmap;
3763         bmap = isl_basic_map_cow(bmap);
3764         if (!bmap)
3765                 goto error;
3766         bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3767         if (!bmap->dim)
3768                 goto error;
3769         bmap->extra -= bmap->n_div;
3770         bmap->n_div = 0;
3771         bmap = isl_basic_map_finalize(bmap);
3772         return (struct isl_basic_set *)bmap;
3773 error:
3774         return NULL;
3775 }
3776
3777 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3778                 __isl_take isl_basic_set *bset)
3779 {
3780         return isl_basic_map_underlying_set((isl_basic_map *)bset);
3781 }
3782
3783 struct isl_basic_map *isl_basic_map_overlying_set(
3784         struct isl_basic_set *bset, struct isl_basic_map *like)
3785 {
3786         struct isl_basic_map *bmap;
3787         struct isl_ctx *ctx;
3788         unsigned total;
3789         int i;
3790
3791         if (!bset || !like)
3792                 goto error;
3793         ctx = bset->ctx;
3794         isl_assert(ctx, bset->n_div == 0, goto error);
3795         isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3796         isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3797                         goto error);
3798         if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3799                 isl_basic_map_free(like);
3800                 return (struct isl_basic_map *)bset;
3801         }
3802         bset = isl_basic_set_cow(bset);
3803         if (!bset)
3804                 goto error;
3805         total = bset->dim->n_out + bset->extra;
3806         bmap = (struct isl_basic_map *)bset;
3807         isl_dim_free(bmap->dim);
3808         bmap->dim = isl_dim_copy(like->dim);
3809         if (!bmap->dim)
3810                 goto error;
3811         bmap->n_div = like->n_div;
3812         bmap->extra += like->n_div;
3813         if (bmap->extra) {
3814                 unsigned ltotal;
3815                 isl_int **div;
3816                 ltotal = total - bmap->extra + like->extra;
3817                 if (ltotal > total)
3818                         ltotal = total;
3819                 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3820                                         bmap->extra * (1 + 1 + total));
3821                 if (isl_blk_is_error(bmap->block2))
3822                         goto error;
3823                 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
3824                 if (!div)
3825                         goto error;
3826                 bmap->div = div;
3827                 for (i = 0; i < bmap->extra; ++i)
3828                         bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3829                 for (i = 0; i < like->n_div; ++i) {
3830                         isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3831                         isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3832                 }
3833                 bmap = isl_basic_map_extend_constraints(bmap, 
3834                                                         0, 2 * like->n_div);
3835                 for (i = 0; i < like->n_div; ++i) {
3836                         if (isl_int_is_zero(bmap->div[i][0]))
3837                                 continue;
3838                         if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3839                                 goto error;
3840                 }
3841         }
3842         isl_basic_map_free(like);
3843         bmap = isl_basic_map_simplify(bmap);
3844         bmap = isl_basic_map_finalize(bmap);
3845         return bmap;
3846 error:
3847         isl_basic_map_free(like);
3848         isl_basic_set_free(bset);
3849         return NULL;
3850 }
3851
3852 struct isl_basic_set *isl_basic_set_from_underlying_set(
3853         struct isl_basic_set *bset, struct isl_basic_set *like)
3854 {
3855         return (struct isl_basic_set *)
3856                 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3857 }
3858
3859 struct isl_set *isl_set_from_underlying_set(
3860         struct isl_set *set, struct isl_basic_set *like)
3861 {
3862         int i;
3863
3864         if (!set || !like)
3865                 goto error;
3866         isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3867                     goto error);
3868         if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3869                 isl_basic_set_free(like);
3870                 return set;
3871         }
3872         set = isl_set_cow(set);
3873         if (!set)
3874                 goto error;
3875         for (i = 0; i < set->n; ++i) {
3876                 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3877                                                       isl_basic_set_copy(like));
3878                 if (!set->p[i])
3879                         goto error;
3880         }
3881         isl_dim_free(set->dim);
3882         set->dim = isl_dim_copy(like->dim);
3883         if (!set->dim)
3884                 goto error;
3885         isl_basic_set_free(like);
3886         return set;
3887 error:
3888         isl_basic_set_free(like);
3889         isl_set_free(set);
3890         return NULL;
3891 }
3892
3893 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3894 {
3895         int i;
3896
3897         map = isl_map_cow(map);
3898         if (!map)
3899                 return NULL;
3900         map->dim = isl_dim_cow(map->dim);
3901         if (!map->dim)
3902                 goto error;
3903
3904         for (i = 1; i < map->n; ++i)
3905                 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3906                                 goto error);
3907         for (i = 0; i < map->n; ++i) {
3908                 map->p[i] = (struct isl_basic_map *)
3909                                 isl_basic_map_underlying_set(map->p[i]);
3910                 if (!map->p[i])
3911                         goto error;
3912         }
3913         if (map->n == 0)
3914                 map->dim = isl_dim_underlying(map->dim, 0);
3915         else {
3916                 isl_dim_free(map->dim);
3917                 map->dim = isl_dim_copy(map->p[0]->dim);
3918         }
3919         if (!map->dim)
3920                 goto error;
3921         return (struct isl_set *)map;
3922 error:
3923         isl_map_free(map);
3924         return NULL;
3925 }
3926
3927 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3928 {
3929         return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3930 }
3931
3932 __isl_give isl_basic_map *isl_basic_map_reset_dim(
3933         __isl_take isl_basic_map *bmap, __isl_take isl_dim *dim)
3934 {
3935         bmap = isl_basic_map_cow(bmap);
3936         if (!bmap || !dim)
3937                 goto error;
3938
3939         isl_dim_free(bmap->dim);
3940         bmap->dim = dim;
3941
3942         bmap = isl_basic_map_finalize(bmap);
3943
3944         return bmap;
3945 error:
3946         isl_basic_map_free(bmap);
3947         isl_dim_free(dim);
3948         return NULL;
3949 }
3950
3951 __isl_give isl_basic_set *isl_basic_set_reset_dim(
3952         __isl_take isl_basic_set *bset, __isl_take isl_dim *dim)
3953 {
3954         return (isl_basic_set *)isl_basic_map_reset_dim((isl_basic_map *)bset,
3955                                                         dim);
3956 }
3957
3958 __isl_give isl_map *isl_map_reset_dim(__isl_take isl_map *map,
3959         __isl_take isl_dim *dim)
3960 {
3961         int i;
3962
3963         map = isl_map_cow(map);
3964         if (!map || !dim)
3965                 goto error;
3966
3967         for (i = 0; i < map->n; ++i) {
3968                 map->p[i] = isl_basic_map_reset_dim(map->p[i],
3969                                                     isl_dim_copy(dim));
3970                 if (!map->p[i])
3971                         goto error;
3972         }
3973         isl_dim_free(map->dim);
3974         map->dim = dim;
3975
3976         return map;
3977 error:
3978         isl_map_free(map);
3979         isl_dim_free(dim);
3980         return NULL;
3981 }
3982
3983 __isl_give isl_set *isl_set_reset_dim(__isl_take isl_set *set,
3984         __isl_take isl_dim *dim)
3985 {
3986         return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3987 }
3988
3989 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3990 {
3991         isl_dim *dim;
3992         struct isl_basic_set *domain;
3993         unsigned n_in;
3994         unsigned n_out;
3995
3996         if (!bmap)
3997                 return NULL;
3998         dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
3999
4000         n_in = isl_basic_map_n_in(bmap);
4001         n_out = isl_basic_map_n_out(bmap);
4002         domain = isl_basic_set_from_basic_map(bmap);
4003         domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4004
4005         domain = isl_basic_set_reset_dim(domain, dim);
4006
4007         return domain;
4008 }
4009
4010 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4011 {
4012         if (!bmap)
4013                 return -1;
4014         return isl_dim_may_be_set(bmap->dim);
4015 }
4016
4017 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4018 {
4019         if (!bmap)
4020                 return NULL;
4021         if (isl_basic_map_may_be_set(bmap))
4022                 return bmap;
4023         return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4024 }
4025
4026 __isl_give isl_basic_map *isl_basic_map_domain_map(
4027         __isl_take isl_basic_map *bmap)
4028 {
4029         int i, k;
4030         isl_dim *dim;
4031         isl_basic_map *domain;
4032         int nparam, n_in, n_out;
4033         unsigned total;
4034
4035         nparam = isl_basic_map_dim(bmap, isl_dim_param);
4036         n_in = isl_basic_map_dim(bmap, isl_dim_in);
4037         n_out = isl_basic_map_dim(bmap, isl_dim_out);
4038
4039         dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
4040         domain = isl_basic_map_universe(dim);
4041
4042         bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4043         bmap = isl_basic_map_apply_range(bmap, domain);
4044         bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4045
4046         total = isl_basic_map_total_dim(bmap);
4047
4048         for (i = 0; i < n_in; ++i) {
4049                 k = isl_basic_map_alloc_equality(bmap);
4050                 if (k < 0)
4051                         goto error;
4052                 isl_seq_clr(bmap->eq[k], 1 + total);
4053                 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4054                 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4055         }
4056
4057         bmap = isl_basic_map_gauss(bmap, NULL);
4058         return isl_basic_map_finalize(bmap);
4059 error:
4060         isl_basic_map_free(bmap);
4061         return NULL;
4062 }
4063
4064 __isl_give isl_basic_map *isl_basic_map_range_map(
4065         __isl_take isl_basic_map *bmap)
4066 {
4067         int i, k;
4068         isl_dim *dim;
4069         isl_basic_map *range;
4070         int nparam, n_in, n_out;
4071         unsigned total;
4072
4073         nparam = isl_basic_map_dim(bmap, isl_dim_param);
4074         n_in = isl_basic_map_dim(bmap, isl_dim_in);
4075         n_out = isl_basic_map_dim(bmap, isl_dim_out);
4076
4077         dim = isl_dim_from_range(isl_dim_range(isl_basic_map_get_dim(bmap)));
4078         range = isl_basic_map_universe(dim);
4079
4080         bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4081         bmap = isl_basic_map_apply_range(bmap, range);
4082         bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4083
4084         total = isl_basic_map_total_dim(bmap);
4085
4086         for (i = 0; i < n_out; ++i) {
4087                 k = isl_basic_map_alloc_equality(bmap);
4088                 if (k < 0)
4089                         goto error;
4090                 isl_seq_clr(bmap->eq[k], 1 + total);
4091                 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4092                 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4093         }
4094
4095         bmap = isl_basic_map_gauss(bmap, NULL);
4096         return isl_basic_map_finalize(bmap);
4097 error:
4098         isl_basic_map_free(bmap);
4099         return NULL;
4100 }
4101
4102 int isl_map_may_be_set(__isl_keep isl_map *map)
4103 {
4104         if (!map)
4105                 return -1;
4106         return isl_dim_may_be_set(map->dim);
4107 }
4108
4109 struct isl_set *isl_map_range(struct isl_map *map)
4110 {
4111         int i;
4112         struct isl_set *set;
4113
4114         if (!map)
4115                 goto error;
4116         if (isl_map_may_be_set(map))
4117                 return (isl_set *)map;
4118
4119         map = isl_map_cow(map);
4120         if (!map)
4121                 goto error;
4122
4123         set = (struct isl_set *) map;
4124         set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
4125         if (!set->dim)
4126                 goto error;
4127         for (i = 0; i < map->n; ++i) {
4128                 set->p[i] = isl_basic_map_range(map->p[i]);
4129                 if (!set->p[i])
4130                         goto error;
4131         }
4132         ISL_F_CLR(set, ISL_MAP_DISJOINT);
4133         ISL_F_CLR(set, ISL_SET_NORMALIZED);
4134         return set;
4135 error:
4136         isl_map_free(map);
4137         return NULL;
4138 }
4139
4140 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4141 {
4142         int i;
4143         isl_dim *domain_dim;
4144
4145         map = isl_map_cow(map);
4146         if (!map)
4147                 return NULL;
4148
4149         domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
4150         map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
4151         map->dim = isl_dim_join(map->dim, domain_dim);
4152         if (!map->dim)
4153                 goto error;
4154         for (i = 0; i < map->n; ++i) {
4155                 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4156                 if (!map->p[i])
4157                         goto error;
4158         }
4159         ISL_F_CLR(map, ISL_MAP_DISJOINT);
4160         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4161         return map;
4162 error:
4163         isl_map_free(map);
4164         return NULL;
4165 }
4166
4167 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4168 {
4169         int i;
4170         isl_dim *range_dim;
4171
4172         map = isl_map_cow(map);
4173         if (!map)
4174                 return NULL;
4175
4176         range_dim = isl_dim_range(isl_map_get_dim(map));
4177         map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
4178         map->dim = isl_dim_join(map->dim, range_dim);
4179         if (!map->dim)
4180                 goto error;
4181         for (i = 0; i < map->n; ++i) {
4182                 map->p[i] = isl_basic_map_range_map(map->p[i]);
4183                 if (!map->p[i])
4184                         goto error;
4185         }
4186         ISL_F_CLR(map, ISL_MAP_DISJOINT);
4187         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4188         return map;
4189 error:
4190         isl_map_free(map);
4191         return NULL;
4192 }
4193
4194 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
4195 {
4196         int i;
4197         struct isl_map *map = NULL;
4198
4199         set = isl_set_cow(set);
4200         if (!set || !dim)
4201                 goto error;
4202         isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
4203         map = (struct isl_map *)set;
4204         for (i = 0; i < set->n; ++i) {
4205                 map->p[i] = isl_basic_map_from_basic_set(
4206                                 set->p[i], isl_dim_copy(dim));
4207                 if (!map->p[i])
4208                         goto error;
4209         }
4210         isl_dim_free(map->dim);
4211         map->dim = dim;
4212         return map;
4213 error:
4214         isl_dim_free(dim);
4215         isl_set_free(set);
4216         return NULL;
4217 }
4218
4219 __isl_give isl_basic_map *isl_basic_map_from_domain(
4220         __isl_take isl_basic_set *bset)
4221 {
4222         return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4223 }
4224
4225 __isl_give isl_basic_map *isl_basic_map_from_range(
4226         __isl_take isl_basic_set *bset)
4227 {
4228         return (isl_basic_map *)bset;
4229 }
4230
4231 struct isl_map *isl_map_from_range(struct isl_set *set)
4232 {
4233         return (struct isl_map *)set;
4234 }
4235
4236 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4237 {
4238         return isl_map_reverse(isl_map_from_range(set));
4239 }
4240
4241 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4242         __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4243 {
4244         return isl_basic_map_apply_range(isl_basic_map_from_domain(domain),
4245                                          isl_basic_map_from_range(range));
4246 }
4247
4248 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4249         __isl_take isl_set *range)
4250 {
4251         return isl_map_apply_range(isl_map_from_domain(domain),
4252                                    isl_map_from_range(range));
4253 }
4254
4255 struct isl_set *isl_set_from_map(struct isl_map *map)
4256 {
4257         int i;
4258         struct isl_set *set = NULL;
4259
4260         if (!map)
4261                 return NULL;
4262         map = isl_map_cow(map);
4263         if (!map)
4264                 return NULL;
4265         map->dim = isl_dim_as_set_dim(map->dim);
4266         if (!map->dim)
4267                 goto error;
4268         set = (struct isl_set *)map;
4269         for (i = 0; i < map->n; ++i) {
4270                 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4271                 if (!set->p[i])
4272                         goto error;
4273         }
4274         return set;
4275 error:
4276         isl_map_free(map);
4277         return NULL;
4278 }
4279
4280 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
4281 {
4282         struct isl_map *map;
4283
4284         if (!dim)
4285                 return NULL;
4286         isl_assert(dim->ctx, n >= 0, return NULL);
4287         map = isl_alloc(dim->ctx, struct isl_map,
4288                         sizeof(struct isl_map) +
4289                         (n - 1) * sizeof(struct isl_basic_map *));
4290         if (!map)
4291                 goto error;
4292
4293         map->ctx = dim->ctx;
4294         isl_ctx_ref(map->ctx);
4295         map->ref = 1;
4296         map->size = n;
4297         map->n = 0;
4298         map->dim = dim;
4299         map->flags = flags;
4300         return map;
4301 error:
4302         isl_dim_free(dim);
4303         return NULL;
4304 }
4305
4306 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4307                 unsigned nparam, unsigned in, unsigned out, int n,
4308                 unsigned flags)
4309 {
4310         struct isl_map *map;
4311         struct isl_dim *dims;
4312
4313         dims = isl_dim_alloc(ctx, nparam, in, out);
4314         if (!dims)
4315                 return NULL;
4316
4317         map = isl_map_alloc_dim(dims, n, flags);
4318         return map;
4319 }
4320
4321 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
4322 {
4323         struct isl_basic_map *bmap;
4324         bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
4325         bmap = isl_basic_map_set_to_empty(bmap);
4326         return bmap;
4327 }
4328
4329 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
4330 {
4331         struct isl_basic_set *bset;
4332         bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
4333         bset = isl_basic_set_set_to_empty(bset);
4334         return bset;
4335 }
4336
4337 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4338 {
4339         struct isl_basic_map *bmap;
4340         if (!model)
4341                 return NULL;
4342         bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4343         bmap = isl_basic_map_set_to_empty(bmap);
4344         return bmap;
4345 }
4346
4347 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4348 {
4349         struct isl_basic_map *bmap;
4350         if (!model)
4351                 return NULL;
4352         bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4353         bmap = isl_basic_map_set_to_empty(bmap);
4354         return bmap;
4355 }
4356
4357 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4358 {
4359         struct isl_basic_set *bset;
4360         if (!model)
4361                 return NULL;
4362         bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
4363         bset = isl_basic_set_set_to_empty(bset);
4364         return bset;
4365 }
4366
4367 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
4368 {
4369         struct isl_basic_map *bmap;
4370         bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
4371         bmap = isl_basic_map_finalize(bmap);
4372         return bmap;
4373 }
4374
4375 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
4376 {
4377         struct isl_basic_set *bset;
4378         bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
4379         bset = isl_basic_set_finalize(bset);
4380         return bset;
4381 }
4382
4383 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_dim *dim)
4384 {
4385         int i;
4386         unsigned total = isl_dim_total(dim);
4387         isl_basic_map *bmap;
4388
4389         bmap= isl_basic_map_alloc_dim(dim, 0, 0, total);
4390         for (i = 0; i < total; ++i) {
4391                 int k = isl_basic_map_alloc_inequality(bmap);
4392                 if (k < 0)
4393                         goto error;
4394                 isl_seq_clr(bmap->ineq[k], 1 + total);
4395                 isl_int_set_si(bmap->ineq[k][1 + i], 1);
4396         }
4397         return bmap;
4398 error:
4399         isl_basic_map_free(bmap);
4400         return NULL;
4401 }
4402
4403 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_dim *dim)
4404 {
4405         return isl_basic_map_nat_universe(dim);
4406 }
4407
4408 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_dim *dim)
4409 {
4410         return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
4411 }
4412
4413 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_dim *dim)
4414 {
4415         return isl_map_nat_universe(dim);
4416 }
4417
4418 __isl_give isl_basic_map *isl_basic_map_universe_like(
4419                 __isl_keep isl_basic_map *model)
4420 {
4421         if (!model)
4422                 return NULL;
4423         return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4424 }
4425
4426 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
4427 {
4428         if (!model)
4429                 return NULL;
4430         return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4431 }
4432
4433 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
4434         __isl_keep isl_set *model)
4435 {
4436         if (!model)
4437                 return NULL;
4438         return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
4439 }
4440
4441 struct isl_map *isl_map_empty(struct isl_dim *dim)
4442 {
4443         return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4444 }
4445
4446 struct isl_map *isl_map_empty_like(struct isl_map *model)
4447 {
4448         if (!model)
4449                 return NULL;
4450         return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4451 }
4452
4453 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
4454 {
4455         if (!model)
4456                 return NULL;
4457         return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
4458 }
4459
4460 struct isl_set *isl_set_empty(struct isl_dim *dim)
4461 {
4462         return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
4463 }
4464
4465 struct isl_set *isl_set_empty_like(struct isl_set *model)
4466 {
4467         if (!model)
4468                 return NULL;
4469         return isl_set_empty(isl_dim_copy(model->dim));
4470 }
4471
4472 struct isl_map *isl_map_universe(struct isl_dim *dim)
4473 {
4474         struct isl_map *map;
4475         if (!dim)
4476                 return NULL;
4477         map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4478         map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
4479         return map;
4480 }
4481
4482 struct isl_set *isl_set_universe(struct isl_dim *dim)
4483 {
4484         struct isl_set *set;
4485         if (!dim)
4486                 return NULL;
4487         set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
4488         set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
4489         return set;
4490 }
4491
4492 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
4493 {
4494         if (!model)
4495                 return NULL;
4496         return isl_set_universe(isl_dim_copy(model->dim));
4497 }
4498
4499 struct isl_map *isl_map_dup(struct isl_map *map)
4500 {
4501         int i;
4502         struct isl_map *dup;
4503
4504         if (!map)
4505                 return NULL;
4506         dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
4507         for (i = 0; i < map->n; ++i)
4508                 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
4509         return dup;
4510 }
4511
4512 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
4513                                                 __isl_take isl_basic_map *bmap)
4514 {
4515         if (!bmap || !map)
4516                 goto error;
4517         if (isl_basic_map_plain_is_empty(bmap)) {
4518                 isl_basic_map_free(bmap);
4519                 return map;
4520         }
4521         isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
4522         isl_assert(map->ctx, map->n < map->size, goto error);
4523         map->p[map->n] = bmap;
4524         map->n++;
4525         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4526         return map;
4527 error:
4528         if (map)
4529                 isl_map_free(map);
4530         if (bmap)
4531                 isl_basic_map_free(bmap);
4532         return NULL;
4533 }
4534
4535 void isl_map_free(struct isl_map *map)
4536 {
4537         int i;
4538
4539         if (!map)
4540                 return;
4541
4542         if (--map->ref > 0)
4543                 return;
4544
4545         isl_ctx_deref(map->ctx);
4546         for (i = 0; i < map->n; ++i)
4547                 isl_basic_map_free(map->p[i]);
4548         isl_dim_free(map->dim);
4549         free(map);
4550 }
4551
4552 struct isl_map *isl_map_extend(struct isl_map *base,
4553                 unsigned nparam, unsigned n_in, unsigned n_out)
4554 {
4555         int i;
4556
4557         base = isl_map_cow(base);
4558         if (!base)
4559                 return NULL;
4560
4561         base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
4562         if (!base->dim)
4563                 goto error;
4564         for (i = 0; i < base->n; ++i) {
4565                 base->p[i] = isl_basic_map_extend_dim(base->p[i],
4566                                 isl_dim_copy(base->dim), 0, 0, 0);
4567                 if (!base->p[i])
4568                         goto error;
4569         }
4570         return base;
4571 error:
4572         isl_map_free(base);
4573         return NULL;
4574 }
4575
4576 struct isl_set *isl_set_extend(struct isl_set *base,
4577                 unsigned nparam, unsigned dim)
4578 {
4579         return (struct isl_set *)isl_map_extend((struct isl_map *)base,
4580                                                         nparam, 0, dim);
4581 }
4582
4583 static struct isl_basic_map *isl_basic_map_fix_pos_si(
4584         struct isl_basic_map *bmap, unsigned pos, int value)
4585 {
4586         int j;
4587
4588         bmap = isl_basic_map_cow(bmap);
4589         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4590         j = isl_basic_map_alloc_equality(bmap);
4591         if (j < 0)
4592                 goto error;
4593         isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4594         isl_int_set_si(bmap->eq[j][pos], -1);
4595         isl_int_set_si(bmap->eq[j][0], value);
4596         bmap = isl_basic_map_simplify(bmap);
4597         return isl_basic_map_finalize(bmap);
4598 error:
4599         isl_basic_map_free(bmap);
4600         return NULL;
4601 }
4602
4603 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
4604         __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
4605 {
4606         int j;
4607
4608         bmap = isl_basic_map_cow(bmap);
4609         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
4610         j = isl_basic_map_alloc_equality(bmap);
4611         if (j < 0)
4612                 goto error;
4613         isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
4614         isl_int_set_si(bmap->eq[j][pos], -1);
4615         isl_int_set(bmap->eq[j][0], value);
4616         bmap = isl_basic_map_simplify(bmap);
4617         return isl_basic_map_finalize(bmap);
4618 error:
4619         isl_basic_map_free(bmap);
4620         return NULL;
4621 }
4622
4623 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
4624                 enum isl_dim_type type, unsigned pos, int value)
4625 {
4626         if (!bmap)
4627                 return NULL;
4628         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4629         return isl_basic_map_fix_pos_si(bmap,
4630                 isl_basic_map_offset(bmap, type) + pos, value);
4631 error:
4632         isl_basic_map_free(bmap);
4633         return NULL;
4634 }
4635
4636 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
4637                 enum isl_dim_type type, unsigned pos, isl_int value)
4638 {
4639         if (!bmap)
4640                 return NULL;
4641         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4642         return isl_basic_map_fix_pos(bmap,
4643                 isl_basic_map_offset(bmap, type) + pos, value);
4644 error:
4645         isl_basic_map_free(bmap);
4646         return NULL;
4647 }
4648
4649 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
4650                 enum isl_dim_type type, unsigned pos, int value)
4651 {
4652         return (struct isl_basic_set *)
4653                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4654                                         type, pos, value);
4655 }
4656
4657 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
4658                 enum isl_dim_type type, unsigned pos, isl_int value)
4659 {
4660         return (struct isl_basic_set *)
4661                 isl_basic_map_fix((struct isl_basic_map *)bset,
4662                                         type, pos, value);
4663 }
4664
4665 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
4666                 unsigned input, int value)
4667 {
4668         return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
4669 }
4670
4671 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
4672                 unsigned dim, int value)
4673 {
4674         return (struct isl_basic_set *)
4675                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
4676                                         isl_dim_set, dim, value);
4677 }
4678
4679 struct isl_map *isl_map_fix_si(struct isl_map *map,
4680                 enum isl_dim_type type, unsigned pos, int value)
4681 {
4682         int i;
4683
4684         map = isl_map_cow(map);
4685         if (!map)
4686                 return NULL;
4687
4688         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4689         for (i = 0; i < map->n; ++i) {
4690                 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
4691                 if (!map->p[i])
4692                         goto error;
4693         }
4694         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4695         return map;
4696 error:
4697         isl_map_free(map);
4698         return NULL;
4699 }
4700
4701 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
4702                 enum isl_dim_type type, unsigned pos, int value)
4703 {
4704         return (struct isl_set *)
4705                 isl_map_fix_si((struct isl_map *)set, type, pos, value);
4706 }
4707
4708 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
4709                 enum isl_dim_type type, unsigned pos, isl_int value)
4710 {
4711         int i;
4712
4713         map = isl_map_cow(map);
4714         if (!map)
4715                 return NULL;
4716
4717         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4718         for (i = 0; i < map->n; ++i) {
4719                 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
4720                 if (!map->p[i])
4721                         goto error;
4722         }
4723         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4724         return map;
4725 error:
4726         isl_map_free(map);
4727         return NULL;
4728 }
4729
4730 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
4731                 enum isl_dim_type type, unsigned pos, isl_int value)
4732 {
4733         return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
4734 }
4735
4736 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
4737                 unsigned input, int value)
4738 {
4739         return isl_map_fix_si(map, isl_dim_in, input, value);
4740 }
4741
4742 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
4743 {
4744         return (struct isl_set *)
4745                 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
4746 }
4747
4748 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
4749                 __isl_take isl_basic_map *bmap,
4750                 enum isl_dim_type type, unsigned pos, int value)
4751 {
4752         int j;
4753
4754         if (!bmap)
4755                 return NULL;
4756         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
4757         pos += isl_basic_map_offset(bmap, type);
4758         bmap = isl_basic_map_cow(bmap);
4759         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
4760         j = isl_basic_map_alloc_inequality(bmap);
4761         if (j < 0)
4762                 goto error;
4763         isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
4764         isl_int_set_si(bmap->ineq[j][pos], 1);
4765         isl_int_set_si(bmap->ineq[j][0], -value);
4766         bmap = isl_basic_map_simplify(bmap);
4767         return isl_basic_map_finalize(bmap);
4768 error:
4769         isl_basic_map_free(bmap);
4770         return NULL;
4771 }
4772
4773 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
4774         unsigned dim, isl_int value)
4775 {
4776         int j;
4777
4778         bset = isl_basic_set_cow(bset);
4779         bset = isl_basic_set_extend_constraints(bset, 0, 1);
4780         j = isl_basic_set_alloc_inequality(bset);
4781         if (j < 0)
4782                 goto error;
4783         isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
4784         isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
4785         isl_int_neg(bset->ineq[j][0], value);
4786         bset = isl_basic_set_simplify(bset);
4787         return isl_basic_set_finalize(bset);
4788 error:
4789         isl_basic_set_free(bset);
4790         return NULL;
4791 }
4792
4793 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
4794                 enum isl_dim_type type, unsigned pos, int value)
4795 {
4796         int i;
4797
4798         map = isl_map_cow(map);
4799         if (!map)
4800                 return NULL;
4801
4802         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
4803         for (i = 0; i < map->n; ++i) {
4804                 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
4805                                                          type, pos, value);
4806                 if (!map->p[i])
4807                         goto error;
4808         }
4809         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4810         return map;
4811 error:
4812         isl_map_free(map);
4813         return NULL;
4814 }
4815
4816 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
4817                 enum isl_dim_type type, unsigned pos, int value)
4818 {
4819         return (struct isl_set *)
4820                 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4821 }
4822
4823 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4824                                         isl_int value)
4825 {
4826         int i;
4827
4828         set = isl_set_cow(set);
4829         if (!set)
4830                 return NULL;
4831
4832         isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4833         for (i = 0; i < set->n; ++i) {
4834                 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4835                 if (!set->p[i])
4836                         goto error;
4837         }
4838         return set;
4839 error:
4840         isl_set_free(set);
4841         return NULL;
4842 }
4843
4844 struct isl_map *isl_map_reverse(struct isl_map *map)
4845 {
4846         int i;
4847
4848         map = isl_map_cow(map);
4849         if (!map)
4850                 return NULL;
4851
4852         map->dim = isl_dim_reverse(map->dim);
4853         if (!map->dim)
4854                 goto error;
4855         for (i = 0; i < map->n; ++i) {
4856                 map->p[i] = isl_basic_map_reverse(map->p[i]);
4857                 if (!map->p[i])
4858                         goto error;
4859         }
4860         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4861         return map;
4862 error:
4863         isl_map_free(map);
4864         return NULL;
4865 }
4866
4867 static struct isl_map *isl_basic_map_partial_lexopt(
4868                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4869                 struct isl_set **empty, int max)
4870 {
4871         if (!bmap)
4872                 goto error;
4873         if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4874                 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4875         else
4876                 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4877 error:
4878         isl_basic_map_free(bmap);
4879         isl_basic_set_free(dom);
4880         if (empty)
4881                 *empty = NULL;
4882         return NULL;
4883 }
4884
4885 struct isl_map *isl_basic_map_partial_lexmax(
4886                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4887                 struct isl_set **empty)
4888 {
4889         return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4890 }
4891
4892 struct isl_map *isl_basic_map_partial_lexmin(
4893                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4894                 struct isl_set **empty)
4895 {
4896         return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4897 }
4898
4899 struct isl_set *isl_basic_set_partial_lexmin(
4900                 struct isl_basic_set *bset, struct isl_basic_set *dom,
4901                 struct isl_set **empty)
4902 {
4903         return (struct isl_set *)
4904                 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4905                         dom, empty);
4906 }
4907
4908 struct isl_set *isl_basic_set_partial_lexmax(
4909                 struct isl_basic_set *bset, struct isl_basic_set *dom,
4910                 struct isl_set **empty)
4911 {
4912         return (struct isl_set *)
4913                 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4914                         dom, empty);
4915 }
4916
4917 /* Given a basic map "bmap", compute the lexicographically minimal
4918  * (or maximal) image element for each domain element in dom.
4919  * Set *empty to those elements in dom that do not have an image element.
4920  *
4921  * We first make sure the basic sets in dom are disjoint and then
4922  * simply collect the results over each of the basic sets separately.
4923  * We could probably improve the efficiency a bit by moving the union
4924  * domain down into the parametric integer programming.
4925  */
4926 static __isl_give isl_map *basic_map_partial_lexopt(
4927                 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4928                 __isl_give isl_set **empty, int max)
4929 {
4930         int i;
4931         struct isl_map *res;
4932
4933         dom = isl_set_make_disjoint(dom);
4934         if (!dom)
4935                 goto error;
4936
4937         if (isl_set_plain_is_empty(dom)) {
4938                 res = isl_map_empty_like_basic_map(bmap);
4939                 *empty = isl_set_empty_like(dom);
4940                 isl_set_free(dom);
4941                 isl_basic_map_free(bmap);
4942                 return res;
4943         }
4944
4945         res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4946                         isl_basic_set_copy(dom->p[0]), empty, max);
4947                 
4948         for (i = 1; i < dom->n; ++i) {
4949                 struct isl_map *res_i;
4950                 struct isl_set *empty_i;
4951
4952                 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4953                                 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4954
4955                 res = isl_map_union_disjoint(res, res_i);
4956                 *empty = isl_set_union_disjoint(*empty, empty_i);
4957         }
4958
4959         isl_set_free(dom);
4960         isl_basic_map_free(bmap);
4961         return res;
4962 error:
4963         *empty = NULL;
4964         isl_set_free(dom);
4965         isl_basic_map_free(bmap);
4966         return NULL;
4967 }
4968
4969 /* Given a map "map", compute the lexicographically minimal
4970  * (or maximal) image element for each domain element in dom.
4971  * Set *empty to those elements in dom that do not have an image element.
4972  *
4973  * We first compute the lexicographically minimal or maximal element
4974  * in the first basic map.  This results in a partial solution "res"
4975  * and a subset "todo" of dom that still need to be handled.
4976  * We then consider each of the remaining maps in "map" and successively
4977  * improve both "res" and "todo".
4978  *
4979  * Let res^k and todo^k be the results after k steps and let i = k + 1.
4980  * Assume we are computing the lexicographical maximum.
4981  * We first compute the lexicographically maximal element in basic map i.
4982  * This results in a partial solution res_i and a subset todo_i.
4983  * Then we combine these results with those obtain for the first k basic maps
4984  * to obtain a result that is valid for the first k+1 basic maps.
4985  * In particular, the set where there is no solution is the set where
4986  * there is no solution for the first k basic maps and also no solution
4987  * for the ith basic map, i.e.,
4988  *
4989  *      todo^i = todo^k * todo_i
4990  *
4991  * On dom(res^k) * dom(res_i), we need to pick the larger of the two
4992  * solutions, arbitrarily breaking ties in favor of res^k.
4993  * That is, when res^k(a) >= res_i(a), we pick res^k and
4994  * when res^k(a) < res_i(a), we pick res_i.  (Here, ">=" and "<" denote
4995  * the lexicographic order.)
4996  * In practice, we compute
4997  *
4998  *      res^k * (res_i . "<=")
4999  *
5000  * and
5001  *
5002  *      res_i * (res^k . "<")
5003  *
5004  * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5005  * where only one of res^k and res_i provides a solution and we simply pick
5006  * that one, i.e.,
5007  *
5008  *      res^k * todo_i
5009  * and
5010  *      res_i * todo^k
5011  *
5012  * Note that we only compute these intersections when dom(res^k) intersects
5013  * dom(res_i).  Otherwise, the only effect of these intersections is to
5014  * potentially break up res^k and res_i into smaller pieces.
5015  * We want to avoid such splintering as much as possible.
5016  * In fact, an earlier implementation of this function would look for
5017  * better results in the domain of res^k and for extra results in todo^k,
5018  * but this would always result in a splintering according to todo^k,
5019  * even when the domain of basic map i is disjoint from the domains of
5020  * the previous basic maps.
5021  */
5022 static __isl_give isl_map *isl_map_partial_lexopt(
5023                 __isl_take isl_map *map, __isl_take isl_set *dom,
5024                 __isl_give isl_set **empty, int max)
5025 {
5026         int i;
5027         struct isl_map *res;
5028         struct isl_set *todo;
5029
5030         if (!map || !dom)
5031                 goto error;
5032
5033         if (isl_map_plain_is_empty(map)) {
5034                 if (empty)
5035                         *empty = dom;
5036                 else
5037                         isl_set_free(dom);
5038                 return map;
5039         }
5040
5041         res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5042                                         isl_set_copy(dom), &todo, max);
5043
5044         for (i = 1; i < map->n; ++i) {
5045                 isl_map *lt, *le;
5046                 isl_map *res_i;
5047                 isl_set *todo_i;
5048                 isl_dim *dim = isl_dim_range(isl_map_get_dim(res));
5049
5050                 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5051                                         isl_set_copy(dom), &todo_i, max);
5052
5053                 if (max) {
5054                         lt = isl_map_lex_lt(isl_dim_copy(dim));
5055                         le = isl_map_lex_le(dim);
5056                 } else {
5057                         lt = isl_map_lex_gt(isl_dim_copy(dim));
5058                         le = isl_map_lex_ge(dim);
5059                 }
5060                 lt = isl_map_apply_range(isl_map_copy(res), lt);
5061                 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5062                 le = isl_map_apply_range(isl_map_copy(res_i), le);
5063                 le = isl_map_intersect(le, isl_map_copy(res));
5064
5065                 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5066                         res = isl_map_intersect_domain(res,
5067                                                         isl_set_copy(todo_i));
5068                         res_i = isl_map_intersect_domain(res_i,
5069                                                         isl_set_copy(todo));
5070                 }
5071
5072                 res = isl_map_union_disjoint(res, res_i);
5073                 res = isl_map_union_disjoint(res, lt);
5074                 res = isl_map_union_disjoint(res, le);
5075
5076                 todo = isl_set_intersect(todo, todo_i);
5077         }
5078
5079         isl_set_free(dom);
5080         isl_map_free(map);
5081
5082         if (empty)
5083                 *empty = todo;
5084         else
5085                 isl_set_free(todo);
5086
5087         return res;
5088 error:
5089         if (empty)
5090                 *empty = NULL;
5091         isl_set_free(dom);
5092         isl_map_free(map);
5093         return NULL;
5094 }
5095
5096 __isl_give isl_map *isl_map_partial_lexmax(
5097                 __isl_take isl_map *map, __isl_take isl_set *dom,
5098                 __isl_give isl_set **empty)
5099 {
5100         return isl_map_partial_lexopt(map, dom, empty, 1);
5101 }
5102
5103 __isl_give isl_map *isl_map_partial_lexmin(
5104                 __isl_take isl_map *map, __isl_take isl_set *dom,
5105                 __isl_give isl_set **empty)
5106 {
5107         return isl_map_partial_lexopt(map, dom, empty, 0);
5108 }
5109
5110 __isl_give isl_set *isl_set_partial_lexmin(
5111                 __isl_take isl_set *set, __isl_take isl_set *dom,
5112                 __isl_give isl_set **empty)
5113 {
5114         return (struct isl_set *)
5115                 isl_map_partial_lexmin((struct isl_map *)set,
5116                         dom, empty);
5117 }
5118
5119 __isl_give isl_set *isl_set_partial_lexmax(
5120                 __isl_take isl_set *set, __isl_take isl_set *dom,
5121                 __isl_give isl_set **empty)
5122 {
5123         return (struct isl_set *)
5124                 isl_map_partial_lexmax((struct isl_map *)set,
5125                         dom, empty);
5126 }
5127
5128 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5129 {
5130         struct isl_basic_set *dom = NULL;
5131         struct isl_dim *dom_dim;
5132
5133         if (!bmap)
5134                 goto error;
5135         dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
5136         dom = isl_basic_set_universe(dom_dim);
5137         return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
5138 error:
5139         isl_basic_map_free(bmap);
5140         return NULL;
5141 }
5142
5143 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
5144 {
5145         return isl_basic_map_lexopt(bmap, 0);
5146 }
5147
5148 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
5149 {
5150         return isl_basic_map_lexopt(bmap, 1);
5151 }
5152
5153 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
5154 {
5155         return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
5156 }
5157
5158 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
5159 {
5160         return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
5161 }
5162
5163 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
5164 {
5165         struct isl_set *dom = NULL;
5166         struct isl_dim *dom_dim;
5167
5168         if (!map)
5169                 goto error;
5170         dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
5171         dom = isl_set_universe(dom_dim);
5172         return isl_map_partial_lexopt(map, dom, NULL, max);
5173 error:
5174         isl_map_free(map);
5175         return NULL;
5176 }
5177
5178 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
5179 {
5180         return isl_map_lexopt(map, 0);
5181 }
5182
5183 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
5184 {
5185         return isl_map_lexopt(map, 1);
5186 }
5187
5188 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
5189 {
5190         return (isl_set *)isl_map_lexmin((isl_map *)set);
5191 }
5192
5193 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
5194 {
5195         return (isl_set *)isl_map_lexmax((isl_map *)set);
5196 }
5197
5198 /* Construct a map that equates the two given dimensions in the given space.
5199  */
5200 static __isl_give isl_map *equate(__isl_take isl_dim *dim,
5201         enum isl_dim_type src_type, int src_pos,
5202         enum isl_dim_type dst_type, int dst_pos)
5203 {
5204         isl_basic_map *bmap;
5205         int k;
5206
5207         bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
5208         k = isl_basic_map_alloc_equality(bmap);
5209         if (k < 0)
5210                 goto error;
5211         isl_seq_clr(bmap->eq[k], 1 + isl_basic_map_total_dim(bmap));
5212         src_pos += isl_basic_map_offset(bmap, src_type);
5213         dst_pos += isl_basic_map_offset(bmap, dst_type);
5214         isl_int_set_si(bmap->eq[k][src_pos], 1);
5215         isl_int_set_si(bmap->eq[k][dst_pos], -1);
5216
5217         return isl_map_from_basic_map(bmap);
5218 error:
5219         isl_basic_map_free(bmap);
5220         return NULL;
5221 }
5222
5223 /* Extract the first and only affine expression from list
5224  * and then add it to *pwaff with the given dom.
5225  * This domain is known to be disjoint from other domains
5226  * because of the way isl_basic_set_foreach_lexmax works.
5227  */
5228 static int update_dim_max(__isl_take isl_basic_set *dom,
5229         __isl_take isl_aff_list *list, void *user)
5230 {
5231         isl_ctx *ctx = isl_basic_set_get_ctx(dom);
5232         isl_aff *aff;
5233         isl_pw_aff **pwaff = user;
5234         isl_pw_aff *pwaff_i;
5235
5236         if (isl_aff_list_n_aff(list) != 1)
5237                 isl_die(ctx, isl_error_internal,
5238                         "expecting single element list", goto error);
5239
5240         aff = isl_aff_list_get_aff(list, 0);
5241         pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
5242
5243         *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
5244
5245         isl_aff_list_free(list);
5246
5247         return 0;
5248 error:
5249         isl_basic_set_free(dom);
5250         isl_aff_list_free(list);
5251         return -1;
5252 }
5253
5254 /* Given a one-dimensional basic set, compute the maximum of that
5255  * dimension as an isl_pw_aff.
5256  *
5257  * The isl_pw_aff is constructed by having isl_basic_set_foreach_lexmax
5258  * call update_dim_max on each leaf of the result.
5259  */
5260 static __isl_give isl_pw_aff *basic_set_dim_max(__isl_keep isl_basic_set *bset)
5261 {
5262         isl_dim *dim = isl_basic_set_get_dim(bset);
5263         isl_pw_aff *pwaff;
5264         int r;
5265
5266         dim = isl_dim_domain(isl_dim_from_range(dim));
5267         pwaff = isl_pw_aff_empty(dim);
5268
5269         r = isl_basic_set_foreach_lexmax(bset, &update_dim_max, &pwaff);
5270         if (r < 0)
5271                 return isl_pw_aff_free(pwaff);
5272
5273         return pwaff;
5274 }
5275
5276 /* Compute the maximum of the given set dimension as a function of the
5277  * parameters, but independently of the other set dimensions.
5278  *
5279  * We first project the set onto the given dimension and then compute
5280  * the "lexicographic" maximum in each basic set, combining the results
5281  * using isl_pw_aff_union_max.
5282  */
5283 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
5284 {
5285         int i;
5286         isl_map *map;
5287         isl_pw_aff *pwaff;
5288
5289         map = isl_map_from_domain(set);
5290         map = isl_map_add_dims(map, isl_dim_out, 1);
5291         map = isl_map_intersect(map,
5292                 equate(isl_map_get_dim(map), isl_dim_in, pos,
5293                                              isl_dim_out, 0));
5294         set = isl_map_range(map);
5295         if (!set)
5296                 return NULL;
5297
5298         if (set->n == 0) {
5299                 isl_dim *dim = isl_set_get_dim(set);
5300                 dim = isl_dim_domain(isl_dim_from_range(dim));
5301                 isl_set_free(set);
5302                 return isl_pw_aff_empty(dim);
5303         }
5304
5305         pwaff = basic_set_dim_max(set->p[0]);
5306         for (i = 1; i < set->n; ++i) {
5307                 isl_pw_aff *pwaff_i;
5308
5309                 pwaff_i = basic_set_dim_max(set->p[i]);
5310                 pwaff = isl_pw_aff_union_max(pwaff, pwaff_i);
5311         }
5312
5313         isl_set_free(set);
5314
5315         return pwaff;
5316 }
5317
5318 /* Apply a preimage specified by "mat" on the parameters of "bset".
5319  * bset is assumed to have only parameters and divs.
5320  */
5321 static struct isl_basic_set *basic_set_parameter_preimage(
5322         struct isl_basic_set *bset, struct isl_mat *mat)
5323 {
5324         unsigned nparam;
5325
5326         if (!bset || !mat)
5327                 goto error;
5328
5329         bset->dim = isl_dim_cow(bset->dim);
5330         if (!bset->dim)
5331                 goto error;
5332
5333         nparam = isl_basic_set_dim(bset, isl_dim_param);
5334
5335         isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
5336
5337         bset->dim->nparam = 0;
5338         bset->dim->n_out = nparam;
5339         bset = isl_basic_set_preimage(bset, mat);
5340         if (bset) {
5341                 bset->dim->nparam = bset->dim->n_out;
5342                 bset->dim->n_out = 0;
5343         }
5344         return bset;
5345 error:
5346         isl_mat_free(mat);
5347         isl_basic_set_free(bset);
5348         return NULL;
5349 }
5350
5351 /* Apply a preimage specified by "mat" on the parameters of "set".
5352  * set is assumed to have only parameters and divs.
5353  */
5354 static struct isl_set *set_parameter_preimage(
5355         struct isl_set *set, struct isl_mat *mat)
5356 {
5357         struct isl_dim *dim = NULL;
5358         unsigned nparam;
5359
5360         if (!set || !mat)
5361                 goto error;
5362
5363         dim = isl_dim_copy(set->dim);
5364         dim = isl_dim_cow(dim);
5365         if (!dim)
5366                 goto error;
5367
5368         nparam = isl_set_dim(set, isl_dim_param);
5369
5370         isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
5371
5372         dim->nparam = 0;
5373         dim->n_out = nparam;
5374         isl_set_reset_dim(set, dim);
5375         set = isl_set_preimage(set, mat);
5376         if (!set)
5377                 goto error2;
5378         dim = isl_dim_copy(set->dim);
5379         dim = isl_dim_cow(dim);
5380         if (!dim)
5381                 goto error2;
5382         dim->nparam = dim->n_out;
5383         dim->n_out = 0;
5384         isl_set_reset_dim(set, dim);
5385         return set;
5386 error:
5387         isl_dim_free(dim);
5388         isl_mat_free(mat);
5389 error2:
5390         isl_set_free(set);
5391         return NULL;
5392 }
5393
5394 /* Intersect the basic set "bset" with the affine space specified by the
5395  * equalities in "eq".
5396  */
5397 static struct isl_basic_set *basic_set_append_equalities(
5398         struct isl_basic_set *bset, struct isl_mat *eq)
5399 {
5400         int i, k;
5401         unsigned len;
5402
5403         if (!bset || !eq)
5404                 goto error;
5405
5406         bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
5407                                         eq->n_row, 0);
5408         if (!bset)
5409                 goto error;
5410
5411         len = 1 + isl_dim_total(bset->dim) + bset->extra;
5412         for (i = 0; i < eq->n_row; ++i) {
5413                 k = isl_basic_set_alloc_equality(bset);
5414                 if (k < 0)
5415                         goto error;
5416                 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
5417                 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
5418         }
5419         isl_mat_free(eq);
5420
5421         bset = isl_basic_set_gauss(bset, NULL);
5422         bset = isl_basic_set_finalize(bset);
5423
5424         return bset;
5425 error:
5426         isl_mat_free(eq);
5427         isl_basic_set_free(bset);
5428         return NULL;
5429 }
5430
5431 /* Intersect the set "set" with the affine space specified by the
5432  * equalities in "eq".
5433  */
5434 static struct isl_set *set_append_equalities(struct isl_set *set,
5435         struct isl_mat *eq)
5436 {
5437         int i;
5438
5439         if (!set || !eq)
5440                 goto error;
5441
5442         for (i = 0; i < set->n; ++i) {
5443                 set->p[i] = basic_set_append_equalities(set->p[i],
5444                                         isl_mat_copy(eq));
5445                 if (!set->p[i])
5446                         goto error;
5447         }
5448         isl_mat_free(eq);
5449         return set;
5450 error:
5451         isl_mat_free(eq);
5452         isl_set_free(set);
5453         return NULL;
5454 }
5455
5456 /* Project the given basic set onto its parameter domain, possibly introducing
5457  * new, explicit, existential variables in the constraints.
5458  * The input has parameters and (possibly implicit) existential variables.
5459  * The output has the same parameters, but only
5460  * explicit existentially quantified variables.
5461  *
5462  * The actual projection is performed by pip, but pip doesn't seem
5463  * to like equalities very much, so we first remove the equalities
5464  * among the parameters by performing a variable compression on
5465  * the parameters.  Afterward, an inverse transformation is performed
5466  * and the equalities among the parameters are inserted back in.
5467  */
5468 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
5469 {
5470         int i, j;
5471         struct isl_mat *eq;
5472         struct isl_mat *T, *T2;
5473         struct isl_set *set;
5474         unsigned nparam, n_div;
5475
5476         bset = isl_basic_set_cow(bset);
5477         if (!bset)
5478                 return NULL;
5479
5480         if (bset->n_eq == 0)
5481                 return isl_basic_set_lexmin(bset);
5482
5483         isl_basic_set_gauss(bset, NULL);
5484
5485         nparam = isl_basic_set_dim(bset, isl_dim_param);
5486         n_div = isl_basic_set_dim(bset, isl_dim_div);
5487
5488         for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
5489                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
5490                         ++i;
5491         }
5492         if (i == bset->n_eq)
5493                 return isl_basic_set_lexmin(bset);
5494
5495         eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
5496                 0, 1 + nparam);
5497         eq = isl_mat_cow(eq);
5498         T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
5499         if (T && T->n_col == 0) {
5500                 isl_mat_free(T);
5501                 isl_mat_free(T2);
5502                 isl_mat_free(eq);
5503                 bset = isl_basic_set_set_to_empty(bset);
5504                 return isl_set_from_basic_set(bset);
5505         }
5506         bset = basic_set_parameter_preimage(bset, T);
5507
5508         set = isl_basic_set_lexmin(bset);
5509         set = set_parameter_preimage(set, T2);
5510         set = set_append_equalities(set, eq);
5511         return set;
5512 }
5513
5514 /* Compute an explicit representation for all the existentially
5515  * quantified variables.
5516  * The input and output dimensions are first turned into parameters.
5517  * compute_divs then returns a map with the same parameters and
5518  * no input or output dimensions and the dimension specification
5519  * is reset to that of the input.
5520  */
5521 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
5522 {
5523         struct isl_basic_set *bset;
5524         struct isl_set *set;
5525         struct isl_map *map;
5526         struct isl_dim *dim, *orig_dim = NULL;
5527         unsigned         nparam;
5528         unsigned         n_in;
5529         unsigned         n_out;
5530
5531         bmap = isl_basic_map_cow(bmap);
5532         if (!bmap)
5533                 return NULL;
5534
5535         nparam = isl_basic_map_dim(bmap, isl_dim_param);
5536         n_in = isl_basic_map_dim(bmap, isl_dim_in);
5537         n_out = isl_basic_map_dim(bmap, isl_dim_out);
5538         dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
5539         if (!dim)
5540                 goto error;
5541
5542         orig_dim = bmap->dim;
5543         bmap->dim = dim;
5544         bset = (struct isl_basic_set *)bmap;
5545
5546         set = parameter_compute_divs(bset);
5547         map = (struct isl_map *)set;
5548         map = isl_map_reset_dim(map, orig_dim);
5549
5550         return map;
5551 error:
5552         isl_basic_map_free(bmap);
5553         return NULL;
5554 }
5555
5556 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
5557 {
5558         int i;
5559         unsigned off;
5560
5561         if (!bmap)
5562                 return -1;
5563
5564         off = isl_dim_total(bmap->dim);
5565         for (i = 0; i < bmap->n_div; ++i) {
5566                 if (isl_int_is_zero(bmap->div[i][0]))
5567                         return 0;
5568                 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
5569                                 return -1);
5570         }
5571         return 1;
5572 }
5573
5574 static int map_divs_known(__isl_keep isl_map *map)
5575 {
5576         int i;
5577
5578         if (!map)
5579                 return -1;
5580
5581         for (i = 0; i < map->n; ++i) {
5582                 int known = isl_basic_map_divs_known(map->p[i]);
5583                 if (known <= 0)
5584                         return known;
5585         }
5586
5587         return 1;
5588 }
5589
5590 /* If bmap contains any unknown divs, then compute explicit
5591  * expressions for them.  However, this computation may be
5592  * quite expensive, so first try to remove divs that aren't
5593  * strictly needed.
5594  */
5595 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
5596 {
5597         int known;
5598         struct isl_map *map;
5599
5600         known = isl_basic_map_divs_known(bmap);
5601         if (known < 0)
5602                 goto error;
5603         if (known)
5604                 return isl_map_from_basic_map(bmap);
5605
5606         bmap = isl_basic_map_drop_redundant_divs(bmap);
5607
5608         known = isl_basic_map_divs_known(bmap);
5609         if (known < 0)
5610                 goto error;
5611         if (known)
5612                 return isl_map_from_basic_map(bmap);
5613
5614         map = compute_divs(bmap);
5615         return map;
5616 error:
5617         isl_basic_map_free(bmap);
5618         return NULL;
5619 }
5620
5621 struct isl_map *isl_map_compute_divs(struct isl_map *map)
5622 {
5623         int i;
5624         int known;
5625         struct isl_map *res;
5626
5627         if (!map)
5628                 return NULL;
5629         if (map->n == 0)
5630                 return map;
5631
5632         known = map_divs_known(map);
5633         if (known < 0) {
5634                 isl_map_free(map);
5635                 return NULL;
5636         }
5637         if (known)
5638                 return map;
5639
5640         res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
5641         for (i = 1 ; i < map->n; ++i) {
5642                 struct isl_map *r2;
5643                 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
5644                 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
5645                         res = isl_map_union_disjoint(res, r2);
5646                 else
5647                         res = isl_map_union(res, r2);
5648         }
5649         isl_map_free(map);
5650
5651         return res;
5652 }
5653
5654 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
5655 {
5656         return (struct isl_set *)
5657                 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
5658 }
5659
5660 struct isl_set *isl_set_compute_divs(struct isl_set *set)
5661 {
5662         return (struct isl_set *)
5663                 isl_map_compute_divs((struct isl_map *)set);
5664 }
5665
5666 struct isl_set *isl_map_domain(struct isl_map *map)
5667 {
5668         int i;
5669         struct isl_set *set;
5670
5671         if (!map)
5672                 goto error;
5673
5674         map = isl_map_cow(map);
5675         if (!map)
5676                 return NULL;
5677
5678         set = (struct isl_set *)map;
5679         set->dim = isl_dim_domain(set->dim);
5680         if (!set->dim)
5681                 goto error;
5682         for (i = 0; i < map->n; ++i) {
5683                 set->p[i] = isl_basic_map_domain(map->p[i]);
5684                 if (!set->p[i])
5685                         goto error;
5686         }
5687         ISL_F_CLR(set, ISL_MAP_DISJOINT);
5688         ISL_F_CLR(set, ISL_SET_NORMALIZED);
5689         return set;
5690 error:
5691         isl_map_free(map);
5692         return NULL;
5693 }
5694
5695 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
5696         __isl_take isl_map *map2)
5697 {
5698         int i;
5699         unsigned flags = 0;
5700         struct isl_map *map = NULL;
5701
5702         if (!map1 || !map2)
5703                 goto error;
5704
5705         if (map1->n == 0) {
5706                 isl_map_free(map1);
5707                 return map2;
5708         }
5709         if (map2->n == 0) {
5710                 isl_map_free(map2);
5711                 return map1;
5712         }
5713
5714         isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
5715
5716         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5717             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5718                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5719
5720         map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
5721                                 map1->n + map2->n, flags);
5722         if (!map)
5723                 goto error;
5724         for (i = 0; i < map1->n; ++i) {
5725                 map = isl_map_add_basic_map(map,
5726                                   isl_basic_map_copy(map1->p[i]));
5727                 if (!map)
5728                         goto error;
5729         }
5730         for (i = 0; i < map2->n; ++i) {
5731                 map = isl_map_add_basic_map(map,
5732                                   isl_basic_map_copy(map2->p[i]));
5733                 if (!map)
5734                         goto error;
5735         }
5736         isl_map_free(map1);
5737         isl_map_free(map2);
5738         return map;
5739 error:
5740         isl_map_free(map);
5741         isl_map_free(map1);
5742         isl_map_free(map2);
5743         return NULL;
5744 }
5745
5746 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
5747         __isl_take isl_map *map2)
5748 {
5749         return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
5750 }
5751
5752 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
5753 {
5754         map1 = isl_map_union_disjoint(map1, map2);
5755         if (!map1)
5756                 return NULL;
5757         if (map1->n > 1)
5758                 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
5759         return map1;
5760 }
5761
5762 struct isl_set *isl_set_union_disjoint(
5763                         struct isl_set *set1, struct isl_set *set2)
5764 {
5765         return (struct isl_set *)
5766                 isl_map_union_disjoint(
5767                         (struct isl_map *)set1, (struct isl_map *)set2);
5768 }
5769
5770 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
5771 {
5772         return (struct isl_set *)
5773                 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
5774 }
5775
5776 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
5777         __isl_take isl_set *set)
5778 {
5779         unsigned flags = 0;
5780         struct isl_map *result;
5781         int i, j;
5782
5783         if (!map || !set)
5784                 goto error;
5785
5786         if (!isl_dim_match(map->dim, isl_dim_param, set->dim, isl_dim_param))
5787                 isl_die(set->ctx, isl_error_invalid,
5788                         "parameters don't match", goto error);
5789
5790         if (isl_dim_size(set->dim, isl_dim_set) != 0 &&
5791             !isl_map_compatible_range(map, set))
5792                 isl_die(set->ctx, isl_error_invalid,
5793                         "incompatible spaces", goto error);
5794
5795         if (isl_set_plain_is_universe(set)) {
5796                 isl_set_free(set);
5797                 return map;
5798         }
5799
5800         if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
5801             ISL_F_ISSET(set, ISL_MAP_DISJOINT))
5802                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5803
5804         result = isl_map_alloc_dim(isl_dim_copy(map->dim),
5805                                         map->n * set->n, flags);
5806         if (!result)
5807                 goto error;
5808         for (i = 0; i < map->n; ++i)
5809                 for (j = 0; j < set->n; ++j) {
5810                         result = isl_map_add_basic_map(result,
5811                             isl_basic_map_intersect_range(
5812                                 isl_basic_map_copy(map->p[i]),
5813                                 isl_basic_set_copy(set->p[j])));
5814                         if (!result)
5815                                 goto error;
5816                 }
5817         isl_map_free(map);
5818         isl_set_free(set);
5819         return result;
5820 error:
5821         isl_map_free(map);
5822         isl_set_free(set);
5823         return NULL;
5824 }
5825
5826 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
5827         __isl_take isl_set *set)
5828 {
5829         return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
5830 }
5831
5832 struct isl_map *isl_map_intersect_domain(
5833                 struct isl_map *map, struct isl_set *set)
5834 {
5835         return isl_map_reverse(
5836                 isl_map_intersect_range(isl_map_reverse(map), set));
5837 }
5838
5839 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
5840         __isl_take isl_map *map2)
5841 {
5842         if (!map1 || !map2)
5843                 goto error;
5844         map1 = isl_map_reverse(map1);
5845         map1 = isl_map_apply_range(map1, map2);
5846         return isl_map_reverse(map1);
5847 error:
5848         isl_map_free(map1);
5849         isl_map_free(map2);
5850         return NULL;
5851 }
5852
5853 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
5854         __isl_take isl_map *map2)
5855 {
5856         return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
5857 }
5858
5859 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
5860         __isl_take isl_map *map2)
5861 {
5862         struct isl_dim *dim_result;
5863         struct isl_map *result;
5864         int i, j;
5865
5866         if (!map1 || !map2)
5867                 goto error;
5868
5869         dim_result = isl_dim_join(isl_dim_copy(map1->dim),
5870                                   isl_dim_copy(map2->dim));
5871
5872         result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
5873         if (!result)
5874                 goto error;
5875         for (i = 0; i < map1->n; ++i)
5876                 for (j = 0; j < map2->n; ++j) {
5877                         result = isl_map_add_basic_map(result,
5878                             isl_basic_map_apply_range(
5879                                 isl_basic_map_copy(map1->p[i]),
5880                                 isl_basic_map_copy(map2->p[j])));
5881                         if (!result)
5882                                 goto error;
5883                 }
5884         isl_map_free(map1);
5885         isl_map_free(map2);
5886         if (result && result->n <= 1)
5887                 ISL_F_SET(result, ISL_MAP_DISJOINT);
5888         return result;
5889 error:
5890         isl_map_free(map1);
5891         isl_map_free(map2);
5892         return NULL;
5893 }
5894
5895 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
5896         __isl_take isl_map *map2)
5897 {
5898         return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
5899 }
5900
5901 /*
5902  * returns range - domain
5903  */
5904 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
5905 {
5906         isl_dim *dims, *target_dim;
5907         struct isl_basic_set *bset;
5908         unsigned dim;
5909         unsigned nparam;
5910         int i;
5911
5912         if (!bmap)
5913                 goto error;
5914         isl_assert(bmap->ctx, isl_dim_tuple_match(bmap->dim, isl_dim_in,
5915                                                   bmap->dim, isl_dim_out),
5916                    goto error);
5917         target_dim = isl_dim_domain(isl_basic_map_get_dim(bmap));
5918         dim = isl_basic_map_n_in(bmap);
5919         nparam = isl_basic_map_n_param(bmap);
5920         bset = isl_basic_set_from_basic_map(bmap);
5921         bset = isl_basic_set_cow(bset);
5922         dims = isl_basic_set_get_dim(bset);
5923         dims = isl_dim_add(dims, isl_dim_set, dim);
5924         bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
5925         bset = isl_basic_set_swap_vars(bset, 2*dim);
5926         for (i = 0; i < dim; ++i) {
5927                 int j = isl_basic_map_alloc_equality(
5928                                             (struct isl_basic_map *)bset);
5929                 if (j < 0)
5930                         goto error;
5931                 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
5932                 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
5933                 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
5934                 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
5935         }
5936         bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
5937         bset = isl_basic_set_reset_dim(bset, target_dim);
5938         return bset;
5939 error:
5940         isl_basic_map_free(bmap);
5941         return NULL;
5942 }
5943
5944 /*
5945  * returns range - domain
5946  */
5947 struct isl_set *isl_map_deltas(struct isl_map *map)
5948 {
5949         int i;
5950         isl_dim *dim;
5951         struct isl_set *result;
5952
5953         if (!map)
5954                 return NULL;
5955
5956         isl_assert(map->ctx, isl_dim_tuple_match(map->dim, isl_dim_in,
5957                                                  map->dim, isl_dim_out),
5958                    goto error);
5959         dim = isl_map_get_dim(map);
5960         dim = isl_dim_domain(dim);
5961         result = isl_set_alloc_dim(dim, map->n, 0);
5962         if (!result)
5963                 goto error;
5964         for (i = 0; i < map->n; ++i)
5965                 result = isl_set_add_basic_set(result,
5966                           isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
5967         isl_map_free(map);
5968         return result;
5969 error:
5970         isl_map_free(map);
5971         return NULL;
5972 }
5973
5974 /*
5975  * returns [domain -> range] -> range - domain
5976  */
5977 __isl_give isl_basic_map *isl_basic_map_deltas_map(
5978         __isl_take isl_basic_map *bmap)
5979 {
5980         int i, k;
5981         isl_dim *dim;
5982         isl_basic_map *domain;
5983         int nparam, n;
5984         unsigned total;
5985
5986         if (!isl_dim_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
5987                 isl_die(bmap->ctx, isl_error_invalid,
5988                         "domain and range don't match", goto error);
5989
5990         nparam = isl_basic_map_dim(bmap, isl_dim_param);
5991         n = isl_basic_map_dim(bmap, isl_dim_in);
5992
5993         dim = isl_dim_from_range(isl_dim_domain(isl_basic_map_get_dim(bmap)));
5994         domain = isl_basic_map_universe(dim);
5995
5996         bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
5997         bmap = isl_basic_map_apply_range(bmap, domain);
5998         bmap = isl_basic_map_extend_constraints(bmap, n, 0);
5999
6000         total = isl_basic_map_total_dim(bmap);
6001
6002         for (i = 0; i < n; ++i) {
6003                 k = isl_basic_map_alloc_equality(bmap);
6004                 if (k < 0)
6005                         goto error;
6006                 isl_seq_clr(bmap->eq[k], 1 + total);
6007                 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6008                 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6009                 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6010         }
6011
6012         bmap = isl_basic_map_gauss(bmap, NULL);
6013         return isl_basic_map_finalize(bmap);
6014 error:
6015         isl_basic_map_free(bmap);
6016         return NULL;
6017 }
6018
6019 /*
6020  * returns [domain -> range] -> range - domain
6021  */
6022 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6023 {
6024         int i;
6025         isl_dim *domain_dim;
6026
6027         if (!map)
6028                 return NULL;
6029
6030         if (!isl_dim_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6031                 isl_die(map->ctx, isl_error_invalid,
6032                         "domain and range don't match", goto error);
6033
6034         map = isl_map_cow(map);
6035         if (!map)
6036                 return NULL;
6037
6038         domain_dim = isl_dim_from_range(isl_dim_domain(isl_map_get_dim(map)));
6039         map->dim = isl_dim_from_domain(isl_dim_wrap(map->dim));
6040         map->dim = isl_dim_join(map->dim, domain_dim);
6041         if (!map->dim)
6042                 goto error;
6043         for (i = 0; i < map->n; ++i) {
6044                 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6045                 if (!map->p[i])
6046                         goto error;
6047         }
6048         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6049         return map;
6050 error:
6051         isl_map_free(map);
6052         return NULL;
6053 }
6054
6055 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
6056 {
6057         struct isl_basic_map *bmap;
6058         unsigned nparam;
6059         unsigned dim;
6060         int i;
6061
6062         if (!dims)
6063                 return NULL;
6064
6065         nparam = dims->nparam;
6066         dim = dims->n_out;
6067         bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
6068         if (!bmap)
6069                 goto error;
6070
6071         for (i = 0; i < dim; ++i) {
6072                 int j = isl_basic_map_alloc_equality(bmap);
6073                 if (j < 0)
6074                         goto error;
6075                 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6076                 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6077                 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6078         }
6079         return isl_basic_map_finalize(bmap);
6080 error:
6081         isl_basic_map_free(bmap);
6082         return NULL;
6083 }
6084
6085 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_dim *dim)
6086 {
6087         if (!dim)
6088                 return NULL;
6089         if (dim->n_in != dim->n_out)
6090                 isl_die(dim->ctx, isl_error_invalid,
6091                         "number of input and output dimensions needs to be "
6092                         "the same", goto error);
6093         return basic_map_identity(dim);
6094 error:
6095         isl_dim_free(dim);
6096         return NULL;
6097 }
6098
6099 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
6100 {
6101         if (!model || !model->dim)
6102                 return NULL;
6103         return isl_basic_map_identity(isl_dim_copy(model->dim));
6104 }
6105
6106 __isl_give isl_map *isl_map_identity(__isl_take isl_dim *dim)
6107 {
6108         return isl_map_from_basic_map(isl_basic_map_identity(dim));
6109 }
6110
6111 struct isl_map *isl_map_identity_like(struct isl_map *model)
6112 {
6113         if (!model || !model->dim)
6114                 return NULL;
6115         return isl_map_identity(isl_dim_copy(model->dim));
6116 }
6117
6118 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
6119 {
6120         if (!model || !model->dim)
6121                 return NULL;
6122         return isl_map_identity(isl_dim_copy(model->dim));
6123 }
6124
6125 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
6126 {
6127         isl_dim *dim = isl_set_get_dim(set);
6128         isl_map *id;
6129         id = isl_map_identity(isl_dim_map_from_set(dim));
6130         return isl_map_intersect_range(id, set);
6131 }
6132
6133 /* Construct a basic set with all set dimensions having only non-negative
6134  * values.
6135  */
6136 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
6137 {
6138         int i;
6139         unsigned nparam;
6140         unsigned dim;
6141         struct isl_basic_set *bset;
6142
6143         if (!dims)
6144                 return NULL;
6145         nparam = dims->nparam;
6146         dim = dims->n_out;
6147         bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
6148         if (!bset)
6149                 return NULL;
6150         for (i = 0; i < dim; ++i) {
6151                 int k = isl_basic_set_alloc_inequality(bset);
6152                 if (k < 0)
6153                         goto error;
6154                 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
6155                 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
6156         }
6157         return bset;
6158 error:
6159         isl_basic_set_free(bset);
6160         return NULL;
6161 }
6162
6163 /* Construct the half-space x_pos >= 0.
6164  */
6165 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_dim *dim,
6166         int pos)
6167 {
6168         int k;
6169         isl_basic_set *nonneg;
6170
6171         nonneg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
6172         k = isl_basic_set_alloc_inequality(nonneg);
6173         if (k < 0)
6174                 goto error;
6175         isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
6176         isl_int_set_si(nonneg->ineq[k][pos], 1);
6177
6178         return isl_basic_set_finalize(nonneg);
6179 error:
6180         isl_basic_set_free(nonneg);
6181         return NULL;
6182 }
6183
6184 /* Construct the half-space x_pos <= -1.
6185  */
6186 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_dim *dim, int pos)
6187 {
6188         int k;
6189         isl_basic_set *neg;
6190
6191         neg = isl_basic_set_alloc_dim(dim, 0, 0, 1);
6192         k = isl_basic_set_alloc_inequality(neg);
6193         if (k < 0)
6194                 goto error;
6195         isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
6196         isl_int_set_si(neg->ineq[k][0], -1);
6197         isl_int_set_si(neg->ineq[k][pos], -1);
6198
6199         return isl_basic_set_finalize(neg);
6200 error:
6201         isl_basic_set_free(neg);
6202         return NULL;
6203 }
6204
6205 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
6206         enum isl_dim_type type, unsigned first, unsigned n)
6207 {
6208         int i;
6209         isl_basic_set *nonneg;
6210         isl_basic_set *neg;
6211
6212         if (!set)
6213                 return NULL;
6214         if (n == 0)
6215                 return set;
6216
6217         isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
6218
6219         for (i = 0; i < n; ++i) {
6220                 nonneg = nonneg_halfspace(isl_set_get_dim(set),
6221                                           pos(set->dim, type) + first + i);
6222                 neg = neg_halfspace(isl_set_get_dim(set),
6223                                           pos(set->dim, type) + first + i);
6224
6225                 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
6226         }
6227
6228         return set;
6229 error:
6230         isl_set_free(set);
6231         return NULL;
6232 }
6233
6234 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
6235         int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6236         void *user)
6237 {
6238         isl_set *half;
6239
6240         if (!set)
6241                 return -1;
6242         if (isl_set_plain_is_empty(set)) {
6243                 isl_set_free(set);
6244                 return 0;
6245         }
6246         if (first == len)
6247                 return fn(set, signs, user);
6248
6249         signs[first] = 1;
6250         half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_dim(set),
6251                                                         1 + first));
6252         half = isl_set_intersect(half, isl_set_copy(set));
6253         if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
6254                 goto error;
6255
6256         signs[first] = -1;
6257         half = isl_set_from_basic_set(neg_halfspace(isl_set_get_dim(set),
6258                                                         1 + first));
6259         half = isl_set_intersect(half, set);
6260         return foreach_orthant(half, signs, first + 1, len, fn, user);
6261 error:
6262         isl_set_free(set);
6263         return -1;
6264 }
6265
6266 /* Call "fn" on the intersections of "set" with each of the orthants
6267  * (except for obviously empty intersections).  The orthant is identified
6268  * by the signs array, with each entry having value 1 or -1 according
6269  * to the sign of the corresponding variable.
6270  */
6271 int isl_set_foreach_orthant(__isl_keep isl_set *set,
6272         int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
6273         void *user)
6274 {
6275         unsigned nparam;
6276         unsigned nvar;
6277         int *signs;
6278         int r;
6279
6280         if (!set)
6281                 return -1;
6282         if (isl_set_plain_is_empty(set))
6283                 return 0;
6284
6285         nparam = isl_set_dim(set, isl_dim_param);
6286         nvar = isl_set_dim(set, isl_dim_set);
6287
6288         signs = isl_alloc_array(set->ctx, int, nparam + nvar);
6289
6290         r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
6291                             fn, user);
6292
6293         free(signs);
6294
6295         return r;
6296 }
6297
6298 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
6299 {
6300         return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
6301 }
6302
6303 int isl_basic_map_is_subset(
6304                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6305 {
6306         int is_subset;
6307         struct isl_map *map1;
6308         struct isl_map *map2;
6309
6310         if (!bmap1 || !bmap2)
6311                 return -1;
6312
6313         map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
6314         map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
6315
6316         is_subset = isl_map_is_subset(map1, map2);
6317
6318         isl_map_free(map1);
6319         isl_map_free(map2);
6320
6321         return is_subset;
6322 }
6323
6324 int isl_basic_map_is_equal(
6325                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6326 {
6327         int is_subset;
6328
6329         if (!bmap1 || !bmap2)
6330                 return -1;
6331         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6332         if (is_subset != 1)
6333                 return is_subset;
6334         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6335         return is_subset;
6336 }
6337
6338 int isl_basic_set_is_equal(
6339                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6340 {
6341         return isl_basic_map_is_equal(
6342                 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
6343 }
6344
6345 int isl_map_is_empty(struct isl_map *map)
6346 {
6347         int i;
6348         int is_empty;
6349
6350         if (!map)
6351                 return -1;
6352         for (i = 0; i < map->n; ++i) {
6353                 is_empty = isl_basic_map_is_empty(map->p[i]);
6354                 if (is_empty < 0)
6355                         return -1;
6356                 if (!is_empty)
6357                         return 0;
6358         }
6359         return 1;
6360 }
6361
6362 int isl_map_plain_is_empty(__isl_keep isl_map *map)
6363 {
6364         return map ? map->n == 0 : -1;
6365 }
6366
6367 int isl_map_fast_is_empty(__isl_keep isl_map *map)
6368 {
6369         return isl_map_plain_is_empty(map);
6370 }
6371
6372 int isl_set_plain_is_empty(struct isl_set *set)
6373 {
6374         return set ? set->n == 0 : -1;
6375 }
6376
6377 int isl_set_fast_is_empty(__isl_keep isl_set *set)
6378 {
6379         return isl_set_plain_is_empty(set);
6380 }
6381
6382 int isl_set_is_empty(struct isl_set *set)
6383 {
6384         return isl_map_is_empty((struct isl_map *)set);
6385 }
6386
6387 int isl_map_has_equal_dim(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6388 {
6389         if (!map1 || !map2)
6390                 return -1;
6391
6392         return isl_dim_equal(map1->dim, map2->dim);
6393 }
6394
6395 int isl_set_has_equal_dim(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6396 {
6397         if (!set1 || !set2)
6398                 return -1;
6399
6400         return isl_dim_equal(set1->dim, set2->dim);
6401 }
6402
6403 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6404 {
6405         int is_subset;
6406
6407         if (!map1 || !map2)
6408                 return -1;
6409         is_subset = isl_map_is_subset(map1, map2);
6410         if (is_subset != 1)
6411                 return is_subset;
6412         is_subset = isl_map_is_subset(map2, map1);
6413         return is_subset;
6414 }
6415
6416 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
6417 {
6418         return align_params_map_map_and_test(map1, map2, &map_is_equal);
6419 }
6420
6421 int isl_basic_map_is_strict_subset(
6422                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6423 {
6424         int is_subset;
6425
6426         if (!bmap1 || !bmap2)
6427                 return -1;
6428         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
6429         if (is_subset != 1)
6430                 return is_subset;
6431         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
6432         if (is_subset == -1)
6433                 return is_subset;
6434         return !is_subset;
6435 }
6436
6437 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
6438 {
6439         int is_subset;
6440
6441         if (!map1 || !map2)
6442                 return -1;
6443         is_subset = isl_map_is_subset(map1, map2);
6444         if (is_subset != 1)
6445                 return is_subset;
6446         is_subset = isl_map_is_subset(map2, map1);
6447         if (is_subset == -1)
6448                 return is_subset;
6449         return !is_subset;
6450 }
6451
6452 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
6453 {
6454         return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
6455 }
6456
6457 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
6458 {
6459         if (!bmap)
6460                 return -1;
6461         return bmap->n_eq == 0 && bmap->n_ineq == 0;
6462 }
6463
6464 int isl_basic_set_is_universe(struct isl_basic_set *bset)
6465 {
6466         if (!bset)
6467                 return -1;
6468         return bset->n_eq == 0 && bset->n_ineq == 0;
6469 }
6470
6471 int isl_map_plain_is_universe(__isl_keep isl_map *map)
6472 {
6473         int i;
6474
6475         if (!map)
6476                 return -1;
6477
6478         for (i = 0; i < map->n; ++i) {
6479                 int r = isl_basic_map_is_universe(map->p[i]);
6480                 if (r < 0 || r)
6481                         return r;
6482         }
6483
6484         return 0;
6485 }
6486
6487 int isl_set_plain_is_universe(__isl_keep isl_set *set)
6488 {
6489         return isl_map_plain_is_universe((isl_map *) set);
6490 }
6491
6492 int isl_set_fast_is_universe(__isl_keep isl_set *set)
6493 {
6494         return isl_set_plain_is_universe(set);
6495 }
6496
6497 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
6498 {
6499         struct isl_basic_set *bset = NULL;
6500         struct isl_vec *sample = NULL;
6501         int empty;
6502         unsigned total;
6503
6504         if (!bmap)
6505                 return -1;
6506
6507         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
6508                 return 1;
6509
6510         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
6511                 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
6512                 copy = isl_basic_map_remove_redundancies(copy);
6513                 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
6514                 isl_basic_map_free(copy);
6515                 return empty;
6516         }
6517
6518         total = 1 + isl_basic_map_total_dim(bmap);
6519         if (bmap->sample && bmap->sample->size == total) {
6520                 int contains = isl_basic_map_contains(bmap, bmap->sample);
6521                 if (contains < 0)
6522                         return -1;
6523                 if (contains)
6524                         return 0;
6525         }
6526         isl_vec_free(bmap->sample);
6527         bmap->sample = NULL;
6528         bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
6529         if (!bset)
6530                 return -1;
6531         sample = isl_basic_set_sample_vec(bset);
6532         if (!sample)
6533                 return -1;
6534         empty = sample->size == 0;
6535         isl_vec_free(bmap->sample);
6536         bmap->sample = sample;
6537         if (empty)
6538                 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
6539
6540         return empty;
6541 }
6542
6543 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
6544 {
6545         if (!bmap)
6546                 return -1;
6547         return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
6548 }
6549
6550 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
6551 {
6552         return isl_basic_map_plain_is_empty(bmap);
6553 }
6554
6555 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
6556 {
6557         if (!bset)
6558                 return -1;
6559         return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
6560 }
6561
6562 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
6563 {
6564         return isl_basic_set_plain_is_empty(bset);
6565 }
6566
6567 int isl_basic_set_is_empty(struct isl_basic_set *bset)
6568 {
6569         return isl_basic_map_is_empty((struct isl_basic_map *)bset);
6570 }
6571
6572 struct isl_map *isl_basic_map_union(
6573         struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6574 {
6575         struct isl_map *map;
6576         if (!bmap1 || !bmap2)
6577                 return NULL;
6578
6579         isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
6580
6581         map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
6582         if (!map)
6583                 goto error;
6584         map = isl_map_add_basic_map(map, bmap1);
6585         map = isl_map_add_basic_map(map, bmap2);
6586         return map;
6587 error:
6588         isl_basic_map_free(bmap1);
6589         isl_basic_map_free(bmap2);
6590         return NULL;
6591 }
6592
6593 struct isl_set *isl_basic_set_union(
6594                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
6595 {
6596         return (struct isl_set *)isl_basic_map_union(
6597                                             (struct isl_basic_map *)bset1,
6598                                             (struct isl_basic_map *)bset2);
6599 }
6600
6601 /* Order divs such that any div only depends on previous divs */
6602 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
6603 {
6604         int i;
6605         unsigned off;
6606
6607         if (!bmap)
6608                 return NULL;
6609
6610         off = isl_dim_total(bmap->dim);
6611
6612         for (i = 0; i < bmap->n_div; ++i) {
6613                 int pos;
6614                 if (isl_int_is_zero(bmap->div[i][0]))
6615                         continue;
6616                 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
6617                                                             bmap->n_div-i);
6618                 if (pos == -1)
6619                         continue;
6620                 isl_basic_map_swap_div(bmap, i, i + pos);
6621                 --i;
6622         }
6623         return bmap;
6624 }
6625
6626 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
6627 {
6628         return (struct isl_basic_set *)
6629                 isl_basic_map_order_divs((struct isl_basic_map *)bset);
6630 }
6631
6632 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
6633 {
6634         int i;
6635
6636         if (!map)
6637                 return 0;
6638
6639         for (i = 0; i < map->n; ++i) {
6640                 map->p[i] = isl_basic_map_order_divs(map->p[i]);
6641                 if (!map->p[i])
6642                         goto error;
6643         }
6644
6645         return map;
6646 error:
6647         isl_map_free(map);
6648         return NULL;
6649 }
6650
6651 /* Apply the expansion computed by isl_merge_divs.
6652  * The expansion itself is given by "exp" while the resulting
6653  * list of divs is given by "div".
6654  */
6655 __isl_give isl_basic_set *isl_basic_set_expand_divs(
6656         __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
6657 {
6658         int i, j;
6659         int n_div;
6660
6661         bset = isl_basic_set_cow(bset);
6662         if (!bset || !div)
6663                 goto error;
6664
6665         if (div->n_row < bset->n_div)
6666                 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
6667                         "not an expansion", goto error);
6668
6669         bset = isl_basic_map_extend_dim(bset, isl_dim_copy(bset->dim),
6670                                         div->n_row - bset->n_div, 0,
6671                                         2 * (div->n_row - bset->n_div));
6672
6673         n_div = bset->n_div;
6674         for (i = n_div; i < div->n_row; ++i)
6675                 if (isl_basic_set_alloc_div(bset) < 0)
6676                         goto error;
6677
6678         j = n_div - 1;
6679         for (i = div->n_row - 1; i >= 0; --i) {
6680                 if (j >= 0 && exp[j] == i) {
6681                         if (i != j)
6682                                 isl_basic_map_swap_div(bset, i, j);
6683                         j--;
6684                 } else {
6685                         isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
6686                         if (isl_basic_map_add_div_constraints(bset, i) < 0)
6687                                 goto error;
6688                 }
6689         }
6690
6691         isl_mat_free(div);
6692         return bset;
6693 error:
6694         isl_basic_set_free(bset);
6695         isl_mat_free(div);
6696         return NULL;
6697 }
6698
6699 /* Look for a div in dst that corresponds to the div "div" in src.
6700  * The divs before "div" in src and dst are assumed to be the same.
6701  * 
6702  * Returns -1 if no corresponding div was found and the position
6703  * of the corresponding div in dst otherwise.
6704  */
6705 static int find_div(struct isl_basic_map *dst,
6706                         struct isl_basic_map *src, unsigned div)
6707 {
6708         int i;
6709
6710         unsigned total = isl_dim_total(src->dim);
6711
6712         isl_assert(dst->ctx, div <= dst->n_div, return -1);
6713         for (i = div; i < dst->n_div; ++i)
6714                 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
6715                     isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
6716                                                 dst->n_div - div) == -1)
6717                         return i;
6718         return -1;
6719 }
6720
6721 struct isl_basic_map *isl_basic_map_align_divs(
6722                 struct isl_basic_map *dst, struct isl_basic_map *src)
6723 {
6724         int i;
6725         unsigned total = isl_dim_total(src->dim);
6726
6727         if (!dst || !src)
6728                 goto error;
6729
6730         if (src->n_div == 0)
6731                 return dst;
6732
6733         for (i = 0; i < src->n_div; ++i)
6734                 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
6735
6736         src = isl_basic_map_order_divs(src);
6737         dst = isl_basic_map_cow(dst);
6738         dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
6739                         src->n_div, 0, 2 * src->n_div);
6740         if (!dst)
6741                 return NULL;
6742         for (i = 0; i < src->n_div; ++i) {
6743                 int j = find_div(dst, src, i);
6744                 if (j < 0) {
6745                         j = isl_basic_map_alloc_div(dst);
6746                         if (j < 0)
6747                                 goto error;
6748                         isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
6749                         isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
6750                         if (isl_basic_map_add_div_constraints(dst, j) < 0)
6751                                 goto error;
6752                 }
6753                 if (j != i)
6754                         isl_basic_map_swap_div(dst, i, j);
6755         }
6756         return dst;
6757 error:
6758         isl_basic_map_free(dst);
6759         return NULL;
6760 }
6761
6762 struct isl_basic_set *isl_basic_set_align_divs(
6763                 struct isl_basic_set *dst, struct isl_basic_set *src)
6764 {
6765         return (struct isl_basic_set *)isl_basic_map_align_divs(
6766                 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
6767 }
6768
6769 struct isl_map *isl_map_align_divs(struct isl_map *map)
6770 {
6771         int i;
6772
6773         if (!map)
6774                 return NULL;
6775         if (map->n == 0)
6776                 return map;
6777         map = isl_map_compute_divs(map);
6778         map = isl_map_cow(map);
6779         if (!map)
6780                 return NULL;
6781
6782         for (i = 1; i < map->n; ++i)
6783                 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
6784         for (i = 1; i < map->n; ++i)
6785                 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
6786
6787         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6788         return map;
6789 }
6790
6791 struct isl_set *isl_set_align_divs(struct isl_set *set)
6792 {
6793         return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
6794 }
6795
6796 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
6797         __isl_take isl_map *map)
6798 {
6799         if (!set || !map)
6800                 goto error;
6801         isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
6802         map = isl_map_intersect_domain(map, set);
6803         set = isl_map_range(map);
6804         return set;
6805 error:
6806         isl_set_free(set);
6807         isl_map_free(map);
6808         return NULL;
6809 }
6810
6811 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
6812         __isl_take isl_map *map)
6813 {
6814         return isl_map_align_params_map_map_and(set, map, &set_apply);
6815 }
6816
6817 /* There is no need to cow as removing empty parts doesn't change
6818  * the meaning of the set.
6819  */
6820 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
6821 {
6822         int i;
6823
6824         if (!map)
6825                 return NULL;
6826
6827         for (i = map->n-1; i >= 0; --i) {
6828                 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
6829                         continue;
6830                 isl_basic_map_free(map->p[i]);
6831                 if (i != map->n-1) {
6832                         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6833                         map->p[i] = map->p[map->n-1];
6834                 }
6835                 map->n--;
6836         }
6837
6838         return map;
6839 }
6840
6841 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
6842 {
6843         return (struct isl_set *)
6844                 isl_map_remove_empty_parts((struct isl_map *)set);
6845 }
6846
6847 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
6848 {
6849         struct isl_basic_map *bmap;
6850         if (!map || map->n == 0)
6851                 return NULL;
6852         bmap = map->p[map->n-1];
6853         isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
6854         return isl_basic_map_copy(bmap);
6855 }
6856
6857 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
6858 {
6859         return (struct isl_basic_set *)
6860                 isl_map_copy_basic_map((struct isl_map *)set);
6861 }
6862
6863 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
6864                                                 __isl_keep isl_basic_map *bmap)
6865 {
6866         int i;
6867
6868         if (!map || !bmap)
6869                 goto error;
6870         for (i = map->n-1; i >= 0; --i) {
6871                 if (map->p[i] != bmap)
6872                         continue;
6873                 map = isl_map_cow(map);
6874                 if (!map)
6875                         goto error;
6876                 isl_basic_map_free(map->p[i]);
6877                 if (i != map->n-1) {
6878                         ISL_F_CLR(map, ISL_SET_NORMALIZED);
6879                         map->p[i] = map->p[map->n-1];
6880                 }
6881                 map->n--;
6882                 return map;
6883         }
6884         return map;
6885 error:
6886         isl_map_free(map);
6887         return NULL;
6888 }
6889
6890 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
6891                                                 struct isl_basic_set *bset)
6892 {
6893         return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
6894                                                 (struct isl_basic_map *)bset);
6895 }
6896
6897 /* Given two basic sets bset1 and bset2, compute the maximal difference
6898  * between the values of dimension pos in bset1 and those in bset2
6899  * for any common value of the parameters and dimensions preceding pos.
6900  */
6901 static enum isl_lp_result basic_set_maximal_difference_at(
6902         __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
6903         int pos, isl_int *opt)
6904 {
6905         struct isl_dim *dims;
6906         struct isl_basic_map *bmap1 = NULL;
6907         struct isl_basic_map *bmap2 = NULL;
6908         struct isl_ctx *ctx;
6909         struct isl_vec *obj;
6910         unsigned total;
6911         unsigned nparam;
6912         unsigned dim1, dim2;
6913         enum isl_lp_result res;
6914
6915         if (!bset1 || !bset2)
6916                 return isl_lp_error;
6917
6918         nparam = isl_basic_set_n_param(bset1);
6919         dim1 = isl_basic_set_n_dim(bset1);
6920         dim2 = isl_basic_set_n_dim(bset2);
6921         dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
6922         bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
6923         dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
6924         bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
6925         if (!bmap1 || !bmap2)
6926                 goto error;
6927         bmap1 = isl_basic_map_cow(bmap1);
6928         bmap1 = isl_basic_map_extend(bmap1, nparam,
6929                         pos, (dim1 - pos) + (dim2 - pos),
6930                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
6931         bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
6932         if (!bmap1)
6933                 goto error;
6934         total = isl_basic_map_total_dim(bmap1);
6935         ctx = bmap1->ctx;
6936         obj = isl_vec_alloc(ctx, 1 + total);
6937         isl_seq_clr(obj->block.data, 1 + total);
6938         isl_int_set_si(obj->block.data[1+nparam+pos], 1);
6939         isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
6940         if (!obj)
6941                 goto error;
6942         res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
6943                                         opt, NULL, NULL);
6944         isl_basic_map_free(bmap1);
6945         isl_vec_free(obj);
6946         return res;
6947 error:
6948         isl_basic_map_free(bmap1);
6949         isl_basic_map_free(bmap2);
6950         return isl_lp_error;
6951 }
6952
6953 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
6954  * for any common value of the parameters and dimensions preceding pos
6955  * in both basic sets, the values of dimension pos in bset1 are
6956  * smaller or larger than those in bset2.
6957  *
6958  * Returns
6959  *       1 if bset1 follows bset2
6960  *      -1 if bset1 precedes bset2
6961  *       0 if bset1 and bset2 are incomparable
6962  *      -2 if some error occurred.
6963  */
6964 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
6965         struct isl_basic_set *bset2, int pos)
6966 {
6967         isl_int opt;
6968         enum isl_lp_result res;
6969         int cmp;
6970
6971         isl_int_init(opt);
6972
6973         res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
6974
6975         if (res == isl_lp_empty)
6976                 cmp = 0;
6977         else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
6978                   res == isl_lp_unbounded)
6979                 cmp = 1;
6980         else if (res == isl_lp_ok && isl_int_is_neg(opt))
6981                 cmp = -1;
6982         else
6983                 cmp = -2;
6984
6985         isl_int_clear(opt);
6986         return cmp;
6987 }
6988
6989 /* Given two basic sets bset1 and bset2, check whether
6990  * for any common value of the parameters and dimensions preceding pos
6991  * there is a value of dimension pos in bset1 that is larger
6992  * than a value of the same dimension in bset2.
6993  *
6994  * Return
6995  *       1 if there exists such a pair
6996  *       0 if there is no such pair, but there is a pair of equal values
6997  *      -1 otherwise
6998  *      -2 if some error occurred.
6999  */
7000 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7001         __isl_keep isl_basic_set *bset2, int pos)
7002 {
7003         isl_int opt;
7004         enum isl_lp_result res;
7005         int cmp;
7006
7007         isl_int_init(opt);
7008
7009         res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7010
7011         if (res == isl_lp_empty)
7012                 cmp = -1;
7013         else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7014                   res == isl_lp_unbounded)
7015                 cmp = 1;
7016         else if (res == isl_lp_ok && isl_int_is_neg(opt))
7017                 cmp = -1;
7018         else if (res == isl_lp_ok)
7019                 cmp = 0;
7020         else
7021                 cmp = -2;
7022
7023         isl_int_clear(opt);
7024         return cmp;
7025 }
7026
7027 /* Given two sets set1 and set2, check whether
7028  * for any common value of the parameters and dimensions preceding pos
7029  * there is a value of dimension pos in set1 that is larger
7030  * than a value of the same dimension in set2.
7031  *
7032  * Return
7033  *       1 if there exists such a pair
7034  *       0 if there is no such pair, but there is a pair of equal values
7035  *      -1 otherwise
7036  *      -2 if some error occurred.
7037  */
7038 int isl_set_follows_at(__isl_keep isl_set *set1,
7039         __isl_keep isl_set *set2, int pos)
7040 {
7041         int i, j;
7042         int follows = -1;
7043
7044         if (!set1 || !set2)
7045                 return -2;
7046
7047         for (i = 0; i < set1->n; ++i)
7048                 for (j = 0; j < set2->n; ++j) {
7049                         int f;
7050                         f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7051                         if (f == 1 || f == -2)
7052                                 return f;
7053                         if (f > follows)
7054                                 follows = f;
7055                 }
7056
7057         return follows;
7058 }
7059
7060 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7061         unsigned pos, isl_int *val)
7062 {
7063         int i;
7064         int d;
7065         unsigned total;
7066
7067         if (!bmap)
7068                 return -1;
7069         total = isl_basic_map_total_dim(bmap);
7070         for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7071                 for (; d+1 > pos; --d)
7072                         if (!isl_int_is_zero(bmap->eq[i][1+d]))
7073                                 break;
7074                 if (d != pos)
7075                         continue;
7076                 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7077                         return 0;
7078                 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7079                         return 0;
7080                 if (!isl_int_is_one(bmap->eq[i][1+d]))
7081                         return 0;
7082                 if (val)
7083                         isl_int_neg(*val, bmap->eq[i][0]);
7084                 return 1;
7085         }
7086         return 0;
7087 }
7088
7089 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7090         unsigned pos, isl_int *val)
7091 {
7092         int i;
7093         isl_int v;
7094         isl_int tmp;
7095         int fixed;
7096
7097         if (!map)
7098                 return -1;
7099         if (map->n == 0)
7100                 return 0;
7101         if (map->n == 1)
7102                 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val); 
7103         isl_int_init(v);
7104         isl_int_init(tmp);
7105         fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v); 
7106         for (i = 1; fixed == 1 && i < map->n; ++i) {
7107                 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp); 
7108                 if (fixed == 1 && isl_int_ne(tmp, v))
7109                         fixed = 0;
7110         }
7111         if (val)
7112                 isl_int_set(*val, v);
7113         isl_int_clear(tmp);
7114         isl_int_clear(v);
7115         return fixed;
7116 }
7117
7118 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
7119         unsigned pos, isl_int *val)
7120 {
7121         return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
7122                                                 pos, val);
7123 }
7124
7125 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
7126         isl_int *val)
7127 {
7128         return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
7129 }
7130
7131 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
7132         enum isl_dim_type type, unsigned pos, isl_int *val)
7133 {
7134         if (pos >= isl_basic_map_dim(bmap, type))
7135                 return -1;
7136         return isl_basic_map_plain_has_fixed_var(bmap,
7137                 isl_basic_map_offset(bmap, type) - 1 + pos, val);
7138 }
7139
7140 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
7141         enum isl_dim_type type, unsigned pos, isl_int *val)
7142 {
7143         if (pos >= isl_map_dim(map, type))
7144                 return -1;
7145         return isl_map_plain_has_fixed_var(map,
7146                 map_offset(map, type) - 1 + pos, val);
7147 }
7148
7149 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
7150         enum isl_dim_type type, unsigned pos, isl_int *val)
7151 {
7152         return isl_map_plain_is_fixed(map, type, pos, val);
7153 }
7154
7155 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7156  * then return this fixed value in *val.
7157  */
7158 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
7159         unsigned dim, isl_int *val)
7160 {
7161         return isl_basic_set_plain_has_fixed_var(bset,
7162                                         isl_basic_set_n_param(bset) + dim, val);
7163 }
7164
7165 /* Check if dimension dim has fixed value and if so and if val is not NULL,
7166  * then return this fixed value in *val.
7167  */
7168 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
7169         unsigned dim, isl_int *val)
7170 {
7171         return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
7172 }
7173
7174 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
7175         unsigned dim, isl_int *val)
7176 {
7177         return isl_set_plain_dim_is_fixed(set, dim, val);
7178 }
7179
7180 /* Check if input variable in has fixed value and if so and if val is not NULL,
7181  * then return this fixed value in *val.
7182  */
7183 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
7184         unsigned in, isl_int *val)
7185 {
7186         return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
7187 }
7188
7189 /* Check if dimension dim has an (obvious) fixed lower bound and if so
7190  * and if val is not NULL, then return this lower bound in *val.
7191  */
7192 int isl_basic_set_plain_dim_has_fixed_lower_bound(
7193         __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
7194 {
7195         int i, i_eq = -1, i_ineq = -1;
7196         isl_int *c;
7197         unsigned total;
7198         unsigned nparam;
7199
7200         if (!bset)
7201                 return -1;
7202         total = isl_basic_set_total_dim(bset);
7203         nparam = isl_basic_set_n_param(bset);
7204         for (i = 0; i < bset->n_eq; ++i) {
7205                 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
7206                         continue;
7207                 if (i_eq != -1)
7208                         return 0;
7209                 i_eq = i;
7210         }
7211         for (i = 0; i < bset->n_ineq; ++i) {
7212                 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
7213                         continue;
7214                 if (i_eq != -1 || i_ineq != -1)
7215                         return 0;
7216                 i_ineq = i;
7217         }
7218         if (i_eq == -1 && i_ineq == -1)
7219                 return 0;
7220         c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
7221         /* The coefficient should always be one due to normalization. */
7222         if (!isl_int_is_one(c[1+nparam+dim]))
7223                 return 0;
7224         if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
7225                 return 0;
7226         if (isl_seq_first_non_zero(c+1+nparam+dim+1,
7227                                         total - nparam - dim - 1) != -1)
7228                 return 0;
7229         if (val)
7230                 isl_int_neg(*val, c[0]);
7231         return 1;
7232 }
7233
7234 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
7235         unsigned dim, isl_int *val)
7236 {
7237         int i;
7238         isl_int v;
7239         isl_int tmp;
7240         int fixed;
7241
7242         if (!set)
7243                 return -1;
7244         if (set->n == 0)
7245                 return 0;
7246         if (set->n == 1)
7247                 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7248                                                                 dim, val);
7249         isl_int_init(v);
7250         isl_int_init(tmp);
7251         fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
7252                                                                 dim, &v);
7253         for (i = 1; fixed == 1 && i < set->n; ++i) {
7254                 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
7255                                                                 dim, &tmp);
7256                 if (fixed == 1 && isl_int_ne(tmp, v))
7257                         fixed = 0;
7258         }
7259         if (val)
7260                 isl_int_set(*val, v);
7261         isl_int_clear(tmp);
7262         isl_int_clear(v);
7263         return fixed;
7264 }
7265
7266 struct constraint {
7267         unsigned        size;
7268         isl_int         *c;
7269 };
7270
7271 /* uset_gist depends on constraints without existentially quantified
7272  * variables sorting first.
7273  */
7274 static int qsort_constraint_cmp(const void *p1, const void *p2)
7275 {
7276         const struct constraint *c1 = (const struct constraint *)p1;
7277         const struct constraint *c2 = (const struct constraint *)p2;
7278         int l1, l2;
7279         unsigned size = isl_min(c1->size, c2->size);
7280
7281         l1 = isl_seq_last_non_zero(c1->c, size);
7282         l2 = isl_seq_last_non_zero(c2->c, size);
7283
7284         if (l1 != l2)
7285                 return l1 - l2;
7286
7287         return isl_seq_cmp(c1->c, c2->c, size);
7288 }
7289
7290 static struct isl_basic_map *isl_basic_map_sort_constraints(
7291         struct isl_basic_map *bmap)
7292 {
7293         int i;
7294         struct constraint *c;
7295         unsigned total;
7296
7297         if (!bmap)
7298                 return NULL;
7299         total = isl_basic_map_total_dim(bmap);
7300         c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
7301         if (!c)
7302                 goto error;
7303         for (i = 0; i < bmap->n_ineq; ++i) {
7304                 c[i].size = total;
7305                 c[i].c = bmap->ineq[i];
7306         }
7307         qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
7308         for (i = 0; i < bmap->n_ineq; ++i)
7309                 bmap->ineq[i] = c[i].c;
7310         free(c);
7311         return bmap;
7312 error:
7313         isl_basic_map_free(bmap);
7314         return NULL;
7315 }
7316
7317 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
7318         __isl_take isl_basic_set *bset)
7319 {
7320         return (struct isl_basic_set *)isl_basic_map_sort_constraints(
7321                                                 (struct isl_basic_map *)bset);
7322 }
7323
7324 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
7325 {
7326         if (!bmap)
7327                 return NULL;
7328         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
7329                 return bmap;
7330         bmap = isl_basic_map_remove_redundancies(bmap);
7331         bmap = isl_basic_map_sort_constraints(bmap);
7332         ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
7333         return bmap;
7334 }
7335
7336 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
7337 {
7338         return (struct isl_basic_set *)isl_basic_map_normalize(
7339                                                 (struct isl_basic_map *)bset);
7340 }
7341
7342 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
7343         const __isl_keep isl_basic_map *bmap2)
7344 {
7345         int i, cmp;
7346         unsigned total;
7347
7348         if (bmap1 == bmap2)
7349                 return 0;
7350         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
7351             ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
7352                 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
7353         if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
7354                 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
7355         if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
7356                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7357         if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
7358                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
7359         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
7360             ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7361                 return 0;
7362         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
7363                 return 1;
7364         if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
7365                 return -1;
7366         if (bmap1->n_eq != bmap2->n_eq)
7367                 return bmap1->n_eq - bmap2->n_eq;
7368         if (bmap1->n_ineq != bmap2->n_ineq)
7369                 return bmap1->n_ineq - bmap2->n_ineq;
7370         if (bmap1->n_div != bmap2->n_div)
7371                 return bmap1->n_div - bmap2->n_div;
7372         total = isl_basic_map_total_dim(bmap1);
7373         for (i = 0; i < bmap1->n_eq; ++i) {
7374                 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
7375                 if (cmp)
7376                         return cmp;
7377         }
7378         for (i = 0; i < bmap1->n_ineq; ++i) {
7379                 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
7380                 if (cmp)
7381                         return cmp;
7382         }
7383         for (i = 0; i < bmap1->n_div; ++i) {
7384                 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
7385                 if (cmp)
7386                         return cmp;
7387         }
7388         return 0;
7389 }
7390
7391 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
7392         __isl_keep isl_basic_map *bmap2)
7393 {
7394         return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
7395 }
7396
7397 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
7398         __isl_keep isl_basic_set *bset2)
7399 {
7400         return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
7401                                             (isl_basic_map *)bset2);
7402 }
7403
7404 static int qsort_bmap_cmp(const void *p1, const void *p2)
7405 {
7406         const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
7407         const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
7408
7409         return isl_basic_map_plain_cmp(bmap1, bmap2);
7410 }
7411
7412 /* We normalize in place, but if anything goes wrong we need
7413  * to return NULL, so we need to make sure we don't change the
7414  * meaning of any possible other copies of map.
7415  */
7416 struct isl_map *isl_map_normalize(struct isl_map *map)
7417 {
7418         int i, j;
7419         struct isl_basic_map *bmap;
7420
7421         if (!map)
7422                 return NULL;
7423         if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
7424                 return map;
7425         for (i = 0; i < map->n; ++i) {
7426                 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
7427                 if (!bmap)
7428                         goto error;
7429                 isl_basic_map_free(map->p[i]);
7430                 map->p[i] = bmap;
7431         }
7432         qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
7433         ISL_F_SET(map, ISL_MAP_NORMALIZED);
7434         map = isl_map_remove_empty_parts(map);
7435         if (!map)
7436                 return NULL;
7437         for (i = map->n - 1; i >= 1; --i) {
7438                 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
7439                         continue;
7440                 isl_basic_map_free(map->p[i-1]);
7441                 for (j = i; j < map->n; ++j)
7442                         map->p[j-1] = map->p[j];
7443                 map->n--;
7444         }
7445         return map;
7446 error:
7447         isl_map_free(map);
7448         return NULL;
7449
7450 }
7451
7452 struct isl_set *isl_set_normalize(struct isl_set *set)
7453 {
7454         return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
7455 }
7456
7457 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7458 {
7459         int i;
7460         int equal;
7461
7462         if (!map1 || !map2)
7463                 return -1;
7464
7465         if (map1 == map2)
7466                 return 1;
7467         if (!isl_dim_equal(map1->dim, map2->dim))
7468                 return 0;
7469
7470         map1 = isl_map_copy(map1);
7471         map2 = isl_map_copy(map2);
7472         map1 = isl_map_normalize(map1);
7473         map2 = isl_map_normalize(map2);
7474         if (!map1 || !map2)
7475                 goto error;
7476         equal = map1->n == map2->n;
7477         for (i = 0; equal && i < map1->n; ++i) {
7478                 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
7479                 if (equal < 0)
7480                         goto error;
7481         }
7482         isl_map_free(map1);
7483         isl_map_free(map2);
7484         return equal;
7485 error:
7486         isl_map_free(map1);
7487         isl_map_free(map2);
7488         return -1;
7489 }
7490
7491 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7492 {
7493         return isl_map_plain_is_equal(map1, map2);
7494 }
7495
7496 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7497 {
7498         return isl_map_plain_is_equal((struct isl_map *)set1,
7499                                                 (struct isl_map *)set2);
7500 }
7501
7502 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7503 {
7504         return isl_set_plain_is_equal(set1, set2);
7505 }
7506
7507 /* Return an interval that ranges from min to max (inclusive)
7508  */
7509 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
7510         isl_int min, isl_int max)
7511 {
7512         int k;
7513         struct isl_basic_set *bset = NULL;
7514
7515         bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
7516         if (!bset)
7517                 goto error;
7518
7519         k = isl_basic_set_alloc_inequality(bset);
7520         if (k < 0)
7521                 goto error;
7522         isl_int_set_si(bset->ineq[k][1], 1);
7523         isl_int_neg(bset->ineq[k][0], min);
7524
7525         k = isl_basic_set_alloc_inequality(bset);
7526         if (k < 0)
7527                 goto error;
7528         isl_int_set_si(bset->ineq[k][1], -1);
7529         isl_int_set(bset->ineq[k][0], max);
7530
7531         return bset;
7532 error:
7533         isl_basic_set_free(bset);
7534         return NULL;
7535 }
7536
7537 /* Return the Cartesian product of the basic sets in list (in the given order).
7538  */
7539 __isl_give isl_basic_set *isl_basic_set_list_product(
7540         __isl_take struct isl_basic_set_list *list)
7541 {
7542         int i;
7543         unsigned dim;
7544         unsigned nparam;
7545         unsigned extra;
7546         unsigned n_eq;
7547         unsigned n_ineq;
7548         struct isl_basic_set *product = NULL;
7549
7550         if (!list)
7551                 goto error;
7552         isl_assert(list->ctx, list->n > 0, goto error);
7553         isl_assert(list->ctx, list->p[0], goto error);
7554         nparam = isl_basic_set_n_param(list->p[0]);
7555         dim = isl_basic_set_n_dim(list->p[0]);
7556         extra = list->p[0]->n_div;
7557         n_eq = list->p[0]->n_eq;
7558         n_ineq = list->p[0]->n_ineq;
7559         for (i = 1; i < list->n; ++i) {
7560                 isl_assert(list->ctx, list->p[i], goto error);
7561                 isl_assert(list->ctx,
7562                     nparam == isl_basic_set_n_param(list->p[i]), goto error);
7563                 dim += isl_basic_set_n_dim(list->p[i]);
7564                 extra += list->p[i]->n_div;
7565                 n_eq += list->p[i]->n_eq;
7566                 n_ineq += list->p[i]->n_ineq;
7567         }
7568         product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
7569                                         n_eq, n_ineq);
7570         if (!product)
7571                 goto error;
7572         dim = 0;
7573         for (i = 0; i < list->n; ++i) {
7574                 isl_basic_set_add_constraints(product,
7575                                         isl_basic_set_copy(list->p[i]), dim);
7576                 dim += isl_basic_set_n_dim(list->p[i]);
7577         }
7578         isl_basic_set_list_free(list);
7579         return product;
7580 error:
7581         isl_basic_set_free(product);
7582         isl_basic_set_list_free(list);
7583         return NULL;
7584 }
7585
7586 struct isl_basic_map *isl_basic_map_product(
7587                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7588 {
7589         struct isl_dim *dim_result = NULL;
7590         struct isl_basic_map *bmap;
7591         unsigned in1, in2, out1, out2, nparam, total, pos;
7592         struct isl_dim_map *dim_map1, *dim_map2;
7593
7594         if (!bmap1 || !bmap2)
7595                 goto error;
7596
7597         isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
7598                                      bmap2->dim, isl_dim_param), goto error);
7599         dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
7600                                                    isl_dim_copy(bmap2->dim));
7601
7602         in1 = isl_basic_map_n_in(bmap1);
7603         in2 = isl_basic_map_n_in(bmap2);
7604         out1 = isl_basic_map_n_out(bmap1);
7605         out2 = isl_basic_map_n_out(bmap2);
7606         nparam = isl_basic_map_n_param(bmap1);
7607
7608         total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
7609         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7610         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7611         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7612         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7613         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7614         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
7615         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
7616         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7617         isl_dim_map_div(dim_map1, bmap1, pos += out2);
7618         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7619
7620         bmap = isl_basic_map_alloc_dim(dim_result,
7621                         bmap1->n_div + bmap2->n_div,
7622                         bmap1->n_eq + bmap2->n_eq,
7623                         bmap1->n_ineq + bmap2->n_ineq);
7624         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7625         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7626         bmap = isl_basic_map_simplify(bmap);
7627         return isl_basic_map_finalize(bmap);
7628 error:
7629         isl_basic_map_free(bmap1);
7630         isl_basic_map_free(bmap2);
7631         return NULL;
7632 }
7633
7634 __isl_give isl_basic_map *isl_basic_map_flat_product(
7635         __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7636 {
7637         isl_basic_map *prod;
7638
7639         prod = isl_basic_map_product(bmap1, bmap2);
7640         prod = isl_basic_map_flatten(prod);
7641         return prod;
7642 }
7643
7644 __isl_give isl_basic_set *isl_basic_set_flat_product(
7645         __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
7646 {
7647         return isl_basic_map_flat_product(bset1, bset2);
7648 }
7649
7650 __isl_give isl_basic_map *isl_basic_map_range_product(
7651         __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7652 {
7653         isl_dim *dim_result = NULL;
7654         isl_basic_map *bmap;
7655         unsigned in, out1, out2, nparam, total, pos;
7656         struct isl_dim_map *dim_map1, *dim_map2;
7657
7658         if (!bmap1 || !bmap2)
7659                 goto error;
7660
7661         dim_result = isl_dim_range_product(isl_dim_copy(bmap1->dim),
7662                                            isl_dim_copy(bmap2->dim));
7663
7664         in = isl_basic_map_dim(bmap1, isl_dim_in);
7665         out1 = isl_basic_map_n_out(bmap1);
7666         out2 = isl_basic_map_n_out(bmap2);
7667         nparam = isl_basic_map_n_param(bmap1);
7668
7669         total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
7670         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
7671         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
7672         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
7673         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
7674         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
7675         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
7676         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
7677         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
7678         isl_dim_map_div(dim_map1, bmap1, pos += out2);
7679         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
7680
7681         bmap = isl_basic_map_alloc_dim(dim_result,
7682                         bmap1->n_div + bmap2->n_div,
7683                         bmap1->n_eq + bmap2->n_eq,
7684                         bmap1->n_ineq + bmap2->n_ineq);
7685         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
7686         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
7687         bmap = isl_basic_map_simplify(bmap);
7688         return isl_basic_map_finalize(bmap);
7689 error:
7690         isl_basic_map_free(bmap1);
7691         isl_basic_map_free(bmap2);
7692         return NULL;
7693 }
7694
7695 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
7696         __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
7697 {
7698         isl_basic_map *prod;
7699
7700         prod = isl_basic_map_range_product(bmap1, bmap2);
7701         prod = isl_basic_map_flatten_range(prod);
7702         return prod;
7703 }
7704
7705 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
7706         __isl_take isl_map *map2,
7707         __isl_give isl_dim *(*dim_product)(__isl_take isl_dim *left,
7708                                            __isl_take isl_dim *right),
7709         __isl_give isl_basic_map *(*basic_map_product)(
7710                 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
7711 {
7712         unsigned flags = 0;
7713         struct isl_map *result;
7714         int i, j;
7715
7716         if (!map1 || !map2)
7717                 goto error;
7718
7719         isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
7720                                          map2->dim, isl_dim_param), goto error);
7721
7722         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
7723             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
7724                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7725
7726         result = isl_map_alloc_dim(dim_product(isl_dim_copy(map1->dim),
7727                                                isl_dim_copy(map2->dim)),
7728                                 map1->n * map2->n, flags);
7729         if (!result)
7730                 goto error;
7731         for (i = 0; i < map1->n; ++i)
7732                 for (j = 0; j < map2->n; ++j) {
7733                         struct isl_basic_map *part;
7734                         part = basic_map_product(isl_basic_map_copy(map1->p[i]),
7735                                                  isl_basic_map_copy(map2->p[j]));
7736                         if (isl_basic_map_is_empty(part))
7737                                 isl_basic_map_free(part);
7738                         else
7739                                 result = isl_map_add_basic_map(result, part);
7740                         if (!result)
7741                                 goto error;
7742                 }
7743         isl_map_free(map1);
7744         isl_map_free(map2);
7745         return result;
7746 error:
7747         isl_map_free(map1);
7748         isl_map_free(map2);
7749         return NULL;
7750 }
7751
7752 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
7753  */
7754 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
7755         __isl_take isl_map *map2)
7756 {
7757         return map_product(map1, map2, &isl_dim_product, &isl_basic_map_product);
7758 }
7759
7760 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
7761         __isl_take isl_map *map2)
7762 {
7763         return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
7764 }
7765
7766 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
7767  */
7768 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
7769         __isl_take isl_map *map2)
7770 {
7771         isl_map *prod;
7772
7773         prod = isl_map_product(map1, map2);
7774         prod = isl_map_flatten(prod);
7775         return prod;
7776 }
7777
7778 /* Given two set A and B, construct its Cartesian product A x B.
7779  */
7780 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
7781 {
7782         return (struct isl_set *)isl_map_product((struct isl_map *)set1,
7783                                                  (struct isl_map *)set2);
7784 }
7785
7786 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
7787         __isl_take isl_set *set2)
7788 {
7789         return (isl_set *)isl_map_flat_product((isl_map *)set1, (isl_map *)set2);
7790 }
7791
7792 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
7793  */
7794 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
7795         __isl_take isl_map *map2)
7796 {
7797         return map_product(map1, map2, &isl_dim_range_product,
7798                                 &isl_basic_map_range_product);
7799 }
7800
7801 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
7802         __isl_take isl_map *map2)
7803 {
7804         return isl_map_align_params_map_map_and(map1, map2,
7805                                                 &map_range_product_aligned);
7806 }
7807
7808 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
7809  */
7810 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
7811         __isl_take isl_map *map2)
7812 {
7813         isl_map *prod;
7814
7815         prod = isl_map_range_product(map1, map2);
7816         prod = isl_map_flatten_range(prod);
7817         return prod;
7818 }
7819
7820 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
7821 {
7822         int i;
7823         uint32_t hash = isl_hash_init();
7824         unsigned total;
7825
7826         if (!bmap)
7827                 return 0;
7828         bmap = isl_basic_map_copy(bmap);
7829         bmap = isl_basic_map_normalize(bmap);
7830         if (!bmap)
7831                 return 0;
7832         total = isl_basic_map_total_dim(bmap);
7833         isl_hash_byte(hash, bmap->n_eq & 0xFF);
7834         for (i = 0; i < bmap->n_eq; ++i) {
7835                 uint32_t c_hash;
7836                 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
7837                 isl_hash_hash(hash, c_hash);
7838         }
7839         isl_hash_byte(hash, bmap->n_ineq & 0xFF);
7840         for (i = 0; i < bmap->n_ineq; ++i) {
7841                 uint32_t c_hash;
7842                 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
7843                 isl_hash_hash(hash, c_hash);
7844         }
7845         isl_hash_byte(hash, bmap->n_div & 0xFF);
7846         for (i = 0; i < bmap->n_div; ++i) {
7847                 uint32_t c_hash;
7848                 if (isl_int_is_zero(bmap->div[i][0]))
7849                         continue;
7850                 isl_hash_byte(hash, i & 0xFF);
7851                 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
7852                 isl_hash_hash(hash, c_hash);
7853         }
7854         isl_basic_map_free(bmap);
7855         return hash;
7856 }
7857
7858 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
7859 {
7860         return isl_basic_map_get_hash((isl_basic_map *)bset);
7861 }
7862
7863 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
7864 {
7865         int i;
7866         uint32_t hash;
7867
7868         if (!map)
7869                 return 0;
7870         map = isl_map_copy(map);
7871         map = isl_map_normalize(map);
7872         if (!map)
7873                 return 0;
7874
7875         hash = isl_hash_init();
7876         for (i = 0; i < map->n; ++i) {
7877                 uint32_t bmap_hash;
7878                 bmap_hash = isl_basic_map_get_hash(map->p[i]);
7879                 isl_hash_hash(hash, bmap_hash);
7880         }
7881                 
7882         isl_map_free(map);
7883
7884         return hash;
7885 }
7886
7887 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
7888 {
7889         return isl_map_get_hash((isl_map *)set);
7890 }
7891
7892 /* Check if the value for dimension dim is completely determined
7893  * by the values of the other parameters and variables.
7894  * That is, check if dimension dim is involved in an equality.
7895  */
7896 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
7897 {
7898         int i;
7899         unsigned nparam;
7900
7901         if (!bset)
7902                 return -1;
7903         nparam = isl_basic_set_n_param(bset);
7904         for (i = 0; i < bset->n_eq; ++i)
7905                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
7906                         return 1;
7907         return 0;
7908 }
7909
7910 /* Check if the value for dimension dim is completely determined
7911  * by the values of the other parameters and variables.
7912  * That is, check if dimension dim is involved in an equality
7913  * for each of the subsets.
7914  */
7915 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
7916 {
7917         int i;
7918
7919         if (!set)
7920                 return -1;
7921         for (i = 0; i < set->n; ++i) {
7922                 int unique;
7923                 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
7924                 if (unique != 1)
7925                         return unique;
7926         }
7927         return 1;
7928 }
7929
7930 int isl_set_n_basic_set(__isl_keep isl_set *set)
7931 {
7932         return set ? set->n : 0;
7933 }
7934
7935 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
7936         int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
7937 {
7938         int i;
7939
7940         if (!map)
7941                 return -1;
7942
7943         for (i = 0; i < map->n; ++i)
7944                 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
7945                         return -1;
7946
7947         return 0;
7948 }
7949
7950 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
7951         int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
7952 {
7953         int i;
7954
7955         if (!set)
7956                 return -1;
7957
7958         for (i = 0; i < set->n; ++i)
7959                 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
7960                         return -1;
7961
7962         return 0;
7963 }
7964
7965 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
7966 {
7967         struct isl_dim *dim;
7968
7969         if (!bset)
7970                 return NULL;
7971
7972         bset = isl_basic_set_cow(bset);
7973         if (!bset)
7974                 return NULL;
7975
7976         dim = isl_basic_set_get_dim(bset);
7977         dim = isl_dim_lift(dim, bset->n_div);
7978         if (!dim)
7979                 goto error;
7980         isl_dim_free(bset->dim);
7981         bset->dim = dim;
7982         bset->extra -= bset->n_div;
7983         bset->n_div = 0;
7984
7985         bset = isl_basic_set_finalize(bset);
7986
7987         return bset;
7988 error:
7989         isl_basic_set_free(bset);
7990         return NULL;
7991 }
7992
7993 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
7994 {
7995         int i;
7996         struct isl_dim *dim;
7997         unsigned n_div;
7998
7999         set = isl_set_align_divs(set);
8000
8001         if (!set)
8002                 return NULL;
8003
8004         set = isl_set_cow(set);
8005         if (!set)
8006                 return NULL;
8007
8008         n_div = set->p[0]->n_div;
8009         dim = isl_set_get_dim(set);
8010         dim = isl_dim_lift(dim, n_div);
8011         if (!dim)
8012                 goto error;
8013         isl_dim_free(set->dim);
8014         set->dim = dim;
8015
8016         for (i = 0; i < set->n; ++i) {
8017                 set->p[i] = isl_basic_set_lift(set->p[i]);
8018                 if (!set->p[i])
8019                         goto error;
8020         }
8021
8022         return set;
8023 error:
8024         isl_set_free(set);
8025         return NULL;
8026 }
8027
8028 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
8029 {
8030         struct isl_dim *dim;
8031         struct isl_basic_map *bmap;
8032         unsigned n_set;
8033         unsigned n_div;
8034         unsigned n_param;
8035         unsigned total;
8036         int i, k, l;
8037
8038         set = isl_set_align_divs(set);
8039
8040         if (!set)
8041                 return NULL;
8042
8043         dim = isl_set_get_dim(set);
8044         if (set->n == 0 || set->p[0]->n_div == 0) {
8045                 isl_set_free(set);
8046                 return isl_map_identity(isl_dim_map_from_set(dim));
8047         }
8048
8049         n_div = set->p[0]->n_div;
8050         dim = isl_dim_map_from_set(dim);
8051         n_param = isl_dim_size(dim, isl_dim_param);
8052         n_set = isl_dim_size(dim, isl_dim_in);
8053         dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
8054         bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
8055         for (i = 0; i < n_set; ++i)
8056                 bmap = var_equal(bmap, i);
8057
8058         total = n_param + n_set + n_set + n_div;
8059         for (i = 0; i < n_div; ++i) {
8060                 k = isl_basic_map_alloc_inequality(bmap);
8061                 if (k < 0)
8062                         goto error;
8063                 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
8064                 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
8065                 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
8066                             set->p[0]->div[i]+1+1+n_param, n_set + n_div);
8067                 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
8068                             set->p[0]->div[i][0]);
8069
8070                 l = isl_basic_map_alloc_inequality(bmap);
8071                 if (l < 0)
8072                         goto error;
8073                 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
8074                 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
8075                             set->p[0]->div[i][0]);
8076                 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
8077         }
8078
8079         isl_set_free(set);
8080         bmap = isl_basic_map_simplify(bmap);
8081         bmap = isl_basic_map_finalize(bmap);
8082         return isl_map_from_basic_map(bmap);
8083 error:
8084         isl_set_free(set);
8085         isl_basic_map_free(bmap);
8086         return NULL;
8087 }
8088
8089 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
8090 {
8091         unsigned dim;
8092         int size = 0;
8093
8094         if (!bset)
8095                 return -1;
8096
8097         dim = isl_basic_set_total_dim(bset);
8098         size += bset->n_eq * (1 + dim);
8099         size += bset->n_ineq * (1 + dim);
8100         size += bset->n_div * (2 + dim);
8101
8102         return size;
8103 }
8104
8105 int isl_set_size(__isl_keep isl_set *set)
8106 {
8107         int i;
8108         int size = 0;
8109
8110         if (!set)
8111                 return -1;
8112
8113         for (i = 0; i < set->n; ++i)
8114                 size += isl_basic_set_size(set->p[i]);
8115
8116         return size;
8117 }
8118
8119 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
8120         enum isl_dim_type type, unsigned pos)
8121 {
8122         int i;
8123         int lower, upper;
8124
8125         if (!bmap)
8126                 return -1;
8127
8128         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
8129
8130         pos += isl_basic_map_offset(bmap, type);
8131
8132         for (i = 0; i < bmap->n_eq; ++i)
8133                 if (!isl_int_is_zero(bmap->eq[i][pos]))
8134                         return 1;
8135
8136         lower = upper = 0;
8137         for (i = 0; i < bmap->n_ineq; ++i) {
8138                 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
8139                 if (sgn > 0)
8140                         lower = 1;
8141                 if (sgn < 0)
8142                         upper = 1;
8143         }
8144
8145         return lower && upper;
8146 }
8147
8148 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
8149         enum isl_dim_type type, unsigned pos)
8150 {
8151         int i;
8152
8153         if (!map)
8154                 return -1;
8155
8156         for (i = 0; i < map->n; ++i) {
8157                 int bounded;
8158                 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
8159                 if (bounded < 0 || !bounded)
8160                         return bounded;
8161         }
8162
8163         return 1;
8164 }
8165
8166 /* Return 1 if the specified dim is involved in both an upper bound
8167  * and a lower bound.
8168  */
8169 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
8170         enum isl_dim_type type, unsigned pos)
8171 {
8172         return isl_map_dim_is_bounded((isl_map *)set, type, pos);
8173 }
8174
8175 /* For each of the "n" variables starting at "first", determine
8176  * the sign of the variable and put the results in the first "n"
8177  * elements of the array "signs".
8178  * Sign
8179  *      1 means that the variable is non-negative
8180  *      -1 means that the variable is non-positive
8181  *      0 means the variable attains both positive and negative values.
8182  */
8183 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
8184         unsigned first, unsigned n, int *signs)
8185 {
8186         isl_vec *bound = NULL;
8187         struct isl_tab *tab = NULL;
8188         struct isl_tab_undo *snap;
8189         int i;
8190
8191         if (!bset || !signs)
8192                 return -1;
8193
8194         bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
8195         tab = isl_tab_from_basic_set(bset);
8196         if (!bound || !tab)
8197                 goto error;
8198
8199         isl_seq_clr(bound->el, bound->size);
8200         isl_int_set_si(bound->el[0], -1);
8201
8202         snap = isl_tab_snap(tab);
8203         for (i = 0; i < n; ++i) {
8204                 int empty;
8205
8206                 isl_int_set_si(bound->el[1 + first + i], -1);
8207                 if (isl_tab_add_ineq(tab, bound->el) < 0)
8208                         goto error;
8209                 empty = tab->empty;
8210                 isl_int_set_si(bound->el[1 + first + i], 0);
8211                 if (isl_tab_rollback(tab, snap) < 0)
8212                         goto error;
8213
8214                 if (empty) {
8215                         signs[i] = 1;
8216                         continue;
8217                 }
8218
8219                 isl_int_set_si(bound->el[1 + first + i], 1);
8220                 if (isl_tab_add_ineq(tab, bound->el) < 0)
8221                         goto error;
8222                 empty = tab->empty;
8223                 isl_int_set_si(bound->el[1 + first + i], 0);
8224                 if (isl_tab_rollback(tab, snap) < 0)
8225                         goto error;
8226
8227                 signs[i] = empty ? -1 : 0;
8228         }
8229
8230         isl_tab_free(tab);
8231         isl_vec_free(bound);
8232         return 0;
8233 error:
8234         isl_tab_free(tab);
8235         isl_vec_free(bound);
8236         return -1;
8237 }
8238
8239 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
8240         enum isl_dim_type type, unsigned first, unsigned n, int *signs)
8241 {
8242         if (!bset || !signs)
8243                 return -1;
8244         isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
8245                 return -1);
8246
8247         first += pos(bset->dim, type) - 1;
8248         return isl_basic_set_vars_get_sign(bset, first, n, signs);
8249 }
8250
8251 /* Check if the given basic map is obviously single-valued.
8252  * In particular, for each output dimension, check that there is
8253  * an equality that defines the output dimension in terms of
8254  * earlier dimensions.
8255  */
8256 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
8257 {
8258         int i, j;
8259         unsigned total;
8260         unsigned n_out;
8261         unsigned o_out;
8262
8263         if (!bmap)
8264                 return -1;
8265
8266         total = 1 + isl_basic_map_total_dim(bmap);
8267         n_out = isl_basic_map_dim(bmap, isl_dim_out);
8268         o_out = isl_basic_map_offset(bmap, isl_dim_out);
8269
8270         for (i = 0; i < n_out; ++i) {
8271                 for (j = 0; j < bmap->n_eq; ++j) {
8272                         if (isl_int_is_zero(bmap->eq[j][o_out + i]))
8273                                 continue;
8274                         if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
8275                                                 total - (o_out + i + 1)) == -1)
8276                                 break;
8277                 }
8278                 if (j >= bmap->n_eq)
8279                         return 0;
8280         }
8281
8282         return 1;
8283 }
8284
8285 /* Check if the given map is obviously single-valued.
8286  */
8287 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
8288 {
8289         if (!map)
8290                 return -1;
8291         if (map->n == 0)
8292                 return 1;
8293         if (map->n >= 2)
8294                 return 0;
8295
8296         return isl_basic_map_plain_is_single_valued(map->p[0]);
8297 }
8298
8299 /* Check if the given map is single-valued.
8300  * We simply compute
8301  *
8302  *      M \circ M^-1
8303  *
8304  * and check if the result is a subset of the identity mapping.
8305  */
8306 int isl_map_is_single_valued(__isl_keep isl_map *map)
8307 {
8308         isl_dim *dim;
8309         isl_map *test;
8310         isl_map *id;
8311         int sv;
8312
8313         sv = isl_map_plain_is_single_valued(map);
8314         if (sv < 0 || sv)
8315                 return sv;
8316
8317         test = isl_map_reverse(isl_map_copy(map));
8318         test = isl_map_apply_range(test, isl_map_copy(map));
8319
8320         dim = isl_dim_map_from_set(isl_dim_range(isl_map_get_dim(map)));
8321         id = isl_map_identity(dim);
8322
8323         sv = isl_map_is_subset(test, id);
8324
8325         isl_map_free(test);
8326         isl_map_free(id);
8327
8328         return sv;
8329 }
8330
8331 int isl_map_is_injective(__isl_keep isl_map *map)
8332 {
8333         int in;
8334
8335         map = isl_map_copy(map);
8336         map = isl_map_reverse(map);
8337         in = isl_map_is_single_valued(map);
8338         isl_map_free(map);
8339
8340         return in;
8341 }
8342
8343 /* Check if the given map is obviously injective.
8344  */
8345 int isl_map_plain_is_injective(__isl_keep isl_map *map)
8346 {
8347         int in;
8348
8349         map = isl_map_copy(map);
8350         map = isl_map_reverse(map);
8351         in = isl_map_plain_is_single_valued(map);
8352         isl_map_free(map);
8353
8354         return in;
8355 }
8356
8357 int isl_map_is_bijective(__isl_keep isl_map *map)
8358 {
8359         int sv;
8360
8361         sv = isl_map_is_single_valued(map);
8362         if (sv < 0 || !sv)
8363                 return sv;
8364
8365         return isl_map_is_injective(map);
8366 }
8367
8368 int isl_set_is_singleton(__isl_keep isl_set *set)
8369 {
8370         return isl_map_is_single_valued((isl_map *)set);
8371 }
8372
8373 int isl_map_is_translation(__isl_keep isl_map *map)
8374 {
8375         int ok;
8376         isl_set *delta;
8377
8378         delta = isl_map_deltas(isl_map_copy(map));
8379         ok = isl_set_is_singleton(delta);
8380         isl_set_free(delta);
8381
8382         return ok;
8383 }
8384
8385 static int unique(isl_int *p, unsigned pos, unsigned len)
8386 {
8387         if (isl_seq_first_non_zero(p, pos) != -1)
8388                 return 0;
8389         if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
8390                 return 0;
8391         return 1;
8392 }
8393
8394 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
8395 {
8396         int i, j;
8397         unsigned nvar;
8398         unsigned ovar;
8399
8400         if (!bset)
8401                 return -1;
8402
8403         if (isl_basic_set_dim(bset, isl_dim_div) != 0)
8404                 return 0;
8405
8406         nvar = isl_basic_set_dim(bset, isl_dim_set);
8407         ovar = isl_dim_offset(bset->dim, isl_dim_set);
8408         for (j = 0; j < nvar; ++j) {
8409                 int lower = 0, upper = 0;
8410                 for (i = 0; i < bset->n_eq; ++i) {
8411                         if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
8412                                 continue;
8413                         if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
8414                                 return 0;
8415                         break;
8416                 }
8417                 if (i < bset->n_eq)
8418                         continue;
8419                 for (i = 0; i < bset->n_ineq; ++i) {
8420                         if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
8421                                 continue;
8422                         if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
8423                                 return 0;
8424                         if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
8425                                 lower = 1;
8426                         else
8427                                 upper = 1;
8428                 }
8429                 if (!lower || !upper)
8430                         return 0;
8431         }
8432
8433         return 1;
8434 }
8435
8436 int isl_set_is_box(__isl_keep isl_set *set)
8437 {
8438         if (!set)
8439                 return -1;
8440         if (set->n != 1)
8441                 return 0;
8442
8443         return isl_basic_set_is_box(set->p[0]);
8444 }
8445
8446 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
8447 {
8448         if (!bset)
8449                 return -1;
8450         
8451         return isl_dim_is_wrapping(bset->dim);
8452 }
8453
8454 int isl_set_is_wrapping(__isl_keep isl_set *set)
8455 {
8456         if (!set)
8457                 return -1;
8458         
8459         return isl_dim_is_wrapping(set->dim);
8460 }
8461
8462 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
8463 {
8464         bmap = isl_basic_map_cow(bmap);
8465         if (!bmap)
8466                 return NULL;
8467
8468         bmap->dim = isl_dim_wrap(bmap->dim);
8469         if (!bmap->dim)
8470                 goto error;
8471
8472         bmap = isl_basic_map_finalize(bmap);
8473
8474         return (isl_basic_set *)bmap;
8475 error:
8476         isl_basic_map_free(bmap);
8477         return NULL;
8478 }
8479
8480 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
8481 {
8482         int i;
8483
8484         map = isl_map_cow(map);
8485         if (!map)
8486                 return NULL;
8487
8488         for (i = 0; i < map->n; ++i) {
8489                 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
8490                 if (!map->p[i])
8491                         goto error;
8492         }
8493         map->dim = isl_dim_wrap(map->dim);
8494         if (!map->dim)
8495                 goto error;
8496
8497         return (isl_set *)map;
8498 error:
8499         isl_map_free(map);
8500         return NULL;
8501 }
8502
8503 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
8504 {
8505         bset = isl_basic_set_cow(bset);
8506         if (!bset)
8507                 return NULL;
8508
8509         bset->dim = isl_dim_unwrap(bset->dim);
8510         if (!bset->dim)
8511                 goto error;
8512
8513         bset = isl_basic_set_finalize(bset);
8514
8515         return (isl_basic_map *)bset;
8516 error:
8517         isl_basic_set_free(bset);
8518         return NULL;
8519 }
8520
8521 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
8522 {
8523         int i;
8524
8525         if (!set)
8526                 return NULL;
8527
8528         if (!isl_set_is_wrapping(set))
8529                 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
8530                         goto error);
8531
8532         set = isl_set_cow(set);
8533         if (!set)
8534                 return NULL;
8535
8536         for (i = 0; i < set->n; ++i) {
8537                 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
8538                 if (!set->p[i])
8539                         goto error;
8540         }
8541
8542         set->dim = isl_dim_unwrap(set->dim);
8543         if (!set->dim)
8544                 goto error;
8545
8546         return (isl_map *)set;
8547 error:
8548         isl_set_free(set);
8549         return NULL;
8550 }
8551
8552 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
8553         enum isl_dim_type type)
8554 {
8555         if (!bmap)
8556                 return NULL;
8557
8558         if (!isl_dim_is_named_or_nested(bmap->dim, type))
8559                 return bmap;
8560
8561         bmap = isl_basic_map_cow(bmap);
8562         if (!bmap)
8563                 return NULL;
8564
8565         bmap->dim = isl_dim_reset(bmap->dim, type);
8566         if (!bmap->dim)
8567                 goto error;
8568
8569         bmap = isl_basic_map_finalize(bmap);
8570
8571         return bmap;
8572 error:
8573         isl_basic_map_free(bmap);
8574         return NULL;
8575 }
8576
8577 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
8578         enum isl_dim_type type)
8579 {
8580         int i;
8581
8582         if (!map)
8583                 return NULL;
8584
8585         if (!isl_dim_is_named_or_nested(map->dim, type))
8586                 return map;
8587
8588         map = isl_map_cow(map);
8589         if (!map)
8590                 return NULL;
8591
8592         for (i = 0; i < map->n; ++i) {
8593                 map->p[i] = isl_basic_map_reset(map->p[i], type);
8594                 if (!map->p[i])
8595                         goto error;
8596         }
8597         map->dim = isl_dim_reset(map->dim, type);
8598         if (!map->dim)
8599                 goto error;
8600
8601         return map;
8602 error:
8603         isl_map_free(map);
8604         return NULL;
8605 }
8606
8607 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
8608 {
8609         if (!bmap)
8610                 return NULL;
8611
8612         if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
8613                 return bmap;
8614
8615         bmap = isl_basic_map_cow(bmap);
8616         if (!bmap)
8617                 return NULL;
8618
8619         bmap->dim = isl_dim_flatten(bmap->dim);
8620         if (!bmap->dim)
8621                 goto error;
8622
8623         bmap = isl_basic_map_finalize(bmap);
8624
8625         return bmap;
8626 error:
8627         isl_basic_map_free(bmap);
8628         return NULL;
8629 }
8630
8631 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
8632 {
8633         return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
8634 }
8635
8636 __isl_give isl_basic_map *isl_basic_map_flatten_range(
8637         __isl_take isl_basic_map *bmap)
8638 {
8639         if (!bmap)
8640                 return NULL;
8641
8642         if (!bmap->dim->nested[1])
8643                 return bmap;
8644
8645         bmap = isl_basic_map_cow(bmap);
8646         if (!bmap)
8647                 return NULL;
8648
8649         bmap->dim = isl_dim_flatten_range(bmap->dim);
8650         if (!bmap->dim)
8651                 goto error;
8652
8653         bmap = isl_basic_map_finalize(bmap);
8654
8655         return bmap;
8656 error:
8657         isl_basic_map_free(bmap);
8658         return NULL;
8659 }
8660
8661 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
8662 {
8663         int i;
8664
8665         if (!map)
8666                 return NULL;
8667
8668         if (!map->dim->nested[0] && !map->dim->nested[1])
8669                 return map;
8670
8671         map = isl_map_cow(map);
8672         if (!map)
8673                 return NULL;
8674
8675         for (i = 0; i < map->n; ++i) {
8676                 map->p[i] = isl_basic_map_flatten(map->p[i]);
8677                 if (!map->p[i])
8678                         goto error;
8679         }
8680         map->dim = isl_dim_flatten(map->dim);
8681         if (!map->dim)
8682                 goto error;
8683
8684         return map;
8685 error:
8686         isl_map_free(map);
8687         return NULL;
8688 }
8689
8690 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
8691 {
8692         return (isl_set *)isl_map_flatten((isl_map *)set);
8693 }
8694
8695 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
8696 {
8697         isl_dim *dim, *flat_dim;
8698         isl_map *map;
8699
8700         dim = isl_set_get_dim(set);
8701         flat_dim = isl_dim_flatten(isl_dim_copy(dim));
8702         map = isl_map_identity(isl_dim_join(isl_dim_reverse(dim), flat_dim));
8703         map = isl_map_intersect_domain(map, set);
8704
8705         return map;
8706 }
8707
8708 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
8709 {
8710         int i;
8711
8712         if (!map)
8713                 return NULL;
8714
8715         if (!map->dim->nested[1])
8716                 return map;
8717
8718         map = isl_map_cow(map);
8719         if (!map)
8720                 return NULL;
8721
8722         for (i = 0; i < map->n; ++i) {
8723                 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
8724                 if (!map->p[i])
8725                         goto error;
8726         }
8727         map->dim = isl_dim_flatten_range(map->dim);
8728         if (!map->dim)
8729                 goto error;
8730
8731         return map;
8732 error:
8733         isl_map_free(map);
8734         return NULL;
8735 }
8736
8737 /* Reorder the dimensions of "bmap" according to the given dim_map
8738  * and set the dimension specification to "dim".
8739  */
8740 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
8741         __isl_take isl_dim *dim, __isl_take struct isl_dim_map *dim_map)
8742 {
8743         isl_basic_map *res;
8744
8745         bmap = isl_basic_map_cow(bmap);
8746         if (!bmap || !dim || !dim_map)
8747                 goto error;
8748
8749         res = isl_basic_map_alloc_dim(dim,
8750                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
8751         res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
8752         res = isl_basic_map_finalize(res);
8753         return res;
8754 error:
8755         free(dim_map);
8756         isl_basic_map_free(bmap);
8757         isl_dim_free(dim);
8758         return NULL;
8759 }
8760
8761 /* Reorder the dimensions of "map" according to given reordering.
8762  */
8763 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
8764         __isl_take isl_reordering *r)
8765 {
8766         int i;
8767         struct isl_dim_map *dim_map;
8768
8769         map = isl_map_cow(map);
8770         dim_map = isl_dim_map_from_reordering(r);
8771         if (!map || !r || !dim_map)
8772                 goto error;
8773
8774         for (i = 0; i < map->n; ++i) {
8775                 struct isl_dim_map *dim_map_i;
8776
8777                 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
8778
8779                 map->p[i] = isl_basic_map_realign(map->p[i],
8780                                             isl_dim_copy(r->dim), dim_map_i);
8781
8782                 if (!map->p[i])
8783                         goto error;
8784         }
8785
8786         map = isl_map_reset_dim(map, isl_dim_copy(r->dim));
8787
8788         isl_reordering_free(r);
8789         free(dim_map);
8790         return map;
8791 error:
8792         free(dim_map);
8793         isl_map_free(map);
8794         isl_reordering_free(r);
8795         return NULL;
8796 }
8797
8798 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
8799         __isl_take isl_reordering *r)
8800 {
8801         return (isl_set *)isl_map_realign((isl_map *)set, r);
8802 }
8803
8804 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
8805         __isl_take isl_dim *model)
8806 {
8807         isl_ctx *ctx;
8808
8809         if (!map || !model)
8810                 goto error;
8811
8812         ctx = isl_dim_get_ctx(model);
8813         if (!isl_dim_has_named_params(model))
8814                 isl_die(ctx, isl_error_invalid,
8815                         "model has unnamed parameters", goto error);
8816         if (!isl_dim_has_named_params(map->dim))
8817                 isl_die(ctx, isl_error_invalid,
8818                         "relation has unnamed parameters", goto error);
8819         if (!isl_dim_match(map->dim, isl_dim_param, model, isl_dim_param)) {
8820                 isl_reordering *exp;
8821
8822                 model = isl_dim_drop(model, isl_dim_in,
8823                                         0, isl_dim_size(model, isl_dim_in));
8824                 model = isl_dim_drop(model, isl_dim_out,
8825                                         0, isl_dim_size(model, isl_dim_out));
8826                 exp = isl_parameter_alignment_reordering(map->dim, model);
8827                 exp = isl_reordering_extend_dim(exp, isl_map_get_dim(map));
8828                 map = isl_map_realign(map, exp);
8829         }
8830
8831         isl_dim_free(model);
8832         return map;
8833 error:
8834         isl_dim_free(model);
8835         isl_map_free(map);
8836         return NULL;
8837 }
8838
8839 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
8840         __isl_take isl_dim *model)
8841 {
8842         return isl_map_align_params(set, model);
8843 }
8844
8845 __isl_give isl_mat *isl_basic_map_equalities_matrix(
8846                 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8847                 enum isl_dim_type c2, enum isl_dim_type c3,
8848                 enum isl_dim_type c4, enum isl_dim_type c5)
8849 {
8850         enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8851         struct isl_mat *mat;
8852         int i, j, k;
8853         int pos;
8854
8855         if (!bmap)
8856                 return NULL;
8857         mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
8858                                 isl_basic_map_total_dim(bmap) + 1);
8859         if (!mat)
8860                 return NULL;
8861         for (i = 0; i < bmap->n_eq; ++i)
8862                 for (j = 0, pos = 0; j < 5; ++j) {
8863                         int off = isl_basic_map_offset(bmap, c[j]);
8864                         for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8865                                 isl_int_set(mat->row[i][pos],
8866                                             bmap->eq[i][off + k]);
8867                                 ++pos;
8868                         }
8869                 }
8870
8871         return mat;
8872 }
8873
8874 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
8875                 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
8876                 enum isl_dim_type c2, enum isl_dim_type c3,
8877                 enum isl_dim_type c4, enum isl_dim_type c5)
8878 {
8879         enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8880         struct isl_mat *mat;
8881         int i, j, k;
8882         int pos;
8883
8884         if (!bmap)
8885                 return NULL;
8886         mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
8887                                 isl_basic_map_total_dim(bmap) + 1);
8888         if (!mat)
8889                 return NULL;
8890         for (i = 0; i < bmap->n_ineq; ++i)
8891                 for (j = 0, pos = 0; j < 5; ++j) {
8892                         int off = isl_basic_map_offset(bmap, c[j]);
8893                         for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8894                                 isl_int_set(mat->row[i][pos],
8895                                             bmap->ineq[i][off + k]);
8896                                 ++pos;
8897                         }
8898                 }
8899
8900         return mat;
8901 }
8902
8903 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
8904         __isl_take isl_dim *dim,
8905         __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
8906         enum isl_dim_type c2, enum isl_dim_type c3,
8907         enum isl_dim_type c4, enum isl_dim_type c5)
8908 {
8909         enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
8910         isl_basic_map *bmap;
8911         unsigned total;
8912         unsigned extra;
8913         int i, j, k, l;
8914         int pos;
8915
8916         if (!dim || !eq || !ineq)
8917                 goto error;
8918
8919         if (eq->n_col != ineq->n_col)
8920                 isl_die(dim->ctx, isl_error_invalid,
8921                         "equalities and inequalities matrices should have "
8922                         "same number of columns", goto error);
8923
8924         total = 1 + isl_dim_total(dim);
8925
8926         if (eq->n_col < total)
8927                 isl_die(dim->ctx, isl_error_invalid,
8928                         "number of columns too small", goto error);
8929
8930         extra = eq->n_col - total;
8931
8932         bmap = isl_basic_map_alloc_dim(isl_dim_copy(dim), extra,
8933                                        eq->n_row, ineq->n_row);
8934         if (!bmap)
8935                 goto error;
8936         for (i = 0; i < extra; ++i) {
8937                 k = isl_basic_map_alloc_div(bmap);
8938                 if (k < 0)
8939                         goto error;
8940                 isl_int_set_si(bmap->div[k][0], 0);
8941         }
8942         for (i = 0; i < eq->n_row; ++i) {
8943                 l = isl_basic_map_alloc_equality(bmap);
8944                 if (l < 0)
8945                         goto error;
8946                 for (j = 0, pos = 0; j < 5; ++j) {
8947                         int off = isl_basic_map_offset(bmap, c[j]);
8948                         for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8949                                 isl_int_set(bmap->eq[l][off + k], 
8950                                             eq->row[i][pos]);
8951                                 ++pos;
8952                         }
8953                 }
8954         }
8955         for (i = 0; i < ineq->n_row; ++i) {
8956                 l = isl_basic_map_alloc_inequality(bmap);
8957                 if (l < 0)
8958                         goto error;
8959                 for (j = 0, pos = 0; j < 5; ++j) {
8960                         int off = isl_basic_map_offset(bmap, c[j]);
8961                         for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
8962                                 isl_int_set(bmap->ineq[l][off + k], 
8963                                             ineq->row[i][pos]);
8964                                 ++pos;
8965                         }
8966                 }
8967         }
8968
8969         isl_dim_free(dim);
8970         isl_mat_free(eq);
8971         isl_mat_free(ineq);
8972
8973         return bmap;
8974 error:
8975         isl_dim_free(dim);
8976         isl_mat_free(eq);
8977         isl_mat_free(ineq);
8978         return NULL;
8979 }
8980
8981 __isl_give isl_mat *isl_basic_set_equalities_matrix(
8982         __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8983         enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8984 {
8985         return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
8986                                                 c1, c2, c3, c4, isl_dim_in);
8987 }
8988
8989 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
8990         __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
8991         enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
8992 {
8993         return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
8994                                                  c1, c2, c3, c4, isl_dim_in);
8995 }
8996
8997 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
8998         __isl_take isl_dim *dim,
8999         __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
9000         enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
9001 {
9002         return (isl_basic_set*)
9003             isl_basic_map_from_constraint_matrices(dim, eq, ineq,
9004                                                    c1, c2, c3, c4, isl_dim_in);
9005 }
9006
9007 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
9008 {
9009         if (!bmap)
9010                 return -1;
9011         
9012         return isl_dim_can_zip(bmap->dim);
9013 }
9014
9015 int isl_map_can_zip(__isl_keep isl_map *map)
9016 {
9017         if (!map)
9018                 return -1;
9019         
9020         return isl_dim_can_zip(map->dim);
9021 }
9022
9023 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
9024  * (A -> C) -> (B -> D).
9025  */
9026 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
9027 {
9028         unsigned pos;
9029         unsigned n1;
9030         unsigned n2;
9031
9032         if (!bmap)
9033                 return NULL;
9034
9035         if (!isl_basic_map_can_zip(bmap))
9036                 isl_die(bmap->ctx, isl_error_invalid,
9037                         "basic map cannot be zipped", goto error);
9038         pos = isl_basic_map_offset(bmap, isl_dim_in) +
9039                 isl_dim_size(bmap->dim->nested[0], isl_dim_in);
9040         n1 = isl_dim_size(bmap->dim->nested[0], isl_dim_out);
9041         n2 = isl_dim_size(bmap->dim->nested[1], isl_dim_in);
9042         bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
9043         if (!bmap)
9044                 return NULL;
9045         bmap->dim = isl_dim_zip(bmap->dim);
9046         if (!bmap->dim)
9047                 goto error;
9048         return bmap;
9049 error:
9050         isl_basic_map_free(bmap);
9051         return NULL;
9052 }
9053
9054 /* Given a map (A -> B) -> (C -> D), return the corresponding map
9055  * (A -> C) -> (B -> D).
9056  */
9057 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
9058 {
9059         int i;
9060
9061         if (!map)
9062                 return NULL;
9063
9064         if (!isl_map_can_zip(map))
9065                 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
9066                         goto error);
9067
9068         map = isl_map_cow(map);
9069         if (!map)
9070                 return NULL;
9071
9072         for (i = 0; i < map->n; ++i) {
9073                 map->p[i] = isl_basic_map_zip(map->p[i]);
9074                 if (!map->p[i])
9075                         goto error;
9076         }
9077
9078         map->dim = isl_dim_zip(map->dim);
9079         if (!map->dim)
9080                 goto error;
9081
9082         return map;
9083 error:
9084         isl_map_free(map);
9085         return NULL;
9086 }
9087
9088 /* Construct a basic map mapping the domain of the affine expression
9089  * to a one-dimensional range prescribed by the affine expression.
9090  */
9091 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
9092 {
9093         int k;
9094         int pos;
9095         isl_local_space *ls;
9096         isl_basic_map *bmap;
9097
9098         if (!aff)
9099                 return NULL;
9100
9101         ls = isl_aff_get_local_space(aff);
9102         ls = isl_local_space_from_domain(ls);
9103         ls = isl_local_space_add_dims(ls, isl_dim_out, 1);
9104         bmap = isl_basic_map_from_local_space(ls);
9105         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
9106         k = isl_basic_map_alloc_equality(bmap);
9107         if (k < 0)
9108                 goto error;
9109
9110         pos = isl_basic_map_offset(bmap, isl_dim_out);
9111         isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
9112         isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
9113         isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
9114                     aff->v->size - (pos + 1));
9115
9116         isl_aff_free(aff);
9117         bmap = isl_basic_map_finalize(bmap);
9118         return bmap;
9119 error:
9120         isl_aff_free(aff);
9121         isl_basic_map_free(bmap);
9122         return NULL;
9123 }
9124
9125 /* Construct a basic map mapping a domain in the given space to
9126  * to an n-dimensional range, with n the number of elements in the list,
9127  * where each coordinate in the range is prescribed by the
9128  * corresponding affine expression.
9129  * The domains of all affine expressions in the list are assumed to match
9130  * domain_dim.
9131  */
9132 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
9133         __isl_take isl_dim *domain_dim, __isl_take isl_aff_list *list)
9134 {
9135         int i;
9136         isl_dim *dim;
9137         isl_basic_map *bmap;
9138
9139         if (!list)
9140                 return NULL;
9141
9142         dim = isl_dim_from_domain(domain_dim);
9143         bmap = isl_basic_map_universe(dim);
9144
9145         for (i = 0; i < list->n; ++i) {
9146                 isl_aff *aff;
9147                 isl_basic_map *bmap_i;
9148
9149                 aff = isl_aff_copy(list->p[i]);
9150                 bmap_i = isl_basic_map_from_aff(aff);
9151
9152                 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
9153         }
9154
9155         isl_aff_list_free(list);
9156         return bmap;
9157 }
9158
9159 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
9160         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9161 {
9162         return isl_map_equate(set, type1, pos1, type2, pos2);
9163 }
9164
9165 /* Add a constraint imposing that the given two dimensions are equal.
9166  */
9167 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
9168         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9169 {
9170         isl_basic_map *bmap = NULL;
9171         int i;
9172
9173         if (!map)
9174                 return NULL;
9175
9176         if (pos1 >= isl_map_dim(map, type1))
9177                 isl_die(map->ctx, isl_error_invalid,
9178                         "index out of bounds", goto error);
9179         if (pos2 >= isl_map_dim(map, type2))
9180                 isl_die(map->ctx, isl_error_invalid,
9181                         "index out of bounds", goto error);
9182
9183         bmap = isl_basic_map_alloc_dim(isl_map_get_dim(map), 0, 1, 0);
9184         i = isl_basic_map_alloc_equality(bmap);
9185         if (i < 0)
9186                 goto error;
9187         isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9188         pos1 += isl_basic_map_offset(bmap, type1);
9189         pos2 += isl_basic_map_offset(bmap, type2);
9190         isl_int_set_si(bmap->eq[i][pos1], -1);
9191         isl_int_set_si(bmap->eq[i][pos2], 1);
9192         bmap = isl_basic_map_finalize(bmap);
9193
9194         map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9195
9196         return map;
9197 error:
9198         isl_basic_map_free(bmap);
9199         isl_map_free(map);
9200         return NULL;
9201 }
9202
9203 /* Add a constraint imposing that the given two dimensions have opposite values.
9204  */
9205 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
9206         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
9207 {
9208         isl_basic_map *bmap = NULL;
9209         int i;
9210
9211         if (!map)
9212                 return NULL;
9213
9214         if (pos1 >= isl_map_dim(map, type1))
9215                 isl_die(map->ctx, isl_error_invalid,
9216                         "index out of bounds", goto error);
9217         if (pos2 >= isl_map_dim(map, type2))
9218                 isl_die(map->ctx, isl_error_invalid,
9219                         "index out of bounds", goto error);
9220
9221         bmap = isl_basic_map_alloc_dim(isl_map_get_dim(map), 0, 1, 0);
9222         i = isl_basic_map_alloc_equality(bmap);
9223         if (i < 0)
9224                 goto error;
9225         isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
9226         pos1 += isl_basic_map_offset(bmap, type1);
9227         pos2 += isl_basic_map_offset(bmap, type2);
9228         isl_int_set_si(bmap->eq[i][pos1], 1);
9229         isl_int_set_si(bmap->eq[i][pos2], 1);
9230         bmap = isl_basic_map_finalize(bmap);
9231
9232         map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
9233
9234         return map;
9235 error:
9236         isl_basic_map_free(bmap);
9237         isl_map_free(map);
9238         return NULL;
9239 }