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