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