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