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