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