isl_basic_map_order_divs: avoid NULL pointer dereference
[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 <strings.h>
15 #include "isl_ctx.h"
16 #include "isl_blk.h"
17 #include "isl_dim_private.h"
18 #include "isl_equalities.h"
19 #include "isl_list.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_private.h"
25 #include "isl_map_piplib.h"
26 #include "isl_sample.h"
27 #include "isl_tab.h"
28 #include "isl_vec.h"
29
30 /* Maps dst positions to src positions */
31 struct isl_dim_map {
32         unsigned len;
33         int pos[1];
34 };
35
36 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
37 {
38         int i;
39         struct isl_dim_map *dim_map;
40         dim_map = isl_alloc(ctx, struct isl_dim_map,
41                                 sizeof(struct isl_dim_map) + len * sizeof(int));
42         if (!dim_map)
43                 return NULL;
44         dim_map->len = 1 + len;
45         dim_map->pos[0] = 0;
46         for (i = 0; i < len; ++i)
47                 dim_map->pos[1 + i] = -1;
48         return dim_map;
49 }
50
51 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
52 {
53         switch (type) {
54         case isl_dim_param:     return dim->nparam;
55         case isl_dim_in:        return dim->n_in;
56         case isl_dim_out:       return dim->n_out;
57         case isl_dim_all:       return dim->nparam + dim->n_in + dim->n_out;
58         default:                return 0;
59         }
60 }
61
62 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
63 {
64         switch (type) {
65         case isl_dim_param:     return 1;
66         case isl_dim_in:        return 1 + dim->nparam;
67         case isl_dim_out:       return 1 + dim->nparam + dim->n_in;
68         default:                return 0;
69         }
70 }
71
72 static void isl_dim_map_dim_range(struct isl_dim_map *dim_map,
73         struct isl_dim *dim, enum isl_dim_type type,
74         unsigned first, unsigned n, unsigned dst_pos)
75 {
76         int i;
77         unsigned src_pos;
78
79         if (!dim_map || !dim)
80                 return;
81         
82         src_pos = pos(dim, type);
83         for (i = 0; i < n; ++i)
84                 dim_map->pos[1 + dst_pos + i] = src_pos + first + i;
85 }
86
87 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
88                 enum isl_dim_type type, unsigned dst_pos)
89 {
90         isl_dim_map_dim_range(dim_map, dim, type, 0, n(dim, type), dst_pos);
91 }
92
93 static void isl_dim_map_div(struct isl_dim_map *dim_map,
94                 struct isl_basic_map *bmap, unsigned dst_pos)
95 {
96         int i;
97         unsigned src_pos;
98
99         if (!dim_map || !bmap)
100                 return;
101         
102         src_pos = 1 + isl_dim_total(bmap->dim);
103         for (i = 0; i < bmap->n_div; ++i)
104                 dim_map->pos[1 + dst_pos + i] = src_pos + i;
105 }
106
107 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
108 {
109         int i;
110
111         for (i = 0; i < dim_map->len; ++i)
112                 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
113         fprintf(stderr, "\n");
114 }
115
116 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
117                                 enum isl_dim_type type)
118 {
119         switch (type) {
120         case isl_dim_param:
121         case isl_dim_in:
122         case isl_dim_out:       return isl_dim_size(bmap->dim, type);
123         case isl_dim_div:       return bmap->n_div;
124         case isl_dim_all:       return isl_basic_map_total_dim(bmap);
125         default:                return 0;
126         }
127 }
128
129 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
130 {
131         return map ? n(map->dim, type) : 0;
132 }
133
134 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
135 {
136         return set ? n(set->dim, type) : 0;
137 }
138
139 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
140                                         enum isl_dim_type type)
141 {
142         struct isl_dim *dim = bmap->dim;
143         switch (type) {
144         case isl_dim_param:     return 1;
145         case isl_dim_in:        return 1 + dim->nparam;
146         case isl_dim_out:       return 1 + dim->nparam + dim->n_in;
147         case isl_dim_div:       return 1 + dim->nparam + dim->n_in + dim->n_out;
148         default:                return 0;
149         }
150 }
151
152 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
153 {
154         return pos(map->dim, type);
155 }
156
157 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
158                                 enum isl_dim_type type)
159 {
160         return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
161 }
162
163 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
164 {
165         return bset->dim->n_out;
166 }
167
168 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
169 {
170         return bset->dim->nparam;
171 }
172
173 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
174 {
175         return isl_dim_total(bset->dim) + bset->n_div;
176 }
177
178 unsigned isl_set_n_dim(const struct isl_set *set)
179 {
180         return set->dim->n_out;
181 }
182
183 unsigned isl_set_n_param(const struct isl_set *set)
184 {
185         return set->dim->nparam;
186 }
187
188 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
189 {
190         return bmap->dim->n_in;
191 }
192
193 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
194 {
195         return bmap->dim->n_out;
196 }
197
198 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
199 {
200         return bmap->dim->nparam;
201 }
202
203 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
204 {
205         return bmap->n_div;
206 }
207
208 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
209 {
210         return isl_dim_total(bmap->dim) + bmap->n_div;
211 }
212
213 unsigned isl_map_n_in(const struct isl_map *map)
214 {
215         return map->dim->n_in;
216 }
217
218 unsigned isl_map_n_out(const struct isl_map *map)
219 {
220         return map->dim->n_out;
221 }
222
223 unsigned isl_map_n_param(const struct isl_map *map)
224 {
225         return map->dim->nparam;
226 }
227
228 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
229 {
230         return map->dim->n_in == set->dim->n_out &&
231                map->dim->nparam == set->dim->nparam;
232 }
233
234 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
235                 struct isl_basic_set *bset)
236 {
237         return bmap->dim->n_in == bset->dim->n_out &&
238                bmap->dim->nparam == bset->dim->nparam;
239 }
240
241 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
242                 struct isl_basic_set *bset)
243 {
244         return bmap->dim->n_out == bset->dim->n_out &&
245                bmap->dim->nparam == bset->dim->nparam;
246 }
247
248 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
249 {
250         if (!bmap)
251                 return NULL;
252         return isl_dim_copy(bmap->dim);
253 }
254
255 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
256 {
257         if (!bset)
258                 return NULL;
259         return isl_dim_copy(bset->dim);
260 }
261
262 struct isl_dim *isl_map_get_dim(struct isl_map *map)
263 {
264         if (!map)
265                 return NULL;
266         return isl_dim_copy(map->dim);
267 }
268
269 struct isl_dim *isl_set_get_dim(struct isl_set *set)
270 {
271         if (!set)
272                 return NULL;
273         return isl_dim_copy(set->dim);
274 }
275
276 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
277         __isl_take isl_basic_map *bmap,
278         enum isl_dim_type type, unsigned pos, const char *s)
279 {
280         if (!bmap)
281                 return NULL;
282         bmap->dim = isl_dim_set_name(bmap->dim, type, pos, s);
283         if (!bmap->dim)
284                 goto error;
285         return bmap;
286 error:
287         isl_basic_map_free(bmap);
288         return NULL;
289 }
290
291 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
292         enum isl_dim_type type, unsigned pos, const char *s)
293 {
294         int i;
295
296         if (!map)
297                 return NULL;
298
299         map->dim = isl_dim_set_name(map->dim, type, pos, s);
300         if (!map->dim)
301                 goto error;
302
303         for (i = 0; i < map->n; ++i) {
304                 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
305                 if (!map->p[i])
306                         goto error;
307         }
308
309         return map;
310 error:
311         isl_map_free(map);
312         return NULL;
313 }
314
315 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
316         __isl_take isl_basic_set *bset,
317         enum isl_dim_type type, unsigned pos, const char *s)
318 {
319         return (isl_basic_set *)isl_basic_map_set_dim_name(
320                 (isl_basic_map *)bset, type, pos, s);
321 }
322
323 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
324         enum isl_dim_type type, unsigned pos, const char *s)
325 {
326         return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
327 }
328
329 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
330 {
331         if (!bmap)
332                 return -1;
333         return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
334 }
335
336 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
337                 struct isl_basic_map *bmap, unsigned extra,
338                 unsigned n_eq, unsigned n_ineq)
339 {
340         int i;
341         size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
342
343         bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
344         if (isl_blk_is_error(bmap->block)) {
345                 free(bmap);
346                 return NULL;
347         }
348
349         bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
350         if (!bmap->ineq) {
351                 isl_blk_free(ctx, bmap->block);
352                 free(bmap);
353                 return NULL;
354         }
355
356         if (extra == 0) {
357                 bmap->block2 = isl_blk_empty();
358                 bmap->div = NULL;
359         } else {
360                 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
361                 if (isl_blk_is_error(bmap->block2)) {
362                         free(bmap->ineq);
363                         isl_blk_free(ctx, bmap->block);
364                         free(bmap);
365                         return NULL;
366                 }
367
368                 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
369                 if (!bmap->div) {
370                         isl_blk_free(ctx, bmap->block2);
371                         free(bmap->ineq);
372                         isl_blk_free(ctx, bmap->block);
373                         free(bmap);
374                         return NULL;
375                 }
376         }
377
378         for (i = 0; i < n_ineq + n_eq; ++i)
379                 bmap->ineq[i] = bmap->block.data + i * row_size;
380
381         for (i = 0; i < extra; ++i)
382                 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
383
384         bmap->ctx = ctx;
385         isl_ctx_ref(ctx);
386         bmap->ref = 1;
387         bmap->flags = 0;
388         bmap->c_size = n_eq + n_ineq;
389         bmap->eq = bmap->ineq + n_ineq;
390         bmap->extra = extra;
391         bmap->n_eq = 0;
392         bmap->n_ineq = 0;
393         bmap->n_div = 0;
394         bmap->sample = NULL;
395
396         return bmap;
397 }
398
399 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
400                 unsigned nparam, unsigned dim, unsigned extra,
401                 unsigned n_eq, unsigned n_ineq)
402 {
403         struct isl_basic_map *bmap;
404         bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
405         return (struct isl_basic_set *)bmap;
406 }
407
408 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
409                 unsigned extra, unsigned n_eq, unsigned n_ineq)
410 {
411         struct isl_basic_map *bmap;
412         if (!dim)
413                 return NULL;
414         isl_assert(dim->ctx, dim->n_in == 0, return NULL);
415         bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
416         return (struct isl_basic_set *)bmap;
417 }
418
419 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
420                 unsigned extra, unsigned n_eq, unsigned n_ineq)
421 {
422         struct isl_basic_map *bmap;
423
424         if (!dim)
425                 return NULL;
426         bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
427         if (!bmap)
428                 goto error;
429         bmap->dim = dim;
430
431         return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
432 error:
433         isl_dim_free(dim);
434         return NULL;
435 }
436
437 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
438                 unsigned nparam, unsigned in, unsigned out, unsigned extra,
439                 unsigned n_eq, unsigned n_ineq)
440 {
441         struct isl_basic_map *bmap;
442         struct isl_dim *dim;
443
444         dim = isl_dim_alloc(ctx, nparam, in, out);
445         if (!dim)
446                 return NULL;
447
448         bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
449         return bmap;
450 }
451
452 static void dup_constraints(
453                 struct isl_basic_map *dst, struct isl_basic_map *src)
454 {
455         int i;
456         unsigned total = isl_basic_map_total_dim(src);
457
458         for (i = 0; i < src->n_eq; ++i) {
459                 int j = isl_basic_map_alloc_equality(dst);
460                 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
461         }
462
463         for (i = 0; i < src->n_ineq; ++i) {
464                 int j = isl_basic_map_alloc_inequality(dst);
465                 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
466         }
467
468         for (i = 0; i < src->n_div; ++i) {
469                 int j = isl_basic_map_alloc_div(dst);
470                 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
471         }
472         ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
473 }
474
475 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
476 {
477         struct isl_basic_map *dup;
478
479         if (!bmap)
480                 return NULL;
481         dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
482                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
483         if (!dup)
484                 return NULL;
485         dup_constraints(dup, bmap);
486         dup->flags = bmap->flags;
487         dup->sample = isl_vec_copy(bmap->sample);
488         return dup;
489 }
490
491 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
492 {
493         struct isl_basic_map *dup;
494
495         dup = isl_basic_map_dup((struct isl_basic_map *)bset);
496         return (struct isl_basic_set *)dup;
497 }
498
499 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
500 {
501         if (!bset)
502                 return NULL;
503
504         if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
505                 bset->ref++;
506                 return bset;
507         }
508         return isl_basic_set_dup(bset);
509 }
510
511 struct isl_set *isl_set_copy(struct isl_set *set)
512 {
513         if (!set)
514                 return NULL;
515
516         set->ref++;
517         return set;
518 }
519
520 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
521 {
522         if (!bmap)
523                 return NULL;
524
525         if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
526                 bmap->ref++;
527                 return bmap;
528         }
529         bmap = isl_basic_map_dup(bmap);
530         if (bmap)
531                 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
532         return bmap;
533 }
534
535 struct isl_map *isl_map_copy(struct isl_map *map)
536 {
537         if (!map)
538                 return NULL;
539
540         map->ref++;
541         return map;
542 }
543
544 void isl_basic_map_free(struct isl_basic_map *bmap)
545 {
546         if (!bmap)
547                 return;
548
549         if (--bmap->ref > 0)
550                 return;
551
552         isl_ctx_deref(bmap->ctx);
553         free(bmap->div);
554         isl_blk_free(bmap->ctx, bmap->block2);
555         free(bmap->ineq);
556         isl_blk_free(bmap->ctx, bmap->block);
557         isl_vec_free(bmap->sample);
558         isl_dim_free(bmap->dim);
559         free(bmap);
560 }
561
562 void isl_basic_set_free(struct isl_basic_set *bset)
563 {
564         isl_basic_map_free((struct isl_basic_map *)bset);
565 }
566
567 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
568 {
569         return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
570 }
571
572 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
573 {
574         struct isl_ctx *ctx;
575         if (!bmap)
576                 return -1;
577         ctx = bmap->ctx;
578         isl_assert(ctx, room_for_con(bmap, 1), return -1);
579         isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
580                         return -1);
581         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
582         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
583         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
584         ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
585         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
586         if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
587                 isl_int *t;
588                 int j = isl_basic_map_alloc_inequality(bmap);
589                 if (j < 0)
590                         return -1;
591                 t = bmap->ineq[j];
592                 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
593                 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
594                 bmap->eq[-1] = t;
595                 bmap->n_eq++;
596                 bmap->n_ineq--;
597                 bmap->eq--;
598                 return 0;
599         }
600         isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
601                       bmap->extra - bmap->n_div);
602         return bmap->n_eq++;
603 }
604
605 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
606 {
607         return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
608 }
609
610 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
611 {
612         if (!bmap)
613                 return -1;
614         isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
615         bmap->n_eq -= n;
616         return 0;
617 }
618
619 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
620 {
621         return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
622 }
623
624 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
625 {
626         isl_int *t;
627         if (!bmap)
628                 return -1;
629         isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
630
631         if (pos != bmap->n_eq - 1) {
632                 t = bmap->eq[pos];
633                 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
634                 bmap->eq[bmap->n_eq - 1] = t;
635         }
636         bmap->n_eq--;
637         return 0;
638 }
639
640 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
641 {
642         return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
643 }
644
645 void isl_basic_map_inequality_to_equality(
646                 struct isl_basic_map *bmap, unsigned pos)
647 {
648         isl_int *t;
649
650         t = bmap->ineq[pos];
651         bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
652         bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
653         bmap->eq[-1] = t;
654         bmap->n_eq++;
655         bmap->n_ineq--;
656         bmap->eq--;
657         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
658         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
659         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
660         ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
661 }
662
663 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
664 {
665         return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
666 }
667
668 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
669 {
670         struct isl_ctx *ctx;
671         if (!bmap)
672                 return -1;
673         ctx = bmap->ctx;
674         isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
675         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
676         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
677         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
678         ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
679         isl_seq_clr(bmap->ineq[bmap->n_ineq] +
680                       1 + isl_basic_map_total_dim(bmap),
681                       bmap->extra - bmap->n_div);
682         return bmap->n_ineq++;
683 }
684
685 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
686 {
687         return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
688 }
689
690 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
691 {
692         if (!bmap)
693                 return -1;
694         isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
695         bmap->n_ineq -= n;
696         return 0;
697 }
698
699 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
700 {
701         return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
702 }
703
704 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
705 {
706         isl_int *t;
707         if (!bmap)
708                 return -1;
709         isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
710
711         if (pos != bmap->n_ineq - 1) {
712                 t = bmap->ineq[pos];
713                 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
714                 bmap->ineq[bmap->n_ineq - 1] = t;
715                 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
716         }
717         bmap->n_ineq--;
718         return 0;
719 }
720
721 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
722 {
723         return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
724 }
725
726 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
727         isl_int *eq)
728 {
729         int k;
730
731         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
732         if (!bmap)
733                 return NULL;
734         k = isl_basic_map_alloc_equality(bmap);
735         if (k < 0)
736                 goto error;
737         isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
738         return bmap;
739 error:
740         isl_basic_map_free(bmap);
741         return NULL;
742 }
743
744 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
745         isl_int *eq)
746 {
747         return (isl_basic_set *)
748                 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
749 }
750
751 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
752         isl_int *ineq)
753 {
754         int k;
755
756         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
757         if (!bmap)
758                 return NULL;
759         k = isl_basic_map_alloc_inequality(bmap);
760         if (k < 0)
761                 goto error;
762         isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
763         return bmap;
764 error:
765         isl_basic_map_free(bmap);
766         return NULL;
767 }
768
769 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
770         isl_int *ineq)
771 {
772         return (isl_basic_set *)
773                 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
774 }
775
776 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
777 {
778         if (!bmap)
779                 return -1;
780         isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
781         isl_seq_clr(bmap->div[bmap->n_div] +
782                       1 + 1 + isl_basic_map_total_dim(bmap),
783                       bmap->extra - bmap->n_div);
784         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
785         return bmap->n_div++;
786 }
787
788 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
789 {
790         return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
791 }
792
793 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
794 {
795         if (!bmap)
796                 return -1;
797         isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
798         bmap->n_div -= n;
799         return 0;
800 }
801
802 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
803 {
804         return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
805 }
806
807 /* Copy constraint from src to dst, putting the vars of src at offset
808  * dim_off in dst and the divs of src at offset div_off in dst.
809  * If both sets are actually map, then dim_off applies to the input
810  * variables.
811  */
812 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
813                             struct isl_basic_map *src_map, isl_int *src,
814                             unsigned in_off, unsigned out_off, unsigned div_off)
815 {
816         unsigned src_nparam = isl_basic_map_n_param(src_map);
817         unsigned dst_nparam = isl_basic_map_n_param(dst_map);
818         unsigned src_in = isl_basic_map_n_in(src_map);
819         unsigned dst_in = isl_basic_map_n_in(dst_map);
820         unsigned src_out = isl_basic_map_n_out(src_map);
821         unsigned dst_out = isl_basic_map_n_out(dst_map);
822         isl_int_set(dst[0], src[0]);
823         isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
824         if (dst_nparam > src_nparam)
825                 isl_seq_clr(dst+1+src_nparam,
826                                 dst_nparam - src_nparam);
827         isl_seq_clr(dst+1+dst_nparam, in_off);
828         isl_seq_cpy(dst+1+dst_nparam+in_off,
829                     src+1+src_nparam,
830                     isl_min(dst_in-in_off, src_in));
831         if (dst_in-in_off > src_in)
832                 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
833                                 dst_in - in_off - src_in);
834         isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
835         isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
836                     src+1+src_nparam+src_in,
837                     isl_min(dst_out-out_off, src_out));
838         if (dst_out-out_off > src_out)
839                 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
840                                 dst_out - out_off - src_out);
841         isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
842         isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
843                     src+1+src_nparam+src_in+src_out,
844                     isl_min(dst_map->extra-div_off, src_map->n_div));
845         if (dst_map->n_div-div_off > src_map->n_div)
846                 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
847                                 div_off+src_map->n_div,
848                                 dst_map->n_div - div_off - src_map->n_div);
849 }
850
851 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
852                      struct isl_basic_map *src_map, isl_int *src,
853                      unsigned in_off, unsigned out_off, unsigned div_off)
854 {
855         isl_int_set(dst[0], src[0]);
856         copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
857 }
858
859 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
860                 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
861 {
862         int i;
863         unsigned div_off;
864
865         if (!bmap1 || !bmap2)
866                 goto error;
867
868         div_off = bmap1->n_div;
869
870         for (i = 0; i < bmap2->n_eq; ++i) {
871                 int i1 = isl_basic_map_alloc_equality(bmap1);
872                 if (i1 < 0)
873                         goto error;
874                 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
875                                 i_pos, o_pos, div_off);
876         }
877
878         for (i = 0; i < bmap2->n_ineq; ++i) {
879                 int i1 = isl_basic_map_alloc_inequality(bmap1);
880                 if (i1 < 0)
881                         goto error;
882                 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
883                                 i_pos, o_pos, div_off);
884         }
885
886         for (i = 0; i < bmap2->n_div; ++i) {
887                 int i1 = isl_basic_map_alloc_div(bmap1);
888                 if (i1 < 0)
889                         goto error;
890                 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
891                          i_pos, o_pos, div_off);
892         }
893
894         isl_basic_map_free(bmap2);
895
896         return bmap1;
897
898 error:
899         isl_basic_map_free(bmap1);
900         isl_basic_map_free(bmap2);
901         return NULL;
902 }
903
904 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
905                                         struct isl_dim_map *dim_map)
906 {
907         int i;
908
909         for (i = 0; i < dim_map->len; ++i) {
910                 if (dim_map->pos[i] < 0)
911                         isl_int_set_si(dst[i], 0);
912                 else
913                         isl_int_set(dst[i], src[dim_map->pos[i]]);
914         }
915 }
916
917 static void copy_div_dim_map(isl_int *dst, isl_int *src,
918                                         struct isl_dim_map *dim_map)
919 {
920         isl_int_set(dst[0], src[0]);
921         copy_constraint_dim_map(dst+1, src+1, dim_map);
922 }
923
924 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
925                 struct isl_basic_map *src, struct isl_dim_map *dim_map)
926 {
927         int i;
928
929         if (!src || !dst || !dim_map)
930                 goto error;
931
932         for (i = 0; i < src->n_eq; ++i) {
933                 int i1 = isl_basic_map_alloc_equality(dst);
934                 if (i1 < 0)
935                         goto error;
936                 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
937         }
938
939         for (i = 0; i < src->n_ineq; ++i) {
940                 int i1 = isl_basic_map_alloc_inequality(dst);
941                 if (i1 < 0)
942                         goto error;
943                 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
944         }
945
946         for (i = 0; i < src->n_div; ++i) {
947                 int i1 = isl_basic_map_alloc_div(dst);
948                 if (i1 < 0)
949                         goto error;
950                 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
951         }
952
953         free(dim_map);
954         isl_basic_map_free(src);
955
956         return dst;
957 error:
958         free(dim_map);
959         isl_basic_map_free(src);
960         isl_basic_map_free(dst);
961         return NULL;
962 }
963
964 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
965                 struct isl_basic_set *bset2, unsigned pos)
966 {
967         return (struct isl_basic_set *)
968                 add_constraints((struct isl_basic_map *)bset1,
969                                 (struct isl_basic_map *)bset2, 0, pos);
970 }
971
972 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
973                 struct isl_dim *dim, unsigned extra,
974                 unsigned n_eq, unsigned n_ineq)
975 {
976         struct isl_basic_map *ext;
977         unsigned flags;
978         int dims_ok;
979
980         if (!dim)
981                 goto error;
982
983         if (!base)
984                 goto error;
985
986         dims_ok = isl_dim_equal(base->dim, dim) &&
987                   base->extra >= base->n_div + extra;
988
989         if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
990                        room_for_ineq(base, n_ineq)) {
991                 isl_dim_free(dim);
992                 return base;
993         }
994
995         isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
996         isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
997         isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
998         extra += base->extra;
999         n_eq += base->n_eq;
1000         n_ineq += base->n_ineq;
1001
1002         ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
1003         dim = NULL;
1004         if (!ext)
1005                 goto error;
1006
1007         if (dims_ok)
1008                 ext->sample = isl_vec_copy(base->sample);
1009         flags = base->flags;
1010         ext = add_constraints(ext, base, 0, 0);
1011         if (ext) {
1012                 ext->flags = flags;
1013                 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1014         }
1015
1016         return ext;
1017
1018 error:
1019         isl_dim_free(dim);
1020         isl_basic_map_free(base);
1021         return NULL;
1022 }
1023
1024 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
1025                 struct isl_dim *dim, unsigned extra,
1026                 unsigned n_eq, unsigned n_ineq)
1027 {
1028         return (struct isl_basic_set *)
1029                 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
1030                                                         extra, n_eq, n_ineq);
1031 }
1032
1033 struct isl_basic_map *isl_basic_map_extend_constraints(
1034                 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1035 {
1036         if (!base)
1037                 return NULL;
1038         return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
1039                                         0, n_eq, n_ineq);
1040 }
1041
1042 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1043                 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1044                 unsigned n_eq, unsigned n_ineq)
1045 {
1046         struct isl_basic_map *bmap;
1047         struct isl_dim *dim;
1048
1049         if (!base)
1050                 return NULL;
1051         dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
1052         if (!dim)
1053                 return NULL;
1054
1055         bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
1056         return bmap;
1057 }
1058
1059 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1060                 unsigned nparam, unsigned dim, unsigned extra,
1061                 unsigned n_eq, unsigned n_ineq)
1062 {
1063         return (struct isl_basic_set *)
1064                 isl_basic_map_extend((struct isl_basic_map *)base,
1065                                         nparam, 0, dim, extra, n_eq, n_ineq);
1066 }
1067
1068 struct isl_basic_set *isl_basic_set_extend_constraints(
1069                 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1070 {
1071         return (struct isl_basic_set *)
1072                 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1073                                                     n_eq, n_ineq);
1074 }
1075
1076 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1077 {
1078         return (struct isl_basic_set *)
1079                 isl_basic_map_cow((struct isl_basic_map *)bset);
1080 }
1081
1082 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1083 {
1084         if (!bmap)
1085                 return NULL;
1086
1087         if (bmap->ref > 1) {
1088                 bmap->ref--;
1089                 bmap = isl_basic_map_dup(bmap);
1090         }
1091         if (bmap)
1092                 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1093         return bmap;
1094 }
1095
1096 struct isl_set *isl_set_cow(struct isl_set *set)
1097 {
1098         if (!set)
1099                 return NULL;
1100
1101         if (set->ref == 1)
1102                 return set;
1103         set->ref--;
1104         return isl_set_dup(set);
1105 }
1106
1107 struct isl_map *isl_map_cow(struct isl_map *map)
1108 {
1109         if (!map)
1110                 return NULL;
1111
1112         if (map->ref == 1)
1113                 return map;
1114         map->ref--;
1115         return isl_map_dup(map);
1116 }
1117
1118 static void swap_vars(struct isl_blk blk, isl_int *a,
1119                         unsigned a_len, unsigned b_len)
1120 {
1121         isl_seq_cpy(blk.data, a+a_len, b_len);
1122         isl_seq_cpy(blk.data+b_len, a, a_len);
1123         isl_seq_cpy(a, blk.data, b_len+a_len);
1124 }
1125
1126 struct isl_basic_set *isl_basic_set_swap_vars(
1127                 struct isl_basic_set *bset, unsigned n)
1128 {
1129         int i;
1130         struct isl_blk blk;
1131         unsigned dim;
1132         unsigned nparam;
1133
1134         if (!bset)
1135                 goto error;
1136
1137         nparam = isl_basic_set_n_param(bset);
1138         dim = isl_basic_set_n_dim(bset);
1139         isl_assert(bset->ctx, n <= dim, goto error);
1140
1141         if (n == dim)
1142                 return bset;
1143
1144         bset = isl_basic_set_cow(bset);
1145         if (!bset)
1146                 return NULL;
1147
1148         blk = isl_blk_alloc(bset->ctx, dim);
1149         if (isl_blk_is_error(blk))
1150                 goto error;
1151
1152         for (i = 0; i < bset->n_eq; ++i)
1153                 swap_vars(blk,
1154                           bset->eq[i]+1+nparam, n, dim - n);
1155
1156         for (i = 0; i < bset->n_ineq; ++i)
1157                 swap_vars(blk,
1158                           bset->ineq[i]+1+nparam, n, dim - n);
1159
1160         for (i = 0; i < bset->n_div; ++i)
1161                 swap_vars(blk,
1162                           bset->div[i]+1+1+nparam, n, dim - n);
1163
1164         isl_blk_free(bset->ctx, blk);
1165
1166         ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1167         return isl_basic_set_gauss(bset, NULL);
1168 error:
1169         isl_basic_set_free(bset);
1170         return NULL;
1171 }
1172
1173 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1174 {
1175         int i;
1176         set = isl_set_cow(set);
1177         if (!set)
1178                 return NULL;
1179
1180         for (i = 0; i < set->n; ++i) {
1181                 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1182                 if (!set->p[i]) {
1183                         isl_set_free(set);
1184                         return NULL;
1185                 }
1186         }
1187         ISL_F_CLR(set, ISL_SET_NORMALIZED);
1188         return set;
1189 }
1190
1191 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1192 {
1193         int i = 0;
1194         unsigned total;
1195         if (!bmap)
1196                 goto error;
1197         total = isl_basic_map_total_dim(bmap);
1198         isl_basic_map_free_div(bmap, bmap->n_div);
1199         isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1200         if (bmap->n_eq > 0)
1201                 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1202         else {
1203                 i = isl_basic_map_alloc_equality(bmap);
1204                 if (i < 0)
1205                         goto error;
1206         }
1207         isl_int_set_si(bmap->eq[i][0], 1);
1208         isl_seq_clr(bmap->eq[i]+1, total);
1209         ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1210         isl_vec_free(bmap->sample);
1211         bmap->sample = NULL;
1212         return isl_basic_map_finalize(bmap);
1213 error:
1214         isl_basic_map_free(bmap);
1215         return NULL;
1216 }
1217
1218 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1219 {
1220         return (struct isl_basic_set *)
1221                 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1222 }
1223
1224 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1225 {
1226         int i;
1227         unsigned off = isl_dim_total(bmap->dim);
1228         isl_int *t = bmap->div[a];
1229         bmap->div[a] = bmap->div[b];
1230         bmap->div[b] = t;
1231
1232         for (i = 0; i < bmap->n_eq; ++i)
1233                 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1234
1235         for (i = 0; i < bmap->n_ineq; ++i)
1236                 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1237
1238         for (i = 0; i < bmap->n_div; ++i)
1239                 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1240         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1241 }
1242
1243 /* Eliminate the specified n dimensions starting at first from the
1244  * constraints using Fourier-Motzkin.  The dimensions themselves
1245  * are not removed.
1246  */
1247 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1248         enum isl_dim_type type, unsigned first, unsigned n)
1249 {
1250         int i;
1251
1252         if (!map)
1253                 return NULL;
1254         if (n == 0)
1255                 return map;
1256
1257         map = isl_map_cow(map);
1258         if (!map)
1259                 return NULL;
1260         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1261         first += pos(map->dim, type) - 1;
1262         
1263         for (i = 0; i < map->n; ++i) {
1264                 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1265                 if (!map->p[i])
1266                         goto error;
1267         }
1268         return map;
1269 error:
1270         isl_map_free(map);
1271         return NULL;
1272 }
1273
1274 /* Eliminate the specified n dimensions starting at first from the
1275  * constraints using Fourier-Motzkin.  The dimensions themselves
1276  * are not removed.
1277  */
1278 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1279         enum isl_dim_type type, unsigned first, unsigned n)
1280 {
1281         return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1282 }
1283
1284 /* Eliminate the specified n dimensions starting at first from the
1285  * constraints using Fourier-Motzkin.  The dimensions themselves
1286  * are not removed.
1287  */
1288 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1289         unsigned first, unsigned n)
1290 {
1291         return isl_set_eliminate(set, isl_dim_set, first, n);
1292 }
1293
1294 /* Project out n dimensions starting at first using Fourier-Motzkin */
1295 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1296         unsigned first, unsigned n)
1297 {
1298         set = isl_set_eliminate_dims(set, first, n);
1299         set = isl_set_drop_dims(set, first, n);
1300         return set;
1301 }
1302
1303 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1304 {
1305         bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1306                                                 bset->n_div);
1307         if (!bset)
1308                 return NULL;
1309         bset->n_div = 0;
1310         return bset;
1311 }
1312
1313 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1314 {
1315         int i;
1316
1317         if (!set)
1318                 return NULL;
1319         if (set->n == 0)
1320                 return set;
1321
1322         set = isl_set_cow(set);
1323         if (!set)
1324                 return NULL;
1325         
1326         for (i = 0; i < set->n; ++i) {
1327                 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1328                 if (!set->p[i])
1329                         goto error;
1330         }
1331         return set;
1332 error:
1333         isl_set_free(set);
1334         return NULL;
1335 }
1336
1337 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1338         enum isl_dim_type type, unsigned first, unsigned n)
1339 {
1340         if (!bmap)
1341                 return NULL;
1342         isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1343                         goto error);
1344         if (n == 0)
1345                 return bmap;
1346         bmap = isl_basic_map_eliminate_vars(bmap,
1347                         isl_basic_map_offset(bmap, type) - 1 + first, n);
1348         if (!bmap)
1349                 return bmap;
1350         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1351                 return bmap;
1352         bmap = isl_basic_map_drop(bmap, type, first, n);
1353         return bmap;
1354 error:
1355         isl_basic_map_free(bmap);
1356         return NULL;
1357 }
1358
1359 __isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
1360         enum isl_dim_type type, unsigned first, unsigned n)
1361 {
1362         return (isl_basic_set *)
1363                 isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
1364 }
1365
1366 struct isl_map *isl_map_remove(struct isl_map *map,
1367         enum isl_dim_type type, unsigned first, unsigned n)
1368 {
1369         int i;
1370
1371         if (n == 0)
1372                 return map;
1373
1374         map = isl_map_cow(map);
1375         if (!map)
1376                 return NULL;
1377         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1378         
1379         for (i = 0; i < map->n; ++i) {
1380                 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1381                         isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1382                 if (!map->p[i])
1383                         goto error;
1384         }
1385         map = isl_map_drop(map, type, first, n);
1386         return map;
1387 error:
1388         isl_map_free(map);
1389         return NULL;
1390 }
1391
1392 __isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
1393         enum isl_dim_type type, unsigned first, unsigned n)
1394 {
1395         return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
1396 }
1397
1398 /* Project out n inputs starting at first using Fourier-Motzkin */
1399 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1400         unsigned first, unsigned n)
1401 {
1402         return isl_map_remove(map, isl_dim_in, first, n);
1403 }
1404
1405 /* Project out n dimensions starting at first using Fourier-Motzkin */
1406 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1407         unsigned first, unsigned n)
1408 {
1409         unsigned nparam = isl_basic_set_n_param(bset);
1410         bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1411         bset = isl_basic_set_drop_dims(bset, first, n);
1412         return bset;
1413 }
1414
1415 static void dump_term(struct isl_basic_map *bmap,
1416                         isl_int c, int pos, FILE *out)
1417 {
1418         const char *name;
1419         unsigned in = isl_basic_map_n_in(bmap);
1420         unsigned dim = in + isl_basic_map_n_out(bmap);
1421         unsigned nparam = isl_basic_map_n_param(bmap);
1422         if (!pos)
1423                 isl_int_print(out, c, 0);
1424         else {
1425                 if (!isl_int_is_one(c))
1426                         isl_int_print(out, c, 0);
1427                 if (pos < 1 + nparam) {
1428                         name = isl_dim_get_name(bmap->dim,
1429                                                 isl_dim_param, pos - 1);
1430                         if (name)
1431                                 fprintf(out, "%s", name);
1432                         else
1433                                 fprintf(out, "p%d", pos - 1);
1434                 } else if (pos < 1 + nparam + in)
1435                         fprintf(out, "i%d", pos - 1 - nparam);
1436                 else if (pos < 1 + nparam + dim)
1437                         fprintf(out, "o%d", pos - 1 - nparam - in);
1438                 else
1439                         fprintf(out, "e%d", pos - 1 - nparam - dim);
1440         }
1441 }
1442
1443 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1444                                 int sign, FILE *out)
1445 {
1446         int i;
1447         int first;
1448         unsigned len = 1 + isl_basic_map_total_dim(bmap);
1449         isl_int v;
1450
1451         isl_int_init(v);
1452         for (i = 0, first = 1; i < len; ++i) {
1453                 if (isl_int_sgn(c[i]) * sign <= 0)
1454                         continue;
1455                 if (!first)
1456                         fprintf(out, " + ");
1457                 first = 0;
1458                 isl_int_abs(v, c[i]);
1459                 dump_term(bmap, v, i, out);
1460         }
1461         isl_int_clear(v);
1462         if (first)
1463                 fprintf(out, "0");
1464 }
1465
1466 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1467                                 const char *op, FILE *out, int indent)
1468 {
1469         int i;
1470
1471         fprintf(out, "%*s", indent, "");
1472
1473         dump_constraint_sign(bmap, c, 1, out);
1474         fprintf(out, " %s ", op);
1475         dump_constraint_sign(bmap, c, -1, out);
1476
1477         fprintf(out, "\n");
1478
1479         for (i = bmap->n_div; i < bmap->extra; ++i) {
1480                 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1481                         continue;
1482                 fprintf(out, "%*s", indent, "");
1483                 fprintf(out, "ERROR: unused div coefficient not zero\n");
1484                 abort();
1485         }
1486 }
1487
1488 static void dump_constraints(struct isl_basic_map *bmap,
1489                                 isl_int **c, unsigned n,
1490                                 const char *op, FILE *out, int indent)
1491 {
1492         int i;
1493
1494         for (i = 0; i < n; ++i)
1495                 dump_constraint(bmap, c[i], op, out, indent);
1496 }
1497
1498 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1499 {
1500         int j;
1501         int first = 1;
1502         unsigned total = isl_basic_map_total_dim(bmap);
1503
1504         for (j = 0; j < 1 + total; ++j) {
1505                 if (isl_int_is_zero(exp[j]))
1506                         continue;
1507                 if (!first && isl_int_is_pos(exp[j]))
1508                         fprintf(out, "+");
1509                 dump_term(bmap, exp[j], j, out);
1510                 first = 0;
1511         }
1512 }
1513
1514 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1515 {
1516         int i;
1517
1518         dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1519         dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1520
1521         for (i = 0; i < bmap->n_div; ++i) {
1522                 fprintf(out, "%*s", indent, "");
1523                 fprintf(out, "e%d = [(", i);
1524                 dump_affine(bmap, bmap->div[i]+1, out);
1525                 fprintf(out, ")/");
1526                 isl_int_print(out, bmap->div[i][0], 0);
1527                 fprintf(out, "]\n");
1528         }
1529 }
1530
1531 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1532 {
1533         if (!bset) {
1534                 fprintf(out, "null basic set\n");
1535                 return;
1536         }
1537
1538         fprintf(out, "%*s", indent, "");
1539         fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1540                         bset->ref, bset->dim->nparam, bset->dim->n_out,
1541                         bset->extra, bset->flags);
1542         dump((struct isl_basic_map *)bset, out, indent);
1543 }
1544
1545 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1546 {
1547         if (!bmap) {
1548                 fprintf(out, "null basic map\n");
1549                 return;
1550         }
1551
1552         fprintf(out, "%*s", indent, "");
1553         fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1554                         "flags: %x, n_name: %d\n",
1555                 bmap->ref,
1556                 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1557                 bmap->extra, bmap->flags, bmap->dim->n_name);
1558         dump(bmap, out, indent);
1559 }
1560
1561 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1562 {
1563         unsigned total;
1564         if (!bmap)
1565                 return -1;
1566         total = isl_basic_map_total_dim(bmap);
1567         isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1568         isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1569         isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1570         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1571         return 0;
1572 }
1573
1574 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1575 {
1576         struct isl_set *set;
1577
1578         if (!dim)
1579                 return NULL;
1580         isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1581         isl_assert(dim->ctx, n >= 0, return NULL);
1582         set = isl_alloc(dim->ctx, struct isl_set,
1583                         sizeof(struct isl_set) +
1584                         (n - 1) * sizeof(struct isl_basic_set *));
1585         if (!set)
1586                 goto error;
1587
1588         set->ctx = dim->ctx;
1589         isl_ctx_ref(set->ctx);
1590         set->ref = 1;
1591         set->size = n;
1592         set->n = 0;
1593         set->dim = dim;
1594         set->flags = flags;
1595         return set;
1596 error:
1597         isl_dim_free(dim);
1598         return NULL;
1599 }
1600
1601 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1602                 unsigned nparam, unsigned dim, int n, unsigned flags)
1603 {
1604         struct isl_set *set;
1605         struct isl_dim *dims;
1606
1607         dims = isl_dim_alloc(ctx, nparam, 0, dim);
1608         if (!dims)
1609                 return NULL;
1610
1611         set = isl_set_alloc_dim(dims, n, flags);
1612         return set;
1613 }
1614
1615 /* Make sure "map" has room for at least "n" more basic maps.
1616  */
1617 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1618 {
1619         int i;
1620         struct isl_map *grown = NULL;
1621
1622         if (!map)
1623                 return NULL;
1624         isl_assert(map->ctx, n >= 0, goto error);
1625         if (map->n + n <= map->size)
1626                 return map;
1627         grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1628         if (!grown)
1629                 goto error;
1630         for (i = 0; i < map->n; ++i) {
1631                 grown->p[i] = isl_basic_map_copy(map->p[i]);
1632                 if (!grown->p[i])
1633                         goto error;
1634                 grown->n++;
1635         }
1636         isl_map_free(map);
1637         return grown;
1638 error:
1639         isl_map_free(grown);
1640         isl_map_free(map);
1641         return NULL;
1642 }
1643
1644 /* Make sure "set" has room for at least "n" more basic sets.
1645  */
1646 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1647 {
1648         return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1649 }
1650
1651 struct isl_set *isl_set_dup(struct isl_set *set)
1652 {
1653         int i;
1654         struct isl_set *dup;
1655
1656         if (!set)
1657                 return NULL;
1658
1659         dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1660         if (!dup)
1661                 return NULL;
1662         for (i = 0; i < set->n; ++i)
1663                 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1664         return dup;
1665 }
1666
1667 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1668 {
1669         struct isl_set *set;
1670
1671         if (!bset)
1672                 return NULL;
1673
1674         set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1675         if (!set) {
1676                 isl_basic_set_free(bset);
1677                 return NULL;
1678         }
1679         return isl_set_add_basic_set(set, bset);
1680 }
1681
1682 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1683 {
1684         struct isl_map *map;
1685
1686         if (!bmap)
1687                 return NULL;
1688
1689         map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1690         if (!map) {
1691                 isl_basic_map_free(bmap);
1692                 return NULL;
1693         }
1694         return isl_map_add_basic_map(map, bmap);
1695 }
1696
1697 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1698                                                 __isl_take isl_basic_set *bset)
1699 {
1700         return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1701                                                 (struct isl_basic_map *)bset);
1702 }
1703
1704 void isl_set_free(struct isl_set *set)
1705 {
1706         int i;
1707
1708         if (!set)
1709                 return;
1710
1711         if (--set->ref > 0)
1712                 return;
1713
1714         isl_ctx_deref(set->ctx);
1715         for (i = 0; i < set->n; ++i)
1716                 isl_basic_set_free(set->p[i]);
1717         isl_dim_free(set->dim);
1718         free(set);
1719 }
1720
1721 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1722 {
1723         int i;
1724
1725         if (!set) {
1726                 fprintf(out, "null set\n");
1727                 return;
1728         }
1729
1730         fprintf(out, "%*s", indent, "");
1731         fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1732                         set->ref, set->n, set->dim->nparam, set->dim->n_out,
1733                         set->flags);
1734         for (i = 0; i < set->n; ++i) {
1735                 fprintf(out, "%*s", indent, "");
1736                 fprintf(out, "basic set %d:\n", i);
1737                 isl_basic_set_dump(set->p[i], out, indent+4);
1738         }
1739 }
1740
1741 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1742 {
1743         int i;
1744
1745         if (!map) {
1746                 fprintf(out, "null map\n");
1747                 return;
1748         }
1749
1750         fprintf(out, "%*s", indent, "");
1751         fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1752                      "flags: %x, n_name: %d\n",
1753                         map->ref, map->n, map->dim->nparam, map->dim->n_in,
1754                         map->dim->n_out, map->flags, map->dim->n_name);
1755         for (i = 0; i < map->n; ++i) {
1756                 fprintf(out, "%*s", indent, "");
1757                 fprintf(out, "basic map %d:\n", i);
1758                 isl_basic_map_dump(map->p[i], out, indent+4);
1759         }
1760 }
1761
1762 struct isl_basic_map *isl_basic_map_intersect_domain(
1763                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1764 {
1765         struct isl_basic_map *bmap_domain;
1766         struct isl_dim *dim;
1767
1768         if (!bmap || !bset)
1769                 goto error;
1770
1771         isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1772                                         bset->dim, isl_dim_param), goto error);
1773
1774         if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1775                 isl_assert(bset->ctx,
1776                     isl_basic_map_compatible_domain(bmap, bset), goto error);
1777
1778         bmap = isl_basic_map_cow(bmap);
1779         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1780                         bset->n_div, bset->n_eq, bset->n_ineq);
1781         if (!bmap)
1782                 goto error;
1783         dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1784         bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1785         bmap = add_constraints(bmap, bmap_domain, 0, 0);
1786
1787         bmap = isl_basic_map_simplify(bmap);
1788         return isl_basic_map_finalize(bmap);
1789 error:
1790         isl_basic_map_free(bmap);
1791         isl_basic_set_free(bset);
1792         return NULL;
1793 }
1794
1795 struct isl_basic_map *isl_basic_map_intersect_range(
1796                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1797 {
1798         struct isl_basic_map *bmap_range;
1799
1800         if (!bmap || !bset)
1801                 goto error;
1802
1803         isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1804                                         bset->dim, isl_dim_param), goto error);
1805
1806         if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1807                 isl_assert(bset->ctx,
1808                     isl_basic_map_compatible_range(bmap, bset), goto error);
1809
1810         bmap = isl_basic_map_cow(bmap);
1811         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1812                         bset->n_div, bset->n_eq, bset->n_ineq);
1813         if (!bmap)
1814                 goto error;
1815         bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1816         bmap = add_constraints(bmap, bmap_range, 0, 0);
1817
1818         bmap = isl_basic_map_simplify(bmap);
1819         return isl_basic_map_finalize(bmap);
1820 error:
1821         isl_basic_map_free(bmap);
1822         isl_basic_set_free(bset);
1823         return NULL;
1824 }
1825
1826 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1827 {
1828         int i;
1829         unsigned total;
1830         isl_int s;
1831
1832         total = 1 + isl_basic_map_total_dim(bmap);
1833         if (total != vec->size)
1834                 return -1;
1835
1836         isl_int_init(s);
1837
1838         for (i = 0; i < bmap->n_eq; ++i) {
1839                 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1840                 if (!isl_int_is_zero(s)) {
1841                         isl_int_clear(s);
1842                         return 0;
1843                 }
1844         }
1845
1846         for (i = 0; i < bmap->n_ineq; ++i) {
1847                 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1848                 if (isl_int_is_neg(s)) {
1849                         isl_int_clear(s);
1850                         return 0;
1851                 }
1852         }
1853
1854         isl_int_clear(s);
1855
1856         return 1;
1857 }
1858
1859 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1860 {
1861         return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
1862 }
1863
1864 struct isl_basic_map *isl_basic_map_intersect(
1865                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1866 {
1867         struct isl_vec *sample = NULL;
1868
1869         if (!bmap1 || !bmap2)
1870                 goto error;
1871
1872         isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1873                                      bmap2->dim, isl_dim_param), goto error);
1874         if (isl_dim_total(bmap1->dim) ==
1875                                 isl_dim_size(bmap1->dim, isl_dim_param) &&
1876             isl_dim_total(bmap2->dim) !=
1877                                 isl_dim_size(bmap2->dim, isl_dim_param))
1878                 return isl_basic_map_intersect(bmap2, bmap1);
1879
1880         if (isl_dim_total(bmap2->dim) !=
1881                                         isl_dim_size(bmap2->dim, isl_dim_param))
1882                 isl_assert(bmap1->ctx,
1883                             isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1884
1885         if (bmap1->sample &&
1886             isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
1887             isl_basic_map_contains(bmap2, bmap1->sample) > 0)
1888                 sample = isl_vec_copy(bmap1->sample);
1889         else if (bmap2->sample &&
1890             isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
1891             isl_basic_map_contains(bmap2, bmap2->sample) > 0)
1892                 sample = isl_vec_copy(bmap2->sample);
1893
1894         bmap1 = isl_basic_map_cow(bmap1);
1895         bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1896                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1897         if (!bmap1)
1898                 goto error;
1899         bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1900
1901         if (sample) {
1902                 isl_vec_free(bmap1->sample);
1903                 bmap1->sample = sample;
1904         }
1905
1906         bmap1 = isl_basic_map_simplify(bmap1);
1907         return isl_basic_map_finalize(bmap1);
1908 error:
1909         if (sample)
1910                 isl_vec_free(sample);
1911         isl_basic_map_free(bmap1);
1912         isl_basic_map_free(bmap2);
1913         return NULL;
1914 }
1915
1916 struct isl_basic_set *isl_basic_set_intersect(
1917                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1918 {
1919         return (struct isl_basic_set *)
1920                 isl_basic_map_intersect(
1921                         (struct isl_basic_map *)bset1,
1922                         (struct isl_basic_map *)bset2);
1923 }
1924
1925 /* Special case of isl_map_intersect, where both map1 and map2
1926  * are convex, without any divs and such that either map1 or map2
1927  * contains a single constraint.  This constraint is then simply
1928  * added to the other map.
1929  */
1930 static __isl_give isl_map *map_intersect_add_constraint(
1931         __isl_take isl_map *map1, __isl_take isl_map *map2)
1932 {
1933         struct isl_basic_map *bmap1;
1934         struct isl_basic_map *bmap2;
1935
1936         isl_assert(map1->ctx, map1->n == 1, goto error);
1937         isl_assert(map2->ctx, map1->n == 1, goto error);
1938         isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
1939         isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
1940
1941         if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
1942                 return isl_map_intersect(map2, map1);
1943
1944         isl_assert(map2->ctx,
1945                     map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
1946
1947         map1 = isl_map_cow(map1);
1948         if (!map1)
1949                 goto error;
1950         if (isl_map_fast_is_empty(map1)) {
1951                 isl_map_free(map2);
1952                 return map1;
1953         }
1954         map1->p[0] = isl_basic_map_cow(map1->p[0]);
1955         if (map2->p[0]->n_eq == 1)
1956                 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
1957         else
1958                 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
1959                                                         map2->p[0]->ineq[0]);
1960
1961         map1->p[0] = isl_basic_map_simplify(map1->p[0]);
1962         map1->p[0] = isl_basic_map_finalize(map1->p[0]);
1963         if (!map1->p[0])
1964                 goto error;
1965
1966         if (isl_basic_map_fast_is_empty(map1->p[0])) {
1967                 isl_basic_map_free(map1->p[0]);
1968                 map1->n = 0;
1969         }
1970
1971         isl_map_free(map2);
1972
1973         return map1;
1974 error:
1975         isl_map_free(map1);
1976         isl_map_free(map2);
1977         return NULL;
1978 }
1979
1980 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1981 {
1982         unsigned flags = 0;
1983         struct isl_map *result;
1984         int i, j;
1985
1986         if (!map1 || !map2)
1987                 goto error;
1988
1989         if (isl_map_fast_is_empty(map1)) {
1990                 isl_map_free(map2);
1991                 return map1;
1992         }
1993         if (isl_map_fast_is_empty(map2)) {
1994                 isl_map_free(map1);
1995                 return map2;
1996         }
1997
1998         if (map1->n == 1 && map2->n == 1 &&
1999             map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2000             isl_dim_equal(map1->dim, map2->dim) &&
2001             (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2002              map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2003                 return map_intersect_add_constraint(map1, map2);
2004         isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
2005                                          map2->dim, isl_dim_param), goto error);
2006         if (isl_dim_total(map1->dim) ==
2007                                 isl_dim_size(map1->dim, isl_dim_param) &&
2008             isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2009                 return isl_map_intersect(map2, map1);
2010
2011         if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
2012                 isl_assert(map1->ctx,
2013                             isl_dim_equal(map1->dim, map2->dim), goto error);
2014
2015         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2016             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2017                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2018
2019         result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2020                                 map1->n * map2->n, flags);
2021         if (!result)
2022                 goto error;
2023         for (i = 0; i < map1->n; ++i)
2024                 for (j = 0; j < map2->n; ++j) {
2025                         struct isl_basic_map *part;
2026                         part = isl_basic_map_intersect(
2027                                     isl_basic_map_copy(map1->p[i]),
2028                                     isl_basic_map_copy(map2->p[j]));
2029                         if (isl_basic_map_is_empty(part))
2030                                 isl_basic_map_free(part);
2031                         else
2032                                 result = isl_map_add_basic_map(result, part);
2033                         if (!result)
2034                                 goto error;
2035                 }
2036         isl_map_free(map1);
2037         isl_map_free(map2);
2038         return result;
2039 error:
2040         isl_map_free(map1);
2041         isl_map_free(map2);
2042         return NULL;
2043 }
2044
2045 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2046 {
2047         return (struct isl_set *)
2048                 isl_map_intersect((struct isl_map *)set1,
2049                                   (struct isl_map *)set2);
2050 }
2051
2052 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2053 {
2054         struct isl_dim *dim;
2055         struct isl_basic_set *bset;
2056         unsigned in;
2057
2058         if (!bmap)
2059                 return NULL;
2060         bmap = isl_basic_map_cow(bmap);
2061         if (!bmap)
2062                 return NULL;
2063         dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
2064         in = isl_basic_map_n_in(bmap);
2065         bset = isl_basic_set_from_basic_map(bmap);
2066         bset = isl_basic_set_swap_vars(bset, in);
2067         return isl_basic_map_from_basic_set(bset, dim);
2068 }
2069
2070 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
2071                 enum isl_dim_type type, unsigned pos, unsigned n)
2072 {
2073         struct isl_dim *res_dim;
2074         struct isl_basic_map *res;
2075         struct isl_dim_map *dim_map;
2076         unsigned total, off;
2077         enum isl_dim_type t;
2078
2079         if (n == 0)
2080                 return bmap;
2081
2082         if (!bmap)
2083                 return NULL;
2084
2085         res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2086
2087         total = isl_basic_map_total_dim(bmap) + n;
2088         dim_map = isl_dim_map_alloc(bmap->ctx, total);
2089         off = 0;
2090         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2091                 if (t != type) {
2092                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
2093                 } else {
2094                         unsigned size = isl_basic_map_dim(bmap, t);
2095                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2096                                                 0, pos, off);
2097                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2098                                                 pos, size - pos, off + pos + n);
2099                 }
2100                 off += isl_dim_size(res_dim, t);
2101         }
2102         isl_dim_map_div(dim_map, bmap, off);
2103
2104         res = isl_basic_map_alloc_dim(res_dim,
2105                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
2106         res = add_constraints_dim_map(res, bmap, dim_map);
2107         res = isl_basic_map_simplify(res);
2108         return isl_basic_map_finalize(res);
2109 }
2110
2111 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2112                 enum isl_dim_type type, unsigned n)
2113 {
2114         if (!bmap)
2115                 return NULL;
2116         return isl_basic_map_insert(bmap, type,
2117                                         isl_basic_map_dim(bmap, type), n);
2118 }
2119
2120 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2121                 enum isl_dim_type type, unsigned n)
2122 {
2123         if (!bset)
2124                 return NULL;
2125         isl_assert(bset->ctx, type != isl_dim_in, goto error);
2126         return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2127 error:
2128         isl_basic_set_free(bset);
2129         return NULL;
2130 }
2131
2132 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2133                 enum isl_dim_type type, unsigned pos, unsigned n)
2134 {
2135         int i;
2136
2137         if (n == 0)
2138                 return map;
2139
2140         map = isl_map_cow(map);
2141         if (!map)
2142                 return NULL;
2143
2144         map->dim = isl_dim_insert(map->dim, type, pos, n);
2145         if (!map->dim)
2146                 goto error;
2147
2148         for (i = 0; i < map->n; ++i) {
2149                 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2150                 if (!map->p[i])
2151                         goto error;
2152         }
2153
2154         return map;
2155 error:
2156         isl_map_free(map);
2157         return NULL;
2158 }
2159
2160 __isl_give isl_map *isl_map_add(__isl_take isl_map *map,
2161                 enum isl_dim_type type, unsigned n)
2162 {
2163         if (!map)
2164                 return NULL;
2165         return isl_map_insert(map, type, isl_map_dim(map, type), n);
2166 }
2167
2168 __isl_give isl_set *isl_set_add(__isl_take isl_set *set,
2169                 enum isl_dim_type type, unsigned n)
2170 {
2171         if (!set)
2172                 return NULL;
2173         isl_assert(set->ctx, type != isl_dim_in, goto error);
2174         return (isl_set *)isl_map_add((isl_map *)set, type, n);
2175 error:
2176         isl_set_free(set);
2177         return NULL;
2178 }
2179
2180 __isl_give isl_basic_map *isl_basic_map_move_dims(
2181         __isl_take isl_basic_map *bmap,
2182         enum isl_dim_type dst_type, unsigned dst_pos,
2183         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2184 {
2185         int i;
2186         struct isl_dim_map *dim_map;
2187         struct isl_basic_map *res;
2188         enum isl_dim_type t;
2189         unsigned total, off;
2190
2191         if (!bmap)
2192                 return NULL;
2193         if (n == 0)
2194                 return bmap;
2195
2196         isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2197                 goto error);
2198
2199         if (dst_type == src_type && dst_pos == src_pos)
2200                 return bmap;
2201
2202         isl_assert(bmap->ctx, dst_type != src_type, goto error);
2203
2204         if (pos(bmap->dim, dst_type) + dst_pos ==
2205             pos(bmap->dim, src_type) + src_pos +
2206                                             ((src_type < dst_type) ? n : 0)) {
2207                 bmap = isl_basic_map_cow(bmap);
2208                 if (!bmap)
2209                         return NULL;
2210
2211                 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2212                                                 src_type, src_pos, n);
2213                 if (!bmap->dim)
2214                         goto error;
2215
2216                 bmap = isl_basic_map_finalize(bmap);
2217
2218                 return bmap;
2219         }
2220
2221         total = isl_basic_map_total_dim(bmap);
2222         dim_map = isl_dim_map_alloc(bmap->ctx, total);
2223
2224         off = 0;
2225         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2226                 unsigned size = isl_dim_size(bmap->dim, t);
2227                 if (t == dst_type) {
2228                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2229                                             0, dst_pos, off);
2230                         off += dst_pos;
2231                         isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2232                                             src_pos, n, off);
2233                         off += n;
2234                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2235                                             dst_pos, size - dst_pos, off);
2236                         off += size - dst_pos;
2237                 } else if (t == src_type) {
2238                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2239                                             0, src_pos, off);
2240                         off += src_pos;
2241                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2242                                         src_pos + n, size - src_pos - n, off);
2243                         off += size - src_pos - n;
2244                 } else {
2245                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
2246                         off += size;
2247                 }
2248         }
2249         isl_dim_map_div(dim_map, bmap, off + n);
2250
2251         res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2252                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
2253         bmap = add_constraints_dim_map(res, bmap, dim_map);
2254
2255         bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2256                                         src_type, src_pos, n);
2257         if (!bmap->dim)
2258                 goto error;
2259
2260         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2261         bmap = isl_basic_map_gauss(bmap, NULL);
2262         bmap = isl_basic_map_finalize(bmap);
2263
2264         return bmap;
2265 error:
2266         isl_basic_map_free(bmap);
2267         return NULL;
2268 }
2269
2270 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
2271         enum isl_dim_type dst_type, unsigned dst_pos,
2272         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2273 {
2274         return (isl_basic_set *)isl_basic_map_move_dims(
2275                 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
2276 }
2277
2278 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
2279         enum isl_dim_type dst_type, unsigned dst_pos,
2280         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2281 {
2282         if (!set)
2283                 return NULL;
2284         isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2285         return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
2286                                         src_type, src_pos, n);
2287 error:
2288         isl_set_free(set);
2289         return NULL;
2290 }
2291
2292 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
2293         enum isl_dim_type dst_type, unsigned dst_pos,
2294         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2295 {
2296         int i;
2297
2298         if (!map)
2299                 return NULL;
2300         if (n == 0)
2301                 return map;
2302
2303         isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2304                 goto error);
2305
2306         if (dst_type == src_type && dst_pos == src_pos)
2307                 return map;
2308
2309         isl_assert(map->ctx, dst_type != src_type, goto error);
2310
2311         map = isl_map_cow(map);
2312         if (!map)
2313                 return NULL;
2314
2315         map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2316         if (!map->dim)
2317                 goto error;
2318
2319         for (i = 0; i < map->n; ++i) {
2320                 map->p[i] = isl_basic_map_move_dims(map->p[i],
2321                                                 dst_type, dst_pos,
2322                                                 src_type, src_pos, n);
2323                 if (!map->p[i])
2324                         goto error;
2325         }
2326
2327         return map;
2328 error:
2329         isl_map_free(map);
2330         return NULL;
2331 }
2332
2333 /* Move the specified dimensions to the last columns right before
2334  * the divs.  Don't change the dimension specification of bmap.
2335  * That's the responsibility of the caller.
2336  */
2337 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2338         enum isl_dim_type type, unsigned first, unsigned n)
2339 {
2340         struct isl_dim_map *dim_map;
2341         struct isl_basic_map *res;
2342         enum isl_dim_type t;
2343         unsigned total, off;
2344
2345         if (!bmap)
2346                 return NULL;
2347         if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2348                 return bmap;
2349
2350         total = isl_basic_map_total_dim(bmap);
2351         dim_map = isl_dim_map_alloc(bmap->ctx, total);
2352
2353         off = 0;
2354         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2355                 unsigned size = isl_dim_size(bmap->dim, t);
2356                 if (t == type) {
2357                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2358                                             0, first, off);
2359                         off += first;
2360                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2361                                             first, n, total - bmap->n_div - n);
2362                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2363                                             first + n, size - (first + n), off);
2364                         off += size - (first + n);
2365                 } else {
2366                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
2367                         off += size;
2368                 }
2369         }
2370         isl_dim_map_div(dim_map, bmap, off + n);
2371
2372         res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2373                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
2374         res = add_constraints_dim_map(res, bmap, dim_map);
2375         return res;
2376 }
2377
2378 /* Turn the n dimensions of type type, starting at first
2379  * into existentially quantified variables.
2380  */
2381 __isl_give isl_basic_map *isl_basic_map_project_out(
2382                 __isl_take isl_basic_map *bmap,
2383                 enum isl_dim_type type, unsigned first, unsigned n)
2384 {
2385         int i;
2386         size_t row_size;
2387         isl_int **new_div;
2388         isl_int *old;
2389
2390         if (n == 0)
2391                 return bmap;
2392
2393         if (!bmap)
2394                 return NULL;
2395
2396         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2397                 return isl_basic_map_remove(bmap, type, first, n);
2398
2399         isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2400                         goto error);
2401
2402         bmap = move_last(bmap, type, first, n);
2403         bmap = isl_basic_map_cow(bmap);
2404
2405         row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2406         old = bmap->block2.data;
2407         bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2408                                         (bmap->extra + n) * (1 + row_size));
2409         if (!bmap->block2.data)
2410                 goto error;
2411         new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
2412         if (!new_div)
2413                 goto error;
2414         for (i = 0; i < n; ++i) {
2415                 new_div[i] = bmap->block2.data +
2416                                 (bmap->extra + i) * (1 + row_size);
2417                 isl_seq_clr(new_div[i], 1 + row_size);
2418         }
2419         for (i = 0; i < bmap->extra; ++i)
2420                 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2421         free(bmap->div);
2422         bmap->div = new_div;
2423         bmap->n_div += n;
2424         bmap->extra += n;
2425
2426         bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2427         if (!bmap->dim)
2428                 goto error;
2429         bmap = isl_basic_map_simplify(bmap);
2430         bmap = isl_basic_map_drop_redundant_divs(bmap);
2431         return isl_basic_map_finalize(bmap);
2432 error:
2433         isl_basic_map_free(bmap);
2434         return NULL;
2435 }
2436
2437 /* Turn the n dimensions of type type, starting at first
2438  * into existentially quantified variables.
2439  */
2440 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2441                 enum isl_dim_type type, unsigned first, unsigned n)
2442 {
2443         return (isl_basic_set *)isl_basic_map_project_out(
2444                         (isl_basic_map *)bset, type, first, n);
2445 }
2446
2447 /* Turn the n dimensions of type type, starting at first
2448  * into existentially quantified variables.
2449  */
2450 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2451                 enum isl_dim_type type, unsigned first, unsigned n)
2452 {
2453         int i;
2454
2455         if (!map)
2456                 return NULL;
2457
2458         if (n == 0)
2459                 return map;
2460
2461         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2462
2463         map = isl_map_cow(map);
2464         if (!map)
2465                 return NULL;
2466
2467         map->dim = isl_dim_drop(map->dim, type, first, n);
2468         if (!map->dim)
2469                 goto error;
2470
2471         for (i = 0; i < map->n; ++i) {
2472                 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2473                 if (!map->p[i])
2474                         goto error;
2475         }
2476
2477         return map;
2478 error:
2479         isl_map_free(map);
2480         return map;
2481 }
2482
2483 /* Turn the n dimensions of type type, starting at first
2484  * into existentially quantified variables.
2485  */
2486 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2487                 enum isl_dim_type type, unsigned first, unsigned n)
2488 {
2489         return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2490 }
2491
2492 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2493 {
2494         int i, j;
2495
2496         for (i = 0; i < n; ++i) {
2497                 j = isl_basic_map_alloc_div(bmap);
2498                 if (j < 0)
2499                         goto error;
2500                 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2501         }
2502         return bmap;
2503 error:
2504         isl_basic_map_free(bmap);
2505         return NULL;
2506 }
2507
2508 struct isl_basic_map *isl_basic_map_apply_range(
2509                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2510 {
2511         struct isl_dim *dim_result = NULL;
2512         struct isl_basic_map *bmap;
2513         unsigned n_in, n_out, n, nparam, total, pos;
2514         struct isl_dim_map *dim_map1, *dim_map2;
2515
2516         if (!bmap1 || !bmap2)
2517                 goto error;
2518
2519         dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2520                                   isl_dim_copy(bmap2->dim));
2521
2522         n_in = isl_basic_map_n_in(bmap1);
2523         n_out = isl_basic_map_n_out(bmap2);
2524         n = isl_basic_map_n_out(bmap1);
2525         nparam = isl_basic_map_n_param(bmap1);
2526
2527         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2528         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2529         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2530         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2531         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2532         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2533         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2534         isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2535         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2536         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2537         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2538
2539         bmap = isl_basic_map_alloc_dim(dim_result,
2540                         bmap1->n_div + bmap2->n_div + n,
2541                         bmap1->n_eq + bmap2->n_eq,
2542                         bmap1->n_ineq + bmap2->n_ineq);
2543         bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2544         bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2545         bmap = add_divs(bmap, n);
2546         bmap = isl_basic_map_simplify(bmap);
2547         bmap = isl_basic_map_drop_redundant_divs(bmap);
2548         return isl_basic_map_finalize(bmap);
2549 error:
2550         isl_basic_map_free(bmap1);
2551         isl_basic_map_free(bmap2);
2552         return NULL;
2553 }
2554
2555 struct isl_basic_set *isl_basic_set_apply(
2556                 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2557 {
2558         if (!bset || !bmap)
2559                 goto error;
2560
2561         isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2562                     goto error);
2563
2564         return (struct isl_basic_set *)
2565                 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2566 error:
2567         isl_basic_set_free(bset);
2568         isl_basic_map_free(bmap);
2569         return NULL;
2570 }
2571
2572 struct isl_basic_map *isl_basic_map_apply_domain(
2573                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2574 {
2575         if (!bmap1 || !bmap2)
2576                 goto error;
2577
2578         isl_assert(bmap1->ctx,
2579             isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2580         isl_assert(bmap1->ctx,
2581             isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2582             goto error);
2583
2584         bmap1 = isl_basic_map_reverse(bmap1);
2585         bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2586         return isl_basic_map_reverse(bmap1);
2587 error:
2588         isl_basic_map_free(bmap1);
2589         isl_basic_map_free(bmap2);
2590         return NULL;
2591 }
2592
2593 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2594  * A \cap B -> f(A) + f(B)
2595  */
2596 struct isl_basic_map *isl_basic_map_sum(
2597                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2598 {
2599         unsigned n_in, n_out, nparam, total, pos;
2600         struct isl_basic_map *bmap = NULL;
2601         struct isl_dim_map *dim_map1, *dim_map2;
2602         int i;
2603
2604         if (!bmap1 || !bmap2)
2605                 goto error;
2606
2607         isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2608                 goto error);
2609
2610         nparam = isl_basic_map_n_param(bmap1);
2611         n_in = isl_basic_map_n_in(bmap1);
2612         n_out = isl_basic_map_n_out(bmap1);
2613
2614         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2615         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2616         dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2617         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2618         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2619         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2620         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2621         isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2622         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2623         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2624         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2625
2626         bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2627                         bmap1->n_div + bmap2->n_div + 2 * n_out,
2628                         bmap1->n_eq + bmap2->n_eq + n_out,
2629                         bmap1->n_ineq + bmap2->n_ineq);
2630         for (i = 0; i < n_out; ++i) {
2631                 int j = isl_basic_map_alloc_equality(bmap);
2632                 if (j < 0)
2633                         goto error;
2634                 isl_seq_clr(bmap->eq[j], 1+total);
2635                 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2636                 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2637                 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2638         }
2639         bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2640         bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2641         bmap = add_divs(bmap, 2 * n_out);
2642
2643         bmap = isl_basic_map_simplify(bmap);
2644         return isl_basic_map_finalize(bmap);
2645 error:
2646         isl_basic_map_free(bmap);
2647         isl_basic_map_free(bmap1);
2648         isl_basic_map_free(bmap2);
2649         return NULL;
2650 }
2651
2652 /* Given two maps A -> f(A) and B -> g(B), construct a map
2653  * A \cap B -> f(A) + f(B)
2654  */
2655 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2656 {
2657         struct isl_map *result;
2658         int i, j;
2659
2660         if (!map1 || !map2)
2661                 goto error;
2662
2663         isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2664
2665         result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2666                                 map1->n * map2->n, 0);
2667         if (!result)
2668                 goto error;
2669         for (i = 0; i < map1->n; ++i)
2670                 for (j = 0; j < map2->n; ++j) {
2671                         struct isl_basic_map *part;
2672                         part = isl_basic_map_sum(
2673                                     isl_basic_map_copy(map1->p[i]),
2674                                     isl_basic_map_copy(map2->p[j]));
2675                         if (isl_basic_map_is_empty(part))
2676                                 isl_basic_map_free(part);
2677                         else
2678                                 result = isl_map_add_basic_map(result, part);
2679                         if (!result)
2680                                 goto error;
2681                 }
2682         isl_map_free(map1);
2683         isl_map_free(map2);
2684         return result;
2685 error:
2686         isl_map_free(map1);
2687         isl_map_free(map2);
2688         return NULL;
2689 }
2690
2691 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
2692         __isl_take isl_set *set2)
2693 {
2694         return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
2695 }
2696
2697 /* Given a basic map A -> f(A), construct A -> -f(A).
2698  */
2699 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2700 {
2701         int i, j;
2702         unsigned off, n;
2703
2704         bmap = isl_basic_map_cow(bmap);
2705         if (!bmap)
2706                 return NULL;
2707
2708         n = isl_basic_map_dim(bmap, isl_dim_out);
2709         off = isl_basic_map_offset(bmap, isl_dim_out);
2710         for (i = 0; i < bmap->n_eq; ++i)
2711                 for (j = 0; j < n; ++j)
2712                         isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2713         for (i = 0; i < bmap->n_ineq; ++i)
2714                 for (j = 0; j < n; ++j)
2715                         isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2716         for (i = 0; i < bmap->n_div; ++i)
2717                 for (j = 0; j < n; ++j)
2718                         isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2719         return isl_basic_map_finalize(bmap);
2720 }
2721
2722 /* Given a map A -> f(A), construct A -> -f(A).
2723  */
2724 struct isl_map *isl_map_neg(struct isl_map *map)
2725 {
2726         int i;
2727
2728         map = isl_map_cow(map);
2729         if (!map)
2730                 return NULL;
2731
2732         for (i = 0; i < map->n; ++i) {
2733                 map->p[i] = isl_basic_map_neg(map->p[i]);
2734                 if (!map->p[i])
2735                         goto error;
2736         }
2737
2738         return map;
2739 error:
2740         isl_map_free(map);
2741         return NULL;
2742 }
2743
2744 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
2745 {
2746         return (isl_set *)isl_map_neg((isl_map *)set);
2747 }
2748
2749 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2750  * A -> floor(f(A)/d).
2751  */
2752 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2753                 isl_int d)
2754 {
2755         unsigned n_in, n_out, nparam, total, pos;
2756         struct isl_basic_map *result = NULL;
2757         struct isl_dim_map *dim_map;
2758         int i;
2759
2760         if (!bmap)
2761                 return NULL;
2762
2763         nparam = isl_basic_map_n_param(bmap);
2764         n_in = isl_basic_map_n_in(bmap);
2765         n_out = isl_basic_map_n_out(bmap);
2766
2767         total = nparam + n_in + n_out + bmap->n_div + n_out;
2768         dim_map = isl_dim_map_alloc(bmap->ctx, total);
2769         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2770         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2771         isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2772         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2773
2774         result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2775                         bmap->n_div + n_out,
2776                         bmap->n_eq, bmap->n_ineq + 2 * n_out);
2777         result = add_constraints_dim_map(result, bmap, dim_map);
2778         result = add_divs(result, n_out);
2779         for (i = 0; i < n_out; ++i) {
2780                 int j;
2781                 j = isl_basic_map_alloc_inequality(result);
2782                 if (j < 0)
2783                         goto error;
2784                 isl_seq_clr(result->ineq[j], 1+total);
2785                 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2786                 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2787                 j = isl_basic_map_alloc_inequality(result);
2788                 if (j < 0)
2789                         goto error;
2790                 isl_seq_clr(result->ineq[j], 1+total);
2791                 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2792                 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2793                 isl_int_sub_ui(result->ineq[j][0], d, 1);
2794         }
2795
2796         result = isl_basic_map_simplify(result);
2797         return isl_basic_map_finalize(result);
2798 error:
2799         isl_basic_map_free(result);
2800         return NULL;
2801 }
2802
2803 /* Given a map A -> f(A) and an integer d, construct a map
2804  * A -> floor(f(A)/d).
2805  */
2806 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2807 {
2808         int i;
2809
2810         map = isl_map_cow(map);
2811         if (!map)
2812                 return NULL;
2813
2814         ISL_F_CLR(map, ISL_MAP_DISJOINT);
2815         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2816         for (i = 0; i < map->n; ++i) {
2817                 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2818                 if (!map->p[i])
2819                         goto error;
2820         }
2821
2822         return map;
2823 error:
2824         isl_map_free(map);
2825         return NULL;
2826 }
2827
2828 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2829 {
2830         int i;
2831         unsigned nparam;
2832         unsigned n_in;
2833
2834         i = isl_basic_map_alloc_equality(bmap);
2835         if (i < 0)
2836                 goto error;
2837         nparam = isl_basic_map_n_param(bmap);
2838         n_in = isl_basic_map_n_in(bmap);
2839         isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2840         isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2841         isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2842         return isl_basic_map_finalize(bmap);
2843 error:
2844         isl_basic_map_free(bmap);
2845         return NULL;
2846 }
2847
2848 /* Add a constraints to "bmap" expressing i_pos < o_pos
2849  */
2850 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2851 {
2852         int i;
2853         unsigned nparam;
2854         unsigned n_in;
2855
2856         i = isl_basic_map_alloc_inequality(bmap);
2857         if (i < 0)
2858                 goto error;
2859         nparam = isl_basic_map_n_param(bmap);
2860         n_in = isl_basic_map_n_in(bmap);
2861         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2862         isl_int_set_si(bmap->ineq[i][0], -1);
2863         isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2864         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2865         return isl_basic_map_finalize(bmap);
2866 error:
2867         isl_basic_map_free(bmap);
2868         return NULL;
2869 }
2870
2871 /* Add a constraint to "bmap" expressing i_pos <= o_pos
2872  */
2873 static __isl_give isl_basic_map *var_less_or_equal(
2874         __isl_take isl_basic_map *bmap, unsigned pos)
2875 {
2876         int i;
2877         unsigned nparam;
2878         unsigned n_in;
2879
2880         i = isl_basic_map_alloc_inequality(bmap);
2881         if (i < 0)
2882                 goto error;
2883         nparam = isl_basic_map_n_param(bmap);
2884         n_in = isl_basic_map_n_in(bmap);
2885         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2886         isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2887         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2888         return isl_basic_map_finalize(bmap);
2889 error:
2890         isl_basic_map_free(bmap);
2891         return NULL;
2892 }
2893
2894 /* Add a constraints to "bmap" expressing i_pos > o_pos
2895  */
2896 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2897 {
2898         int i;
2899         unsigned nparam;
2900         unsigned n_in;
2901
2902         i = isl_basic_map_alloc_inequality(bmap);
2903         if (i < 0)
2904                 goto error;
2905         nparam = isl_basic_map_n_param(bmap);
2906         n_in = isl_basic_map_n_in(bmap);
2907         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2908         isl_int_set_si(bmap->ineq[i][0], -1);
2909         isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2910         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2911         return isl_basic_map_finalize(bmap);
2912 error:
2913         isl_basic_map_free(bmap);
2914         return NULL;
2915 }
2916
2917 /* Add a constraint to "bmap" expressing i_pos >= o_pos
2918  */
2919 static __isl_give isl_basic_map *var_more_or_equal(
2920         __isl_take isl_basic_map *bmap, unsigned pos)
2921 {
2922         int i;
2923         unsigned nparam;
2924         unsigned n_in;
2925
2926         i = isl_basic_map_alloc_inequality(bmap);
2927         if (i < 0)
2928                 goto error;
2929         nparam = isl_basic_map_n_param(bmap);
2930         n_in = isl_basic_map_n_in(bmap);
2931         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2932         isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2933         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2934         return isl_basic_map_finalize(bmap);
2935 error:
2936         isl_basic_map_free(bmap);
2937         return NULL;
2938 }
2939
2940 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2941 {
2942         int i;
2943         struct isl_basic_map *bmap;
2944         bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2945         if (!bmap)
2946                 return NULL;
2947         for (i = 0; i < n_equal && bmap; ++i)
2948                 bmap = var_equal(bmap, i);
2949         return isl_basic_map_finalize(bmap);
2950 }
2951
2952 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
2953  */
2954 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2955 {
2956         int i;
2957         struct isl_basic_map *bmap;
2958         bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2959         if (!bmap)
2960                 return NULL;
2961         for (i = 0; i < pos && bmap; ++i)
2962                 bmap = var_equal(bmap, i);
2963         if (bmap)
2964                 bmap = var_less(bmap, pos);
2965         return isl_basic_map_finalize(bmap);
2966 }
2967
2968 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
2969  */
2970 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
2971         __isl_take isl_dim *dim, unsigned pos)
2972 {
2973         int i;
2974         isl_basic_map *bmap;
2975
2976         bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2977         for (i = 0; i < pos; ++i)
2978                 bmap = var_equal(bmap, i);
2979         bmap = var_less_or_equal(bmap, pos);
2980         return isl_basic_map_finalize(bmap);
2981 }
2982
2983 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2984  */
2985 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2986 {
2987         int i;
2988         struct isl_basic_map *bmap;
2989         bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2990         if (!bmap)
2991                 return NULL;
2992         for (i = 0; i < pos && bmap; ++i)
2993                 bmap = var_equal(bmap, i);
2994         if (bmap)
2995                 bmap = var_more(bmap, pos);
2996         return isl_basic_map_finalize(bmap);
2997 }
2998
2999 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3000  */
3001 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3002         __isl_take isl_dim *dim, unsigned pos)
3003 {
3004         int i;
3005         isl_basic_map *bmap;
3006
3007         bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
3008         for (i = 0; i < pos; ++i)
3009                 bmap = var_equal(bmap, i);
3010         bmap = var_more_or_equal(bmap, pos);
3011         return isl_basic_map_finalize(bmap);
3012 }
3013
3014 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_dim *dims,
3015         unsigned n, int equal)
3016 {
3017         struct isl_map *map;
3018         int i;
3019
3020         map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3021
3022         for (i = 0; i + 1 < n; ++i)
3023                 map = isl_map_add_basic_map(map,
3024                                   isl_basic_map_less_at(isl_dim_copy(dims), i));
3025         if (n > 0) {
3026                 if (equal)
3027                         map = isl_map_add_basic_map(map,
3028                               isl_basic_map_less_or_equal_at(dims, n - 1));
3029                 else
3030                         map = isl_map_add_basic_map(map,
3031                               isl_basic_map_less_at(dims, n - 1));
3032         } else
3033                 isl_dim_free(dims);
3034
3035         return map;
3036 }
3037
3038 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
3039 {
3040         if (!dims)
3041                 return NULL;
3042         return map_lex_lte_first(dims, dims->n_out, equal);
3043 }
3044
3045 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_dim *dim, unsigned n)
3046 {
3047         return map_lex_lte_first(dim, n, 0);
3048 }
3049
3050 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_dim *dim, unsigned n)
3051 {
3052         return map_lex_lte_first(dim, n, 1);
3053 }
3054
3055 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
3056 {
3057         return map_lex_lte(isl_dim_map(set_dim), 0);
3058 }
3059
3060 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
3061 {
3062         return map_lex_lte(isl_dim_map(set_dim), 1);
3063 }
3064
3065 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_dim *dims,
3066         unsigned n, int equal)
3067 {
3068         struct isl_map *map;
3069         int i;
3070
3071         map = isl_map_alloc_dim(isl_dim_copy(dims), n, ISL_MAP_DISJOINT);
3072
3073         for (i = 0; i + 1 < n; ++i)
3074                 map = isl_map_add_basic_map(map,
3075                                   isl_basic_map_more_at(isl_dim_copy(dims), i));
3076         if (n > 0) {
3077                 if (equal)
3078                         map = isl_map_add_basic_map(map,
3079                               isl_basic_map_more_or_equal_at(dims, n - 1));
3080                 else
3081                         map = isl_map_add_basic_map(map,
3082                               isl_basic_map_more_at(dims, n - 1));
3083         } else
3084                 isl_dim_free(dims);
3085
3086         return map;
3087 }
3088
3089 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
3090 {
3091         if (!dims)
3092                 return NULL;
3093         return map_lex_gte_first(dims, dims->n_out, equal);
3094 }
3095
3096 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_dim *dim, unsigned n)
3097 {
3098         return map_lex_gte_first(dim, n, 0);
3099 }
3100
3101 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_dim *dim, unsigned n)
3102 {
3103         return map_lex_gte_first(dim, n, 1);
3104 }
3105
3106 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
3107 {
3108         return map_lex_gte(isl_dim_map(set_dim), 0);
3109 }
3110
3111 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
3112 {
3113         return map_lex_gte(isl_dim_map(set_dim), 1);
3114 }
3115
3116 struct isl_basic_map *isl_basic_map_from_basic_set(
3117                 struct isl_basic_set *bset, struct isl_dim *dim)
3118 {
3119         struct isl_basic_map *bmap;
3120
3121         bset = isl_basic_set_cow(bset);
3122         if (!bset || !dim)
3123                 goto error;
3124
3125         isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
3126         isl_dim_free(bset->dim);
3127         bmap = (struct isl_basic_map *) bset;
3128         bmap->dim = dim;
3129         return isl_basic_map_finalize(bmap);
3130 error:
3131         isl_basic_set_free(bset);
3132         isl_dim_free(dim);
3133         return NULL;
3134 }
3135
3136 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
3137 {
3138         if (!bmap)
3139                 goto error;
3140         if (bmap->dim->n_in == 0)
3141                 return (struct isl_basic_set *)bmap;
3142         bmap = isl_basic_map_cow(bmap);
3143         if (!bmap)
3144                 goto error;
3145         bmap->dim = isl_dim_cow(bmap->dim);
3146         if (!bmap->dim)
3147                 goto error;
3148         bmap->dim->n_out += bmap->dim->n_in;
3149         bmap->dim->n_in = 0;
3150         bmap = isl_basic_map_finalize(bmap);
3151         return (struct isl_basic_set *)bmap;
3152 error:
3153         isl_basic_map_free(bmap);
3154         return NULL;
3155 }
3156
3157 /* For a div d = floor(f/m), add the constraints
3158  *
3159  *              f - m d >= 0
3160  *              -(f-(n-1)) + m d >= 0
3161  *
3162  * Note that the second constraint is the negation of
3163  *
3164  *              f - m d >= n
3165  */
3166 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
3167         unsigned pos, isl_int *div)
3168 {
3169         int i, j;
3170         unsigned total = isl_basic_map_total_dim(bmap);
3171
3172         i = isl_basic_map_alloc_inequality(bmap);
3173         if (i < 0)
3174                 return -1;
3175         isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
3176         isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
3177
3178         j = isl_basic_map_alloc_inequality(bmap);
3179         if (j < 0)
3180                 return -1;
3181         isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
3182         isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
3183         isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
3184         return j;
3185 }
3186
3187 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
3188         unsigned pos, isl_int *div)
3189 {
3190         return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
3191                                                         pos, div);
3192 }
3193
3194 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
3195 {
3196         unsigned total = isl_basic_map_total_dim(bmap);
3197         unsigned div_pos = total - bmap->n_div + div;
3198
3199         return isl_basic_map_add_div_constraints_var(bmap, div_pos,
3200                                                         bmap->div[div]);
3201 }
3202
3203 struct isl_basic_set *isl_basic_map_underlying_set(
3204                 struct isl_basic_map *bmap)
3205 {
3206         if (!bmap)
3207                 goto error;
3208         if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
3209                 return (struct isl_basic_set *)bmap;
3210         bmap = isl_basic_map_cow(bmap);
3211         if (!bmap)
3212                 goto error;
3213         bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
3214         if (!bmap->dim)
3215                 goto error;
3216         bmap->extra -= bmap->n_div;
3217         bmap->n_div = 0;
3218         bmap = isl_basic_map_finalize(bmap);
3219         return (struct isl_basic_set *)bmap;
3220 error:
3221         return NULL;
3222 }
3223
3224 __isl_give isl_basic_set *isl_basic_set_underlying_set(
3225                 __isl_take isl_basic_set *bset)
3226 {
3227         return isl_basic_map_underlying_set((isl_basic_map *)bset);
3228 }
3229
3230 struct isl_basic_map *isl_basic_map_overlying_set(
3231         struct isl_basic_set *bset, struct isl_basic_map *like)
3232 {
3233         struct isl_basic_map *bmap;
3234         struct isl_ctx *ctx;
3235         unsigned total;
3236         int i;
3237
3238         if (!bset || !like)
3239                 goto error;
3240         ctx = bset->ctx;
3241         isl_assert(ctx, bset->n_div == 0, goto error);
3242         isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3243         isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3244                         goto error);
3245         if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3246                 isl_basic_map_free(like);
3247                 return (struct isl_basic_map *)bset;
3248         }
3249         bset = isl_basic_set_cow(bset);
3250         if (!bset)
3251                 goto error;
3252         total = bset->dim->n_out + bset->extra;
3253         bmap = (struct isl_basic_map *)bset;
3254         isl_dim_free(bmap->dim);
3255         bmap->dim = isl_dim_copy(like->dim);
3256         if (!bmap->dim)
3257                 goto error;
3258         bmap->n_div = like->n_div;
3259         bmap->extra += like->n_div;
3260         if (bmap->extra) {
3261                 unsigned ltotal;
3262                 ltotal = total - bmap->extra + like->extra;
3263                 if (ltotal > total)
3264                         ltotal = total;
3265                 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3266                                         bmap->extra * (1 + 1 + total));
3267                 if (isl_blk_is_error(bmap->block2))
3268                         goto error;
3269                 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
3270                                                 bmap->extra);
3271                 if (!bmap->div)
3272                         goto error;
3273                 for (i = 0; i < bmap->extra; ++i)
3274                         bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3275                 for (i = 0; i < like->n_div; ++i) {
3276                         isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3277                         isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3278                 }
3279                 bmap = isl_basic_map_extend_constraints(bmap, 
3280                                                         0, 2 * like->n_div);
3281                 for (i = 0; i < like->n_div; ++i) {
3282                         if (isl_int_is_zero(bmap->div[i][0]))
3283                                 continue;
3284                         if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3285                                 goto error;
3286                 }
3287         }
3288         isl_basic_map_free(like);
3289         bmap = isl_basic_map_simplify(bmap);
3290         bmap = isl_basic_map_finalize(bmap);
3291         return bmap;
3292 error:
3293         isl_basic_map_free(like);
3294         isl_basic_set_free(bset);
3295         return NULL;
3296 }
3297
3298 struct isl_basic_set *isl_basic_set_from_underlying_set(
3299         struct isl_basic_set *bset, struct isl_basic_set *like)
3300 {
3301         return (struct isl_basic_set *)
3302                 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3303 }
3304
3305 struct isl_set *isl_set_from_underlying_set(
3306         struct isl_set *set, struct isl_basic_set *like)
3307 {
3308         int i;
3309
3310         if (!set || !like)
3311                 goto error;
3312         isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3313                     goto error);
3314         if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3315                 isl_basic_set_free(like);
3316                 return set;
3317         }
3318         set = isl_set_cow(set);
3319         if (!set)
3320                 goto error;
3321         for (i = 0; i < set->n; ++i) {
3322                 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3323                                                       isl_basic_set_copy(like));
3324                 if (!set->p[i])
3325                         goto error;
3326         }
3327         isl_dim_free(set->dim);
3328         set->dim = isl_dim_copy(like->dim);
3329         if (!set->dim)
3330                 goto error;
3331         isl_basic_set_free(like);
3332         return set;
3333 error:
3334         isl_basic_set_free(like);
3335         isl_set_free(set);
3336         return NULL;
3337 }
3338
3339 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3340 {
3341         int i;
3342
3343         map = isl_map_cow(map);
3344         if (!map)
3345                 return NULL;
3346         map->dim = isl_dim_cow(map->dim);
3347         if (!map->dim)
3348                 goto error;
3349
3350         for (i = 1; i < map->n; ++i)
3351                 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3352                                 goto error);
3353         for (i = 0; i < map->n; ++i) {
3354                 map->p[i] = (struct isl_basic_map *)
3355                                 isl_basic_map_underlying_set(map->p[i]);
3356                 if (!map->p[i])
3357                         goto error;
3358         }
3359         if (map->n == 0)
3360                 map->dim = isl_dim_underlying(map->dim, 0);
3361         else {
3362                 isl_dim_free(map->dim);
3363                 map->dim = isl_dim_copy(map->p[0]->dim);
3364         }
3365         if (!map->dim)
3366                 goto error;
3367         return (struct isl_set *)map;
3368 error:
3369         isl_map_free(map);
3370         return NULL;
3371 }
3372
3373 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3374 {
3375         return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3376 }
3377
3378 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3379 {
3380         struct isl_basic_set *domain;
3381         unsigned n_in;
3382         unsigned n_out;
3383         if (!bmap)
3384                 return NULL;
3385         n_in = isl_basic_map_n_in(bmap);
3386         n_out = isl_basic_map_n_out(bmap);
3387         domain = isl_basic_set_from_basic_map(bmap);
3388         return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3389 }
3390
3391 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3392 {
3393         return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3394 }
3395
3396 struct isl_set *isl_map_range(struct isl_map *map)
3397 {
3398         int i;
3399         struct isl_set *set;
3400
3401         if (!map)
3402                 goto error;
3403         if (isl_map_dim(map, isl_dim_in) == 0)
3404                 return (isl_set *)map;
3405
3406         map = isl_map_cow(map);
3407         if (!map)
3408                 goto error;
3409
3410         set = (struct isl_set *) map;
3411         set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3412         if (!set->dim)
3413                 goto error;
3414         for (i = 0; i < map->n; ++i) {
3415                 set->p[i] = isl_basic_map_range(map->p[i]);
3416                 if (!set->p[i])
3417                         goto error;
3418         }
3419         ISL_F_CLR(set, ISL_MAP_DISJOINT);
3420         ISL_F_CLR(set, ISL_SET_NORMALIZED);
3421         return set;
3422 error:
3423         isl_map_free(map);
3424         return NULL;
3425 }
3426
3427 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3428 {
3429         int i;
3430         struct isl_map *map = NULL;
3431
3432         set = isl_set_cow(set);
3433         if (!set || !dim)
3434                 goto error;
3435         isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3436         map = (struct isl_map *)set;
3437         for (i = 0; i < set->n; ++i) {
3438                 map->p[i] = isl_basic_map_from_basic_set(
3439                                 set->p[i], isl_dim_copy(dim));
3440                 if (!map->p[i])
3441                         goto error;
3442         }
3443         isl_dim_free(map->dim);
3444         map->dim = dim;
3445         return map;
3446 error:
3447         isl_dim_free(dim);
3448         isl_set_free(set);
3449         return NULL;
3450 }
3451
3452 struct isl_map *isl_map_from_range(struct isl_set *set)
3453 {
3454         return (struct isl_map *)set;
3455 }
3456
3457 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
3458 {
3459         return isl_map_reverse(isl_map_from_range(set));;
3460 }
3461
3462 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
3463         __isl_take isl_set *range)
3464 {
3465         return isl_map_product(isl_map_from_domain(domain),
3466                                isl_map_from_range(range));
3467 }
3468
3469 struct isl_set *isl_set_from_map(struct isl_map *map)
3470 {
3471         int i;
3472         struct isl_set *set = NULL;
3473
3474         if (!map)
3475                 return NULL;
3476         map = isl_map_cow(map);
3477         if (!map)
3478                 return NULL;
3479         map->dim = isl_dim_cow(map->dim);
3480         if (!map->dim)
3481                 goto error;
3482         map->dim->n_out += map->dim->n_in;
3483         map->dim->n_in = 0;
3484         set = (struct isl_set *)map;
3485         for (i = 0; i < map->n; ++i) {
3486                 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
3487                 if (!set->p[i])
3488                         goto error;
3489         }
3490         return set;
3491 error:
3492         isl_map_free(map);
3493         return NULL;
3494 }
3495
3496 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
3497 {
3498         struct isl_map *map;
3499
3500         if (!dim)
3501                 return NULL;
3502         isl_assert(dim->ctx, n >= 0, return NULL);
3503         map = isl_alloc(dim->ctx, struct isl_map,
3504                         sizeof(struct isl_map) +
3505                         (n - 1) * sizeof(struct isl_basic_map *));
3506         if (!map)
3507                 goto error;
3508
3509         map->ctx = dim->ctx;
3510         isl_ctx_ref(map->ctx);
3511         map->ref = 1;
3512         map->size = n;
3513         map->n = 0;
3514         map->dim = dim;
3515         map->flags = flags;
3516         return map;
3517 error:
3518         isl_dim_free(dim);
3519         return NULL;
3520 }
3521
3522 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
3523                 unsigned nparam, unsigned in, unsigned out, int n,
3524                 unsigned flags)
3525 {
3526         struct isl_map *map;
3527         struct isl_dim *dims;
3528
3529         dims = isl_dim_alloc(ctx, nparam, in, out);
3530         if (!dims)
3531                 return NULL;
3532
3533         map = isl_map_alloc_dim(dims, n, flags);
3534         return map;
3535 }
3536
3537 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
3538 {
3539         struct isl_basic_map *bmap;
3540         bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
3541         bmap = isl_basic_map_set_to_empty(bmap);
3542         return bmap;
3543 }
3544
3545 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
3546 {
3547         struct isl_basic_set *bset;
3548         bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
3549         bset = isl_basic_set_set_to_empty(bset);
3550         return bset;
3551 }
3552
3553 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
3554 {
3555         struct isl_basic_map *bmap;
3556         if (!model)
3557                 return NULL;
3558         bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3559         bmap = isl_basic_map_set_to_empty(bmap);
3560         return bmap;
3561 }
3562
3563 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
3564 {
3565         struct isl_basic_map *bmap;
3566         if (!model)
3567                 return NULL;
3568         bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3569         bmap = isl_basic_map_set_to_empty(bmap);
3570         return bmap;
3571 }
3572
3573 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
3574 {
3575         struct isl_basic_set *bset;
3576         if (!model)
3577                 return NULL;
3578         bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3579         bset = isl_basic_set_set_to_empty(bset);
3580         return bset;
3581 }
3582
3583 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
3584 {
3585         struct isl_basic_map *bmap;
3586         bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
3587         return bmap;
3588 }
3589
3590 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
3591 {
3592         struct isl_basic_set *bset;
3593         bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
3594         return bset;
3595 }
3596
3597 __isl_give isl_basic_map *isl_basic_map_universe_like(
3598                 __isl_keep isl_basic_map *model)
3599 {
3600         if (!model)
3601                 return NULL;
3602         return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3603 }
3604
3605 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
3606 {
3607         if (!model)
3608                 return NULL;
3609         return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3610 }
3611
3612 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
3613         __isl_keep isl_set *model)
3614 {
3615         if (!model)
3616                 return NULL;
3617         return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3618 }
3619
3620 struct isl_map *isl_map_empty(struct isl_dim *dim)
3621 {
3622         return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3623 }
3624
3625 struct isl_map *isl_map_empty_like(struct isl_map *model)
3626 {
3627         if (!model)
3628                 return NULL;
3629         return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3630 }
3631
3632 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
3633 {
3634         if (!model)
3635                 return NULL;
3636         return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3637 }
3638
3639 struct isl_set *isl_set_empty(struct isl_dim *dim)
3640 {
3641         return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3642 }
3643
3644 struct isl_set *isl_set_empty_like(struct isl_set *model)
3645 {
3646         if (!model)
3647                 return NULL;
3648         return isl_set_empty(isl_dim_copy(model->dim));
3649 }
3650
3651 struct isl_map *isl_map_universe(struct isl_dim *dim)
3652 {
3653         struct isl_map *map;
3654         if (!dim)
3655                 return NULL;
3656         map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3657         map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
3658         return map;
3659 }
3660
3661 struct isl_set *isl_set_universe(struct isl_dim *dim)
3662 {
3663         struct isl_set *set;
3664         if (!dim)
3665                 return NULL;
3666         set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3667         set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
3668         return set;
3669 }
3670
3671 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
3672 {
3673         if (!model)
3674                 return NULL;
3675         return isl_set_universe(isl_dim_copy(model->dim));
3676 }
3677
3678 struct isl_map *isl_map_dup(struct isl_map *map)
3679 {
3680         int i;
3681         struct isl_map *dup;
3682
3683         if (!map)
3684                 return NULL;
3685         dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
3686         for (i = 0; i < map->n; ++i)
3687                 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
3688         return dup;
3689 }
3690
3691 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
3692                                                 __isl_take isl_basic_map *bmap)
3693 {
3694         if (!bmap || !map)
3695                 goto error;
3696         if (isl_basic_map_fast_is_empty(bmap)) {
3697                 isl_basic_map_free(bmap);
3698                 return map;
3699         }
3700         isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
3701         isl_assert(map->ctx, map->n < map->size, goto error);
3702         map->p[map->n] = bmap;
3703         map->n++;
3704         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3705         return map;
3706 error:
3707         if (map)
3708                 isl_map_free(map);
3709         if (bmap)
3710                 isl_basic_map_free(bmap);
3711         return NULL;
3712 }
3713
3714 void isl_map_free(struct isl_map *map)
3715 {
3716         int i;
3717
3718         if (!map)
3719                 return;
3720
3721         if (--map->ref > 0)
3722                 return;
3723
3724         isl_ctx_deref(map->ctx);
3725         for (i = 0; i < map->n; ++i)
3726                 isl_basic_map_free(map->p[i]);
3727         isl_dim_free(map->dim);
3728         free(map);
3729 }
3730
3731 struct isl_map *isl_map_extend(struct isl_map *base,
3732                 unsigned nparam, unsigned n_in, unsigned n_out)
3733 {
3734         int i;
3735
3736         base = isl_map_cow(base);
3737         if (!base)
3738                 return NULL;
3739
3740         base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
3741         if (!base->dim)
3742                 goto error;
3743         for (i = 0; i < base->n; ++i) {
3744                 base->p[i] = isl_basic_map_extend_dim(base->p[i],
3745                                 isl_dim_copy(base->dim), 0, 0, 0);
3746                 if (!base->p[i])
3747                         goto error;
3748         }
3749         return base;
3750 error:
3751         isl_map_free(base);
3752         return NULL;
3753 }
3754
3755 struct isl_set *isl_set_extend(struct isl_set *base,
3756                 unsigned nparam, unsigned dim)
3757 {
3758         return (struct isl_set *)isl_map_extend((struct isl_map *)base,
3759                                                         nparam, 0, dim);
3760 }
3761
3762 static struct isl_basic_map *isl_basic_map_fix_pos_si(
3763         struct isl_basic_map *bmap, unsigned pos, int value)
3764 {
3765         int j;
3766
3767         bmap = isl_basic_map_cow(bmap);
3768         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3769         j = isl_basic_map_alloc_equality(bmap);
3770         if (j < 0)
3771                 goto error;
3772         isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3773         isl_int_set_si(bmap->eq[j][pos], -1);
3774         isl_int_set_si(bmap->eq[j][0], value);
3775         bmap = isl_basic_map_simplify(bmap);
3776         return isl_basic_map_finalize(bmap);
3777 error:
3778         isl_basic_map_free(bmap);
3779         return NULL;
3780 }
3781
3782 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
3783         __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
3784 {
3785         int j;
3786
3787         bmap = isl_basic_map_cow(bmap);
3788         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3789         j = isl_basic_map_alloc_equality(bmap);
3790         if (j < 0)
3791                 goto error;
3792         isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3793         isl_int_set_si(bmap->eq[j][pos], -1);
3794         isl_int_set(bmap->eq[j][0], value);
3795         bmap = isl_basic_map_simplify(bmap);
3796         return isl_basic_map_finalize(bmap);
3797 error:
3798         isl_basic_map_free(bmap);
3799         return NULL;
3800 }
3801
3802 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
3803                 enum isl_dim_type type, unsigned pos, int value)
3804 {
3805         if (!bmap)
3806                 return NULL;
3807         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3808         return isl_basic_map_fix_pos_si(bmap,
3809                 isl_basic_map_offset(bmap, type) + pos, value);
3810 error:
3811         isl_basic_map_free(bmap);
3812         return NULL;
3813 }
3814
3815 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
3816                 enum isl_dim_type type, unsigned pos, isl_int value)
3817 {
3818         if (!bmap)
3819                 return NULL;
3820         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3821         return isl_basic_map_fix_pos(bmap,
3822                 isl_basic_map_offset(bmap, type) + pos, value);
3823 error:
3824         isl_basic_map_free(bmap);
3825         return NULL;
3826 }
3827
3828 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
3829                 enum isl_dim_type type, unsigned pos, int value)
3830 {
3831         return (struct isl_basic_set *)
3832                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3833                                         type, pos, value);
3834 }
3835
3836 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
3837                 enum isl_dim_type type, unsigned pos, isl_int value)
3838 {
3839         return (struct isl_basic_set *)
3840                 isl_basic_map_fix((struct isl_basic_map *)bset,
3841                                         type, pos, value);
3842 }
3843
3844 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
3845                 unsigned input, int value)
3846 {
3847         return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
3848 }
3849
3850 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
3851                 unsigned dim, int value)
3852 {
3853         return (struct isl_basic_set *)
3854                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3855                                         isl_dim_set, dim, value);
3856 }
3857
3858 struct isl_map *isl_map_fix_si(struct isl_map *map,
3859                 enum isl_dim_type type, unsigned pos, int value)
3860 {
3861         int i;
3862
3863         map = isl_map_cow(map);
3864         if (!map)
3865                 return NULL;
3866
3867         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3868         for (i = 0; i < map->n; ++i) {
3869                 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
3870                 if (!map->p[i])
3871                         goto error;
3872         }
3873         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3874         return map;
3875 error:
3876         isl_map_free(map);
3877         return NULL;
3878 }
3879
3880 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
3881                 enum isl_dim_type type, unsigned pos, int value)
3882 {
3883         return (struct isl_set *)
3884                 isl_map_fix_si((struct isl_map *)set, type, pos, value);
3885 }
3886
3887 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
3888                 enum isl_dim_type type, unsigned pos, isl_int value)
3889 {
3890         int i;
3891
3892         map = isl_map_cow(map);
3893         if (!map)
3894                 return NULL;
3895
3896         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3897         for (i = 0; i < map->n; ++i) {
3898                 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
3899                 if (!map->p[i])
3900                         goto error;
3901         }
3902         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3903         return map;
3904 error:
3905         isl_map_free(map);
3906         return NULL;
3907 }
3908
3909 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
3910                 enum isl_dim_type type, unsigned pos, isl_int value)
3911 {
3912         return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
3913 }
3914
3915 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
3916                 unsigned input, int value)
3917 {
3918         return isl_map_fix_si(map, isl_dim_in, input, value);
3919 }
3920
3921 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
3922 {
3923         return (struct isl_set *)
3924                 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
3925 }
3926
3927 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
3928                 __isl_take isl_basic_map *bmap,
3929                 enum isl_dim_type type, unsigned pos, int value)
3930 {
3931         int j;
3932
3933         if (!bmap)
3934                 return NULL;
3935         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3936         pos += isl_basic_map_offset(bmap, type);
3937         bmap = isl_basic_map_cow(bmap);
3938         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
3939         j = isl_basic_map_alloc_inequality(bmap);
3940         if (j < 0)
3941                 goto error;
3942         isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
3943         isl_int_set_si(bmap->ineq[j][pos], 1);
3944         isl_int_set_si(bmap->ineq[j][0], -value);
3945         bmap = isl_basic_map_simplify(bmap);
3946         return isl_basic_map_finalize(bmap);
3947 error:
3948         isl_basic_map_free(bmap);
3949         return NULL;
3950 }
3951
3952 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
3953         unsigned dim, isl_int value)
3954 {
3955         int j;
3956
3957         bset = isl_basic_set_cow(bset);
3958         bset = isl_basic_set_extend_constraints(bset, 0, 1);
3959         j = isl_basic_set_alloc_inequality(bset);
3960         if (j < 0)
3961                 goto error;
3962         isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
3963         isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
3964         isl_int_neg(bset->ineq[j][0], value);
3965         bset = isl_basic_set_simplify(bset);
3966         return isl_basic_set_finalize(bset);
3967 error:
3968         isl_basic_set_free(bset);
3969         return NULL;
3970 }
3971
3972 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
3973                 enum isl_dim_type type, unsigned pos, int value)
3974 {
3975         int i;
3976
3977         map = isl_map_cow(map);
3978         if (!map)
3979                 return NULL;
3980
3981         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3982         for (i = 0; i < map->n; ++i) {
3983                 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
3984                                                          type, pos, value);
3985                 if (!map->p[i])
3986                         goto error;
3987         }
3988         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3989         return map;
3990 error:
3991         isl_map_free(map);
3992         return NULL;
3993 }
3994
3995 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
3996                 enum isl_dim_type type, unsigned pos, int value)
3997 {
3998         return (struct isl_set *)
3999                 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
4000 }
4001
4002 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
4003                                         isl_int value)
4004 {
4005         int i;
4006
4007         set = isl_set_cow(set);
4008         if (!set)
4009                 return NULL;
4010
4011         isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
4012         for (i = 0; i < set->n; ++i) {
4013                 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
4014                 if (!set->p[i])
4015                         goto error;
4016         }
4017         return set;
4018 error:
4019         isl_set_free(set);
4020         return NULL;
4021 }
4022
4023 struct isl_map *isl_map_reverse(struct isl_map *map)
4024 {
4025         int i;
4026
4027         map = isl_map_cow(map);
4028         if (!map)
4029                 return NULL;
4030
4031         map->dim = isl_dim_reverse(map->dim);
4032         if (!map->dim)
4033                 goto error;
4034         for (i = 0; i < map->n; ++i) {
4035                 map->p[i] = isl_basic_map_reverse(map->p[i]);
4036                 if (!map->p[i])
4037                         goto error;
4038         }
4039         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4040         return map;
4041 error:
4042         isl_map_free(map);
4043         return NULL;
4044 }
4045
4046 static struct isl_map *isl_basic_map_partial_lexopt(
4047                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4048                 struct isl_set **empty, int max)
4049 {
4050         if (!bmap)
4051                 goto error;
4052         if (bmap->ctx->opt->pip == ISL_PIP_PIP)
4053                 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
4054         else
4055                 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
4056 error:
4057         isl_basic_map_free(bmap);
4058         isl_basic_set_free(dom);
4059         if (empty)
4060                 *empty = NULL;
4061         return NULL;
4062 }
4063
4064 struct isl_map *isl_basic_map_partial_lexmax(
4065                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4066                 struct isl_set **empty)
4067 {
4068         return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
4069 }
4070
4071 struct isl_map *isl_basic_map_partial_lexmin(
4072                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
4073                 struct isl_set **empty)
4074 {
4075         return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
4076 }
4077
4078 struct isl_set *isl_basic_set_partial_lexmin(
4079                 struct isl_basic_set *bset, struct isl_basic_set *dom,
4080                 struct isl_set **empty)
4081 {
4082         return (struct isl_set *)
4083                 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
4084                         dom, empty);
4085 }
4086
4087 struct isl_set *isl_basic_set_partial_lexmax(
4088                 struct isl_basic_set *bset, struct isl_basic_set *dom,
4089                 struct isl_set **empty)
4090 {
4091         return (struct isl_set *)
4092                 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
4093                         dom, empty);
4094 }
4095
4096 /* Given a basic map "bmap", compute the lexicograhically minimal
4097  * (or maximal) image element for each domain element in dom.
4098  * Set *empty to those elements in dom that do not have an image element.
4099  *
4100  * We first make sure the basic sets in dom are disjoint and then
4101  * simply collect the results over each of the basic sets separately.
4102  * We could probably improve the efficiency a bit by moving the union
4103  * domain down into the parametric integer programming.
4104  */
4105 static __isl_give isl_map *basic_map_partial_lexopt(
4106                 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
4107                 __isl_give isl_set **empty, int max)
4108 {
4109         int i;
4110         struct isl_map *res;
4111
4112         dom = isl_set_make_disjoint(dom);
4113         if (!dom)
4114                 goto error;
4115
4116         if (isl_set_fast_is_empty(dom)) {
4117                 res = isl_map_empty_like_basic_map(bmap);
4118                 *empty = isl_set_empty_like(dom);
4119                 isl_set_free(dom);
4120                 isl_basic_map_free(bmap);
4121                 return res;
4122         }
4123
4124         res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4125                         isl_basic_set_copy(dom->p[0]), empty, max);
4126                 
4127         for (i = 1; i < dom->n; ++i) {
4128                 struct isl_map *res_i;
4129                 struct isl_set *empty_i;
4130
4131                 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
4132                                 isl_basic_set_copy(dom->p[i]), &empty_i, max);
4133
4134                 res = isl_map_union_disjoint(res, res_i);
4135                 *empty = isl_set_union_disjoint(*empty, empty_i);
4136         }
4137
4138         isl_set_free(dom);
4139         isl_basic_map_free(bmap);
4140         return res;
4141 error:
4142         *empty = NULL;
4143         isl_set_free(dom);
4144         isl_basic_map_free(bmap);
4145         return NULL;
4146 }
4147
4148 /* Given a map "map", compute the lexicograhically minimal
4149  * (or maximal) image element for each domain element in dom.
4150  * Set *empty to those elements in dom that do not have an image element.
4151  *
4152  * We first compute the lexicographically minimal or maximal element
4153  * in the first basic map.  This results in a partial solution "res"
4154  * and a subset "todo" of dom that still need to be handled.
4155  * We then consider each of the remaining maps in "map" and successively
4156  * improve both "res" and "todo".
4157  *
4158  * Let res^k and todo^k be the results after k steps and let i = k + 1.
4159  * Assume we are computing the lexicographical maximum.
4160  * We first intersect basic map i with a relation that maps elements
4161  * to elements that are lexicographically larger than the image elements
4162  * in res^k and the compute the maximum image element of this intersection.
4163  * The result ("better") corresponds to those image elements in basic map i
4164  * that are better than what we had before.  The remainder ("keep") are the
4165  * domain elements for which the image element in res_k was better.
4166  * We also compute the lexicographical maximum of basic map i in todo^k.
4167  * res^i is the result of the operation + better + those elements in
4168  *              res^k that we should keep
4169  * todo^i is the remainder of the maximum operation on todo^k.
4170  */
4171 static __isl_give isl_map *isl_map_partial_lexopt(
4172                 __isl_take isl_map *map, __isl_take isl_set *dom,
4173                 __isl_give isl_set **empty, int max)
4174 {
4175         int i;
4176         struct isl_map *res;
4177         struct isl_set *todo;
4178
4179         if (!map || !dom)
4180                 goto error;
4181
4182         if (isl_map_fast_is_empty(map)) {
4183                 if (empty)
4184                         *empty = dom;
4185                 else
4186                         isl_set_free(dom);
4187                 return map;
4188         }
4189
4190         res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
4191                                         isl_set_copy(dom), &todo, max);
4192
4193         for (i = 1; i < map->n; ++i) {
4194                 struct isl_map *lt;
4195                 struct isl_map *better;
4196                 struct isl_set *keep;
4197                 struct isl_map *res_i;
4198                 struct isl_set *todo_i;
4199                 struct isl_dim *dim = isl_map_get_dim(res);
4200
4201                 dim = isl_dim_range(dim);
4202                 if (max)
4203                         lt = isl_map_lex_lt(dim);
4204                 else
4205                         lt = isl_map_lex_gt(dim);
4206                 lt = isl_map_apply_range(isl_map_copy(res), lt);
4207                 lt = isl_map_intersect(lt,
4208                         isl_map_from_basic_map(isl_basic_map_copy(map->p[i])));
4209                 better = isl_map_partial_lexopt(lt,
4210                         isl_map_domain(isl_map_copy(res)),
4211                         &keep, max);
4212
4213                 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
4214                                                 todo, &todo_i, max);
4215
4216                 res = isl_map_intersect_domain(res, keep);
4217                 res = isl_map_union_disjoint(res, res_i);
4218                 res = isl_map_union_disjoint(res, better);
4219                 todo = todo_i;
4220         }
4221
4222         isl_set_free(dom);
4223         isl_map_free(map);
4224
4225         if (empty)
4226                 *empty = todo;
4227         else
4228                 isl_set_free(todo);
4229
4230         return res;
4231 error:
4232         if (empty)
4233                 *empty = NULL;
4234         isl_set_free(dom);
4235         isl_map_free(map);
4236         return NULL;
4237 }
4238
4239 __isl_give isl_map *isl_map_partial_lexmax(
4240                 __isl_take isl_map *map, __isl_take isl_set *dom,
4241                 __isl_give isl_set **empty)
4242 {
4243         return isl_map_partial_lexopt(map, dom, empty, 1);
4244 }
4245
4246 __isl_give isl_map *isl_map_partial_lexmin(
4247                 __isl_take isl_map *map, __isl_take isl_set *dom,
4248                 __isl_give isl_set **empty)
4249 {
4250         return isl_map_partial_lexopt(map, dom, empty, 0);
4251 }
4252
4253 __isl_give isl_set *isl_set_partial_lexmin(
4254                 __isl_take isl_set *set, __isl_take isl_set *dom,
4255                 __isl_give isl_set **empty)
4256 {
4257         return (struct isl_set *)
4258                 isl_map_partial_lexmin((struct isl_map *)set,
4259                         dom, empty);
4260 }
4261
4262 __isl_give isl_set *isl_set_partial_lexmax(
4263                 __isl_take isl_set *set, __isl_take isl_set *dom,
4264                 __isl_give isl_set **empty)
4265 {
4266         return (struct isl_set *)
4267                 isl_map_partial_lexmax((struct isl_map *)set,
4268                         dom, empty);
4269 }
4270
4271 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4272 {
4273         struct isl_basic_set *dom = NULL;
4274         struct isl_dim *dom_dim;
4275
4276         if (!bmap)
4277                 goto error;
4278         dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4279         dom = isl_basic_set_universe(dom_dim);
4280         return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4281 error:
4282         isl_basic_map_free(bmap);
4283         return NULL;
4284 }
4285
4286 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4287 {
4288         return isl_basic_map_lexopt(bmap, 0);
4289 }
4290
4291 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4292 {
4293         return isl_basic_map_lexopt(bmap, 1);
4294 }
4295
4296 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4297 {
4298         return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4299 }
4300
4301 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4302 {
4303         return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4304 }
4305
4306 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4307 {
4308         struct isl_set *dom = NULL;
4309         struct isl_dim *dom_dim;
4310
4311         if (!map)
4312                 goto error;
4313         dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4314         dom = isl_set_universe(dom_dim);
4315         return isl_map_partial_lexopt(map, dom, NULL, max);
4316 error:
4317         isl_map_free(map);
4318         return NULL;
4319 }
4320
4321 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4322 {
4323         return isl_map_lexopt(map, 0);
4324 }
4325
4326 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
4327 {
4328         return isl_map_lexopt(map, 1);
4329 }
4330
4331 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
4332 {
4333         return (isl_set *)isl_map_lexmin((isl_map *)set);
4334 }
4335
4336 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
4337 {
4338         return (isl_set *)isl_map_lexmax((isl_map *)set);
4339 }
4340
4341 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
4342         struct isl_dim *dim)
4343 {
4344         int i;
4345
4346         if (!map || !dim)
4347                 goto error;
4348
4349         for (i = 0; i < map->n; ++i) {
4350                 isl_dim_free(map->p[i]->dim);
4351                 map->p[i]->dim = isl_dim_copy(dim);
4352         }
4353         isl_dim_free(map->dim);
4354         map->dim = dim;
4355
4356         return map;
4357 error:
4358         isl_map_free(map);
4359         isl_dim_free(dim);
4360         return NULL;
4361 }
4362
4363 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
4364         struct isl_dim *dim)
4365 {
4366         return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
4367 }
4368
4369 /* Apply a preimage specified by "mat" on the parameters of "bset".
4370  * bset is assumed to have only parameters and divs.
4371  */
4372 static struct isl_basic_set *basic_set_parameter_preimage(
4373         struct isl_basic_set *bset, struct isl_mat *mat)
4374 {
4375         unsigned nparam;
4376
4377         if (!bset || !mat)
4378                 goto error;
4379
4380         bset->dim = isl_dim_cow(bset->dim);
4381         if (!bset->dim)
4382                 goto error;
4383
4384         nparam = isl_basic_set_dim(bset, isl_dim_param);
4385
4386         isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
4387
4388         bset->dim->nparam = 0;
4389         bset->dim->n_out = nparam;
4390         bset = isl_basic_set_preimage(bset, mat);
4391         if (bset) {
4392                 bset->dim->nparam = bset->dim->n_out;
4393                 bset->dim->n_out = 0;
4394         }
4395         return bset;
4396 error:
4397         isl_mat_free(mat);
4398         isl_basic_set_free(bset);
4399         return NULL;
4400 }
4401
4402 /* Apply a preimage specified by "mat" on the parameters of "set".
4403  * set is assumed to have only parameters and divs.
4404  */
4405 static struct isl_set *set_parameter_preimage(
4406         struct isl_set *set, struct isl_mat *mat)
4407 {
4408         struct isl_dim *dim = NULL;
4409         unsigned nparam;
4410
4411         if (!set || !mat)
4412                 goto error;
4413
4414         dim = isl_dim_copy(set->dim);
4415         dim = isl_dim_cow(dim);
4416         if (!dim)
4417                 goto error;
4418
4419         nparam = isl_set_dim(set, isl_dim_param);
4420
4421         isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
4422
4423         dim->nparam = 0;
4424         dim->n_out = nparam;
4425         isl_set_reset_dim(set, dim);
4426         set = isl_set_preimage(set, mat);
4427         if (!set)
4428                 goto error2;
4429         dim = isl_dim_copy(set->dim);
4430         dim = isl_dim_cow(dim);
4431         if (!dim)
4432                 goto error2;
4433         dim->nparam = dim->n_out;
4434         dim->n_out = 0;
4435         isl_set_reset_dim(set, dim);
4436         return set;
4437 error:
4438         isl_dim_free(dim);
4439         isl_mat_free(mat);
4440 error2:
4441         isl_set_free(set);
4442         return NULL;
4443 }
4444
4445 /* Intersect the basic set "bset" with the affine space specified by the
4446  * equalities in "eq".
4447  */
4448 static struct isl_basic_set *basic_set_append_equalities(
4449         struct isl_basic_set *bset, struct isl_mat *eq)
4450 {
4451         int i, k;
4452         unsigned len;
4453
4454         if (!bset || !eq)
4455                 goto error;
4456
4457         bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
4458                                         eq->n_row, 0);
4459         if (!bset)
4460                 goto error;
4461
4462         len = 1 + isl_dim_total(bset->dim) + bset->extra;
4463         for (i = 0; i < eq->n_row; ++i) {
4464                 k = isl_basic_set_alloc_equality(bset);
4465                 if (k < 0)
4466                         goto error;
4467                 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
4468                 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
4469         }
4470         isl_mat_free(eq);
4471
4472         bset = isl_basic_set_gauss(bset, NULL);
4473         bset = isl_basic_set_finalize(bset);
4474
4475         return bset;
4476 error:
4477         isl_mat_free(eq);
4478         isl_basic_set_free(bset);
4479         return NULL;
4480 }
4481
4482 /* Intersect the set "set" with the affine space specified by the
4483  * equalities in "eq".
4484  */
4485 static struct isl_set *set_append_equalities(struct isl_set *set,
4486         struct isl_mat *eq)
4487 {
4488         int i;
4489
4490         if (!set || !eq)
4491                 goto error;
4492
4493         for (i = 0; i < set->n; ++i) {
4494                 set->p[i] = basic_set_append_equalities(set->p[i],
4495                                         isl_mat_copy(eq));
4496                 if (!set->p[i])
4497                         goto error;
4498         }
4499         isl_mat_free(eq);
4500         return set;
4501 error:
4502         isl_mat_free(eq);
4503         isl_set_free(set);
4504         return NULL;
4505 }
4506
4507 /* Project the given basic set onto its parameter domain, possibly introducing
4508  * new, explicit, existential variables in the constraints.
4509  * The input has parameters and (possibly implicit) existential variables.
4510  * The output has the same parameters, but only
4511  * explicit existentially quantified variables.
4512  *
4513  * The actual projection is performed by pip, but pip doesn't seem
4514  * to like equalities very much, so we first remove the equalities
4515  * among the parameters by performing a variable compression on
4516  * the parameters.  Afterward, an inverse transformation is performed
4517  * and the equalities among the parameters are inserted back in.
4518  */
4519 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
4520 {
4521         int i, j;
4522         struct isl_mat *eq;
4523         struct isl_mat *T, *T2;
4524         struct isl_set *set;
4525         unsigned nparam, n_div;
4526
4527         bset = isl_basic_set_cow(bset);
4528         if (!bset)
4529                 return NULL;
4530
4531         if (bset->n_eq == 0)
4532                 return isl_basic_set_lexmin(bset);
4533
4534         isl_basic_set_gauss(bset, NULL);
4535
4536         nparam = isl_basic_set_dim(bset, isl_dim_param);
4537         n_div = isl_basic_set_dim(bset, isl_dim_div);
4538
4539         for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
4540                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
4541                         ++i;
4542         }
4543         if (i == bset->n_eq)
4544                 return isl_basic_set_lexmin(bset);
4545
4546         eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
4547                 0, 1 + nparam);
4548         eq = isl_mat_cow(eq);
4549         T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
4550         if (T && T->n_col == 0) {
4551                 isl_mat_free(T);
4552                 isl_mat_free(T2);
4553                 isl_mat_free(eq);
4554                 bset = isl_basic_set_set_to_empty(bset);
4555                 return isl_set_from_basic_set(bset);
4556         }
4557         bset = basic_set_parameter_preimage(bset, T);
4558
4559         set = isl_basic_set_lexmin(bset);
4560         set = set_parameter_preimage(set, T2);
4561         set = set_append_equalities(set, eq);
4562         return set;
4563 }
4564
4565 /* Compute an explicit representation for all the existentially
4566  * quantified variables.
4567  * The input and output dimensions are first turned into parameters.
4568  * compute_divs then returns a map with the same parameters and
4569  * no input or output dimensions and the dimension specification
4570  * is reset to that of the input.
4571  */
4572 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
4573 {
4574         struct isl_basic_set *bset;
4575         struct isl_set *set;
4576         struct isl_map *map;
4577         struct isl_dim *dim, *orig_dim = NULL;
4578         unsigned         nparam;
4579         unsigned         n_in;
4580         unsigned         n_out;
4581
4582         bmap = isl_basic_map_cow(bmap);
4583         if (!bmap)
4584                 return NULL;
4585
4586         nparam = isl_basic_map_dim(bmap, isl_dim_param);
4587         n_in = isl_basic_map_dim(bmap, isl_dim_in);
4588         n_out = isl_basic_map_dim(bmap, isl_dim_out);
4589         dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
4590         if (!dim)
4591                 goto error;
4592
4593         orig_dim = bmap->dim;
4594         bmap->dim = dim;
4595         bset = (struct isl_basic_set *)bmap;
4596
4597         set = parameter_compute_divs(bset);
4598         map = (struct isl_map *)set;
4599         map = isl_map_reset_dim(map, orig_dim);
4600
4601         return map;
4602 error:
4603         isl_basic_map_free(bmap);
4604         return NULL;
4605 }
4606
4607 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
4608 {
4609         int i;
4610         unsigned off;
4611
4612         if (!bmap)
4613                 return -1;
4614
4615         off = isl_dim_total(bmap->dim);
4616         for (i = 0; i < bmap->n_div; ++i) {
4617                 if (isl_int_is_zero(bmap->div[i][0]))
4618                         return 0;
4619                 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
4620                                 return -1);
4621         }
4622         return 1;
4623 }
4624
4625 static int map_divs_known(__isl_keep isl_map *map)
4626 {
4627         int i;
4628
4629         if (!map)
4630                 return -1;
4631
4632         for (i = 0; i < map->n; ++i) {
4633                 int known = basic_map_divs_known(map->p[i]);
4634                 if (known <= 0)
4635                         return known;
4636         }
4637
4638         return 1;
4639 }
4640
4641 /* If bmap contains any unknown divs, then compute explicit
4642  * expressions for them.  However, this computation may be
4643  * quite expensive, so first try to remove divs that aren't
4644  * strictly needed.
4645  */
4646 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
4647 {
4648         int i;
4649         int known;
4650         struct isl_map *map;
4651
4652         known = basic_map_divs_known(bmap);
4653         if (known < 0)
4654                 goto error;
4655         if (known)
4656                 return isl_map_from_basic_map(bmap);
4657
4658         bmap = isl_basic_map_drop_redundant_divs(bmap);
4659
4660         known = basic_map_divs_known(bmap);
4661         if (known < 0)
4662                 goto error;
4663         if (known)
4664                 return isl_map_from_basic_map(bmap);
4665
4666         map = compute_divs(bmap);
4667         return map;
4668 error:
4669         isl_basic_map_free(bmap);
4670         return NULL;
4671 }
4672
4673 struct isl_map *isl_map_compute_divs(struct isl_map *map)
4674 {
4675         int i;
4676         int known;
4677         struct isl_map *res;
4678
4679         if (!map)
4680                 return NULL;
4681         if (map->n == 0)
4682                 return map;
4683
4684         known = map_divs_known(map);
4685         if (known < 0) {
4686                 isl_map_free(map);
4687                 return NULL;
4688         }
4689         if (known)
4690                 return map;
4691
4692         res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
4693         for (i = 1 ; i < map->n; ++i) {
4694                 struct isl_map *r2;
4695                 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
4696                 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4697                         res = isl_map_union_disjoint(res, r2);
4698                 else
4699                         res = isl_map_union(res, r2);
4700         }
4701         isl_map_free(map);
4702
4703         return res;
4704 }
4705
4706 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
4707 {
4708         return (struct isl_set *)
4709                 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
4710 }
4711
4712 struct isl_set *isl_set_compute_divs(struct isl_set *set)
4713 {
4714         return (struct isl_set *)
4715                 isl_map_compute_divs((struct isl_map *)set);
4716 }
4717
4718 struct isl_set *isl_map_domain(struct isl_map *map)
4719 {
4720         int i;
4721         struct isl_set *set;
4722
4723         if (!map)
4724                 goto error;
4725
4726         map = isl_map_cow(map);
4727         if (!map)
4728                 return NULL;
4729
4730         set = (struct isl_set *)map;
4731         set->dim = isl_dim_domain(set->dim);
4732         if (!set->dim)
4733                 goto error;
4734         for (i = 0; i < map->n; ++i) {
4735                 set->p[i] = isl_basic_map_domain(map->p[i]);
4736                 if (!set->p[i])
4737                         goto error;
4738         }
4739         ISL_F_CLR(set, ISL_MAP_DISJOINT);
4740         ISL_F_CLR(set, ISL_SET_NORMALIZED);
4741         return set;
4742 error:
4743         isl_map_free(map);
4744         return NULL;
4745 }
4746
4747 struct isl_map *isl_map_union_disjoint(
4748                         struct isl_map *map1, struct isl_map *map2)
4749 {
4750         int i;
4751         unsigned flags = 0;
4752         struct isl_map *map = NULL;
4753
4754         if (!map1 || !map2)
4755                 goto error;
4756
4757         if (map1->n == 0) {
4758                 isl_map_free(map1);
4759                 return map2;
4760         }
4761         if (map2->n == 0) {
4762                 isl_map_free(map2);
4763                 return map1;
4764         }
4765
4766         isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4767
4768         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4769             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4770                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4771
4772         map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
4773                                 map1->n + map2->n, flags);
4774         if (!map)
4775                 goto error;
4776         for (i = 0; i < map1->n; ++i) {
4777                 map = isl_map_add_basic_map(map,
4778                                   isl_basic_map_copy(map1->p[i]));
4779                 if (!map)
4780                         goto error;
4781         }
4782         for (i = 0; i < map2->n; ++i) {
4783                 map = isl_map_add_basic_map(map,
4784                                   isl_basic_map_copy(map2->p[i]));
4785                 if (!map)
4786                         goto error;
4787         }
4788         isl_map_free(map1);
4789         isl_map_free(map2);
4790         return map;
4791 error:
4792         isl_map_free(map);
4793         isl_map_free(map1);
4794         isl_map_free(map2);
4795         return NULL;
4796 }
4797
4798 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
4799 {
4800         map1 = isl_map_union_disjoint(map1, map2);
4801         if (!map1)
4802                 return NULL;
4803         if (map1->n > 1)
4804                 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
4805         return map1;
4806 }
4807
4808 struct isl_set *isl_set_union_disjoint(
4809                         struct isl_set *set1, struct isl_set *set2)
4810 {
4811         return (struct isl_set *)
4812                 isl_map_union_disjoint(
4813                         (struct isl_map *)set1, (struct isl_map *)set2);
4814 }
4815
4816 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
4817 {
4818         return (struct isl_set *)
4819                 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
4820 }
4821
4822 struct isl_map *isl_map_intersect_range(
4823                 struct isl_map *map, struct isl_set *set)
4824 {
4825         unsigned flags = 0;
4826         struct isl_map *result;
4827         int i, j;
4828
4829         if (!map || !set)
4830                 goto error;
4831
4832         if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
4833             ISL_F_ISSET(set, ISL_MAP_DISJOINT))
4834                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4835
4836         result = isl_map_alloc_dim(isl_dim_copy(map->dim),
4837                                         map->n * set->n, flags);
4838         if (!result)
4839                 goto error;
4840         for (i = 0; i < map->n; ++i)
4841                 for (j = 0; j < set->n; ++j) {
4842                         result = isl_map_add_basic_map(result,
4843                             isl_basic_map_intersect_range(
4844                                 isl_basic_map_copy(map->p[i]),
4845                                 isl_basic_set_copy(set->p[j])));
4846                         if (!result)
4847                                 goto error;
4848                 }
4849         isl_map_free(map);
4850         isl_set_free(set);
4851         return result;
4852 error:
4853         isl_map_free(map);
4854         isl_set_free(set);
4855         return NULL;
4856 }
4857
4858 struct isl_map *isl_map_intersect_domain(
4859                 struct isl_map *map, struct isl_set *set)
4860 {
4861         return isl_map_reverse(
4862                 isl_map_intersect_range(isl_map_reverse(map), set));
4863 }
4864
4865 struct isl_map *isl_map_apply_domain(
4866                 struct isl_map *map1, struct isl_map *map2)
4867 {
4868         if (!map1 || !map2)
4869                 goto error;
4870         map1 = isl_map_reverse(map1);
4871         map1 = isl_map_apply_range(map1, map2);
4872         return isl_map_reverse(map1);
4873 error:
4874         isl_map_free(map1);
4875         isl_map_free(map2);
4876         return NULL;
4877 }
4878
4879 struct isl_map *isl_map_apply_range(
4880                 struct isl_map *map1, struct isl_map *map2)
4881 {
4882         struct isl_dim *dim_result;
4883         struct isl_map *result;
4884         int i, j;
4885
4886         if (!map1 || !map2)
4887                 goto error;
4888
4889         dim_result = isl_dim_join(isl_dim_copy(map1->dim),
4890                                   isl_dim_copy(map2->dim));
4891
4892         result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
4893         if (!result)
4894                 goto error;
4895         for (i = 0; i < map1->n; ++i)
4896                 for (j = 0; j < map2->n; ++j) {
4897                         result = isl_map_add_basic_map(result,
4898                             isl_basic_map_apply_range(
4899                                 isl_basic_map_copy(map1->p[i]),
4900                                 isl_basic_map_copy(map2->p[j])));
4901                         if (!result)
4902                                 goto error;
4903                 }
4904         isl_map_free(map1);
4905         isl_map_free(map2);
4906         if (result && result->n <= 1)
4907                 ISL_F_SET(result, ISL_MAP_DISJOINT);
4908         return result;
4909 error:
4910         isl_map_free(map1);
4911         isl_map_free(map2);
4912         return NULL;
4913 }
4914
4915 /*
4916  * returns range - domain
4917  */
4918 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
4919 {
4920         isl_dim *dims;
4921         struct isl_basic_set *bset;
4922         unsigned dim;
4923         unsigned nparam;
4924         int i;
4925
4926         if (!bmap)
4927                 goto error;
4928         dim = isl_basic_map_n_in(bmap);
4929         nparam = isl_basic_map_n_param(bmap);
4930         isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
4931         bset = isl_basic_set_from_basic_map(bmap);
4932         bset = isl_basic_set_cow(bset);
4933         dims = isl_basic_set_get_dim(bset);
4934         dims = isl_dim_add(dims, isl_dim_set, dim);
4935         bset = isl_basic_set_extend_dim(bset, dims, 0, dim, 0);
4936         bset = isl_basic_set_swap_vars(bset, 2*dim);
4937         for (i = 0; i < dim; ++i) {
4938                 int j = isl_basic_map_alloc_equality(
4939                                             (struct isl_basic_map *)bset);
4940                 if (j < 0)
4941                         goto error;
4942                 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
4943                 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
4944                 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
4945                 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
4946         }
4947         return isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
4948 error:
4949         isl_basic_map_free(bmap);
4950         return NULL;
4951 }
4952
4953 /*
4954  * returns range - domain
4955  */
4956 struct isl_set *isl_map_deltas(struct isl_map *map)
4957 {
4958         int i;
4959         isl_dim *dim;
4960         struct isl_set *result;
4961
4962         if (!map)
4963                 return NULL;
4964
4965         isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
4966         dim = isl_map_get_dim(map);
4967         dim = isl_dim_domain(dim);
4968         result = isl_set_alloc_dim(dim, map->n, map->flags);
4969         if (!result)
4970                 goto error;
4971         for (i = 0; i < map->n; ++i)
4972                 result = isl_set_add_basic_set(result,
4973                           isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
4974         isl_map_free(map);
4975         return result;
4976 error:
4977         isl_map_free(map);
4978         return NULL;
4979 }
4980
4981 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
4982 {
4983         struct isl_basic_map *bmap;
4984         unsigned nparam;
4985         unsigned dim;
4986         int i;
4987
4988         if (!dims)
4989                 return NULL;
4990
4991         nparam = dims->nparam;
4992         dim = dims->n_out;
4993         bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
4994         if (!bmap)
4995                 goto error;
4996
4997         for (i = 0; i < dim; ++i) {
4998                 int j = isl_basic_map_alloc_equality(bmap);
4999                 if (j < 0)
5000                         goto error;
5001                 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
5002                 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
5003                 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
5004         }
5005         return isl_basic_map_finalize(bmap);
5006 error:
5007         isl_basic_map_free(bmap);
5008         return NULL;
5009 }
5010
5011 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
5012 {
5013         struct isl_dim *dim = isl_dim_map(set_dim);
5014         if (!dim)
5015                 return NULL;
5016         return basic_map_identity(dim);
5017 }
5018
5019 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
5020 {
5021         if (!model || !model->dim)
5022                 return NULL;
5023         isl_assert(model->ctx,
5024                         model->dim->n_in == model->dim->n_out, return NULL);
5025         return basic_map_identity(isl_dim_copy(model->dim));
5026 }
5027
5028 static struct isl_map *map_identity(struct isl_dim *dim)
5029 {
5030         struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
5031         return isl_map_add_basic_map(map, basic_map_identity(isl_dim_copy(dim)));
5032 }
5033
5034 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
5035 {
5036         struct isl_dim *dim = isl_dim_map(set_dim);
5037         if (!dim)
5038                 return NULL;
5039         return map_identity(dim);
5040 }
5041
5042 struct isl_map *isl_map_identity_like(struct isl_map *model)
5043 {
5044         if (!model || !model->dim)
5045                 return NULL;
5046         isl_assert(model->ctx,
5047                         model->dim->n_in == model->dim->n_out, return NULL);
5048         return map_identity(isl_dim_copy(model->dim));
5049 }
5050
5051 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
5052 {
5053         if (!model || !model->dim)
5054                 return NULL;
5055         isl_assert(model->ctx,
5056                         model->dim->n_in == model->dim->n_out, return NULL);
5057         return map_identity(isl_dim_copy(model->dim));
5058 }
5059
5060 /* Construct a basic set with all set dimensions having only non-negative
5061  * values.
5062  */
5063 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
5064 {
5065         int i;
5066         unsigned nparam;
5067         unsigned dim;
5068         struct isl_basic_set *bset;
5069
5070         if (!dims)
5071                 return NULL;
5072         nparam = dims->nparam;
5073         dim = dims->n_out;
5074         bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
5075         if (!bset)
5076                 return NULL;
5077         for (i = 0; i < dim; ++i) {
5078                 int k = isl_basic_set_alloc_inequality(bset);
5079                 if (k < 0)
5080                         goto error;
5081                 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
5082                 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
5083         }
5084         return bset;
5085 error:
5086         isl_basic_set_free(bset);
5087         return NULL;
5088 }
5089
5090 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
5091         enum isl_dim_type type, unsigned first, unsigned n)
5092 {
5093         int i;
5094         isl_basic_set *nonneg = NULL;
5095         isl_basic_set *neg = NULL;
5096
5097         if (!set)
5098                 return NULL;
5099         if (n == 0)
5100                 return set;
5101
5102         isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
5103
5104         for (i = 0; i < n; ++i) {
5105                 int k;
5106
5107                 neg = NULL;
5108                 nonneg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5109                 k = isl_basic_set_alloc_inequality(nonneg);
5110                 if (k < 0)
5111                         goto error;
5112                 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
5113                 isl_int_set_si(nonneg->ineq[k][pos(set->dim, type) + first + i], 1);
5114
5115                 neg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
5116                 k = isl_basic_set_alloc_inequality(neg);
5117                 if (k < 0)
5118                         goto error;
5119                 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
5120                 isl_int_set_si(neg->ineq[k][0], -1);
5121                 isl_int_set_si(neg->ineq[k][pos(set->dim, type) + first + i], -1);
5122
5123                 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
5124         }
5125
5126         return set;
5127 error:
5128         isl_basic_set_free(nonneg);
5129         isl_basic_set_free(neg);
5130         isl_set_free(set);
5131         return NULL;
5132 }
5133
5134 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
5135 {
5136         return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
5137 }
5138
5139 int isl_basic_map_is_subset(
5140                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5141 {
5142         int is_subset;
5143         struct isl_map *map1;
5144         struct isl_map *map2;
5145
5146         if (!bmap1 || !bmap2)
5147                 return -1;
5148
5149         map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
5150         map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
5151
5152         is_subset = isl_map_is_subset(map1, map2);
5153
5154         isl_map_free(map1);
5155         isl_map_free(map2);
5156
5157         return is_subset;
5158 }
5159
5160 int isl_basic_map_is_equal(
5161                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5162 {
5163         int is_subset;
5164
5165         if (!bmap1 || !bmap2)
5166                 return -1;
5167         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5168         if (is_subset != 1)
5169                 return is_subset;
5170         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5171         return is_subset;
5172 }
5173
5174 int isl_basic_set_is_equal(
5175                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5176 {
5177         return isl_basic_map_is_equal(
5178                 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
5179 }
5180
5181 int isl_map_is_empty(struct isl_map *map)
5182 {
5183         int i;
5184         int is_empty;
5185
5186         if (!map)
5187                 return -1;
5188         for (i = 0; i < map->n; ++i) {
5189                 is_empty = isl_basic_map_is_empty(map->p[i]);
5190                 if (is_empty < 0)
5191                         return -1;
5192                 if (!is_empty)
5193                         return 0;
5194         }
5195         return 1;
5196 }
5197
5198 int isl_map_fast_is_empty(struct isl_map *map)
5199 {
5200         return map->n == 0;
5201 }
5202
5203 int isl_set_fast_is_empty(struct isl_set *set)
5204 {
5205         return set->n == 0;
5206 }
5207
5208 int isl_set_is_empty(struct isl_set *set)
5209 {
5210         return isl_map_is_empty((struct isl_map *)set);
5211 }
5212
5213 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
5214 {
5215         int is_subset;
5216
5217         if (!map1 || !map2)
5218                 return -1;
5219         is_subset = isl_map_is_subset(map1, map2);
5220         if (is_subset != 1)
5221                 return is_subset;
5222         is_subset = isl_map_is_subset(map2, map1);
5223         return is_subset;
5224 }
5225
5226 int isl_basic_map_is_strict_subset(
5227                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5228 {
5229         int is_subset;
5230
5231         if (!bmap1 || !bmap2)
5232                 return -1;
5233         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
5234         if (is_subset != 1)
5235                 return is_subset;
5236         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
5237         if (is_subset == -1)
5238                 return is_subset;
5239         return !is_subset;
5240 }
5241
5242 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
5243 {
5244         int is_subset;
5245
5246         if (!map1 || !map2)
5247                 return -1;
5248         is_subset = isl_map_is_subset(map1, map2);
5249         if (is_subset != 1)
5250                 return is_subset;
5251         is_subset = isl_map_is_subset(map2, map1);
5252         if (is_subset == -1)
5253                 return is_subset;
5254         return !is_subset;
5255 }
5256
5257 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5258 {
5259         return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
5260 }
5261
5262 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
5263 {
5264         if (!bmap)
5265                 return -1;
5266         return bmap->n_eq == 0 && bmap->n_ineq == 0;
5267 }
5268
5269 int isl_basic_set_is_universe(struct isl_basic_set *bset)
5270 {
5271         if (!bset)
5272                 return -1;
5273         return bset->n_eq == 0 && bset->n_ineq == 0;
5274 }
5275
5276 int isl_map_fast_is_universe(__isl_keep isl_map *map)
5277 {
5278         if (!map)
5279                 return -1;
5280
5281         return map->n == 1 && isl_basic_map_is_universe(map->p[0]);
5282 }
5283
5284 int isl_set_fast_is_universe(__isl_keep isl_set *set)
5285 {
5286         return isl_map_fast_is_universe((isl_map *) set);
5287 }
5288
5289 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
5290 {
5291         struct isl_basic_set *bset = NULL;
5292         struct isl_vec *sample = NULL;
5293         int empty;
5294         unsigned total;
5295
5296         if (!bmap)
5297                 return -1;
5298
5299         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
5300                 return 1;
5301
5302         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
5303                 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
5304                 copy = isl_basic_map_convex_hull(copy);
5305                 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
5306                 isl_basic_map_free(copy);
5307                 return empty;
5308         }
5309
5310         total = 1 + isl_basic_map_total_dim(bmap);
5311         if (bmap->sample && bmap->sample->size == total) {
5312                 int contains = isl_basic_map_contains(bmap, bmap->sample);
5313                 if (contains < 0)
5314                         return -1;
5315                 if (contains)
5316                         return 0;
5317         }
5318         isl_vec_free(bmap->sample);
5319         bmap->sample = NULL;
5320         bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
5321         if (!bset)
5322                 return -1;
5323         sample = isl_basic_set_sample_vec(bset);
5324         if (!sample)
5325                 return -1;
5326         empty = sample->size == 0;
5327         isl_vec_free(bmap->sample);
5328         bmap->sample = sample;
5329         if (empty)
5330                 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
5331
5332         return empty;
5333 }
5334
5335 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
5336 {
5337         if (!bmap)
5338                 return -1;
5339         return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
5340 }
5341
5342 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
5343 {
5344         if (!bset)
5345                 return -1;
5346         return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
5347 }
5348
5349 int isl_basic_set_is_empty(struct isl_basic_set *bset)
5350 {
5351         return isl_basic_map_is_empty((struct isl_basic_map *)bset);
5352 }
5353
5354 struct isl_map *isl_basic_map_union(
5355         struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5356 {
5357         struct isl_map *map;
5358         if (!bmap1 || !bmap2)
5359                 return NULL;
5360
5361         isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
5362
5363         map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
5364         if (!map)
5365                 goto error;
5366         map = isl_map_add_basic_map(map, bmap1);
5367         map = isl_map_add_basic_map(map, bmap2);
5368         return map;
5369 error:
5370         isl_basic_map_free(bmap1);
5371         isl_basic_map_free(bmap2);
5372         return NULL;
5373 }
5374
5375 struct isl_set *isl_basic_set_union(
5376                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5377 {
5378         return (struct isl_set *)isl_basic_map_union(
5379                                             (struct isl_basic_map *)bset1,
5380                                             (struct isl_basic_map *)bset2);
5381 }
5382
5383 /* Order divs such that any div only depends on previous divs */
5384 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
5385 {
5386         int i;
5387         unsigned off;
5388
5389         if (!bmap)
5390                 return NULL;
5391
5392         off = isl_dim_total(bmap->dim);
5393
5394         for (i = 0; i < bmap->n_div; ++i) {
5395                 int pos;
5396                 if (isl_int_is_zero(bmap->div[i][0]))
5397                         continue;
5398                 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
5399                                                             bmap->n_div-i);
5400                 if (pos == -1)
5401                         continue;
5402                 isl_basic_map_swap_div(bmap, i, i + pos);
5403                 --i;
5404         }
5405         return bmap;
5406 }
5407
5408 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
5409 {
5410         return (struct isl_basic_set *)
5411                 isl_basic_map_order_divs((struct isl_basic_map *)bset);
5412 }
5413
5414 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
5415 {
5416         int i;
5417
5418         if (!map)
5419                 return 0;
5420
5421         for (i = 0; i < map->n; ++i) {
5422                 map->p[i] = isl_basic_map_order_divs(map->p[i]);
5423                 if (!map->p[i])
5424                         goto error;
5425         }
5426
5427         return map;
5428 error:
5429         isl_map_free(map);
5430         return NULL;
5431 }
5432
5433 /* Look for a div in dst that corresponds to the div "div" in src.
5434  * The divs before "div" in src and dst are assumed to be the same.
5435  * 
5436  * Returns -1 if no corresponding div was found and the position
5437  * of the corresponding div in dst otherwise.
5438  */
5439 static int find_div(struct isl_basic_map *dst,
5440                         struct isl_basic_map *src, unsigned div)
5441 {
5442         int i;
5443
5444         unsigned total = isl_dim_total(src->dim);
5445
5446         isl_assert(dst->ctx, div <= dst->n_div, return -1);
5447         for (i = div; i < dst->n_div; ++i)
5448                 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
5449                     isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
5450                                                 dst->n_div - div) == -1)
5451                         return i;
5452         return -1;
5453 }
5454
5455 struct isl_basic_map *isl_basic_map_align_divs(
5456                 struct isl_basic_map *dst, struct isl_basic_map *src)
5457 {
5458         int i;
5459         unsigned total = isl_dim_total(src->dim);
5460
5461         if (!dst || !src)
5462                 goto error;
5463
5464         if (src->n_div == 0)
5465                 return dst;
5466
5467         for (i = 0; i < src->n_div; ++i)
5468                 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
5469
5470         src = isl_basic_map_order_divs(src);
5471         dst = isl_basic_map_cow(dst);
5472         dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
5473                         src->n_div, 0, 2 * src->n_div);
5474         if (!dst)
5475                 return NULL;
5476         for (i = 0; i < src->n_div; ++i) {
5477                 int j = find_div(dst, src, i);
5478                 if (j < 0) {
5479                         j = isl_basic_map_alloc_div(dst);
5480                         if (j < 0)
5481                                 goto error;
5482                         isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
5483                         isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
5484                         if (isl_basic_map_add_div_constraints(dst, j) < 0)
5485                                 goto error;
5486                 }
5487                 if (j != i)
5488                         isl_basic_map_swap_div(dst, i, j);
5489         }
5490         return dst;
5491 error:
5492         isl_basic_map_free(dst);
5493         return NULL;
5494 }
5495
5496 struct isl_basic_set *isl_basic_set_align_divs(
5497                 struct isl_basic_set *dst, struct isl_basic_set *src)
5498 {
5499         return (struct isl_basic_set *)isl_basic_map_align_divs(
5500                 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
5501 }
5502
5503 struct isl_map *isl_map_align_divs(struct isl_map *map)
5504 {
5505         int i;
5506
5507         if (!map)
5508                 return NULL;
5509         if (map->n == 0)
5510                 return map;
5511         map = isl_map_compute_divs(map);
5512         map = isl_map_cow(map);
5513         if (!map)
5514                 return NULL;
5515
5516         for (i = 1; i < map->n; ++i)
5517                 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
5518         for (i = 1; i < map->n; ++i)
5519                 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
5520
5521         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5522         return map;
5523 }
5524
5525 struct isl_set *isl_set_align_divs(struct isl_set *set)
5526 {
5527         return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
5528 }
5529
5530 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
5531 {
5532         if (!set || !map)
5533                 goto error;
5534         isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
5535         map = isl_map_intersect_domain(map, set);
5536         set = isl_map_range(map);
5537         return set;
5538 error:
5539         isl_set_free(set);
5540         isl_map_free(map);
5541         return NULL;
5542 }
5543
5544 /* There is no need to cow as removing empty parts doesn't change
5545  * the meaning of the set.
5546  */
5547 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
5548 {
5549         int i;
5550
5551         if (!map)
5552                 return NULL;
5553
5554         for (i = map->n-1; i >= 0; --i) {
5555                 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
5556                         continue;
5557                 isl_basic_map_free(map->p[i]);
5558                 if (i != map->n-1) {
5559                         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5560                         map->p[i] = map->p[map->n-1];
5561                 }
5562                 map->n--;
5563         }
5564
5565         return map;
5566 }
5567
5568 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
5569 {
5570         return (struct isl_set *)
5571                 isl_map_remove_empty_parts((struct isl_map *)set);
5572 }
5573
5574 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
5575 {
5576         struct isl_basic_map *bmap;
5577         if (!map || map->n == 0)
5578                 return NULL;
5579         bmap = map->p[map->n-1];
5580         isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
5581         return isl_basic_map_copy(bmap);
5582 }
5583
5584 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
5585 {
5586         return (struct isl_basic_set *)
5587                 isl_map_copy_basic_map((struct isl_map *)set);
5588 }
5589
5590 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
5591                                                 __isl_keep isl_basic_map *bmap)
5592 {
5593         int i;
5594
5595         if (!map || !bmap)
5596                 goto error;
5597         for (i = map->n-1; i >= 0; --i) {
5598                 if (map->p[i] != bmap)
5599                         continue;
5600                 map = isl_map_cow(map);
5601                 if (!map)
5602                         goto error;
5603                 isl_basic_map_free(map->p[i]);
5604                 if (i != map->n-1) {
5605                         ISL_F_CLR(map, ISL_SET_NORMALIZED);
5606                         map->p[i] = map->p[map->n-1];
5607                 }
5608                 map->n--;
5609                 return map;
5610         }
5611         return map;
5612 error:
5613         isl_map_free(map);
5614         return NULL;
5615 }
5616
5617 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
5618                                                 struct isl_basic_set *bset)
5619 {
5620         return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
5621                                                 (struct isl_basic_map *)bset);
5622 }
5623
5624 /* Given two basic sets bset1 and bset2, compute the maximal difference
5625  * between the values of dimension pos in bset1 and those in bset2
5626  * for any common value of the parameters and dimensions preceding pos.
5627  */
5628 static enum isl_lp_result basic_set_maximal_difference_at(
5629         __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
5630         int pos, isl_int *opt)
5631 {
5632         struct isl_dim *dims;
5633         struct isl_basic_map *bmap1 = NULL;
5634         struct isl_basic_map *bmap2 = NULL;
5635         struct isl_ctx *ctx;
5636         struct isl_vec *obj;
5637         unsigned total;
5638         unsigned nparam;
5639         unsigned dim1, dim2;
5640         enum isl_lp_result res;
5641
5642         if (!bset1 || !bset2)
5643                 return isl_lp_error;
5644
5645         nparam = isl_basic_set_n_param(bset1);
5646         dim1 = isl_basic_set_n_dim(bset1);
5647         dim2 = isl_basic_set_n_dim(bset2);
5648         dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
5649         bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
5650         dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
5651         bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
5652         if (!bmap1 || !bmap2)
5653                 goto error;
5654         bmap1 = isl_basic_map_cow(bmap1);
5655         bmap1 = isl_basic_map_extend(bmap1, nparam,
5656                         pos, (dim1 - pos) + (dim2 - pos),
5657                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
5658         bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
5659         if (!bmap1)
5660                 goto error;
5661         total = isl_basic_map_total_dim(bmap1);
5662         ctx = bmap1->ctx;
5663         obj = isl_vec_alloc(ctx, 1 + total);
5664         isl_seq_clr(obj->block.data, 1 + total);
5665         isl_int_set_si(obj->block.data[1+nparam+pos], 1);
5666         isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
5667         if (!obj)
5668                 goto error;
5669         res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
5670                                         opt, NULL, NULL);
5671         isl_basic_map_free(bmap1);
5672         isl_vec_free(obj);
5673         return res;
5674 error:
5675         isl_basic_map_free(bmap1);
5676         isl_basic_map_free(bmap2);
5677         return isl_lp_error;
5678 }
5679
5680 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
5681  * for any common value of the parameters and dimensions preceding pos
5682  * in both basic sets, the values of dimension pos in bset1 are
5683  * smaller or larger than those in bset2.
5684  *
5685  * Returns
5686  *       1 if bset1 follows bset2
5687  *      -1 if bset1 precedes bset2
5688  *       0 if bset1 and bset2 are incomparable
5689  *      -2 if some error occurred.
5690  */
5691 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
5692         struct isl_basic_set *bset2, int pos)
5693 {
5694         isl_int opt;
5695         enum isl_lp_result res;
5696         int cmp;
5697
5698         isl_int_init(opt);
5699
5700         res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5701
5702         if (res == isl_lp_empty)
5703                 cmp = 0;
5704         else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5705                   res == isl_lp_unbounded)
5706                 cmp = 1;
5707         else if (res == isl_lp_ok && isl_int_is_neg(opt))
5708                 cmp = -1;
5709         else
5710                 cmp = -2;
5711
5712         isl_int_clear(opt);
5713         return cmp;
5714 }
5715
5716 /* Given two basic sets bset1 and bset2, check whether
5717  * for any common value of the parameters and dimensions preceding pos
5718  * there is a value of dimension pos in bset1 that is larger
5719  * than a value of the same dimension in bset2.
5720  *
5721  * Return
5722  *       1 if there exists such a pair
5723  *       0 if there is no such pair, but there is a pair of equal values
5724  *      -1 otherwise
5725  *      -2 if some error occurred.
5726  */
5727 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
5728         __isl_keep isl_basic_set *bset2, int pos)
5729 {
5730         isl_int opt;
5731         enum isl_lp_result res;
5732         int cmp;
5733
5734         isl_int_init(opt);
5735
5736         res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5737
5738         if (res == isl_lp_empty)
5739                 cmp = -1;
5740         else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5741                   res == isl_lp_unbounded)
5742                 cmp = 1;
5743         else if (res == isl_lp_ok && isl_int_is_neg(opt))
5744                 cmp = -1;
5745         else if (res == isl_lp_ok)
5746                 cmp = 0;
5747         else
5748                 cmp = -2;
5749
5750         isl_int_clear(opt);
5751         return cmp;
5752 }
5753
5754 /* Given two sets set1 and set2, check whether
5755  * for any common value of the parameters and dimensions preceding pos
5756  * there is a value of dimension pos in set1 that is larger
5757  * than a value of the same dimension in set2.
5758  *
5759  * Return
5760  *       1 if there exists such a pair
5761  *       0 if there is no such pair, but there is a pair of equal values
5762  *      -1 otherwise
5763  *      -2 if some error occurred.
5764  */
5765 int isl_set_follows_at(__isl_keep isl_set *set1,
5766         __isl_keep isl_set *set2, int pos)
5767 {
5768         int i, j;
5769         int follows = -1;
5770
5771         if (!set1 || !set2)
5772                 return -2;
5773
5774         for (i = 0; i < set1->n; ++i)
5775                 for (j = 0; j < set2->n; ++j) {
5776                         int f;
5777                         f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
5778                         if (f == 1 || f == -2)
5779                                 return f;
5780                         if (f > follows)
5781                                 follows = f;
5782                 }
5783
5784         return follows;
5785 }
5786
5787 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
5788         unsigned pos, isl_int *val)
5789 {
5790         int i;
5791         int d;
5792         unsigned total;
5793
5794         if (!bmap)
5795                 return -1;
5796         total = isl_basic_map_total_dim(bmap);
5797         for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
5798                 for (; d+1 > pos; --d)
5799                         if (!isl_int_is_zero(bmap->eq[i][1+d]))
5800                                 break;
5801                 if (d != pos)
5802                         continue;
5803                 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
5804                         return 0;
5805                 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
5806                         return 0;
5807                 if (!isl_int_is_one(bmap->eq[i][1+d]))
5808                         return 0;
5809                 if (val)
5810                         isl_int_neg(*val, bmap->eq[i][0]);
5811                 return 1;
5812         }
5813         return 0;
5814 }
5815
5816 static int isl_map_fast_has_fixed_var(struct isl_map *map,
5817         unsigned pos, isl_int *val)
5818 {
5819         int i;
5820         isl_int v;
5821         isl_int tmp;
5822         int fixed;
5823
5824         if (!map)
5825                 return -1;
5826         if (map->n == 0)
5827                 return 0;
5828         if (map->n == 1)
5829                 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val); 
5830         isl_int_init(v);
5831         isl_int_init(tmp);
5832         fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v); 
5833         for (i = 1; fixed == 1 && i < map->n; ++i) {
5834                 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp); 
5835                 if (fixed == 1 && isl_int_ne(tmp, v))
5836                         fixed = 0;
5837         }
5838         if (val)
5839                 isl_int_set(*val, v);
5840         isl_int_clear(tmp);
5841         isl_int_clear(v);
5842         return fixed;
5843 }
5844
5845 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
5846         unsigned pos, isl_int *val)
5847 {
5848         return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
5849                                                 pos, val);
5850 }
5851
5852 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
5853         isl_int *val)
5854 {
5855         return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
5856 }
5857
5858 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
5859         enum isl_dim_type type, unsigned pos, isl_int *val)
5860 {
5861         if (pos >= isl_basic_map_dim(bmap, type))
5862                 return -1;
5863         return isl_basic_map_fast_has_fixed_var(bmap,
5864                 isl_basic_map_offset(bmap, type) - 1 + pos, val);
5865 }
5866
5867 int isl_map_fast_is_fixed(struct isl_map *map,
5868         enum isl_dim_type type, unsigned pos, isl_int *val)
5869 {
5870         if (pos >= isl_map_dim(map, type))
5871                 return -1;
5872         return isl_map_fast_has_fixed_var(map,
5873                 map_offset(map, type) - 1 + pos, val);
5874 }
5875
5876 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5877  * then return this fixed value in *val.
5878  */
5879 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
5880         isl_int *val)
5881 {
5882         return isl_basic_set_fast_has_fixed_var(bset,
5883                                         isl_basic_set_n_param(bset) + dim, val);
5884 }
5885
5886 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5887  * then return this fixed value in *val.
5888  */
5889 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
5890 {
5891         return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
5892 }
5893
5894 /* Check if input variable in has fixed value and if so and if val is not NULL,
5895  * then return this fixed value in *val.
5896  */
5897 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
5898 {
5899         return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
5900 }
5901
5902 /* Check if dimension dim has an (obvious) fixed lower bound and if so
5903  * and if val is not NULL, then return this lower bound in *val.
5904  */
5905 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
5906         unsigned dim, isl_int *val)
5907 {
5908         int i, i_eq = -1, i_ineq = -1;
5909         isl_int *c;
5910         unsigned total;
5911         unsigned nparam;
5912
5913         if (!bset)
5914                 return -1;
5915         total = isl_basic_set_total_dim(bset);
5916         nparam = isl_basic_set_n_param(bset);
5917         for (i = 0; i < bset->n_eq; ++i) {
5918                 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
5919                         continue;
5920                 if (i_eq != -1)
5921                         return 0;
5922                 i_eq = i;
5923         }
5924         for (i = 0; i < bset->n_ineq; ++i) {
5925                 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
5926                         continue;
5927                 if (i_eq != -1 || i_ineq != -1)
5928                         return 0;
5929                 i_ineq = i;
5930         }
5931         if (i_eq == -1 && i_ineq == -1)
5932                 return 0;
5933         c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
5934         /* The coefficient should always be one due to normalization. */
5935         if (!isl_int_is_one(c[1+nparam+dim]))
5936                 return 0;
5937         if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
5938                 return 0;
5939         if (isl_seq_first_non_zero(c+1+nparam+dim+1,
5940                                         total - nparam - dim - 1) != -1)
5941                 return 0;
5942         if (val)
5943                 isl_int_neg(*val, c[0]);
5944         return 1;
5945 }
5946
5947 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
5948         unsigned dim, isl_int *val)
5949 {
5950         int i;
5951         isl_int v;
5952         isl_int tmp;
5953         int fixed;
5954
5955         if (!set)
5956                 return -1;
5957         if (set->n == 0)
5958                 return 0;
5959         if (set->n == 1)
5960                 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5961                                                                 dim, val);
5962         isl_int_init(v);
5963         isl_int_init(tmp);
5964         fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5965                                                                 dim, &v);
5966         for (i = 1; fixed == 1 && i < set->n; ++i) {
5967                 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
5968                                                                 dim, &tmp);
5969                 if (fixed == 1 && isl_int_ne(tmp, v))
5970                         fixed = 0;
5971         }
5972         if (val)
5973                 isl_int_set(*val, v);
5974         isl_int_clear(tmp);
5975         isl_int_clear(v);
5976         return fixed;
5977 }
5978
5979 struct constraint {
5980         unsigned        size;
5981         isl_int         *c;
5982 };
5983
5984 static int qsort_constraint_cmp(const void *p1, const void *p2)
5985 {
5986         const struct constraint *c1 = (const struct constraint *)p1;
5987         const struct constraint *c2 = (const struct constraint *)p2;
5988         unsigned size = isl_min(c1->size, c2->size);
5989         return isl_seq_cmp(c1->c, c2->c, size);
5990 }
5991
5992 static struct isl_basic_map *isl_basic_map_sort_constraints(
5993         struct isl_basic_map *bmap)
5994 {
5995         int i;
5996         struct constraint *c;
5997         unsigned total;
5998
5999         if (!bmap)
6000                 return NULL;
6001         total = isl_basic_map_total_dim(bmap);
6002         c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
6003         if (!c)
6004                 goto error;
6005         for (i = 0; i < bmap->n_ineq; ++i) {
6006                 c[i].size = total;
6007                 c[i].c = bmap->ineq[i];
6008         }
6009         qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
6010         for (i = 0; i < bmap->n_ineq; ++i)
6011                 bmap->ineq[i] = c[i].c;
6012         free(c);
6013         return bmap;
6014 error:
6015         isl_basic_map_free(bmap);
6016         return NULL;
6017 }
6018
6019 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
6020         __isl_take isl_basic_set *bset)
6021 {
6022         return (struct isl_basic_set *)isl_basic_map_sort_constraints(
6023                                                 (struct isl_basic_map *)bset);
6024 }
6025
6026 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
6027 {
6028         if (!bmap)
6029                 return NULL;
6030         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
6031                 return bmap;
6032         bmap = isl_basic_map_convex_hull(bmap);
6033         bmap = isl_basic_map_sort_constraints(bmap);
6034         ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
6035         return bmap;
6036 }
6037
6038 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
6039 {
6040         return (struct isl_basic_set *)isl_basic_map_normalize(
6041                                                 (struct isl_basic_map *)bset);
6042 }
6043
6044 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
6045         const struct isl_basic_map *bmap2)
6046 {
6047         int i, cmp;
6048         unsigned total;
6049
6050         if (bmap1 == bmap2)
6051                 return 0;
6052         if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
6053                 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
6054         if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
6055                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6056         if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
6057                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
6058         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
6059             ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6060                 return 0;
6061         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
6062                 return 1;
6063         if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
6064                 return -1;
6065         if (bmap1->n_eq != bmap2->n_eq)
6066                 return bmap1->n_eq - bmap2->n_eq;
6067         if (bmap1->n_ineq != bmap2->n_ineq)
6068                 return bmap1->n_ineq - bmap2->n_ineq;
6069         if (bmap1->n_div != bmap2->n_div)
6070                 return bmap1->n_div - bmap2->n_div;
6071         total = isl_basic_map_total_dim(bmap1);
6072         for (i = 0; i < bmap1->n_eq; ++i) {
6073                 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
6074                 if (cmp)
6075                         return cmp;
6076         }
6077         for (i = 0; i < bmap1->n_ineq; ++i) {
6078                 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
6079                 if (cmp)
6080                         return cmp;
6081         }
6082         for (i = 0; i < bmap1->n_div; ++i) {
6083                 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
6084                 if (cmp)
6085                         return cmp;
6086         }
6087         return 0;
6088 }
6089
6090 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
6091         struct isl_basic_map *bmap2)
6092 {
6093         return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
6094 }
6095
6096 int isl_basic_set_fast_is_equal(__isl_keep isl_basic_set *bset1,
6097         __isl_keep isl_basic_set *bset2)
6098 {
6099         return isl_basic_map_fast_is_equal((isl_basic_map *)bset1,
6100                                             (isl_basic_map *)bset2);
6101 }
6102
6103 static int qsort_bmap_cmp(const void *p1, const void *p2)
6104 {
6105         const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
6106         const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
6107
6108         return isl_basic_map_fast_cmp(bmap1, bmap2);
6109 }
6110
6111 /* We normalize in place, but if anything goes wrong we need
6112  * to return NULL, so we need to make sure we don't change the
6113  * meaning of any possible other copies of map.
6114  */
6115 struct isl_map *isl_map_normalize(struct isl_map *map)
6116 {
6117         int i, j;
6118         struct isl_basic_map *bmap;
6119
6120         if (!map)
6121                 return NULL;
6122         if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
6123                 return map;
6124         for (i = 0; i < map->n; ++i) {
6125                 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
6126                 if (!bmap)
6127                         goto error;
6128                 isl_basic_map_free(map->p[i]);
6129                 map->p[i] = bmap;
6130         }
6131         qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
6132         ISL_F_SET(map, ISL_MAP_NORMALIZED);
6133         map = isl_map_remove_empty_parts(map);
6134         if (!map)
6135                 return NULL;
6136         for (i = map->n - 1; i >= 1; --i) {
6137                 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
6138                         continue;
6139                 isl_basic_map_free(map->p[i-1]);
6140                 for (j = i; j < map->n; ++j)
6141                         map->p[j-1] = map->p[j];
6142                 map->n--;
6143         }
6144         return map;
6145 error:
6146         isl_map_free(map);
6147         return NULL;
6148
6149 }
6150
6151 struct isl_set *isl_set_normalize(struct isl_set *set)
6152 {
6153         return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
6154 }
6155
6156 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
6157 {
6158         int i;
6159         int equal;
6160
6161         if (!map1 || !map2)
6162                 return -1;
6163
6164         if (map1 == map2)
6165                 return 1;
6166         if (!isl_dim_equal(map1->dim, map2->dim))
6167                 return 0;
6168
6169         map1 = isl_map_copy(map1);
6170         map2 = isl_map_copy(map2);
6171         map1 = isl_map_normalize(map1);
6172         map2 = isl_map_normalize(map2);
6173         if (!map1 || !map2)
6174                 goto error;
6175         equal = map1->n == map2->n;
6176         for (i = 0; equal && i < map1->n; ++i) {
6177                 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
6178                 if (equal < 0)
6179                         goto error;
6180         }
6181         isl_map_free(map1);
6182         isl_map_free(map2);
6183         return equal;
6184 error:
6185         isl_map_free(map1);
6186         isl_map_free(map2);
6187         return -1;
6188 }
6189
6190 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
6191 {
6192         return isl_map_fast_is_equal((struct isl_map *)set1,
6193                                                 (struct isl_map *)set2);
6194 }
6195
6196 /* Return an interval that ranges from min to max (inclusive)
6197  */
6198 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
6199         isl_int min, isl_int max)
6200 {
6201         int k;
6202         struct isl_basic_set *bset = NULL;
6203
6204         bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
6205         if (!bset)
6206                 goto error;
6207
6208         k = isl_basic_set_alloc_inequality(bset);
6209         if (k < 0)
6210                 goto error;
6211         isl_int_set_si(bset->ineq[k][1], 1);
6212         isl_int_neg(bset->ineq[k][0], min);
6213
6214         k = isl_basic_set_alloc_inequality(bset);
6215         if (k < 0)
6216                 goto error;
6217         isl_int_set_si(bset->ineq[k][1], -1);
6218         isl_int_set(bset->ineq[k][0], max);
6219
6220         return bset;
6221 error:
6222         isl_basic_set_free(bset);
6223         return NULL;
6224 }
6225
6226 /* Return the Cartesian product of the basic sets in list (in the given order).
6227  */
6228 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
6229 {
6230         int i;
6231         unsigned dim;
6232         unsigned nparam;
6233         unsigned extra;
6234         unsigned n_eq;
6235         unsigned n_ineq;
6236         struct isl_basic_set *product = NULL;
6237
6238         if (!list)
6239                 goto error;
6240         isl_assert(list->ctx, list->n > 0, goto error);
6241         isl_assert(list->ctx, list->p[0], goto error);
6242         nparam = isl_basic_set_n_param(list->p[0]);
6243         dim = isl_basic_set_n_dim(list->p[0]);
6244         extra = list->p[0]->n_div;
6245         n_eq = list->p[0]->n_eq;
6246         n_ineq = list->p[0]->n_ineq;
6247         for (i = 1; i < list->n; ++i) {
6248                 isl_assert(list->ctx, list->p[i], goto error);
6249                 isl_assert(list->ctx,
6250                     nparam == isl_basic_set_n_param(list->p[i]), goto error);
6251                 dim += isl_basic_set_n_dim(list->p[i]);
6252                 extra += list->p[i]->n_div;
6253                 n_eq += list->p[i]->n_eq;
6254                 n_ineq += list->p[i]->n_ineq;
6255         }
6256         product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
6257                                         n_eq, n_ineq);
6258         if (!product)
6259                 goto error;
6260         dim = 0;
6261         for (i = 0; i < list->n; ++i) {
6262                 isl_basic_set_add_constraints(product,
6263                                         isl_basic_set_copy(list->p[i]), dim);
6264                 dim += isl_basic_set_n_dim(list->p[i]);
6265         }
6266         isl_basic_set_list_free(list);
6267         return product;
6268 error:
6269         isl_basic_set_free(product);
6270         isl_basic_set_list_free(list);
6271         return NULL;
6272 }
6273
6274 struct isl_basic_map *isl_basic_map_product(
6275                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6276 {
6277         struct isl_dim *dim_result = NULL;
6278         struct isl_basic_map *bmap;
6279         unsigned in1, in2, out1, out2, nparam, total, pos;
6280         struct isl_dim_map *dim_map1, *dim_map2;
6281
6282         if (!bmap1 || !bmap2)
6283                 goto error;
6284
6285         isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
6286                                      bmap2->dim, isl_dim_param), goto error);
6287         dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
6288                                                    isl_dim_copy(bmap2->dim));
6289
6290         in1 = isl_basic_map_n_in(bmap1);
6291         in2 = isl_basic_map_n_in(bmap2);
6292         out1 = isl_basic_map_n_out(bmap1);
6293         out2 = isl_basic_map_n_out(bmap2);
6294         nparam = isl_basic_map_n_param(bmap1);
6295
6296         total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
6297         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
6298         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
6299         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
6300         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
6301         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
6302         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
6303         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
6304         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
6305         isl_dim_map_div(dim_map1, bmap1, pos += out2);
6306         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
6307
6308         bmap = isl_basic_map_alloc_dim(dim_result,
6309                         bmap1->n_div + bmap2->n_div,
6310                         bmap1->n_eq + bmap2->n_eq,
6311                         bmap1->n_ineq + bmap2->n_ineq);
6312         bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
6313         bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
6314         bmap = isl_basic_map_simplify(bmap);
6315         return isl_basic_map_finalize(bmap);
6316 error:
6317         isl_basic_map_free(bmap1);
6318         isl_basic_map_free(bmap2);
6319         return NULL;
6320 }
6321
6322 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
6323  */
6324 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
6325 {
6326         unsigned flags = 0;
6327         struct isl_map *result;
6328         int i, j;
6329
6330         if (!map1 || !map2)
6331                 goto error;
6332
6333         isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
6334                                          map2->dim, isl_dim_param), goto error);
6335
6336         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6337             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6338                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6339
6340         result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
6341                                                    isl_dim_copy(map2->dim)),
6342                                 map1->n * map2->n, flags);
6343         if (!result)
6344                 goto error;
6345         for (i = 0; i < map1->n; ++i)
6346                 for (j = 0; j < map2->n; ++j) {
6347                         struct isl_basic_map *part;
6348                         part = isl_basic_map_product(
6349                                     isl_basic_map_copy(map1->p[i]),
6350                                     isl_basic_map_copy(map2->p[j]));
6351                         if (isl_basic_map_is_empty(part))
6352                                 isl_basic_map_free(part);
6353                         else
6354                                 result = isl_map_add_basic_map(result, part);
6355                         if (!result)
6356                                 goto error;
6357                 }
6358         isl_map_free(map1);
6359         isl_map_free(map2);
6360         return result;
6361 error:
6362         isl_map_free(map1);
6363         isl_map_free(map2);
6364         return NULL;
6365 }
6366
6367 /* Given two set A and B, construct its Cartesian product A x B.
6368  */
6369 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
6370 {
6371         return (struct isl_set *)isl_map_product((struct isl_map *)set1,
6372                                                  (struct isl_map *)set2);
6373 }
6374
6375 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
6376 {
6377         int i;
6378         uint32_t hash = isl_hash_init();
6379         unsigned total;
6380
6381         if (!bset)
6382                 return 0;
6383         bset = isl_basic_set_copy(bset);
6384         bset = isl_basic_set_normalize(bset);
6385         if (!bset)
6386                 return 0;
6387         total = isl_basic_set_total_dim(bset);
6388         isl_hash_byte(hash, bset->n_eq & 0xFF);
6389         for (i = 0; i < bset->n_eq; ++i) {
6390                 uint32_t c_hash;
6391                 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
6392                 isl_hash_hash(hash, c_hash);
6393         }
6394         isl_hash_byte(hash, bset->n_ineq & 0xFF);
6395         for (i = 0; i < bset->n_ineq; ++i) {
6396                 uint32_t c_hash;
6397                 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
6398                 isl_hash_hash(hash, c_hash);
6399         }
6400         isl_hash_byte(hash, bset->n_div & 0xFF);
6401         for (i = 0; i < bset->n_div; ++i) {
6402                 uint32_t c_hash;
6403                 if (isl_int_is_zero(bset->div[i][0]))
6404                         continue;
6405                 isl_hash_byte(hash, i & 0xFF);
6406                 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
6407                 isl_hash_hash(hash, c_hash);
6408         }
6409         isl_basic_set_free(bset);
6410         return hash;
6411 }
6412
6413 uint32_t isl_set_get_hash(struct isl_set *set)
6414 {
6415         int i;
6416         uint32_t hash;
6417
6418         if (!set)
6419                 return 0;
6420         set = isl_set_copy(set);
6421         set = isl_set_normalize(set);
6422         if (!set)
6423                 return 0;
6424
6425         hash = isl_hash_init();
6426         for (i = 0; i < set->n; ++i) {
6427                 uint32_t bset_hash;
6428                 bset_hash = isl_basic_set_get_hash(set->p[i]);
6429                 isl_hash_hash(hash, bset_hash);
6430         }
6431                 
6432         isl_set_free(set);
6433
6434         return hash;
6435 }
6436
6437 /* Check if the value for dimension dim is completely determined
6438  * by the values of the other parameters and variables.
6439  * That is, check if dimension dim is involved in an equality.
6440  */
6441 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
6442 {
6443         int i;
6444         unsigned nparam;
6445
6446         if (!bset)
6447                 return -1;
6448         nparam = isl_basic_set_n_param(bset);
6449         for (i = 0; i < bset->n_eq; ++i)
6450                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
6451                         return 1;
6452         return 0;
6453 }
6454
6455 /* Check if the value for dimension dim is completely determined
6456  * by the values of the other parameters and variables.
6457  * That is, check if dimension dim is involved in an equality
6458  * for each of the subsets.
6459  */
6460 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
6461 {
6462         int i;
6463
6464         if (!set)
6465                 return -1;
6466         for (i = 0; i < set->n; ++i) {
6467                 int unique;
6468                 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
6469                 if (unique != 1)
6470                         return unique;
6471         }
6472         return 1;
6473 }
6474
6475 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
6476         int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
6477 {
6478         int i;
6479
6480         if (!map)
6481                 return -1;
6482
6483         for (i = 0; i < map->n; ++i)
6484                 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
6485                         return -1;
6486
6487         return 0;
6488 }
6489
6490 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
6491         int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
6492 {
6493         int i;
6494
6495         if (!set)
6496                 return -1;
6497
6498         for (i = 0; i < set->n; ++i)
6499                 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
6500                         return -1;
6501
6502         return 0;
6503 }
6504
6505 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
6506 {
6507         struct isl_dim *dim;
6508
6509         if (!bset)
6510                 return NULL;
6511
6512         if (bset->n_div == 0)
6513                 return bset;
6514
6515         bset = isl_basic_set_cow(bset);
6516         if (!bset)
6517                 return NULL;
6518
6519         dim = isl_basic_set_get_dim(bset);
6520         dim = isl_dim_add(dim, isl_dim_set, bset->n_div);
6521         if (!dim)
6522                 goto error;
6523         isl_dim_free(bset->dim);
6524         bset->dim = dim;
6525         bset->n_div = 0;
6526
6527         bset = isl_basic_set_finalize(bset);
6528
6529         return bset;
6530 error:
6531         isl_basic_set_free(bset);
6532         return NULL;
6533 }
6534
6535 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
6536 {
6537         int i;
6538         struct isl_dim *dim;
6539         unsigned n_div;
6540
6541         set = isl_set_align_divs(set);
6542
6543         if (!set)
6544                 return NULL;
6545         if (set->n == 0 || set->p[0]->n_div == 0)
6546                 return set;
6547
6548         set = isl_set_cow(set);
6549         if (!set)
6550                 return NULL;
6551
6552         n_div = set->p[0]->n_div;
6553         dim = isl_set_get_dim(set);
6554         dim = isl_dim_add(dim, isl_dim_set, n_div);
6555         if (!dim)
6556                 goto error;
6557         isl_dim_free(set->dim);
6558         set->dim = dim;
6559
6560         for (i = 0; i < set->n; ++i) {
6561                 set->p[i] = isl_basic_set_lift(set->p[i]);
6562                 if (!set->p[i])
6563                         goto error;
6564         }
6565
6566         return set;
6567 error:
6568         isl_set_free(set);
6569         return NULL;
6570 }
6571
6572 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
6573 {
6574         struct isl_dim *dim;
6575         struct isl_basic_map *bmap;
6576         unsigned n_set;
6577         unsigned n_div;
6578         unsigned n_param;
6579         unsigned total;
6580         int i, k, l;
6581
6582         set = isl_set_align_divs(set);
6583
6584         if (!set)
6585                 return NULL;
6586
6587         dim = isl_set_get_dim(set);
6588         if (set->n == 0 || set->p[0]->n_div == 0) {
6589                 isl_set_free(set);
6590                 return isl_map_identity(dim);
6591         }
6592
6593         n_div = set->p[0]->n_div;
6594         dim = isl_dim_map(dim);
6595         n_param = isl_dim_size(dim, isl_dim_param);
6596         n_set = isl_dim_size(dim, isl_dim_in);
6597         dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
6598         bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
6599         for (i = 0; i < n_set; ++i)
6600                 bmap = var_equal(bmap, i);
6601
6602         total = n_param + n_set + n_set + n_div;
6603         for (i = 0; i < n_div; ++i) {
6604                 k = isl_basic_map_alloc_inequality(bmap);
6605                 if (k < 0)
6606                         goto error;
6607                 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
6608                 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
6609                 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
6610                             set->p[0]->div[i]+1+1+n_param, n_set + n_div);
6611                 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
6612                             set->p[0]->div[i][0]);
6613
6614                 l = isl_basic_map_alloc_inequality(bmap);
6615                 if (l < 0)
6616                         goto error;
6617                 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
6618                 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
6619                             set->p[0]->div[i][0]);
6620                 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
6621         }
6622
6623         isl_set_free(set);
6624         return isl_map_from_basic_map(bmap);
6625 error:
6626         isl_set_free(set);
6627         isl_basic_map_free(bmap);
6628         return NULL;
6629 }
6630
6631 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
6632 {
6633         unsigned dim;
6634         int size = 0;
6635
6636         if (!bset)
6637                 return -1;
6638
6639         dim = isl_basic_set_total_dim(bset);
6640         size += bset->n_eq * (1 + dim);
6641         size += bset->n_ineq * (1 + dim);
6642         size += bset->n_div * (2 + dim);
6643
6644         return size;
6645 }
6646
6647 int isl_set_size(__isl_keep isl_set *set)
6648 {
6649         int i;
6650         int size = 0;
6651
6652         if (!set)
6653                 return -1;
6654
6655         for (i = 0; i < set->n; ++i)
6656                 size += isl_basic_set_size(set->p[i]);
6657
6658         return size;
6659 }
6660
6661 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
6662         enum isl_dim_type type, unsigned pos)
6663 {
6664         int i;
6665         int lower, upper;
6666
6667         if (!bmap)
6668                 return -1;
6669
6670         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
6671
6672         pos += isl_basic_map_offset(bmap, type);
6673
6674         for (i = 0; i < bmap->n_eq; ++i)
6675                 if (!isl_int_is_zero(bmap->eq[i][pos]))
6676                         return 1;
6677
6678         lower = upper = 0;
6679         for (i = 0; i < bmap->n_ineq; ++i) {
6680                 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
6681                 if (sgn > 0)
6682                         lower = 1;
6683                 if (sgn < 0)
6684                         upper = 1;
6685         }
6686
6687         return lower && upper;
6688 }
6689
6690 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
6691         enum isl_dim_type type, unsigned pos)
6692 {
6693         int i;
6694
6695         if (!map)
6696                 return -1;
6697
6698         for (i = 0; i < map->n; ++i) {
6699                 int bounded;
6700                 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
6701                 if (bounded < 0 || !bounded)
6702                         return bounded;
6703         }
6704
6705         return 1;
6706 }
6707
6708 /* Return 1 if the specified dim is involved in both an upper bound
6709  * and a lower bound.
6710  */
6711 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
6712         enum isl_dim_type type, unsigned pos)
6713 {
6714         return isl_map_dim_is_bounded((isl_map *)set, type, pos);
6715 }
6716
6717 /* For each of the "n" variables starting at "first", determine
6718  * the sign of the variable and put the results in the first "n"
6719  * elements of the array "signs".
6720  * Sign
6721  *      1 means that the variable is non-negative
6722  *      -1 means that the variable is non-positive
6723  *      0 means the variable attains both positive and negative values.
6724  */
6725 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
6726         unsigned first, unsigned n, int *signs)
6727 {
6728         isl_vec *bound = NULL;
6729         struct isl_tab *tab = NULL;
6730         struct isl_tab_undo *snap;
6731         int i;
6732
6733         if (!bset || !signs)
6734                 return -1;
6735
6736         bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
6737         tab = isl_tab_from_basic_set(bset);
6738         if (!bound || !tab)
6739                 goto error;
6740
6741         isl_seq_clr(bound->el, bound->size);
6742         isl_int_set_si(bound->el[0], -1);
6743
6744         snap = isl_tab_snap(tab);
6745         for (i = 0; i < n; ++i) {
6746                 int empty;
6747
6748                 isl_int_set_si(bound->el[1 + first + i], -1);
6749                 if (isl_tab_add_ineq(tab, bound->el) < 0)
6750                         goto error;
6751                 empty = tab->empty;
6752                 isl_int_set_si(bound->el[1 + first + i], 0);
6753                 if (isl_tab_rollback(tab, snap) < 0)
6754                         goto error;
6755
6756                 if (empty) {
6757                         signs[i] = 1;
6758                         continue;
6759                 }
6760
6761                 isl_int_set_si(bound->el[1 + first + i], 1);
6762                 if (isl_tab_add_ineq(tab, bound->el) < 0)
6763                         goto error;
6764                 empty = tab->empty;
6765                 isl_int_set_si(bound->el[1 + first + i], 0);
6766                 if (isl_tab_rollback(tab, snap) < 0)
6767                         goto error;
6768
6769                 signs[i] = empty ? -1 : 0;
6770         }
6771
6772         isl_tab_free(tab);
6773         isl_vec_free(bound);
6774         return 0;
6775 error:
6776         isl_tab_free(tab);
6777         isl_vec_free(bound);
6778         return -1;
6779 }
6780
6781 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
6782         enum isl_dim_type type, unsigned first, unsigned n, int *signs)
6783 {
6784         if (!bset || !signs)
6785                 return -1;
6786         isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
6787                 return -1);
6788
6789         first += pos(bset->dim, type) - 1;
6790         return isl_basic_set_vars_get_sign(bset, first, n, signs);
6791 }
6792
6793 /* Check if the given map is single-valued.
6794  * We simply compute
6795  *
6796  *      M \circ M^-1
6797  *
6798  * and check if the result is a subset of the identity mapping.
6799  */
6800 int isl_map_is_single_valued(__isl_keep isl_map *map)
6801 {
6802         isl_map *test;
6803         isl_map *id;
6804         int sv;
6805
6806         test = isl_map_reverse(isl_map_copy(map));
6807         test = isl_map_apply_range(test, isl_map_copy(map));
6808
6809         id = isl_map_identity(isl_dim_range(isl_map_get_dim(map)));
6810
6811         sv = isl_map_is_subset(test, id);
6812
6813         isl_map_free(test);
6814         isl_map_free(id);
6815
6816         return sv;
6817 }
6818
6819 int isl_set_is_singleton(__isl_keep isl_set *set)
6820 {
6821         return isl_map_is_single_valued((isl_map *)set);
6822 }
6823
6824 int isl_map_is_translation(__isl_keep isl_map *map)
6825 {
6826         int ok;
6827         isl_set *delta;
6828
6829         delta = isl_map_deltas(isl_map_copy(map));
6830         ok = isl_set_is_singleton(delta);
6831         isl_set_free(delta);
6832
6833         return ok;
6834 }
6835
6836 static int unique(isl_int *p, unsigned pos, unsigned len)
6837 {
6838         if (isl_seq_first_non_zero(p, pos) != -1)
6839                 return 0;
6840         if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
6841                 return 0;
6842         return 1;
6843 }
6844
6845 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
6846 {
6847         int i, j;
6848         unsigned nvar;
6849         unsigned ovar;
6850
6851         if (!bset)
6852                 return -1;
6853
6854         if (isl_basic_set_dim(bset, isl_dim_div) != 0)
6855                 return 0;
6856
6857         nvar = isl_basic_set_dim(bset, isl_dim_set);
6858         ovar = isl_dim_offset(bset->dim, isl_dim_set);
6859         for (j = 0; j < nvar; ++j) {
6860                 int lower = 0, upper = 0;
6861                 for (i = 0; i < bset->n_eq; ++i) {
6862                         if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
6863                                 continue;
6864                         if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
6865                                 return 0;
6866                         break;
6867                 }
6868                 if (i < bset->n_eq)
6869                         continue;
6870                 for (i = 0; i < bset->n_ineq; ++i) {
6871                         if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
6872                                 continue;
6873                         if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
6874                                 return 0;
6875                         if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
6876                                 lower = 1;
6877                         else
6878                                 upper = 1;
6879                 }
6880                 if (!lower || !upper)
6881                         return 0;
6882         }
6883
6884         return 1;
6885 }
6886
6887 int isl_set_is_box(__isl_keep isl_set *set)
6888 {
6889         if (!set)
6890                 return -1;
6891         if (set->n != 1)
6892                 return 0;
6893
6894         return isl_basic_set_is_box(set->p[0]);
6895 }