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