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