add isl_set_{from,to}_underlying_set
[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_seq.h"
6 #include "isl_set.h"
7 #include "isl_map.h"
8 #include "isl_map_private.h"
9 #include "isl_map_piplib.h"
10 #include "isl_sample.h"
11 #include "isl_vec.h"
12
13 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
14                 struct isl_basic_map *bmap,
15                 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
16                 unsigned n_eq, unsigned n_ineq)
17 {
18         int i;
19         size_t row_size = 1 + nparam + n_in + n_out + extra;
20
21         bmap->block = isl_blk_alloc(ctx, (n_eq + n_ineq) * row_size);
22         if (isl_blk_is_error(bmap->block)) {
23                 free(bmap);
24                 return NULL;
25         }
26
27         bmap->eq = isl_alloc_array(ctx, isl_int *, n_eq + n_ineq);
28         if (!bmap->eq) {
29                 isl_blk_free(ctx, bmap->block);
30                 free(bmap);
31                 return NULL;
32         }
33
34         if (extra == 0) {
35                 bmap->block2 = isl_blk_empty();
36                 bmap->div = NULL;
37         } else {
38                 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
39                 if (isl_blk_is_error(bmap->block2)) {
40                         free(bmap->eq);
41                         isl_blk_free(ctx, bmap->block);
42                         free(bmap);
43                         return NULL;
44                 }
45
46                 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
47                 if (!bmap->div) {
48                         isl_blk_free(ctx, bmap->block2);
49                         free(bmap->eq);
50                         isl_blk_free(ctx, bmap->block);
51                         free(bmap);
52                         return NULL;
53                 }
54         }
55
56         for (i = 0; i < n_eq + n_ineq; ++i)
57                 bmap->eq[i] = bmap->block.data + i * row_size;
58
59         for (i = 0; i < extra; ++i)
60                 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
61
62         bmap->ctx = ctx;
63         isl_ctx_ref(ctx);
64         bmap->ref = 1;
65         bmap->flags = 0;
66         bmap->c_size = n_eq + n_ineq;
67         bmap->ineq = bmap->eq + n_eq;
68         bmap->nparam = nparam;
69         bmap->n_in = n_in;
70         bmap->n_out = n_out;
71         bmap->extra = extra;
72         bmap->n_eq = 0;
73         bmap->n_ineq = 0;
74         bmap->n_div = 0;
75         bmap->sample = NULL;
76
77         return bmap;
78 }
79
80 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
81                 unsigned nparam, unsigned dim, unsigned extra,
82                 unsigned n_eq, unsigned n_ineq)
83 {
84         struct isl_basic_map *bmap;
85         bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
86         return (struct isl_basic_set *)bmap;
87 }
88
89 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
90                 unsigned nparam, unsigned in, unsigned out, unsigned extra,
91                 unsigned n_eq, unsigned n_ineq)
92 {
93         struct isl_basic_map *bmap;
94
95         bmap = isl_alloc_type(ctx, struct isl_basic_map);
96         if (!bmap)
97                 return NULL;
98
99         return basic_map_init(ctx, bmap,
100                                nparam, in, out, extra, n_eq, n_ineq);
101 }
102
103 static void dup_constraints(
104                 struct isl_basic_map *dst, struct isl_basic_map *src)
105 {
106         int i;
107         unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
108
109         for (i = 0; i < src->n_eq; ++i) {
110                 int j = isl_basic_map_alloc_equality(dst);
111                 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
112         }
113
114         for (i = 0; i < src->n_ineq; ++i) {
115                 int j = isl_basic_map_alloc_inequality(dst);
116                 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
117         }
118
119         for (i = 0; i < src->n_div; ++i) {
120                 int j = isl_basic_map_alloc_div(dst);
121                 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
122         }
123         F_SET(dst, ISL_BASIC_SET_FINAL);
124 }
125
126 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
127 {
128         struct isl_basic_map *dup;
129
130         if (!bmap)
131                 return NULL;
132         dup = isl_basic_map_alloc(bmap->ctx, bmap->nparam,
133                         bmap->n_in, bmap->n_out,
134                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
135         if (!dup)
136                 return NULL;
137         dup->flags = bmap->flags;
138         dup_constraints(dup, bmap);
139         dup->sample = isl_vec_copy(bmap->ctx, bmap->sample);
140         return dup;
141 }
142
143 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
144 {
145         struct isl_basic_map *dup;
146
147         dup = isl_basic_map_dup((struct isl_basic_map *)bset);
148         return (struct isl_basic_set *)dup;
149 }
150
151 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
152 {
153         if (!bset)
154                 return NULL;
155
156         if (F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
157                 bset->ref++;
158                 return bset;
159         }
160         return isl_basic_set_dup(bset);
161 }
162
163 struct isl_set *isl_set_copy(struct isl_set *set)
164 {
165         if (!set)
166                 return NULL;
167
168         set->ref++;
169         return set;
170 }
171
172 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
173 {
174         if (!bmap)
175                 return NULL;
176
177         if (F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
178                 bmap->ref++;
179                 return bmap;
180         }
181         return isl_basic_map_dup(bmap);
182 }
183
184 struct isl_map *isl_map_copy(struct isl_map *map)
185 {
186         if (!map)
187                 return NULL;
188
189         map->ref++;
190         return map;
191 }
192
193 void isl_basic_map_free(struct isl_basic_map *bmap)
194 {
195         if (!bmap)
196                 return;
197
198         if (--bmap->ref > 0)
199                 return;
200
201         isl_ctx_deref(bmap->ctx);
202         free(bmap->div);
203         isl_blk_free(bmap->ctx, bmap->block2);
204         free(bmap->eq);
205         isl_blk_free(bmap->ctx, bmap->block);
206         isl_vec_free(bmap->ctx, bmap->sample);
207         free(bmap);
208 }
209
210 void isl_basic_set_free(struct isl_basic_set *bset)
211 {
212         isl_basic_map_free((struct isl_basic_map *)bset);
213 }
214
215 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
216 {
217         struct isl_ctx *ctx;
218         if (!bmap)
219                 return -1;
220         ctx = bmap->ctx;
221         isl_assert(ctx, bmap->n_eq + bmap->n_ineq < bmap->c_size, return -1);
222         isl_assert(ctx, bmap->eq + bmap->n_eq <= bmap->ineq, return -1);
223         if (bmap->eq + bmap->n_eq == bmap->ineq) {
224                 isl_int *t;
225                 int j = isl_basic_map_alloc_inequality(bmap);
226                 if (j < 0)
227                         return -1;
228                 t = bmap->ineq[0];
229                 bmap->ineq[0] = bmap->ineq[j];
230                 bmap->ineq[j] = t;
231                 bmap->n_ineq--;
232                 bmap->ineq++;
233         }
234         return bmap->n_eq++;
235 }
236
237 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
238 {
239         return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
240 }
241
242 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
243 {
244         if (!bmap)
245                 return -1;
246         isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
247         bmap->n_eq -= n;
248         return 0;
249 }
250
251 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
252 {
253         isl_int *t;
254         if (!bmap)
255                 return -1;
256         isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
257
258         if (pos != bmap->n_eq - 1) {
259                 t = bmap->eq[pos];
260                 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
261                 bmap->eq[bmap->n_eq - 1] = t;
262         }
263         bmap->n_eq--;
264         return 0;
265 }
266
267 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
268 {
269         return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
270 }
271
272 void isl_basic_map_inequality_to_equality(
273                 struct isl_basic_map *bmap, unsigned pos)
274 {
275         isl_int *t;
276
277         t = bmap->ineq[pos];
278         bmap->ineq[pos] = bmap->ineq[0];
279         bmap->ineq[0] = bmap->eq[bmap->n_eq];
280         bmap->eq[bmap->n_eq] = t;
281         bmap->n_eq++;
282         bmap->n_ineq--;
283         bmap->ineq++;
284 }
285
286 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
287 {
288         struct isl_ctx *ctx;
289         if (!bmap)
290                 return -1;
291         ctx = bmap->ctx;
292         isl_assert(ctx, (bmap->ineq - bmap->eq) + bmap->n_ineq < bmap->c_size,
293                         return -1);
294         F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
295         F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
296         return bmap->n_ineq++;
297 }
298
299 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
300 {
301         return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
302 }
303
304 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
305 {
306         if (!bmap)
307                 return -1;
308         isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
309         bmap->n_ineq -= n;
310         return 0;
311 }
312
313 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
314 {
315         return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
316 }
317
318 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
319 {
320         isl_int *t;
321         if (!bmap)
322                 return -1;
323         isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
324
325         if (pos != bmap->n_ineq - 1) {
326                 t = bmap->ineq[pos];
327                 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
328                 bmap->ineq[bmap->n_ineq - 1] = t;
329         }
330         bmap->n_ineq--;
331         return 0;
332 }
333
334 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
335 {
336         return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
337 }
338
339 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
340 {
341         if (!bmap)
342                 return -1;
343         isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
344         return bmap->n_div++;
345 }
346
347 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
348 {
349         if (!bmap)
350                 return -1;
351         isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
352         bmap->n_div -= n;
353         return 0;
354 }
355
356 /* Copy constraint from src to dst, putting the vars of src at offset
357  * dim_off in dst and the divs of src at offset div_off in dst.
358  * If both sets are actually map, then dim_off applies to the input
359  * variables.
360  */
361 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
362                             struct isl_basic_map *src_map, isl_int *src,
363                             unsigned in_off, unsigned out_off, unsigned div_off)
364 {
365         isl_int_set(dst[0], src[0]);
366         isl_seq_cpy(dst+1, src+1, isl_min(dst_map->nparam, src_map->nparam));
367         if (dst_map->nparam > src_map->nparam)
368                 isl_seq_clr(dst+1+src_map->nparam,
369                                 dst_map->nparam - src_map->nparam);
370         isl_seq_clr(dst+1+dst_map->nparam, in_off);
371         isl_seq_cpy(dst+1+dst_map->nparam+in_off,
372                     src+1+src_map->nparam,
373                     isl_min(dst_map->n_in-in_off, src_map->n_in));
374         if (dst_map->n_in-in_off > src_map->n_in)
375                 isl_seq_clr(dst+1+dst_map->nparam+in_off+src_map->n_in,
376                                 dst_map->n_in - in_off - src_map->n_in);
377         isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in, out_off);
378         isl_seq_cpy(dst+1+dst_map->nparam+dst_map->n_in+out_off,
379                     src+1+src_map->nparam+src_map->n_in,
380                     isl_min(dst_map->n_out-out_off, src_map->n_out));
381         if (dst_map->n_out-out_off > src_map->n_out)
382                 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in+out_off+src_map->n_out,
383                                 dst_map->n_out - out_off - src_map->n_out);
384         isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in+dst_map->n_out, div_off);
385         isl_seq_cpy(dst+1+dst_map->nparam+dst_map->n_in+dst_map->n_out+div_off,
386                     src+1+src_map->nparam+src_map->n_in+src_map->n_out,
387                     isl_min(dst_map->extra-div_off, src_map->extra));
388         if (dst_map->extra-div_off > src_map->extra)
389                 isl_seq_clr(dst+1+dst_map->nparam+dst_map->n_in+dst_map->n_out+
390                                 div_off+src_map->extra,
391                                 dst_map->extra - div_off - src_map->extra);
392 }
393
394 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
395                      struct isl_basic_map *src_map, isl_int *src,
396                      unsigned in_off, unsigned out_off, unsigned div_off)
397 {
398         isl_int_set(dst[0], src[0]);
399         copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
400 }
401
402 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
403                 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
404 {
405         int i;
406         unsigned div_off = bmap1->n_div;
407
408         for (i = 0; i < bmap2->n_eq; ++i) {
409                 int i1 = isl_basic_map_alloc_equality(bmap1);
410                 if (i1 < 0)
411                         goto error;
412                 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
413                                 i_pos, o_pos, div_off);
414         }
415
416         for (i = 0; i < bmap2->n_ineq; ++i) {
417                 int i1 = isl_basic_map_alloc_inequality(bmap1);
418                 if (i1 < 0)
419                         goto error;
420                 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
421                                 i_pos, o_pos, div_off);
422         }
423
424         for (i = 0; i < bmap2->n_div; ++i) {
425                 int i1 = isl_basic_map_alloc_div(bmap1);
426                 if (i1 < 0)
427                         goto error;
428                 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
429                          i_pos, o_pos, div_off);
430         }
431
432         isl_basic_map_free(bmap2);
433
434         return bmap1;
435
436 error:
437         isl_basic_map_free(bmap1);
438         isl_basic_map_free(bmap2);
439         return NULL;
440 }
441
442 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
443                 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
444                 unsigned n_eq, unsigned n_ineq)
445 {
446         struct isl_basic_map *ext;
447         unsigned flags;
448         int dims_ok;
449
450         base = isl_basic_map_cow(base);
451         if (!base)
452                 goto error;
453
454         dims_ok = base && base->nparam == nparam &&
455                   base->n_in == n_in && base->n_out == n_out &&
456                   base->extra >= base->n_div + extra;
457
458         if (dims_ok && n_eq == 0 && n_ineq == 0)
459                 return base;
460
461         isl_assert(base->ctx, base->nparam <= nparam, goto error);
462         isl_assert(base->ctx, base->n_in <= n_in, goto error);
463         isl_assert(base->ctx, base->n_out <= n_out, goto error);
464         extra += base->extra;
465         n_eq += base->n_eq;
466         n_ineq += base->n_ineq;
467
468         ext = isl_basic_map_alloc(base->ctx, nparam, n_in, n_out,
469                                         extra, n_eq, n_ineq);
470         if (!base)
471                 return ext;
472         if (!ext)
473                 goto error;
474
475         flags = base->flags;
476         ext = add_constraints(ext, base, 0, 0);
477         if (ext) {
478                 ext->flags = flags;
479                 F_CLR(ext, ISL_BASIC_SET_FINAL);
480         }
481
482         return ext;
483
484 error:
485         isl_basic_map_free(base);
486         return NULL;
487 }
488
489 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
490                 unsigned nparam, unsigned dim, unsigned extra,
491                 unsigned n_eq, unsigned n_ineq)
492 {
493         return (struct isl_basic_set *)
494                 isl_basic_map_extend((struct isl_basic_map *)base,
495                                         nparam, 0, dim, extra, n_eq, n_ineq);
496 }
497
498 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
499 {
500         return (struct isl_basic_set *)
501                 isl_basic_map_cow((struct isl_basic_map *)bset);
502 }
503
504 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
505 {
506         if (!bmap)
507                 return NULL;
508
509         if (bmap->ref > 1) {
510                 bmap->ref--;
511                 bmap = isl_basic_map_dup(bmap);
512         }
513         F_CLR(bmap, ISL_BASIC_SET_FINAL);
514         return bmap;
515 }
516
517 struct isl_set *isl_set_cow(struct isl_set *set)
518 {
519         if (!set)
520                 return NULL;
521
522         if (set->ref == 1)
523                 return set;
524         set->ref--;
525         return isl_set_dup(set);
526 }
527
528 struct isl_map *isl_map_cow(struct isl_map *map)
529 {
530         if (!map)
531                 return NULL;
532
533         if (map->ref == 1)
534                 return map;
535         map->ref--;
536         return isl_map_dup(map);
537 }
538
539 static void swap_vars(struct isl_blk blk, isl_int *a,
540                         unsigned a_len, unsigned b_len)
541 {
542         isl_seq_cpy(blk.data, a+a_len, b_len);
543         isl_seq_cpy(blk.data+b_len, a, a_len);
544         isl_seq_cpy(a, blk.data, b_len+a_len);
545 }
546
547 struct isl_basic_set *isl_basic_set_swap_vars(
548                 struct isl_basic_set *bset, unsigned n)
549 {
550         int i;
551         struct isl_blk blk;
552
553         if (!bset)
554                 goto error;
555
556         isl_assert(bset->ctx, n <= bset->dim, goto error);
557
558         if (n == bset->dim)
559                 return bset;
560
561         bset = isl_basic_set_cow(bset);
562         if (!bset)
563                 return NULL;
564
565         blk = isl_blk_alloc(bset->ctx, bset->dim);
566         if (isl_blk_is_error(blk))
567                 goto error;
568
569         for (i = 0; i < bset->n_eq; ++i)
570                 swap_vars(blk,
571                           bset->eq[i]+1+bset->nparam, n, bset->dim - n);
572
573         for (i = 0; i < bset->n_ineq; ++i)
574                 swap_vars(blk,
575                           bset->ineq[i]+1+bset->nparam, n, bset->dim - n);
576
577         for (i = 0; i < bset->n_div; ++i)
578                 swap_vars(blk,
579                           bset->div[i]+1+1+bset->nparam, n, bset->dim - n);
580
581         isl_blk_free(bset->ctx, blk);
582
583         return bset;
584
585 error:
586         isl_basic_set_free(bset);
587         return NULL;
588 }
589
590 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
591 {
592         int i;
593         set = isl_set_cow(set);
594         if (!set)
595                 return NULL;
596
597         for (i = 0; i < set->n; ++i) {
598                 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
599                 if (!set->p[i]) {
600                         isl_set_free(set);
601                         return NULL;
602                 }
603         }
604         return set;
605 }
606
607 static void constraint_drop_vars(isl_int *c, unsigned n, unsigned rem)
608 {
609         isl_seq_cpy(c, c + n, rem);
610         isl_seq_clr(c + rem, n);
611 }
612
613 struct isl_basic_set *isl_basic_set_drop_vars(
614                 struct isl_basic_set *bset, unsigned first, unsigned n)
615 {
616         int i;
617
618         if (!bset)
619                 goto error;
620
621         isl_assert(bset->ctx, first + n <= bset->dim, goto error);
622
623         if (n == 0)
624                 return bset;
625
626         bset = isl_basic_set_cow(bset);
627         if (!bset)
628                 return NULL;
629
630         for (i = 0; i < bset->n_eq; ++i)
631                 constraint_drop_vars(bset->eq[i]+1+bset->nparam+first, n,
632                                      (bset->dim-first-n)+bset->extra);
633
634         for (i = 0; i < bset->n_ineq; ++i)
635                 constraint_drop_vars(bset->ineq[i]+1+bset->nparam+first, n,
636                                      (bset->dim-first-n)+bset->extra);
637
638         for (i = 0; i < bset->n_div; ++i)
639                 constraint_drop_vars(bset->div[i]+1+1+bset->nparam+first, n,
640                                      (bset->dim-first-n)+bset->extra);
641
642         bset->dim -= n;
643         bset->extra += n;
644
645         return isl_basic_set_finalize(bset);
646 error:
647         isl_basic_set_free(bset);
648         return NULL;
649 }
650
651 struct isl_set *isl_set_drop_vars(
652                 struct isl_set *set, unsigned first, unsigned n)
653 {
654         int i;
655
656         if (!set)
657                 goto error;
658
659         isl_assert(set->ctx, first + n <= set->dim, goto error);
660
661         if (n == 0)
662                 return set;
663         set = isl_set_cow(set);
664         if (!set)
665                 goto error;
666
667         for (i = 0; i < set->n; ++i) {
668                 set->p[i] = isl_basic_set_drop_vars(set->p[i], first, n);
669                 if (!set->p[i])
670                         goto error;
671         }
672         set->dim -= n;
673
674         return set;
675 error:
676         isl_set_free(set);
677         return NULL;
678 }
679
680 /*
681  * We don't cow, as the div is assumed to be redundant.
682  */
683 static struct isl_basic_map *isl_basic_map_drop_div(
684                 struct isl_basic_map *bmap, unsigned div)
685 {
686         int i;
687         unsigned pos;
688
689         if (!bmap)
690                 goto error;
691
692         pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
693
694         isl_assert(bmap->ctx, div < bmap->n_div, goto error);
695
696         for (i = 0; i < bmap->n_eq; ++i)
697                 constraint_drop_vars(bmap->eq[i]+pos, 1, bmap->extra-div-1);
698
699         for (i = 0; i < bmap->n_ineq; ++i) {
700                 if (!isl_int_is_zero(bmap->ineq[i][pos])) {
701                         isl_basic_map_drop_inequality(bmap, i);
702                         --i;
703                         continue;
704                 }
705                 constraint_drop_vars(bmap->ineq[i]+pos, 1, bmap->extra-div-1);
706         }
707
708         for (i = 0; i < bmap->n_div; ++i)
709                 constraint_drop_vars(bmap->div[i]+1+pos, 1, bmap->extra-div-1);
710
711         if (div != bmap->n_div - 1) {
712                 int j;
713                 isl_int *t = bmap->div[div];
714
715                 for (j = div; j < bmap->n_div - 1; ++j)
716                         bmap->div[j] = bmap->div[j+1];
717
718                 bmap->div[bmap->n_div - 1] = t;
719         }
720         isl_basic_map_free_div(bmap, 1);
721
722         return bmap;
723 error:
724         isl_basic_map_free(bmap);
725         return NULL;
726 }
727
728 static void eliminate_div(struct isl_basic_map *bmap, isl_int *eq, unsigned div)
729 {
730         int i;
731         unsigned pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
732         unsigned len;
733         len = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
734
735         for (i = 0; i < bmap->n_eq; ++i)
736                 if (bmap->eq[i] != eq)
737                         isl_seq_elim(bmap->eq[i], eq, pos, len, NULL);
738
739         for (i = 0; i < bmap->n_ineq; ++i)
740                 isl_seq_elim(bmap->ineq[i], eq, pos, len, NULL);
741
742         /* We need to be careful about circular definitions,
743          * so for now we just remove the definitions of other divs that
744          * depend on this div and (possibly) recompute them later.
745          */
746         for (i = 0; i < bmap->n_div; ++i)
747                 if (!isl_int_is_zero(bmap->div[i][0]) &&
748                     !isl_int_is_zero(bmap->div[i][1 + pos]))
749                         isl_seq_clr(bmap->div[i], 1 + len);
750
751         isl_basic_map_drop_div(bmap, div);
752 }
753
754 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
755 {
756         int i = 0;
757         unsigned total;
758         if (!bmap)
759                 goto error;
760         total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra;
761         isl_basic_map_free_div(bmap, bmap->n_div);
762         isl_basic_map_free_inequality(bmap, bmap->n_ineq);
763         if (bmap->n_eq > 0)
764                 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
765         else {
766                 isl_basic_map_alloc_equality(bmap);
767                 if (i < 0)
768                         goto error;
769         }
770         isl_int_set_si(bmap->eq[i][0], 1);
771         isl_seq_clr(bmap->eq[i]+1, total);
772         F_SET(bmap, ISL_BASIC_MAP_EMPTY);
773         return isl_basic_map_finalize(bmap);
774 error:
775         isl_basic_map_free(bmap);
776         return NULL;
777 }
778
779 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
780 {
781         return (struct isl_basic_set *)
782                 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
783 }
784
785 static void swap_equality(struct isl_basic_map *bmap, int a, int b)
786 {
787         isl_int *t = bmap->eq[a];
788         bmap->eq[a] = bmap->eq[b];
789         bmap->eq[b] = t;
790 }
791
792 static void swap_inequality(struct isl_basic_map *bmap, int a, int b)
793 {
794         isl_int *t = bmap->ineq[a];
795         bmap->ineq[a] = bmap->ineq[b];
796         bmap->ineq[b] = t;
797 }
798
799 static void swap_div(struct isl_basic_map *bmap, int a, int b)
800 {
801         int i;
802         unsigned off = bmap->nparam + bmap->n_in + bmap->n_out;
803         isl_int *t = bmap->div[a];
804         bmap->div[a] = bmap->div[b];
805         bmap->div[b] = t;
806
807         for (i = 0; i < bmap->n_eq; ++i)
808                 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
809
810         for (i = 0; i < bmap->n_ineq; ++i)
811                 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
812
813         for (i = 0; i < bmap->n_div; ++i)
814                 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
815 }
816
817 struct isl_basic_map *isl_basic_map_gauss(
818         struct isl_basic_map *bmap, int *progress)
819 {
820         int k;
821         int done;
822         int last_var;
823         unsigned total_var;
824         unsigned total;
825
826         if (!bmap)
827                 return NULL;
828
829         total_var = bmap->nparam + bmap->n_in + bmap->n_out;
830         total = total_var + bmap->n_div;
831
832         last_var = total - 1;
833         for (done = 0; done < bmap->n_eq; ++done) {
834                 for (; last_var >= 0; --last_var) {
835                         for (k = done; k < bmap->n_eq; ++k)
836                                 if (!isl_int_is_zero(bmap->eq[k][1+last_var]))
837                                         break;
838                         if (k < bmap->n_eq)
839                                 break;
840                 }
841                 if (last_var < 0)
842                         break;
843                 if (k != done)
844                         swap_equality(bmap, k, done);
845                 if (isl_int_is_neg(bmap->eq[done][1+last_var]))
846                         isl_seq_neg(bmap->eq[done], bmap->eq[done], 1+total);
847
848                 for (k = 0; k < bmap->n_eq; ++k) {
849                         if (k == done)
850                                 continue;
851                         if (isl_int_is_zero(bmap->eq[k][1+last_var]))
852                                 continue;
853                         if (progress)
854                                 *progress = 1;
855                         isl_seq_elim(bmap->eq[k], bmap->eq[done],
856                                         1+last_var, 1+total, NULL);
857                 }
858
859                 for (k = 0; k < bmap->n_ineq; ++k) {
860                         if (isl_int_is_zero(bmap->ineq[k][1+last_var]))
861                                 continue;
862                         if (progress)
863                                 *progress = 1;
864                         isl_seq_elim(bmap->ineq[k], bmap->eq[done],
865                                         1+last_var, 1+total, NULL);
866                 }
867
868                 for (k = 0; k < bmap->n_div; ++k) {
869                         if (isl_int_is_zero(bmap->div[k][0]))
870                                 continue;
871                         if (isl_int_is_zero(bmap->div[k][1+1+last_var]))
872                                 continue;
873                         if (progress)
874                                 *progress = 1;
875                         isl_seq_elim(bmap->div[k]+1, bmap->eq[done],
876                                         1+last_var, 1+total, &bmap->div[k][0]);
877                 }
878
879                 if (last_var >= total_var &&
880                     isl_int_is_zero(bmap->div[last_var - total_var][0])) {
881                         unsigned div = last_var - total_var;
882                         isl_seq_neg(bmap->div[div]+1, bmap->eq[done], 1+total);
883                         isl_int_set_si(bmap->div[div][1+1+last_var], 0);
884                         isl_int_set(bmap->div[div][0],
885                                     bmap->eq[done][1+last_var]);
886                 }
887         }
888         if (done == bmap->n_eq)
889                 return bmap;
890         for (k = done; k < bmap->n_eq; ++k) {
891                 if (isl_int_is_zero(bmap->eq[k][0]))
892                         continue;
893                 return isl_basic_map_set_to_empty(bmap);
894         }
895         isl_basic_map_free_equality(bmap, bmap->n_eq-done);
896         return bmap;
897 }
898
899 struct isl_basic_set *isl_basic_set_gauss(
900         struct isl_basic_set *bset, int *progress)
901 {
902         return (struct isl_basic_set*)isl_basic_map_gauss(
903                         (struct isl_basic_map *)bset, progress);
904 }
905
906 static unsigned int round_up(unsigned int v)
907 {
908         int old_v = v;
909
910         while (v) {
911                 old_v = v;
912                 v ^= v & -v;
913         }
914         return old_v << 1;
915 }
916
917 static int hash_index(int *index, unsigned int size, int bits,
918                         struct isl_basic_map *bmap, int k)
919 {
920         int h;
921         unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
922         u_int32_t hash = isl_seq_hash(bmap->ineq[k]+1, total, bits);
923         for (h = hash; index[h]; h = (h+1) % size)
924                 if (k != index[h]-1 &&
925                     isl_seq_eq(bmap->ineq[k]+1,
926                                bmap->ineq[index[h]-1]+1, total))
927                         break;
928         return h;
929 }
930
931 static struct isl_basic_map *remove_duplicate_constraints(
932         struct isl_basic_map *bmap, int *progress)
933 {
934         unsigned int size;
935         int *index;
936         int k, l, h;
937         int bits;
938         unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
939         isl_int sum;
940
941         if (bmap->n_ineq <= 1)
942                 return bmap;
943
944         size = round_up(4 * (bmap->n_ineq+1) / 3 - 1);
945         bits = ffs(size) - 1;
946         index = isl_alloc_array(ctx, int, size);
947         memset(index, 0, size * sizeof(int));
948         if (!index)
949                 return bmap;
950
951         index[isl_seq_hash(bmap->ineq[0]+1, total, bits)] = 1;
952         for (k = 1; k < bmap->n_ineq; ++k) {
953                 h = hash_index(index, size, bits, bmap, k);
954                 if (!index[h]) {
955                         index[h] = k+1;
956                         continue;
957                 }
958                 *progress = 1;
959                 l = index[h] - 1;
960                 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
961                         swap_inequality(bmap, k, l);
962                 isl_basic_map_drop_inequality(bmap, k);
963                 --k;
964         }
965         isl_int_init(sum);
966         for (k = 0; k < bmap->n_ineq-1; ++k) {
967                 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
968                 h = hash_index(index, size, bits, bmap, k);
969                 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
970                 if (!index[h])
971                         continue;
972                 l = index[h] - 1;
973                 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
974                 if (isl_int_is_pos(sum))
975                         continue;
976                 if (isl_int_is_zero(sum)) {
977                         /* We need to break out of the loop after these
978                          * changes since the contents of the hash
979                          * will no longer be valid.
980                          * Plus, we probably we want to regauss first.
981                          */
982                         isl_basic_map_drop_inequality(bmap, l);
983                         isl_basic_map_inequality_to_equality(bmap, k);
984                 } else
985                         bmap = isl_basic_map_set_to_empty(bmap);
986                 break;
987         }
988         isl_int_clear(sum);
989
990         free(index);
991         return bmap;
992 }
993
994 static struct isl_basic_map *remove_duplicate_divs(
995         struct isl_basic_map *bmap, int *progress)
996 {
997         unsigned int size;
998         int *index;
999         int k, l, h;
1000         int bits;
1001         struct isl_blk eq;
1002         unsigned total_var = bmap->nparam + bmap->n_in + bmap->n_out;
1003         unsigned total = total_var + bmap->n_div;
1004         struct isl_ctx *ctx;
1005
1006         if (bmap->n_div <= 1)
1007                 return bmap;
1008
1009         ctx = bmap->ctx;
1010         for (k = bmap->n_div - 1; k >= 0; --k)
1011                 if (!isl_int_is_zero(bmap->div[k][0]))
1012                         break;
1013         if (k <= 0)
1014                 return bmap;
1015
1016         size = round_up(4 * bmap->n_div / 3 - 1);
1017         bits = ffs(size) - 1;
1018         index = isl_alloc_array(ctx, int, size);
1019         memset(index, 0, size * sizeof(int));
1020         if (!index)
1021                 return bmap;
1022         eq = isl_blk_alloc(ctx, 1+total);
1023         if (isl_blk_is_error(eq))
1024                 goto out;
1025
1026         isl_seq_clr(eq.data, 1+total);
1027         index[isl_seq_hash(bmap->div[k], 2+total, bits)] = k + 1;
1028         for (--k; k >= 0; --k) {
1029                 u_int32_t hash;
1030
1031                 if (isl_int_is_zero(bmap->div[k][0]))
1032                         continue;
1033
1034                 hash = isl_seq_hash(bmap->div[k], 2+total, bits);
1035                 for (h = hash; index[h]; h = (h+1) % size)
1036                         if (isl_seq_eq(bmap->div[k],
1037                                        bmap->div[index[h]-1], 2+total))
1038                                 break;
1039                 if (index[h]) {
1040                         *progress = 1;
1041                         l = index[h] - 1;
1042                         isl_int_set_si(eq.data[1+total_var+k], -1);
1043                         isl_int_set_si(eq.data[1+total_var+l], 1);
1044                         eliminate_div(bmap, eq.data, l);
1045                         isl_int_set_si(eq.data[1+total_var+k], 0);
1046                         isl_int_set_si(eq.data[1+total_var+l], 0);
1047                 }
1048                 index[h] = k+1;
1049         }
1050
1051         isl_blk_free(ctx, eq);
1052 out:
1053         free(index);
1054         return bmap;
1055 }
1056
1057 /* Elimininate divs based on equalities
1058  */
1059 static struct isl_basic_map *eliminate_divs_eq(
1060                 struct isl_basic_map *bmap, int *progress)
1061 {
1062         int d;
1063         int i;
1064         int modified = 0;
1065         unsigned off;
1066
1067         if (!bmap)
1068                 return NULL;
1069
1070         off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1071
1072         for (d = bmap->n_div - 1; d >= 0 ; --d) {
1073                 for (i = 0; i < bmap->n_eq; ++i) {
1074                         if (!isl_int_is_one(bmap->eq[i][off + d]) &&
1075                             !isl_int_is_negone(bmap->eq[i][off + d]))
1076                                 continue;
1077                         modified = 1;
1078                         *progress = 1;
1079                         eliminate_div(bmap, bmap->eq[i], d);
1080                         isl_basic_map_drop_equality(bmap, i);
1081                         break;
1082                 }
1083         }
1084         if (modified)
1085                 return eliminate_divs_eq(bmap, progress);
1086         return bmap;
1087 }
1088
1089 static struct isl_basic_map *normalize_constraints(struct isl_basic_map *bmap)
1090 {
1091         int i;
1092         isl_int gcd;
1093         unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1094
1095         isl_int_init(gcd);
1096         for (i = bmap->n_eq - 1; i >= 0; --i) {
1097                 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
1098                 if (isl_int_is_zero(gcd)) {
1099                         if (!isl_int_is_zero(bmap->eq[i][0])) {
1100                                 bmap = isl_basic_map_set_to_empty(bmap);
1101                                 break;
1102                         }
1103                         isl_basic_map_drop_equality(bmap, i);
1104                         continue;
1105                 }
1106                 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1107                         isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
1108                 if (isl_int_is_one(gcd))
1109                         continue;
1110                 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
1111                         bmap = isl_basic_map_set_to_empty(bmap);
1112                         break;
1113                 }
1114                 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
1115         }
1116
1117         for (i = bmap->n_ineq - 1; i >= 0; --i) {
1118                 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
1119                 if (isl_int_is_zero(gcd)) {
1120                         if (isl_int_is_neg(bmap->ineq[i][0])) {
1121                                 bmap = isl_basic_map_set_to_empty(bmap);
1122                                 break;
1123                         }
1124                         isl_basic_map_drop_inequality(bmap, i);
1125                         continue;
1126                 }
1127                 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1128                         isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
1129                 if (isl_int_is_one(gcd))
1130                         continue;
1131                 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
1132                 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
1133         }
1134         isl_int_clear(gcd);
1135
1136         return bmap;
1137 }
1138
1139
1140 /* Eliminate the specified variables from the constraints using
1141  * Fourier-Motzkin.  The variables themselves are not removed.
1142  */
1143 struct isl_basic_map *isl_basic_map_eliminate_vars(
1144         struct isl_basic_map *bmap, int pos, unsigned n)
1145 {
1146         int d;
1147         int i, j, k;
1148         unsigned total;
1149         unsigned extra;
1150
1151         if (!bmap)
1152                 return NULL;
1153         total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1154         extra = bmap->extra - bmap->n_div;
1155
1156         bmap = isl_basic_map_cow(bmap);
1157         bmap = isl_basic_map_gauss(bmap, NULL);
1158         for (d = pos + n - 1; d >= pos; --d) {
1159                 int n_lower, n_upper;
1160                 if (!bmap)
1161                         return NULL;
1162                 for (i = 0; i < bmap->n_eq; ++i) {
1163                         if (isl_int_is_zero(bmap->eq[i][1+d]))
1164                                 continue;
1165                         isl_basic_map_drop_equality(bmap, i);
1166                         break;
1167                 }
1168                 if (i < bmap->n_eq)
1169                         continue;
1170                 n_lower = 0;
1171                 n_upper = 0;
1172                 for (i = 0; i < bmap->n_ineq; ++i) {
1173                         if (isl_int_is_pos(bmap->ineq[i][1+d]))
1174                                 n_lower++;
1175                         else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1176                                 n_upper++;
1177                 }
1178                 bmap = isl_basic_map_extend(bmap,
1179                                 bmap->nparam, bmap->n_in, bmap->n_out, 0,
1180                                 0, n_lower * n_upper);
1181                 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1182                         int last;
1183                         if (isl_int_is_zero(bmap->ineq[i][1+d]))
1184                                 continue;
1185                         last = -1;
1186                         for (j = 0; j < i; ++j) {
1187                                 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1188                                         continue;
1189                                 last = j;
1190                                 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1191                                     isl_int_sgn(bmap->ineq[j][1+d]))
1192                                         continue;
1193                                 k = isl_basic_map_alloc_inequality(bmap);
1194                                 if (k < 0)
1195                                         goto error;
1196                                 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1197                                                 1+total);
1198                                 isl_seq_clr(bmap->ineq[k]+1+total, extra);
1199                                 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1200                                                 1+d, 1+total, NULL);
1201                         }
1202                         isl_basic_map_drop_inequality(bmap, i);
1203                         i = last + 1;
1204                 }
1205                 if (n_lower > 0 && n_upper > 0) {
1206                         bmap = normalize_constraints(bmap);
1207                         bmap = isl_basic_map_gauss(bmap, NULL);
1208                         if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1209                                 break;
1210                 }
1211         }
1212         return bmap;
1213 error:
1214         isl_basic_map_free(bmap);
1215         return NULL;
1216 }
1217
1218 struct isl_basic_set *isl_basic_set_eliminate_vars(
1219         struct isl_basic_set *bset, unsigned pos, unsigned n)
1220 {
1221         return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1222                         (struct isl_basic_map *)bset, pos, n);
1223 }
1224
1225 /* Elimininate divs based on inequalities
1226  */
1227 static struct isl_basic_map *eliminate_divs_ineq(
1228                 struct isl_basic_map *bmap, int *progress)
1229 {
1230         int d;
1231         int i;
1232         unsigned off;
1233         struct isl_ctx *ctx;
1234
1235         if (!bmap)
1236                 return NULL;
1237
1238         ctx = bmap->ctx;
1239         off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1240
1241         for (d = bmap->n_div - 1; d >= 0 ; --d) {
1242                 for (i = 0; i < bmap->n_eq; ++i)
1243                         if (!isl_int_is_zero(bmap->eq[i][off + d]))
1244                                 break;
1245                 if (i < bmap->n_eq)
1246                         continue;
1247                 for (i = 0; i < bmap->n_ineq; ++i)
1248                         if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
1249                                 break;
1250                 if (i < bmap->n_ineq)
1251                         continue;
1252                 *progress = 1;
1253                 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
1254                 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1255                         break;
1256                 bmap = isl_basic_map_drop_div(bmap, d);
1257                 if (!bmap)
1258                         break;
1259         }
1260         return bmap;
1261 }
1262
1263 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1264 {
1265         int progress = 1;
1266         if (!bmap)
1267                 return NULL;
1268         while (progress) {
1269                 progress = 0;
1270                 bmap = normalize_constraints(bmap);
1271                 bmap = eliminate_divs_eq(bmap, &progress);
1272                 bmap = eliminate_divs_ineq(bmap, &progress);
1273                 bmap = isl_basic_map_gauss(bmap, &progress);
1274                 bmap = remove_duplicate_divs(bmap, &progress);
1275                 bmap = remove_duplicate_constraints(bmap, &progress);
1276         }
1277         return bmap;
1278 }
1279
1280 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1281 {
1282         return (struct isl_basic_set *)
1283                 isl_basic_map_simplify((struct isl_basic_map *)bset);
1284 }
1285
1286 static void dump_term(struct isl_basic_map *bmap,
1287                         isl_int c, int pos, FILE *out)
1288 {
1289         unsigned in = bmap->n_in;
1290         unsigned dim = bmap->n_in + bmap->n_out;
1291         if (!pos)
1292                 isl_int_print(out, c);
1293         else {
1294                 if (!isl_int_is_one(c))
1295                         isl_int_print(out, c);
1296                 if (pos < 1 + bmap->nparam)
1297                         fprintf(out, "p%d", pos - 1);
1298                 else if (pos < 1 + bmap->nparam + in)
1299                         fprintf(out, "i%d", pos - 1 - bmap->nparam);
1300                 else if (pos < 1 + bmap->nparam + dim)
1301                         fprintf(out, "o%d", pos - 1 - bmap->nparam - in);
1302                 else
1303                         fprintf(out, "e%d", pos - 1 - bmap->nparam - dim);
1304         }
1305 }
1306
1307 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1308                                 int sign, FILE *out)
1309 {
1310         int i;
1311         int first;
1312         unsigned len = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1313         isl_int v;
1314
1315         isl_int_init(v);
1316         for (i = 0, first = 1; i < len; ++i) {
1317                 if (isl_int_sgn(c[i]) * sign <= 0)
1318                         continue;
1319                 if (!first)
1320                         fprintf(out, " + ");
1321                 first = 0;
1322                 isl_int_abs(v, c[i]);
1323                 dump_term(bmap, v, i, out);
1324         }
1325         isl_int_clear(v);
1326         if (first)
1327                 fprintf(out, "0");
1328 }
1329
1330 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1331                                 const char *op, FILE *out, int indent)
1332 {
1333         int i;
1334
1335         fprintf(out, "%*s", indent, "");
1336
1337         dump_constraint_sign(bmap, c, 1, out);
1338         fprintf(out, " %s ", op);
1339         dump_constraint_sign(bmap, c, -1, out);
1340
1341         fprintf(out, "\n");
1342
1343         for (i = bmap->n_div; i < bmap->extra; ++i) {
1344                 if (isl_int_is_zero(c[1+bmap->nparam+bmap->n_in+bmap->n_out+i]))
1345                         continue;
1346                 fprintf(out, "%*s", indent, "");
1347                 fprintf(out, "ERROR: unused div coefficient not zero\n");
1348         }
1349 }
1350
1351 static void dump_constraints(struct isl_basic_map *bmap,
1352                                 isl_int **c, unsigned n,
1353                                 const char *op, FILE *out, int indent)
1354 {
1355         int i;
1356
1357         for (i = 0; i < n; ++i)
1358                 dump_constraint(bmap, c[i], op, out, indent);
1359 }
1360
1361 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1362 {
1363         int j;
1364         int first = 1;
1365         unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1366
1367         for (j = 0; j < 1 + total; ++j) {
1368                 if (isl_int_is_zero(exp[j]))
1369                         continue;
1370                 if (!first && isl_int_is_pos(exp[j]))
1371                         fprintf(out, "+");
1372                 dump_term(bmap, exp[j], j, out);
1373                 first = 0;
1374         }
1375 }
1376
1377 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1378 {
1379         int i;
1380
1381         dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1382         dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1383
1384         for (i = 0; i < bmap->n_div; ++i) {
1385                 fprintf(out, "%*s", indent, "");
1386                 fprintf(out, "e%d = [(", i);
1387                 dump_affine(bmap, bmap->div[i]+1, out);
1388                 fprintf(out, ")/");
1389                 isl_int_print(out, bmap->div[i][0]);
1390                 fprintf(out, "]\n");
1391         }
1392 }
1393
1394 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1395 {
1396         if (!bset) {
1397                 fprintf(out, "null basic set\n");
1398                 return;
1399         }
1400
1401         fprintf(out, "%*s", indent, "");
1402         fprintf(out, "nparam: %d, dim: %d, extra: %d\n",
1403                         bset->nparam, bset->dim, bset->extra);
1404         dump((struct isl_basic_map *)bset, out, indent);
1405 }
1406
1407 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1408 {
1409         if (!bmap) {
1410                 fprintf(out, "null basic map\n");
1411                 return;
1412         }
1413
1414         fprintf(out, "%*s", indent, "");
1415         fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, flags: %x\n",
1416                 bmap->ref,
1417                 bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, bmap->flags);
1418         dump(bmap, out, indent);
1419 }
1420
1421 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1422 {
1423         unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1424         if (!bmap)
1425                 return -1;
1426         isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1427         isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1428         isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1429         return 0;
1430 }
1431
1432 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1433                 unsigned nparam, unsigned dim, int n, unsigned flags)
1434 {
1435         struct isl_set *set;
1436
1437         isl_assert(ctx, n >= 0, return NULL);
1438         set = isl_alloc(ctx, struct isl_set,
1439                         sizeof(struct isl_set) +
1440                         n * sizeof(struct isl_basic_set *));
1441         if (!set)
1442                 return NULL;
1443
1444         set->ctx = ctx;
1445         isl_ctx_ref(ctx);
1446         set->ref = 1;
1447         set->size = n;
1448         set->n = 0;
1449         set->nparam = nparam;
1450         set->zero = 0;
1451         set->dim = dim;
1452         set->flags = flags;
1453         return set;
1454 }
1455
1456 struct isl_set *isl_set_dup(struct isl_set *set)
1457 {
1458         int i;
1459         struct isl_set *dup;
1460
1461         if (!set)
1462                 return NULL;
1463
1464         dup = isl_set_alloc(set->ctx, set->nparam, set->dim, set->n, set->flags);
1465         if (!dup)
1466                 return NULL;
1467         for (i = 0; i < set->n; ++i)
1468                 dup = isl_set_add(dup,
1469                                   isl_basic_set_dup(set->p[i]));
1470         return dup;
1471 }
1472
1473 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1474 {
1475         struct isl_set *set;
1476
1477         if (!bset)
1478                 return NULL;
1479
1480         set = isl_set_alloc(bset->ctx, bset->nparam, bset->dim, 1, ISL_MAP_DISJOINT);
1481         if (!set) {
1482                 isl_basic_set_free(bset);
1483                 return NULL;
1484         }
1485         return isl_set_add(set, bset);
1486 }
1487
1488 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1489 {
1490         struct isl_map *map;
1491
1492         if (!bmap)
1493                 return NULL;
1494
1495         map = isl_map_alloc(bmap->ctx, bmap->nparam, bmap->n_in, bmap->n_out, 1,
1496                                 ISL_MAP_DISJOINT);
1497         if (!map) {
1498                 isl_basic_map_free(bmap);
1499                 return NULL;
1500         }
1501         return isl_map_add(map, bmap);
1502 }
1503
1504 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1505 {
1506         if (!bset || !set)
1507                 goto error;
1508         isl_assert(set->ctx, set->nparam == bset->nparam, goto error);
1509         isl_assert(set->ctx, set->dim == bset->dim, goto error);
1510         isl_assert(set->ctx, set->n < set->size, goto error);
1511         set->p[set->n] = bset;
1512         set->n++;
1513         return set;
1514 error:
1515         if (set)
1516                 isl_set_free(set);
1517         if (bset)
1518                 isl_basic_set_free(bset);
1519         return NULL;
1520 }
1521
1522 void isl_set_free(struct isl_set *set)
1523 {
1524         int i;
1525
1526         if (!set)
1527                 return;
1528
1529         if (--set->ref > 0)
1530                 return;
1531
1532         isl_ctx_deref(set->ctx);
1533         for (i = 0; i < set->n; ++i)
1534                 isl_basic_set_free(set->p[i]);
1535         free(set);
1536 }
1537
1538 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1539 {
1540         int i;
1541
1542         if (!set) {
1543                 fprintf(out, "null set\n");
1544                 return;
1545         }
1546
1547         fprintf(out, "%*s", indent, "");
1548         fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1549                         set->ref, set->n, set->nparam, set->dim, set->flags);
1550         for (i = 0; i < set->n; ++i) {
1551                 fprintf(out, "%*s", indent, "");
1552                 fprintf(out, "basic set %d:\n", i);
1553                 isl_basic_set_dump(set->p[i], out, indent+4);
1554         }
1555 }
1556
1557 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1558 {
1559         int i;
1560
1561         if (!map) {
1562                 fprintf(out, "null map\n");
1563                 return;
1564         }
1565
1566         fprintf(out, "%*s", indent, "");
1567         fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, flags: %x\n",
1568                         map->ref, map->n, map->nparam, map->n_in, map->n_out,
1569                         map->flags);
1570         for (i = 0; i < map->n; ++i) {
1571                 fprintf(out, "%*s", indent, "");
1572                 fprintf(out, "basic map %d:\n", i);
1573                 isl_basic_map_dump(map->p[i], out, indent+4);
1574         }
1575 }
1576
1577 struct isl_basic_map *isl_basic_map_intersect_domain(
1578                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1579 {
1580         struct isl_basic_map *bmap_domain;
1581
1582         if (!bmap || !bset)
1583                 goto error;
1584
1585         isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
1586         isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
1587
1588         bmap = isl_basic_map_extend(bmap,
1589                         bset->nparam, bmap->n_in, bmap->n_out,
1590                         bset->n_div, bset->n_eq, bset->n_ineq);
1591         if (!bmap)
1592                 goto error;
1593         bmap_domain = isl_basic_map_from_basic_set(bset,
1594                                                 bset->dim, 0);
1595         bmap = add_constraints(bmap, bmap_domain, 0, 0);
1596
1597         bmap = isl_basic_map_simplify(bmap);
1598         return isl_basic_map_finalize(bmap);
1599 error:
1600         isl_basic_map_free(bmap);
1601         isl_basic_set_free(bset);
1602         return NULL;
1603 }
1604
1605 struct isl_basic_map *isl_basic_map_intersect_range(
1606                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1607 {
1608         struct isl_basic_map *bmap_range;
1609
1610         if (!bmap || !bset)
1611                 goto error;
1612
1613         isl_assert(bset->ctx, bset->dim == bmap->n_out, goto error);
1614         isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
1615
1616         bmap = isl_basic_map_extend(bmap,
1617                         bset->nparam, bmap->n_in, bmap->n_out,
1618                         bset->n_div, bset->n_eq, bset->n_ineq);
1619         if (!bmap)
1620                 goto error;
1621         bmap_range = isl_basic_map_from_basic_set(bset,
1622                                                 0, bset->dim);
1623         bmap = add_constraints(bmap, bmap_range, 0, 0);
1624
1625         bmap = isl_basic_map_simplify(bmap);
1626         return isl_basic_map_finalize(bmap);
1627 error:
1628         isl_basic_map_free(bmap);
1629         isl_basic_set_free(bset);
1630         return NULL;
1631 }
1632
1633 struct isl_basic_map *isl_basic_map_intersect(
1634                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1635 {
1636         if (!bmap1 || !bmap2)
1637                 goto error;
1638
1639         isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
1640         isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
1641         isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
1642
1643         bmap1 = isl_basic_map_extend(bmap1,
1644                         bmap1->nparam, bmap1->n_in, bmap1->n_out,
1645                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1646         if (!bmap1)
1647                 goto error;
1648         bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1649
1650         bmap1 = isl_basic_map_simplify(bmap1);
1651         return isl_basic_map_finalize(bmap1);
1652 error:
1653         isl_basic_map_free(bmap1);
1654         isl_basic_map_free(bmap2);
1655         return NULL;
1656 }
1657
1658 struct isl_basic_set *isl_basic_set_intersect(
1659                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1660 {
1661         return (struct isl_basic_set *)
1662                 isl_basic_map_intersect(
1663                         (struct isl_basic_map *)bset1,
1664                         (struct isl_basic_map *)bset2);
1665 }
1666
1667 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1668 {
1669         unsigned flags = 0;
1670         struct isl_map *result;
1671         int i, j;
1672
1673         if (!map1 || !map2)
1674                 goto error;
1675
1676         if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
1677             F_ISSET(map2, ISL_MAP_DISJOINT))
1678                 FL_SET(flags, ISL_MAP_DISJOINT);
1679
1680         result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
1681                                 map1->n * map2->n, flags);
1682         if (!result)
1683                 goto error;
1684         for (i = 0; i < map1->n; ++i)
1685                 for (j = 0; j < map2->n; ++j) {
1686                         struct isl_basic_map *part;
1687                         part = isl_basic_map_intersect(
1688                                     isl_basic_map_copy(map1->p[i]),
1689                                     isl_basic_map_copy(map2->p[j]));
1690                         if (isl_basic_map_is_empty(part))
1691                                 isl_basic_map_free(part);
1692                         else
1693                                 result = isl_map_add(result, part);
1694                         if (!result)
1695                                 goto error;
1696                 }
1697         isl_map_free(map1);
1698         isl_map_free(map2);
1699         return result;
1700 error:
1701         isl_map_free(map1);
1702         isl_map_free(map2);
1703         return NULL;
1704 }
1705
1706 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1707 {
1708         return (struct isl_set *)
1709                 isl_map_intersect((struct isl_map *)set1,
1710                                   (struct isl_map *)set2);
1711 }
1712
1713 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1714 {
1715         struct isl_basic_set *bset;
1716         unsigned in;
1717
1718         if (!bmap)
1719                 return NULL;
1720         bmap = isl_basic_map_cow(bmap);
1721         if (!bmap)
1722                 return NULL;
1723         in = bmap->n_in;
1724         bset = isl_basic_set_from_basic_map(bmap);
1725         bset = isl_basic_set_swap_vars(bset, in);
1726         return isl_basic_map_from_basic_set(bset, bset->dim-in, in);
1727 }
1728
1729 /* Turn final n dimensions into existentially quantified variables.
1730  */
1731 struct isl_basic_set *isl_basic_set_project_out(
1732                 struct isl_basic_set *bset, unsigned n, unsigned flags)
1733 {
1734         int i;
1735         size_t row_size;
1736         isl_int **new_div;
1737         isl_int *old;
1738
1739         if (!bset)
1740                 return NULL;
1741
1742         isl_assert(bset->ctx, n <= bset->dim, goto error);
1743
1744         if (n == 0)
1745                 return bset;
1746
1747         bset = isl_basic_set_cow(bset);
1748
1749         row_size = 1 + bset->nparam + bset->dim + bset->extra;
1750         old = bset->block2.data;
1751         bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1752                                         (bset->extra + n) * (1 + row_size));
1753         if (!bset->block2.data)
1754                 goto error;
1755         new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1756         if (!new_div)
1757                 goto error;
1758         for (i = 0; i < n; ++i) {
1759                 new_div[i] = bset->block2.data +
1760                                 (bset->extra + i) * (1 + row_size);
1761                 isl_seq_clr(new_div[i], 1 + row_size);
1762         }
1763         for (i = 0; i < bset->extra; ++i)
1764                 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1765         free(bset->div);
1766         bset->div = new_div;
1767         bset->n_div += n;
1768         bset->extra += n;
1769         bset->dim -= n;
1770         bset = isl_basic_set_simplify(bset);
1771         return isl_basic_set_finalize(bset);
1772 error:
1773         isl_basic_set_free(bset);
1774         return NULL;
1775 }
1776
1777 struct isl_basic_map *isl_basic_map_apply_range(
1778                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1779 {
1780         struct isl_basic_set *bset;
1781         unsigned n_in, n_out;
1782
1783         if (!bmap1 || !bmap2)
1784                 goto error;
1785
1786         isl_assert(bmap->ctx, bmap1->n_out == bmap2->n_in, goto error);
1787         isl_assert(bmap->ctx, bmap1->nparam == bmap2->nparam, goto error);
1788
1789         n_in = bmap1->n_in;
1790         n_out = bmap2->n_out;
1791
1792         bmap2 = isl_basic_map_reverse(bmap2);
1793         if (!bmap2)
1794                 goto error;
1795         bmap1 = isl_basic_map_extend(bmap1, bmap1->nparam,
1796                         bmap1->n_in + bmap2->n_in, bmap1->n_out,
1797                         bmap2->extra, bmap2->n_eq, bmap2->n_ineq);
1798         if (!bmap1)
1799                 goto error;
1800         bmap1 = add_constraints(bmap1, bmap2, bmap1->n_in - bmap2->n_in, 0);
1801         bmap1 = isl_basic_map_simplify(bmap1);
1802         bset = isl_basic_set_from_basic_map(bmap1);
1803         bset = isl_basic_set_project_out(bset,
1804                                                 bset->dim - (n_in + n_out), 0);
1805         return isl_basic_map_from_basic_set(bset, n_in, n_out);
1806 error:
1807         isl_basic_map_free(bmap1);
1808         isl_basic_map_free(bmap2);
1809         return NULL;
1810 }
1811
1812 struct isl_basic_set *isl_basic_set_apply(
1813                 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1814 {
1815         if (!bset || !bmap)
1816                 goto error;
1817
1818         isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
1819
1820         return (struct isl_basic_set *)
1821                 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1822 error:
1823         isl_basic_set_free(bset);
1824         isl_basic_map_free(bmap);
1825         return NULL;
1826 }
1827
1828 struct isl_basic_map *isl_basic_map_apply_domain(
1829                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1830 {
1831         if (!bmap1 || !bmap2)
1832                 goto error;
1833
1834         isl_assert(ctx, bmap1->n_in == bmap2->n_in, goto error);
1835         isl_assert(ctx, bmap1->nparam == bmap2->nparam, goto error);
1836
1837         bmap1 = isl_basic_map_reverse(bmap1);
1838         bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1839         return isl_basic_map_reverse(bmap1);
1840 error:
1841         isl_basic_map_free(bmap1);
1842         isl_basic_map_free(bmap2);
1843         return NULL;
1844 }
1845
1846 static struct isl_basic_map *var_equal(struct isl_ctx *ctx,
1847                 struct isl_basic_map *bmap, unsigned pos)
1848 {
1849         int i;
1850         i = isl_basic_map_alloc_equality(bmap);
1851         if (i < 0)
1852                 goto error;
1853         isl_seq_clr(bmap->eq[i],
1854                     1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
1855         isl_int_set_si(bmap->eq[i][1+bmap->nparam+pos], -1);
1856         isl_int_set_si(bmap->eq[i][1+bmap->nparam+bmap->n_in+pos], 1);
1857         return isl_basic_map_finalize(bmap);
1858 error:
1859         isl_basic_map_free(bmap);
1860         return NULL;
1861 }
1862
1863 static struct isl_basic_map *var_more(struct isl_ctx *ctx,
1864                 struct isl_basic_map *bmap, unsigned pos)
1865 {
1866         int i;
1867         i = isl_basic_map_alloc_inequality(bmap);
1868         if (i < 0)
1869                 goto error;
1870         isl_seq_clr(bmap->ineq[i],
1871                     1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
1872         isl_int_set_si(bmap->ineq[i][0], -1);
1873         isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], -1);
1874         isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], 1);
1875         return isl_basic_map_finalize(bmap);
1876 error:
1877         isl_basic_map_free(bmap);
1878         return NULL;
1879 }
1880
1881 static struct isl_basic_map *var_less(struct isl_ctx *ctx,
1882                 struct isl_basic_map *bmap, unsigned pos)
1883 {
1884         int i;
1885         i = isl_basic_map_alloc_inequality(bmap);
1886         if (i < 0)
1887                 goto error;
1888         isl_seq_clr(bmap->ineq[i],
1889                     1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
1890         isl_int_set_si(bmap->ineq[i][0], -1);
1891         isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], 1);
1892         isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], -1);
1893         return isl_basic_map_finalize(bmap);
1894 error:
1895         isl_basic_map_free(bmap);
1896         return NULL;
1897 }
1898
1899 struct isl_basic_map *isl_basic_map_equal(struct isl_ctx *ctx,
1900                 unsigned nparam, unsigned in, unsigned out, unsigned n_equal)
1901 {
1902         int i;
1903         struct isl_basic_map *bmap;
1904         bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, n_equal, 0);
1905         if (!bmap)
1906                 return NULL;
1907         for (i = 0; i < n_equal && bmap; ++i)
1908                 bmap = var_equal(ctx, bmap, i);
1909         return isl_basic_map_finalize(bmap);
1910 }
1911
1912 struct isl_basic_map *isl_basic_map_less_at(struct isl_ctx *ctx,
1913                 unsigned nparam, unsigned in, unsigned out, unsigned pos)
1914 {
1915         int i;
1916         struct isl_basic_map *bmap;
1917         bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
1918         if (!bmap)
1919                 return NULL;
1920         for (i = 0; i < pos && bmap; ++i)
1921                 bmap = var_equal(ctx, bmap, i);
1922         if (bmap)
1923                 bmap = var_less(ctx, bmap, pos);
1924         return isl_basic_map_finalize(bmap);
1925 }
1926
1927 struct isl_basic_map *isl_basic_map_more_at(struct isl_ctx *ctx,
1928                 unsigned nparam, unsigned in, unsigned out, unsigned pos)
1929 {
1930         int i;
1931         struct isl_basic_map *bmap;
1932         bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
1933         if (!bmap)
1934                 return NULL;
1935         for (i = 0; i < pos && bmap; ++i)
1936                 bmap = var_equal(ctx, bmap, i);
1937         if (bmap)
1938                 bmap = var_more(ctx, bmap, pos);
1939         return isl_basic_map_finalize(bmap);
1940 }
1941
1942 struct isl_basic_map *isl_basic_map_from_basic_set(
1943                 struct isl_basic_set *bset, unsigned n_in, unsigned n_out)
1944 {
1945         struct isl_basic_map *bmap;
1946
1947         bset = isl_basic_set_cow(bset);
1948         if (!bset)
1949                 return NULL;
1950
1951         isl_assert(ctx, bset->dim == n_in + n_out, goto error);
1952         bmap = (struct isl_basic_map *) bset;
1953         bmap->n_in = n_in;
1954         bmap->n_out = n_out;
1955         return isl_basic_map_finalize(bmap);
1956 error:
1957         isl_basic_set_free(bset);
1958         return NULL;
1959 }
1960
1961 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
1962 {
1963         if (!bmap)
1964                 goto error;
1965         if (bmap->n_in == 0)
1966                 return (struct isl_basic_set *)bmap;
1967         bmap = isl_basic_map_cow(bmap);
1968         if (!bmap)
1969                 goto error;
1970         bmap->n_out += bmap->n_in;
1971         bmap->n_in = 0;
1972         bmap = isl_basic_map_finalize(bmap);
1973         return (struct isl_basic_set *)bmap;
1974 error:
1975         return NULL;
1976 }
1977
1978 /* For a div d = floor(f/m), add the constraints
1979  *
1980  *              f - m d >= 0
1981  *              -(f-(n-1)) + m d >= 0
1982  *
1983  * Note that the second constraint is the negation of
1984  *
1985  *              f - m d >= n
1986  */
1987 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
1988 {
1989         int i, j;
1990         unsigned div_pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
1991         unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra;
1992
1993         i = isl_basic_map_alloc_inequality(bmap);
1994         if (i < 0)
1995                 return -1;
1996         isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
1997         isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
1998
1999         j = isl_basic_map_alloc_inequality(bmap);
2000         if (j < 0)
2001                 return -1;
2002         isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2003         isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2004         isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2005         return j;
2006 }
2007
2008 struct isl_basic_set *isl_basic_map_underlying_set(
2009                 struct isl_basic_map *bmap)
2010 {
2011         if (!bmap)
2012                 goto error;
2013         if (bmap->nparam == 0 && bmap->n_in == 0 && bmap->n_div == 0)
2014                 return (struct isl_basic_set *)bmap;
2015         bmap = isl_basic_map_cow(bmap);
2016         if (!bmap)
2017                 goto error;
2018         bmap->n_out += bmap->nparam + bmap->n_in + bmap->n_div;
2019         bmap->nparam = 0;
2020         bmap->n_in = 0;
2021         bmap->extra -= bmap->n_div;
2022         bmap->n_div = 0;
2023         bmap = isl_basic_map_finalize(bmap);
2024         return (struct isl_basic_set *)bmap;
2025 error:
2026         return NULL;
2027 }
2028
2029 struct isl_basic_map *isl_basic_map_overlying_set(
2030         struct isl_basic_set *bset, struct isl_basic_map *like)
2031 {
2032         struct isl_basic_map *bmap;
2033         struct isl_ctx *ctx;
2034         unsigned total;
2035         int i, k;
2036
2037         if (!bset || !like)
2038                 goto error;
2039         ctx = bset->ctx;
2040         isl_assert(ctx, bset->dim ==
2041                         like->nparam + like->n_in + like->n_out + like->n_div,
2042                         goto error);
2043         if (like->nparam == 0 && like->n_in == 0 && like->n_div == 0) {
2044                 isl_basic_map_free(like);
2045                 return (struct isl_basic_map *)bset;
2046         }
2047         bset = isl_basic_set_cow(bset);
2048         if (!bset)
2049                 goto error;
2050         total = bset->dim + bset->extra;
2051         bmap = (struct isl_basic_map *)bset;
2052         bmap->nparam = like->nparam;
2053         bmap->n_in = like->n_in;
2054         bmap->n_out = like->n_out;
2055         bmap->extra += like->n_div;
2056         if (bmap->extra) {
2057                 unsigned ltotal;
2058                 ltotal = total - bmap->extra + like->extra;
2059                 if (ltotal > total)
2060                         ltotal = total;
2061                 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2062                                         bmap->extra * (1 + 1 + total));
2063                 if (isl_blk_is_error(bmap->block2))
2064                         goto error;
2065                 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2066                                                 bmap->extra);
2067                 if (!bmap->div)
2068                         goto error;
2069                 bmap = isl_basic_map_extend(bmap, bmap->nparam,
2070                         bmap->n_in, bmap->n_out, 0, 0, 2 * like->n_div);
2071                 for (i = 0; i < like->n_div; ++i) {
2072                         k = isl_basic_map_alloc_div(bmap);
2073                         if (k < 0)
2074                                 goto error;
2075                         isl_seq_cpy(bmap->div[k], like->div[i], 1 + 1 + ltotal);
2076                         isl_seq_clr(bmap->div[k]+1+1+ltotal, total - ltotal);
2077                         if (add_div_constraints(bmap, k) < 0)
2078                                 goto error;
2079                 }
2080         }
2081         isl_basic_map_free(like);
2082         bmap = isl_basic_map_finalize(bmap);
2083         return bmap;
2084 error:
2085         isl_basic_map_free(like);
2086         isl_basic_set_free(bset);
2087         return NULL;
2088 }
2089
2090 struct isl_basic_set *isl_basic_set_from_underlying_set(
2091         struct isl_basic_set *bset, struct isl_basic_set *like)
2092 {
2093         return (struct isl_basic_set *)
2094                 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2095 }
2096
2097 struct isl_set *isl_set_from_underlying_set(
2098         struct isl_set *set, struct isl_basic_set *like)
2099 {
2100         int i;
2101
2102         if (!set || !like)
2103                 goto error;
2104         isl_assert(set->ctx, set->dim == like->nparam + like->dim + like->n_div,
2105                     goto error);
2106         if (like->nparam == 0 && like->n_div == 0) {
2107                 isl_basic_set_free(like);
2108                 return set;
2109         }
2110         set = isl_set_cow(set);
2111         if (!set)
2112                 goto error;
2113         for (i = 0; i < set->n; ++i) {
2114                 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2115                                                       isl_basic_set_copy(like));
2116                 if (!set->p[i])
2117                         goto error;
2118         }
2119         set->nparam = like->nparam;
2120         set->dim = like->dim;
2121         isl_basic_set_free(like);
2122         return set;
2123 error:
2124         isl_basic_set_free(like);
2125         isl_set_free(set);
2126         return NULL;
2127 }
2128
2129 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2130 {
2131         int i;
2132
2133         map = isl_map_align_divs(map);
2134         map = isl_map_cow(map);
2135         if (!map)
2136                 return NULL;
2137
2138         for (i = 0; i < map->n; ++i) {
2139                 map->p[i] = (struct isl_basic_map *)
2140                                 isl_basic_map_underlying_set(map->p[i]);
2141                 if (!map->p[i])
2142                         goto error;
2143         }
2144         if (map->n == 0)
2145                 map->n_out += map->nparam + map->n_in;
2146         else
2147                 map->n_out = map->p[0]->n_out;
2148         map->nparam = 0;
2149         map->n_in = 0;
2150         return (struct isl_set *)map;
2151 error:
2152         isl_map_free(map);
2153         return NULL;
2154 }
2155
2156 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2157 {
2158         return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2159 }
2160
2161 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2162 {
2163         struct isl_basic_set *domain;
2164         unsigned n_out;
2165         if (!bmap)
2166                 return NULL;
2167         n_out = bmap->n_out;
2168         domain = isl_basic_set_from_basic_map(bmap);
2169         return isl_basic_set_project_out(domain, n_out, 0);
2170 }
2171
2172 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2173 {
2174         return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2175 }
2176
2177 struct isl_set *isl_map_range(struct isl_map *map)
2178 {
2179         int i;
2180         struct isl_set *set;
2181
2182         if (!map)
2183                 goto error;
2184         map = isl_map_cow(map);
2185         if (!map)
2186                 goto error;
2187
2188         set = (struct isl_set *) map;
2189         set->zero = 0;
2190         for (i = 0; i < map->n; ++i) {
2191                 set->p[i] = isl_basic_map_range(map->p[i]);
2192                 if (!set->p[i])
2193                         goto error;
2194         }
2195         F_CLR(set, ISL_MAP_DISJOINT);
2196         return set;
2197 error:
2198         isl_map_free(map);
2199         return NULL;
2200 }
2201
2202 struct isl_map *isl_map_from_set(struct isl_set *set,
2203                 unsigned n_in, unsigned n_out)
2204 {
2205         int i;
2206         struct isl_map *map = NULL;
2207
2208         if (!set)
2209                 return NULL;
2210         isl_assert(set->ctx, set->dim == n_in + n_out, goto error);
2211         set = isl_set_cow(set);
2212         if (!set)
2213                 return NULL;
2214         map = (struct isl_map *)set;
2215         for (i = 0; i < set->n; ++i) {
2216                 map->p[i] = isl_basic_map_from_basic_set(
2217                                 set->p[i], n_in, n_out);
2218                 if (!map->p[i])
2219                         goto error;
2220         }
2221         map->n_in = n_in;
2222         map->n_out = n_out;
2223         return map;
2224 error:
2225         isl_set_free(set);
2226         return NULL;
2227 }
2228
2229 struct isl_set *isl_set_from_map(struct isl_map *map)
2230 {
2231         int i;
2232         struct isl_set *set = NULL;
2233
2234         if (!map)
2235                 return NULL;
2236         map = isl_map_cow(map);
2237         if (!map)
2238                 return NULL;
2239         map->n_out += map->n_in;
2240         map->n_in = 0;
2241         set = (struct isl_set *)map;
2242         for (i = 0; i < map->n; ++i) {
2243                 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2244                 if (!set->p[i])
2245                         goto error;
2246         }
2247         return set;
2248 error:
2249         isl_map_free(map);
2250         return NULL;
2251 }
2252
2253 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2254                 unsigned nparam, unsigned in, unsigned out, int n,
2255                 unsigned flags)
2256 {
2257         struct isl_map *map;
2258
2259         map = isl_alloc(ctx, struct isl_map,
2260                         sizeof(struct isl_map) +
2261                         n * sizeof(struct isl_basic_map *));
2262         if (!map)
2263                 return NULL;
2264
2265         map->ctx = ctx;
2266         isl_ctx_ref(ctx);
2267         map->ref = 1;
2268         map->size = n;
2269         map->n = 0;
2270         map->nparam = nparam;
2271         map->n_in = in;
2272         map->n_out = out;
2273         map->flags = flags;
2274         return map;
2275 }
2276
2277 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2278                 unsigned nparam, unsigned in, unsigned out)
2279 {
2280         struct isl_basic_map *bmap;
2281         bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2282         bmap = isl_basic_map_set_to_empty(bmap);
2283         return bmap;
2284 }
2285
2286 struct isl_basic_set *isl_basic_set_empty(struct isl_ctx *ctx,
2287                 unsigned nparam, unsigned dim)
2288 {
2289         struct isl_basic_set *bset;
2290         bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 1, 0);
2291         bset = isl_basic_set_set_to_empty(bset);
2292         return bset;
2293 }
2294
2295 struct isl_basic_map *isl_basic_map_universe(struct isl_ctx *ctx,
2296                 unsigned nparam, unsigned in, unsigned out)
2297 {
2298         struct isl_basic_map *bmap;
2299         bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 0, 0);
2300         return bmap;
2301 }
2302
2303 struct isl_basic_set *isl_basic_set_universe(struct isl_ctx *ctx,
2304                 unsigned nparam, unsigned dim)
2305 {
2306         struct isl_basic_set *bset;
2307         bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 0, 0);
2308         return bset;
2309 }
2310
2311 struct isl_map *isl_map_empty(struct isl_ctx *ctx,
2312                 unsigned nparam, unsigned in, unsigned out)
2313 {
2314         return isl_map_alloc(ctx, nparam, in, out, 0, ISL_MAP_DISJOINT);
2315 }
2316
2317 struct isl_set *isl_set_empty(struct isl_ctx *ctx,
2318                 unsigned nparam, unsigned dim)
2319 {
2320         return isl_set_alloc(ctx, nparam, dim, 0, ISL_MAP_DISJOINT);
2321 }
2322
2323 struct isl_map *isl_map_dup(struct isl_map *map)
2324 {
2325         int i;
2326         struct isl_map *dup;
2327
2328         if (!map)
2329                 return NULL;
2330         dup = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out, map->n,
2331                                 map->flags);
2332         for (i = 0; i < map->n; ++i)
2333                 dup = isl_map_add(dup,
2334                                   isl_basic_map_dup(map->p[i]));
2335         return dup;
2336 }
2337
2338 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2339 {
2340         if (!bmap || !map)
2341                 goto error;
2342         isl_assert(map->ctx, map->nparam == bmap->nparam, goto error);
2343         isl_assert(map->ctx, map->n_in == bmap->n_in, goto error);
2344         isl_assert(map->ctx, map->n_out == bmap->n_out, goto error);
2345         isl_assert(map->ctx, map->n < map->size, goto error);
2346         map->p[map->n] = bmap;
2347         map->n++;
2348         return map;
2349 error:
2350         if (map)
2351                 isl_map_free(map);
2352         if (bmap)
2353                 isl_basic_map_free(bmap);
2354         return NULL;
2355 }
2356
2357 void isl_map_free(struct isl_map *map)
2358 {
2359         int i;
2360
2361         if (!map)
2362                 return;
2363
2364         if (--map->ref > 0)
2365                 return;
2366
2367         isl_ctx_deref(map->ctx);
2368         for (i = 0; i < map->n; ++i)
2369                 isl_basic_map_free(map->p[i]);
2370         free(map);
2371 }
2372
2373 struct isl_map *isl_map_extend(struct isl_map *base,
2374                 unsigned nparam, unsigned n_in, unsigned n_out)
2375 {
2376         int i;
2377
2378         base = isl_map_cow(base);
2379         if (!base)
2380                 return NULL;
2381
2382         isl_assert(base->ctx, base->nparam <= nparam, goto error);
2383         isl_assert(base->ctx, base->n_in <= n_in, goto error);
2384         isl_assert(base->ctx, base->n_out <= n_out, goto error);
2385         base->nparam = nparam;
2386         base->n_in = n_in;
2387         base->n_out = n_out;
2388         for (i = 0; i < base->n; ++i) {
2389                 base->p[i] = isl_basic_map_extend(base->p[i],
2390                                 nparam, n_in, n_out, 0, 0, 0);
2391                 if (!base->p[i])
2392                         goto error;
2393         }
2394         return base;
2395 error:
2396         isl_map_free(base);
2397         return NULL;
2398 }
2399
2400 struct isl_set *isl_set_extend(struct isl_set *base,
2401                 unsigned nparam, unsigned dim)
2402 {
2403         return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2404                                                         nparam, 0, dim);
2405 }
2406
2407 static struct isl_basic_map *isl_basic_map_fix_var(struct isl_basic_map *bmap,
2408                 unsigned var, int value)
2409 {
2410         int j;
2411
2412         bmap = isl_basic_map_cow(bmap);
2413         bmap = isl_basic_map_extend(bmap,
2414                         bmap->nparam, bmap->n_in, bmap->n_out, 0, 1, 0);
2415         j = isl_basic_map_alloc_equality(bmap);
2416         if (j < 0)
2417                 goto error;
2418         isl_seq_clr(bmap->eq[j],
2419                     1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2420         isl_int_set_si(bmap->eq[j][1 + var], -1);
2421         isl_int_set_si(bmap->eq[j][0], value);
2422         bmap = isl_basic_map_simplify(bmap);
2423         return isl_basic_map_finalize(bmap);
2424 error:
2425         isl_basic_map_free(bmap);
2426         return NULL;
2427 }
2428
2429 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2430                 unsigned input, int value)
2431 {
2432         if (!bmap)
2433                 return NULL;
2434         isl_assert(bmap->ctx, input < bmap->n_in, goto error);
2435         return isl_basic_map_fix_var(bmap, bmap->nparam + input, value);
2436 error:
2437         isl_basic_map_free(bmap);
2438         return NULL;
2439 }
2440
2441 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2442                 unsigned dim, int value)
2443 {
2444         if (!bset)
2445                 return NULL;
2446         isl_assert(bset->ctx, dim < bset->dim, goto error);
2447         return (struct isl_basic_set *)
2448                 isl_basic_map_fix_var((struct isl_basic_map *)bset,
2449                                                 bset->nparam + dim, value);
2450 error:
2451         isl_basic_set_free(bset);
2452         return NULL;
2453 }
2454
2455 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2456                 unsigned input, int value)
2457 {
2458         int i;
2459
2460         map = isl_map_cow(map);
2461         if (!map)
2462                 return NULL;
2463
2464         isl_assert(ctx, input < map->n_in, goto error);
2465         for (i = 0; i < map->n; ++i) {
2466                 map->p[i] = isl_basic_map_fix_input_si(map->p[i], input, value);
2467                 if (!map->p[i])
2468                         goto error;
2469         }
2470         return map;
2471 error:
2472         isl_map_free(map);
2473         return NULL;
2474 }
2475
2476 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2477 {
2478         int i;
2479
2480         set = isl_set_cow(set);
2481         if (!set)
2482                 return NULL;
2483
2484         isl_assert(set->ctx, dim < set->dim, goto error);
2485         for (i = 0; i < set->n; ++i) {
2486                 set->p[i] = isl_basic_set_fix_dim_si(set->p[i], dim, value);
2487                 if (!set->p[i])
2488                         goto error;
2489         }
2490         return set;
2491 error:
2492         isl_set_free(set);
2493         return NULL;
2494 }
2495
2496 struct isl_map *isl_map_reverse(struct isl_map *map)
2497 {
2498         int i;
2499         unsigned t;
2500
2501         map = isl_map_cow(map);
2502         if (!map)
2503                 return NULL;
2504
2505         t = map->n_in;
2506         map->n_in = map->n_out;
2507         map->n_out = t;
2508         for (i = 0; i < map->n; ++i) {
2509                 map->p[i] = isl_basic_map_reverse(map->p[i]);
2510                 if (!map->p[i])
2511                         goto error;
2512         }
2513         return map;
2514 error:
2515         isl_map_free(map);
2516         return NULL;
2517 }
2518
2519 struct isl_map *isl_basic_map_lexmax(
2520                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2521                 struct isl_set **empty)
2522 {
2523         return isl_pip_basic_map_lexmax(bmap, dom, empty);
2524 }
2525
2526 struct isl_map *isl_basic_map_lexmin(
2527                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2528                 struct isl_set **empty)
2529 {
2530         return isl_pip_basic_map_lexmin(bmap, dom, empty);
2531 }
2532
2533 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2534 {
2535         struct isl_basic_map *bmap = NULL;
2536         struct isl_basic_set *dom = NULL;
2537         struct isl_map *min;
2538
2539         if (!bset)
2540                 goto error;
2541         bmap = isl_basic_map_from_basic_set(bset, 0, bset->dim);
2542         if (!bmap)
2543                 goto error;
2544         dom = isl_basic_set_alloc(bmap->ctx, bmap->nparam, 0, 0, 0, 0);
2545         if (!dom)
2546                 goto error;
2547         min = isl_basic_map_lexmin(bmap, dom, NULL);
2548         return isl_map_range(min);
2549 error:
2550         isl_basic_map_free(bmap);
2551         return NULL;
2552 }
2553
2554 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2555 {
2556         if (!bmap)
2557                 return NULL;
2558         if (bmap->n_div == 0)
2559                 return isl_map_from_basic_map(bmap);
2560         return isl_pip_basic_map_compute_divs(bmap);
2561 }
2562
2563 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2564 {
2565         int i;
2566         struct isl_map *res;
2567
2568         if (!map)
2569                 return NULL;
2570         if (map->n == 0)
2571                 return map;
2572         res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2573         for (i = 1 ; i < map->n; ++i) {
2574                 struct isl_map *r2;
2575                 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2576                 if (F_ISSET(map, ISL_MAP_DISJOINT))
2577                         res = isl_map_union_disjoint(res, r2);
2578                 else
2579                         res = isl_map_union(res, r2);
2580         }
2581         isl_map_free(map);
2582
2583         return res;
2584 }
2585
2586 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2587 {
2588         return (struct isl_set *)
2589                 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2590 }
2591
2592 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2593 {
2594         return (struct isl_set *)
2595                 isl_map_compute_divs((struct isl_map *)set);
2596 }
2597
2598 struct isl_set *isl_map_domain(struct isl_map *map)
2599 {
2600         int i;
2601         struct isl_set *set;
2602
2603         if (!map)
2604                 goto error;
2605
2606         map = isl_map_cow(map);
2607         if (!map)
2608                 return NULL;
2609
2610         set = (struct isl_set *)map;
2611         set->dim = map->n_in;
2612         set->zero = 0;
2613         for (i = 0; i < map->n; ++i) {
2614                 set->p[i] = isl_basic_map_domain(map->p[i]);
2615                 if (!set->p[i])
2616                         goto error;
2617         }
2618         F_CLR(set, ISL_MAP_DISJOINT);
2619         return set;
2620 error:
2621         isl_map_free(map);
2622         return NULL;
2623 }
2624
2625 struct isl_map *isl_map_union_disjoint(
2626                         struct isl_map *map1, struct isl_map *map2)
2627 {
2628         int i;
2629         unsigned flags = 0;
2630         struct isl_map *map = NULL;
2631
2632         if (!map1 || !map2)
2633                 goto error;
2634
2635         if (map1->n == 0) {
2636                 isl_map_free(map1);
2637                 return map2;
2638         }
2639         if (map2->n == 0) {
2640                 isl_map_free(map2);
2641                 return map1;
2642         }
2643
2644         isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
2645         isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
2646         isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
2647
2648         if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
2649             F_ISSET(map2, ISL_MAP_DISJOINT))
2650                 FL_SET(flags, ISL_MAP_DISJOINT);
2651
2652         map = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
2653                                 map1->n + map2->n, flags);
2654         if (!map)
2655                 goto error;
2656         for (i = 0; i < map1->n; ++i) {
2657                 map = isl_map_add(map,
2658                                   isl_basic_map_copy(map1->p[i]));
2659                 if (!map)
2660                         goto error;
2661         }
2662         for (i = 0; i < map2->n; ++i) {
2663                 map = isl_map_add(map,
2664                                   isl_basic_map_copy(map2->p[i]));
2665                 if (!map)
2666                         goto error;
2667         }
2668         isl_map_free(map1);
2669         isl_map_free(map2);
2670         return map;
2671 error:
2672         isl_map_free(map);
2673         isl_map_free(map1);
2674         isl_map_free(map2);
2675         return NULL;
2676 }
2677
2678 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
2679 {
2680         map1 = isl_map_union_disjoint(map1, map2);
2681         if (!map1)
2682                 return NULL;
2683         if (map1->n > 1)
2684                 F_CLR(map1, ISL_MAP_DISJOINT);
2685         return map1;
2686 }
2687
2688 struct isl_set *isl_set_union_disjoint(
2689                         struct isl_set *set1, struct isl_set *set2)
2690 {
2691         return (struct isl_set *)
2692                 isl_map_union_disjoint(
2693                         (struct isl_map *)set1, (struct isl_map *)set2);
2694 }
2695
2696 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
2697 {
2698         return (struct isl_set *)
2699                 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
2700 }
2701
2702 struct isl_map *isl_map_intersect_range(
2703                 struct isl_map *map, struct isl_set *set)
2704 {
2705         unsigned flags = 0;
2706         struct isl_map *result;
2707         int i, j;
2708
2709         if (!map || !set)
2710                 goto error;
2711
2712         if (F_ISSET(map, ISL_MAP_DISJOINT) &&
2713             F_ISSET(set, ISL_MAP_DISJOINT))
2714                 FL_SET(flags, ISL_MAP_DISJOINT);
2715
2716         result = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
2717                                 map->n * set->n, flags);
2718         if (!result)
2719                 goto error;
2720         for (i = 0; i < map->n; ++i)
2721                 for (j = 0; j < set->n; ++j) {
2722                         result = isl_map_add(result,
2723                             isl_basic_map_intersect_range(
2724                                 isl_basic_map_copy(map->p[i]),
2725                                 isl_basic_set_copy(set->p[j])));
2726                         if (!result)
2727                                 goto error;
2728                 }
2729         isl_map_free(map);
2730         isl_set_free(set);
2731         return result;
2732 error:
2733         isl_map_free(map);
2734         isl_set_free(set);
2735         return NULL;
2736 }
2737
2738 struct isl_map *isl_map_intersect_domain(
2739                 struct isl_map *map, struct isl_set *set)
2740 {
2741         return isl_map_reverse(
2742                 isl_map_intersect_range(isl_map_reverse(map), set));
2743 }
2744
2745 struct isl_map *isl_map_apply_domain(
2746                 struct isl_map *map1, struct isl_map *map2)
2747 {
2748         if (!map1 || !map2)
2749                 goto error;
2750         map1 = isl_map_reverse(map1);
2751         map1 = isl_map_apply_range(map1, map2);
2752         return isl_map_reverse(map1);
2753 error:
2754         isl_map_free(map1);
2755         isl_map_free(map2);
2756         return NULL;
2757 }
2758
2759 struct isl_map *isl_map_apply_range(
2760                 struct isl_map *map1, struct isl_map *map2)
2761 {
2762         struct isl_map *result;
2763         int i, j;
2764
2765         if (!map1 || !map2)
2766                 goto error;
2767
2768         isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
2769         isl_assert(map1->ctx, map1->n_out == map2->n_in, goto error);
2770
2771         result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map2->n_out,
2772                                 map1->n * map2->n, 0);
2773         if (!result)
2774                 goto error;
2775         for (i = 0; i < map1->n; ++i)
2776                 for (j = 0; j < map2->n; ++j) {
2777                         result = isl_map_add(result,
2778                             isl_basic_map_apply_range(
2779                                 isl_basic_map_copy(map1->p[i]),
2780                                 isl_basic_map_copy(map2->p[j])));
2781                         if (!result)
2782                                 goto error;
2783                 }
2784         isl_map_free(map1);
2785         isl_map_free(map2);
2786         if (result && result->n <= 1)
2787                 F_SET(result, ISL_MAP_DISJOINT);
2788         return result;
2789 error:
2790         isl_map_free(map1);
2791         isl_map_free(map2);
2792         return NULL;
2793 }
2794
2795 /*
2796  * returns range - domain
2797  */
2798 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
2799 {
2800         struct isl_basic_set *bset;
2801         unsigned dim;
2802         int i;
2803
2804         if (!bmap)
2805                 goto error;
2806         isl_assert(bmap->ctx, bmap->n_in == bmap->n_out, goto error);
2807         dim = bmap->n_in;
2808         bset = isl_basic_set_from_basic_map(bmap);
2809         bset = isl_basic_set_extend(bset, bset->nparam, 3*dim, 0,
2810                                         dim, 0);
2811         bset = isl_basic_set_swap_vars(bset, 2*dim);
2812         for (i = 0; i < dim; ++i) {
2813                 int j = isl_basic_map_alloc_equality(
2814                                             (struct isl_basic_map *)bset);
2815                 if (j < 0)
2816                         goto error;
2817                 isl_seq_clr(bset->eq[j],
2818                             1 + bset->nparam + bset->dim + bset->extra);
2819                 isl_int_set_si(bset->eq[j][1+bset->nparam+i], 1);
2820                 isl_int_set_si(bset->eq[j][1+bset->nparam+dim+i], 1);
2821                 isl_int_set_si(bset->eq[j][1+bset->nparam+2*dim+i], -1);
2822         }
2823         return isl_basic_set_project_out(bset, 2*dim, 0);
2824 error:
2825         isl_basic_map_free(bmap);
2826         return NULL;
2827 }
2828
2829 /* If the only constraints a div d=floor(f/m)
2830  * appears in are its two defining constraints
2831  *
2832  *      f - m d >=0
2833  *      -(f - (m - 1)) + m d >= 0
2834  *
2835  * then it can safely be removed.
2836  */
2837 static int div_is_redundant(struct isl_basic_map *bmap, int div)
2838 {
2839         int i;
2840         unsigned pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
2841
2842         for (i = 0; i < bmap->n_eq; ++i)
2843                 if (!isl_int_is_zero(bmap->eq[i][pos]))
2844                         return 0;
2845
2846         for (i = 0; i < bmap->n_ineq; ++i) {
2847                 if (isl_int_is_zero(bmap->ineq[i][pos]))
2848                         continue;
2849                 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
2850                         int neg;
2851                         isl_int_sub(bmap->div[div][1],
2852                                         bmap->div[div][1], bmap->div[div][0]);
2853                         isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
2854                         neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
2855                         isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
2856                         isl_int_add(bmap->div[div][1],
2857                                         bmap->div[div][1], bmap->div[div][0]);
2858                         if (!neg)
2859                                 return 0;
2860                         if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
2861                                                     bmap->n_div-div-1) != -1)
2862                                 return 0;
2863                 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
2864                         if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
2865                                 return 0;
2866                         if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
2867                                                     bmap->n_div-div-1) != -1)
2868                                 return 0;
2869                 } else
2870                         return 0;
2871         }
2872
2873         for (i = 0; i < bmap->n_div; ++i)
2874                 if (!isl_int_is_zero(bmap->div[i][1+pos]))
2875                         return 0;
2876
2877         return 1;
2878 }
2879
2880 /*
2881  * Remove divs that don't occur in any of the constraints or other divs.
2882  * These can arise when dropping some of the variables in a quast
2883  * returned by piplib.
2884  */
2885 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
2886 {
2887         int i;
2888
2889         if (!bmap)
2890                 return NULL;
2891
2892         for (i = bmap->n_div-1; i >= 0; --i) {
2893                 if (!div_is_redundant(bmap, i))
2894                         continue;
2895                 bmap = isl_basic_map_drop_div(bmap, i);
2896         }
2897         return bmap;
2898 }
2899
2900 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
2901 {
2902         bmap = remove_redundant_divs(bmap);
2903         if (!bmap)
2904                 return NULL;
2905         F_SET(bmap, ISL_BASIC_SET_FINAL);
2906         return bmap;
2907 }
2908
2909 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
2910 {
2911         return (struct isl_basic_set *)
2912                 isl_basic_map_finalize((struct isl_basic_map *)bset);
2913 }
2914
2915 struct isl_set *isl_set_finalize(struct isl_set *set)
2916 {
2917         int i;
2918
2919         if (!set)
2920                 return NULL;
2921         for (i = 0; i < set->n; ++i) {
2922                 set->p[i] = isl_basic_set_finalize(set->p[i]);
2923                 if (!set->p[i])
2924                         goto error;
2925         }
2926         return set;
2927 error:
2928         isl_set_free(set);
2929         return NULL;
2930 }
2931
2932 struct isl_map *isl_map_finalize(struct isl_map *map)
2933 {
2934         int i;
2935
2936         if (!map)
2937                 return NULL;
2938         for (i = 0; i < map->n; ++i) {
2939                 map->p[i] = isl_basic_map_finalize(map->p[i]);
2940                 if (!map->p[i])
2941                         goto error;
2942         }
2943         return map;
2944 error:
2945         isl_map_free(map);
2946         return NULL;
2947 }
2948
2949 struct isl_basic_map *isl_basic_map_identity(struct isl_ctx *ctx,
2950                 unsigned nparam, unsigned dim)
2951 {
2952         struct isl_basic_map *bmap;
2953         int i;
2954
2955         bmap = isl_basic_map_alloc(ctx, nparam, dim, dim, 0, dim, 0);
2956         if (!bmap)
2957                 goto error;
2958
2959         for (i = 0; i < dim; ++i) {
2960                 int j = isl_basic_map_alloc_equality(bmap);
2961                 if (j < 0)
2962                         goto error;
2963                 isl_seq_clr(bmap->eq[j],
2964                     1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2965                 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
2966                 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
2967         }
2968         return isl_basic_map_finalize(bmap);
2969 error:
2970         isl_basic_map_free(bmap);
2971         return NULL;
2972 }
2973
2974 struct isl_map *isl_map_identity(struct isl_ctx *ctx,
2975                 unsigned nparam, unsigned dim)
2976 {
2977         struct isl_map *map = isl_map_alloc(ctx, nparam, dim, dim, 1,
2978                                                 ISL_MAP_DISJOINT);
2979         if (!map)
2980                 goto error;
2981         map = isl_map_add(map,
2982                 isl_basic_map_identity(ctx, nparam, dim));
2983         return map;
2984 error:
2985         isl_map_free(map);
2986         return NULL;
2987 }
2988
2989 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
2990 {
2991         return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
2992 }
2993
2994 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
2995 {
2996         return isl_map_is_subset(
2997                         (struct isl_map *)set1, (struct isl_map *)set2);
2998 }
2999
3000 int isl_basic_map_is_subset(
3001                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3002 {
3003         int is_subset;
3004         struct isl_map *map1;
3005         struct isl_map *map2;
3006
3007         if (!bmap1 || !bmap2)
3008                 return -1;
3009
3010         map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3011         map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3012
3013         is_subset = isl_map_is_subset(map1, map2);
3014
3015         isl_map_free(map1);
3016         isl_map_free(map2);
3017
3018         return is_subset;
3019 }
3020
3021 int isl_basic_map_is_equal(
3022                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3023 {
3024         int is_subset;
3025
3026         if (!bmap1 || !bmap2)
3027                 return -1;
3028         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3029         if (is_subset != 1)
3030                 return is_subset;
3031         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3032         return is_subset;
3033 }
3034
3035 int isl_basic_set_is_equal(
3036                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3037 {
3038         return isl_basic_map_is_equal(
3039                 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3040 }
3041
3042 int isl_map_is_empty(struct isl_map *map)
3043 {
3044         int i;
3045         int is_empty;
3046
3047         if (!map)
3048                 return -1;
3049         for (i = 0; i < map->n; ++i) {
3050                 is_empty = isl_basic_map_is_empty(map->p[i]);
3051                 if (is_empty < 0)
3052                         return -1;
3053                 if (!is_empty)
3054                         return 0;
3055         }
3056         return 1;
3057 }
3058
3059 int isl_set_is_empty(struct isl_set *set)
3060 {
3061         return isl_map_is_empty((struct isl_map *)set);
3062 }
3063
3064 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3065 {
3066         int i;
3067         int is_subset = 0;
3068         struct isl_map *diff;
3069
3070         if (!map1 || !map2)
3071                 return -1;
3072
3073         if (isl_map_is_empty(map1))
3074                 return 1;
3075
3076         if (isl_map_is_empty(map2))
3077                 return 0;
3078
3079         diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3080         if (!diff)
3081                 return -1;
3082
3083         is_subset = isl_map_is_empty(diff);
3084         isl_map_free(diff);
3085
3086         return is_subset;
3087 }
3088
3089 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3090 {
3091         int is_subset;
3092
3093         if (!map1 || !map2)
3094                 return -1;
3095         is_subset = isl_map_is_subset(map1, map2);
3096         if (is_subset != 1)
3097                 return is_subset;
3098         is_subset = isl_map_is_subset(map2, map1);
3099         return is_subset;
3100 }
3101
3102 int isl_basic_map_is_strict_subset(
3103                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3104 {
3105         int is_subset;
3106
3107         if (!bmap1 || !bmap2)
3108                 return -1;
3109         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3110         if (is_subset != 1)
3111                 return is_subset;
3112         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3113         if (is_subset == -1)
3114                 return is_subset;
3115         return !is_subset;
3116 }
3117
3118 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
3119 {
3120         int i;
3121         unsigned total;
3122         isl_int s;
3123
3124         total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3125         if (total != vec->size)
3126                 return -1;
3127
3128         isl_int_init(s);
3129
3130         for (i = 0; i < bmap->n_eq; ++i) {
3131                 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
3132                 if (!isl_int_is_zero(s)) {
3133                         isl_int_clear(s);
3134                         return 0;
3135                 }
3136         }
3137
3138         for (i = 0; i < bmap->n_ineq; ++i) {
3139                 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
3140                 if (isl_int_is_neg(s)) {
3141                         isl_int_clear(s);
3142                         return 0;
3143                 }
3144         }
3145
3146         isl_int_clear(s);
3147
3148         return 1;
3149 }
3150
3151 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3152 {
3153         if (!bmap)
3154                 return -1;
3155         return bmap->n_eq == 0 && bmap->n_ineq == 0;
3156 }
3157
3158 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3159 {
3160         struct isl_basic_set *bset = NULL;
3161         struct isl_vec *sample = NULL;
3162         int empty;
3163         unsigned total;
3164
3165         if (!bmap)
3166                 return -1;
3167
3168         if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3169                 return 1;
3170
3171         total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3172         if (bmap->sample && bmap->sample->size == total) {
3173                 int contains = basic_map_contains(bmap, bmap->sample);
3174                 if (contains < 0)
3175                         return -1;
3176                 if (contains)
3177                         return 0;
3178         }
3179         bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3180         if (!bset)
3181                 return -1;
3182         sample = isl_basic_set_sample(bset);
3183         if (!sample)
3184                 return -1;
3185         empty = sample->size == 0;
3186         if (bmap->sample)
3187                 isl_vec_free(bmap->ctx, bmap->sample);
3188         bmap->sample = sample;
3189
3190         return empty;
3191 }
3192
3193 struct isl_map *isl_basic_map_union(
3194         struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3195 {
3196         struct isl_map *map;
3197         if (!bmap1 || !bmap2)
3198                 return NULL;
3199
3200         isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
3201         isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
3202         isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
3203
3204         map = isl_map_alloc(bmap1->ctx, bmap1->nparam,
3205                                 bmap1->n_in, bmap1->n_out, 2, 0);
3206         if (!map)
3207                 goto error;
3208         map = isl_map_add(map, bmap1);
3209         map = isl_map_add(map, bmap2);
3210         return map;
3211 error:
3212         isl_basic_map_free(bmap1);
3213         isl_basic_map_free(bmap2);
3214         return NULL;
3215 }
3216
3217 struct isl_set *isl_basic_set_union(
3218                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3219 {
3220         return (struct isl_set *)isl_basic_map_union(
3221                                             (struct isl_basic_map *)bset1,
3222                                             (struct isl_basic_map *)bset2);
3223 }
3224
3225 /* Order divs such that any div only depends on previous divs */
3226 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3227 {
3228         int i;
3229         unsigned off = bmap->nparam + bmap->n_in + bmap->n_out;
3230
3231         for (i = 0; i < bmap->n_div; ++i) {
3232                 int pos;
3233                 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3234                                                             bmap->n_div-i);
3235                 if (pos == -1)
3236                         continue;
3237                 swap_div(bmap, i, pos);
3238                 --i;
3239         }
3240         return bmap;
3241 }
3242
3243 static int find_div(struct isl_basic_map *dst,
3244                         struct isl_basic_map *src, unsigned div)
3245 {
3246         int i;
3247
3248         unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3249
3250         for (i = 0; i < dst->n_div; ++i)
3251                 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total) &&
3252                     isl_seq_first_non_zero(dst->div[i]+1+1+total,
3253                                                 dst->n_div - src->n_div) == -1)
3254                         return i;
3255         return -1;
3256 }
3257
3258 struct isl_basic_map *isl_basic_map_align_divs(
3259                 struct isl_basic_map *dst, struct isl_basic_map *src)
3260 {
3261         int i;
3262         unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3263
3264         if (src->n_div == 0)
3265                 return dst;
3266
3267         src = order_divs(src);
3268         dst = isl_basic_map_extend(dst, dst->nparam, dst->n_in,
3269                         dst->n_out, src->n_div, 0, 2 * src->n_div);
3270         if (!dst)
3271                 return NULL;
3272         for (i = 0; i < src->n_div; ++i) {
3273                 int j = find_div(dst, src, i);
3274                 if (j < 0) {
3275                         j = isl_basic_map_alloc_div(dst);
3276                         if (j < 0)
3277                                 goto error;
3278                         isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
3279                         isl_seq_clr(dst->div[j]+1+1+total,
3280                                                     dst->extra - src->n_div);
3281                         if (add_div_constraints(dst, j) < 0)
3282                                 goto error;
3283                 }
3284                 if (j != i)
3285                         swap_div(dst, i, j);
3286         }
3287         return dst;
3288 error:
3289         isl_basic_map_free(dst);
3290         return NULL;
3291 }
3292
3293 struct isl_map *isl_map_align_divs(struct isl_map *map)
3294 {
3295         int i;
3296
3297         map = isl_map_compute_divs(map);
3298         map = isl_map_cow(map);
3299         if (!map)
3300                 return NULL;
3301
3302         for (i = 1; i < map->n; ++i)
3303                 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3304         for (i = 1; i < map->n; ++i)
3305                 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3306
3307         return map;
3308 }
3309
3310 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3311                 struct isl_basic_map *src, isl_int *c,
3312                 unsigned len, int oppose)
3313 {
3314         struct isl_basic_map *copy = NULL;
3315         int is_empty;
3316         int k;
3317         unsigned total;
3318
3319         copy = isl_basic_map_copy(src);
3320         copy = isl_basic_map_cow(copy);
3321         if (!copy)
3322                 goto error;
3323         copy = isl_basic_map_extend(copy,
3324                 copy->nparam, copy->n_in, copy->n_out, 0, 0, 1);
3325         k = isl_basic_map_alloc_inequality(copy);
3326         if (k < 0)
3327                 goto error;
3328         if (oppose)
3329                 isl_seq_neg(copy->ineq[k], c, len);
3330         else
3331                 isl_seq_cpy(copy->ineq[k], c, len);
3332         total = 1 + copy->nparam + copy->n_in + copy->n_out + copy->extra;
3333         isl_seq_clr(copy->ineq[k]+len, total - len);
3334         isl_inequality_negate(copy, k);
3335         copy = isl_basic_map_simplify(copy);
3336         copy = isl_basic_map_finalize(copy);
3337         is_empty = isl_basic_map_is_empty(copy);
3338         if (is_empty < 0)
3339                 goto error;
3340         if (!is_empty)
3341                 dst = isl_map_add(dst, copy);
3342         else
3343                 isl_basic_map_free(copy);
3344         return dst;
3345 error:
3346         isl_basic_map_free(copy);
3347         isl_map_free(dst);
3348         return NULL;
3349 }
3350
3351 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3352 {
3353         int i, j, k;
3354         unsigned flags = 0;
3355         struct isl_map *rest = NULL;
3356         unsigned max;
3357         unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3358
3359         assert(bmap);
3360
3361         if (!map)
3362                 goto error;
3363
3364         if (F_ISSET(map, ISL_MAP_DISJOINT))
3365                 FL_SET(flags, ISL_MAP_DISJOINT);
3366
3367         max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3368         rest = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
3369                                 max, flags);
3370         if (!rest)
3371                 goto error;
3372
3373         for (i = 0; i < map->n; ++i) {
3374                 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3375                 if (!map->p[i])
3376                         goto error;
3377         }
3378
3379         for (j = 0; j < map->n; ++j)
3380                 map->p[j] = isl_basic_map_cow(map->p[j]);
3381
3382         for (i = 0; i < bmap->n_eq; ++i) {
3383                 for (j = 0; j < map->n; ++j) {
3384                         rest = add_cut_constraint(rest,
3385                                 map->p[j], bmap->eq[i], 1+total, 0);
3386                         if (!rest)
3387                                 goto error;
3388
3389                         rest = add_cut_constraint(rest,
3390                                 map->p[j], bmap->eq[i], 1+total, 1);
3391                         if (!rest)
3392                                 goto error;
3393
3394                         map->p[j] = isl_basic_map_extend(map->p[j],
3395                                 map->p[j]->nparam, map->p[j]->n_in,
3396                                 map->p[j]->n_out, 0, 1, 0);
3397                         if (!map->p[j])
3398                                 goto error;
3399                         k = isl_basic_map_alloc_equality(map->p[j]);
3400                         if (k < 0)
3401                                 goto error;
3402                         isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3403                         isl_seq_clr(map->p[j]->eq[k]+1+total,
3404                                         map->p[j]->extra - bmap->n_div);
3405                 }
3406         }
3407
3408         for (i = 0; i < bmap->n_ineq; ++i) {
3409                 for (j = 0; j < map->n; ++j) {
3410                         rest = add_cut_constraint(rest,
3411                                 map->p[j], bmap->ineq[i], 1+total, 0);
3412                         if (!rest)
3413                                 goto error;
3414
3415                         map->p[j] = isl_basic_map_extend(map->p[j],
3416                                 map->p[j]->nparam, map->p[j]->n_in,
3417                                 map->p[j]->n_out, 0, 0, 1);
3418                         if (!map->p[j])
3419                                 goto error;
3420                         k = isl_basic_map_alloc_inequality(map->p[j]);
3421                         if (k < 0)
3422                                 goto error;
3423                         isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3424                         isl_seq_clr(map->p[j]->ineq[k]+1+total,
3425                                         map->p[j]->extra - bmap->n_div);
3426                 }
3427         }
3428
3429         isl_map_free(map);
3430         return rest;
3431 error:
3432         isl_map_free(map);
3433         isl_map_free(rest);
3434         return NULL;
3435 }
3436
3437 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3438 {
3439         int i;
3440         if (!map1 || !map2)
3441                 goto error;
3442
3443         isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
3444         isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
3445         isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
3446
3447         if (isl_map_is_empty(map2)) {
3448                 isl_map_free(map2);
3449                 return map1;
3450         }
3451
3452         map1 = isl_map_compute_divs(map1);
3453         map2 = isl_map_compute_divs(map2);
3454         if (!map1 || !map2)
3455                 goto error;
3456
3457         for (i = 0; map1 && i < map2->n; ++i)
3458                 map1 = subtract(map1, map2->p[i]);
3459
3460         isl_map_free(map2);
3461         return map1;
3462 error:
3463         isl_map_free(map1);
3464         isl_map_free(map2);
3465         return NULL;
3466 }
3467
3468 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3469 {
3470         return (struct isl_set *)
3471                 isl_map_subtract(
3472                         (struct isl_map *)set1, (struct isl_map *)set2);
3473 }
3474
3475 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3476 {
3477         if (!set || !map)
3478                 goto error;
3479         isl_assert(set->ctx, set->dim == map->n_in, goto error);
3480         map = isl_map_intersect_domain(map, set);
3481         set = isl_map_range(map);
3482         return set;
3483 error:
3484         isl_set_free(set);
3485         isl_map_free(map);
3486         return NULL;
3487 }
3488
3489 /* There is no need to cow as removing empty parts doesn't change
3490  * the meaning of the set.
3491  */
3492 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3493 {
3494         int i;
3495
3496         if (!map)
3497                 return NULL;
3498
3499         for (i = map->n-1; i >= 0; --i) {
3500                 if (!F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3501                         continue;
3502                 isl_basic_map_free(map->p[i]);
3503                 if (i != map->n-1)
3504                         map->p[i] = map->p[map->n-1];
3505                 map->n--;
3506         }
3507
3508         return map;
3509 }
3510
3511 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3512 {
3513         return (struct isl_set *)
3514                 isl_map_remove_empty_parts((struct isl_map *)set);
3515 }