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