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