isl_map_intersect: allow intersection with pure parameter constraints
[platform/upstream/isl.git] / isl_map.c
1 #include <string.h>
2 #include <strings.h>
3 #include "isl_ctx.h"
4 #include "isl_blk.h"
5 #include "isl_dim.h"
6 #include "isl_list.h"
7 #include "isl_lp.h"
8 #include "isl_seq.h"
9 #include "isl_set.h"
10 #include "isl_map.h"
11 #include "isl_map_private.h"
12 #include "isl_map_piplib.h"
13 #include "isl_sample.h"
14 #include "isl_vec.h"
15
16 /* Maps dst positions to src positions */
17 struct isl_dim_map {
18         unsigned len;
19         int pos[1];
20 };
21
22 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
23 {
24         int i;
25         struct isl_dim_map *dim_map;
26         dim_map = isl_alloc(ctx, struct isl_dim_map,
27                                 sizeof(struct isl_dim_map) + len * sizeof(int));
28         if (!dim_map)
29                 return NULL;
30         dim_map->len = 1 + len;
31         dim_map->pos[0] = 0;
32         for (i = 0; i < len; ++i)
33                 dim_map->pos[1 + i] = -1;
34         return dim_map;
35 }
36
37 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
38 {
39         switch (type) {
40         case isl_dim_param:     return dim->nparam;
41         case isl_dim_in:        return dim->n_in;
42         case isl_dim_out:       return dim->n_out;
43         }
44 }
45
46 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
47 {
48         switch (type) {
49         case isl_dim_param:     return 1;
50         case isl_dim_in:        return 1 + dim->nparam;
51         case isl_dim_out:       return 1 + dim->nparam + dim->n_in;
52         }
53 }
54
55 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
56                 enum isl_dim_type type, unsigned dst_pos)
57 {
58         int i;
59         unsigned src_pos;
60
61         if (!dim_map || !dim)
62                 return;
63         
64         src_pos = pos(dim, type);
65         for (i = 0; i < n(dim, type); ++i)
66                 dim_map->pos[1 + dst_pos + i] = src_pos + i;
67 }
68
69 static void isl_dim_map_div(struct isl_dim_map *dim_map,
70                 struct isl_basic_map *bmap, unsigned dst_pos)
71 {
72         int i;
73         unsigned src_pos;
74
75         if (!dim_map || !bmap)
76                 return;
77         
78         src_pos = 1 + isl_dim_total(bmap->dim);
79         for (i = 0; i < bmap->n_div; ++i)
80                 dim_map->pos[1 + dst_pos + i] = src_pos + i;
81 }
82
83 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
84 {
85         int i;
86
87         for (i = 0; i < dim_map->len; ++i)
88                 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
89         fprintf(stderr, "\n");
90 }
91
92 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
93                                 enum isl_dim_type type)
94 {
95         struct isl_dim *dim = bmap->dim;
96         switch (type) {
97         case isl_dim_param:
98         case isl_dim_in:
99         case isl_dim_out:       return isl_dim_size(bmap->dim, type);
100         case isl_dim_div:       return bmap->n_div;
101         case isl_dim_all:       return isl_basic_map_total_dim(bmap);
102         }
103 }
104
105 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
106 {
107         return n(map->dim, type);
108 }
109
110 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(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1525                                      bmap2->dim, isl_dim_param), goto error);
1526         if (isl_dim_total(bmap1->dim) ==
1527                                 isl_dim_size(bmap1->dim, isl_dim_param) &&
1528             isl_dim_total(bmap2->dim) !=
1529                                 isl_dim_size(bmap2->dim, isl_dim_param))
1530                 return isl_basic_map_intersect(bmap2, bmap1);
1531
1532         if (isl_dim_total(bmap2->dim) !=
1533                                         isl_dim_size(bmap2->dim, isl_dim_param))
1534                 isl_assert(bmap1->ctx,
1535                             isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1536
1537         bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1538                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1539         if (!bmap1)
1540                 goto error;
1541         bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1542
1543         bmap1 = isl_basic_map_simplify(bmap1);
1544         return isl_basic_map_finalize(bmap1);
1545 error:
1546         isl_basic_map_free(bmap1);
1547         isl_basic_map_free(bmap2);
1548         return NULL;
1549 }
1550
1551 struct isl_basic_set *isl_basic_set_intersect(
1552                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1553 {
1554         return (struct isl_basic_set *)
1555                 isl_basic_map_intersect(
1556                         (struct isl_basic_map *)bset1,
1557                         (struct isl_basic_map *)bset2);
1558 }
1559
1560 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1561 {
1562         unsigned flags = 0;
1563         struct isl_map *result;
1564         int i, j;
1565
1566         if (!map1 || !map2)
1567                 goto error;
1568
1569         isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1570                                          map2->dim, isl_dim_param), goto error);
1571         if (isl_dim_total(map1->dim) ==
1572                                 isl_dim_size(map1->dim, isl_dim_param) &&
1573             isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1574                 return isl_map_intersect(map2, map1);
1575
1576         if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1577                 isl_assert(map1->ctx,
1578                             isl_dim_equal(map1->dim, map2->dim), goto error);
1579
1580         if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
1581             F_ISSET(map2, ISL_MAP_DISJOINT))
1582                 FL_SET(flags, ISL_MAP_DISJOINT);
1583
1584         result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1585                                 map1->n * map2->n, flags);
1586         if (!result)
1587                 goto error;
1588         for (i = 0; i < map1->n; ++i)
1589                 for (j = 0; j < map2->n; ++j) {
1590                         struct isl_basic_map *part;
1591                         part = isl_basic_map_intersect(
1592                                     isl_basic_map_copy(map1->p[i]),
1593                                     isl_basic_map_copy(map2->p[j]));
1594                         if (isl_basic_map_is_empty(part))
1595                                 isl_basic_map_free(part);
1596                         else
1597                                 result = isl_map_add(result, part);
1598                         if (!result)
1599                                 goto error;
1600                 }
1601         isl_map_free(map1);
1602         isl_map_free(map2);
1603         return result;
1604 error:
1605         isl_map_free(map1);
1606         isl_map_free(map2);
1607         return NULL;
1608 }
1609
1610 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1611 {
1612         return (struct isl_set *)
1613                 isl_map_intersect((struct isl_map *)set1,
1614                                   (struct isl_map *)set2);
1615 }
1616
1617 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1618 {
1619         struct isl_dim *dim;
1620         struct isl_basic_set *bset;
1621         unsigned in;
1622
1623         if (!bmap)
1624                 return NULL;
1625         bmap = isl_basic_map_cow(bmap);
1626         if (!bmap)
1627                 return NULL;
1628         dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1629         in = isl_basic_map_n_in(bmap);
1630         bset = isl_basic_set_from_basic_map(bmap);
1631         bset = isl_basic_set_swap_vars(bset, in);
1632         return isl_basic_map_from_basic_set(bset, dim);
1633 }
1634
1635 /* Turn final n dimensions into existentially quantified variables.
1636  */
1637 struct isl_basic_set *isl_basic_set_project_out(
1638                 struct isl_basic_set *bset, unsigned n, unsigned flags)
1639 {
1640         int i;
1641         size_t row_size;
1642         isl_int **new_div;
1643         isl_int *old;
1644
1645         if (!bset)
1646                 return NULL;
1647
1648         isl_assert(bset->ctx, n <= isl_basic_set_n_dim(bset), goto error);
1649
1650         if (n == 0)
1651                 return bset;
1652
1653         bset = isl_basic_set_cow(bset);
1654
1655         row_size = 1 + isl_dim_total(bset->dim) + bset->extra;
1656         old = bset->block2.data;
1657         bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1658                                         (bset->extra + n) * (1 + row_size));
1659         if (!bset->block2.data)
1660                 goto error;
1661         new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1662         if (!new_div)
1663                 goto error;
1664         for (i = 0; i < n; ++i) {
1665                 new_div[i] = bset->block2.data +
1666                                 (bset->extra + i) * (1 + row_size);
1667                 isl_seq_clr(new_div[i], 1 + row_size);
1668         }
1669         for (i = 0; i < bset->extra; ++i)
1670                 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1671         free(bset->div);
1672         bset->div = new_div;
1673         bset->n_div += n;
1674         bset->extra += n;
1675         bset->dim = isl_dim_drop_outputs(bset->dim,
1676                                             isl_basic_set_n_dim(bset) - n, n);
1677         if (!bset->dim)
1678                 goto error;
1679         bset = isl_basic_set_simplify(bset);
1680         return isl_basic_set_finalize(bset);
1681 error:
1682         isl_basic_set_free(bset);
1683         return NULL;
1684 }
1685
1686 struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
1687 {
1688         int i, j;
1689
1690         for (i = 0; i < n; ++i) {
1691                 j = isl_basic_map_alloc_div(bmap);
1692                 if (j < 0)
1693                         goto error;
1694                 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
1695         }
1696         return bmap;
1697 error:
1698         isl_basic_map_free(bmap);
1699         return NULL;
1700 }
1701
1702 struct isl_basic_map *isl_basic_map_apply_range(
1703                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1704 {
1705         struct isl_dim *dim_result = NULL;
1706         struct isl_basic_map *bmap;
1707         unsigned n_in, n_out, n, nparam, total, pos;
1708         struct isl_dim_map *dim_map1, *dim_map2;
1709
1710         if (!bmap1 || !bmap2)
1711                 goto error;
1712
1713         dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
1714                                   isl_dim_copy(bmap2->dim));
1715
1716         n_in = isl_basic_map_n_in(bmap1);
1717         n_out = isl_basic_map_n_out(bmap2);
1718         n = isl_basic_map_n_out(bmap1);
1719         nparam = isl_basic_map_n_param(bmap1);
1720
1721         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
1722         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1723         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
1724         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1725         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
1726         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1727         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
1728         isl_dim_map_div(dim_map1, bmap1, pos += n_out);
1729         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1730         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1731         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1732
1733         bmap = isl_basic_map_alloc_dim(dim_result,
1734                         bmap1->n_div + bmap2->n_div + n,
1735                         bmap1->n_eq + bmap2->n_eq,
1736                         bmap1->n_ineq + bmap2->n_ineq);
1737         bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1738         bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1739         bmap = add_divs(bmap, n);
1740         bmap = isl_basic_map_simplify(bmap);
1741         return isl_basic_map_finalize(bmap);
1742 error:
1743         isl_basic_map_free(bmap1);
1744         isl_basic_map_free(bmap2);
1745         return NULL;
1746 }
1747
1748 struct isl_basic_set *isl_basic_set_apply(
1749                 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1750 {
1751         if (!bset || !bmap)
1752                 goto error;
1753
1754         isl_assert(set->ctx, isl_basic_map_compatible_domain(bmap, bset),
1755                     goto error);
1756
1757         return (struct isl_basic_set *)
1758                 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1759 error:
1760         isl_basic_set_free(bset);
1761         isl_basic_map_free(bmap);
1762         return NULL;
1763 }
1764
1765 struct isl_basic_map *isl_basic_map_apply_domain(
1766                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1767 {
1768         if (!bmap1 || !bmap2)
1769                 goto error;
1770
1771         isl_assert(ctx,
1772             isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
1773         isl_assert(ctx,
1774             isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
1775             goto error);
1776
1777         bmap1 = isl_basic_map_reverse(bmap1);
1778         bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1779         return isl_basic_map_reverse(bmap1);
1780 error:
1781         isl_basic_map_free(bmap1);
1782         isl_basic_map_free(bmap2);
1783         return NULL;
1784 }
1785
1786 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
1787  * A \cap B -> f(A) + f(B)
1788  */
1789 struct isl_basic_map *isl_basic_map_sum(
1790                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1791 {
1792         unsigned n_in, n_out, nparam, total, pos;
1793         struct isl_basic_map *bmap = NULL;
1794         struct isl_dim_map *dim_map1, *dim_map2;
1795         int i;
1796
1797         if (!bmap1 || !bmap2)
1798                 goto error;
1799
1800         isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
1801                 goto error);
1802
1803         nparam = isl_basic_map_n_param(bmap1);
1804         n_in = isl_basic_map_n_in(bmap1);
1805         n_out = isl_basic_map_n_out(bmap1);
1806
1807         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
1808         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
1809         dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
1810         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
1811         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
1812         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
1813         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
1814         isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
1815         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
1816         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
1817         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
1818
1819         bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
1820                         bmap1->n_div + bmap2->n_div + 2 * n_out,
1821                         bmap1->n_eq + bmap2->n_eq + n_out,
1822                         bmap1->n_ineq + bmap2->n_ineq);
1823         for (i = 0; i < n_out; ++i) {
1824                 int j = isl_basic_map_alloc_equality(bmap);
1825                 if (j < 0)
1826                         goto error;
1827                 isl_seq_clr(bmap->eq[j], 1+total);
1828                 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
1829                 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
1830                 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
1831         }
1832         bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
1833         bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
1834         bmap = add_divs(bmap, 2 * n_out);
1835
1836         bmap = isl_basic_map_simplify(bmap);
1837         return isl_basic_map_finalize(bmap);
1838 error:
1839         isl_basic_map_free(bmap);
1840         isl_basic_map_free(bmap1);
1841         isl_basic_map_free(bmap2);
1842         return NULL;
1843 }
1844
1845 /* Given a basic map A -> f(A), construct A -> -f(A).
1846  */
1847 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
1848 {
1849         int i, j;
1850         unsigned off, n;
1851
1852         bmap = isl_basic_map_cow(bmap);
1853         if (!bmap)
1854                 return NULL;
1855
1856         n = isl_basic_map_dim(bmap, isl_dim_out);
1857         off = basic_map_offset(bmap, isl_dim_out);
1858         for (i = 0; i < bmap->n_eq; ++i)
1859                 for (j = 0; j < n; ++j)
1860                         isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
1861         for (i = 0; i < bmap->n_ineq; ++i)
1862                 for (j = 0; j < n; ++j)
1863                         isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
1864         for (i = 0; i < bmap->n_div; ++i)
1865                 for (j = 0; j < n; ++j)
1866                         isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
1867         return isl_basic_map_finalize(bmap);
1868 }
1869
1870 /* Given a basic map A -> f(A) and an integer d, construct a basic map
1871  * A -> floor(f(A)/d).
1872  */
1873 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
1874                 isl_int d)
1875 {
1876         unsigned n_in, n_out, nparam, total, pos;
1877         struct isl_basic_map *result = NULL;
1878         struct isl_dim_map *dim_map;
1879         int i;
1880
1881         if (!bmap)
1882                 return NULL;
1883
1884         nparam = isl_basic_map_n_param(bmap);
1885         n_in = isl_basic_map_n_in(bmap);
1886         n_out = isl_basic_map_n_out(bmap);
1887
1888         total = nparam + n_in + n_out + bmap->n_div + n_out;
1889         dim_map = isl_dim_map_alloc(bmap->ctx, total);
1890         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
1891         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
1892         isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
1893         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
1894
1895         result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
1896                         bmap->n_div + n_out,
1897                         bmap->n_eq, bmap->n_ineq + 2 * n_out);
1898         result = add_constraints_dim_map(result, bmap, dim_map);
1899         result = add_divs(result, n_out);
1900         for (i = 0; i < n_out; ++i) {
1901                 int j;
1902                 j = isl_basic_map_alloc_inequality(result);
1903                 if (j < 0)
1904                         goto error;
1905                 isl_seq_clr(result->ineq[j], 1+total);
1906                 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
1907                 isl_int_set_si(result->ineq[j][1+pos+i], 1);
1908                 j = isl_basic_map_alloc_inequality(result);
1909                 if (j < 0)
1910                         goto error;
1911                 isl_seq_clr(result->ineq[j], 1+total);
1912                 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
1913                 isl_int_set_si(result->ineq[j][1+pos+i], -1);
1914                 isl_int_sub_ui(result->ineq[j][0], d, 1);
1915         }
1916
1917         result = isl_basic_map_simplify(result);
1918         return isl_basic_map_finalize(result);
1919 error:
1920         isl_basic_map_free(result);
1921         return NULL;
1922 }
1923
1924 static struct isl_basic_map *var_equal(struct isl_ctx *ctx,
1925                 struct isl_basic_map *bmap, unsigned pos)
1926 {
1927         int i;
1928         unsigned nparam;
1929         unsigned n_in;
1930
1931         i = isl_basic_map_alloc_equality(bmap);
1932         if (i < 0)
1933                 goto error;
1934         nparam = isl_basic_map_n_param(bmap);
1935         n_in = isl_basic_map_n_in(bmap);
1936         isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
1937         isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
1938         isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
1939         return isl_basic_map_finalize(bmap);
1940 error:
1941         isl_basic_map_free(bmap);
1942         return NULL;
1943 }
1944
1945 static struct isl_basic_map *var_more(struct isl_ctx *ctx,
1946                 struct isl_basic_map *bmap, unsigned pos)
1947 {
1948         int i;
1949         unsigned nparam;
1950         unsigned n_in;
1951
1952         i = isl_basic_map_alloc_inequality(bmap);
1953         if (i < 0)
1954                 goto error;
1955         nparam = isl_basic_map_n_param(bmap);
1956         n_in = isl_basic_map_n_in(bmap);
1957         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
1958         isl_int_set_si(bmap->ineq[i][0], -1);
1959         isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
1960         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
1961         return isl_basic_map_finalize(bmap);
1962 error:
1963         isl_basic_map_free(bmap);
1964         return NULL;
1965 }
1966
1967 static struct isl_basic_map *var_less(struct isl_ctx *ctx,
1968                 struct isl_basic_map *bmap, unsigned pos)
1969 {
1970         int i;
1971         unsigned nparam;
1972         unsigned n_in;
1973
1974         i = isl_basic_map_alloc_inequality(bmap);
1975         if (i < 0)
1976                 goto error;
1977         nparam = isl_basic_map_n_param(bmap);
1978         n_in = isl_basic_map_n_in(bmap);
1979         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
1980         isl_int_set_si(bmap->ineq[i][0], -1);
1981         isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
1982         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
1983         return isl_basic_map_finalize(bmap);
1984 error:
1985         isl_basic_map_free(bmap);
1986         return NULL;
1987 }
1988
1989 struct isl_basic_map *isl_basic_map_equal(struct isl_ctx *ctx,
1990                 unsigned nparam, unsigned in, unsigned out, unsigned n_equal)
1991 {
1992         int i;
1993         struct isl_basic_map *bmap;
1994         bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, n_equal, 0);
1995         if (!bmap)
1996                 return NULL;
1997         for (i = 0; i < n_equal && bmap; ++i)
1998                 bmap = var_equal(ctx, bmap, i);
1999         return isl_basic_map_finalize(bmap);
2000 }
2001
2002 struct isl_basic_map *isl_basic_map_less_at(struct isl_ctx *ctx,
2003                 unsigned nparam, unsigned in, unsigned out, unsigned pos)
2004 {
2005         int i;
2006         struct isl_basic_map *bmap;
2007         bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
2008         if (!bmap)
2009                 return NULL;
2010         for (i = 0; i < pos && bmap; ++i)
2011                 bmap = var_equal(ctx, bmap, i);
2012         if (bmap)
2013                 bmap = var_less(ctx, bmap, pos);
2014         return isl_basic_map_finalize(bmap);
2015 }
2016
2017 struct isl_basic_map *isl_basic_map_more_at(struct isl_ctx *ctx,
2018                 unsigned nparam, unsigned in, unsigned out, unsigned pos)
2019 {
2020         int i;
2021         struct isl_basic_map *bmap;
2022         bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
2023         if (!bmap)
2024                 return NULL;
2025         for (i = 0; i < pos && bmap; ++i)
2026                 bmap = var_equal(ctx, bmap, i);
2027         if (bmap)
2028                 bmap = var_more(ctx, bmap, pos);
2029         return isl_basic_map_finalize(bmap);
2030 }
2031
2032 struct isl_basic_map *isl_basic_map_from_basic_set(
2033                 struct isl_basic_set *bset, struct isl_dim *dim)
2034 {
2035         struct isl_basic_map *bmap;
2036
2037         bset = isl_basic_set_cow(bset);
2038         if (!bset || !dim)
2039                 goto error;
2040
2041         isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2042         isl_dim_free(bset->dim);
2043         bmap = (struct isl_basic_map *) bset;
2044         bmap->dim = dim;
2045         return isl_basic_map_finalize(bmap);
2046 error:
2047         isl_basic_set_free(bset);
2048         isl_dim_free(dim);
2049         return NULL;
2050 }
2051
2052 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2053 {
2054         if (!bmap)
2055                 goto error;
2056         if (bmap->dim->n_in == 0)
2057                 return (struct isl_basic_set *)bmap;
2058         bmap = isl_basic_map_cow(bmap);
2059         if (!bmap)
2060                 goto error;
2061         bmap->dim = isl_dim_cow(bmap->dim);
2062         if (!bmap->dim)
2063                 goto error;
2064         bmap->dim->n_out += bmap->dim->n_in;
2065         bmap->dim->n_in = 0;
2066         bmap = isl_basic_map_finalize(bmap);
2067         return (struct isl_basic_set *)bmap;
2068 error:
2069         isl_basic_map_free(bmap);
2070         return NULL;
2071 }
2072
2073 /* For a div d = floor(f/m), add the constraints
2074  *
2075  *              f - m d >= 0
2076  *              -(f-(n-1)) + m d >= 0
2077  *
2078  * Note that the second constraint is the negation of
2079  *
2080  *              f - m d >= n
2081  */
2082 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2083 {
2084         int i, j;
2085         unsigned total = isl_basic_map_total_dim(bmap);
2086         unsigned div_pos = 1 + total - bmap->n_div + div;
2087
2088         i = isl_basic_map_alloc_inequality(bmap);
2089         if (i < 0)
2090                 return -1;
2091         isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2092         isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2093
2094         j = isl_basic_map_alloc_inequality(bmap);
2095         if (j < 0)
2096                 return -1;
2097         isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2098         isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2099         isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2100         return j;
2101 }
2102
2103 struct isl_basic_set *isl_basic_map_underlying_set(
2104                 struct isl_basic_map *bmap)
2105 {
2106         if (!bmap)
2107                 goto error;
2108         if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2109                 return (struct isl_basic_set *)bmap;
2110         bmap = isl_basic_map_cow(bmap);
2111         if (!bmap)
2112                 goto error;
2113         bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2114         if (!bmap->dim)
2115                 goto error;
2116         bmap->extra -= bmap->n_div;
2117         bmap->n_div = 0;
2118         bmap = isl_basic_map_finalize(bmap);
2119         return (struct isl_basic_set *)bmap;
2120 error:
2121         return NULL;
2122 }
2123
2124 struct isl_basic_map *isl_basic_map_overlying_set(
2125         struct isl_basic_set *bset, struct isl_basic_map *like)
2126 {
2127         struct isl_basic_map *bmap;
2128         struct isl_ctx *ctx;
2129         unsigned total;
2130         int i;
2131
2132         if (!bset || !like)
2133                 goto error;
2134         ctx = bset->ctx;
2135         isl_assert(ctx, bset->n_div == 0, goto error);
2136         isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2137         isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2138                         goto error);
2139         if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2140                 isl_basic_map_free(like);
2141                 return (struct isl_basic_map *)bset;
2142         }
2143         bset = isl_basic_set_cow(bset);
2144         if (!bset)
2145                 goto error;
2146         total = bset->dim->n_out + bset->extra;
2147         bmap = (struct isl_basic_map *)bset;
2148         isl_dim_free(bmap->dim);
2149         bmap->dim = isl_dim_copy(like->dim);
2150         if (!bmap->dim)
2151                 goto error;
2152         bmap->n_div = like->n_div;
2153         bmap->extra += like->n_div;
2154         if (bmap->extra) {
2155                 unsigned ltotal;
2156                 ltotal = total - bmap->extra + like->extra;
2157                 if (ltotal > total)
2158                         ltotal = total;
2159                 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2160                                         bmap->extra * (1 + 1 + total));
2161                 if (isl_blk_is_error(bmap->block2))
2162                         goto error;
2163                 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2164                                                 bmap->extra);
2165                 if (!bmap->div)
2166                         goto error;
2167                 for (i = 0; i < bmap->extra; ++i)
2168                         bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2169                 for (i = 0; i < like->n_div; ++i) {
2170                         isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2171                         isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2172                 }
2173                 bmap = isl_basic_map_extend_constraints(bmap, 
2174                                                         0, 2 * like->n_div);
2175                 for (i = 0; i < like->n_div; ++i) {
2176                         if (isl_int_is_zero(bmap->div[i][0]))
2177                                 continue;
2178                         if (add_div_constraints(bmap, i) < 0)
2179                                 goto error;
2180                 }
2181         }
2182         isl_basic_map_free(like);
2183         bmap = isl_basic_map_simplify(bmap);
2184         bmap = isl_basic_map_finalize(bmap);
2185         return bmap;
2186 error:
2187         isl_basic_map_free(like);
2188         isl_basic_set_free(bset);
2189         return NULL;
2190 }
2191
2192 struct isl_basic_set *isl_basic_set_from_underlying_set(
2193         struct isl_basic_set *bset, struct isl_basic_set *like)
2194 {
2195         return (struct isl_basic_set *)
2196                 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2197 }
2198
2199 struct isl_set *isl_set_from_underlying_set(
2200         struct isl_set *set, struct isl_basic_set *like)
2201 {
2202         int i;
2203
2204         if (!set || !like)
2205                 goto error;
2206         isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2207                     goto error);
2208         if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2209                 isl_basic_set_free(like);
2210                 return set;
2211         }
2212         set = isl_set_cow(set);
2213         if (!set)
2214                 goto error;
2215         for (i = 0; i < set->n; ++i) {
2216                 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2217                                                       isl_basic_set_copy(like));
2218                 if (!set->p[i])
2219                         goto error;
2220         }
2221         isl_dim_free(set->dim);
2222         set->dim = isl_dim_copy(like->dim);
2223         if (!set->dim)
2224                 goto error;
2225         isl_basic_set_free(like);
2226         return set;
2227 error:
2228         isl_basic_set_free(like);
2229         isl_set_free(set);
2230         return NULL;
2231 }
2232
2233 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2234 {
2235         int i;
2236
2237         map = isl_map_cow(map);
2238         if (!map)
2239                 return NULL;
2240         map->dim = isl_dim_cow(map->dim);
2241         if (!map->dim)
2242                 goto error;
2243
2244         for (i = 1; i < map->n; ++i)
2245                 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2246                                 goto error);
2247         for (i = 0; i < map->n; ++i) {
2248                 map->p[i] = (struct isl_basic_map *)
2249                                 isl_basic_map_underlying_set(map->p[i]);
2250                 if (!map->p[i])
2251                         goto error;
2252         }
2253         if (map->n == 0)
2254                 map->dim = isl_dim_underlying(map->dim, 0);
2255         else {
2256                 isl_dim_free(map->dim);
2257                 map->dim = isl_dim_copy(map->p[0]->dim);
2258         }
2259         if (!map->dim)
2260                 goto error;
2261         return (struct isl_set *)map;
2262 error:
2263         isl_map_free(map);
2264         return NULL;
2265 }
2266
2267 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2268 {
2269         return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2270 }
2271
2272 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2273 {
2274         struct isl_basic_set *domain;
2275         unsigned n_out;
2276         if (!bmap)
2277                 return NULL;
2278         n_out = isl_basic_map_n_out(bmap);
2279         domain = isl_basic_set_from_basic_map(bmap);
2280         return isl_basic_set_project_out(domain, n_out, 0);
2281 }
2282
2283 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2284 {
2285         return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2286 }
2287
2288 struct isl_set *isl_map_range(struct isl_map *map)
2289 {
2290         int i;
2291         struct isl_set *set;
2292
2293         if (!map)
2294                 goto error;
2295         map = isl_map_cow(map);
2296         if (!map)
2297                 goto error;
2298
2299         set = (struct isl_set *) map;
2300         if (set->dim->n_in != 0) {
2301                 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2302                 if (!set->dim)
2303                         goto error;
2304         }
2305         for (i = 0; i < map->n; ++i) {
2306                 set->p[i] = isl_basic_map_range(map->p[i]);
2307                 if (!set->p[i])
2308                         goto error;
2309         }
2310         F_CLR(set, ISL_MAP_DISJOINT);
2311         F_CLR(set, ISL_SET_NORMALIZED);
2312         return set;
2313 error:
2314         isl_map_free(map);
2315         return NULL;
2316 }
2317
2318 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
2319 {
2320         int i;
2321         struct isl_map *map = NULL;
2322
2323         set = isl_set_cow(set);
2324         if (!set || !dim)
2325                 goto error;
2326         isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
2327         map = (struct isl_map *)set;
2328         for (i = 0; i < set->n; ++i) {
2329                 map->p[i] = isl_basic_map_from_basic_set(
2330                                 set->p[i], isl_dim_copy(dim));
2331                 if (!map->p[i])
2332                         goto error;
2333         }
2334         isl_dim_free(map->dim);
2335         map->dim = dim;
2336         return map;
2337 error:
2338         isl_dim_free(dim);
2339         isl_set_free(set);
2340         return NULL;
2341 }
2342
2343 struct isl_set *isl_set_from_map(struct isl_map *map)
2344 {
2345         int i;
2346         struct isl_set *set = NULL;
2347
2348         if (!map)
2349                 return NULL;
2350         map = isl_map_cow(map);
2351         if (!map)
2352                 return NULL;
2353         map->dim = isl_dim_cow(map->dim);
2354         if (!map->dim)
2355                 goto error;
2356         map->dim->n_out += map->dim->n_in;
2357         map->dim->n_in = 0;
2358         set = (struct isl_set *)map;
2359         for (i = 0; i < map->n; ++i) {
2360                 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2361                 if (!set->p[i])
2362                         goto error;
2363         }
2364         return set;
2365 error:
2366         isl_map_free(map);
2367         return NULL;
2368 }
2369
2370 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
2371 {
2372         struct isl_map *map;
2373
2374         if (!dim)
2375                 return NULL;
2376         isl_assert(dim->ctx, n >= 0, return NULL);
2377         map = isl_alloc(dim->ctx, struct isl_map,
2378                         sizeof(struct isl_map) +
2379                         n * sizeof(struct isl_basic_map *));
2380         if (!map)
2381                 goto error;
2382
2383         map->ctx = dim->ctx;
2384         isl_ctx_ref(map->ctx);
2385         map->ref = 1;
2386         map->size = n;
2387         map->n = 0;
2388         map->dim = dim;
2389         map->flags = flags;
2390         return map;
2391 error:
2392         isl_dim_free(dim);
2393         return NULL;
2394 }
2395
2396 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2397                 unsigned nparam, unsigned in, unsigned out, int n,
2398                 unsigned flags)
2399 {
2400         struct isl_map *map;
2401         struct isl_dim *dims;
2402
2403         dims = isl_dim_alloc(ctx, nparam, in, out);
2404         if (!dims)
2405                 return NULL;
2406
2407         map = isl_map_alloc_dim(dims, n, flags);
2408         return map;
2409 }
2410
2411 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2412                 unsigned nparam, unsigned in, unsigned out)
2413 {
2414         struct isl_basic_map *bmap;
2415         bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2416         bmap = isl_basic_map_set_to_empty(bmap);
2417         return bmap;
2418 }
2419
2420 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
2421 {
2422         struct isl_basic_set *bset;
2423         bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
2424         bset = isl_basic_set_set_to_empty(bset);
2425         return bset;
2426 }
2427
2428 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
2429 {
2430         struct isl_basic_map *bmap;
2431         if (!model)
2432                 return NULL;
2433         bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2434         bmap = isl_basic_map_set_to_empty(bmap);
2435         return bmap;
2436 }
2437
2438 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
2439 {
2440         struct isl_basic_map *bmap;
2441         if (!model)
2442                 return NULL;
2443         bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2444         bmap = isl_basic_map_set_to_empty(bmap);
2445         return bmap;
2446 }
2447
2448 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
2449 {
2450         struct isl_basic_set *bset;
2451         if (!model)
2452                 return NULL;
2453         bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
2454         bset = isl_basic_set_set_to_empty(bset);
2455         return bset;
2456 }
2457
2458 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
2459 {
2460         struct isl_basic_map *bmap;
2461         bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
2462         return bmap;
2463 }
2464
2465 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
2466 {
2467         struct isl_basic_set *bset;
2468         bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
2469         return bset;
2470 }
2471
2472 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
2473 {
2474         if (!model)
2475                 return NULL;
2476         return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
2477 }
2478
2479 struct isl_map *isl_map_empty(struct isl_ctx *ctx,
2480                 unsigned nparam, unsigned in, unsigned out)
2481 {
2482         return isl_map_alloc(ctx, nparam, in, out, 0, ISL_MAP_DISJOINT);
2483 }
2484
2485 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
2486 {
2487         if (!model)
2488                 return NULL;
2489         return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
2490 }
2491
2492 struct isl_set *isl_set_empty(struct isl_dim *dim)
2493 {
2494         return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
2495 }
2496
2497 struct isl_set *isl_set_empty_like(struct isl_set *model)
2498 {
2499         if (!model)
2500                 return NULL;
2501         return isl_set_empty(isl_dim_copy(model->dim));
2502 }
2503
2504 struct isl_set *isl_set_universe(struct isl_dim *dim)
2505 {
2506         struct isl_set *set;
2507         if (!dim)
2508                 return NULL;
2509         set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
2510         set = isl_set_add(set, isl_basic_set_universe(dim));
2511         return set;
2512 }
2513
2514 struct isl_map *isl_map_dup(struct isl_map *map)
2515 {
2516         int i;
2517         struct isl_map *dup;
2518
2519         if (!map)
2520                 return NULL;
2521         dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
2522         for (i = 0; i < map->n; ++i)
2523                 dup = isl_map_add(dup, isl_basic_map_copy(map->p[i]));
2524         return dup;
2525 }
2526
2527 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2528 {
2529         if (!bmap || !map)
2530                 goto error;
2531         isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
2532         isl_assert(map->ctx, map->n < map->size, goto error);
2533         map->p[map->n] = bmap;
2534         map->n++;
2535         F_CLR(map, ISL_MAP_NORMALIZED);
2536         return map;
2537 error:
2538         if (map)
2539                 isl_map_free(map);
2540         if (bmap)
2541                 isl_basic_map_free(bmap);
2542         return NULL;
2543 }
2544
2545 void isl_map_free(struct isl_map *map)
2546 {
2547         int i;
2548
2549         if (!map)
2550                 return;
2551
2552         if (--map->ref > 0)
2553                 return;
2554
2555         isl_ctx_deref(map->ctx);
2556         for (i = 0; i < map->n; ++i)
2557                 isl_basic_map_free(map->p[i]);
2558         isl_dim_free(map->dim);
2559         free(map);
2560 }
2561
2562 struct isl_map *isl_map_extend(struct isl_map *base,
2563                 unsigned nparam, unsigned n_in, unsigned n_out)
2564 {
2565         int i;
2566
2567         base = isl_map_cow(base);
2568         if (!base)
2569                 return NULL;
2570
2571         base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
2572         if (!base->dim)
2573                 goto error;
2574         for (i = 0; i < base->n; ++i) {
2575                 base->p[i] = isl_basic_map_extend_dim(base->p[i],
2576                                 isl_dim_copy(base->dim), 0, 0, 0);
2577                 if (!base->p[i])
2578                         goto error;
2579         }
2580         return base;
2581 error:
2582         isl_map_free(base);
2583         return NULL;
2584 }
2585
2586 struct isl_set *isl_set_extend(struct isl_set *base,
2587                 unsigned nparam, unsigned dim)
2588 {
2589         return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2590                                                         nparam, 0, dim);
2591 }
2592
2593 static struct isl_basic_map *isl_basic_map_fix_pos(struct isl_basic_map *bmap,
2594                 unsigned pos, int value)
2595 {
2596         int j;
2597
2598         bmap = isl_basic_map_cow(bmap);
2599         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
2600         j = isl_basic_map_alloc_equality(bmap);
2601         if (j < 0)
2602                 goto error;
2603         isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
2604         isl_int_set_si(bmap->eq[j][pos], -1);
2605         isl_int_set_si(bmap->eq[j][0], value);
2606         bmap = isl_basic_map_simplify(bmap);
2607         return isl_basic_map_finalize(bmap);
2608 error:
2609         isl_basic_map_free(bmap);
2610         return NULL;
2611 }
2612
2613 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
2614                 enum isl_dim_type type, unsigned pos, int value)
2615 {
2616         if (!bmap)
2617                 return NULL;
2618         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
2619         return isl_basic_map_fix_pos(bmap, basic_map_offset(bmap, type) + pos,
2620                                         value);
2621 error:
2622         isl_basic_map_free(bmap);
2623         return NULL;
2624 }
2625
2626 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
2627                 enum isl_dim_type type, unsigned pos, int value)
2628 {
2629         return (struct isl_basic_set *)
2630                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2631                                         type, pos, value);
2632 }
2633
2634 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2635                 unsigned input, int value)
2636 {
2637         return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
2638 }
2639
2640 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2641                 unsigned dim, int value)
2642 {
2643         return (struct isl_basic_set *)
2644                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
2645                                         isl_dim_set, dim, value);
2646 }
2647
2648 struct isl_map *isl_map_fix_si(struct isl_map *map,
2649                 enum isl_dim_type type, unsigned pos, int value)
2650 {
2651         int i;
2652
2653         map = isl_map_cow(map);
2654         if (!map)
2655                 return NULL;
2656
2657         isl_assert(ctx, pos < isl_map_dim(map, type), goto error);
2658         for (i = 0; i < map->n; ++i) {
2659                 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
2660                 if (!map->p[i])
2661                         goto error;
2662         }
2663         F_CLR(map, ISL_MAP_NORMALIZED);
2664         return map;
2665 error:
2666         isl_map_free(map);
2667         return NULL;
2668 }
2669
2670 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2671                 unsigned input, int value)
2672 {
2673         return isl_map_fix_si(map, isl_dim_in, input, value);
2674 }
2675
2676 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2677 {
2678         return (struct isl_set *)
2679                 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
2680 }
2681
2682 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
2683         unsigned dim, isl_int value)
2684 {
2685         int j;
2686         unsigned nparam;
2687
2688         bset = isl_basic_set_cow(bset);
2689         bset = isl_basic_set_extend_constraints(bset, 0, 1);
2690         j = isl_basic_set_alloc_inequality(bset);
2691         if (j < 0)
2692                 goto error;
2693         isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
2694         isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
2695         isl_int_neg(bset->ineq[j][0], value);
2696         bset = isl_basic_set_simplify(bset);
2697         return isl_basic_set_finalize(bset);
2698 error:
2699         isl_basic_set_free(bset);
2700         return NULL;
2701 }
2702
2703 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
2704                                         isl_int value)
2705 {
2706         int i;
2707
2708         set = isl_set_cow(set);
2709         if (!set)
2710                 return NULL;
2711
2712         isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
2713         for (i = 0; i < set->n; ++i) {
2714                 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
2715                 if (!set->p[i])
2716                         goto error;
2717         }
2718         return set;
2719 error:
2720         isl_set_free(set);
2721         return NULL;
2722 }
2723
2724 struct isl_map *isl_map_reverse(struct isl_map *map)
2725 {
2726         int i;
2727         unsigned t;
2728
2729         map = isl_map_cow(map);
2730         if (!map)
2731                 return NULL;
2732
2733         map->dim = isl_dim_reverse(map->dim);
2734         if (!map->dim)
2735                 goto error;
2736         for (i = 0; i < map->n; ++i) {
2737                 map->p[i] = isl_basic_map_reverse(map->p[i]);
2738                 if (!map->p[i])
2739                         goto error;
2740         }
2741         F_CLR(map, ISL_MAP_NORMALIZED);
2742         return map;
2743 error:
2744         isl_map_free(map);
2745         return NULL;
2746 }
2747
2748 struct isl_map *isl_basic_map_lexmax(
2749                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2750                 struct isl_set **empty)
2751 {
2752         return isl_pip_basic_map_lexmax(bmap, dom, empty);
2753 }
2754
2755 struct isl_map *isl_basic_map_lexmin(
2756                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2757                 struct isl_set **empty)
2758 {
2759         return isl_pip_basic_map_lexmin(bmap, dom, empty);
2760 }
2761
2762 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2763 {
2764         struct isl_basic_map *bmap = NULL;
2765         struct isl_basic_set *dom = NULL;
2766         struct isl_map *min;
2767         struct isl_dim *param_dim;
2768
2769         if (!bset)
2770                 goto error;
2771         bmap = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
2772         if (!bmap)
2773                 goto error;
2774         param_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
2775         dom = isl_basic_set_universe(param_dim);
2776         if (!dom)
2777                 goto error;
2778         min = isl_basic_map_lexmin(bmap, dom, NULL);
2779         return isl_map_range(min);
2780 error:
2781         isl_basic_map_free(bmap);
2782         return NULL;
2783 }
2784
2785 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2786 {
2787         int i;
2788         unsigned off;
2789
2790         if (!bmap)
2791                 return NULL;
2792         off = isl_dim_total(bmap->dim);
2793         for (i = 0; i < bmap->n_div; ++i) {
2794                 if (isl_int_is_zero(bmap->div[i][0]))
2795                         return isl_pip_basic_map_compute_divs(bmap);
2796                 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
2797                                 goto error);
2798         }
2799         return isl_map_from_basic_map(bmap);
2800 error:
2801         isl_basic_map_free(bmap);
2802         return NULL;
2803 }
2804
2805 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2806 {
2807         int i;
2808         struct isl_map *res;
2809
2810         if (!map)
2811                 return NULL;
2812         if (map->n == 0)
2813                 return map;
2814         res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2815         for (i = 1 ; i < map->n; ++i) {
2816                 struct isl_map *r2;
2817                 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2818                 if (F_ISSET(map, ISL_MAP_DISJOINT))
2819                         res = isl_map_union_disjoint(res, r2);
2820                 else
2821                         res = isl_map_union(res, r2);
2822         }
2823         isl_map_free(map);
2824
2825         return res;
2826 }
2827
2828 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2829 {
2830         return (struct isl_set *)
2831                 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2832 }
2833
2834 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2835 {
2836         return (struct isl_set *)
2837                 isl_map_compute_divs((struct isl_map *)set);
2838 }
2839
2840 struct isl_set *isl_map_domain(struct isl_map *map)
2841 {
2842         int i;
2843         struct isl_set *set;
2844
2845         if (!map)
2846                 goto error;
2847
2848         map = isl_map_cow(map);
2849         if (!map)
2850                 return NULL;
2851
2852         set = (struct isl_set *)map;
2853         set->dim = isl_dim_domain(set->dim);
2854         if (!set->dim)
2855                 goto error;
2856         for (i = 0; i < map->n; ++i) {
2857                 set->p[i] = isl_basic_map_domain(map->p[i]);
2858                 if (!set->p[i])
2859                         goto error;
2860         }
2861         F_CLR(set, ISL_MAP_DISJOINT);
2862         F_CLR(set, ISL_SET_NORMALIZED);
2863         return set;
2864 error:
2865         isl_map_free(map);
2866         return NULL;
2867 }
2868
2869 struct isl_map *isl_map_union_disjoint(
2870                         struct isl_map *map1, struct isl_map *map2)
2871 {
2872         int i;
2873         unsigned flags = 0;
2874         struct isl_map *map = NULL;
2875
2876         if (!map1 || !map2)
2877                 goto error;
2878
2879         if (map1->n == 0) {
2880                 isl_map_free(map1);
2881                 return map2;
2882         }
2883         if (map2->n == 0) {
2884                 isl_map_free(map2);
2885                 return map1;
2886         }
2887
2888         isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2889
2890         if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
2891             F_ISSET(map2, ISL_MAP_DISJOINT))
2892                 FL_SET(flags, ISL_MAP_DISJOINT);
2893
2894         map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2895                                 map1->n + map2->n, flags);
2896         if (!map)
2897                 goto error;
2898         for (i = 0; i < map1->n; ++i) {
2899                 map = isl_map_add(map,
2900                                   isl_basic_map_copy(map1->p[i]));
2901                 if (!map)
2902                         goto error;
2903         }
2904         for (i = 0; i < map2->n; ++i) {
2905                 map = isl_map_add(map,
2906                                   isl_basic_map_copy(map2->p[i]));
2907                 if (!map)
2908                         goto error;
2909         }
2910         isl_map_free(map1);
2911         isl_map_free(map2);
2912         return map;
2913 error:
2914         isl_map_free(map);
2915         isl_map_free(map1);
2916         isl_map_free(map2);
2917         return NULL;
2918 }
2919
2920 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
2921 {
2922         map1 = isl_map_union_disjoint(map1, map2);
2923         if (!map1)
2924                 return NULL;
2925         if (map1->n > 1)
2926                 F_CLR(map1, ISL_MAP_DISJOINT);
2927         return map1;
2928 }
2929
2930 struct isl_set *isl_set_union_disjoint(
2931                         struct isl_set *set1, struct isl_set *set2)
2932 {
2933         return (struct isl_set *)
2934                 isl_map_union_disjoint(
2935                         (struct isl_map *)set1, (struct isl_map *)set2);
2936 }
2937
2938 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
2939 {
2940         return (struct isl_set *)
2941                 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
2942 }
2943
2944 struct isl_map *isl_map_intersect_range(
2945                 struct isl_map *map, struct isl_set *set)
2946 {
2947         unsigned flags = 0;
2948         struct isl_map *result;
2949         int i, j;
2950
2951         if (!map || !set)
2952                 goto error;
2953
2954         if (F_ISSET(map, ISL_MAP_DISJOINT) &&
2955             F_ISSET(set, ISL_MAP_DISJOINT))
2956                 FL_SET(flags, ISL_MAP_DISJOINT);
2957
2958         result = isl_map_alloc_dim(isl_dim_copy(map->dim),
2959                                         map->n * set->n, flags);
2960         if (!result)
2961                 goto error;
2962         for (i = 0; i < map->n; ++i)
2963                 for (j = 0; j < set->n; ++j) {
2964                         result = isl_map_add(result,
2965                             isl_basic_map_intersect_range(
2966                                 isl_basic_map_copy(map->p[i]),
2967                                 isl_basic_set_copy(set->p[j])));
2968                         if (!result)
2969                                 goto error;
2970                 }
2971         isl_map_free(map);
2972         isl_set_free(set);
2973         return result;
2974 error:
2975         isl_map_free(map);
2976         isl_set_free(set);
2977         return NULL;
2978 }
2979
2980 struct isl_map *isl_map_intersect_domain(
2981                 struct isl_map *map, struct isl_set *set)
2982 {
2983         return isl_map_reverse(
2984                 isl_map_intersect_range(isl_map_reverse(map), set));
2985 }
2986
2987 struct isl_map *isl_map_apply_domain(
2988                 struct isl_map *map1, struct isl_map *map2)
2989 {
2990         if (!map1 || !map2)
2991                 goto error;
2992         map1 = isl_map_reverse(map1);
2993         map1 = isl_map_apply_range(map1, map2);
2994         return isl_map_reverse(map1);
2995 error:
2996         isl_map_free(map1);
2997         isl_map_free(map2);
2998         return NULL;
2999 }
3000
3001 struct isl_map *isl_map_apply_range(
3002                 struct isl_map *map1, struct isl_map *map2)
3003 {
3004         struct isl_dim *dim_result;
3005         struct isl_map *result;
3006         int i, j;
3007         unsigned nparam;
3008         unsigned n_in;
3009         unsigned n_out;
3010
3011         if (!map1 || !map2)
3012                 goto error;
3013
3014         dim_result = isl_dim_join(isl_dim_copy(map1->dim),
3015                                   isl_dim_copy(map2->dim));
3016
3017         result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
3018         if (!result)
3019                 goto error;
3020         for (i = 0; i < map1->n; ++i)
3021                 for (j = 0; j < map2->n; ++j) {
3022                         result = isl_map_add(result,
3023                             isl_basic_map_apply_range(
3024                                 isl_basic_map_copy(map1->p[i]),
3025                                 isl_basic_map_copy(map2->p[j])));
3026                         if (!result)
3027                                 goto error;
3028                 }
3029         isl_map_free(map1);
3030         isl_map_free(map2);
3031         if (result && result->n <= 1)
3032                 F_SET(result, ISL_MAP_DISJOINT);
3033         return result;
3034 error:
3035         isl_map_free(map1);
3036         isl_map_free(map2);
3037         return NULL;
3038 }
3039
3040 /*
3041  * returns range - domain
3042  */
3043 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
3044 {
3045         struct isl_basic_set *bset;
3046         unsigned dim;
3047         unsigned nparam;
3048         int i;
3049
3050         if (!bmap)
3051                 goto error;
3052         dim = isl_basic_map_n_in(bmap);
3053         nparam = isl_basic_map_n_param(bmap);
3054         isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
3055         bset = isl_basic_set_from_basic_map(bmap);
3056         bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
3057         bset = isl_basic_set_swap_vars(bset, 2*dim);
3058         for (i = 0; i < dim; ++i) {
3059                 int j = isl_basic_map_alloc_equality(
3060                                             (struct isl_basic_map *)bset);
3061                 if (j < 0)
3062                         goto error;
3063                 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
3064                 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
3065                 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
3066                 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
3067         }
3068         return isl_basic_set_project_out(bset, 2*dim, 0);
3069 error:
3070         isl_basic_map_free(bmap);
3071         return NULL;
3072 }
3073
3074 /*
3075  * returns range - domain
3076  */
3077 struct isl_set *isl_map_deltas(struct isl_map *map)
3078 {
3079         int i;
3080         struct isl_set *result;
3081
3082         if (!map)
3083                 return NULL;
3084
3085         isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
3086         result = isl_set_alloc(map->ctx, isl_map_n_param(map),
3087                                         isl_map_n_in(map), map->n, map->flags);
3088         if (!result)
3089                 goto error;
3090         for (i = 0; i < map->n; ++i)
3091                 result = isl_set_add(result,
3092                           isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
3093         isl_map_free(map);
3094         return result;
3095 error:
3096         isl_map_free(map);
3097         return NULL;
3098 }
3099
3100 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
3101 {
3102         struct isl_basic_map *bmap;
3103         unsigned nparam;
3104         unsigned dim;
3105         int i;
3106
3107         if (!dims)
3108                 return NULL;
3109
3110         nparam = dims->nparam;
3111         dim = dims->n_out;
3112         bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
3113         if (!bmap)
3114                 goto error;
3115
3116         for (i = 0; i < dim; ++i) {
3117                 int j = isl_basic_map_alloc_equality(bmap);
3118                 if (j < 0)
3119                         goto error;
3120                 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
3121                 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3122                 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3123         }
3124         return isl_basic_map_finalize(bmap);
3125 error:
3126         isl_basic_map_free(bmap);
3127         return NULL;
3128 }
3129
3130 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
3131 {
3132         struct isl_dim *dim = isl_dim_map(set_dim);
3133         if (!dim)
3134                 return NULL;
3135         return basic_map_identity(dim);
3136 }
3137
3138 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
3139 {
3140         if (!model || !model->dim)
3141                 return NULL;
3142         isl_assert(model->ctx,
3143                         model->dim->n_in == model->dim->n_out, return NULL);
3144         return basic_map_identity(isl_dim_copy(model->dim));
3145 }
3146
3147 static struct isl_map *map_identity(struct isl_dim *dim)
3148 {
3149         struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
3150         return isl_map_add(map, basic_map_identity(isl_dim_copy(dim)));
3151 }
3152
3153 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
3154 {
3155         struct isl_dim *dim = isl_dim_map(set_dim);
3156         if (!dim)
3157                 return NULL;
3158         return map_identity(dim);
3159 }
3160
3161 struct isl_map *isl_map_identity_like(struct isl_basic_map *model)
3162 {
3163         if (!model || !model->dim)
3164                 return NULL;
3165         isl_assert(model->ctx,
3166                         model->dim->n_in == model->dim->n_out, return NULL);
3167         return map_identity(isl_dim_copy(model->dim));
3168 }
3169
3170 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3171 {
3172         return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3173 }
3174
3175 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3176 {
3177         return isl_map_is_subset(
3178                         (struct isl_map *)set1, (struct isl_map *)set2);
3179 }
3180
3181 int isl_basic_map_is_subset(
3182                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3183 {
3184         int is_subset;
3185         struct isl_map *map1;
3186         struct isl_map *map2;
3187
3188         if (!bmap1 || !bmap2)
3189                 return -1;
3190
3191         map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3192         map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3193
3194         is_subset = isl_map_is_subset(map1, map2);
3195
3196         isl_map_free(map1);
3197         isl_map_free(map2);
3198
3199         return is_subset;
3200 }
3201
3202 int isl_basic_map_is_equal(
3203                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3204 {
3205         int is_subset;
3206
3207         if (!bmap1 || !bmap2)
3208                 return -1;
3209         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3210         if (is_subset != 1)
3211                 return is_subset;
3212         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3213         return is_subset;
3214 }
3215
3216 int isl_basic_set_is_equal(
3217                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3218 {
3219         return isl_basic_map_is_equal(
3220                 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3221 }
3222
3223 int isl_map_is_empty(struct isl_map *map)
3224 {
3225         int i;
3226         int is_empty;
3227
3228         if (!map)
3229                 return -1;
3230         for (i = 0; i < map->n; ++i) {
3231                 is_empty = isl_basic_map_is_empty(map->p[i]);
3232                 if (is_empty < 0)
3233                         return -1;
3234                 if (!is_empty)
3235                         return 0;
3236         }
3237         return 1;
3238 }
3239
3240 int isl_set_is_empty(struct isl_set *set)
3241 {
3242         return isl_map_is_empty((struct isl_map *)set);
3243 }
3244
3245 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3246 {
3247         int i;
3248         int is_subset = 0;
3249         struct isl_map *diff;
3250
3251         if (!map1 || !map2)
3252                 return -1;
3253
3254         if (isl_map_is_empty(map1))
3255                 return 1;
3256
3257         if (isl_map_is_empty(map2))
3258                 return 0;
3259
3260         diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3261         if (!diff)
3262                 return -1;
3263
3264         is_subset = isl_map_is_empty(diff);
3265         isl_map_free(diff);
3266
3267         return is_subset;
3268 }
3269
3270 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3271 {
3272         int is_subset;
3273
3274         if (!map1 || !map2)
3275                 return -1;
3276         is_subset = isl_map_is_subset(map1, map2);
3277         if (is_subset != 1)
3278                 return is_subset;
3279         is_subset = isl_map_is_subset(map2, map1);
3280         return is_subset;
3281 }
3282
3283 int isl_basic_map_is_strict_subset(
3284                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3285 {
3286         int is_subset;
3287
3288         if (!bmap1 || !bmap2)
3289                 return -1;
3290         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3291         if (is_subset != 1)
3292                 return is_subset;
3293         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3294         if (is_subset == -1)
3295                 return is_subset;
3296         return !is_subset;
3297 }
3298
3299 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
3300 {
3301         int i;
3302         unsigned total;
3303         isl_int s;
3304
3305         total = 1 + isl_basic_map_total_dim(bmap);
3306         if (total != vec->size)
3307                 return -1;
3308
3309         isl_int_init(s);
3310
3311         for (i = 0; i < bmap->n_eq; ++i) {
3312                 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
3313                 if (!isl_int_is_zero(s)) {
3314                         isl_int_clear(s);
3315                         return 0;
3316                 }
3317         }
3318
3319         for (i = 0; i < bmap->n_ineq; ++i) {
3320                 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
3321                 if (isl_int_is_neg(s)) {
3322                         isl_int_clear(s);
3323                         return 0;
3324                 }
3325         }
3326
3327         isl_int_clear(s);
3328
3329         return 1;
3330 }
3331
3332 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3333 {
3334         if (!bmap)
3335                 return -1;
3336         return bmap->n_eq == 0 && bmap->n_ineq == 0;
3337 }
3338
3339 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3340 {
3341         struct isl_basic_set *bset = NULL;
3342         struct isl_vec *sample = NULL;
3343         int empty;
3344         unsigned total;
3345
3346         if (!bmap)
3347                 return -1;
3348
3349         if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3350                 return 1;
3351
3352         if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3353                 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3354                 copy = isl_basic_map_convex_hull(copy);
3355                 empty = F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3356                 isl_basic_map_free(copy);
3357                 return empty;
3358         }
3359
3360         total = 1 + isl_basic_map_total_dim(bmap);
3361         if (bmap->sample && bmap->sample->size == total) {
3362                 int contains = basic_map_contains(bmap, bmap->sample);
3363                 if (contains < 0)
3364                         return -1;
3365                 if (contains)
3366                         return 0;
3367         }
3368         bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3369         if (!bset)
3370                 return -1;
3371         sample = isl_basic_set_sample(bset);
3372         if (!sample)
3373                 return -1;
3374         empty = sample->size == 0;
3375         if (bmap->sample)
3376                 isl_vec_free(bmap->ctx, bmap->sample);
3377         bmap->sample = sample;
3378
3379         return empty;
3380 }
3381
3382 int isl_basic_set_is_empty(struct isl_basic_set *bset)
3383 {
3384         return isl_basic_map_is_empty((struct isl_basic_map *)bset);
3385 }
3386
3387 struct isl_map *isl_basic_map_union(
3388         struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3389 {
3390         struct isl_map *map;
3391         if (!bmap1 || !bmap2)
3392                 return NULL;
3393
3394         isl_assert(map1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
3395
3396         map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
3397         if (!map)
3398                 goto error;
3399         map = isl_map_add(map, bmap1);
3400         map = isl_map_add(map, bmap2);
3401         return map;
3402 error:
3403         isl_basic_map_free(bmap1);
3404         isl_basic_map_free(bmap2);
3405         return NULL;
3406 }
3407
3408 struct isl_set *isl_basic_set_union(
3409                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3410 {
3411         return (struct isl_set *)isl_basic_map_union(
3412                                             (struct isl_basic_map *)bset1,
3413                                             (struct isl_basic_map *)bset2);
3414 }
3415
3416 /* Order divs such that any div only depends on previous divs */
3417 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3418 {
3419         int i;
3420         unsigned off = isl_dim_total(bmap->dim);
3421
3422         for (i = 0; i < bmap->n_div; ++i) {
3423                 int pos;
3424                 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3425                                                             bmap->n_div-i);
3426                 if (pos == -1)
3427                         continue;
3428                 swap_div(bmap, i, pos);
3429                 --i;
3430         }
3431         return bmap;
3432 }
3433
3434 /* Look for a div in dst that corresponds to the div "div" in src.
3435  * The divs before "div" in src and dst are assumed to be the same.
3436  * 
3437  * Returns -1 if no corresponding div was found and the position
3438  * of the corresponding div in dst otherwise.
3439  */
3440 static int find_div(struct isl_basic_map *dst,
3441                         struct isl_basic_map *src, unsigned div)
3442 {
3443         int i;
3444
3445         unsigned total = isl_dim_total(src->dim);
3446
3447         isl_assert(dst->ctx, div <= dst->n_div, return -1);
3448         for (i = div; i < dst->n_div; ++i)
3449                 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
3450                     isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
3451                                                 dst->n_div - div) == -1)
3452                         return i;
3453         return -1;
3454 }
3455
3456 struct isl_basic_map *isl_basic_map_align_divs(
3457                 struct isl_basic_map *dst, struct isl_basic_map *src)
3458 {
3459         int i;
3460         unsigned total = isl_dim_total(src->dim);
3461
3462         if (!dst || !src)
3463                 goto error;
3464
3465         if (src->n_div == 0)
3466                 return dst;
3467
3468         for (i = 0; i < src->n_div; ++i)
3469                 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
3470
3471         src = order_divs(src);
3472         dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
3473                         src->n_div, 0, 2 * src->n_div);
3474         if (!dst)
3475                 return NULL;
3476         for (i = 0; i < src->n_div; ++i) {
3477                 int j = find_div(dst, src, i);
3478                 if (j < 0) {
3479                         j = isl_basic_map_alloc_div(dst);
3480                         if (j < 0)
3481                                 goto error;
3482                         isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
3483                         isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
3484                         if (add_div_constraints(dst, j) < 0)
3485                                 goto error;
3486                 }
3487                 if (j != i)
3488                         swap_div(dst, i, j);
3489         }
3490         return dst;
3491 error:
3492         isl_basic_map_free(dst);
3493         return NULL;
3494 }
3495
3496 struct isl_basic_set *isl_basic_set_align_divs(
3497                 struct isl_basic_set *dst, struct isl_basic_set *src)
3498 {
3499         return (struct isl_basic_set *)isl_basic_map_align_divs(
3500                 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
3501 }
3502
3503 struct isl_map *isl_map_align_divs(struct isl_map *map)
3504 {
3505         int i;
3506
3507         map = isl_map_compute_divs(map);
3508         map = isl_map_cow(map);
3509         if (!map)
3510                 return NULL;
3511
3512         for (i = 1; i < map->n; ++i)
3513                 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3514         for (i = 1; i < map->n; ++i)
3515                 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3516
3517         F_CLR(map, ISL_MAP_NORMALIZED);
3518         return map;
3519 }
3520
3521 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3522                 struct isl_basic_map *src, isl_int *c,
3523                 unsigned len, int oppose)
3524 {
3525         struct isl_basic_map *copy = NULL;
3526         int is_empty;
3527         int k;
3528         unsigned total;
3529
3530         copy = isl_basic_map_copy(src);
3531         copy = isl_basic_map_cow(copy);
3532         if (!copy)
3533                 goto error;
3534         copy = isl_basic_map_extend_constraints(copy, 0, 1);
3535         k = isl_basic_map_alloc_inequality(copy);
3536         if (k < 0)
3537                 goto error;
3538         if (oppose)
3539                 isl_seq_neg(copy->ineq[k], c, len);
3540         else
3541                 isl_seq_cpy(copy->ineq[k], c, len);
3542         total = 1 + isl_basic_map_total_dim(copy);
3543         isl_seq_clr(copy->ineq[k]+len, total - len);
3544         isl_inequality_negate(copy, k);
3545         copy = isl_basic_map_simplify(copy);
3546         copy = isl_basic_map_finalize(copy);
3547         is_empty = isl_basic_map_is_empty(copy);
3548         if (is_empty < 0)
3549                 goto error;
3550         if (!is_empty)
3551                 dst = isl_map_add(dst, copy);
3552         else
3553                 isl_basic_map_free(copy);
3554         return dst;
3555 error:
3556         isl_basic_map_free(copy);
3557         isl_map_free(dst);
3558         return NULL;
3559 }
3560
3561 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3562 {
3563         int i, j, k;
3564         unsigned flags = 0;
3565         struct isl_map *rest = NULL;
3566         unsigned max;
3567         unsigned total = isl_basic_map_total_dim(bmap);
3568
3569         assert(bmap);
3570
3571         if (!map)
3572                 goto error;
3573
3574         if (F_ISSET(map, ISL_MAP_DISJOINT))
3575                 FL_SET(flags, ISL_MAP_DISJOINT);
3576
3577         max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3578         rest = isl_map_alloc_dim(isl_dim_copy(map->dim), max, flags);
3579         if (!rest)
3580                 goto error;
3581
3582         for (i = 0; i < map->n; ++i) {
3583                 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3584                 if (!map->p[i])
3585                         goto error;
3586         }
3587
3588         for (j = 0; j < map->n; ++j)
3589                 map->p[j] = isl_basic_map_cow(map->p[j]);
3590
3591         for (i = 0; i < bmap->n_eq; ++i) {
3592                 for (j = 0; j < map->n; ++j) {
3593                         rest = add_cut_constraint(rest,
3594                                 map->p[j], bmap->eq[i], 1+total, 0);
3595                         if (!rest)
3596                                 goto error;
3597
3598                         rest = add_cut_constraint(rest,
3599                                 map->p[j], bmap->eq[i], 1+total, 1);
3600                         if (!rest)
3601                                 goto error;
3602
3603                         map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3604                                 1, 0);
3605                         if (!map->p[j])
3606                                 goto error;
3607                         k = isl_basic_map_alloc_equality(map->p[j]);
3608                         if (k < 0)
3609                                 goto error;
3610                         isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3611                         isl_seq_clr(map->p[j]->eq[k]+1+total,
3612                                         map->p[j]->n_div - bmap->n_div);
3613                 }
3614         }
3615
3616         for (i = 0; i < bmap->n_ineq; ++i) {
3617                 for (j = 0; j < map->n; ++j) {
3618                         rest = add_cut_constraint(rest,
3619                                 map->p[j], bmap->ineq[i], 1+total, 0);
3620                         if (!rest)
3621                                 goto error;
3622
3623                         map->p[j] = isl_basic_map_extend_constraints(map->p[j],
3624                                 0, 1);
3625                         if (!map->p[j])
3626                                 goto error;
3627                         k = isl_basic_map_alloc_inequality(map->p[j]);
3628                         if (k < 0)
3629                                 goto error;
3630                         isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3631                         isl_seq_clr(map->p[j]->ineq[k]+1+total,
3632                                         map->p[j]->n_div - bmap->n_div);
3633                 }
3634         }
3635
3636         isl_map_free(map);
3637         return rest;
3638 error:
3639         isl_map_free(map);
3640         isl_map_free(rest);
3641         return NULL;
3642 }
3643
3644 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3645 {
3646         int i;
3647         if (!map1 || !map2)
3648                 goto error;
3649
3650         isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
3651
3652         if (isl_map_is_empty(map2)) {
3653                 isl_map_free(map2);
3654                 return map1;
3655         }
3656
3657         map1 = isl_map_compute_divs(map1);
3658         map2 = isl_map_compute_divs(map2);
3659         if (!map1 || !map2)
3660                 goto error;
3661
3662         for (i = 0; map1 && i < map2->n; ++i)
3663                 map1 = subtract(map1, map2->p[i]);
3664
3665         isl_map_free(map2);
3666         return map1;
3667 error:
3668         isl_map_free(map1);
3669         isl_map_free(map2);
3670         return NULL;
3671 }
3672
3673 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3674 {
3675         return (struct isl_set *)
3676                 isl_map_subtract(
3677                         (struct isl_map *)set1, (struct isl_map *)set2);
3678 }
3679
3680 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3681 {
3682         if (!set || !map)
3683                 goto error;
3684         isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
3685         map = isl_map_intersect_domain(map, set);
3686         set = isl_map_range(map);
3687         return set;
3688 error:
3689         isl_set_free(set);
3690         isl_map_free(map);
3691         return NULL;
3692 }
3693
3694 /* There is no need to cow as removing empty parts doesn't change
3695  * the meaning of the set.
3696  */
3697 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3698 {
3699         int i;
3700
3701         if (!map)
3702                 return NULL;
3703
3704         for (i = map->n-1; i >= 0; --i) {
3705                 if (!F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3706                         continue;
3707                 isl_basic_map_free(map->p[i]);
3708                 if (i != map->n-1) {
3709                         F_CLR(map, ISL_MAP_NORMALIZED);
3710                         map->p[i] = map->p[map->n-1];
3711                 }
3712                 map->n--;
3713         }
3714
3715         return map;
3716 }
3717
3718 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3719 {
3720         return (struct isl_set *)
3721                 isl_map_remove_empty_parts((struct isl_map *)set);
3722 }
3723
3724 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3725 {
3726         struct isl_basic_set *bset;
3727         if (!set || set->n == 0)
3728                 return NULL;
3729         bset = set->p[set->n-1];
3730         isl_assert(set->ctx, F_ISSET(bset, ISL_BASIC_SET_FINAL), return NULL);
3731         return isl_basic_set_copy(bset);
3732 }
3733
3734 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3735                                                 struct isl_basic_set *bset)
3736 {
3737         int i;
3738
3739         if (!set || !bset)
3740                 goto error;
3741         for (i = set->n-1; i >= 0; --i) {
3742                 if (set->p[i] != bset)
3743                         continue;
3744                 set = isl_set_cow(set);
3745                 if (!set)
3746                         goto error;
3747                 isl_basic_set_free(set->p[i]);
3748                 if (i != set->n-1) {
3749                         F_CLR(set, ISL_SET_NORMALIZED);
3750                         set->p[i] = set->p[set->n-1];
3751                 }
3752                 set->n--;
3753                 return set;
3754         }
3755         isl_basic_set_free(bset);
3756         return set;
3757 error:
3758         isl_set_free(set);
3759         isl_basic_set_free(bset);
3760         return NULL;
3761 }
3762
3763 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
3764  * for any common value of the parameters and dimensions preceding dim
3765  * in both basic sets, the values of dimension pos in bset1 are
3766  * smaller or larger than those in bset2.
3767  *
3768  * Returns
3769  *       1 if bset1 follows bset2
3770  *      -1 if bset1 precedes bset2
3771  *       0 if bset1 and bset2 are incomparable
3772  *      -2 if some error occurred.
3773  */
3774 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
3775         struct isl_basic_set *bset2, int pos)
3776 {
3777         struct isl_dim *dims;
3778         struct isl_basic_map *bmap1 = NULL;
3779         struct isl_basic_map *bmap2 = NULL;
3780         struct isl_ctx *ctx;
3781         struct isl_vec *obj;
3782         unsigned total;
3783         unsigned nparam;
3784         unsigned dim1, dim2;
3785         isl_int num, den;
3786         enum isl_lp_result res;
3787         int cmp;
3788
3789         if (!bset1 || !bset2)
3790                 return -2;
3791
3792         nparam = isl_basic_set_n_param(bset1);
3793         dim1 = isl_basic_set_n_dim(bset1);
3794         dim2 = isl_basic_set_n_dim(bset2);
3795         dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
3796         bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
3797         dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
3798         bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
3799         if (!bmap1 || !bmap2)
3800                 goto error;
3801         bmap1 = isl_basic_map_extend(bmap1, nparam,
3802                         pos, (dim1 - pos) + (dim2 - pos),
3803                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
3804         bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
3805         if (!bmap1)
3806                 goto error;
3807         total = isl_basic_map_total_dim(bmap1);
3808         ctx = bmap1->ctx;
3809         obj = isl_vec_alloc(ctx, total);
3810         isl_seq_clr(obj->block.data, total);
3811         isl_int_set_si(obj->block.data[nparam+pos], 1);
3812         isl_int_set_si(obj->block.data[nparam+pos+(dim1-pos)], -1);
3813         if (!obj)
3814                 goto error;
3815         isl_int_init(num);
3816         isl_int_init(den);
3817         res = isl_solve_lp(bmap1, 0, obj->block.data, ctx->one, &num, &den);
3818         if (res == isl_lp_empty)
3819                 cmp = 0;
3820         else if (res == isl_lp_ok && isl_int_is_pos(num))
3821                 cmp = 1;
3822         else if ((res == isl_lp_ok && isl_int_is_neg(num)) ||
3823                   res == isl_lp_unbounded)
3824                 cmp = -1;
3825         else
3826                 cmp = -2;
3827         isl_int_clear(num);
3828         isl_int_clear(den);
3829         isl_basic_map_free(bmap1);
3830         isl_vec_free(ctx, obj);
3831         return cmp;
3832 error:
3833         isl_basic_map_free(bmap1);
3834         isl_basic_map_free(bmap2);
3835         return -2;
3836 }
3837
3838 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
3839         unsigned pos, isl_int *val)
3840 {
3841         int i;
3842         int d;
3843         unsigned total;
3844
3845         if (!bmap)
3846                 return -1;
3847         total = isl_basic_map_total_dim(bmap);
3848         for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
3849                 for (; d+1 > pos; --d)
3850                         if (!isl_int_is_zero(bmap->eq[i][1+d]))
3851                                 break;
3852                 if (d != pos)
3853                         continue;
3854                 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
3855                         return 0;
3856                 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
3857                         return 0;
3858                 if (!isl_int_is_one(bmap->eq[i][1+d]))
3859                         return 0;
3860                 if (val)
3861                         isl_int_neg(*val, bmap->eq[i][0]);
3862                 return 1;
3863         }
3864         return 0;
3865 }
3866
3867 static int isl_map_fast_has_fixed_var(struct isl_map *map,
3868         unsigned pos, isl_int *val)
3869 {
3870         int i;
3871         isl_int v;
3872         isl_int tmp;
3873         int fixed;
3874
3875         if (!map)
3876                 return -1;
3877         if (map->n == 0)
3878                 return 0;
3879         if (map->n == 1)
3880                 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val); 
3881         isl_int_init(v);
3882         isl_int_init(tmp);
3883         fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v); 
3884         for (i = 1; fixed == 1 && i < map->n; ++i) {
3885                 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp); 
3886                 if (fixed == 1 && isl_int_ne(tmp, v))
3887                         fixed = 0;
3888         }
3889         if (val)
3890                 isl_int_set(*val, v);
3891         isl_int_clear(tmp);
3892         isl_int_clear(v);
3893         return fixed;
3894 }
3895
3896 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
3897         isl_int *val)
3898 {
3899         return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
3900 }
3901
3902 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
3903         enum isl_dim_type type, unsigned pos, isl_int *val)
3904 {
3905         if (pos >= isl_basic_map_dim(bmap, type))
3906                 return -1;
3907         return isl_basic_map_fast_has_fixed_var(bmap,
3908                 basic_map_offset(bmap, type) - 1 + pos, val);
3909 }
3910
3911 /* Check if dimension dim has fixed value and if so and if val is not NULL,
3912  * then return this fixed value in *val.
3913  */
3914 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
3915 {
3916         return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
3917 }
3918
3919 /* Check if input variable in has fixed value and if so and if val is not NULL,
3920  * then return this fixed value in *val.
3921  */
3922 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
3923 {
3924         return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
3925 }
3926
3927 /* Check if dimension dim has an (obvious) fixed lower bound and if so
3928  * and if val is not NULL, then return this lower bound in *val.
3929  */
3930 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
3931         unsigned dim, isl_int *val)
3932 {
3933         int i, i_eq = -1, i_ineq = -1;
3934         isl_int *c;
3935         unsigned total;
3936         unsigned nparam;
3937
3938         if (!bset)
3939                 return -1;
3940         total = isl_basic_set_total_dim(bset);
3941         nparam = isl_basic_set_n_param(bset);
3942         for (i = 0; i < bset->n_eq; ++i) {
3943                 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
3944                         continue;
3945                 if (i_eq != -1)
3946                         return 0;
3947                 i_eq = i;
3948         }
3949         for (i = 0; i < bset->n_ineq; ++i) {
3950                 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
3951                         continue;
3952                 if (i_eq != -1 || i_ineq != -1)
3953                         return 0;
3954                 i_ineq = i;
3955         }
3956         if (i_eq == -1 && i_ineq == -1)
3957                 return 0;
3958         c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
3959         /* The coefficient should always be one due to normalization. */
3960         if (!isl_int_is_one(c[1+nparam+dim]))
3961                 return 0;
3962         if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
3963                 return 0;
3964         if (isl_seq_first_non_zero(c+1+nparam+dim+1,
3965                                         total - nparam - dim - 1) != -1)
3966                 return 0;
3967         if (val)
3968                 isl_int_neg(*val, c[0]);
3969         return 1;
3970 }
3971
3972 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
3973         unsigned dim, isl_int *val)
3974 {
3975         int i;
3976         isl_int v;
3977         isl_int tmp;
3978         int fixed;
3979
3980         if (!set)
3981                 return -1;
3982         if (set->n == 0)
3983                 return 0;
3984         if (set->n == 1)
3985                 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
3986                                                                 dim, val);
3987         isl_int_init(v);
3988         isl_int_init(tmp);
3989         fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
3990                                                                 dim, &v);
3991         for (i = 1; fixed == 1 && i < set->n; ++i) {
3992                 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
3993                                                                 dim, &tmp);
3994                 if (fixed == 1 && isl_int_ne(tmp, v))
3995                         fixed = 0;
3996         }
3997         if (val)
3998                 isl_int_set(*val, v);
3999         isl_int_clear(tmp);
4000         isl_int_clear(v);
4001         return fixed;
4002 }
4003
4004 struct constraint {
4005         unsigned        size;
4006         isl_int         *c;
4007 };
4008
4009 static int qsort_constraint_cmp(const void *p1, const void *p2)
4010 {
4011         const struct constraint *c1 = (const struct constraint *)p1;
4012         const struct constraint *c2 = (const struct constraint *)p2;
4013         unsigned size = isl_min(c1->size, c2->size);
4014         return isl_seq_cmp(c1->c, c2->c, size);
4015 }
4016
4017 static struct isl_basic_map *isl_basic_map_sort_constraints(
4018         struct isl_basic_map *bmap)
4019 {
4020         int i;
4021         struct constraint *c;
4022         unsigned total;
4023
4024         if (!bmap)
4025                 return NULL;
4026         total = isl_basic_map_total_dim(bmap);
4027         c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
4028         if (!c)
4029                 goto error;
4030         for (i = 0; i < bmap->n_ineq; ++i) {
4031                 c[i].size = total;
4032                 c[i].c = bmap->ineq[i];
4033         }
4034         qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
4035         for (i = 0; i < bmap->n_ineq; ++i)
4036                 bmap->ineq[i] = c[i].c;
4037         free(c);
4038         return bmap;
4039 error:
4040         isl_basic_map_free(bmap);
4041         return NULL;
4042 }
4043
4044 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
4045 {
4046         if (!bmap)
4047                 return NULL;
4048         if (F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
4049                 return bmap;
4050         bmap = isl_basic_map_convex_hull(bmap);
4051         bmap = isl_basic_map_sort_constraints(bmap);
4052         F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
4053         return bmap;
4054 }
4055
4056 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
4057 {
4058         return (struct isl_basic_set *)isl_basic_map_normalize(
4059                                                 (struct isl_basic_map *)bset);
4060 }
4061
4062 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
4063         const struct isl_basic_map *bmap2)
4064 {
4065         int i, cmp;
4066         unsigned total;
4067
4068         if (bmap1 == bmap2)
4069                 return 0;
4070         if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
4071                 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
4072         if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
4073                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4074         if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
4075                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
4076         if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
4077             F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4078                 return 0;
4079         if (F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
4080                 return 1;
4081         if (F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
4082                 return -1;
4083         if (bmap1->n_eq != bmap2->n_eq)
4084                 return bmap1->n_eq - bmap2->n_eq;
4085         if (bmap1->n_ineq != bmap2->n_ineq)
4086                 return bmap1->n_ineq - bmap2->n_ineq;
4087         if (bmap1->n_div != bmap2->n_div)
4088                 return bmap1->n_div - bmap2->n_div;
4089         total = isl_basic_map_total_dim(bmap1);
4090         for (i = 0; i < bmap1->n_eq; ++i) {
4091                 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
4092                 if (cmp)
4093                         return cmp;
4094         }
4095         for (i = 0; i < bmap1->n_ineq; ++i) {
4096                 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
4097                 if (cmp)
4098                         return cmp;
4099         }
4100         for (i = 0; i < bmap1->n_div; ++i) {
4101                 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
4102                 if (cmp)
4103                         return cmp;
4104         }
4105         return 0;
4106 }
4107
4108 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
4109         struct isl_basic_map *bmap2)
4110 {
4111         return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
4112 }
4113
4114 static int qsort_bmap_cmp(const void *p1, const void *p2)
4115 {
4116         const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
4117         const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
4118
4119         return isl_basic_map_fast_cmp(bmap1, bmap2);
4120 }
4121
4122 /* We normalize in place, but if anything goes wrong we need
4123  * to return NULL, so we need to make sure we don't change the
4124  * meaning of any possible other copies of map.
4125  */
4126 struct isl_map *isl_map_normalize(struct isl_map *map)
4127 {
4128         int i, j;
4129         struct isl_basic_map *bmap;
4130
4131         if (!map)
4132                 return NULL;
4133         if (F_ISSET(map, ISL_MAP_NORMALIZED))
4134                 return map;
4135         for (i = 0; i < map->n; ++i) {
4136                 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
4137                 if (!bmap)
4138                         goto error;
4139                 isl_basic_map_free(map->p[i]);
4140                 map->p[i] = bmap;
4141         }
4142         qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
4143         F_SET(map, ISL_MAP_NORMALIZED);
4144         map = isl_map_remove_empty_parts(map);
4145         if (!map)
4146                 return NULL;
4147         for (i = map->n - 1; i >= 1; --i) {
4148                 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
4149                         continue;
4150                 isl_basic_map_free(map->p[i-1]);
4151                 for (j = i; j < map->n; ++j)
4152                         map->p[j-1] = map->p[j];
4153                 map->n--;
4154         }
4155         return map;
4156 error:
4157         isl_map_free(map);
4158         return NULL;
4159
4160 }
4161
4162 struct isl_set *isl_set_normalize(struct isl_set *set)
4163 {
4164         return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
4165 }
4166
4167 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
4168 {
4169         int i;
4170         int equal;
4171
4172         if (!map1 || !map2)
4173                 return -1;
4174
4175         if (map1 == map2)
4176                 return 1;
4177         if (!isl_dim_equal(map1->dim, map2->dim))
4178                 return 0;
4179
4180         map1 = isl_map_copy(map1);
4181         map2 = isl_map_copy(map2);
4182         map1 = isl_map_normalize(map1);
4183         map2 = isl_map_normalize(map2);
4184         if (!map1 || !map2)
4185                 goto error;
4186         equal = map1->n == map2->n;
4187         for (i = 0; equal && i < map1->n; ++i) {
4188                 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
4189                 if (equal < 0)
4190                         goto error;
4191         }
4192         isl_map_free(map1);
4193         isl_map_free(map2);
4194         return equal;
4195 error:
4196         isl_map_free(map1);
4197         isl_map_free(map2);
4198         return -1;
4199 }
4200
4201 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
4202 {
4203         return isl_map_fast_is_equal((struct isl_map *)set1,
4204                                                 (struct isl_map *)set2);
4205 }
4206
4207 /* Return an interval that ranges from min to max (inclusive)
4208  */
4209 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
4210         isl_int min, isl_int max)
4211 {
4212         int k;
4213         struct isl_basic_set *bset = NULL;
4214
4215         bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
4216         if (!bset)
4217                 goto error;
4218
4219         k = isl_basic_set_alloc_inequality(bset);
4220         if (k < 0)
4221                 goto error;
4222         isl_int_set_si(bset->ineq[k][1], 1);
4223         isl_int_neg(bset->ineq[k][0], min);
4224
4225         k = isl_basic_set_alloc_inequality(bset);
4226         if (k < 0)
4227                 goto error;
4228         isl_int_set_si(bset->ineq[k][1], -1);
4229         isl_int_set(bset->ineq[k][0], max);
4230
4231         return bset;
4232 error:
4233         isl_basic_set_free(bset);
4234         return NULL;
4235 }
4236
4237 /* Return the Cartesian product of the basic sets in list (in the given order).
4238  */
4239 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
4240 {
4241         int i;
4242         unsigned dim;
4243         unsigned nparam;
4244         unsigned extra;
4245         unsigned n_eq;
4246         unsigned n_ineq;
4247         struct isl_basic_set *product = NULL;
4248
4249         if (!list)
4250                 goto error;
4251         isl_assert(list->ctx, list->n > 0, goto error);
4252         isl_assert(list->ctx, list->p[0], goto error);
4253         nparam = isl_basic_set_n_param(list->p[0]);
4254         dim = isl_basic_set_n_dim(list->p[0]);
4255         extra = list->p[0]->n_div;
4256         n_eq = list->p[0]->n_eq;
4257         n_ineq = list->p[0]->n_ineq;
4258         for (i = 1; i < list->n; ++i) {
4259                 isl_assert(list->ctx, list->p[i], goto error);
4260                 isl_assert(list->ctx,
4261                     nparam == isl_basic_set_n_param(list->p[i]), goto error);
4262                 dim += isl_basic_set_n_dim(list->p[i]);
4263                 extra += list->p[i]->n_div;
4264                 n_eq += list->p[i]->n_eq;
4265                 n_ineq += list->p[i]->n_ineq;
4266         }
4267         product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
4268                                         n_eq, n_ineq);
4269         if (!product)
4270                 goto error;
4271         dim = 0;
4272         for (i = 0; i < list->n; ++i) {
4273                 isl_basic_set_add_constraints(product,
4274                                         isl_basic_set_copy(list->p[i]), dim);
4275                 dim += isl_basic_set_n_dim(list->p[i]);
4276         }
4277         isl_basic_set_list_free(list);
4278         return product;
4279 error:
4280         isl_basic_set_free(product);
4281         isl_basic_set_list_free(list);
4282         return NULL;
4283 }
4284
4285 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
4286 {
4287         int i;
4288         uint32_t hash;
4289         unsigned total;
4290
4291         if (!bset)
4292                 return 0;
4293         bset = isl_basic_set_copy(bset);
4294         bset = isl_basic_set_normalize(bset);
4295         if (!bset)
4296                 return 0;
4297         total = isl_basic_set_total_dim(bset);
4298         isl_hash_byte(hash, bset->n_eq & 0xFF);
4299         for (i = 0; i < bset->n_eq; ++i) {
4300                 uint32_t c_hash;
4301                 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
4302                 isl_hash_hash(hash, c_hash);
4303         }
4304         isl_hash_byte(hash, bset->n_ineq & 0xFF);
4305         for (i = 0; i < bset->n_ineq; ++i) {
4306                 uint32_t c_hash;
4307                 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
4308                 isl_hash_hash(hash, c_hash);
4309         }
4310         isl_hash_byte(hash, bset->n_div & 0xFF);
4311         for (i = 0; i < bset->n_div; ++i) {
4312                 uint32_t c_hash;
4313                 if (isl_int_is_zero(bset->div[i][0]))
4314                         continue;
4315                 isl_hash_byte(hash, i & 0xFF);
4316                 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
4317                 isl_hash_hash(hash, c_hash);
4318         }
4319         isl_basic_set_free(bset);
4320         return hash;
4321 }
4322
4323 uint32_t isl_set_get_hash(struct isl_set *set)
4324 {
4325         int i;
4326         uint32_t hash;
4327
4328         if (!set)
4329                 return 0;
4330         set = isl_set_copy(set);
4331         set = isl_set_normalize(set);
4332         if (!set)
4333                 return 0;
4334
4335         hash = isl_hash_init();
4336         for (i = 0; i < set->n; ++i) {
4337                 uint32_t bset_hash;
4338                 bset_hash = isl_basic_set_get_hash(set->p[i]);
4339                 isl_hash_hash(hash, bset_hash);
4340         }
4341                 
4342         isl_set_free(set);
4343
4344         return hash;
4345 }
4346
4347 /* Check if the value for dimension dim is completely determined
4348  * by the values of the other parameters and variables.
4349  * That is, check if dimension dim is involved in an equality.
4350  */
4351 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
4352 {
4353         int i;
4354         unsigned nparam;
4355
4356         if (!bset)
4357                 return -1;
4358         nparam = isl_basic_set_n_param(bset);
4359         for (i = 0; i < bset->n_eq; ++i)
4360                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
4361                         return 1;
4362         return 0;
4363 }
4364
4365 /* Check if the value for dimension dim is completely determined
4366  * by the values of the other parameters and variables.
4367  * That is, check if dimension dim is involved in an equality
4368  * for each of the subsets.
4369  */
4370 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
4371 {
4372         int i;
4373
4374         if (!set)
4375                 return -1;
4376         for (i = 0; i < set->n; ++i) {
4377                 int unique;
4378                 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
4379                 if (unique != 1)
4380                         return unique;
4381         }
4382         return 1;
4383 }