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