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