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