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