isl_basic_map_is_empty: don't compute sample for rational sets
[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_dims(
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 static struct isl_set *isl_set_drop_dims(
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_dims(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                 if (progress)
959                         *progress = 1;
960                 l = index[h] - 1;
961                 if (isl_int_lt(bmap->ineq[k][0], bmap->ineq[l][0]))
962                         swap_inequality(bmap, k, l);
963                 isl_basic_map_drop_inequality(bmap, k);
964                 --k;
965         }
966         isl_int_init(sum);
967         for (k = 0; k < bmap->n_ineq-1; ++k) {
968                 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
969                 h = hash_index(index, size, bits, bmap, k);
970                 isl_seq_neg(bmap->ineq[k]+1, bmap->ineq[k]+1, total);
971                 if (!index[h])
972                         continue;
973                 l = index[h] - 1;
974                 isl_int_add(sum, bmap->ineq[k][0], bmap->ineq[l][0]);
975                 if (isl_int_is_pos(sum))
976                         continue;
977                 if (isl_int_is_zero(sum)) {
978                         /* We need to break out of the loop after these
979                          * changes since the contents of the hash
980                          * will no longer be valid.
981                          * Plus, we probably we want to regauss first.
982                          */
983                         isl_basic_map_drop_inequality(bmap, l);
984                         isl_basic_map_inequality_to_equality(bmap, k);
985                 } else
986                         bmap = isl_basic_map_set_to_empty(bmap);
987                 break;
988         }
989         isl_int_clear(sum);
990
991         free(index);
992         return bmap;
993 }
994
995 static struct isl_basic_map *remove_duplicate_divs(
996         struct isl_basic_map *bmap, int *progress)
997 {
998         unsigned int size;
999         int *index;
1000         int k, l, h;
1001         int bits;
1002         struct isl_blk eq;
1003         unsigned total_var = bmap->nparam + bmap->n_in + bmap->n_out;
1004         unsigned total = total_var + bmap->n_div;
1005         struct isl_ctx *ctx;
1006
1007         if (bmap->n_div <= 1)
1008                 return bmap;
1009
1010         ctx = bmap->ctx;
1011         for (k = bmap->n_div - 1; k >= 0; --k)
1012                 if (!isl_int_is_zero(bmap->div[k][0]))
1013                         break;
1014         if (k <= 0)
1015                 return bmap;
1016
1017         size = round_up(4 * bmap->n_div / 3 - 1);
1018         bits = ffs(size) - 1;
1019         index = isl_alloc_array(ctx, int, size);
1020         memset(index, 0, size * sizeof(int));
1021         if (!index)
1022                 return bmap;
1023         eq = isl_blk_alloc(ctx, 1+total);
1024         if (isl_blk_is_error(eq))
1025                 goto out;
1026
1027         isl_seq_clr(eq.data, 1+total);
1028         index[isl_seq_hash(bmap->div[k], 2+total, bits)] = k + 1;
1029         for (--k; k >= 0; --k) {
1030                 u_int32_t hash;
1031
1032                 if (isl_int_is_zero(bmap->div[k][0]))
1033                         continue;
1034
1035                 hash = isl_seq_hash(bmap->div[k], 2+total, bits);
1036                 for (h = hash; index[h]; h = (h+1) % size)
1037                         if (isl_seq_eq(bmap->div[k],
1038                                        bmap->div[index[h]-1], 2+total))
1039                                 break;
1040                 if (index[h]) {
1041                         *progress = 1;
1042                         l = index[h] - 1;
1043                         isl_int_set_si(eq.data[1+total_var+k], -1);
1044                         isl_int_set_si(eq.data[1+total_var+l], 1);
1045                         eliminate_div(bmap, eq.data, l);
1046                         isl_int_set_si(eq.data[1+total_var+k], 0);
1047                         isl_int_set_si(eq.data[1+total_var+l], 0);
1048                 }
1049                 index[h] = k+1;
1050         }
1051
1052         isl_blk_free(ctx, eq);
1053 out:
1054         free(index);
1055         return bmap;
1056 }
1057
1058 /* Elimininate divs based on equalities
1059  */
1060 static struct isl_basic_map *eliminate_divs_eq(
1061                 struct isl_basic_map *bmap, int *progress)
1062 {
1063         int d;
1064         int i;
1065         int modified = 0;
1066         unsigned off;
1067
1068         if (!bmap)
1069                 return NULL;
1070
1071         off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1072
1073         for (d = bmap->n_div - 1; d >= 0 ; --d) {
1074                 for (i = 0; i < bmap->n_eq; ++i) {
1075                         if (!isl_int_is_one(bmap->eq[i][off + d]) &&
1076                             !isl_int_is_negone(bmap->eq[i][off + d]))
1077                                 continue;
1078                         modified = 1;
1079                         *progress = 1;
1080                         eliminate_div(bmap, bmap->eq[i], d);
1081                         isl_basic_map_drop_equality(bmap, i);
1082                         break;
1083                 }
1084         }
1085         if (modified)
1086                 return eliminate_divs_eq(bmap, progress);
1087         return bmap;
1088 }
1089
1090 static struct isl_basic_map *normalize_constraints(struct isl_basic_map *bmap)
1091 {
1092         int i;
1093         isl_int gcd;
1094         unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1095
1096         isl_int_init(gcd);
1097         for (i = bmap->n_eq - 1; i >= 0; --i) {
1098                 isl_seq_gcd(bmap->eq[i]+1, total, &gcd);
1099                 if (isl_int_is_zero(gcd)) {
1100                         if (!isl_int_is_zero(bmap->eq[i][0])) {
1101                                 bmap = isl_basic_map_set_to_empty(bmap);
1102                                 break;
1103                         }
1104                         isl_basic_map_drop_equality(bmap, i);
1105                         continue;
1106                 }
1107                 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1108                         isl_int_gcd(gcd, gcd, bmap->eq[i][0]);
1109                 if (isl_int_is_one(gcd))
1110                         continue;
1111                 if (!isl_int_is_divisible_by(bmap->eq[i][0], gcd)) {
1112                         bmap = isl_basic_map_set_to_empty(bmap);
1113                         break;
1114                 }
1115                 isl_seq_scale_down(bmap->eq[i], bmap->eq[i], gcd, 1+total);
1116         }
1117
1118         for (i = bmap->n_ineq - 1; i >= 0; --i) {
1119                 isl_seq_gcd(bmap->ineq[i]+1, total, &gcd);
1120                 if (isl_int_is_zero(gcd)) {
1121                         if (isl_int_is_neg(bmap->ineq[i][0])) {
1122                                 bmap = isl_basic_map_set_to_empty(bmap);
1123                                 break;
1124                         }
1125                         isl_basic_map_drop_inequality(bmap, i);
1126                         continue;
1127                 }
1128                 if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
1129                         isl_int_gcd(gcd, gcd, bmap->ineq[i][0]);
1130                 if (isl_int_is_one(gcd))
1131                         continue;
1132                 isl_int_fdiv_q(bmap->ineq[i][0], bmap->ineq[i][0], gcd);
1133                 isl_seq_scale_down(bmap->ineq[i]+1, bmap->ineq[i]+1, gcd, total);
1134         }
1135         isl_int_clear(gcd);
1136
1137         return bmap;
1138 }
1139
1140
1141 /* Eliminate the specified variables from the constraints using
1142  * Fourier-Motzkin.  The variables themselves are not removed.
1143  */
1144 struct isl_basic_map *isl_basic_map_eliminate_vars(
1145         struct isl_basic_map *bmap, int pos, unsigned n)
1146 {
1147         int d;
1148         int i, j, k;
1149         unsigned total;
1150         unsigned extra;
1151
1152         if (!bmap)
1153                 return NULL;
1154         total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1155         extra = bmap->extra - bmap->n_div;
1156
1157         bmap = isl_basic_map_cow(bmap);
1158         bmap = isl_basic_map_gauss(bmap, NULL);
1159         for (d = pos + n - 1; d >= pos; --d) {
1160                 int n_lower, n_upper;
1161                 if (!bmap)
1162                         return NULL;
1163                 for (i = 0; i < bmap->n_eq; ++i) {
1164                         if (isl_int_is_zero(bmap->eq[i][1+d]))
1165                                 continue;
1166                         isl_basic_map_drop_equality(bmap, i);
1167                         break;
1168                 }
1169                 if (i < bmap->n_eq)
1170                         continue;
1171                 n_lower = 0;
1172                 n_upper = 0;
1173                 for (i = 0; i < bmap->n_ineq; ++i) {
1174                         if (isl_int_is_pos(bmap->ineq[i][1+d]))
1175                                 n_lower++;
1176                         else if (isl_int_is_neg(bmap->ineq[i][1+d]))
1177                                 n_upper++;
1178                 }
1179                 bmap = isl_basic_map_extend(bmap,
1180                                 bmap->nparam, bmap->n_in, bmap->n_out, 0,
1181                                 0, n_lower * n_upper);
1182                 for (i = bmap->n_ineq - 1; i >= 0; --i) {
1183                         int last;
1184                         if (isl_int_is_zero(bmap->ineq[i][1+d]))
1185                                 continue;
1186                         last = -1;
1187                         for (j = 0; j < i; ++j) {
1188                                 if (isl_int_is_zero(bmap->ineq[j][1+d]))
1189                                         continue;
1190                                 last = j;
1191                                 if (isl_int_sgn(bmap->ineq[i][1+d]) ==
1192                                     isl_int_sgn(bmap->ineq[j][1+d]))
1193                                         continue;
1194                                 k = isl_basic_map_alloc_inequality(bmap);
1195                                 if (k < 0)
1196                                         goto error;
1197                                 isl_seq_cpy(bmap->ineq[k], bmap->ineq[i],
1198                                                 1+total);
1199                                 isl_seq_clr(bmap->ineq[k]+1+total, extra);
1200                                 isl_seq_elim(bmap->ineq[k], bmap->ineq[j],
1201                                                 1+d, 1+total, NULL);
1202                         }
1203                         isl_basic_map_drop_inequality(bmap, i);
1204                         i = last + 1;
1205                 }
1206                 if (n_lower > 0 && n_upper > 0) {
1207                         bmap = normalize_constraints(bmap);
1208                         bmap = remove_duplicate_constraints(bmap, NULL);
1209                         bmap = isl_basic_map_gauss(bmap, NULL);
1210                         bmap = isl_basic_map_convex_hull(bmap);
1211                         if (!bmap)
1212                                 goto error;
1213                         if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1214                                 break;
1215                 }
1216         }
1217         return bmap;
1218 error:
1219         isl_basic_map_free(bmap);
1220         return NULL;
1221 }
1222
1223 struct isl_basic_set *isl_basic_set_eliminate_vars(
1224         struct isl_basic_set *bset, unsigned pos, unsigned n)
1225 {
1226         return (struct isl_basic_set *)isl_basic_map_eliminate_vars(
1227                         (struct isl_basic_map *)bset, pos, n);
1228 }
1229
1230 /* Project out n dimensions starting at first using Fourier-Motzkin */
1231 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1232         unsigned first, unsigned n)
1233 {
1234         int i;
1235
1236         if (n == 0)
1237                 return set;
1238
1239         set = isl_set_cow(set);
1240         if (!set)
1241                 return NULL;
1242         isl_assert(set->ctx, first+n <= set->dim, goto error);
1243         
1244         for (i = 0; i < set->n; ++i) {
1245                 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1246                                                         set->nparam + first, n);
1247                 if (!set->p[i])
1248                         goto error;
1249         }
1250         set = isl_set_drop_dims(set, first, n);
1251         return set;
1252 error:
1253         isl_set_free(set);
1254         return NULL;
1255 }
1256
1257 /* Project out n dimensions starting at first using Fourier-Motzkin */
1258 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1259         unsigned first, unsigned n)
1260 {
1261         bset = isl_basic_set_eliminate_vars(bset, bset->nparam + first, n);
1262         bset = isl_basic_set_drop_dims(bset, first, n);
1263         return bset;
1264 }
1265
1266 /* Elimininate divs based on inequalities
1267  */
1268 static struct isl_basic_map *eliminate_divs_ineq(
1269                 struct isl_basic_map *bmap, int *progress)
1270 {
1271         int d;
1272         int i;
1273         unsigned off;
1274         struct isl_ctx *ctx;
1275
1276         if (!bmap)
1277                 return NULL;
1278
1279         ctx = bmap->ctx;
1280         off = 1 + bmap->nparam + bmap->n_in + bmap->n_out;
1281
1282         for (d = bmap->n_div - 1; d >= 0 ; --d) {
1283                 for (i = 0; i < bmap->n_eq; ++i)
1284                         if (!isl_int_is_zero(bmap->eq[i][off + d]))
1285                                 break;
1286                 if (i < bmap->n_eq)
1287                         continue;
1288                 for (i = 0; i < bmap->n_ineq; ++i)
1289                         if (isl_int_abs_gt(bmap->ineq[i][off + d], ctx->one))
1290                                 break;
1291                 if (i < bmap->n_ineq)
1292                         continue;
1293                 *progress = 1;
1294                 bmap = isl_basic_map_eliminate_vars(bmap, (off-1)+d, 1);
1295                 if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
1296                         break;
1297                 bmap = isl_basic_map_drop_div(bmap, d);
1298                 if (!bmap)
1299                         break;
1300         }
1301         return bmap;
1302 }
1303
1304 struct isl_basic_map *isl_basic_map_simplify(struct isl_basic_map *bmap)
1305 {
1306         int progress = 1;
1307         if (!bmap)
1308                 return NULL;
1309         while (progress) {
1310                 progress = 0;
1311                 bmap = normalize_constraints(bmap);
1312                 bmap = eliminate_divs_eq(bmap, &progress);
1313                 bmap = eliminate_divs_ineq(bmap, &progress);
1314                 bmap = isl_basic_map_gauss(bmap, &progress);
1315                 bmap = remove_duplicate_divs(bmap, &progress);
1316                 bmap = remove_duplicate_constraints(bmap, &progress);
1317         }
1318         return bmap;
1319 }
1320
1321 struct isl_basic_set *isl_basic_set_simplify(struct isl_basic_set *bset)
1322 {
1323         return (struct isl_basic_set *)
1324                 isl_basic_map_simplify((struct isl_basic_map *)bset);
1325 }
1326
1327 static void dump_term(struct isl_basic_map *bmap,
1328                         isl_int c, int pos, FILE *out)
1329 {
1330         unsigned in = bmap->n_in;
1331         unsigned dim = bmap->n_in + bmap->n_out;
1332         if (!pos)
1333                 isl_int_print(out, c);
1334         else {
1335                 if (!isl_int_is_one(c))
1336                         isl_int_print(out, c);
1337                 if (pos < 1 + bmap->nparam)
1338                         fprintf(out, "p%d", pos - 1);
1339                 else if (pos < 1 + bmap->nparam + in)
1340                         fprintf(out, "i%d", pos - 1 - bmap->nparam);
1341                 else if (pos < 1 + bmap->nparam + dim)
1342                         fprintf(out, "o%d", pos - 1 - bmap->nparam - in);
1343                 else
1344                         fprintf(out, "e%d", pos - 1 - bmap->nparam - dim);
1345         }
1346 }
1347
1348 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1349                                 int sign, FILE *out)
1350 {
1351         int i;
1352         int first;
1353         unsigned len = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1354         isl_int v;
1355
1356         isl_int_init(v);
1357         for (i = 0, first = 1; i < len; ++i) {
1358                 if (isl_int_sgn(c[i]) * sign <= 0)
1359                         continue;
1360                 if (!first)
1361                         fprintf(out, " + ");
1362                 first = 0;
1363                 isl_int_abs(v, c[i]);
1364                 dump_term(bmap, v, i, out);
1365         }
1366         isl_int_clear(v);
1367         if (first)
1368                 fprintf(out, "0");
1369 }
1370
1371 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1372                                 const char *op, FILE *out, int indent)
1373 {
1374         int i;
1375
1376         fprintf(out, "%*s", indent, "");
1377
1378         dump_constraint_sign(bmap, c, 1, out);
1379         fprintf(out, " %s ", op);
1380         dump_constraint_sign(bmap, c, -1, out);
1381
1382         fprintf(out, "\n");
1383
1384         for (i = bmap->n_div; i < bmap->extra; ++i) {
1385                 if (isl_int_is_zero(c[1+bmap->nparam+bmap->n_in+bmap->n_out+i]))
1386                         continue;
1387                 fprintf(out, "%*s", indent, "");
1388                 fprintf(out, "ERROR: unused div coefficient not zero\n");
1389         }
1390 }
1391
1392 static void dump_constraints(struct isl_basic_map *bmap,
1393                                 isl_int **c, unsigned n,
1394                                 const char *op, FILE *out, int indent)
1395 {
1396         int i;
1397
1398         for (i = 0; i < n; ++i)
1399                 dump_constraint(bmap, c[i], op, out, indent);
1400 }
1401
1402 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1403 {
1404         int j;
1405         int first = 1;
1406         unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1407
1408         for (j = 0; j < 1 + total; ++j) {
1409                 if (isl_int_is_zero(exp[j]))
1410                         continue;
1411                 if (!first && isl_int_is_pos(exp[j]))
1412                         fprintf(out, "+");
1413                 dump_term(bmap, exp[j], j, out);
1414                 first = 0;
1415         }
1416 }
1417
1418 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1419 {
1420         int i;
1421
1422         dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1423         dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1424
1425         for (i = 0; i < bmap->n_div; ++i) {
1426                 fprintf(out, "%*s", indent, "");
1427                 fprintf(out, "e%d = [(", i);
1428                 dump_affine(bmap, bmap->div[i]+1, out);
1429                 fprintf(out, ")/");
1430                 isl_int_print(out, bmap->div[i][0]);
1431                 fprintf(out, "]\n");
1432         }
1433 }
1434
1435 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1436 {
1437         if (!bset) {
1438                 fprintf(out, "null basic set\n");
1439                 return;
1440         }
1441
1442         fprintf(out, "%*s", indent, "");
1443         fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1444                         bset->ref, bset->nparam, bset->dim, bset->extra,
1445                         bset->flags);
1446         dump((struct isl_basic_map *)bset, out, indent);
1447 }
1448
1449 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1450 {
1451         if (!bmap) {
1452                 fprintf(out, "null basic map\n");
1453                 return;
1454         }
1455
1456         fprintf(out, "%*s", indent, "");
1457         fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, flags: %x\n",
1458                 bmap->ref,
1459                 bmap->nparam, bmap->n_in, bmap->n_out, bmap->extra, bmap->flags);
1460         dump(bmap, out, indent);
1461 }
1462
1463 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1464 {
1465         unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
1466         if (!bmap)
1467                 return -1;
1468         isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1469         isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1470         isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1471         return 0;
1472 }
1473
1474 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1475                 unsigned nparam, unsigned dim, int n, unsigned flags)
1476 {
1477         struct isl_set *set;
1478
1479         isl_assert(ctx, n >= 0, return NULL);
1480         set = isl_alloc(ctx, struct isl_set,
1481                         sizeof(struct isl_set) +
1482                         n * sizeof(struct isl_basic_set *));
1483         if (!set)
1484                 return NULL;
1485
1486         set->ctx = ctx;
1487         isl_ctx_ref(ctx);
1488         set->ref = 1;
1489         set->size = n;
1490         set->n = 0;
1491         set->nparam = nparam;
1492         set->zero = 0;
1493         set->dim = dim;
1494         set->flags = flags;
1495         return set;
1496 }
1497
1498 struct isl_set *isl_set_dup(struct isl_set *set)
1499 {
1500         int i;
1501         struct isl_set *dup;
1502
1503         if (!set)
1504                 return NULL;
1505
1506         dup = isl_set_alloc(set->ctx, set->nparam, set->dim, set->n, set->flags);
1507         if (!dup)
1508                 return NULL;
1509         for (i = 0; i < set->n; ++i)
1510                 dup = isl_set_add(dup,
1511                                   isl_basic_set_dup(set->p[i]));
1512         return dup;
1513 }
1514
1515 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1516 {
1517         struct isl_set *set;
1518
1519         if (!bset)
1520                 return NULL;
1521
1522         set = isl_set_alloc(bset->ctx, bset->nparam, bset->dim, 1, ISL_MAP_DISJOINT);
1523         if (!set) {
1524                 isl_basic_set_free(bset);
1525                 return NULL;
1526         }
1527         return isl_set_add(set, bset);
1528 }
1529
1530 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1531 {
1532         struct isl_map *map;
1533
1534         if (!bmap)
1535                 return NULL;
1536
1537         map = isl_map_alloc(bmap->ctx, bmap->nparam, bmap->n_in, bmap->n_out, 1,
1538                                 ISL_MAP_DISJOINT);
1539         if (!map) {
1540                 isl_basic_map_free(bmap);
1541                 return NULL;
1542         }
1543         return isl_map_add(map, bmap);
1544 }
1545
1546 struct isl_set *isl_set_add(struct isl_set *set, struct isl_basic_set *bset)
1547 {
1548         if (!bset || !set)
1549                 goto error;
1550         isl_assert(set->ctx, set->nparam == bset->nparam, goto error);
1551         isl_assert(set->ctx, set->dim == bset->dim, goto error);
1552         isl_assert(set->ctx, set->n < set->size, goto error);
1553         set->p[set->n] = bset;
1554         set->n++;
1555         return set;
1556 error:
1557         if (set)
1558                 isl_set_free(set);
1559         if (bset)
1560                 isl_basic_set_free(bset);
1561         return NULL;
1562 }
1563
1564 void isl_set_free(struct isl_set *set)
1565 {
1566         int i;
1567
1568         if (!set)
1569                 return;
1570
1571         if (--set->ref > 0)
1572                 return;
1573
1574         isl_ctx_deref(set->ctx);
1575         for (i = 0; i < set->n; ++i)
1576                 isl_basic_set_free(set->p[i]);
1577         free(set);
1578 }
1579
1580 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1581 {
1582         int i;
1583
1584         if (!set) {
1585                 fprintf(out, "null set\n");
1586                 return;
1587         }
1588
1589         fprintf(out, "%*s", indent, "");
1590         fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1591                         set->ref, set->n, set->nparam, set->dim, set->flags);
1592         for (i = 0; i < set->n; ++i) {
1593                 fprintf(out, "%*s", indent, "");
1594                 fprintf(out, "basic set %d:\n", i);
1595                 isl_basic_set_dump(set->p[i], out, indent+4);
1596         }
1597 }
1598
1599 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1600 {
1601         int i;
1602
1603         if (!map) {
1604                 fprintf(out, "null map\n");
1605                 return;
1606         }
1607
1608         fprintf(out, "%*s", indent, "");
1609         fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, flags: %x\n",
1610                         map->ref, map->n, map->nparam, map->n_in, map->n_out,
1611                         map->flags);
1612         for (i = 0; i < map->n; ++i) {
1613                 fprintf(out, "%*s", indent, "");
1614                 fprintf(out, "basic map %d:\n", i);
1615                 isl_basic_map_dump(map->p[i], out, indent+4);
1616         }
1617 }
1618
1619 struct isl_basic_map *isl_basic_map_intersect_domain(
1620                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1621 {
1622         struct isl_basic_map *bmap_domain;
1623
1624         if (!bmap || !bset)
1625                 goto error;
1626
1627         isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
1628         isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
1629
1630         bmap = isl_basic_map_extend(bmap,
1631                         bset->nparam, bmap->n_in, bmap->n_out,
1632                         bset->n_div, bset->n_eq, bset->n_ineq);
1633         if (!bmap)
1634                 goto error;
1635         bmap_domain = isl_basic_map_from_basic_set(bset,
1636                                                 bset->dim, 0);
1637         bmap = add_constraints(bmap, bmap_domain, 0, 0);
1638
1639         bmap = isl_basic_map_simplify(bmap);
1640         return isl_basic_map_finalize(bmap);
1641 error:
1642         isl_basic_map_free(bmap);
1643         isl_basic_set_free(bset);
1644         return NULL;
1645 }
1646
1647 struct isl_basic_map *isl_basic_map_intersect_range(
1648                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1649 {
1650         struct isl_basic_map *bmap_range;
1651
1652         if (!bmap || !bset)
1653                 goto error;
1654
1655         isl_assert(bset->ctx, bset->dim == bmap->n_out, goto error);
1656         isl_assert(bset->ctx, bset->nparam == bmap->nparam, goto error);
1657
1658         bmap = isl_basic_map_extend(bmap,
1659                         bset->nparam, bmap->n_in, bmap->n_out,
1660                         bset->n_div, bset->n_eq, bset->n_ineq);
1661         if (!bmap)
1662                 goto error;
1663         bmap_range = isl_basic_map_from_basic_set(bset,
1664                                                 0, bset->dim);
1665         bmap = add_constraints(bmap, bmap_range, 0, 0);
1666
1667         bmap = isl_basic_map_simplify(bmap);
1668         return isl_basic_map_finalize(bmap);
1669 error:
1670         isl_basic_map_free(bmap);
1671         isl_basic_set_free(bset);
1672         return NULL;
1673 }
1674
1675 struct isl_basic_map *isl_basic_map_intersect(
1676                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1677 {
1678         if (!bmap1 || !bmap2)
1679                 goto error;
1680
1681         isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
1682         isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
1683         isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
1684
1685         bmap1 = isl_basic_map_extend(bmap1,
1686                         bmap1->nparam, bmap1->n_in, bmap1->n_out,
1687                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1688         if (!bmap1)
1689                 goto error;
1690         bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1691
1692         bmap1 = isl_basic_map_simplify(bmap1);
1693         return isl_basic_map_finalize(bmap1);
1694 error:
1695         isl_basic_map_free(bmap1);
1696         isl_basic_map_free(bmap2);
1697         return NULL;
1698 }
1699
1700 struct isl_basic_set *isl_basic_set_intersect(
1701                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1702 {
1703         return (struct isl_basic_set *)
1704                 isl_basic_map_intersect(
1705                         (struct isl_basic_map *)bset1,
1706                         (struct isl_basic_map *)bset2);
1707 }
1708
1709 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1710 {
1711         unsigned flags = 0;
1712         struct isl_map *result;
1713         int i, j;
1714
1715         if (!map1 || !map2)
1716                 goto error;
1717
1718         if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
1719             F_ISSET(map2, ISL_MAP_DISJOINT))
1720                 FL_SET(flags, ISL_MAP_DISJOINT);
1721
1722         result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
1723                                 map1->n * map2->n, flags);
1724         if (!result)
1725                 goto error;
1726         for (i = 0; i < map1->n; ++i)
1727                 for (j = 0; j < map2->n; ++j) {
1728                         struct isl_basic_map *part;
1729                         part = isl_basic_map_intersect(
1730                                     isl_basic_map_copy(map1->p[i]),
1731                                     isl_basic_map_copy(map2->p[j]));
1732                         if (isl_basic_map_is_empty(part))
1733                                 isl_basic_map_free(part);
1734                         else
1735                                 result = isl_map_add(result, part);
1736                         if (!result)
1737                                 goto error;
1738                 }
1739         isl_map_free(map1);
1740         isl_map_free(map2);
1741         return result;
1742 error:
1743         isl_map_free(map1);
1744         isl_map_free(map2);
1745         return NULL;
1746 }
1747
1748 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1749 {
1750         return (struct isl_set *)
1751                 isl_map_intersect((struct isl_map *)set1,
1752                                   (struct isl_map *)set2);
1753 }
1754
1755 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1756 {
1757         struct isl_basic_set *bset;
1758         unsigned in;
1759
1760         if (!bmap)
1761                 return NULL;
1762         bmap = isl_basic_map_cow(bmap);
1763         if (!bmap)
1764                 return NULL;
1765         in = bmap->n_in;
1766         bset = isl_basic_set_from_basic_map(bmap);
1767         bset = isl_basic_set_swap_vars(bset, in);
1768         return isl_basic_map_from_basic_set(bset, bset->dim-in, in);
1769 }
1770
1771 /* Turn final n dimensions into existentially quantified variables.
1772  */
1773 struct isl_basic_set *isl_basic_set_project_out(
1774                 struct isl_basic_set *bset, unsigned n, unsigned flags)
1775 {
1776         int i;
1777         size_t row_size;
1778         isl_int **new_div;
1779         isl_int *old;
1780
1781         if (!bset)
1782                 return NULL;
1783
1784         isl_assert(bset->ctx, n <= bset->dim, goto error);
1785
1786         if (n == 0)
1787                 return bset;
1788
1789         bset = isl_basic_set_cow(bset);
1790
1791         row_size = 1 + bset->nparam + bset->dim + bset->extra;
1792         old = bset->block2.data;
1793         bset->block2 = isl_blk_extend(bset->ctx, bset->block2,
1794                                         (bset->extra + n) * (1 + row_size));
1795         if (!bset->block2.data)
1796                 goto error;
1797         new_div = isl_alloc_array(ctx, isl_int *, bset->extra + n);
1798         if (!new_div)
1799                 goto error;
1800         for (i = 0; i < n; ++i) {
1801                 new_div[i] = bset->block2.data +
1802                                 (bset->extra + i) * (1 + row_size);
1803                 isl_seq_clr(new_div[i], 1 + row_size);
1804         }
1805         for (i = 0; i < bset->extra; ++i)
1806                 new_div[n + i] = bset->block2.data + (bset->div[i] - old);
1807         free(bset->div);
1808         bset->div = new_div;
1809         bset->n_div += n;
1810         bset->extra += n;
1811         bset->dim -= n;
1812         bset = isl_basic_set_simplify(bset);
1813         return isl_basic_set_finalize(bset);
1814 error:
1815         isl_basic_set_free(bset);
1816         return NULL;
1817 }
1818
1819 struct isl_basic_map *isl_basic_map_apply_range(
1820                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1821 {
1822         struct isl_basic_set *bset;
1823         unsigned n_in, n_out;
1824
1825         if (!bmap1 || !bmap2)
1826                 goto error;
1827
1828         isl_assert(bmap->ctx, bmap1->n_out == bmap2->n_in, goto error);
1829         isl_assert(bmap->ctx, bmap1->nparam == bmap2->nparam, goto error);
1830
1831         n_in = bmap1->n_in;
1832         n_out = bmap2->n_out;
1833
1834         bmap2 = isl_basic_map_reverse(bmap2);
1835         if (!bmap2)
1836                 goto error;
1837         bmap1 = isl_basic_map_extend(bmap1, bmap1->nparam,
1838                         bmap1->n_in + bmap2->n_in, bmap1->n_out,
1839                         bmap2->extra, bmap2->n_eq, bmap2->n_ineq);
1840         if (!bmap1)
1841                 goto error;
1842         bmap1 = add_constraints(bmap1, bmap2, bmap1->n_in - bmap2->n_in, 0);
1843         bmap1 = isl_basic_map_simplify(bmap1);
1844         bset = isl_basic_set_from_basic_map(bmap1);
1845         bset = isl_basic_set_project_out(bset,
1846                                                 bset->dim - (n_in + n_out), 0);
1847         return isl_basic_map_from_basic_set(bset, n_in, n_out);
1848 error:
1849         isl_basic_map_free(bmap1);
1850         isl_basic_map_free(bmap2);
1851         return NULL;
1852 }
1853
1854 struct isl_basic_set *isl_basic_set_apply(
1855                 struct isl_basic_set *bset, struct isl_basic_map *bmap)
1856 {
1857         if (!bset || !bmap)
1858                 goto error;
1859
1860         isl_assert(bset->ctx, bset->dim == bmap->n_in, goto error);
1861
1862         return (struct isl_basic_set *)
1863                 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
1864 error:
1865         isl_basic_set_free(bset);
1866         isl_basic_map_free(bmap);
1867         return NULL;
1868 }
1869
1870 struct isl_basic_map *isl_basic_map_apply_domain(
1871                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1872 {
1873         if (!bmap1 || !bmap2)
1874                 goto error;
1875
1876         isl_assert(ctx, bmap1->n_in == bmap2->n_in, goto error);
1877         isl_assert(ctx, bmap1->nparam == bmap2->nparam, goto error);
1878
1879         bmap1 = isl_basic_map_reverse(bmap1);
1880         bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
1881         return isl_basic_map_reverse(bmap1);
1882 error:
1883         isl_basic_map_free(bmap1);
1884         isl_basic_map_free(bmap2);
1885         return NULL;
1886 }
1887
1888 static struct isl_basic_map *var_equal(struct isl_ctx *ctx,
1889                 struct isl_basic_map *bmap, unsigned pos)
1890 {
1891         int i;
1892         i = isl_basic_map_alloc_equality(bmap);
1893         if (i < 0)
1894                 goto error;
1895         isl_seq_clr(bmap->eq[i],
1896                     1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
1897         isl_int_set_si(bmap->eq[i][1+bmap->nparam+pos], -1);
1898         isl_int_set_si(bmap->eq[i][1+bmap->nparam+bmap->n_in+pos], 1);
1899         return isl_basic_map_finalize(bmap);
1900 error:
1901         isl_basic_map_free(bmap);
1902         return NULL;
1903 }
1904
1905 static struct isl_basic_map *var_more(struct isl_ctx *ctx,
1906                 struct isl_basic_map *bmap, unsigned pos)
1907 {
1908         int i;
1909         i = isl_basic_map_alloc_inequality(bmap);
1910         if (i < 0)
1911                 goto error;
1912         isl_seq_clr(bmap->ineq[i],
1913                     1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
1914         isl_int_set_si(bmap->ineq[i][0], -1);
1915         isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], -1);
1916         isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], 1);
1917         return isl_basic_map_finalize(bmap);
1918 error:
1919         isl_basic_map_free(bmap);
1920         return NULL;
1921 }
1922
1923 static struct isl_basic_map *var_less(struct isl_ctx *ctx,
1924                 struct isl_basic_map *bmap, unsigned pos)
1925 {
1926         int i;
1927         i = isl_basic_map_alloc_inequality(bmap);
1928         if (i < 0)
1929                 goto error;
1930         isl_seq_clr(bmap->ineq[i],
1931                     1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
1932         isl_int_set_si(bmap->ineq[i][0], -1);
1933         isl_int_set_si(bmap->ineq[i][1+bmap->nparam+pos], 1);
1934         isl_int_set_si(bmap->ineq[i][1+bmap->nparam+bmap->n_in+pos], -1);
1935         return isl_basic_map_finalize(bmap);
1936 error:
1937         isl_basic_map_free(bmap);
1938         return NULL;
1939 }
1940
1941 struct isl_basic_map *isl_basic_map_equal(struct isl_ctx *ctx,
1942                 unsigned nparam, unsigned in, unsigned out, unsigned n_equal)
1943 {
1944         int i;
1945         struct isl_basic_map *bmap;
1946         bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, n_equal, 0);
1947         if (!bmap)
1948                 return NULL;
1949         for (i = 0; i < n_equal && bmap; ++i)
1950                 bmap = var_equal(ctx, bmap, i);
1951         return isl_basic_map_finalize(bmap);
1952 }
1953
1954 struct isl_basic_map *isl_basic_map_less_at(struct isl_ctx *ctx,
1955                 unsigned nparam, unsigned in, unsigned out, unsigned pos)
1956 {
1957         int i;
1958         struct isl_basic_map *bmap;
1959         bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
1960         if (!bmap)
1961                 return NULL;
1962         for (i = 0; i < pos && bmap; ++i)
1963                 bmap = var_equal(ctx, bmap, i);
1964         if (bmap)
1965                 bmap = var_less(ctx, bmap, pos);
1966         return isl_basic_map_finalize(bmap);
1967 }
1968
1969 struct isl_basic_map *isl_basic_map_more_at(struct isl_ctx *ctx,
1970                 unsigned nparam, unsigned in, unsigned out, unsigned pos)
1971 {
1972         int i;
1973         struct isl_basic_map *bmap;
1974         bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, pos, 1);
1975         if (!bmap)
1976                 return NULL;
1977         for (i = 0; i < pos && bmap; ++i)
1978                 bmap = var_equal(ctx, bmap, i);
1979         if (bmap)
1980                 bmap = var_more(ctx, bmap, pos);
1981         return isl_basic_map_finalize(bmap);
1982 }
1983
1984 struct isl_basic_map *isl_basic_map_from_basic_set(
1985                 struct isl_basic_set *bset, unsigned n_in, unsigned n_out)
1986 {
1987         struct isl_basic_map *bmap;
1988
1989         bset = isl_basic_set_cow(bset);
1990         if (!bset)
1991                 return NULL;
1992
1993         isl_assert(ctx, bset->dim == n_in + n_out, goto error);
1994         bmap = (struct isl_basic_map *) bset;
1995         bmap->n_in = n_in;
1996         bmap->n_out = n_out;
1997         return isl_basic_map_finalize(bmap);
1998 error:
1999         isl_basic_set_free(bset);
2000         return NULL;
2001 }
2002
2003 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2004 {
2005         if (!bmap)
2006                 goto error;
2007         if (bmap->n_in == 0)
2008                 return (struct isl_basic_set *)bmap;
2009         bmap = isl_basic_map_cow(bmap);
2010         if (!bmap)
2011                 goto error;
2012         bmap->n_out += bmap->n_in;
2013         bmap->n_in = 0;
2014         bmap = isl_basic_map_finalize(bmap);
2015         return (struct isl_basic_set *)bmap;
2016 error:
2017         return NULL;
2018 }
2019
2020 /* For a div d = floor(f/m), add the constraints
2021  *
2022  *              f - m d >= 0
2023  *              -(f-(n-1)) + m d >= 0
2024  *
2025  * Note that the second constraint is the negation of
2026  *
2027  *              f - m d >= n
2028  */
2029 static int add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2030 {
2031         int i, j;
2032         unsigned div_pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
2033         unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra;
2034
2035         i = isl_basic_map_alloc_inequality(bmap);
2036         if (i < 0)
2037                 return -1;
2038         isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2039         isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2040
2041         j = isl_basic_map_alloc_inequality(bmap);
2042         if (j < 0)
2043                 return -1;
2044         isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2045         isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2046         isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2047         return j;
2048 }
2049
2050 struct isl_basic_set *isl_basic_map_underlying_set(
2051                 struct isl_basic_map *bmap)
2052 {
2053         if (!bmap)
2054                 goto error;
2055         if (bmap->nparam == 0 && bmap->n_in == 0 && bmap->n_div == 0)
2056                 return (struct isl_basic_set *)bmap;
2057         bmap = isl_basic_map_cow(bmap);
2058         if (!bmap)
2059                 goto error;
2060         bmap->n_out += bmap->nparam + bmap->n_in + bmap->n_div;
2061         bmap->nparam = 0;
2062         bmap->n_in = 0;
2063         bmap->extra -= bmap->n_div;
2064         bmap->n_div = 0;
2065         bmap = isl_basic_map_finalize(bmap);
2066         return (struct isl_basic_set *)bmap;
2067 error:
2068         return NULL;
2069 }
2070
2071 struct isl_basic_map *isl_basic_map_overlying_set(
2072         struct isl_basic_set *bset, struct isl_basic_map *like)
2073 {
2074         struct isl_basic_map *bmap;
2075         struct isl_ctx *ctx;
2076         unsigned total;
2077         int i, k;
2078
2079         if (!bset || !like)
2080                 goto error;
2081         ctx = bset->ctx;
2082         isl_assert(ctx, bset->dim ==
2083                         like->nparam + like->n_in + like->n_out + like->n_div,
2084                         goto error);
2085         if (like->nparam == 0 && like->n_in == 0 && like->n_div == 0) {
2086                 isl_basic_map_free(like);
2087                 return (struct isl_basic_map *)bset;
2088         }
2089         bset = isl_basic_set_cow(bset);
2090         if (!bset)
2091                 goto error;
2092         total = bset->dim + bset->extra;
2093         bmap = (struct isl_basic_map *)bset;
2094         bmap->nparam = like->nparam;
2095         bmap->n_in = like->n_in;
2096         bmap->n_out = like->n_out;
2097         bmap->extra += like->n_div;
2098         if (bmap->extra) {
2099                 unsigned ltotal;
2100                 ltotal = total - bmap->extra + like->extra;
2101                 if (ltotal > total)
2102                         ltotal = total;
2103                 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2104                                         bmap->extra * (1 + 1 + total));
2105                 if (isl_blk_is_error(bmap->block2))
2106                         goto error;
2107                 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2108                                                 bmap->extra);
2109                 if (!bmap->div)
2110                         goto error;
2111                 bmap = isl_basic_map_extend(bmap, bmap->nparam,
2112                         bmap->n_in, bmap->n_out, 0, 0, 2 * like->n_div);
2113                 for (i = 0; i < like->n_div; ++i) {
2114                         k = isl_basic_map_alloc_div(bmap);
2115                         if (k < 0)
2116                                 goto error;
2117                         isl_seq_cpy(bmap->div[k], like->div[i], 1 + 1 + ltotal);
2118                         isl_seq_clr(bmap->div[k]+1+1+ltotal, total - ltotal);
2119                         if (add_div_constraints(bmap, k) < 0)
2120                                 goto error;
2121                 }
2122         }
2123         isl_basic_map_free(like);
2124         bmap = isl_basic_map_finalize(bmap);
2125         return bmap;
2126 error:
2127         isl_basic_map_free(like);
2128         isl_basic_set_free(bset);
2129         return NULL;
2130 }
2131
2132 struct isl_basic_set *isl_basic_set_from_underlying_set(
2133         struct isl_basic_set *bset, struct isl_basic_set *like)
2134 {
2135         return (struct isl_basic_set *)
2136                 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2137 }
2138
2139 struct isl_set *isl_set_from_underlying_set(
2140         struct isl_set *set, struct isl_basic_set *like)
2141 {
2142         int i;
2143
2144         if (!set || !like)
2145                 goto error;
2146         isl_assert(set->ctx, set->dim == like->nparam + like->dim + like->n_div,
2147                     goto error);
2148         if (like->nparam == 0 && like->n_div == 0) {
2149                 isl_basic_set_free(like);
2150                 return set;
2151         }
2152         set = isl_set_cow(set);
2153         if (!set)
2154                 goto error;
2155         for (i = 0; i < set->n; ++i) {
2156                 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2157                                                       isl_basic_set_copy(like));
2158                 if (!set->p[i])
2159                         goto error;
2160         }
2161         set->nparam = like->nparam;
2162         set->dim = like->dim;
2163         isl_basic_set_free(like);
2164         return set;
2165 error:
2166         isl_basic_set_free(like);
2167         isl_set_free(set);
2168         return NULL;
2169 }
2170
2171 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2172 {
2173         int i;
2174
2175         map = isl_map_align_divs(map);
2176         map = isl_map_cow(map);
2177         if (!map)
2178                 return NULL;
2179
2180         for (i = 0; i < map->n; ++i) {
2181                 map->p[i] = (struct isl_basic_map *)
2182                                 isl_basic_map_underlying_set(map->p[i]);
2183                 if (!map->p[i])
2184                         goto error;
2185         }
2186         if (map->n == 0)
2187                 map->n_out += map->nparam + map->n_in;
2188         else
2189                 map->n_out = map->p[0]->n_out;
2190         map->nparam = 0;
2191         map->n_in = 0;
2192         return (struct isl_set *)map;
2193 error:
2194         isl_map_free(map);
2195         return NULL;
2196 }
2197
2198 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2199 {
2200         return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2201 }
2202
2203 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2204 {
2205         struct isl_basic_set *domain;
2206         unsigned n_out;
2207         if (!bmap)
2208                 return NULL;
2209         n_out = bmap->n_out;
2210         domain = isl_basic_set_from_basic_map(bmap);
2211         return isl_basic_set_project_out(domain, n_out, 0);
2212 }
2213
2214 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2215 {
2216         return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2217 }
2218
2219 struct isl_set *isl_map_range(struct isl_map *map)
2220 {
2221         int i;
2222         struct isl_set *set;
2223
2224         if (!map)
2225                 goto error;
2226         map = isl_map_cow(map);
2227         if (!map)
2228                 goto error;
2229
2230         set = (struct isl_set *) map;
2231         set->zero = 0;
2232         for (i = 0; i < map->n; ++i) {
2233                 set->p[i] = isl_basic_map_range(map->p[i]);
2234                 if (!set->p[i])
2235                         goto error;
2236         }
2237         F_CLR(set, ISL_MAP_DISJOINT);
2238         return set;
2239 error:
2240         isl_map_free(map);
2241         return NULL;
2242 }
2243
2244 struct isl_map *isl_map_from_set(struct isl_set *set,
2245                 unsigned n_in, unsigned n_out)
2246 {
2247         int i;
2248         struct isl_map *map = NULL;
2249
2250         if (!set)
2251                 return NULL;
2252         isl_assert(set->ctx, set->dim == n_in + n_out, goto error);
2253         set = isl_set_cow(set);
2254         if (!set)
2255                 return NULL;
2256         map = (struct isl_map *)set;
2257         for (i = 0; i < set->n; ++i) {
2258                 map->p[i] = isl_basic_map_from_basic_set(
2259                                 set->p[i], n_in, n_out);
2260                 if (!map->p[i])
2261                         goto error;
2262         }
2263         map->n_in = n_in;
2264         map->n_out = n_out;
2265         return map;
2266 error:
2267         isl_set_free(set);
2268         return NULL;
2269 }
2270
2271 struct isl_set *isl_set_from_map(struct isl_map *map)
2272 {
2273         int i;
2274         struct isl_set *set = NULL;
2275
2276         if (!map)
2277                 return NULL;
2278         map = isl_map_cow(map);
2279         if (!map)
2280                 return NULL;
2281         map->n_out += map->n_in;
2282         map->n_in = 0;
2283         set = (struct isl_set *)map;
2284         for (i = 0; i < map->n; ++i) {
2285                 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
2286                 if (!set->p[i])
2287                         goto error;
2288         }
2289         return set;
2290 error:
2291         isl_map_free(map);
2292         return NULL;
2293 }
2294
2295 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
2296                 unsigned nparam, unsigned in, unsigned out, int n,
2297                 unsigned flags)
2298 {
2299         struct isl_map *map;
2300
2301         map = isl_alloc(ctx, struct isl_map,
2302                         sizeof(struct isl_map) +
2303                         n * sizeof(struct isl_basic_map *));
2304         if (!map)
2305                 return NULL;
2306
2307         map->ctx = ctx;
2308         isl_ctx_ref(ctx);
2309         map->ref = 1;
2310         map->size = n;
2311         map->n = 0;
2312         map->nparam = nparam;
2313         map->n_in = in;
2314         map->n_out = out;
2315         map->flags = flags;
2316         return map;
2317 }
2318
2319 struct isl_basic_map *isl_basic_map_empty(struct isl_ctx *ctx,
2320                 unsigned nparam, unsigned in, unsigned out)
2321 {
2322         struct isl_basic_map *bmap;
2323         bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 1, 0);
2324         bmap = isl_basic_map_set_to_empty(bmap);
2325         return bmap;
2326 }
2327
2328 struct isl_basic_set *isl_basic_set_empty(struct isl_ctx *ctx,
2329                 unsigned nparam, unsigned dim)
2330 {
2331         struct isl_basic_set *bset;
2332         bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 1, 0);
2333         bset = isl_basic_set_set_to_empty(bset);
2334         return bset;
2335 }
2336
2337 struct isl_basic_map *isl_basic_map_universe(struct isl_ctx *ctx,
2338                 unsigned nparam, unsigned in, unsigned out)
2339 {
2340         struct isl_basic_map *bmap;
2341         bmap = isl_basic_map_alloc(ctx, nparam, in, out, 0, 0, 0);
2342         return bmap;
2343 }
2344
2345 struct isl_basic_set *isl_basic_set_universe(struct isl_ctx *ctx,
2346                 unsigned nparam, unsigned dim)
2347 {
2348         struct isl_basic_set *bset;
2349         bset = isl_basic_set_alloc(ctx, nparam, dim, 0, 0, 0);
2350         return bset;
2351 }
2352
2353 struct isl_map *isl_map_empty(struct isl_ctx *ctx,
2354                 unsigned nparam, unsigned in, unsigned out)
2355 {
2356         return isl_map_alloc(ctx, nparam, in, out, 0, ISL_MAP_DISJOINT);
2357 }
2358
2359 struct isl_set *isl_set_empty(struct isl_ctx *ctx,
2360                 unsigned nparam, unsigned dim)
2361 {
2362         return isl_set_alloc(ctx, nparam, dim, 0, ISL_MAP_DISJOINT);
2363 }
2364
2365 struct isl_map *isl_map_dup(struct isl_map *map)
2366 {
2367         int i;
2368         struct isl_map *dup;
2369
2370         if (!map)
2371                 return NULL;
2372         dup = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out, map->n,
2373                                 map->flags);
2374         for (i = 0; i < map->n; ++i)
2375                 dup = isl_map_add(dup,
2376                                   isl_basic_map_dup(map->p[i]));
2377         return dup;
2378 }
2379
2380 struct isl_map *isl_map_add(struct isl_map *map, struct isl_basic_map *bmap)
2381 {
2382         if (!bmap || !map)
2383                 goto error;
2384         isl_assert(map->ctx, map->nparam == bmap->nparam, goto error);
2385         isl_assert(map->ctx, map->n_in == bmap->n_in, goto error);
2386         isl_assert(map->ctx, map->n_out == bmap->n_out, goto error);
2387         isl_assert(map->ctx, map->n < map->size, goto error);
2388         map->p[map->n] = bmap;
2389         map->n++;
2390         return map;
2391 error:
2392         if (map)
2393                 isl_map_free(map);
2394         if (bmap)
2395                 isl_basic_map_free(bmap);
2396         return NULL;
2397 }
2398
2399 void isl_map_free(struct isl_map *map)
2400 {
2401         int i;
2402
2403         if (!map)
2404                 return;
2405
2406         if (--map->ref > 0)
2407                 return;
2408
2409         isl_ctx_deref(map->ctx);
2410         for (i = 0; i < map->n; ++i)
2411                 isl_basic_map_free(map->p[i]);
2412         free(map);
2413 }
2414
2415 struct isl_map *isl_map_extend(struct isl_map *base,
2416                 unsigned nparam, unsigned n_in, unsigned n_out)
2417 {
2418         int i;
2419
2420         base = isl_map_cow(base);
2421         if (!base)
2422                 return NULL;
2423
2424         isl_assert(base->ctx, base->nparam <= nparam, goto error);
2425         isl_assert(base->ctx, base->n_in <= n_in, goto error);
2426         isl_assert(base->ctx, base->n_out <= n_out, goto error);
2427         base->nparam = nparam;
2428         base->n_in = n_in;
2429         base->n_out = n_out;
2430         for (i = 0; i < base->n; ++i) {
2431                 base->p[i] = isl_basic_map_extend(base->p[i],
2432                                 nparam, n_in, n_out, 0, 0, 0);
2433                 if (!base->p[i])
2434                         goto error;
2435         }
2436         return base;
2437 error:
2438         isl_map_free(base);
2439         return NULL;
2440 }
2441
2442 struct isl_set *isl_set_extend(struct isl_set *base,
2443                 unsigned nparam, unsigned dim)
2444 {
2445         return (struct isl_set *)isl_map_extend((struct isl_map *)base,
2446                                                         nparam, 0, dim);
2447 }
2448
2449 static struct isl_basic_map *isl_basic_map_fix_var(struct isl_basic_map *bmap,
2450                 unsigned var, int value)
2451 {
2452         int j;
2453
2454         bmap = isl_basic_map_cow(bmap);
2455         bmap = isl_basic_map_extend(bmap,
2456                         bmap->nparam, bmap->n_in, bmap->n_out, 0, 1, 0);
2457         j = isl_basic_map_alloc_equality(bmap);
2458         if (j < 0)
2459                 goto error;
2460         isl_seq_clr(bmap->eq[j],
2461                     1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
2462         isl_int_set_si(bmap->eq[j][1 + var], -1);
2463         isl_int_set_si(bmap->eq[j][0], value);
2464         bmap = isl_basic_map_simplify(bmap);
2465         return isl_basic_map_finalize(bmap);
2466 error:
2467         isl_basic_map_free(bmap);
2468         return NULL;
2469 }
2470
2471 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
2472                 unsigned input, int value)
2473 {
2474         if (!bmap)
2475                 return NULL;
2476         isl_assert(bmap->ctx, input < bmap->n_in, goto error);
2477         return isl_basic_map_fix_var(bmap, bmap->nparam + input, value);
2478 error:
2479         isl_basic_map_free(bmap);
2480         return NULL;
2481 }
2482
2483 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
2484                 unsigned dim, int value)
2485 {
2486         if (!bset)
2487                 return NULL;
2488         isl_assert(bset->ctx, dim < bset->dim, goto error);
2489         return (struct isl_basic_set *)
2490                 isl_basic_map_fix_var((struct isl_basic_map *)bset,
2491                                                 bset->nparam + dim, value);
2492 error:
2493         isl_basic_set_free(bset);
2494         return NULL;
2495 }
2496
2497 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
2498                 unsigned input, int value)
2499 {
2500         int i;
2501
2502         map = isl_map_cow(map);
2503         if (!map)
2504                 return NULL;
2505
2506         isl_assert(ctx, input < map->n_in, goto error);
2507         for (i = 0; i < map->n; ++i) {
2508                 map->p[i] = isl_basic_map_fix_input_si(map->p[i], input, value);
2509                 if (!map->p[i])
2510                         goto error;
2511         }
2512         return map;
2513 error:
2514         isl_map_free(map);
2515         return NULL;
2516 }
2517
2518 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
2519 {
2520         int i;
2521
2522         set = isl_set_cow(set);
2523         if (!set)
2524                 return NULL;
2525
2526         isl_assert(set->ctx, dim < set->dim, goto error);
2527         for (i = 0; i < set->n; ++i) {
2528                 set->p[i] = isl_basic_set_fix_dim_si(set->p[i], dim, value);
2529                 if (!set->p[i])
2530                         goto error;
2531         }
2532         return set;
2533 error:
2534         isl_set_free(set);
2535         return NULL;
2536 }
2537
2538 struct isl_map *isl_map_reverse(struct isl_map *map)
2539 {
2540         int i;
2541         unsigned t;
2542
2543         map = isl_map_cow(map);
2544         if (!map)
2545                 return NULL;
2546
2547         t = map->n_in;
2548         map->n_in = map->n_out;
2549         map->n_out = t;
2550         for (i = 0; i < map->n; ++i) {
2551                 map->p[i] = isl_basic_map_reverse(map->p[i]);
2552                 if (!map->p[i])
2553                         goto error;
2554         }
2555         return map;
2556 error:
2557         isl_map_free(map);
2558         return NULL;
2559 }
2560
2561 struct isl_map *isl_basic_map_lexmax(
2562                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2563                 struct isl_set **empty)
2564 {
2565         return isl_pip_basic_map_lexmax(bmap, dom, empty);
2566 }
2567
2568 struct isl_map *isl_basic_map_lexmin(
2569                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
2570                 struct isl_set **empty)
2571 {
2572         return isl_pip_basic_map_lexmin(bmap, dom, empty);
2573 }
2574
2575 struct isl_set *isl_basic_set_lexmin(struct isl_basic_set *bset)
2576 {
2577         struct isl_basic_map *bmap = NULL;
2578         struct isl_basic_set *dom = NULL;
2579         struct isl_map *min;
2580
2581         if (!bset)
2582                 goto error;
2583         bmap = isl_basic_map_from_basic_set(bset, 0, bset->dim);
2584         if (!bmap)
2585                 goto error;
2586         dom = isl_basic_set_alloc(bmap->ctx, bmap->nparam, 0, 0, 0, 0);
2587         if (!dom)
2588                 goto error;
2589         min = isl_basic_map_lexmin(bmap, dom, NULL);
2590         return isl_map_range(min);
2591 error:
2592         isl_basic_map_free(bmap);
2593         return NULL;
2594 }
2595
2596 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
2597 {
2598         if (!bmap)
2599                 return NULL;
2600         if (bmap->n_div == 0)
2601                 return isl_map_from_basic_map(bmap);
2602         return isl_pip_basic_map_compute_divs(bmap);
2603 }
2604
2605 struct isl_map *isl_map_compute_divs(struct isl_map *map)
2606 {
2607         int i;
2608         struct isl_map *res;
2609
2610         if (!map)
2611                 return NULL;
2612         if (map->n == 0)
2613                 return map;
2614         res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
2615         for (i = 1 ; i < map->n; ++i) {
2616                 struct isl_map *r2;
2617                 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
2618                 if (F_ISSET(map, ISL_MAP_DISJOINT))
2619                         res = isl_map_union_disjoint(res, r2);
2620                 else
2621                         res = isl_map_union(res, r2);
2622         }
2623         isl_map_free(map);
2624
2625         return res;
2626 }
2627
2628 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
2629 {
2630         return (struct isl_set *)
2631                 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
2632 }
2633
2634 struct isl_set *isl_set_compute_divs(struct isl_set *set)
2635 {
2636         return (struct isl_set *)
2637                 isl_map_compute_divs((struct isl_map *)set);
2638 }
2639
2640 struct isl_set *isl_map_domain(struct isl_map *map)
2641 {
2642         int i;
2643         struct isl_set *set;
2644
2645         if (!map)
2646                 goto error;
2647
2648         map = isl_map_cow(map);
2649         if (!map)
2650                 return NULL;
2651
2652         set = (struct isl_set *)map;
2653         set->dim = map->n_in;
2654         set->zero = 0;
2655         for (i = 0; i < map->n; ++i) {
2656                 set->p[i] = isl_basic_map_domain(map->p[i]);
2657                 if (!set->p[i])
2658                         goto error;
2659         }
2660         F_CLR(set, ISL_MAP_DISJOINT);
2661         return set;
2662 error:
2663         isl_map_free(map);
2664         return NULL;
2665 }
2666
2667 struct isl_map *isl_map_union_disjoint(
2668                         struct isl_map *map1, struct isl_map *map2)
2669 {
2670         int i;
2671         unsigned flags = 0;
2672         struct isl_map *map = NULL;
2673
2674         if (!map1 || !map2)
2675                 goto error;
2676
2677         if (map1->n == 0) {
2678                 isl_map_free(map1);
2679                 return map2;
2680         }
2681         if (map2->n == 0) {
2682                 isl_map_free(map2);
2683                 return map1;
2684         }
2685
2686         isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
2687         isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
2688         isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
2689
2690         if (F_ISSET(map1, ISL_MAP_DISJOINT) &&
2691             F_ISSET(map2, ISL_MAP_DISJOINT))
2692                 FL_SET(flags, ISL_MAP_DISJOINT);
2693
2694         map = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map1->n_out,
2695                                 map1->n + map2->n, flags);
2696         if (!map)
2697                 goto error;
2698         for (i = 0; i < map1->n; ++i) {
2699                 map = isl_map_add(map,
2700                                   isl_basic_map_copy(map1->p[i]));
2701                 if (!map)
2702                         goto error;
2703         }
2704         for (i = 0; i < map2->n; ++i) {
2705                 map = isl_map_add(map,
2706                                   isl_basic_map_copy(map2->p[i]));
2707                 if (!map)
2708                         goto error;
2709         }
2710         isl_map_free(map1);
2711         isl_map_free(map2);
2712         return map;
2713 error:
2714         isl_map_free(map);
2715         isl_map_free(map1);
2716         isl_map_free(map2);
2717         return NULL;
2718 }
2719
2720 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
2721 {
2722         map1 = isl_map_union_disjoint(map1, map2);
2723         if (!map1)
2724                 return NULL;
2725         if (map1->n > 1)
2726                 F_CLR(map1, ISL_MAP_DISJOINT);
2727         return map1;
2728 }
2729
2730 struct isl_set *isl_set_union_disjoint(
2731                         struct isl_set *set1, struct isl_set *set2)
2732 {
2733         return (struct isl_set *)
2734                 isl_map_union_disjoint(
2735                         (struct isl_map *)set1, (struct isl_map *)set2);
2736 }
2737
2738 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
2739 {
2740         return (struct isl_set *)
2741                 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
2742 }
2743
2744 struct isl_map *isl_map_intersect_range(
2745                 struct isl_map *map, struct isl_set *set)
2746 {
2747         unsigned flags = 0;
2748         struct isl_map *result;
2749         int i, j;
2750
2751         if (!map || !set)
2752                 goto error;
2753
2754         if (F_ISSET(map, ISL_MAP_DISJOINT) &&
2755             F_ISSET(set, ISL_MAP_DISJOINT))
2756                 FL_SET(flags, ISL_MAP_DISJOINT);
2757
2758         result = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
2759                                 map->n * set->n, flags);
2760         if (!result)
2761                 goto error;
2762         for (i = 0; i < map->n; ++i)
2763                 for (j = 0; j < set->n; ++j) {
2764                         result = isl_map_add(result,
2765                             isl_basic_map_intersect_range(
2766                                 isl_basic_map_copy(map->p[i]),
2767                                 isl_basic_set_copy(set->p[j])));
2768                         if (!result)
2769                                 goto error;
2770                 }
2771         isl_map_free(map);
2772         isl_set_free(set);
2773         return result;
2774 error:
2775         isl_map_free(map);
2776         isl_set_free(set);
2777         return NULL;
2778 }
2779
2780 struct isl_map *isl_map_intersect_domain(
2781                 struct isl_map *map, struct isl_set *set)
2782 {
2783         return isl_map_reverse(
2784                 isl_map_intersect_range(isl_map_reverse(map), set));
2785 }
2786
2787 struct isl_map *isl_map_apply_domain(
2788                 struct isl_map *map1, struct isl_map *map2)
2789 {
2790         if (!map1 || !map2)
2791                 goto error;
2792         map1 = isl_map_reverse(map1);
2793         map1 = isl_map_apply_range(map1, map2);
2794         return isl_map_reverse(map1);
2795 error:
2796         isl_map_free(map1);
2797         isl_map_free(map2);
2798         return NULL;
2799 }
2800
2801 struct isl_map *isl_map_apply_range(
2802                 struct isl_map *map1, struct isl_map *map2)
2803 {
2804         struct isl_map *result;
2805         int i, j;
2806
2807         if (!map1 || !map2)
2808                 goto error;
2809
2810         isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
2811         isl_assert(map1->ctx, map1->n_out == map2->n_in, goto error);
2812
2813         result = isl_map_alloc(map1->ctx, map1->nparam, map1->n_in, map2->n_out,
2814                                 map1->n * map2->n, 0);
2815         if (!result)
2816                 goto error;
2817         for (i = 0; i < map1->n; ++i)
2818                 for (j = 0; j < map2->n; ++j) {
2819                         result = isl_map_add(result,
2820                             isl_basic_map_apply_range(
2821                                 isl_basic_map_copy(map1->p[i]),
2822                                 isl_basic_map_copy(map2->p[j])));
2823                         if (!result)
2824                                 goto error;
2825                 }
2826         isl_map_free(map1);
2827         isl_map_free(map2);
2828         if (result && result->n <= 1)
2829                 F_SET(result, ISL_MAP_DISJOINT);
2830         return result;
2831 error:
2832         isl_map_free(map1);
2833         isl_map_free(map2);
2834         return NULL;
2835 }
2836
2837 /*
2838  * returns range - domain
2839  */
2840 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
2841 {
2842         struct isl_basic_set *bset;
2843         unsigned dim;
2844         int i;
2845
2846         if (!bmap)
2847                 goto error;
2848         isl_assert(bmap->ctx, bmap->n_in == bmap->n_out, goto error);
2849         dim = bmap->n_in;
2850         bset = isl_basic_set_from_basic_map(bmap);
2851         bset = isl_basic_set_extend(bset, bset->nparam, 3*dim, 0,
2852                                         dim, 0);
2853         bset = isl_basic_set_swap_vars(bset, 2*dim);
2854         for (i = 0; i < dim; ++i) {
2855                 int j = isl_basic_map_alloc_equality(
2856                                             (struct isl_basic_map *)bset);
2857                 if (j < 0)
2858                         goto error;
2859                 isl_seq_clr(bset->eq[j],
2860                             1 + bset->nparam + bset->dim + bset->extra);
2861                 isl_int_set_si(bset->eq[j][1+bset->nparam+i], 1);
2862                 isl_int_set_si(bset->eq[j][1+bset->nparam+dim+i], 1);
2863                 isl_int_set_si(bset->eq[j][1+bset->nparam+2*dim+i], -1);
2864         }
2865         return isl_basic_set_project_out(bset, 2*dim, 0);
2866 error:
2867         isl_basic_map_free(bmap);
2868         return NULL;
2869 }
2870
2871 /* If the only constraints a div d=floor(f/m)
2872  * appears in are its two defining constraints
2873  *
2874  *      f - m d >=0
2875  *      -(f - (m - 1)) + m d >= 0
2876  *
2877  * then it can safely be removed.
2878  */
2879 static int div_is_redundant(struct isl_basic_map *bmap, int div)
2880 {
2881         int i;
2882         unsigned pos = 1 + bmap->nparam + bmap->n_in + bmap->n_out + div;
2883
2884         for (i = 0; i < bmap->n_eq; ++i)
2885                 if (!isl_int_is_zero(bmap->eq[i][pos]))
2886                         return 0;
2887
2888         for (i = 0; i < bmap->n_ineq; ++i) {
2889                 if (isl_int_is_zero(bmap->ineq[i][pos]))
2890                         continue;
2891                 if (isl_int_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
2892                         int neg;
2893                         isl_int_sub(bmap->div[div][1],
2894                                         bmap->div[div][1], bmap->div[div][0]);
2895                         isl_int_add_ui(bmap->div[div][1], bmap->div[div][1], 1);
2896                         neg = isl_seq_is_neg(bmap->ineq[i], bmap->div[div]+1, pos);
2897                         isl_int_sub_ui(bmap->div[div][1], bmap->div[div][1], 1);
2898                         isl_int_add(bmap->div[div][1],
2899                                         bmap->div[div][1], bmap->div[div][0]);
2900                         if (!neg)
2901                                 return 0;
2902                         if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
2903                                                     bmap->n_div-div-1) != -1)
2904                                 return 0;
2905                 } else if (isl_int_abs_eq(bmap->ineq[i][pos], bmap->div[div][0])) {
2906                         if (!isl_seq_eq(bmap->ineq[i], bmap->div[div]+1, pos))
2907                                 return 0;
2908                         if (isl_seq_first_non_zero(bmap->ineq[i]+pos+1,
2909                                                     bmap->n_div-div-1) != -1)
2910                                 return 0;
2911                 } else
2912                         return 0;
2913         }
2914
2915         for (i = 0; i < bmap->n_div; ++i)
2916                 if (!isl_int_is_zero(bmap->div[i][1+pos]))
2917                         return 0;
2918
2919         return 1;
2920 }
2921
2922 /*
2923  * Remove divs that don't occur in any of the constraints or other divs.
2924  * These can arise when dropping some of the variables in a quast
2925  * returned by piplib.
2926  */
2927 static struct isl_basic_map *remove_redundant_divs(struct isl_basic_map *bmap)
2928 {
2929         int i;
2930
2931         if (!bmap)
2932                 return NULL;
2933
2934         for (i = bmap->n_div-1; i >= 0; --i) {
2935                 if (!div_is_redundant(bmap, i))
2936                         continue;
2937                 bmap = isl_basic_map_drop_div(bmap, i);
2938         }
2939         return bmap;
2940 }
2941
2942 struct isl_basic_map *isl_basic_map_finalize(struct isl_basic_map *bmap)
2943 {
2944         bmap = remove_redundant_divs(bmap);
2945         if (!bmap)
2946                 return NULL;
2947         F_SET(bmap, ISL_BASIC_SET_FINAL);
2948         return bmap;
2949 }
2950
2951 struct isl_basic_set *isl_basic_set_finalize(struct isl_basic_set *bset)
2952 {
2953         return (struct isl_basic_set *)
2954                 isl_basic_map_finalize((struct isl_basic_map *)bset);
2955 }
2956
2957 struct isl_set *isl_set_finalize(struct isl_set *set)
2958 {
2959         int i;
2960
2961         if (!set)
2962                 return NULL;
2963         for (i = 0; i < set->n; ++i) {
2964                 set->p[i] = isl_basic_set_finalize(set->p[i]);
2965                 if (!set->p[i])
2966                         goto error;
2967         }
2968         return set;
2969 error:
2970         isl_set_free(set);
2971         return NULL;
2972 }
2973
2974 struct isl_map *isl_map_finalize(struct isl_map *map)
2975 {
2976         int i;
2977
2978         if (!map)
2979                 return NULL;
2980         for (i = 0; i < map->n; ++i) {
2981                 map->p[i] = isl_basic_map_finalize(map->p[i]);
2982                 if (!map->p[i])
2983                         goto error;
2984         }
2985         return map;
2986 error:
2987         isl_map_free(map);
2988         return NULL;
2989 }
2990
2991 struct isl_basic_map *isl_basic_map_identity(struct isl_ctx *ctx,
2992                 unsigned nparam, unsigned dim)
2993 {
2994         struct isl_basic_map *bmap;
2995         int i;
2996
2997         bmap = isl_basic_map_alloc(ctx, nparam, dim, dim, 0, dim, 0);
2998         if (!bmap)
2999                 goto error;
3000
3001         for (i = 0; i < dim; ++i) {
3002                 int j = isl_basic_map_alloc_equality(bmap);
3003                 if (j < 0)
3004                         goto error;
3005                 isl_seq_clr(bmap->eq[j],
3006                     1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->extra);
3007                 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
3008                 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
3009         }
3010         return isl_basic_map_finalize(bmap);
3011 error:
3012         isl_basic_map_free(bmap);
3013         return NULL;
3014 }
3015
3016 struct isl_map *isl_map_identity(struct isl_ctx *ctx,
3017                 unsigned nparam, unsigned dim)
3018 {
3019         struct isl_map *map = isl_map_alloc(ctx, nparam, dim, dim, 1,
3020                                                 ISL_MAP_DISJOINT);
3021         if (!map)
3022                 goto error;
3023         map = isl_map_add(map,
3024                 isl_basic_map_identity(ctx, nparam, dim));
3025         return map;
3026 error:
3027         isl_map_free(map);
3028         return NULL;
3029 }
3030
3031 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
3032 {
3033         return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
3034 }
3035
3036 int isl_set_is_subset(struct isl_set *set1, struct isl_set *set2)
3037 {
3038         return isl_map_is_subset(
3039                         (struct isl_map *)set1, (struct isl_map *)set2);
3040 }
3041
3042 int isl_basic_map_is_subset(
3043                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3044 {
3045         int is_subset;
3046         struct isl_map *map1;
3047         struct isl_map *map2;
3048
3049         if (!bmap1 || !bmap2)
3050                 return -1;
3051
3052         map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
3053         map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
3054
3055         is_subset = isl_map_is_subset(map1, map2);
3056
3057         isl_map_free(map1);
3058         isl_map_free(map2);
3059
3060         return is_subset;
3061 }
3062
3063 int isl_basic_map_is_equal(
3064                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3065 {
3066         int is_subset;
3067
3068         if (!bmap1 || !bmap2)
3069                 return -1;
3070         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3071         if (is_subset != 1)
3072                 return is_subset;
3073         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3074         return is_subset;
3075 }
3076
3077 int isl_basic_set_is_equal(
3078                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3079 {
3080         return isl_basic_map_is_equal(
3081                 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
3082 }
3083
3084 int isl_map_is_empty(struct isl_map *map)
3085 {
3086         int i;
3087         int is_empty;
3088
3089         if (!map)
3090                 return -1;
3091         for (i = 0; i < map->n; ++i) {
3092                 is_empty = isl_basic_map_is_empty(map->p[i]);
3093                 if (is_empty < 0)
3094                         return -1;
3095                 if (!is_empty)
3096                         return 0;
3097         }
3098         return 1;
3099 }
3100
3101 int isl_set_is_empty(struct isl_set *set)
3102 {
3103         return isl_map_is_empty((struct isl_map *)set);
3104 }
3105
3106 int isl_map_is_subset(struct isl_map *map1, struct isl_map *map2)
3107 {
3108         int i;
3109         int is_subset = 0;
3110         struct isl_map *diff;
3111
3112         if (!map1 || !map2)
3113                 return -1;
3114
3115         if (isl_map_is_empty(map1))
3116                 return 1;
3117
3118         if (isl_map_is_empty(map2))
3119                 return 0;
3120
3121         diff = isl_map_subtract(isl_map_copy(map1), isl_map_copy(map2));
3122         if (!diff)
3123                 return -1;
3124
3125         is_subset = isl_map_is_empty(diff);
3126         isl_map_free(diff);
3127
3128         return is_subset;
3129 }
3130
3131 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
3132 {
3133         int is_subset;
3134
3135         if (!map1 || !map2)
3136                 return -1;
3137         is_subset = isl_map_is_subset(map1, map2);
3138         if (is_subset != 1)
3139                 return is_subset;
3140         is_subset = isl_map_is_subset(map2, map1);
3141         return is_subset;
3142 }
3143
3144 int isl_basic_map_is_strict_subset(
3145                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3146 {
3147         int is_subset;
3148
3149         if (!bmap1 || !bmap2)
3150                 return -1;
3151         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
3152         if (is_subset != 1)
3153                 return is_subset;
3154         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
3155         if (is_subset == -1)
3156                 return is_subset;
3157         return !is_subset;
3158 }
3159
3160 static int basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
3161 {
3162         int i;
3163         unsigned total;
3164         isl_int s;
3165
3166         total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3167         if (total != vec->size)
3168                 return -1;
3169
3170         isl_int_init(s);
3171
3172         for (i = 0; i < bmap->n_eq; ++i) {
3173                 isl_seq_inner_product(vec->block.data, bmap->eq[i], total, &s);
3174                 if (!isl_int_is_zero(s)) {
3175                         isl_int_clear(s);
3176                         return 0;
3177                 }
3178         }
3179
3180         for (i = 0; i < bmap->n_ineq; ++i) {
3181                 isl_seq_inner_product(vec->block.data, bmap->ineq[i], total, &s);
3182                 if (isl_int_is_neg(s)) {
3183                         isl_int_clear(s);
3184                         return 0;
3185                 }
3186         }
3187
3188         isl_int_clear(s);
3189
3190         return 1;
3191 }
3192
3193 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
3194 {
3195         if (!bmap)
3196                 return -1;
3197         return bmap->n_eq == 0 && bmap->n_ineq == 0;
3198 }
3199
3200 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
3201 {
3202         struct isl_basic_set *bset = NULL;
3203         struct isl_vec *sample = NULL;
3204         int empty;
3205         unsigned total;
3206
3207         if (!bmap)
3208                 return -1;
3209
3210         if (F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
3211                 return 1;
3212
3213         if (F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
3214                 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
3215                 copy = isl_basic_map_convex_hull(copy);
3216                 empty = F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
3217                 isl_basic_map_free(copy);
3218                 return empty;
3219         }
3220
3221         total = 1 + bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3222         if (bmap->sample && bmap->sample->size == total) {
3223                 int contains = basic_map_contains(bmap, bmap->sample);
3224                 if (contains < 0)
3225                         return -1;
3226                 if (contains)
3227                         return 0;
3228         }
3229         bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
3230         if (!bset)
3231                 return -1;
3232         sample = isl_basic_set_sample(bset);
3233         if (!sample)
3234                 return -1;
3235         empty = sample->size == 0;
3236         if (bmap->sample)
3237                 isl_vec_free(bmap->ctx, bmap->sample);
3238         bmap->sample = sample;
3239
3240         return empty;
3241 }
3242
3243 struct isl_map *isl_basic_map_union(
3244         struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3245 {
3246         struct isl_map *map;
3247         if (!bmap1 || !bmap2)
3248                 return NULL;
3249
3250         isl_assert(bmap1->ctx, bmap1->nparam == bmap2->nparam, goto error);
3251         isl_assert(bmap1->ctx, bmap1->n_in == bmap2->n_in, goto error);
3252         isl_assert(bmap1->ctx, bmap1->n_out == bmap2->n_out, goto error);
3253
3254         map = isl_map_alloc(bmap1->ctx, bmap1->nparam,
3255                                 bmap1->n_in, bmap1->n_out, 2, 0);
3256         if (!map)
3257                 goto error;
3258         map = isl_map_add(map, bmap1);
3259         map = isl_map_add(map, bmap2);
3260         return map;
3261 error:
3262         isl_basic_map_free(bmap1);
3263         isl_basic_map_free(bmap2);
3264         return NULL;
3265 }
3266
3267 struct isl_set *isl_basic_set_union(
3268                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
3269 {
3270         return (struct isl_set *)isl_basic_map_union(
3271                                             (struct isl_basic_map *)bset1,
3272                                             (struct isl_basic_map *)bset2);
3273 }
3274
3275 /* Order divs such that any div only depends on previous divs */
3276 static struct isl_basic_map *order_divs(struct isl_basic_map *bmap)
3277 {
3278         int i;
3279         unsigned off = bmap->nparam + bmap->n_in + bmap->n_out;
3280
3281         for (i = 0; i < bmap->n_div; ++i) {
3282                 int pos;
3283                 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
3284                                                             bmap->n_div-i);
3285                 if (pos == -1)
3286                         continue;
3287                 swap_div(bmap, i, pos);
3288                 --i;
3289         }
3290         return bmap;
3291 }
3292
3293 static int find_div(struct isl_basic_map *dst,
3294                         struct isl_basic_map *src, unsigned div)
3295 {
3296         int i;
3297
3298         unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3299
3300         for (i = 0; i < dst->n_div; ++i)
3301                 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total) &&
3302                     isl_seq_first_non_zero(dst->div[i]+1+1+total,
3303                                                 dst->n_div - src->n_div) == -1)
3304                         return i;
3305         return -1;
3306 }
3307
3308 struct isl_basic_map *isl_basic_map_align_divs(
3309                 struct isl_basic_map *dst, struct isl_basic_map *src)
3310 {
3311         int i;
3312         unsigned total = src->nparam + src->n_in + src->n_out + src->n_div;
3313
3314         if (src->n_div == 0)
3315                 return dst;
3316
3317         src = order_divs(src);
3318         dst = isl_basic_map_extend(dst, dst->nparam, dst->n_in,
3319                         dst->n_out, src->n_div, 0, 2 * src->n_div);
3320         if (!dst)
3321                 return NULL;
3322         for (i = 0; i < src->n_div; ++i) {
3323                 int j = find_div(dst, src, i);
3324                 if (j < 0) {
3325                         j = isl_basic_map_alloc_div(dst);
3326                         if (j < 0)
3327                                 goto error;
3328                         isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
3329                         isl_seq_clr(dst->div[j]+1+1+total,
3330                                                     dst->extra - src->n_div);
3331                         if (add_div_constraints(dst, j) < 0)
3332                                 goto error;
3333                 }
3334                 if (j != i)
3335                         swap_div(dst, i, j);
3336         }
3337         return dst;
3338 error:
3339         isl_basic_map_free(dst);
3340         return NULL;
3341 }
3342
3343 struct isl_map *isl_map_align_divs(struct isl_map *map)
3344 {
3345         int i;
3346
3347         map = isl_map_compute_divs(map);
3348         map = isl_map_cow(map);
3349         if (!map)
3350                 return NULL;
3351
3352         for (i = 1; i < map->n; ++i)
3353                 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
3354         for (i = 1; i < map->n; ++i)
3355                 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
3356
3357         return map;
3358 }
3359
3360 static struct isl_map *add_cut_constraint(struct isl_map *dst,
3361                 struct isl_basic_map *src, isl_int *c,
3362                 unsigned len, int oppose)
3363 {
3364         struct isl_basic_map *copy = NULL;
3365         int is_empty;
3366         int k;
3367         unsigned total;
3368
3369         copy = isl_basic_map_copy(src);
3370         copy = isl_basic_map_cow(copy);
3371         if (!copy)
3372                 goto error;
3373         copy = isl_basic_map_extend(copy,
3374                 copy->nparam, copy->n_in, copy->n_out, 0, 0, 1);
3375         k = isl_basic_map_alloc_inequality(copy);
3376         if (k < 0)
3377                 goto error;
3378         if (oppose)
3379                 isl_seq_neg(copy->ineq[k], c, len);
3380         else
3381                 isl_seq_cpy(copy->ineq[k], c, len);
3382         total = 1 + copy->nparam + copy->n_in + copy->n_out + copy->extra;
3383         isl_seq_clr(copy->ineq[k]+len, total - len);
3384         isl_inequality_negate(copy, k);
3385         copy = isl_basic_map_simplify(copy);
3386         copy = isl_basic_map_finalize(copy);
3387         is_empty = isl_basic_map_is_empty(copy);
3388         if (is_empty < 0)
3389                 goto error;
3390         if (!is_empty)
3391                 dst = isl_map_add(dst, copy);
3392         else
3393                 isl_basic_map_free(copy);
3394         return dst;
3395 error:
3396         isl_basic_map_free(copy);
3397         isl_map_free(dst);
3398         return NULL;
3399 }
3400
3401 static struct isl_map *subtract(struct isl_map *map, struct isl_basic_map *bmap)
3402 {
3403         int i, j, k;
3404         unsigned flags = 0;
3405         struct isl_map *rest = NULL;
3406         unsigned max;
3407         unsigned total = bmap->nparam + bmap->n_in + bmap->n_out + bmap->n_div;
3408
3409         assert(bmap);
3410
3411         if (!map)
3412                 goto error;
3413
3414         if (F_ISSET(map, ISL_MAP_DISJOINT))
3415                 FL_SET(flags, ISL_MAP_DISJOINT);
3416
3417         max = map->n * (2 * bmap->n_eq + bmap->n_ineq);
3418         rest = isl_map_alloc(map->ctx, map->nparam, map->n_in, map->n_out,
3419                                 max, flags);
3420         if (!rest)
3421                 goto error;
3422
3423         for (i = 0; i < map->n; ++i) {
3424                 map->p[i] = isl_basic_map_align_divs(map->p[i], bmap);
3425                 if (!map->p[i])
3426                         goto error;
3427         }
3428
3429         for (j = 0; j < map->n; ++j)
3430                 map->p[j] = isl_basic_map_cow(map->p[j]);
3431
3432         for (i = 0; i < bmap->n_eq; ++i) {
3433                 for (j = 0; j < map->n; ++j) {
3434                         rest = add_cut_constraint(rest,
3435                                 map->p[j], bmap->eq[i], 1+total, 0);
3436                         if (!rest)
3437                                 goto error;
3438
3439                         rest = add_cut_constraint(rest,
3440                                 map->p[j], bmap->eq[i], 1+total, 1);
3441                         if (!rest)
3442                                 goto error;
3443
3444                         map->p[j] = isl_basic_map_extend(map->p[j],
3445                                 map->p[j]->nparam, map->p[j]->n_in,
3446                                 map->p[j]->n_out, 0, 1, 0);
3447                         if (!map->p[j])
3448                                 goto error;
3449                         k = isl_basic_map_alloc_equality(map->p[j]);
3450                         if (k < 0)
3451                                 goto error;
3452                         isl_seq_cpy(map->p[j]->eq[k], bmap->eq[i], 1+total);
3453                         isl_seq_clr(map->p[j]->eq[k]+1+total,
3454                                         map->p[j]->extra - bmap->n_div);
3455                 }
3456         }
3457
3458         for (i = 0; i < bmap->n_ineq; ++i) {
3459                 for (j = 0; j < map->n; ++j) {
3460                         rest = add_cut_constraint(rest,
3461                                 map->p[j], bmap->ineq[i], 1+total, 0);
3462                         if (!rest)
3463                                 goto error;
3464
3465                         map->p[j] = isl_basic_map_extend(map->p[j],
3466                                 map->p[j]->nparam, map->p[j]->n_in,
3467                                 map->p[j]->n_out, 0, 0, 1);
3468                         if (!map->p[j])
3469                                 goto error;
3470                         k = isl_basic_map_alloc_inequality(map->p[j]);
3471                         if (k < 0)
3472                                 goto error;
3473                         isl_seq_cpy(map->p[j]->ineq[k], bmap->ineq[i], 1+total);
3474                         isl_seq_clr(map->p[j]->ineq[k]+1+total,
3475                                         map->p[j]->extra - bmap->n_div);
3476                 }
3477         }
3478
3479         isl_map_free(map);
3480         return rest;
3481 error:
3482         isl_map_free(map);
3483         isl_map_free(rest);
3484         return NULL;
3485 }
3486
3487 struct isl_map *isl_map_subtract(struct isl_map *map1, struct isl_map *map2)
3488 {
3489         int i;
3490         if (!map1 || !map2)
3491                 goto error;
3492
3493         isl_assert(map1->ctx, map1->nparam == map2->nparam, goto error);
3494         isl_assert(map1->ctx, map1->n_in == map2->n_in, goto error);
3495         isl_assert(map1->ctx, map1->n_out == map2->n_out, goto error);
3496
3497         if (isl_map_is_empty(map2)) {
3498                 isl_map_free(map2);
3499                 return map1;
3500         }
3501
3502         map1 = isl_map_compute_divs(map1);
3503         map2 = isl_map_compute_divs(map2);
3504         if (!map1 || !map2)
3505                 goto error;
3506
3507         for (i = 0; map1 && i < map2->n; ++i)
3508                 map1 = subtract(map1, map2->p[i]);
3509
3510         isl_map_free(map2);
3511         return map1;
3512 error:
3513         isl_map_free(map1);
3514         isl_map_free(map2);
3515         return NULL;
3516 }
3517
3518 struct isl_set *isl_set_subtract(struct isl_set *set1, struct isl_set *set2)
3519 {
3520         return (struct isl_set *)
3521                 isl_map_subtract(
3522                         (struct isl_map *)set1, (struct isl_map *)set2);
3523 }
3524
3525 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
3526 {
3527         if (!set || !map)
3528                 goto error;
3529         isl_assert(set->ctx, set->dim == map->n_in, goto error);
3530         map = isl_map_intersect_domain(map, set);
3531         set = isl_map_range(map);
3532         return set;
3533 error:
3534         isl_set_free(set);
3535         isl_map_free(map);
3536         return NULL;
3537 }
3538
3539 /* There is no need to cow as removing empty parts doesn't change
3540  * the meaning of the set.
3541  */
3542 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
3543 {
3544         int i;
3545
3546         if (!map)
3547                 return NULL;
3548
3549         for (i = map->n-1; i >= 0; --i) {
3550                 if (!F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
3551                         continue;
3552                 isl_basic_map_free(map->p[i]);
3553                 if (i != map->n-1)
3554                         map->p[i] = map->p[map->n-1];
3555                 map->n--;
3556         }
3557
3558         return map;
3559 }
3560
3561 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
3562 {
3563         return (struct isl_set *)
3564                 isl_map_remove_empty_parts((struct isl_map *)set);
3565 }
3566
3567 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
3568 {
3569         struct isl_basic_set *bset;
3570         if (!set || set->n == 0)
3571                 return NULL;
3572         bset = set->p[set->n-1];
3573         isl_assert(set->ctx, F_ISSET(bset, ISL_BASIC_SET_FINAL), return NULL);
3574         return isl_basic_set_copy(bset);
3575 }
3576
3577 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
3578                                                 struct isl_basic_set *bset)
3579 {
3580         int i;
3581
3582         if (!set || !bset)
3583                 goto error;
3584         for (i = set->n-1; i >= 0; --i) {
3585                 if (set->p[i] != bset)
3586                         continue;
3587                 set = isl_set_cow(set);
3588                 if (!set)
3589                         goto error;
3590                 isl_basic_set_free(set->p[i]);
3591                 if (i != set->n-1)
3592                         set->p[i] = set->p[set->n-1];
3593                 set->n--;
3594                 return set;
3595         }
3596         isl_basic_set_free(bset);
3597         return set;
3598 error:
3599         isl_set_free(set);
3600         isl_basic_set_free(bset);
3601         return NULL;
3602 }