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