add isl_basic_set_free_equality
[platform/upstream/isl.git] / isl_map.c
1 #include <string.h>
2 #include <strings.h>
3 #include "isl_ctx.h"
4 #include "isl_blk.h"
5 #include "isl_dim.h"
6 #include "isl_list.h"
7 #include "isl_lp.h"
8 #include "isl_seq.h"
9 #include "isl_set.h"
10 #include "isl_map.h"
11 #include "isl_map_private.h"
12 #include "isl_map_piplib.h"
13 #include "isl_sample.h"
14 #include "isl_vec.h"
15
16 /* Maps dst positions to src positions */
17 struct isl_dim_map {
18         unsigned len;
19         int pos[1];
20 };
21
22 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
23 {
24         int i;
25         struct isl_dim_map *dim_map;
26         dim_map = isl_alloc(ctx, struct isl_dim_map,
27                                 sizeof(struct isl_dim_map) + len * sizeof(int));
28         if (!dim_map)
29                 return NULL;
30         dim_map->len = 1 + len;
31         dim_map->pos[0] = 0;
32         for (i = 0; i < len; ++i)
33                 dim_map->pos[1 + i] = -1;
34         return dim_map;
35 }
36
37 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
38 {
39         switch (type) {
40         case isl_dim_param:     return dim->nparam;
41         case isl_dim_in:        return dim->n_in;
42         case isl_dim_out:       return dim->n_out;
43         }
44 }
45
46 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
47 {
48         switch (type) {
49         case isl_dim_param:     return 1;
50         case isl_dim_in:        return 1 + dim->nparam;
51         case isl_dim_out:       return 1 + dim->nparam + dim->n_in;
52         }
53 }
54
55 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
56                 enum isl_dim_type type, unsigned dst_pos)
57 {
58         int i;
59         unsigned src_pos;
60
61         if (!dim_map || !dim)
62                 return;
63         
64         src_pos = pos(dim, type);
65         for (i = 0; i < n(dim, type); ++i)
66                 dim_map->pos[1 + dst_pos + i] = src_pos + i;
67 }
68
69 static void isl_dim_map_div(struct isl_dim_map *dim_map,
70                 struct isl_basic_map *bmap, unsigned dst_pos)
71 {
72         int i;
73         unsigned src_pos;
74
75         if (!dim_map || !bmap)
76                 return;
77         
78         src_pos = 1 + isl_dim_total(bmap->dim);
79         for (i = 0; i < bmap->n_div; ++i)
80                 dim_map->pos[1 + dst_pos + i] = src_pos + i;
81 }
82
83 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
84 {
85         int i;
86
87         for (i = 0; i < dim_map->len; ++i)
88                 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
89         fprintf(stderr, "\n");
90 }
91
92 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
93                                 enum isl_dim_type type)
94 {
95         struct isl_dim *dim = bmap->dim;
96         switch (type) {
97         case isl_dim_param:
98         case isl_dim_in:
99         case isl_dim_out:       return isl_dim_size(bmap->dim, type);
100         case isl_dim_div:       return bmap->n_div;
101         case isl_dim_all:       return isl_basic_map_total_dim(bmap);
102         }
103 }
104
105 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
106 {
107         return n(map->dim, type);
108 }
109
110 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
111 {
112         return n(set->dim, type);
113 }
114
115 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
116                                         enum isl_dim_type type)
117 {
118         struct isl_dim *dim = bmap->dim;
119         switch (type) {
120         case isl_dim_param:     return 1;
121         case isl_dim_in:        return 1 + dim->nparam;
122         case isl_dim_out:       return 1 + dim->nparam + dim->n_in;
123         case isl_dim_div:       return 1 + dim->nparam + dim->n_in + dim->n_out;
124         }
125 }
126
127 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
128 {
129         return pos(map->dim, type);
130 }
131
132 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
133                                 enum isl_dim_type type)
134 {
135         return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
136 }
137
138 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
139 {
140         return bset->dim->n_out;
141 }
142
143 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
144 {
145         return bset->dim->nparam;
146 }
147
148 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
149 {
150         return isl_dim_total(bset->dim) + bset->n_div;
151 }
152
153 unsigned isl_set_n_dim(const struct isl_set *set)
154 {
155         return set->dim->n_out;
156 }
157
158 unsigned isl_set_n_param(const struct isl_set *set)
159 {
160         return set->dim->nparam;
161 }
162
163 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
164 {
165         return bmap->dim->n_in;
166 }
167
168 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
169 {
170         return bmap->dim->n_out;
171 }
172
173 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
174 {
175         return bmap->dim->nparam;
176 }
177
178 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
179 {
180         return bmap->n_div;
181 }
182
183 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
184 {
185         return isl_dim_total(bmap->dim) + bmap->n_div;
186 }
187
188 unsigned isl_map_n_in(const struct isl_map *map)
189 {
190         return map->dim->n_in;
191 }
192
193 unsigned isl_map_n_out(const struct isl_map *map)
194 {
195         return map->dim->n_out;
196 }
197
198 unsigned isl_map_n_param(const struct isl_map *map)
199 {
200         return map->dim->nparam;
201 }
202
203 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
204 {
205         return map->dim->n_in == set->dim->n_out &&
206                map->dim->nparam == set->dim->nparam;
207 }
208
209 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
210                 struct isl_basic_set *bset)
211 {
212         return bmap->dim->n_in == bset->dim->n_out &&
213                bmap->dim->nparam == bset->dim->nparam;
214 }
215
216 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
217                 struct isl_basic_set *bset)
218 {
219         return bmap->dim->n_out == bset->dim->n_out &&
220                bmap->dim->nparam == bset->dim->nparam;
221 }
222
223 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
224                 struct isl_basic_map *bmap, unsigned extra,
225                 unsigned n_eq, unsigned n_ineq)
226 {
227         int i;
228         size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
229
230         bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
231         if (isl_blk_is_error(bmap->block)) {
232                 free(bmap);
233                 return NULL;
234         }
235
236         bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
237         if (!bmap->ineq) {
238                 isl_blk_free(ctx, bmap->block);
239                 free(bmap);
240                 return NULL;
241         }
242
243         if (extra == 0) {
244                 bmap->block2 = isl_blk_empty();
245                 bmap->div = NULL;
246         } else {
247                 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
248                 if (isl_blk_is_error(bmap->block2)) {
249                         free(bmap->ineq);
250                         isl_blk_free(ctx, bmap->block);
251                         free(bmap);
252                         return NULL;
253                 }
254
255                 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
256                 if (!bmap->div) {
257                         isl_blk_free(ctx, bmap->block2);
258                         free(bmap->ineq);
259                         isl_blk_free(ctx, bmap->block);
260                         free(bmap);
261                         return NULL;
262                 }
263         }
264
265         for (i = 0; i < n_ineq + n_eq; ++i)
266                 bmap->ineq[i] = bmap->block.data + i * row_size;
267
268         for (i = 0; i < extra; ++i)
269                 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
270
271         bmap->ctx = ctx;
272         isl_ctx_ref(ctx);
273         bmap->ref = 1;
274         bmap->flags = 0;
275         bmap->c_size = n_eq + n_ineq;
276         bmap->eq = bmap->ineq + n_ineq;
277         bmap->extra = extra;
278         bmap->n_eq = 0;
279         bmap->n_ineq = 0;
280         bmap->n_div = 0;
281         bmap->sample = NULL;
282
283         return bmap;
284 error:
285         isl_basic_map_free(bmap);
286         return NULL;
287 }
288
289 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
290                 unsigned nparam, unsigned dim, unsigned extra,
291                 unsigned n_eq, unsigned n_ineq)
292 {
293         struct isl_basic_map *bmap;
294         bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
295         return (struct isl_basic_set *)bmap;
296 }
297
298 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
299                 unsigned extra, unsigned n_eq, unsigned n_ineq)
300 {
301         struct isl_basic_map *bmap;
302         if (!dim)
303                 return NULL;
304         isl_assert(dim->ctx, dim->n_in == 0, return NULL);
305         bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
306         return (struct isl_basic_set *)bmap;
307 }
308
309 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
310                 unsigned extra, unsigned n_eq, unsigned n_ineq)
311 {
312         struct isl_basic_map *bmap;
313
314         if (!dim)
315                 return NULL;
316         bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
317         if (!bmap)
318                 goto error;
319         bmap->dim = dim;
320
321         return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
322 error:
323         isl_dim_free(dim);
324         return NULL;
325 }
326
327 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
328                 unsigned nparam, unsigned in, unsigned out, unsigned extra,
329                 unsigned n_eq, unsigned n_ineq)
330 {
331         struct isl_basic_map *bmap;
332         struct isl_dim *dim;
333
334         dim = isl_dim_alloc(ctx, nparam, in, out);
335         if (!dim)
336                 return NULL;
337
338         bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
339         return bmap;
340 }
341
342 static void dup_constraints(
343                 struct isl_basic_map *dst, struct isl_basic_map *src)
344 {
345         int i;
346         unsigned total = isl_basic_map_total_dim(src);
347
348         for (i = 0; i < src->n_eq; ++i) {
349                 int j = isl_basic_map_alloc_equality(dst);
350                 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
351         }
352
353         for (i = 0; i < src->n_ineq; ++i) {
354                 int j = isl_basic_map_alloc_inequality(dst);
355                 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
356         }
357
358         for (i = 0; i < src->n_div; ++i) {
359                 int j = isl_basic_map_alloc_div(dst);
360                 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
361         }
362         ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
363 }
364
365 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
366 {
367         struct isl_basic_map *dup;
368
369         if (!bmap)
370                 return NULL;
371         dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
372                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
373         if (!dup)
374                 return NULL;
375         dup_constraints(dup, bmap);
376         dup->flags = bmap->flags;
377         dup->sample = isl_vec_copy(bmap->ctx, bmap->sample);
378         return dup;
379 }
380
381 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
382 {
383         struct isl_basic_map *dup;
384
385         dup = isl_basic_map_dup((struct isl_basic_map *)bset);
386         return (struct isl_basic_set *)dup;
387 }
388
389 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
390 {
391         if (!bset)
392                 return NULL;
393
394         if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
395                 bset->ref++;
396                 return bset;
397         }
398         return isl_basic_set_dup(bset);
399 }
400
401 struct isl_set *isl_set_copy(struct isl_set *set)
402 {
403         if (!set)
404                 return NULL;
405
406         set->ref++;
407         return set;
408 }
409
410 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
411 {
412         if (!bmap)
413                 return NULL;
414
415         if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
416                 bmap->ref++;
417                 return bmap;
418         }
419         return isl_basic_map_dup(bmap);
420 }
421
422 struct isl_map *isl_map_copy(struct isl_map *map)
423 {
424         if (!map)
425                 return NULL;
426
427         map->ref++;
428         return map;
429 }
430
431 void isl_basic_map_free(struct isl_basic_map *bmap)
432 {
433         if (!bmap)
434                 return;
435
436         if (--bmap->ref > 0)
437                 return;
438
439         isl_ctx_deref(bmap->ctx);
440         free(bmap->div);
441         isl_blk_free(bmap->ctx, bmap->block2);
442         free(bmap->ineq);
443         isl_blk_free(bmap->ctx, bmap->block);
444         isl_vec_free(bmap->ctx, bmap->sample);
445         isl_dim_free(bmap->dim);
446         free(bmap);
447 }
448
449 void isl_basic_set_free(struct isl_basic_set *bset)
450 {
451         isl_basic_map_free((struct isl_basic_map *)bset);
452 }
453
454 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
455 {
456         return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
457 }
458
459 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
460 {
461         struct isl_ctx *ctx;
462         if (!bmap)
463                 return -1;
464         ctx = bmap->ctx;
465         isl_assert(ctx, room_for_con(bmap, 1), return -1);
466         isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
467                         return -1);
468         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
469         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
470         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
471         ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
472         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
473         if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
474                 isl_int *t;
475                 int j = isl_basic_map_alloc_inequality(bmap);
476                 if (j < 0)
477                         return -1;
478                 t = bmap->ineq[j];
479                 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
480                 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
481                 bmap->eq[-1] = t;
482                 bmap->n_eq++;
483                 bmap->n_ineq--;
484                 bmap->eq--;
485                 return 0;
486         }
487         isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
488                       bmap->extra - bmap->n_div);
489         return bmap->n_eq++;
490 }
491
492 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
493 {
494         return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
495 }
496
497 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
498 {
499         if (!bmap)
500                 return -1;
501         isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
502         bmap->n_eq -= n;
503         return 0;
504 }
505
506 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
507 {
508         return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
509 }
510
511 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
512 {
513         isl_int *t;
514         if (!bmap)
515                 return -1;
516         isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
517
518         if (pos != bmap->n_eq - 1) {
519                 t = bmap->eq[pos];
520                 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
521                 bmap->eq[bmap->n_eq - 1] = t;
522         }
523         bmap->n_eq--;
524         return 0;
525 }
526
527 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
528 {
529         return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
530 }
531
532 void isl_basic_map_inequality_to_equality(
533                 struct isl_basic_map *bmap, unsigned pos)
534 {
535         isl_int *t;
536
537         t = bmap->ineq[pos];
538         bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
539         bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
540         bmap->eq[-1] = t;
541         bmap->n_eq++;
542         bmap->n_ineq--;
543         bmap->eq--;
544         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
545         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
546         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
547         ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
548 }
549
550 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
551 {
552         return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
553 }
554
555 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
556 {
557         struct isl_ctx *ctx;
558         if (!bmap)
559                 return -1;
560         ctx = bmap->ctx;
561         isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
562         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
563         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
564         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
565         ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
566         isl_seq_clr(bmap->ineq[bmap->n_ineq] +
567                       1 + isl_basic_map_total_dim(bmap),
568                       bmap->extra - bmap->n_div);
569         return bmap->n_ineq++;
570 }
571
572 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
573 {
574         return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
575 }
576
577 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
578 {
579         if (!bmap)
580                 return -1;
581         isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
582         bmap->n_ineq -= n;
583         return 0;
584 }
585
586 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
587 {
588         return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
589 }
590
591 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
592 {
593         isl_int *t;
594         if (!bmap)
595                 return -1;
596         isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
597
598         if (pos != bmap->n_ineq - 1) {
599                 t = bmap->ineq[pos];
600                 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
601                 bmap->ineq[bmap->n_ineq - 1] = t;
602                 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
603         }
604         bmap->n_ineq--;
605         return 0;
606 }
607
608 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
609 {
610         return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
611 }
612
613 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
614 {
615         if (!bmap)
616                 return -1;
617         isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
618         isl_seq_clr(bmap->div[bmap->n_div] +
619                       1 + 1 + isl_basic_map_total_dim(bmap),
620                       bmap->extra - bmap->n_div);
621         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
622         return bmap->n_div++;
623 }
624
625 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
626 {
627         return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
628 }
629
630 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
631 {
632         if (!bmap)
633                 return -1;
634         isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
635         bmap->n_div -= n;
636         return 0;
637 }
638
639 /* Copy constraint from src to dst, putting the vars of src at offset
640  * dim_off in dst and the divs of src at offset div_off in dst.
641  * If both sets are actually map, then dim_off applies to the input
642  * variables.
643  */
644 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
645                             struct isl_basic_map *src_map, isl_int *src,
646                             unsigned in_off, unsigned out_off, unsigned div_off)
647 {
648         unsigned src_nparam = isl_basic_map_n_param(src_map);
649         unsigned dst_nparam = isl_basic_map_n_param(dst_map);
650         unsigned src_in = isl_basic_map_n_in(src_map);
651         unsigned dst_in = isl_basic_map_n_in(dst_map);
652         unsigned src_out = isl_basic_map_n_out(src_map);
653         unsigned dst_out = isl_basic_map_n_out(dst_map);
654         isl_int_set(dst[0], src[0]);
655         isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
656         if (dst_nparam > src_nparam)
657                 isl_seq_clr(dst+1+src_nparam,
658                                 dst_nparam - src_nparam);
659         isl_seq_clr(dst+1+dst_nparam, in_off);
660         isl_seq_cpy(dst+1+dst_nparam+in_off,
661                     src+1+src_nparam,
662                     isl_min(dst_in-in_off, src_in));
663         if (dst_in-in_off > src_in)
664                 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
665                                 dst_in - in_off - src_in);
666         isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
667         isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
668                     src+1+src_nparam+src_in,
669                     isl_min(dst_out-out_off, src_out));
670         if (dst_out-out_off > src_out)
671                 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
672                                 dst_out - out_off - src_out);
673         isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
674         isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
675                     src+1+src_nparam+src_in+src_out,
676                     isl_min(dst_map->extra-div_off, src_map->n_div));
677         if (dst_map->n_div-div_off > src_map->n_div)
678                 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
679                                 div_off+src_map->n_div,
680                                 dst_map->n_div - div_off - src_map->n_div);
681 }
682
683 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
684                      struct isl_basic_map *src_map, isl_int *src,
685                      unsigned in_off, unsigned out_off, unsigned div_off)
686 {
687         isl_int_set(dst[0], src[0]);
688         copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
689 }
690
691 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
692                 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
693 {
694         int i;
695         unsigned div_off;
696
697         if (!bmap1 || !bmap2)
698                 goto error;
699
700         div_off = bmap1->n_div;
701
702         for (i = 0; i < bmap2->n_eq; ++i) {
703                 int i1 = isl_basic_map_alloc_equality(bmap1);
704                 if (i1 < 0)
705                         goto error;
706                 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
707                                 i_pos, o_pos, div_off);
708         }
709
710         for (i = 0; i < bmap2->n_ineq; ++i) {
711                 int i1 = isl_basic_map_alloc_inequality(bmap1);
712                 if (i1 < 0)
713                         goto error;
714                 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
715                                 i_pos, o_pos, div_off);
716         }
717
718         for (i = 0; i < bmap2->n_div; ++i) {
719                 int i1 = isl_basic_map_alloc_div(bmap1);
720                 if (i1 < 0)
721                         goto error;
722                 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
723                          i_pos, o_pos, div_off);
724         }
725
726         isl_basic_map_free(bmap2);
727
728         return bmap1;
729
730 error:
731         isl_basic_map_free(bmap1);
732         isl_basic_map_free(bmap2);
733         return NULL;
734 }
735
736 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
737                                         struct isl_dim_map *dim_map)
738 {
739         int i;
740
741         for (i = 0; i < dim_map->len; ++i) {
742                 if (dim_map->pos[i] < 0)
743                         isl_int_set_si(dst[i], 0);
744                 else
745                         isl_int_set(dst[i], src[dim_map->pos[i]]);
746         }
747 }
748
749 static void copy_div_dim_map(isl_int *dst, isl_int *src,
750                                         struct isl_dim_map *dim_map)
751 {
752         isl_int_set(dst[0], src[0]);
753         copy_constraint_dim_map(dst+1, src+1, dim_map);
754 }
755
756 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
757                 struct isl_basic_map *src, struct isl_dim_map *dim_map)
758 {
759         int i;
760
761         if (!src || !dst || !dim_map)
762                 goto error;
763
764         for (i = 0; i < src->n_eq; ++i) {
765                 int i1 = isl_basic_map_alloc_equality(dst);
766                 if (i1 < 0)
767                         goto error;
768                 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
769         }
770
771         for (i = 0; i < src->n_ineq; ++i) {
772                 int i1 = isl_basic_map_alloc_inequality(dst);
773                 if (i1 < 0)
774                         goto error;
775                 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
776         }
777
778         for (i = 0; i < src->n_div; ++i) {
779                 int i1 = isl_basic_map_alloc_div(dst);
780                 if (i1 < 0)
781                         goto error;
782                 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
783         }
784
785         free(dim_map);
786         isl_basic_map_free(src);
787
788         return dst;
789 error:
790         free(dim_map);
791         isl_basic_map_free(src);
792         isl_basic_map_free(dst);
793         return NULL;
794 }
795
796 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
797                 struct isl_basic_set *bset2, unsigned pos)
798 {
799         return (struct isl_basic_set *)
800                 add_constraints((struct isl_basic_map *)bset1,
801                                 (struct isl_basic_map *)bset2, 0, pos);
802 }
803
804 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
805                 struct isl_dim *dim, unsigned extra,
806                 unsigned n_eq, unsigned n_ineq)
807 {
808         struct isl_basic_map *ext;
809         unsigned flags;
810         int dims_ok;
811
812         if (!dim)
813                 goto error;
814
815         if (!base)
816                 goto error;
817
818         dims_ok = isl_dim_equal(base->dim, dim) &&
819                   base->extra >= base->n_div + extra;
820
821         if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
822                        room_for_ineq(base, n_ineq)) {
823                 isl_dim_free(dim);
824                 return base;
825         }
826
827         isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
828         isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
829         isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
830         extra += base->extra;
831         n_eq += base->n_eq;
832         n_ineq += base->n_ineq;
833
834         ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
835         dim = NULL;
836         if (!ext)
837                 goto error;
838
839         flags = base->flags;
840         ext = add_constraints(ext, base, 0, 0);
841         if (ext) {
842                 ext->flags = flags;
843                 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
844         }
845
846         return ext;
847
848 error:
849         isl_dim_free(dim);
850         isl_basic_map_free(base);
851         return NULL;
852 }
853
854 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
855                 struct isl_dim *dim, unsigned extra,
856                 unsigned n_eq, unsigned n_ineq)
857 {
858         return (struct isl_basic_set *)
859                 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
860                                                         extra, n_eq, n_ineq);
861 }
862
863 struct isl_basic_map *isl_basic_map_extend_constraints(
864                 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
865 {
866         if (!base)
867                 return NULL;
868         return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
869                                         0, n_eq, n_ineq);
870 }
871
872 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
873                 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
874                 unsigned n_eq, unsigned n_ineq)
875 {
876         struct isl_basic_map *bmap;
877         struct isl_dim *dim;
878
879         if (!base)
880                 return NULL;
881         dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
882         if (!dim)
883                 return NULL;
884
885         bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
886         return bmap;
887 }
888
889 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
890                 unsigned nparam, unsigned dim, unsigned extra,
891                 unsigned n_eq, unsigned n_ineq)
892 {
893         return (struct isl_basic_set *)
894                 isl_basic_map_extend((struct isl_basic_map *)base,
895                                         nparam, 0, dim, extra, n_eq, n_ineq);
896 }
897
898 struct isl_basic_set *isl_basic_set_extend_constraints(
899                 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
900 {
901         return (struct isl_basic_set *)
902                 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
903                                                     n_eq, n_ineq);
904 }
905
906 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
907 {
908         return (struct isl_basic_set *)
909                 isl_basic_map_cow((struct isl_basic_map *)bset);
910 }
911
912 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
913 {
914         if (!bmap)
915                 return NULL;
916
917         if (bmap->ref > 1) {
918                 bmap->ref--;
919                 bmap = isl_basic_map_dup(bmap);
920         }
921         ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
922         return bmap;
923 }
924
925 struct isl_set *isl_set_cow(struct isl_set *set)
926 {
927         if (!set)
928                 return NULL;
929
930         if (set->ref == 1)
931                 return set;
932         set->ref--;
933         return isl_set_dup(set);
934 }
935
936 struct isl_map *isl_map_cow(struct isl_map *map)
937 {
938         if (!map)
939                 return NULL;
940
941         if (map->ref == 1)
942                 return map;
943         map->ref--;
944         return isl_map_dup(map);
945 }
946
947 static void swap_vars(struct isl_blk blk, isl_int *a,
948                         unsigned a_len, unsigned b_len)
949 {
950         isl_seq_cpy(blk.data, a+a_len, b_len);
951         isl_seq_cpy(blk.data+b_len, a, a_len);
952         isl_seq_cpy(a, blk.data, b_len+a_len);
953 }
954
955 struct isl_basic_set *isl_basic_set_swap_vars(
956                 struct isl_basic_set *bset, unsigned n)
957 {
958         int i;
959         struct isl_blk blk;
960         unsigned dim;
961         unsigned nparam;
962
963         if (!bset)
964                 goto error;
965
966         nparam = isl_basic_set_n_param(bset);
967         dim = isl_basic_set_n_dim(bset);
968         isl_assert(bset->ctx, n <= dim, goto error);
969
970         if (n == dim)
971                 return bset;
972
973         bset = isl_basic_set_cow(bset);
974         if (!bset)
975                 return NULL;
976
977         blk = isl_blk_alloc(bset->ctx, dim);
978         if (isl_blk_is_error(blk))
979                 goto error;
980
981         for (i = 0; i < bset->n_eq; ++i)
982                 swap_vars(blk,
983                           bset->eq[i]+1+nparam, n, dim - n);
984
985         for (i = 0; i < bset->n_ineq; ++i)
986                 swap_vars(blk,
987                           bset->ineq[i]+1+nparam, n, dim - n);
988
989         for (i = 0; i < bset->n_div; ++i)
990                 swap_vars(blk,
991                           bset->div[i]+1+1+nparam, n, dim - n);
992
993         isl_blk_free(bset->ctx, blk);
994
995         ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
996         return bset;
997
998 error:
999         isl_basic_set_free(bset);
1000         return NULL;
1001 }
1002
1003 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1004 {
1005         int i;
1006         set = isl_set_cow(set);
1007         if (!set)
1008                 return NULL;
1009
1010         for (i = 0; i < set->n; ++i) {
1011                 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1012                 if (!set->p[i]) {
1013                         isl_set_free(set);
1014                         return NULL;
1015                 }
1016         }
1017         ISL_F_CLR(set, ISL_SET_NORMALIZED);
1018         return set;
1019 }
1020
1021 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1022 {
1023         int i = 0;
1024         unsigned total;
1025         if (!bmap)
1026                 goto error;
1027         total = isl_basic_map_total_dim(bmap);
1028         isl_basic_map_free_div(bmap, bmap->n_div);
1029         isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1030         if (bmap->n_eq > 0)
1031                 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1032         else {
1033                 isl_basic_map_alloc_equality(bmap);
1034                 if (i < 0)
1035                         goto error;
1036         }
1037         isl_int_set_si(bmap->eq[i][0], 1);
1038         isl_seq_clr(bmap->eq[i]+1, total);
1039         ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1040         return isl_basic_map_finalize(bmap);
1041 error:
1042         isl_basic_map_free(bmap);
1043         return NULL;
1044 }
1045
1046 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1047 {
1048         return (struct isl_basic_set *)
1049                 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1050 }
1051
1052 static void swap_div(struct isl_basic_map *bmap, int a, int b)
1053 {
1054         int i;
1055         unsigned off = isl_dim_total(bmap->dim);
1056         isl_int *t = bmap->div[a];
1057         bmap->div[a] = bmap->div[b];
1058         bmap->div[b] = t;
1059
1060         for (i = 0; i < bmap->n_eq; ++i)
1061                 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1062
1063         for (i = 0; i < bmap->n_ineq; ++i)
1064                 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1065
1066         for (i = 0; i < bmap->n_div; ++i)
1067                 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1068         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1069 }
1070
1071 /* Eliminate the specified n dimensions starting at first from the
1072  * constraints using Fourier-Motzkin, The dimensions themselves
1073  * are not removed.
1074  */
1075 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1076         unsigned first, unsigned n)
1077 {
1078         int i;
1079         unsigned nparam;
1080
1081         if (!set)
1082                 return NULL;
1083         if (n == 0)
1084                 return set;
1085
1086         set = isl_set_cow(set);
1087         if (!set)
1088                 return NULL;
1089         isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1090         nparam = isl_set_n_param(set);
1091         
1092         for (i = 0; i < set->n; ++i) {
1093                 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1094                                                             nparam + first, n);
1095                 if (!set->p[i])
1096                         goto error;
1097         }
1098         return set;
1099 error:
1100         isl_set_free(set);
1101         return NULL;
1102 }
1103
1104 /* Project out n dimensions starting at first using Fourier-Motzkin */
1105 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1106         unsigned first, unsigned n)
1107 {
1108         set = isl_set_eliminate_dims(set, first, n);
1109         set = isl_set_drop_dims(set, first, n);
1110         return set;
1111 }
1112
1113 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1114 {
1115         bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1116                                                 bset->n_div);
1117         if (!bset)
1118                 return NULL;
1119         bset->n_div = 0;
1120         return bset;
1121 }
1122
1123 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1124 {
1125         int i;
1126
1127         if (!set)
1128                 return NULL;
1129         if (set->n == 0)
1130                 return set;
1131
1132         set = isl_set_cow(set);
1133         if (!set)
1134                 return NULL;
1135         
1136         for (i = 0; i < set->n; ++i) {
1137                 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1138                 if (!set->p[i])
1139                         goto error;
1140         }
1141         return set;
1142 error:
1143         isl_set_free(set);
1144         return NULL;
1145 }
1146
1147 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1148         enum isl_dim_type type, unsigned first, unsigned n)
1149 {
1150         if (!bmap)
1151                 return NULL;
1152         isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1153                         goto error);
1154         if (n == 0)
1155                 return bmap;
1156         bmap = isl_basic_map_eliminate_vars(bmap,
1157                         isl_basic_map_offset(bmap, type) - 1 + first, n);
1158         bmap = isl_basic_map_drop(bmap, type, first, n);
1159         return bmap;
1160 error:
1161         isl_basic_map_free(bmap);
1162         return NULL;
1163 }
1164
1165 struct isl_map *isl_map_remove(struct isl_map *map,
1166         enum isl_dim_type type, unsigned first, unsigned n)
1167 {
1168         int i;
1169         unsigned nparam;
1170
1171         if (n == 0)
1172                 return map;
1173
1174         map = isl_map_cow(map);
1175         if (!map)
1176                 return NULL;
1177         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1178         
1179         for (i = 0; i < map->n; ++i) {
1180                 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1181                         isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1182                 if (!map->p[i])
1183                         goto error;
1184         }
1185         map = isl_map_drop(map, type, first, n);
1186         return map;
1187 error:
1188         isl_map_free(map);
1189         return NULL;
1190 }
1191
1192 /* Project out n inputs starting at first using Fourier-Motzkin */
1193 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1194         unsigned first, unsigned n)
1195 {
1196         return isl_map_remove(map, isl_dim_in, first, n);
1197 }
1198
1199 /* Project out n dimensions starting at first using Fourier-Motzkin */
1200 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1201         unsigned first, unsigned n)
1202 {
1203         unsigned nparam = isl_basic_set_n_param(bset);
1204         bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1205         bset = isl_basic_set_drop_dims(bset, first, n);
1206         return bset;
1207 }
1208
1209 static void dump_term(struct isl_basic_map *bmap,
1210                         isl_int c, int pos, FILE *out)
1211 {
1212         const char *name;
1213         unsigned in = isl_basic_map_n_in(bmap);
1214         unsigned dim = in + isl_basic_map_n_out(bmap);
1215         unsigned nparam = isl_basic_map_n_param(bmap);
1216         if (!pos)
1217                 isl_int_print(out, c, 0);
1218         else {
1219                 if (!isl_int_is_one(c))
1220                         isl_int_print(out, c, 0);
1221                 if (pos < 1 + nparam) {
1222                         name = isl_dim_get_name(bmap->dim,
1223                                                 isl_dim_param, pos - 1);
1224                         if (name)
1225                                 fprintf(out, "%s", name);
1226                         else
1227                                 fprintf(out, "p%d", pos - 1);
1228                 } else if (pos < 1 + nparam + in)
1229                         fprintf(out, "i%d", pos - 1 - nparam);
1230                 else if (pos < 1 + nparam + dim)
1231                         fprintf(out, "o%d", pos - 1 - nparam - in);
1232                 else
1233                         fprintf(out, "e%d", pos - 1 - nparam - dim);
1234         }
1235 }
1236
1237 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1238                                 int sign, FILE *out)
1239 {
1240         int i;
1241         int first;
1242         unsigned len = 1 + isl_basic_map_total_dim(bmap);
1243         isl_int v;
1244
1245         isl_int_init(v);
1246         for (i = 0, first = 1; i < len; ++i) {
1247                 if (isl_int_sgn(c[i]) * sign <= 0)
1248                         continue;
1249                 if (!first)
1250                         fprintf(out, " + ");
1251                 first = 0;
1252                 isl_int_abs(v, c[i]);
1253                 dump_term(bmap, v, i, out);
1254         }
1255         isl_int_clear(v);
1256         if (first)
1257                 fprintf(out, "0");
1258 }
1259
1260 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1261                                 const char *op, FILE *out, int indent)
1262 {
1263         int i;
1264
1265         fprintf(out, "%*s", indent, "");
1266
1267         dump_constraint_sign(bmap, c, 1, out);
1268         fprintf(out, " %s ", op);
1269         dump_constraint_sign(bmap, c, -1, out);
1270
1271         fprintf(out, "\n");
1272
1273         for (i = bmap->n_div; i < bmap->extra; ++i) {
1274                 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1275                         continue;
1276                 fprintf(out, "%*s", indent, "");
1277                 fprintf(out, "ERROR: unused div coefficient not zero\n");
1278                 abort();
1279         }
1280 }
1281
1282 static void dump_constraints(struct isl_basic_map *bmap,
1283                                 isl_int **c, unsigned n,
1284                                 const char *op, FILE *out, int indent)
1285 {
1286         int i;
1287
1288         for (i = 0; i < n; ++i)
1289                 dump_constraint(bmap, c[i], op, out, indent);
1290 }
1291
1292 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1293 {
1294         int j;
1295         int first = 1;
1296         unsigned total = isl_basic_map_total_dim(bmap);
1297
1298         for (j = 0; j < 1 + total; ++j) {
1299                 if (isl_int_is_zero(exp[j]))
1300                         continue;
1301                 if (!first && isl_int_is_pos(exp[j]))
1302                         fprintf(out, "+");
1303                 dump_term(bmap, exp[j], j, out);
1304                 first = 0;
1305         }
1306 }
1307
1308 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1309 {
1310         int i;
1311
1312         dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1313         dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1314
1315         for (i = 0; i < bmap->n_div; ++i) {
1316                 fprintf(out, "%*s", indent, "");
1317                 fprintf(out, "e%d = [(", i);
1318                 dump_affine(bmap, bmap->div[i]+1, out);
1319                 fprintf(out, ")/");
1320                 isl_int_print(out, bmap->div[i][0], 0);
1321                 fprintf(out, "]\n");
1322         }
1323 }
1324
1325 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1326 {
1327         if (!bset) {
1328                 fprintf(out, "null basic set\n");
1329                 return;
1330         }
1331
1332         fprintf(out, "%*s", indent, "");
1333         fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1334                         bset->ref, bset->dim->nparam, bset->dim->n_out,
1335                         bset->extra, bset->flags);
1336         dump((struct isl_basic_map *)bset, out, indent);
1337 }
1338
1339 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1340 {
1341         if (!bmap) {
1342                 fprintf(out, "null basic map\n");
1343                 return;
1344         }
1345
1346         fprintf(out, "%*s", indent, "");
1347         fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1348                         "flags: %x, n_name: %d\n",
1349                 bmap->ref,
1350                 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1351                 bmap->extra, bmap->flags, bmap->dim->n_name);
1352         dump(bmap, out, indent);
1353 }
1354
1355 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1356 {
1357         unsigned total;
1358         if (!bmap)
1359                 return -1;
1360         total = isl_basic_map_total_dim(bmap);
1361         isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1362         isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1363         isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1364         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1365         return 0;
1366 }
1367
1368 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1369 {
1370         struct isl_set *set;
1371
1372         if (!dim)
1373                 return NULL;
1374         isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1375         isl_assert(dim->ctx, n >= 0, return NULL);
1376         set = isl_alloc(dim->ctx, struct isl_set,
1377                         sizeof(struct isl_set) +
1378                         n * sizeof(struct isl_basic_set *));
1379         if (!set)
1380                 goto error;
1381
1382         set->ctx = dim->ctx;
1383         isl_ctx_ref(set->ctx);
1384         set->ref = 1;
1385         set->size = n;
1386         set->n = 0;
1387         set->dim = dim;
1388         set->flags = flags;
1389         return set;
1390 error:
1391         isl_dim_free(dim);
1392         return NULL;
1393 }
1394
1395 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1396                 unsigned nparam, unsigned dim, int n, unsigned flags)
1397 {
1398         struct isl_set *set;
1399         struct isl_dim *dims;
1400
1401         dims = isl_dim_alloc(ctx, nparam, 0, dim);
1402         if (!dims)
1403                 return NULL;
1404
1405         set = isl_set_alloc_dim(dims, n, flags);
1406         return set;
1407 }
1408
1409 struct isl_set *isl_set_dup(struct isl_set *set)
1410 {
1411         int i;
1412         struct isl_set *dup;
1413
1414         if (!set)
1415                 return NULL;
1416
1417         dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1418         if (!dup)
1419                 return NULL;
1420         for (i = 0; i < set->n; ++i)
1421                 dup = isl_set_add(dup, isl_basic_set_copy(set->p[i]));
1422         return dup;
1423 }
1424
1425 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1426 {
1427         struct isl_set *set;
1428
1429         if (!bset)
1430                 return NULL;
1431
1432         set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1433         if (!set) {
1434                 isl_basic_set_free(bset);
1435                 return NULL;
1436         }
1437         return isl_set_add(set, bset);
1438 }
1439
1440 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1441 {
1442         struct isl_map *map;
1443
1444         if (!bmap)
1445                 return NULL;
1446
1447         map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1448         if (!map) {
1449                 isl_basic_map_free(bmap);
1450                 return NULL;
1451         }
1452         return isl_map_add(map, bmap);
1453 }
1454
1455 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1456 {
1457         if (!bset || !set)
1458                 goto error;
1459         isl_assert(set->ctx, isl_dim_equal(set->dim, bset->dim), goto error);
1460         isl_assert(set->ctx, set->n < set->size, goto error);
1461         set->p[set->n] = bset;
1462         set->n++;
1463         return set;
1464 error:
1465         if (set)
1466                 isl_set_free(set);
1467         if (bset)
1468                 isl_basic_set_free(bset);
1469         return NULL;
1470 }
1471
1472 void isl_set_free(struct isl_set *set)
1473 {
1474         int i;
1475
1476         if (!set)
1477                 return;
1478
1479         if (--set->ref > 0)
1480                 return;
1481
1482         isl_ctx_deref(set->ctx);
1483         for (i = 0; i < set->n; ++i)
1484                 isl_basic_set_free(set->p[i]);
1485         isl_dim_free(set->dim);
1486         free(set);
1487 }
1488
1489 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1490 {
1491         int i;
1492
1493         if (!set) {
1494                 fprintf(out, "null set\n");
1495                 return;
1496         }
1497
1498         fprintf(out, "%*s", indent, "");
1499         fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1500                         set->ref, set->n, set->dim->nparam, set->dim->n_out,
1501                         set->flags);
1502         for (i = 0; i < set->n; ++i) {
1503                 fprintf(out, "%*s", indent, "");
1504                 fprintf(out, "basic set %d:\n", i);
1505                 isl_basic_set_dump(set->p[i], out, indent+4);
1506         }
1507 }
1508
1509 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1510 {
1511         int i;
1512
1513         if (!map) {
1514                 fprintf(out, "null map\n");
1515                 return;
1516         }
1517
1518         fprintf(out, "%*s", indent, "");
1519         fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1520                      "flags: %x, n_name: %d\n",
1521                         map->ref, map->n, map->dim->nparam, map->dim->n_in,
1522                         map->dim->n_out, map->flags, map->dim->n_name);
1523         for (i = 0; i < map->n; ++i) {
1524                 fprintf(out, "%*s", indent, "");
1525                 fprintf(out, "basic map %d:\n", i);
1526                 isl_basic_map_dump(map->p[i], out, indent+4);
1527         }
1528 }
1529
1530 struct isl_basic_map *isl_basic_map_intersect_domain(
1531                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1532 {
1533         struct isl_basic_map *bmap_domain;
1534         struct isl_dim *dim;
1535
1536         if (!bmap || !bset)
1537                 goto error;
1538
1539         isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1540                                         bset->dim, isl_dim_param), goto error);
1541
1542         if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1543                 isl_assert(bset->ctx,
1544                     isl_basic_map_compatible_domain(bmap, bset), goto error);
1545
1546         bmap = isl_basic_map_cow(bmap);
1547         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1548                         bset->n_div, bset->n_eq, bset->n_ineq);
1549         if (!bmap)
1550                 goto error;
1551         dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1552         bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1553         bmap = add_constraints(bmap, bmap_domain, 0, 0);
1554
1555         bmap = isl_basic_map_simplify(bmap);
1556         return isl_basic_map_finalize(bmap);
1557 error:
1558         isl_basic_map_free(bmap);
1559         isl_basic_set_free(bset);
1560         return NULL;
1561 }
1562
1563 struct isl_basic_map *isl_basic_map_intersect_range(
1564                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1565 {
1566         struct isl_basic_map *bmap_range;
1567
1568         if (!bmap || !bset)
1569                 goto error;
1570
1571         isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1572                                         bset->dim, isl_dim_param), goto error);
1573
1574         if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1575                 isl_assert(bset->ctx,
1576                     isl_basic_map_compatible_range(bmap, bset), goto error);
1577
1578         bmap = isl_basic_map_cow(bmap);
1579         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1580                         bset->n_div, bset->n_eq, bset->n_ineq);
1581         if (!bmap)
1582                 goto error;
1583         bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1584         bmap = add_constraints(bmap, bmap_range, 0, 0);
1585
1586         bmap = isl_basic_map_simplify(bmap);
1587         return isl_basic_map_finalize(bmap);
1588 error:
1589         isl_basic_map_free(bmap);
1590         isl_basic_set_free(bset);
1591         return NULL;
1592 }
1593
1594 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1595 {
1596         int i;
1597         unsigned total;
1598         isl_int s;
1599
1600         total = 1 + isl_basic_map_total_dim(bmap);
1601         if (total != vec->size)
1602                 return -1;
1603
1604         isl_int_init(s);
1605
1606         for (i = 0; i < bmap->n_eq; ++i) {
1607                 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
1608                 if (!isl_int_is_zero(s)) {
1609                         isl_int_clear(s);
1610                         return 0;
1611                 }
1612         }
1613
1614         for (i = 0; i < bmap->n_ineq; ++i) {
1615                 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
1616                 if (isl_int_is_neg(s)) {
1617                         isl_int_clear(s);
1618                         return 0;
1619                 }
1620         }
1621
1622         isl_int_clear(s);
1623
1624         return 1;
1625 }
1626
1627 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1628 {
1629         return basic_map_contains((struct isl_basic_map *)bset, vec);
1630 }
1631
1632 struct isl_basic_map *isl_basic_map_intersect(
1633                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1634 {
1635         struct isl_vec *sample = NULL;
1636
1637         if (!bmap1 || !bmap2)
1638                 goto error;
1639
1640         isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1641                                      bmap2->dim, isl_dim_param), goto error);
1642         if (isl_dim_total(bmap1->dim) ==
1643                                 isl_dim_size(bmap1->dim, isl_dim_param) &&
1644             isl_dim_total(bmap2->dim) !=
1645                                 isl_dim_size(bmap2->dim, isl_dim_param))
1646                 return isl_basic_map_intersect(bmap2, bmap1);
1647
1648         if (isl_dim_total(bmap2->dim) !=
1649                                         isl_dim_size(bmap2->dim, isl_dim_param))
1650                 isl_assert(bmap1->ctx,
1651                             isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1652
1653         if (bmap1->sample &&
1654             basic_map_contains(bmap1, bmap1->sample) > 0 &&
1655             basic_map_contains(bmap2, bmap1->sample) > 0)
1656                 sample = isl_vec_copy(bmap1->ctx, bmap1->sample);
1657         else if (bmap2->sample &&
1658             basic_map_contains(bmap1, bmap2->sample) > 0 &&
1659             basic_map_contains(bmap2, bmap2->sample) > 0)
1660                 sample = isl_vec_copy(bmap2->ctx, bmap2->sample);
1661
1662         bmap1 = isl_basic_map_cow(bmap1);
1663         bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1664                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1665         if (!bmap1)
1666                 goto error;
1667         bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1668
1669         if (sample) {
1670                 isl_vec_free(bmap1->ctx, bmap1->sample);
1671                 bmap1->sample = sample;
1672         }
1673
1674         bmap1 = isl_basic_map_simplify(bmap1);
1675         return isl_basic_map_finalize(bmap1);
1676 error:
1677         if (sample)
1678                 isl_vec_free(bmap1->ctx, sample);
1679         isl_basic_map_free(bmap1);
1680         isl_basic_map_free(bmap2);
1681         return NULL;
1682 }
1683
1684 struct isl_basic_set *isl_basic_set_intersect(
1685                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1686 {
1687         return (struct isl_basic_set *)
1688                 isl_basic_map_intersect(
1689                         (struct isl_basic_map *)bset1,
1690                         (struct isl_basic_map *)bset2);
1691 }
1692
1693 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1694 {
1695         unsigned flags = 0;
1696         struct isl_map *result;
1697         int i, j;
1698
1699         if (!map1 || !map2)
1700                 goto error;
1701
1702         isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1703                                          map2->dim, isl_dim_param), goto error);
1704         if (isl_dim_total(map1->dim) ==
1705                                 isl_dim_size(map1->dim, isl_dim_param) &&
1706             isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1707                 return isl_map_intersect(map2, map1);
1708
1709         if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1710                 isl_assert(map1->ctx,
1711                             isl_dim_equal(map1->dim, map2->dim), goto error);
1712
1713         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1714             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1715                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1716
1717         result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1718                                 map1->n * map2->n, flags);
1719         if (!result)
1720                 goto error;
1721         for (i = 0; i < map1->n; ++i)
1722                 for (j = 0; j < map2->n; ++j) {
1723                         struct isl_basic_map *part;
1724                         part = isl_basic_map_intersect(
1725                                     isl_basic_map_copy(map1->p[i]),
1726                                     isl_basic_map_copy(map2->p[j]));
1727                         if (isl_basic_map_is_empty(part))
1728                                 isl_basic_map_free(part);
1729                         else
1730                                 result = isl_map_add(result, part);
1731                         if (!result)
1732                                 goto error;
1733                 }
1734         isl_map_free(map1);
1735         isl_map_free(map2);
1736         return result;
1737 error:
1738         isl_map_free(map1);
1739         isl_map_free(map2);
1740         return NULL;
1741 }
1742
1743 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1744 {
1745         return (struct isl_set *)
1746                 isl_map_intersect((struct isl_map *)set1,
1747                                   (struct isl_map *)set2);
1748 }
1749
1750 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1751 {
1752         struct isl_dim *dim;
1753         struct isl_basic_set *bset;
1754         unsigned in;
1755
1756         if (!bmap)
1757                 return NULL;
1758         bmap = isl_basic_map_cow(bmap);
1759         if (!bmap)
1760                 return NULL;
1761         dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1762         in = isl_basic_map_n_in(bmap);
1763         bset = isl_basic_set_from_basic_map(bmap);
1764         bset = isl_basic_set_swap_vars(bset, in);
1765         return isl_basic_map_from_basic_set(bset, dim);
1766 }
1767
1768 /* Turn final n dimensions into existentially quantified variables.
1769  */
1770 struct isl_basic_set *isl_basic_set_project_out(
1771                 struct isl_basic_set *bset, unsigned n, unsigned flags)
1772 {
1773         int i;
1774         size_t row_size;
1775         isl_int **new_div;
1776         isl_int *old;
1777
1778         if (!bset)
1779                 return NULL;
1780
1781         isl_assert(bset->ctx, n <= isl_basic_set_n_dim(bset), goto error);
1782
1783         if (n == 0)
1784                 return bset;
1785
1786         bset = isl_basic_set_cow(bset);
1787
1788         row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1789         old = bset->block2.data;
1790         bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1791                                         (bset->extra + n) * (1 + row_size));
1792         if (!bset->block2.data)
1793                 goto error;
1794         new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1795         if (!new_div)
1796                 goto error;
1797         for (i = 0; i < n; ++i) {
1798                 new_div[i] = bset->block2.data +
1799                                 (bset->extra + i) * (1 + row_size);
1800                 isl_seq_clr(new_div[i], 1 + row_size);
1801         }
1802         for (i = 0; i < bset->extra; ++i)
1803                 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1804         free(bset->div);
1805         bset->div = new_div;
1806         bset->n_div += n;
1807         bset->extra += n;
1808         bset->dim = isl_dim_drop_outputs(bset->dim,
1809                                             isl_basic_set_n_dim(bset) - n, n);
1810         if (!bset->dim)
1811                 goto error;
1812         bset = isl_basic_set_simplify(bset);
1813         return isl_basic_set_finalize(bset);
1814 error:
1815         isl_basic_set_free(bset);
1816         return NULL;
1817 }
1818
1819 struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1820 {
1821         int i, j;
1822
1823         for (i = 0; i < n; ++i) {
1824                 j = isl_basic_map_alloc_div(bmap);
1825                 if (j < 0)
1826                         goto error;
1827                 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1828         }
1829         return bmap;
1830 error:
1831         isl_basic_map_free(bmap);
1832         return NULL;
1833 }
1834
1835 struct isl_basic_map *isl_basic_map_apply_range(
1836                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1837 {
1838         struct isl_dim *dim_result = NULL;
1839         struct isl_basic_map *bmap;
1840         unsigned n_in, n_out, n, nparam, total, pos;
1841         struct isl_dim_map *dim_map1, *dim_map2;
1842
1843         if (!bmap1 || !bmap2)
1844                 goto error;
1845
1846         dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1847                                   isl_dim_copy(bmap2->dim));
1848
1849         n_in = isl_basic_map_n_in(bmap1);
1850         n_out = isl_basic_map_n_out(bmap2);
1851         n = isl_basic_map_n_out(bmap1);
1852         nparam = isl_basic_map_n_param(bmap1);
1853
1854         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1855         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1856         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1857         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1858         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1859         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1860         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1861         isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1862         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1863         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1864         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1865
1866         bmap = isl_basic_map_alloc_dim(dim_result,
1867                         bmap1->n_div + bmap2->n_div + n,
1868                         bmap1->n_eq + bmap2->n_eq,
1869                         bmap1->n_ineq + bmap2->n_ineq);
1870         bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1871         bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1872         bmap = add_divs(bmap, n);
1873         bmap = isl_basic_map_simplify(bmap);
1874         return isl_basic_map_finalize(bmap);
1875 error:
1876         isl_basic_map_free(bmap1);
1877         isl_basic_map_free(bmap2);
1878         return NULL;
1879 }
1880
1881 struct isl_basic_set *isl_basic_set_apply(
1882                 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1883 {
1884         if (!bset || !bmap)
1885                 goto error;
1886
1887         isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
1888                     goto error);
1889
1890         return (struct isl_basic_set *)
1891                 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1892 error:
1893         isl_basic_set_free(bset);
1894         isl_basic_map_free(bmap);
1895         return NULL;
1896 }
1897
1898 struct isl_basic_map *isl_basic_map_apply_domain(
1899                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1900 {
1901         if (!bmap1 || !bmap2)
1902                 goto error;
1903
1904         isl_assert(ctx,
1905             isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1906         isl_assert(ctx,
1907             isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
1908             goto error);
1909
1910         bmap1 = isl_basic_map_reverse(bmap1);
1911         bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1912         return isl_basic_map_reverse(bmap1);
1913 error:
1914         isl_basic_map_free(bmap1);
1915         isl_basic_map_free(bmap2);
1916         return NULL;
1917 }
1918
1919 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1920  * A \cap B -> f(A) + f(B)
1921  */
1922 struct isl_basic_map *isl_basic_map_sum(
1923                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1924 {
1925         unsigned n_in, n_out, nparam, total, pos;
1926         struct isl_basic_map *bmap = NULL;
1927         struct isl_dim_map *dim_map1, *dim_map2;
1928         int i;
1929
1930         if (!bmap1 || !bmap2)
1931                 goto error;
1932
1933         isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1934                 goto error);
1935
1936         nparam = isl_basic_map_n_param(bmap1);
1937         n_in = isl_basic_map_n_in(bmap1);
1938         n_out = isl_basic_map_n_out(bmap1);
1939
1940         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
1941         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1942         dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
1943         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1944         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
1945         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1946         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1947         isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
1948         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1949         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1950         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
1951
1952         bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
1953                         bmap1->n_div + bmap2->n_div + 2 * n_out,
1954                         bmap1->n_eq + bmap2->n_eq + n_out,
1955                         bmap1->n_ineq + bmap2->n_ineq);
1956         for (i = 0; i < n_out; ++i) {
1957                 int j = isl_basic_map_alloc_equality(bmap);
1958                 if (j < 0)
1959                         goto error;
1960                 isl_seq_clr(bmap->eq[j], 1+total);
1961                 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
1962                 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
1963                 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
1964         }
1965         bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1966         bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1967         bmap = add_divs(bmap, 2 * n_out);
1968
1969         bmap = isl_basic_map_simplify(bmap);
1970         return isl_basic_map_finalize(bmap);
1971 error:
1972         isl_basic_map_free(bmap);
1973         isl_basic_map_free(bmap1);
1974         isl_basic_map_free(bmap2);
1975         return NULL;
1976 }
1977
1978 /* Given a basic map A -> f(A), construct A -> -f(A).
1979  */
1980 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
1981 {
1982         int i, j;
1983         unsigned off, n;
1984
1985         bmap = isl_basic_map_cow(bmap);
1986         if (!bmap)
1987                 return NULL;
1988
1989         n = isl_basic_map_dim(bmap, isl_dim_out);
1990         off = isl_basic_map_offset(bmap, isl_dim_out);
1991         for (i = 0; i < bmap->n_eq; ++i)
1992                 for (j = 0; j < n; ++j)
1993                         isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
1994         for (i = 0; i < bmap->n_ineq; ++i)
1995                 for (j = 0; j < n; ++j)
1996                         isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
1997         for (i = 0; i < bmap->n_div; ++i)
1998                 for (j = 0; j < n; ++j)
1999                         isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2000         return isl_basic_map_finalize(bmap);
2001 }
2002
2003 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2004  * A -> floor(f(A)/d).
2005  */
2006 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2007                 isl_int d)
2008 {
2009         unsigned n_in, n_out, nparam, total, pos;
2010         struct isl_basic_map *result = NULL;
2011         struct isl_dim_map *dim_map;
2012         int i;
2013
2014         if (!bmap)
2015                 return NULL;
2016
2017         nparam = isl_basic_map_n_param(bmap);
2018         n_in = isl_basic_map_n_in(bmap);
2019         n_out = isl_basic_map_n_out(bmap);
2020
2021         total = nparam + n_in + n_out + bmap->n_div + n_out;
2022         dim_map = isl_dim_map_alloc(bmap->ctx, total);
2023         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2024         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2025         isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2026         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2027
2028         result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2029                         bmap->n_div + n_out,
2030                         bmap->n_eq, bmap->n_ineq + 2 * n_out);
2031         result = add_constraints_dim_map(result, bmap, dim_map);
2032         result = add_divs(result, n_out);
2033         for (i = 0; i < n_out; ++i) {
2034                 int j;
2035                 j = isl_basic_map_alloc_inequality(result);
2036                 if (j < 0)
2037                         goto error;
2038                 isl_seq_clr(result->ineq[j], 1+total);
2039                 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2040                 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2041                 j = isl_basic_map_alloc_inequality(result);
2042                 if (j < 0)
2043                         goto error;
2044                 isl_seq_clr(result->ineq[j], 1+total);
2045                 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2046                 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2047                 isl_int_sub_ui(result->ineq[j][0], d, 1);
2048         }
2049
2050         result = isl_basic_map_simplify(result);
2051         return isl_basic_map_finalize(result);
2052 error:
2053         isl_basic_map_free(result);
2054         return NULL;
2055 }
2056
2057 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2058 {
2059         int i;
2060         unsigned nparam;
2061         unsigned n_in;
2062
2063         i = isl_basic_map_alloc_equality(bmap);
2064         if (i < 0)
2065                 goto error;
2066         nparam = isl_basic_map_n_param(bmap);
2067         n_in = isl_basic_map_n_in(bmap);
2068         isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2069         isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2070         isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2071         return isl_basic_map_finalize(bmap);
2072 error:
2073         isl_basic_map_free(bmap);
2074         return NULL;
2075 }
2076
2077 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2078 {
2079         int i;
2080         unsigned nparam;
2081         unsigned n_in;
2082
2083         i = isl_basic_map_alloc_inequality(bmap);
2084         if (i < 0)
2085                 goto error;
2086         nparam = isl_basic_map_n_param(bmap);
2087         n_in = isl_basic_map_n_in(bmap);
2088         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2089         isl_int_set_si(bmap->ineq[i][0], -1);
2090         isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2091         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2092         return isl_basic_map_finalize(bmap);
2093 error:
2094         isl_basic_map_free(bmap);
2095         return NULL;
2096 }
2097
2098 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2099 {
2100         int i;
2101         unsigned nparam;
2102         unsigned n_in;
2103
2104         i = isl_basic_map_alloc_inequality(bmap);
2105         if (i < 0)
2106                 goto error;
2107         nparam = isl_basic_map_n_param(bmap);
2108         n_in = isl_basic_map_n_in(bmap);
2109         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2110         isl_int_set_si(bmap->ineq[i][0], -1);
2111         isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2112         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2113         return isl_basic_map_finalize(bmap);
2114 error:
2115         isl_basic_map_free(bmap);
2116         return NULL;
2117 }
2118
2119 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2120 {
2121         int i;
2122         struct isl_basic_map *bmap;
2123         bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2124         if (!bmap)
2125                 return NULL;
2126         for (i = 0; i < n_equal && bmap; ++i)
2127                 bmap = var_equal(bmap, i);
2128         return isl_basic_map_finalize(bmap);
2129 }
2130
2131 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2132 {
2133         int i;
2134         struct isl_basic_map *bmap;
2135         bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2136         if (!bmap)
2137                 return NULL;
2138         for (i = 0; i < pos && bmap; ++i)
2139                 bmap = var_equal(bmap, i);
2140         if (bmap)
2141                 bmap = var_less(bmap, pos);
2142         return isl_basic_map_finalize(bmap);
2143 }
2144
2145 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2146 {
2147         int i;
2148         struct isl_basic_map *bmap;
2149         bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2150         if (!bmap)
2151                 return NULL;
2152         for (i = 0; i < pos && bmap; ++i)
2153                 bmap = var_equal(bmap, i);
2154         if (bmap)
2155                 bmap = var_more(bmap, pos);
2156         return isl_basic_map_finalize(bmap);
2157 }
2158
2159 struct isl_basic_map *isl_basic_map_from_basic_set(
2160                 struct isl_basic_set *bset, struct isl_dim *dim)
2161 {
2162         struct isl_basic_map *bmap;
2163
2164         bset = isl_basic_set_cow(bset);
2165         if (!bset || !dim)
2166                 goto error;
2167
2168         isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2169         isl_dim_free(bset->dim);
2170         bmap = (struct isl_basic_map *) bset;
2171         bmap->dim = dim;
2172         return isl_basic_map_finalize(bmap);
2173 error:
2174         isl_basic_set_free(bset);
2175         isl_dim_free(dim);
2176         return NULL;
2177 }
2178
2179 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2180 {
2181         if (!bmap)
2182                 goto error;
2183         if (bmap->dim->n_in == 0)
2184                 return (struct isl_basic_set *)bmap;
2185         bmap = isl_basic_map_cow(bmap);
2186         if (!bmap)
2187                 goto error;
2188         bmap->dim = isl_dim_cow(bmap->dim);
2189         if (!bmap->dim)
2190                 goto error;
2191         bmap->dim->n_out += bmap->dim->n_in;
2192         bmap->dim->n_in = 0;
2193         bmap = isl_basic_map_finalize(bmap);
2194         return (struct isl_basic_set *)bmap;
2195 error:
2196         isl_basic_map_free(bmap);
2197         return NULL;
2198 }
2199
2200 /* For a div d = floor(f/m), add the constraints
2201  *
2202  *              f - m d >= 0
2203  *              -(f-(n-1)) + m d >= 0
2204  *
2205  * Note that the second constraint is the negation of
2206  *
2207  *              f - m d >= n
2208  */
2209 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2210 {
2211         int i, j;
2212         unsigned total = isl_basic_map_total_dim(bmap);
2213         unsigned div_pos = 1 + total - bmap->n_div + div;
2214
2215         i = isl_basic_map_alloc_inequality(bmap);
2216         if (i < 0)
2217                 return -1;
2218         isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2219         isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2220
2221         j = isl_basic_map_alloc_inequality(bmap);
2222         if (j < 0)
2223                 return -1;
2224         isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2225         isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2226         isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2227         return j;
2228 }
2229
2230 struct isl_basic_set *isl_basic_map_underlying_set(
2231                 struct isl_basic_map *bmap)
2232 {
2233         if (!bmap)
2234                 goto error;
2235         if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2236                 return (struct isl_basic_set *)bmap;
2237         bmap = isl_basic_map_cow(bmap);
2238         if (!bmap)
2239                 goto error;
2240         bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2241         if (!bmap->dim)
2242                 goto error;
2243         bmap->extra -= bmap->n_div;
2244         bmap->n_div = 0;
2245         bmap = isl_basic_map_finalize(bmap);
2246         return (struct isl_basic_set *)bmap;
2247 error:
2248         return NULL;
2249 }
2250
2251 struct isl_basic_map *isl_basic_map_overlying_set(
2252         struct isl_basic_set *bset, struct isl_basic_map *like)
2253 {
2254         struct isl_basic_map *bmap;
2255         struct isl_ctx *ctx;
2256         unsigned total;
2257         int i;
2258
2259         if (!bset || !like)
2260                 goto error;
2261         ctx = bset->ctx;
2262         isl_assert(ctx, bset->n_div == 0, goto error);
2263         isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2264         isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2265                         goto error);
2266         if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2267                 isl_basic_map_free(like);
2268                 return (struct isl_basic_map *)bset;
2269         }
2270         bset = isl_basic_set_cow(bset);
2271         if (!bset)
2272                 goto error;
2273         total = bset->dim->n_out + bset->extra;
2274         bmap = (struct isl_basic_map *)bset;
2275         isl_dim_free(bmap->dim);
2276         bmap->dim = isl_dim_copy(like->dim);
2277         if (!bmap->dim)
2278                 goto error;
2279         bmap->n_div = like->n_div;
2280         bmap->extra += like->n_div;
2281         if (bmap->extra) {
2282                 unsigned ltotal;
2283                 ltotal = total - bmap->extra + like->extra;
2284                 if (ltotal > total)
2285                         ltotal = total;
2286                 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2287                                         bmap->extra * (1 + 1 + total));
2288                 if (isl_blk_is_error(bmap->block2))
2289                         goto error;
2290                 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2291                                                 bmap->extra);
2292                 if (!bmap->div)
2293                         goto error;
2294                 for (i = 0; i < bmap->extra; ++i)
2295                         bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2296                 for (i = 0; i < like->n_div; ++i) {
2297                         isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2298                         isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2299                 }
2300                 bmap = isl_basic_map_extend_constraints(bmap, 
2301                                                         0, 2 * like->n_div);
2302                 for (i = 0; i < like->n_div; ++i) {
2303                         if (isl_int_is_zero(bmap->div[i][0]))
2304                                 continue;
2305                         if (add_div_constraints(bmap, i) < 0)
2306                                 goto error;
2307                 }
2308         }
2309         isl_basic_map_free(like);
2310         bmap = isl_basic_map_simplify(bmap);
2311         bmap = isl_basic_map_finalize(bmap);
2312         return bmap;
2313 error:
2314         isl_basic_map_free(like);
2315         isl_basic_set_free(bset);
2316         return NULL;
2317 }
2318
2319 struct isl_basic_set *isl_basic_set_from_underlying_set(
2320         struct isl_basic_set *bset, struct isl_basic_set *like)
2321 {
2322         return (struct isl_basic_set *)
2323                 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2324 }
2325
2326 struct isl_set *isl_set_from_underlying_set(
2327         struct isl_set *set, struct isl_basic_set *like)
2328 {
2329         int i;
2330
2331         if (!set || !like)
2332                 goto error;
2333         isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2334                     goto error);
2335         if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2336                 isl_basic_set_free(like);
2337                 return set;
2338         }
2339         set = isl_set_cow(set);
2340         if (!set)
2341                 goto error;
2342         for (i = 0; i < set->n; ++i) {
2343                 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2344                                                       isl_basic_set_copy(like));
2345                 if (!set->p[i])
2346                         goto error;
2347         }
2348         isl_dim_free(set->dim);
2349         set->dim = isl_dim_copy(like->dim);
2350         if (!set->dim)
2351                 goto error;
2352         isl_basic_set_free(like);
2353         return set;
2354 error:
2355         isl_basic_set_free(like);
2356         isl_set_free(set);
2357         return NULL;
2358 }
2359
2360 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2361 {
2362         int i;
2363
2364         map = isl_map_cow(map);
2365         if (!map)
2366                 return NULL;
2367         map->dim = isl_dim_cow(map->dim);
2368         if (!map->dim)
2369                 goto error;
2370
2371         for (i = 1; i < map->n; ++i)
2372                 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2373                                 goto error);
2374         for (i = 0; i < map->n; ++i) {
2375                 map->p[i] = (struct isl_basic_map *)
2376                                 isl_basic_map_underlying_set(map->p[i]);
2377                 if (!map->p[i])
2378                         goto error;
2379         }
2380         if (map->n == 0)
2381                 map->dim = isl_dim_underlying(map->dim, 0);
2382         else {
2383                 isl_dim_free(map->dim);
2384                 map->dim = isl_dim_copy(map->p[0]->dim);
2385         }
2386         if (!map->dim)
2387                 goto error;
2388         return (struct isl_set *)map;
2389 error:
2390         isl_map_free(map);
2391         return NULL;
2392 }
2393
2394 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2395 {
2396         return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2397 }
2398
2399 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2400 {
2401         struct isl_basic_set *domain;
2402         unsigned n_out;
2403         if (!bmap)
2404                 return NULL;
2405         n_out = isl_basic_map_n_out(bmap);
2406         domain = isl_basic_set_from_basic_map(bmap);
2407         return isl_basic_set_project_out(domain, n_out, 0);
2408 }
2409
2410 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2411 {
2412         return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2413 }
2414
2415 struct isl_set *isl_map_range(struct isl_map *map)
2416 {
2417         int i;
2418         struct isl_set *set;
2419
2420         if (!map)
2421                 goto error;
2422         map = isl_map_cow(map);
2423         if (!map)
2424                 goto error;
2425
2426         set = (struct isl_set *) map;
2427         if (set->dim->n_in != 0) {
2428                 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2429                 if (!set->dim)
2430                         goto error;
2431         }
2432         for (i = 0; i < map->n; ++i) {
2433                 set->p[i] = isl_basic_map_range(map->p[i]);
2434                 if (!set->p[i])
2435                         goto error;
2436         }
2437         ISL_F_CLR(set, ISL_MAP_DISJOINT);
2438         ISL_F_CLR(set, ISL_SET_NORMALIZED);
2439         return set;
2440 error:
2441         isl_map_free(map);
2442         return NULL;
2443 }
2444
2445 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2446 {
2447         int i;
2448         struct isl_map *map = NULL;
2449
2450         set = isl_set_cow(set);
2451         if (!set || !dim)
2452                 goto error;
2453         isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2454         map = (struct isl_map *)set;
2455         for (i = 0; i < set->n; ++i) {
2456                 map->p[i] = isl_basic_map_from_basic_set(
2457                                 set->p[i], isl_dim_copy(dim));
2458                 if (!map->p[i])
2459                         goto error;
2460         }
2461         isl_dim_free(map->dim);
2462         map->dim = dim;
2463         return map;
2464 error:
2465         isl_dim_free(dim);
2466         isl_set_free(set);
2467         return NULL;
2468 }
2469
2470 struct isl_map *isl_map_from_range(struct isl_set *set)
2471 {
2472         return (struct isl_map *)set;
2473 }
2474
2475 struct isl_set *isl_set_from_map(struct isl_map *map)
2476 {
2477         int i;
2478         struct isl_set *set = NULL;
2479
2480         if (!map)
2481                 return NULL;
2482         map = isl_map_cow(map);
2483         if (!map)
2484                 return NULL;
2485         map->dim = isl_dim_cow(map->dim);
2486         if (!map->dim)
2487                 goto error;
2488         map->dim->n_out += map->dim->n_in;
2489         map->dim->n_in = 0;
2490         set = (struct isl_set *)map;
2491         for (i = 0; i < map->n; ++i) {
2492                 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2493                 if (!set->p[i])
2494                         goto error;
2495         }
2496         return set;
2497 error:
2498         isl_map_free(map);
2499         return NULL;
2500 }
2501
2502 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2503 {
2504         struct isl_map *map;
2505
2506         if (!dim)
2507                 return NULL;
2508         isl_assert(dim->ctx, n >= 0, return NULL);
2509         map = isl_alloc(dim->ctx, struct isl_map,
2510                         sizeof(struct isl_map) +
2511                         n * sizeof(struct isl_basic_map *));
2512         if (!map)
2513                 goto error;
2514
2515         map->ctx = dim->ctx;
2516         isl_ctx_ref(map->ctx);
2517         map->ref = 1;
2518         map->size = n;
2519         map->n = 0;
2520         map->dim = dim;
2521         map->flags = flags;
2522         return map;
2523 error:
2524         isl_dim_free(dim);
2525         return NULL;
2526 }
2527
2528 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2529                 unsigned nparam, unsigned in, unsigned out, int n,
2530                 unsigned flags)
2531 {
2532         struct isl_map *map;
2533         struct isl_dim *dims;
2534
2535         dims = isl_dim_alloc(ctx, nparam, in, out);
2536         if (!dims)
2537                 return NULL;
2538
2539         map = isl_map_alloc_dim(dims, n, flags);
2540         return map;
2541 }
2542
2543 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2544                 unsigned nparam, unsigned in, unsigned out)
2545 {
2546         struct isl_basic_map *bmap;
2547         bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2548         bmap = isl_basic_map_set_to_empty(bmap);
2549         return bmap;
2550 }
2551
2552 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2553 {
2554         struct isl_basic_set *bset;
2555         bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2556         bset = isl_basic_set_set_to_empty(bset);
2557         return bset;
2558 }
2559
2560 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2561 {
2562         struct isl_basic_map *bmap;
2563         if (!model)
2564                 return NULL;
2565         bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2566         bmap = isl_basic_map_set_to_empty(bmap);
2567         return bmap;
2568 }
2569
2570 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2571 {
2572         struct isl_basic_map *bmap;
2573         if (!model)
2574                 return NULL;
2575         bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2576         bmap = isl_basic_map_set_to_empty(bmap);
2577         return bmap;
2578 }
2579
2580 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2581 {
2582         struct isl_basic_set *bset;
2583         if (!model)
2584                 return NULL;
2585         bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2586         bset = isl_basic_set_set_to_empty(bset);
2587         return bset;
2588 }
2589
2590 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2591 {
2592         struct isl_basic_map *bmap;
2593         bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2594         return bmap;
2595 }
2596
2597 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2598 {
2599         struct isl_basic_set *bset;
2600         bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2601         return bset;
2602 }
2603
2604 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2605 {
2606         if (!model)
2607                 return NULL;
2608         return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2609 }
2610
2611 struct isl_map *isl_map_empty(struct isl_dim *dim)
2612 {
2613         return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2614 }
2615
2616 struct isl_map *isl_map_empty_like(struct isl_map *model)
2617 {
2618         if (!model)
2619                 return NULL;
2620         return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2621 }
2622
2623 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2624 {
2625         if (!model)
2626                 return NULL;
2627         return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2628 }
2629
2630 struct isl_set *isl_set_empty(struct isl_dim *dim)
2631 {
2632         return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2633 }
2634
2635 struct isl_set *isl_set_empty_like(struct isl_set *model)
2636 {
2637         if (!model)
2638                 return NULL;
2639         return isl_set_empty(isl_dim_copy(model->dim));
2640 }
2641
2642 struct isl_set *isl_set_universe(struct isl_dim *dim)
2643 {
2644         struct isl_set *set;
2645         if (!dim)
2646                 return NULL;
2647         set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2648         set = isl_set_add(set, isl_basic_set_universe(dim));
2649         return set;
2650 }
2651
2652 struct isl_map *isl_map_dup(struct isl_map *map)
2653 {
2654         int i;
2655         struct isl_map *dup;
2656
2657         if (!map)
2658                 return NULL;
2659         dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2660         for (i = 0; i < map->n; ++i)
2661                 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2662         return dup;
2663 }
2664
2665 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2666 {
2667         if (!bmap || !map)
2668                 goto error;
2669         isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2670         isl_assert(map->ctx, map->n < map->size, goto error);
2671         map->p[map->n] = bmap;
2672         map->n++;
2673         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2674         return map;
2675 error:
2676         if (map)
2677                 isl_map_free(map);
2678         if (bmap)
2679                 isl_basic_map_free(bmap);
2680         return NULL;
2681 }
2682
2683 void isl_map_free(struct isl_map *map)
2684 {
2685         int i;
2686
2687         if (!map)
2688                 return;
2689
2690         if (--map->ref > 0)
2691                 return;
2692
2693         isl_ctx_deref(map->ctx);
2694         for (i = 0; i < map->n; ++i)
2695                 isl_basic_map_free(map->p[i]);
2696         isl_dim_free(map->dim);
2697         free(map);
2698 }
2699
2700 struct isl_map *isl_map_extend(struct isl_map *base,
2701                 unsigned nparam, unsigned n_in, unsigned n_out)
2702 {
2703         int i;
2704
2705         base = isl_map_cow(base);
2706         if (!base)
2707                 return NULL;
2708
2709         base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2710         if (!base->dim)
2711                 goto error;
2712         for (i = 0; i < base->n; ++i) {
2713                 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2714                                 isl_dim_copy(base->dim), 0, 0, 0);
2715                 if (!base->p[i])
2716                         goto error;
2717         }
2718         return base;
2719 error:
2720         isl_map_free(base);
2721         return NULL;
2722 }
2723
2724 struct isl_set *isl_set_extend(struct isl_set *base,
2725                 unsigned nparam, unsigned dim)
2726 {
2727         return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2728                                                         nparam, 0, dim);
2729 }
2730
2731 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
2732                 unsigned pos, int value)
2733 {
2734         int j;
2735
2736         bmap = isl_basic_map_cow(bmap);
2737         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2738         j = isl_basic_map_alloc_equality(bmap);
2739         if (j < 0)
2740                 goto error;
2741         isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
2742         isl_int_set_si(bmap->eq[j][pos], -1);
2743         isl_int_set_si(bmap->eq[j][0], value);
2744         bmap = isl_basic_map_simplify(bmap);
2745         return isl_basic_map_finalize(bmap);
2746 error:
2747         isl_basic_map_free(bmap);
2748         return NULL;
2749 }
2750
2751 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
2752                 enum isl_dim_type type, unsigned pos, int value)
2753 {
2754         if (!bmap)
2755                 return NULL;
2756         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
2757         return isl_basic_map_fix_pos(bmap, isl_basic_map_offset(bmap, type) + pos,
2758                                         value);
2759 error:
2760         isl_basic_map_free(bmap);
2761         return NULL;
2762 }
2763
2764 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
2765                 enum isl_dim_type type, unsigned pos, int value)
2766 {
2767         return (struct isl_basic_set *)
2768                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2769                                         type, pos, value);
2770 }
2771
2772 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2773                 unsigned input, int value)
2774 {
2775         return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
2776 }
2777
2778 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2779                 unsigned dim, int value)
2780 {
2781         return (struct isl_basic_set *)
2782                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2783                                         isl_dim_set, dim, value);
2784 }
2785
2786 struct isl_map *isl_map_fix_si(struct isl_map *map,
2787                 enum isl_dim_type type, unsigned pos, int value)
2788 {
2789         int i;
2790
2791         map = isl_map_cow(map);
2792         if (!map)
2793                 return NULL;
2794
2795         isl_assert(ctx, pos < isl_map_dim(map, type), goto error);
2796         for (i = 0; i < map->n; ++i) {
2797                 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
2798                 if (!map->p[i])
2799                         goto error;
2800         }
2801         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2802         return map;
2803 error:
2804         isl_map_free(map);
2805         return NULL;
2806 }
2807
2808 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2809                 unsigned input, int value)
2810 {
2811         return isl_map_fix_si(map, isl_dim_in, input, value);
2812 }
2813
2814 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2815 {
2816         return (struct isl_set *)
2817                 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
2818 }
2819
2820 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2821         unsigned dim, isl_int value)
2822 {
2823         int j;
2824         unsigned nparam;
2825
2826         bset = isl_basic_set_cow(bset);
2827         bset = isl_basic_set_extend_constraints(bset, 0, 1);
2828         j = isl_basic_set_alloc_inequality(bset);
2829         if (j < 0)
2830                 goto error;
2831         isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
2832         isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
2833         isl_int_neg(bset->ineq[j][0], value);
2834         bset = isl_basic_set_simplify(bset);
2835         return isl_basic_set_finalize(bset);
2836 error:
2837         isl_basic_set_free(bset);
2838         return NULL;
2839 }
2840
2841 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2842                                         isl_int value)
2843 {
2844         int i;
2845
2846         set = isl_set_cow(set);
2847         if (!set)
2848                 return NULL;
2849
2850         isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2851         for (i = 0; i < set->n; ++i) {
2852                 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2853                 if (!set->p[i])
2854                         goto error;
2855         }
2856         return set;
2857 error:
2858         isl_set_free(set);
2859         return NULL;
2860 }
2861
2862 struct isl_map *isl_map_reverse(struct isl_map *map)
2863 {
2864         int i;
2865         unsigned t;
2866
2867         map = isl_map_cow(map);
2868         if (!map)
2869                 return NULL;
2870
2871         map->dim = isl_dim_reverse(map->dim);
2872         if (!map->dim)
2873                 goto error;
2874         for (i = 0; i < map->n; ++i) {
2875                 map->p[i] = isl_basic_map_reverse(map->p[i]);
2876                 if (!map->p[i])
2877                         goto error;
2878         }
2879         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2880         return map;
2881 error:
2882         isl_map_free(map);
2883         return NULL;
2884 }
2885
2886 struct isl_map *isl_basic_map_lexmax(
2887                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2888                 struct isl_set **empty)
2889 {
2890         return isl_pip_basic_map_lexmax(bmap, dom, empty);
2891 }
2892
2893 struct isl_map *isl_basic_map_lexmin(
2894                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2895                 struct isl_set **empty)
2896 {
2897         return isl_pip_basic_map_lexmin(bmap, dom, empty);
2898 }
2899
2900 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2901 {
2902         struct isl_basic_map *bmap = NULL;
2903         struct isl_basic_set *dom = NULL;
2904         struct isl_map *min;
2905         struct isl_dim *param_dim;
2906
2907         if (!bset)
2908                 goto error;
2909         bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2910         if (!bmap)
2911                 goto error;
2912         param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
2913         dom = isl_basic_set_universe(param_dim);
2914         if (!dom)
2915                 goto error;
2916         min = isl_basic_map_lexmin(bmap, dom, NULL);
2917         return isl_map_range(min);
2918 error:
2919         isl_basic_map_free(bmap);
2920         return NULL;
2921 }
2922
2923 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2924 {
2925         int i;
2926         unsigned off;
2927
2928         if (!bmap)
2929                 return NULL;
2930         off = isl_dim_total(bmap->dim);
2931         for (i = 0; i < bmap->n_div; ++i) {
2932                 if (isl_int_is_zero(bmap->div[i][0]))
2933                         return isl_pip_basic_map_compute_divs(bmap);
2934                 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
2935                                 goto error);
2936         }
2937         return isl_map_from_basic_map(bmap);
2938 error:
2939         isl_basic_map_free(bmap);
2940         return NULL;
2941 }
2942
2943 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2944 {
2945         int i;
2946         struct isl_map *res;
2947
2948         if (!map)
2949                 return NULL;
2950         if (map->n == 0)
2951                 return map;
2952         res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2953         for (i = 1 ; i < map->n; ++i) {
2954                 struct isl_map *r2;
2955                 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2956                 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
2957                         res = isl_map_union_disjoint(res, r2);
2958                 else
2959                         res = isl_map_union(res, r2);
2960         }
2961         isl_map_free(map);
2962
2963         return res;
2964 }
2965
2966 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2967 {
2968         return (struct isl_set *)
2969                 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2970 }
2971
2972 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2973 {
2974         return (struct isl_set *)
2975                 isl_map_compute_divs((struct isl_map *)set);
2976 }
2977
2978 struct isl_set *isl_map_domain(struct isl_map *map)
2979 {
2980         int i;
2981         struct isl_set *set;
2982
2983         if (!map)
2984                 goto error;
2985
2986         map = isl_map_cow(map);
2987         if (!map)
2988                 return NULL;
2989
2990         set = (struct isl_set *)map;
2991         set->dim = isl_dim_domain(set->dim);
2992         if (!set->dim)
2993                 goto error;
2994         for (i = 0; i < map->n; ++i) {
2995                 set->p[i] = isl_basic_map_domain(map->p[i]);
2996                 if (!set->p[i])
2997                         goto error;
2998         }
2999         ISL_F_CLR(set, ISL_MAP_DISJOINT);
3000         ISL_F_CLR(set, ISL_SET_NORMALIZED);
3001         return set;
3002 error:
3003         isl_map_free(map);
3004         return NULL;
3005 }
3006
3007 struct isl_map *isl_map_union_disjoint(
3008                         struct isl_map *map1, struct isl_map *map2)
3009 {
3010         int i;
3011         unsigned flags = 0;
3012         struct isl_map *map = NULL;
3013
3014         if (!map1 || !map2)
3015                 goto error;
3016
3017         if (map1->n == 0) {
3018                 isl_map_free(map1);
3019                 return map2;
3020         }
3021         if (map2->n == 0) {
3022                 isl_map_free(map2);
3023                 return map1;
3024         }
3025
3026         isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3027
3028         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
3029             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
3030                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3031
3032         map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
3033                                 map1->n + map2->n, flags);
3034         if (!map)
3035                 goto error;
3036         for (i = 0; i < map1->n; ++i) {
3037                 map = isl_map_add(map,
3038                                   isl_basic_map_copy(map1->p[i]));
3039                 if (!map)
3040                         goto error;
3041         }
3042         for (i = 0; i < map2->n; ++i) {
3043                 map = isl_map_add(map,
3044                                   isl_basic_map_copy(map2->p[i]));
3045                 if (!map)
3046                         goto error;
3047         }
3048         isl_map_free(map1);
3049         isl_map_free(map2);
3050         return map;
3051 error:
3052         isl_map_free(map);
3053         isl_map_free(map1);
3054         isl_map_free(map2);
3055         return NULL;
3056 }
3057
3058 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
3059 {
3060         map1 = isl_map_union_disjoint(map1, map2);
3061         if (!map1)
3062                 return NULL;
3063         if (map1->n > 1)
3064                 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
3065         return map1;
3066 }
3067
3068 struct isl_set *isl_set_union_disjoint(
3069                         struct isl_set *set1, struct isl_set *set2)
3070 {
3071         return (struct isl_set *)
3072                 isl_map_union_disjoint(
3073                         (struct isl_map *)set1, (struct isl_map *)set2);
3074 }
3075
3076 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
3077 {
3078         return (struct isl_set *)
3079                 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
3080 }
3081
3082 struct isl_map *isl_map_intersect_range(
3083                 struct isl_map *map, struct isl_set *set)
3084 {
3085         unsigned flags = 0;
3086         struct isl_map *result;
3087         int i, j;
3088
3089         if (!map || !set)
3090                 goto error;
3091
3092         if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
3093             ISL_F_ISSET(set, ISL_MAP_DISJOINT))
3094                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3095
3096         result = isl_map_alloc_dim(isl_dim_copy(map->dim),
3097                                         map->n * set->n, flags);
3098         if (!result)
3099                 goto error;
3100         for (i = 0; i < map->n; ++i)
3101                 for (j = 0; j < set->n; ++j) {
3102                         result = isl_map_add(result,
3103                             isl_basic_map_intersect_range(
3104                                 isl_basic_map_copy(map->p[i]),
3105                                 isl_basic_set_copy(set->p[j])));
3106                         if (!result)
3107                                 goto error;
3108                 }
3109         isl_map_free(map);
3110         isl_set_free(set);
3111         return result;
3112 error:
3113         isl_map_free(map);
3114         isl_set_free(set);
3115         return NULL;
3116 }
3117
3118 struct isl_map *isl_map_intersect_domain(
3119                 struct isl_map *map, struct isl_set *set)
3120 {
3121         return isl_map_reverse(
3122                 isl_map_intersect_range(isl_map_reverse(map), set));
3123 }
3124
3125 struct isl_map *isl_map_apply_domain(
3126                 struct isl_map *map1, struct isl_map *map2)
3127 {
3128         if (!map1 || !map2)
3129                 goto error;
3130         map1 = isl_map_reverse(map1);
3131         map1 = isl_map_apply_range(map1, map2);
3132         return isl_map_reverse(map1);
3133 error:
3134         isl_map_free(map1);
3135         isl_map_free(map2);
3136         return NULL;
3137 }
3138
3139 struct isl_map *isl_map_apply_range(
3140                 struct isl_map *map1, struct isl_map *map2)
3141 {
3142         struct isl_dim *dim_result;
3143         struct isl_map *result;
3144         int i, j;
3145         unsigned nparam;
3146         unsigned n_in;
3147         unsigned n_out;
3148
3149         if (!map1 || !map2)
3150                 goto error;
3151
3152         dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3153                                   isl_dim_copy(map2->dim));
3154
3155         result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3156         if (!result)
3157                 goto error;
3158         for (i = 0; i < map1->n; ++i)
3159                 for (j = 0; j < map2->n; ++j) {
3160                         result = isl_map_add(result,
3161                             isl_basic_map_apply_range(
3162                                 isl_basic_map_copy(map1->p[i]),
3163                                 isl_basic_map_copy(map2->p[j])));
3164                         if (!result)
3165                                 goto error;
3166                 }
3167         isl_map_free(map1);
3168         isl_map_free(map2);
3169         if (result && result->n <= 1)
3170                 ISL_F_SET(result, ISL_MAP_DISJOINT);
3171         return result;
3172 error:
3173         isl_map_free(map1);
3174         isl_map_free(map2);
3175         return NULL;
3176 }
3177
3178 /*
3179  * returns range - domain
3180  */
3181 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3182 {
3183         struct isl_basic_set *bset;
3184         unsigned dim;
3185         unsigned nparam;
3186         int i;
3187
3188         if (!bmap)
3189                 goto error;
3190         dim = isl_basic_map_n_in(bmap);
3191         nparam = isl_basic_map_n_param(bmap);
3192         isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3193         bset = isl_basic_set_from_basic_map(bmap);
3194         bset = isl_basic_set_cow(bset);
3195         bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3196         bset = isl_basic_set_swap_vars(bset, 2*dim);
3197         for (i = 0; i < dim; ++i) {
3198                 int j = isl_basic_map_alloc_equality(
3199                                             (struct isl_basic_map *)bset);
3200                 if (j < 0)
3201                         goto error;
3202                 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3203                 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3204                 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3205                 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3206         }
3207         return isl_basic_set_project_out(bset, 2*dim, 0);
3208 error:
3209         isl_basic_map_free(bmap);
3210         return NULL;
3211 }
3212
3213 /*
3214  * returns range - domain
3215  */
3216 struct isl_set *isl_map_deltas(struct isl_map *map)
3217 {
3218         int i;
3219         struct isl_set *result;
3220
3221         if (!map)
3222                 return NULL;
3223
3224         isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3225         result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3226                                         isl_map_n_in(map), map->n, map->flags);
3227         if (!result)
3228                 goto error;
3229         for (i = 0; i < map->n; ++i)
3230                 result = isl_set_add(result,
3231                           isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3232         isl_map_free(map);
3233         return result;
3234 error:
3235         isl_map_free(map);
3236         return NULL;
3237 }
3238
3239 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3240 {
3241         struct isl_basic_map *bmap;
3242         unsigned nparam;
3243         unsigned dim;
3244         int i;
3245
3246         if (!dims)
3247                 return NULL;
3248
3249         nparam = dims->nparam;
3250         dim = dims->n_out;
3251         bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3252         if (!bmap)
3253                 goto error;
3254
3255         for (i = 0; i < dim; ++i) {
3256                 int j = isl_basic_map_alloc_equality(bmap);
3257                 if (j < 0)
3258                         goto error;
3259                 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3260                 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3261                 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3262         }
3263         return isl_basic_map_finalize(bmap);
3264 error:
3265         isl_basic_map_free(bmap);
3266         return NULL;
3267 }
3268
3269 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3270 {
3271         struct isl_dim *dim = isl_dim_map(set_dim);
3272         if (!dim)
3273                 return NULL;
3274         return basic_map_identity(dim);
3275 }
3276
3277 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3278 {
3279         if (!model || !model->dim)
3280                 return NULL;
3281         isl_assert(model->ctx,
3282                         model->dim->n_in == model->dim->n_out, return NULL);
3283         return basic_map_identity(isl_dim_copy(model->dim));
3284 }
3285
3286 static struct isl_map *map_identity(struct isl_dim *dim)
3287 {
3288         struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3289         return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3290 }
3291
3292 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3293 {
3294         struct isl_dim *dim = isl_dim_map(set_dim);
3295         if (!dim)
3296                 return NULL;
3297         return map_identity(dim);
3298 }
3299
3300 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
3301 {
3302         if (!model || !model->dim)
3303                 return NULL;
3304         isl_assert(model->ctx,
3305                         model->dim->n_in == model->dim->n_out, return NULL);
3306         return map_identity(isl_dim_copy(model->dim));
3307 }
3308
3309 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3310 {
3311         return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3312 }
3313
3314 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3315 {
3316         return isl_map_is_subset(
3317                         (struct isl_map *)set1, (struct isl_map *)set2);
3318 }
3319
3320 int isl_basic_map_is_subset(
3321                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3322 {
3323         int is_subset;
3324         struct isl_map *map1;
3325         struct isl_map *map2;
3326
3327         if (!bmap1 || !bmap2)
3328                 return -1;
3329
3330         map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3331         map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3332
3333         is_subset = isl_map_is_subset(map1, map2);
3334
3335         isl_map_free(map1);
3336         isl_map_free(map2);
3337
3338         return is_subset;
3339 }
3340
3341 int isl_basic_map_is_equal(
3342                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3343 {
3344         int is_subset;
3345
3346         if (!bmap1 || !bmap2)
3347                 return -1;
3348         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3349         if (is_subset != 1)
3350                 return is_subset;
3351         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3352         return is_subset;
3353 }
3354
3355 int isl_basic_set_is_equal(
3356                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3357 {
3358         return isl_basic_map_is_equal(
3359                 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3360 }
3361
3362 int isl_map_is_empty(struct isl_map *map)
3363 {
3364         int i;
3365         int is_empty;
3366
3367         if (!map)
3368                 return -1;
3369         for (i = 0; i < map->n; ++i) {
3370                 is_empty = isl_basic_map_is_empty(map->p[i]);
3371                 if (is_empty < 0)
3372                         return -1;
3373                 if (!is_empty)
3374                         return 0;
3375         }
3376         return 1;
3377 }
3378
3379 int isl_map_fast_is_empty(struct isl_map *map)
3380 {
3381         return map->n == 0;
3382 }
3383
3384 int isl_set_is_empty(struct isl_set *set)
3385 {
3386         return isl_map_is_empty((struct isl_map *)set);
3387 }
3388
3389 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3390 {
3391         int i;
3392         int is_subset = 0;
3393         struct isl_map *diff;
3394
3395         if (!map1 || !map2)
3396                 return -1;
3397
3398         if (isl_map_is_empty(map1))
3399                 return 1;
3400
3401         if (isl_map_is_empty(map2))
3402                 return 0;
3403
3404         diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3405         if (!diff)
3406                 return -1;
3407
3408         is_subset = isl_map_is_empty(diff);
3409         isl_map_free(diff);
3410
3411         return is_subset;
3412 }
3413
3414 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3415 {
3416         int is_subset;
3417
3418         if (!map1 || !map2)
3419                 return -1;
3420         is_subset = isl_map_is_subset(map1, map2);
3421         if (is_subset != 1)
3422                 return is_subset;
3423         is_subset = isl_map_is_subset(map2, map1);
3424         return is_subset;
3425 }
3426
3427 int isl_basic_map_is_strict_subset(
3428                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3429 {
3430         int is_subset;
3431
3432         if (!bmap1 || !bmap2)
3433                 return -1;
3434         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3435         if (is_subset != 1)
3436                 return is_subset;
3437         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3438         if (is_subset == -1)
3439                 return is_subset;
3440         return !is_subset;
3441 }
3442
3443 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3444 {
3445         if (!bmap)
3446                 return -1;
3447         return bmap->n_eq == 0 && bmap->n_ineq == 0;
3448 }
3449
3450 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3451 {
3452         struct isl_basic_set *bset = NULL;
3453         struct isl_vec *sample = NULL;
3454         int empty;
3455         unsigned total;
3456
3457         if (!bmap)
3458                 return -1;
3459
3460         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3461                 return 1;
3462
3463         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3464                 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3465                 copy = isl_basic_map_convex_hull(copy);
3466                 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3467                 isl_basic_map_free(copy);
3468                 return empty;
3469         }
3470
3471         total = 1 + isl_basic_map_total_dim(bmap);
3472         if (bmap->sample && bmap->sample->size == total) {
3473                 int contains = basic_map_contains(bmap, bmap->sample);
3474                 if (contains < 0)
3475                         return -1;
3476                 if (contains)
3477                         return 0;
3478         }
3479         bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3480         if (!bset)
3481                 return -1;
3482         sample = isl_basic_set_sample(bset);
3483         if (!sample)
3484                 return -1;
3485         empty = sample->size == 0;
3486         if (bmap->sample)
3487                 isl_vec_free(bmap->ctx, bmap->sample);
3488         bmap->sample = sample;
3489
3490         return empty;
3491 }
3492
3493 int isl_basic_set_is_empty(struct isl_basic_set *bset)
3494 {
3495         return isl_basic_map_is_empty((struct isl_basic_map *)bset);
3496 }
3497
3498 struct isl_map *isl_basic_map_union(
3499         struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3500 {
3501         struct isl_map *map;
3502         if (!bmap1 || !bmap2)
3503                 return NULL;
3504
3505         isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3506
3507         map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3508         if (!map)
3509                 goto error;
3510         map = isl_map_add(map, bmap1);
3511         map = isl_map_add(map, bmap2);
3512         return map;
3513 error:
3514         isl_basic_map_free(bmap1);
3515         isl_basic_map_free(bmap2);
3516         return NULL;
3517 }
3518
3519 struct isl_set *isl_basic_set_union(
3520                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3521 {
3522         return (struct isl_set *)isl_basic_map_union(
3523                                             (struct isl_basic_map *)bset1,
3524                                             (struct isl_basic_map *)bset2);
3525 }
3526
3527 /* Order divs such that any div only depends on previous divs */
3528 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3529 {
3530         int i;
3531         unsigned off = isl_dim_total(bmap->dim);
3532
3533         for (i = 0; i < bmap->n_div; ++i) {
3534                 int pos;
3535                 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3536                                                             bmap->n_div-i);
3537                 if (pos == -1)
3538                         continue;
3539                 swap_div(bmap, i, pos);
3540                 --i;
3541         }
3542         return bmap;
3543 }
3544
3545 /* Look for a div in dst that corresponds to the div "div" in src.
3546  * The divs before "div" in src and dst are assumed to be the same.
3547  * 
3548  * Returns -1 if no corresponding div was found and the position
3549  * of the corresponding div in dst otherwise.
3550  */
3551 static int find_div(struct isl_basic_map *dst,
3552                         struct isl_basic_map *src, unsigned div)
3553 {
3554         int i;
3555
3556         unsigned total = isl_dim_total(src->dim);
3557
3558         isl_assert(dst->ctx, div <= dst->n_div, return -1);
3559         for (i = div; i < dst->n_div; ++i)
3560                 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
3561                     isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
3562                                                 dst->n_div - div) == -1)
3563                         return i;
3564         return -1;
3565 }
3566
3567 struct isl_basic_map *isl_basic_map_align_divs(
3568                 struct isl_basic_map *dst, struct isl_basic_map *src)
3569 {
3570         int i;
3571         unsigned total = isl_dim_total(src->dim);
3572
3573         if (!dst || !src)
3574                 goto error;
3575
3576         if (src->n_div == 0)
3577                 return dst;
3578
3579         for (i = 0; i < src->n_div; ++i)
3580                 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
3581
3582         src = order_divs(src);
3583         dst = isl_basic_map_cow(dst);
3584         dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
3585                         src->n_div, 0, 2 * src->n_div);
3586         if (!dst)
3587                 return NULL;
3588         for (i = 0; i < src->n_div; ++i) {
3589                 int j = find_div(dst, src, i);
3590                 if (j < 0) {
3591                         j = isl_basic_map_alloc_div(dst);
3592                         if (j < 0)
3593                                 goto error;
3594                         isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
3595                         isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
3596                         if (add_div_constraints(dst, j) < 0)
3597                                 goto error;
3598                 }
3599                 if (j != i)
3600                         swap_div(dst, i, j);
3601         }
3602         return dst;
3603 error:
3604         isl_basic_map_free(dst);
3605         return NULL;
3606 }
3607
3608 struct isl_basic_set *isl_basic_set_align_divs(
3609                 struct isl_basic_set *dst, struct isl_basic_set *src)
3610 {
3611         return (struct isl_basic_set *)isl_basic_map_align_divs(
3612                 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3613 }
3614
3615 struct isl_map *isl_map_align_divs(struct isl_map *map)
3616 {
3617         int i;
3618
3619         map = isl_map_compute_divs(map);
3620         map = isl_map_cow(map);
3621         if (!map)
3622                 return NULL;
3623
3624         for (i = 1; i < map->n; ++i)
3625                 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3626         for (i = 1; i < map->n; ++i)
3627                 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3628
3629         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3630         return map;
3631 }
3632
3633 struct isl_set *isl_set_align_divs(struct isl_set *set)
3634 {
3635         return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
3636 }
3637
3638 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3639                 struct isl_basic_map *src, isl_int *c,
3640                 unsigned len, int oppose)
3641 {
3642         struct isl_basic_map *copy = NULL;
3643         int is_empty;
3644         int k;
3645         unsigned total;
3646
3647         copy = isl_basic_map_copy(src);
3648         copy = isl_basic_map_cow(copy);
3649         if (!copy)
3650                 goto error;
3651         copy = isl_basic_map_extend_constraints(copy, 0, 1);
3652         k = isl_basic_map_alloc_inequality(copy);
3653         if (k < 0)
3654                 goto error;
3655         if (oppose)
3656                 isl_seq_neg(copy->ineq[k], c, len);
3657         else
3658                 isl_seq_cpy(copy->ineq[k], c, len);
3659         total = 1 + isl_basic_map_total_dim(copy);
3660         isl_seq_clr(copy->ineq[k]+len, total - len);
3661         isl_inequality_negate(copy, k);
3662         copy = isl_basic_map_simplify(copy);
3663         copy = isl_basic_map_finalize(copy);
3664         is_empty = isl_basic_map_is_empty(copy);
3665         if (is_empty < 0)
3666                 goto error;
3667         if (!is_empty)
3668                 dst = isl_map_add(dst, copy);
3669         else
3670                 isl_basic_map_free(copy);
3671         return dst;
3672 error:
3673         isl_basic_map_free(copy);
3674         isl_map_free(dst);
3675         return NULL;
3676 }
3677
3678 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3679 {
3680         int i, j, k;
3681         unsigned flags = 0;
3682         struct isl_map *rest = NULL;
3683         unsigned max;
3684         unsigned total = isl_basic_map_total_dim(bmap);
3685
3686         assert(bmap);
3687
3688         if (!map)
3689                 goto error;
3690
3691         if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
3692                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
3693
3694         max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3695         rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
3696         if (!rest)
3697                 goto error;
3698
3699         for (i = 0; i < map->n; ++i) {
3700                 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3701                 if (!map->p[i])
3702                         goto error;
3703         }
3704
3705         for (j = 0; j < map->n; ++j)
3706                 map->p[j] = isl_basic_map_cow(map->p[j]);
3707
3708         for (i = 0; i < bmap->n_eq; ++i) {
3709                 for (j = 0; j < map->n; ++j) {
3710                         rest = add_cut_constraint(rest,
3711                                 map->p[j], bmap->eq[i], 1+total, 0);
3712                         if (!rest)
3713                                 goto error;
3714
3715                         rest = add_cut_constraint(rest,
3716                                 map->p[j], bmap->eq[i], 1+total, 1);
3717                         if (!rest)
3718                                 goto error;
3719
3720                         map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3721                                 1, 0);
3722                         if (!map->p[j])
3723                                 goto error;
3724                         k = isl_basic_map_alloc_equality(map->p[j]);
3725                         if (k < 0)
3726                                 goto error;
3727                         isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3728                         isl_seq_clr(map->p[j]->eq[k]+1+total,
3729                                         map->p[j]->n_div - bmap->n_div);
3730                 }
3731         }
3732
3733         for (i = 0; i < bmap->n_ineq; ++i) {
3734                 for (j = 0; j < map->n; ++j) {
3735                         rest = add_cut_constraint(rest,
3736                                 map->p[j], bmap->ineq[i], 1+total, 0);
3737                         if (!rest)
3738                                 goto error;
3739
3740                         map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3741                                 0, 1);
3742                         if (!map->p[j])
3743                                 goto error;
3744                         k = isl_basic_map_alloc_inequality(map->p[j]);
3745                         if (k < 0)
3746                                 goto error;
3747                         isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3748                         isl_seq_clr(map->p[j]->ineq[k]+1+total,
3749                                         map->p[j]->n_div - bmap->n_div);
3750                 }
3751         }
3752
3753         isl_map_free(map);
3754         return rest;
3755 error:
3756         isl_map_free(map);
3757         isl_map_free(rest);
3758         return NULL;
3759 }
3760
3761 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3762 {
3763         int i;
3764         if (!map1 || !map2)
3765                 goto error;
3766
3767         isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3768
3769         if (isl_map_is_empty(map2)) {
3770                 isl_map_free(map2);
3771                 return map1;
3772         }
3773
3774         map1 = isl_map_compute_divs(map1);
3775         map2 = isl_map_compute_divs(map2);
3776         if (!map1 || !map2)
3777                 goto error;
3778
3779         for (i = 0; map1 && i < map2->n; ++i)
3780                 map1 = subtract(map1, map2->p[i]);
3781
3782         isl_map_free(map2);
3783         return map1;
3784 error:
3785         isl_map_free(map1);
3786         isl_map_free(map2);
3787         return NULL;
3788 }
3789
3790 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3791 {
3792         return (struct isl_set *)
3793                 isl_map_subtract(
3794                         (struct isl_map *)set1, (struct isl_map *)set2);
3795 }
3796
3797 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3798 {
3799         if (!set || !map)
3800                 goto error;
3801         isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
3802         map = isl_map_intersect_domain(map, set);
3803         set = isl_map_range(map);
3804         return set;
3805 error:
3806         isl_set_free(set);
3807         isl_map_free(map);
3808         return NULL;
3809 }
3810
3811 /* There is no need to cow as removing empty parts doesn't change
3812  * the meaning of the set.
3813  */
3814 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3815 {
3816         int i;
3817
3818         if (!map)
3819                 return NULL;
3820
3821         for (i = map->n-1; i >= 0; --i) {
3822                 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3823                         continue;
3824                 isl_basic_map_free(map->p[i]);
3825                 if (i != map->n-1) {
3826                         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3827                         map->p[i] = map->p[map->n-1];
3828                 }
3829                 map->n--;
3830         }
3831
3832         return map;
3833 }
3834
3835 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3836 {
3837         return (struct isl_set *)
3838                 isl_map_remove_empty_parts((struct isl_map *)set);
3839 }
3840
3841 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
3842 {
3843         struct isl_basic_map *bmap;
3844         if (!map || map->n == 0)
3845                 return NULL;
3846         bmap = map->p[map->n-1];
3847         isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
3848         return isl_basic_map_copy(bmap);
3849 }
3850
3851 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3852 {
3853         (struct isl_basic_set *)isl_map_copy_basic_map((struct isl_map *)set);
3854 }
3855
3856 struct isl_map *isl_map_drop_basic_map(struct isl_map *map,
3857                                                 struct isl_basic_map *bmap)
3858 {
3859         int i;
3860
3861         if (!map || !bmap)
3862                 goto error;
3863         for (i = map->n-1; i >= 0; --i) {
3864                 if (map->p[i] != bmap)
3865                         continue;
3866                 map = isl_map_cow(map);
3867                 if (!map)
3868                         goto error;
3869                 isl_basic_map_free(map->p[i]);
3870                 if (i != map->n-1) {
3871                         ISL_F_CLR(map, ISL_SET_NORMALIZED);
3872                         map->p[i] = map->p[map->n-1];
3873                 }
3874                 map->n--;
3875                 return map;
3876         }
3877         isl_basic_map_free(bmap);
3878         return map;
3879 error:
3880         isl_map_free(map);
3881         isl_basic_map_free(bmap);
3882         return NULL;
3883 }
3884
3885 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3886                                                 struct isl_basic_set *bset)
3887 {
3888         (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
3889                                                 (struct isl_basic_map *)bset);
3890 }
3891
3892 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3893  * for any common value of the parameters and dimensions preceding dim
3894  * in both basic sets, the values of dimension pos in bset1 are
3895  * smaller or larger than those in bset2.
3896  *
3897  * Returns
3898  *       1 if bset1 follows bset2
3899  *      -1 if bset1 precedes bset2
3900  *       0 if bset1 and bset2 are incomparable
3901  *      -2 if some error occurred.
3902  */
3903 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
3904         struct isl_basic_set *bset2, int pos)
3905 {
3906         struct isl_dim *dims;
3907         struct isl_basic_map *bmap1 = NULL;
3908         struct isl_basic_map *bmap2 = NULL;
3909         struct isl_ctx *ctx;
3910         struct isl_vec *obj;
3911         unsigned total;
3912         unsigned nparam;
3913         unsigned dim1, dim2;
3914         isl_int num, den;
3915         enum isl_lp_result res;
3916         int cmp;
3917
3918         if (!bset1 || !bset2)
3919                 return -2;
3920
3921         nparam = isl_basic_set_n_param(bset1);
3922         dim1 = isl_basic_set_n_dim(bset1);
3923         dim2 = isl_basic_set_n_dim(bset2);
3924         dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
3925         bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
3926         dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
3927         bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
3928         if (!bmap1 || !bmap2)
3929                 goto error;
3930         bmap1 = isl_basic_map_cow(bmap1);
3931         bmap1 = isl_basic_map_extend(bmap1, nparam,
3932                         pos, (dim1 - pos) + (dim2 - pos),
3933                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3934         bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
3935         if (!bmap1)
3936                 goto error;
3937         total = isl_basic_map_total_dim(bmap1);
3938         ctx = bmap1->ctx;
3939         obj = isl_vec_alloc(ctx, 1 + total);
3940         isl_seq_clr(obj->block.data, 1 + total);
3941         isl_int_set_si(obj->block.data[1+nparam+pos], 1);
3942         isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
3943         if (!obj)
3944                 goto error;
3945         isl_int_init(num);
3946         isl_int_init(den);
3947         res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
3948         if (res == isl_lp_empty)
3949                 cmp = 0;
3950         else if (res == isl_lp_ok && isl_int_is_pos(num))
3951                 cmp = 1;
3952         else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
3953                   res == isl_lp_unbounded)
3954                 cmp = -1;
3955         else
3956                 cmp = -2;
3957         isl_int_clear(num);
3958         isl_int_clear(den);
3959         isl_basic_map_free(bmap1);
3960         isl_vec_free(ctx, obj);
3961         return cmp;
3962 error:
3963         isl_basic_map_free(bmap1);
3964         isl_basic_map_free(bmap2);
3965         return -2;
3966 }
3967
3968 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
3969         unsigned pos, isl_int *val)
3970 {
3971         int i;
3972         int d;
3973         unsigned total;
3974
3975         if (!bmap)
3976                 return -1;
3977         total = isl_basic_map_total_dim(bmap);
3978         for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
3979                 for (; d+1 > pos; --d)
3980                         if (!isl_int_is_zero(bmap->eq[i][1+d]))
3981                                 break;
3982                 if (d != pos)
3983                         continue;
3984                 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
3985                         return 0;
3986                 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
3987                         return 0;
3988                 if (!isl_int_is_one(bmap->eq[i][1+d]))
3989                         return 0;
3990                 if (val)
3991                         isl_int_neg(*val, bmap->eq[i][0]);
3992                 return 1;
3993         }
3994         return 0;
3995 }
3996
3997 static int isl_map_fast_has_fixed_var(struct isl_map *map,
3998         unsigned pos, isl_int *val)
3999 {
4000         int i;
4001         isl_int v;
4002         isl_int tmp;
4003         int fixed;
4004
4005         if (!map)
4006                 return -1;
4007         if (map->n == 0)
4008                 return 0;
4009         if (map->n == 1)
4010                 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val); 
4011         isl_int_init(v);
4012         isl_int_init(tmp);
4013         fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v); 
4014         for (i = 1; fixed == 1 && i < map->n; ++i) {
4015                 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp); 
4016                 if (fixed == 1 && isl_int_ne(tmp, v))
4017                         fixed = 0;
4018         }
4019         if (val)
4020                 isl_int_set(*val, v);
4021         isl_int_clear(tmp);
4022         isl_int_clear(v);
4023         return fixed;
4024 }
4025
4026 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
4027         isl_int *val)
4028 {
4029         return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
4030 }
4031
4032 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
4033         enum isl_dim_type type, unsigned pos, isl_int *val)
4034 {
4035         if (pos >= isl_basic_map_dim(bmap, type))
4036                 return -1;
4037         return isl_basic_map_fast_has_fixed_var(bmap,
4038                 isl_basic_map_offset(bmap, type) - 1 + pos, val);
4039 }
4040
4041 /* Check if dimension dim has fixed value and if so and if val is not NULL,
4042  * then return this fixed value in *val.
4043  */
4044 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
4045 {
4046         return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
4047 }
4048
4049 /* Check if input variable in has fixed value and if so and if val is not NULL,
4050  * then return this fixed value in *val.
4051  */
4052 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
4053 {
4054         return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
4055 }
4056
4057 /* Check if dimension dim has an (obvious) fixed lower bound and if so
4058  * and if val is not NULL, then return this lower bound in *val.
4059  */
4060 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
4061         unsigned dim, isl_int *val)
4062 {
4063         int i, i_eq = -1, i_ineq = -1;
4064         isl_int *c;
4065         unsigned total;
4066         unsigned nparam;
4067
4068         if (!bset)
4069                 return -1;
4070         total = isl_basic_set_total_dim(bset);
4071         nparam = isl_basic_set_n_param(bset);
4072         for (i = 0; i < bset->n_eq; ++i) {
4073                 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
4074                         continue;
4075                 if (i_eq != -1)
4076                         return 0;
4077                 i_eq = i;
4078         }
4079         for (i = 0; i < bset->n_ineq; ++i) {
4080                 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
4081                         continue;
4082                 if (i_eq != -1 || i_ineq != -1)
4083                         return 0;
4084                 i_ineq = i;
4085         }
4086         if (i_eq == -1 && i_ineq == -1)
4087                 return 0;
4088         c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
4089         /* The coefficient should always be one due to normalization. */
4090         if (!isl_int_is_one(c[1+nparam+dim]))
4091                 return 0;
4092         if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
4093                 return 0;
4094         if (isl_seq_first_non_zero(c+1+nparam+dim+1,
4095                                         total - nparam - dim - 1) != -1)
4096                 return 0;
4097         if (val)
4098                 isl_int_neg(*val, c[0]);
4099         return 1;
4100 }
4101
4102 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
4103         unsigned dim, isl_int *val)
4104 {
4105         int i;
4106         isl_int v;
4107         isl_int tmp;
4108         int fixed;
4109
4110         if (!set)
4111                 return -1;
4112         if (set->n == 0)
4113                 return 0;
4114         if (set->n == 1)
4115                 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4116                                                                 dim, val);
4117         isl_int_init(v);
4118         isl_int_init(tmp);
4119         fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
4120                                                                 dim, &v);
4121         for (i = 1; fixed == 1 && i < set->n; ++i) {
4122                 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
4123                                                                 dim, &tmp);
4124                 if (fixed == 1 && isl_int_ne(tmp, v))
4125                         fixed = 0;
4126         }
4127         if (val)
4128                 isl_int_set(*val, v);
4129         isl_int_clear(tmp);
4130         isl_int_clear(v);
4131         return fixed;
4132 }
4133
4134 struct constraint {
4135         unsigned        size;
4136         isl_int         *c;
4137 };
4138
4139 static int qsort_constraint_cmp(const void *p1, const void *p2)
4140 {
4141         const struct constraint *c1 = (const struct constraint *)p1;
4142         const struct constraint *c2 = (const struct constraint *)p2;
4143         unsigned size = isl_min(c1->size, c2->size);
4144         return isl_seq_cmp(c1->c, c2->c, size);
4145 }
4146
4147 static struct isl_basic_map *isl_basic_map_sort_constraints(
4148         struct isl_basic_map *bmap)
4149 {
4150         int i;
4151         struct constraint *c;
4152         unsigned total;
4153
4154         if (!bmap)
4155                 return NULL;
4156         total = isl_basic_map_total_dim(bmap);
4157         c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4158         if (!c)
4159                 goto error;
4160         for (i = 0; i < bmap->n_ineq; ++i) {
4161                 c[i].size = total;
4162                 c[i].c = bmap->ineq[i];
4163         }
4164         qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4165         for (i = 0; i < bmap->n_ineq; ++i)
4166                 bmap->ineq[i] = c[i].c;
4167         free(c);
4168         return bmap;
4169 error:
4170         isl_basic_map_free(bmap);
4171         return NULL;
4172 }
4173
4174 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4175 {
4176         if (!bmap)
4177                 return NULL;
4178         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4179                 return bmap;
4180         bmap = isl_basic_map_convex_hull(bmap);
4181         bmap = isl_basic_map_sort_constraints(bmap);
4182         ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4183         return bmap;
4184 }
4185
4186 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4187 {
4188         return (struct isl_basic_set *)isl_basic_map_normalize(
4189                                                 (struct isl_basic_map *)bset);
4190 }
4191
4192 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4193         const struct isl_basic_map *bmap2)
4194 {
4195         int i, cmp;
4196         unsigned total;
4197
4198         if (bmap1 == bmap2)
4199                 return 0;
4200         if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4201                 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4202         if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4203                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4204         if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4205                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4206         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4207             ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4208                 return 0;
4209         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4210                 return 1;
4211         if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4212                 return -1;
4213         if (bmap1->n_eq != bmap2->n_eq)
4214                 return bmap1->n_eq - bmap2->n_eq;
4215         if (bmap1->n_ineq != bmap2->n_ineq)
4216                 return bmap1->n_ineq - bmap2->n_ineq;
4217         if (bmap1->n_div != bmap2->n_div)
4218                 return bmap1->n_div - bmap2->n_div;
4219         total = isl_basic_map_total_dim(bmap1);
4220         for (i = 0; i < bmap1->n_eq; ++i) {
4221                 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4222                 if (cmp)
4223                         return cmp;
4224         }
4225         for (i = 0; i < bmap1->n_ineq; ++i) {
4226                 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4227                 if (cmp)
4228                         return cmp;
4229         }
4230         for (i = 0; i < bmap1->n_div; ++i) {
4231                 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4232                 if (cmp)
4233                         return cmp;
4234         }
4235         return 0;
4236 }
4237
4238 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4239         struct isl_basic_map *bmap2)
4240 {
4241         return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4242 }
4243
4244 static int qsort_bmap_cmp(const void *p1, const void *p2)
4245 {
4246         const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4247         const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4248
4249         return isl_basic_map_fast_cmp(bmap1, bmap2);
4250 }
4251
4252 /* We normalize in place, but if anything goes wrong we need
4253  * to return NULL, so we need to make sure we don't change the
4254  * meaning of any possible other copies of map.
4255  */
4256 struct isl_map *isl_map_normalize(struct isl_map *map)
4257 {
4258         int i, j;
4259         struct isl_basic_map *bmap;
4260
4261         if (!map)
4262                 return NULL;
4263         if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
4264                 return map;
4265         for (i = 0; i < map->n; ++i) {
4266                 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4267                 if (!bmap)
4268                         goto error;
4269                 isl_basic_map_free(map->p[i]);
4270                 map->p[i] = bmap;
4271         }
4272         qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4273         ISL_F_SET(map, ISL_MAP_NORMALIZED);
4274         map = isl_map_remove_empty_parts(map);
4275         if (!map)
4276                 return NULL;
4277         for (i = map->n - 1; i >= 1; --i) {
4278                 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4279                         continue;
4280                 isl_basic_map_free(map->p[i-1]);
4281                 for (j = i; j < map->n; ++j)
4282                         map->p[j-1] = map->p[j];
4283                 map->n--;
4284         }
4285         return map;
4286 error:
4287         isl_map_free(map);
4288         return NULL;
4289
4290 }
4291
4292 struct isl_set *isl_set_normalize(struct isl_set *set)
4293 {
4294         return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4295 }
4296
4297 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4298 {
4299         int i;
4300         int equal;
4301
4302         if (!map1 || !map2)
4303                 return -1;
4304
4305         if (map1 == map2)
4306                 return 1;
4307         if (!isl_dim_equal(map1->dim, map2->dim))
4308                 return 0;
4309
4310         map1 = isl_map_copy(map1);
4311         map2 = isl_map_copy(map2);
4312         map1 = isl_map_normalize(map1);
4313         map2 = isl_map_normalize(map2);
4314         if (!map1 || !map2)
4315                 goto error;
4316         equal = map1->n == map2->n;
4317         for (i = 0; equal && i < map1->n; ++i) {
4318                 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4319                 if (equal < 0)
4320                         goto error;
4321         }
4322         isl_map_free(map1);
4323         isl_map_free(map2);
4324         return equal;
4325 error:
4326         isl_map_free(map1);
4327         isl_map_free(map2);
4328         return -1;
4329 }
4330
4331 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4332 {
4333         return isl_map_fast_is_equal((struct isl_map *)set1,
4334                                                 (struct isl_map *)set2);
4335 }
4336
4337 /* Return an interval that ranges from min to max (inclusive)
4338  */
4339 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4340         isl_int min, isl_int max)
4341 {
4342         int k;
4343         struct isl_basic_set *bset = NULL;
4344
4345         bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4346         if (!bset)
4347                 goto error;
4348
4349         k = isl_basic_set_alloc_inequality(bset);
4350         if (k < 0)
4351                 goto error;
4352         isl_int_set_si(bset->ineq[k][1], 1);
4353         isl_int_neg(bset->ineq[k][0], min);
4354
4355         k = isl_basic_set_alloc_inequality(bset);
4356         if (k < 0)
4357                 goto error;
4358         isl_int_set_si(bset->ineq[k][1], -1);
4359         isl_int_set(bset->ineq[k][0], max);
4360
4361         return bset;
4362 error:
4363         isl_basic_set_free(bset);
4364         return NULL;
4365 }
4366
4367 /* Return the Cartesian product of the basic sets in list (in the given order).
4368  */
4369 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4370 {
4371         int i;
4372         unsigned dim;
4373         unsigned nparam;
4374         unsigned extra;
4375         unsigned n_eq;
4376         unsigned n_ineq;
4377         struct isl_basic_set *product = NULL;
4378
4379         if (!list)
4380                 goto error;
4381         isl_assert(list->ctx, list->n > 0, goto error);
4382         isl_assert(list->ctx, list->p[0], goto error);
4383         nparam = isl_basic_set_n_param(list->p[0]);
4384         dim = isl_basic_set_n_dim(list->p[0]);
4385         extra = list->p[0]->n_div;
4386         n_eq = list->p[0]->n_eq;
4387         n_ineq = list->p[0]->n_ineq;
4388         for (i = 1; i < list->n; ++i) {
4389                 isl_assert(list->ctx, list->p[i], goto error);
4390                 isl_assert(list->ctx,
4391                     nparam == isl_basic_set_n_param(list->p[i]), goto error);
4392                 dim += isl_basic_set_n_dim(list->p[i]);
4393                 extra += list->p[i]->n_div;
4394                 n_eq += list->p[i]->n_eq;
4395                 n_ineq += list->p[i]->n_ineq;
4396         }
4397         product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4398                                         n_eq, n_ineq);
4399         if (!product)
4400                 goto error;
4401         dim = 0;
4402         for (i = 0; i < list->n; ++i) {
4403                 isl_basic_set_add_constraints(product,
4404                                         isl_basic_set_copy(list->p[i]), dim);
4405                 dim += isl_basic_set_n_dim(list->p[i]);
4406         }
4407         isl_basic_set_list_free(list);
4408         return product;
4409 error:
4410         isl_basic_set_free(product);
4411         isl_basic_set_list_free(list);
4412         return NULL;
4413 }
4414
4415 struct isl_basic_map *isl_basic_map_product(
4416                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4417 {
4418         struct isl_dim *dim_result = NULL;
4419         struct isl_basic_map *bmap;
4420         unsigned in1, in2, out1, out2, nparam, total, pos;
4421         struct isl_dim_map *dim_map1, *dim_map2;
4422
4423         if (!bmap1 || !bmap2)
4424                 goto error;
4425
4426         isl_assert(map1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
4427                                      bmap2->dim, isl_dim_param), goto error);
4428         dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
4429                                                    isl_dim_copy(bmap2->dim));
4430
4431         in1 = isl_basic_map_n_in(bmap1);
4432         in2 = isl_basic_map_n_in(bmap2);
4433         out1 = isl_basic_map_n_out(bmap1);
4434         out2 = isl_basic_map_n_out(bmap2);
4435         nparam = isl_basic_map_n_param(bmap1);
4436
4437         total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
4438         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
4439         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
4440         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
4441         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
4442         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
4443         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
4444         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
4445         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
4446         isl_dim_map_div(dim_map1, bmap1, pos += out2);
4447         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
4448
4449         bmap = isl_basic_map_alloc_dim(dim_result,
4450                         bmap1->n_div + bmap2->n_div,
4451                         bmap1->n_eq + bmap2->n_eq,
4452                         bmap1->n_ineq + bmap2->n_ineq);
4453         bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
4454         bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
4455         bmap = isl_basic_map_simplify(bmap);
4456         return isl_basic_map_finalize(bmap);
4457 error:
4458         isl_basic_map_free(bmap1);
4459         isl_basic_map_free(bmap2);
4460         return NULL;
4461 }
4462
4463 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
4464  */
4465 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
4466 {
4467         unsigned flags = 0;
4468         struct isl_map *result;
4469         int i, j;
4470
4471         if (!map1 || !map2)
4472                 goto error;
4473
4474         isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
4475                                          map2->dim, isl_dim_param), goto error);
4476
4477         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4478             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4479                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4480
4481         result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
4482                                                    isl_dim_copy(map2->dim)),
4483                                 map1->n * map2->n, flags);
4484         if (!result)
4485                 goto error;
4486         for (i = 0; i < map1->n; ++i)
4487                 for (j = 0; j < map2->n; ++j) {
4488                         struct isl_basic_map *part;
4489                         part = isl_basic_map_product(
4490                                     isl_basic_map_copy(map1->p[i]),
4491                                     isl_basic_map_copy(map2->p[j]));
4492                         if (isl_basic_map_is_empty(part))
4493                                 isl_basic_map_free(part);
4494                         else
4495                                 result = isl_map_add(result, part);
4496                         if (!result)
4497                                 goto error;
4498                 }
4499         isl_map_free(map1);
4500         isl_map_free(map2);
4501         return result;
4502 error:
4503         isl_map_free(map1);
4504         isl_map_free(map2);
4505         return NULL;
4506 }
4507
4508 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4509 {
4510         int i;
4511         uint32_t hash;
4512         unsigned total;
4513
4514         if (!bset)
4515                 return 0;
4516         bset = isl_basic_set_copy(bset);
4517         bset = isl_basic_set_normalize(bset);
4518         if (!bset)
4519                 return 0;
4520         total = isl_basic_set_total_dim(bset);
4521         isl_hash_byte(hash, bset->n_eq & 0xFF);
4522         for (i = 0; i < bset->n_eq; ++i) {
4523                 uint32_t c_hash;
4524                 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4525                 isl_hash_hash(hash, c_hash);
4526         }
4527         isl_hash_byte(hash, bset->n_ineq & 0xFF);
4528         for (i = 0; i < bset->n_ineq; ++i) {
4529                 uint32_t c_hash;
4530                 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4531                 isl_hash_hash(hash, c_hash);
4532         }
4533         isl_hash_byte(hash, bset->n_div & 0xFF);
4534         for (i = 0; i < bset->n_div; ++i) {
4535                 uint32_t c_hash;
4536                 if (isl_int_is_zero(bset->div[i][0]))
4537                         continue;
4538                 isl_hash_byte(hash, i & 0xFF);
4539                 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4540                 isl_hash_hash(hash, c_hash);
4541         }
4542         isl_basic_set_free(bset);
4543         return hash;
4544 }
4545
4546 uint32_t isl_set_get_hash(struct isl_set *set)
4547 {
4548         int i;
4549         uint32_t hash;
4550
4551         if (!set)
4552                 return 0;
4553         set = isl_set_copy(set);
4554         set = isl_set_normalize(set);
4555         if (!set)
4556                 return 0;
4557
4558         hash = isl_hash_init();
4559         for (i = 0; i < set->n; ++i) {
4560                 uint32_t bset_hash;
4561                 bset_hash = isl_basic_set_get_hash(set->p[i]);
4562                 isl_hash_hash(hash, bset_hash);
4563         }
4564                 
4565         isl_set_free(set);
4566
4567         return hash;
4568 }
4569
4570 /* Check if the value for dimension dim is completely determined
4571  * by the values of the other parameters and variables.
4572  * That is, check if dimension dim is involved in an equality.
4573  */
4574 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
4575 {
4576         int i;
4577         unsigned nparam;
4578
4579         if (!bset)
4580                 return -1;
4581         nparam = isl_basic_set_n_param(bset);
4582         for (i = 0; i < bset->n_eq; ++i)
4583                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
4584                         return 1;
4585         return 0;
4586 }
4587
4588 /* Check if the value for dimension dim is completely determined
4589  * by the values of the other parameters and variables.
4590  * That is, check if dimension dim is involved in an equality
4591  * for each of the subsets.
4592  */
4593 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
4594 {
4595         int i;
4596
4597         if (!set)
4598                 return -1;
4599         for (i = 0; i < set->n; ++i) {
4600                 int unique;
4601                 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
4602                 if (unique != 1)
4603                         return unique;
4604         }
4605         return 1;
4606 }