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