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