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