6840726d6aa3858d66fb7d9be3c55f1914edafcd
[platform/upstream/isl.git] / isl_map.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
7  * Written by Sven Verdoolaege, K.U.Leuven, Departement
8  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
11  */
12
13 #include <string.h>
14 #include <strings.h>
15 #include "isl_ctx.h"
16 #include "isl_blk.h"
17 #include "isl_dim.h"
18 #include "isl_equalities.h"
19 #include "isl_list.h"
20 #include "isl_lp.h"
21 #include "isl_seq.h"
22 #include "isl_set.h"
23 #include "isl_map.h"
24 #include "isl_map_private.h"
25 #include "isl_map_piplib.h"
26 #include "isl_sample.h"
27 #include "isl_tab.h"
28 #include "isl_vec.h"
29
30 /* Maps dst positions to src positions */
31 struct isl_dim_map {
32         unsigned len;
33         int pos[1];
34 };
35
36 static struct isl_dim_map *isl_dim_map_alloc(struct isl_ctx *ctx, unsigned len)
37 {
38         int i;
39         struct isl_dim_map *dim_map;
40         dim_map = isl_alloc(ctx, struct isl_dim_map,
41                                 sizeof(struct isl_dim_map) + len * sizeof(int));
42         if (!dim_map)
43                 return NULL;
44         dim_map->len = 1 + len;
45         dim_map->pos[0] = 0;
46         for (i = 0; i < len; ++i)
47                 dim_map->pos[1 + i] = -1;
48         return dim_map;
49 }
50
51 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
52 {
53         switch (type) {
54         case isl_dim_param:     return dim->nparam;
55         case isl_dim_in:        return dim->n_in;
56         case isl_dim_out:       return dim->n_out;
57         case isl_dim_all:       return dim->nparam + dim->n_in + dim->n_out;
58         }
59 }
60
61 static unsigned pos(struct isl_dim *dim, enum isl_dim_type type)
62 {
63         switch (type) {
64         case isl_dim_param:     return 1;
65         case isl_dim_in:        return 1 + dim->nparam;
66         case isl_dim_out:       return 1 + dim->nparam + dim->n_in;
67         }
68 }
69
70 static void isl_dim_map_dim_range(struct isl_dim_map *dim_map,
71         struct isl_dim *dim, enum isl_dim_type type,
72         unsigned first, unsigned n, unsigned dst_pos)
73 {
74         int i;
75         unsigned src_pos;
76
77         if (!dim_map || !dim)
78                 return;
79         
80         src_pos = pos(dim, type);
81         for (i = 0; i < n; ++i)
82                 dim_map->pos[1 + dst_pos + i] = src_pos + first + i;
83 }
84
85 static void isl_dim_map_dim(struct isl_dim_map *dim_map, struct isl_dim *dim,
86                 enum isl_dim_type type, unsigned dst_pos)
87 {
88         isl_dim_map_dim_range(dim_map, dim, type, 0, n(dim, type), dst_pos);
89 }
90
91 static void isl_dim_map_div(struct isl_dim_map *dim_map,
92                 struct isl_basic_map *bmap, unsigned dst_pos)
93 {
94         int i;
95         unsigned src_pos;
96
97         if (!dim_map || !bmap)
98                 return;
99         
100         src_pos = 1 + isl_dim_total(bmap->dim);
101         for (i = 0; i < bmap->n_div; ++i)
102                 dim_map->pos[1 + dst_pos + i] = src_pos + i;
103 }
104
105 static void isl_dim_map_dump(struct isl_dim_map *dim_map)
106 {
107         int i;
108
109         for (i = 0; i < dim_map->len; ++i)
110                 fprintf(stderr, "%d -> %d; ", i, dim_map->pos[i]);
111         fprintf(stderr, "\n");
112 }
113
114 unsigned isl_basic_map_dim(const struct isl_basic_map *bmap,
115                                 enum isl_dim_type type)
116 {
117         switch (type) {
118         case isl_dim_param:
119         case isl_dim_in:
120         case isl_dim_out:       return isl_dim_size(bmap->dim, type);
121         case isl_dim_div:       return bmap->n_div;
122         case isl_dim_all:       return isl_basic_map_total_dim(bmap);
123         }
124 }
125
126 unsigned isl_map_dim(const struct isl_map *map, enum isl_dim_type type)
127 {
128         return map ? n(map->dim, type) : 0;
129 }
130
131 unsigned isl_set_dim(const struct isl_set *set, enum isl_dim_type type)
132 {
133         return set ? n(set->dim, type) : 0;
134 }
135
136 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
137                                         enum isl_dim_type type)
138 {
139         struct isl_dim *dim = bmap->dim;
140         switch (type) {
141         case isl_dim_param:     return 1;
142         case isl_dim_in:        return 1 + dim->nparam;
143         case isl_dim_out:       return 1 + dim->nparam + dim->n_in;
144         case isl_dim_div:       return 1 + dim->nparam + dim->n_in + dim->n_out;
145         }
146 }
147
148 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
149 {
150         return pos(map->dim, type);
151 }
152
153 unsigned isl_basic_set_dim(const struct isl_basic_set *bset,
154                                 enum isl_dim_type type)
155 {
156         return isl_basic_map_dim((const struct isl_basic_map*)bset, type);
157 }
158
159 unsigned isl_basic_set_n_dim(const struct isl_basic_set *bset)
160 {
161         return bset->dim->n_out;
162 }
163
164 unsigned isl_basic_set_n_param(const struct isl_basic_set *bset)
165 {
166         return bset->dim->nparam;
167 }
168
169 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
170 {
171         return isl_dim_total(bset->dim) + bset->n_div;
172 }
173
174 unsigned isl_set_n_dim(const struct isl_set *set)
175 {
176         return set->dim->n_out;
177 }
178
179 unsigned isl_set_n_param(const struct isl_set *set)
180 {
181         return set->dim->nparam;
182 }
183
184 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
185 {
186         return bmap->dim->n_in;
187 }
188
189 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
190 {
191         return bmap->dim->n_out;
192 }
193
194 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
195 {
196         return bmap->dim->nparam;
197 }
198
199 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
200 {
201         return bmap->n_div;
202 }
203
204 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
205 {
206         return isl_dim_total(bmap->dim) + bmap->n_div;
207 }
208
209 unsigned isl_map_n_in(const struct isl_map *map)
210 {
211         return map->dim->n_in;
212 }
213
214 unsigned isl_map_n_out(const struct isl_map *map)
215 {
216         return map->dim->n_out;
217 }
218
219 unsigned isl_map_n_param(const struct isl_map *map)
220 {
221         return map->dim->nparam;
222 }
223
224 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
225 {
226         return map->dim->n_in == set->dim->n_out &&
227                map->dim->nparam == set->dim->nparam;
228 }
229
230 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
231                 struct isl_basic_set *bset)
232 {
233         return bmap->dim->n_in == bset->dim->n_out &&
234                bmap->dim->nparam == bset->dim->nparam;
235 }
236
237 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
238                 struct isl_basic_set *bset)
239 {
240         return bmap->dim->n_out == bset->dim->n_out &&
241                bmap->dim->nparam == bset->dim->nparam;
242 }
243
244 struct isl_dim *isl_basic_map_get_dim(struct isl_basic_map *bmap)
245 {
246         if (!bmap)
247                 return NULL;
248         return isl_dim_copy(bmap->dim);
249 }
250
251 struct isl_dim *isl_basic_set_get_dim(struct isl_basic_set *bset)
252 {
253         if (!bset)
254                 return NULL;
255         return isl_dim_copy(bset->dim);
256 }
257
258 struct isl_dim *isl_map_get_dim(struct isl_map *map)
259 {
260         if (!map)
261                 return NULL;
262         return isl_dim_copy(map->dim);
263 }
264
265 struct isl_dim *isl_set_get_dim(struct isl_set *set)
266 {
267         if (!set)
268                 return NULL;
269         return isl_dim_copy(set->dim);
270 }
271
272 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
273                 struct isl_basic_map *bmap, unsigned extra,
274                 unsigned n_eq, unsigned n_ineq)
275 {
276         int i;
277         size_t row_size = 1 + isl_dim_total(bmap->dim) + extra;
278
279         bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
280         if (isl_blk_is_error(bmap->block)) {
281                 free(bmap);
282                 return NULL;
283         }
284
285         bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
286         if (!bmap->ineq) {
287                 isl_blk_free(ctx, bmap->block);
288                 free(bmap);
289                 return NULL;
290         }
291
292         if (extra == 0) {
293                 bmap->block2 = isl_blk_empty();
294                 bmap->div = NULL;
295         } else {
296                 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
297                 if (isl_blk_is_error(bmap->block2)) {
298                         free(bmap->ineq);
299                         isl_blk_free(ctx, bmap->block);
300                         free(bmap);
301                         return NULL;
302                 }
303
304                 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
305                 if (!bmap->div) {
306                         isl_blk_free(ctx, bmap->block2);
307                         free(bmap->ineq);
308                         isl_blk_free(ctx, bmap->block);
309                         free(bmap);
310                         return NULL;
311                 }
312         }
313
314         for (i = 0; i < n_ineq + n_eq; ++i)
315                 bmap->ineq[i] = bmap->block.data + i * row_size;
316
317         for (i = 0; i < extra; ++i)
318                 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
319
320         bmap->ctx = ctx;
321         isl_ctx_ref(ctx);
322         bmap->ref = 1;
323         bmap->flags = 0;
324         bmap->c_size = n_eq + n_ineq;
325         bmap->eq = bmap->ineq + n_ineq;
326         bmap->extra = extra;
327         bmap->n_eq = 0;
328         bmap->n_ineq = 0;
329         bmap->n_div = 0;
330         bmap->sample = NULL;
331
332         return bmap;
333 error:
334         isl_basic_map_free(bmap);
335         return NULL;
336 }
337
338 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
339                 unsigned nparam, unsigned dim, unsigned extra,
340                 unsigned n_eq, unsigned n_ineq)
341 {
342         struct isl_basic_map *bmap;
343         bmap = isl_basic_map_alloc(ctx, nparam, 0, dim, extra, n_eq, n_ineq);
344         return (struct isl_basic_set *)bmap;
345 }
346
347 struct isl_basic_set *isl_basic_set_alloc_dim(struct isl_dim *dim,
348                 unsigned extra, unsigned n_eq, unsigned n_ineq)
349 {
350         struct isl_basic_map *bmap;
351         if (!dim)
352                 return NULL;
353         isl_assert(dim->ctx, dim->n_in == 0, return NULL);
354         bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
355         return (struct isl_basic_set *)bmap;
356 }
357
358 struct isl_basic_map *isl_basic_map_alloc_dim(struct isl_dim *dim,
359                 unsigned extra, unsigned n_eq, unsigned n_ineq)
360 {
361         struct isl_basic_map *bmap;
362
363         if (!dim)
364                 return NULL;
365         bmap = isl_alloc_type(dim->ctx, struct isl_basic_map);
366         if (!bmap)
367                 goto error;
368         bmap->dim = dim;
369
370         return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
371 error:
372         isl_dim_free(dim);
373         return NULL;
374 }
375
376 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
377                 unsigned nparam, unsigned in, unsigned out, unsigned extra,
378                 unsigned n_eq, unsigned n_ineq)
379 {
380         struct isl_basic_map *bmap;
381         struct isl_dim *dim;
382
383         dim = isl_dim_alloc(ctx, nparam, in, out);
384         if (!dim)
385                 return NULL;
386
387         bmap = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
388         return bmap;
389 }
390
391 static void dup_constraints(
392                 struct isl_basic_map *dst, struct isl_basic_map *src)
393 {
394         int i;
395         unsigned total = isl_basic_map_total_dim(src);
396
397         for (i = 0; i < src->n_eq; ++i) {
398                 int j = isl_basic_map_alloc_equality(dst);
399                 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
400         }
401
402         for (i = 0; i < src->n_ineq; ++i) {
403                 int j = isl_basic_map_alloc_inequality(dst);
404                 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
405         }
406
407         for (i = 0; i < src->n_div; ++i) {
408                 int j = isl_basic_map_alloc_div(dst);
409                 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
410         }
411         ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
412 }
413
414 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
415 {
416         struct isl_basic_map *dup;
417
418         if (!bmap)
419                 return NULL;
420         dup = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
421                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
422         if (!dup)
423                 return NULL;
424         dup_constraints(dup, bmap);
425         dup->flags = bmap->flags;
426         dup->sample = isl_vec_copy(bmap->sample);
427         return dup;
428 }
429
430 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
431 {
432         struct isl_basic_map *dup;
433
434         dup = isl_basic_map_dup((struct isl_basic_map *)bset);
435         return (struct isl_basic_set *)dup;
436 }
437
438 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
439 {
440         if (!bset)
441                 return NULL;
442
443         if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
444                 bset->ref++;
445                 return bset;
446         }
447         return isl_basic_set_dup(bset);
448 }
449
450 struct isl_set *isl_set_copy(struct isl_set *set)
451 {
452         if (!set)
453                 return NULL;
454
455         set->ref++;
456         return set;
457 }
458
459 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
460 {
461         if (!bmap)
462                 return NULL;
463
464         if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
465                 bmap->ref++;
466                 return bmap;
467         }
468         return isl_basic_map_dup(bmap);
469 }
470
471 struct isl_map *isl_map_copy(struct isl_map *map)
472 {
473         if (!map)
474                 return NULL;
475
476         map->ref++;
477         return map;
478 }
479
480 void isl_basic_map_free(struct isl_basic_map *bmap)
481 {
482         if (!bmap)
483                 return;
484
485         if (--bmap->ref > 0)
486                 return;
487
488         isl_ctx_deref(bmap->ctx);
489         free(bmap->div);
490         isl_blk_free(bmap->ctx, bmap->block2);
491         free(bmap->ineq);
492         isl_blk_free(bmap->ctx, bmap->block);
493         isl_vec_free(bmap->sample);
494         isl_dim_free(bmap->dim);
495         free(bmap);
496 }
497
498 void isl_basic_set_free(struct isl_basic_set *bset)
499 {
500         isl_basic_map_free((struct isl_basic_map *)bset);
501 }
502
503 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
504 {
505         return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
506 }
507
508 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
509 {
510         struct isl_ctx *ctx;
511         if (!bmap)
512                 return -1;
513         ctx = bmap->ctx;
514         isl_assert(ctx, room_for_con(bmap, 1), return -1);
515         isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
516                         return -1);
517         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
518         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
519         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
520         ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
521         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
522         if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
523                 isl_int *t;
524                 int j = isl_basic_map_alloc_inequality(bmap);
525                 if (j < 0)
526                         return -1;
527                 t = bmap->ineq[j];
528                 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
529                 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
530                 bmap->eq[-1] = t;
531                 bmap->n_eq++;
532                 bmap->n_ineq--;
533                 bmap->eq--;
534                 return 0;
535         }
536         isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
537                       bmap->extra - bmap->n_div);
538         return bmap->n_eq++;
539 }
540
541 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
542 {
543         return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
544 }
545
546 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
547 {
548         if (!bmap)
549                 return -1;
550         isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
551         bmap->n_eq -= n;
552         return 0;
553 }
554
555 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
556 {
557         return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
558 }
559
560 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
561 {
562         isl_int *t;
563         if (!bmap)
564                 return -1;
565         isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
566
567         if (pos != bmap->n_eq - 1) {
568                 t = bmap->eq[pos];
569                 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
570                 bmap->eq[bmap->n_eq - 1] = t;
571         }
572         bmap->n_eq--;
573         return 0;
574 }
575
576 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
577 {
578         return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
579 }
580
581 void isl_basic_map_inequality_to_equality(
582                 struct isl_basic_map *bmap, unsigned pos)
583 {
584         isl_int *t;
585
586         t = bmap->ineq[pos];
587         bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
588         bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
589         bmap->eq[-1] = t;
590         bmap->n_eq++;
591         bmap->n_ineq--;
592         bmap->eq--;
593         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
594         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
595         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
596         ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
597 }
598
599 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
600 {
601         return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
602 }
603
604 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
605 {
606         struct isl_ctx *ctx;
607         if (!bmap)
608                 return -1;
609         ctx = bmap->ctx;
610         isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
611         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
612         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
613         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
614         ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
615         isl_seq_clr(bmap->ineq[bmap->n_ineq] +
616                       1 + isl_basic_map_total_dim(bmap),
617                       bmap->extra - bmap->n_div);
618         return bmap->n_ineq++;
619 }
620
621 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
622 {
623         return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
624 }
625
626 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
627 {
628         if (!bmap)
629                 return -1;
630         isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
631         bmap->n_ineq -= n;
632         return 0;
633 }
634
635 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
636 {
637         return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
638 }
639
640 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
641 {
642         isl_int *t;
643         if (!bmap)
644                 return -1;
645         isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
646
647         if (pos != bmap->n_ineq - 1) {
648                 t = bmap->ineq[pos];
649                 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
650                 bmap->ineq[bmap->n_ineq - 1] = t;
651                 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
652         }
653         bmap->n_ineq--;
654         return 0;
655 }
656
657 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
658 {
659         return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
660 }
661
662 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
663         isl_int *eq)
664 {
665         int k;
666
667         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
668         if (!bmap)
669                 return NULL;
670         k = isl_basic_map_alloc_equality(bmap);
671         if (k < 0)
672                 goto error;
673         isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
674         return bmap;
675 error:
676         isl_basic_map_free(bmap);
677         return NULL;
678 }
679
680 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
681         isl_int *eq)
682 {
683         return (isl_basic_set *)
684                 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
685 }
686
687 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
688         isl_int *ineq)
689 {
690         int k;
691
692         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
693         if (!bmap)
694                 return NULL;
695         k = isl_basic_map_alloc_inequality(bmap);
696         if (k < 0)
697                 goto error;
698         isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
699         return bmap;
700 error:
701         isl_basic_map_free(bmap);
702         return NULL;
703 }
704
705 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
706         isl_int *ineq)
707 {
708         return (isl_basic_set *)
709                 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
710 }
711
712 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
713 {
714         if (!bmap)
715                 return -1;
716         isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
717         isl_seq_clr(bmap->div[bmap->n_div] +
718                       1 + 1 + isl_basic_map_total_dim(bmap),
719                       bmap->extra - bmap->n_div);
720         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
721         return bmap->n_div++;
722 }
723
724 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
725 {
726         return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
727 }
728
729 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
730 {
731         if (!bmap)
732                 return -1;
733         isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
734         bmap->n_div -= n;
735         return 0;
736 }
737
738 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
739 {
740         return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
741 }
742
743 /* Copy constraint from src to dst, putting the vars of src at offset
744  * dim_off in dst and the divs of src at offset div_off in dst.
745  * If both sets are actually map, then dim_off applies to the input
746  * variables.
747  */
748 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
749                             struct isl_basic_map *src_map, isl_int *src,
750                             unsigned in_off, unsigned out_off, unsigned div_off)
751 {
752         unsigned src_nparam = isl_basic_map_n_param(src_map);
753         unsigned dst_nparam = isl_basic_map_n_param(dst_map);
754         unsigned src_in = isl_basic_map_n_in(src_map);
755         unsigned dst_in = isl_basic_map_n_in(dst_map);
756         unsigned src_out = isl_basic_map_n_out(src_map);
757         unsigned dst_out = isl_basic_map_n_out(dst_map);
758         isl_int_set(dst[0], src[0]);
759         isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
760         if (dst_nparam > src_nparam)
761                 isl_seq_clr(dst+1+src_nparam,
762                                 dst_nparam - src_nparam);
763         isl_seq_clr(dst+1+dst_nparam, in_off);
764         isl_seq_cpy(dst+1+dst_nparam+in_off,
765                     src+1+src_nparam,
766                     isl_min(dst_in-in_off, src_in));
767         if (dst_in-in_off > src_in)
768                 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
769                                 dst_in - in_off - src_in);
770         isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
771         isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
772                     src+1+src_nparam+src_in,
773                     isl_min(dst_out-out_off, src_out));
774         if (dst_out-out_off > src_out)
775                 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
776                                 dst_out - out_off - src_out);
777         isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
778         isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
779                     src+1+src_nparam+src_in+src_out,
780                     isl_min(dst_map->extra-div_off, src_map->n_div));
781         if (dst_map->n_div-div_off > src_map->n_div)
782                 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
783                                 div_off+src_map->n_div,
784                                 dst_map->n_div - div_off - src_map->n_div);
785 }
786
787 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
788                      struct isl_basic_map *src_map, isl_int *src,
789                      unsigned in_off, unsigned out_off, unsigned div_off)
790 {
791         isl_int_set(dst[0], src[0]);
792         copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
793 }
794
795 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
796                 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
797 {
798         int i;
799         unsigned div_off;
800
801         if (!bmap1 || !bmap2)
802                 goto error;
803
804         div_off = bmap1->n_div;
805
806         for (i = 0; i < bmap2->n_eq; ++i) {
807                 int i1 = isl_basic_map_alloc_equality(bmap1);
808                 if (i1 < 0)
809                         goto error;
810                 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
811                                 i_pos, o_pos, div_off);
812         }
813
814         for (i = 0; i < bmap2->n_ineq; ++i) {
815                 int i1 = isl_basic_map_alloc_inequality(bmap1);
816                 if (i1 < 0)
817                         goto error;
818                 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
819                                 i_pos, o_pos, div_off);
820         }
821
822         for (i = 0; i < bmap2->n_div; ++i) {
823                 int i1 = isl_basic_map_alloc_div(bmap1);
824                 if (i1 < 0)
825                         goto error;
826                 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
827                          i_pos, o_pos, div_off);
828         }
829
830         isl_basic_map_free(bmap2);
831
832         return bmap1;
833
834 error:
835         isl_basic_map_free(bmap1);
836         isl_basic_map_free(bmap2);
837         return NULL;
838 }
839
840 static void copy_constraint_dim_map(isl_int *dst, isl_int *src,
841                                         struct isl_dim_map *dim_map)
842 {
843         int i;
844
845         for (i = 0; i < dim_map->len; ++i) {
846                 if (dim_map->pos[i] < 0)
847                         isl_int_set_si(dst[i], 0);
848                 else
849                         isl_int_set(dst[i], src[dim_map->pos[i]]);
850         }
851 }
852
853 static void copy_div_dim_map(isl_int *dst, isl_int *src,
854                                         struct isl_dim_map *dim_map)
855 {
856         isl_int_set(dst[0], src[0]);
857         copy_constraint_dim_map(dst+1, src+1, dim_map);
858 }
859
860 static struct isl_basic_map *add_constraints_dim_map(struct isl_basic_map *dst,
861                 struct isl_basic_map *src, struct isl_dim_map *dim_map)
862 {
863         int i;
864
865         if (!src || !dst || !dim_map)
866                 goto error;
867
868         for (i = 0; i < src->n_eq; ++i) {
869                 int i1 = isl_basic_map_alloc_equality(dst);
870                 if (i1 < 0)
871                         goto error;
872                 copy_constraint_dim_map(dst->eq[i1], src->eq[i], dim_map);
873         }
874
875         for (i = 0; i < src->n_ineq; ++i) {
876                 int i1 = isl_basic_map_alloc_inequality(dst);
877                 if (i1 < 0)
878                         goto error;
879                 copy_constraint_dim_map(dst->ineq[i1], src->ineq[i], dim_map);
880         }
881
882         for (i = 0; i < src->n_div; ++i) {
883                 int i1 = isl_basic_map_alloc_div(dst);
884                 if (i1 < 0)
885                         goto error;
886                 copy_div_dim_map(dst->div[i1], src->div[i], dim_map);
887         }
888
889         free(dim_map);
890         isl_basic_map_free(src);
891
892         return dst;
893 error:
894         free(dim_map);
895         isl_basic_map_free(src);
896         isl_basic_map_free(dst);
897         return NULL;
898 }
899
900 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
901                 struct isl_basic_set *bset2, unsigned pos)
902 {
903         return (struct isl_basic_set *)
904                 add_constraints((struct isl_basic_map *)bset1,
905                                 (struct isl_basic_map *)bset2, 0, pos);
906 }
907
908 struct isl_basic_map *isl_basic_map_extend_dim(struct isl_basic_map *base,
909                 struct isl_dim *dim, unsigned extra,
910                 unsigned n_eq, unsigned n_ineq)
911 {
912         struct isl_basic_map *ext;
913         unsigned flags;
914         int dims_ok;
915
916         if (!dim)
917                 goto error;
918
919         if (!base)
920                 goto error;
921
922         dims_ok = isl_dim_equal(base->dim, dim) &&
923                   base->extra >= base->n_div + extra;
924
925         if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
926                        room_for_ineq(base, n_ineq)) {
927                 isl_dim_free(dim);
928                 return base;
929         }
930
931         isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
932         isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
933         isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
934         extra += base->extra;
935         n_eq += base->n_eq;
936         n_ineq += base->n_ineq;
937
938         ext = isl_basic_map_alloc_dim(dim, extra, n_eq, n_ineq);
939         dim = NULL;
940         if (!ext)
941                 goto error;
942
943         if (dims_ok)
944                 ext->sample = isl_vec_copy(base->sample);
945         flags = base->flags;
946         ext = add_constraints(ext, base, 0, 0);
947         if (ext) {
948                 ext->flags = flags;
949                 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
950         }
951
952         return ext;
953
954 error:
955         isl_dim_free(dim);
956         isl_basic_map_free(base);
957         return NULL;
958 }
959
960 struct isl_basic_set *isl_basic_set_extend_dim(struct isl_basic_set *base,
961                 struct isl_dim *dim, unsigned extra,
962                 unsigned n_eq, unsigned n_ineq)
963 {
964         return (struct isl_basic_set *)
965                 isl_basic_map_extend_dim((struct isl_basic_map *)base, dim,
966                                                         extra, n_eq, n_ineq);
967 }
968
969 struct isl_basic_map *isl_basic_map_extend_constraints(
970                 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
971 {
972         if (!base)
973                 return NULL;
974         return isl_basic_map_extend_dim(base, isl_dim_copy(base->dim),
975                                         0, n_eq, n_ineq);
976 }
977
978 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
979                 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
980                 unsigned n_eq, unsigned n_ineq)
981 {
982         struct isl_basic_map *bmap;
983         struct isl_dim *dim;
984
985         if (!base)
986                 return NULL;
987         dim = isl_dim_alloc(base->ctx, nparam, n_in, n_out);
988         if (!dim)
989                 return NULL;
990
991         bmap = isl_basic_map_extend_dim(base, dim, extra, n_eq, n_ineq);
992         return bmap;
993 }
994
995 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
996                 unsigned nparam, unsigned dim, unsigned extra,
997                 unsigned n_eq, unsigned n_ineq)
998 {
999         return (struct isl_basic_set *)
1000                 isl_basic_map_extend((struct isl_basic_map *)base,
1001                                         nparam, 0, dim, extra, n_eq, n_ineq);
1002 }
1003
1004 struct isl_basic_set *isl_basic_set_extend_constraints(
1005                 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1006 {
1007         return (struct isl_basic_set *)
1008                 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1009                                                     n_eq, n_ineq);
1010 }
1011
1012 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1013 {
1014         return (struct isl_basic_set *)
1015                 isl_basic_map_cow((struct isl_basic_map *)bset);
1016 }
1017
1018 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1019 {
1020         if (!bmap)
1021                 return NULL;
1022
1023         if (bmap->ref > 1) {
1024                 bmap->ref--;
1025                 bmap = isl_basic_map_dup(bmap);
1026         }
1027         ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1028         return bmap;
1029 }
1030
1031 struct isl_set *isl_set_cow(struct isl_set *set)
1032 {
1033         if (!set)
1034                 return NULL;
1035
1036         if (set->ref == 1)
1037                 return set;
1038         set->ref--;
1039         return isl_set_dup(set);
1040 }
1041
1042 struct isl_map *isl_map_cow(struct isl_map *map)
1043 {
1044         if (!map)
1045                 return NULL;
1046
1047         if (map->ref == 1)
1048                 return map;
1049         map->ref--;
1050         return isl_map_dup(map);
1051 }
1052
1053 static void swap_vars(struct isl_blk blk, isl_int *a,
1054                         unsigned a_len, unsigned b_len)
1055 {
1056         isl_seq_cpy(blk.data, a+a_len, b_len);
1057         isl_seq_cpy(blk.data+b_len, a, a_len);
1058         isl_seq_cpy(a, blk.data, b_len+a_len);
1059 }
1060
1061 struct isl_basic_set *isl_basic_set_swap_vars(
1062                 struct isl_basic_set *bset, unsigned n)
1063 {
1064         int i;
1065         struct isl_blk blk;
1066         unsigned dim;
1067         unsigned nparam;
1068
1069         if (!bset)
1070                 goto error;
1071
1072         nparam = isl_basic_set_n_param(bset);
1073         dim = isl_basic_set_n_dim(bset);
1074         isl_assert(bset->ctx, n <= dim, goto error);
1075
1076         if (n == dim)
1077                 return bset;
1078
1079         bset = isl_basic_set_cow(bset);
1080         if (!bset)
1081                 return NULL;
1082
1083         blk = isl_blk_alloc(bset->ctx, dim);
1084         if (isl_blk_is_error(blk))
1085                 goto error;
1086
1087         for (i = 0; i < bset->n_eq; ++i)
1088                 swap_vars(blk,
1089                           bset->eq[i]+1+nparam, n, dim - n);
1090
1091         for (i = 0; i < bset->n_ineq; ++i)
1092                 swap_vars(blk,
1093                           bset->ineq[i]+1+nparam, n, dim - n);
1094
1095         for (i = 0; i < bset->n_div; ++i)
1096                 swap_vars(blk,
1097                           bset->div[i]+1+1+nparam, n, dim - n);
1098
1099         isl_blk_free(bset->ctx, blk);
1100
1101         ISL_F_CLR(bset, ISL_BASIC_SET_NORMALIZED);
1102         return isl_basic_set_gauss(bset, NULL);
1103 error:
1104         isl_basic_set_free(bset);
1105         return NULL;
1106 }
1107
1108 struct isl_set *isl_set_swap_vars(struct isl_set *set, unsigned n)
1109 {
1110         int i;
1111         set = isl_set_cow(set);
1112         if (!set)
1113                 return NULL;
1114
1115         for (i = 0; i < set->n; ++i) {
1116                 set->p[i] = isl_basic_set_swap_vars(set->p[i], n);
1117                 if (!set->p[i]) {
1118                         isl_set_free(set);
1119                         return NULL;
1120                 }
1121         }
1122         ISL_F_CLR(set, ISL_SET_NORMALIZED);
1123         return set;
1124 }
1125
1126 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1127 {
1128         int i = 0;
1129         unsigned total;
1130         if (!bmap)
1131                 goto error;
1132         total = isl_basic_map_total_dim(bmap);
1133         isl_basic_map_free_div(bmap, bmap->n_div);
1134         isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1135         if (bmap->n_eq > 0)
1136                 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1137         else {
1138                 isl_basic_map_alloc_equality(bmap);
1139                 if (i < 0)
1140                         goto error;
1141         }
1142         isl_int_set_si(bmap->eq[i][0], 1);
1143         isl_seq_clr(bmap->eq[i]+1, total);
1144         ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1145         isl_vec_free(bmap->sample);
1146         bmap->sample = NULL;
1147         return isl_basic_map_finalize(bmap);
1148 error:
1149         isl_basic_map_free(bmap);
1150         return NULL;
1151 }
1152
1153 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1154 {
1155         return (struct isl_basic_set *)
1156                 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1157 }
1158
1159 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1160 {
1161         int i;
1162         unsigned off = isl_dim_total(bmap->dim);
1163         isl_int *t = bmap->div[a];
1164         bmap->div[a] = bmap->div[b];
1165         bmap->div[b] = t;
1166
1167         for (i = 0; i < bmap->n_eq; ++i)
1168                 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1169
1170         for (i = 0; i < bmap->n_ineq; ++i)
1171                 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1172
1173         for (i = 0; i < bmap->n_div; ++i)
1174                 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1175         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1176 }
1177
1178 /* Eliminate the specified n dimensions starting at first from the
1179  * constraints using Fourier-Motzkin, The dimensions themselves
1180  * are not removed.
1181  */
1182 struct isl_set *isl_set_eliminate_dims(struct isl_set *set,
1183         unsigned first, unsigned n)
1184 {
1185         int i;
1186         unsigned nparam;
1187
1188         if (!set)
1189                 return NULL;
1190         if (n == 0)
1191                 return set;
1192
1193         set = isl_set_cow(set);
1194         if (!set)
1195                 return NULL;
1196         isl_assert(set->ctx, first+n <= isl_set_n_dim(set), goto error);
1197         nparam = isl_set_n_param(set);
1198         
1199         for (i = 0; i < set->n; ++i) {
1200                 set->p[i] = isl_basic_set_eliminate_vars(set->p[i],
1201                                                             nparam + first, n);
1202                 if (!set->p[i])
1203                         goto error;
1204         }
1205         return set;
1206 error:
1207         isl_set_free(set);
1208         return NULL;
1209 }
1210
1211 /* Project out n dimensions starting at first using Fourier-Motzkin */
1212 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1213         unsigned first, unsigned n)
1214 {
1215         set = isl_set_eliminate_dims(set, first, n);
1216         set = isl_set_drop_dims(set, first, n);
1217         return set;
1218 }
1219
1220 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1221 {
1222         bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1223                                                 bset->n_div);
1224         if (!bset)
1225                 return NULL;
1226         bset->n_div = 0;
1227         return bset;
1228 }
1229
1230 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1231 {
1232         int i;
1233
1234         if (!set)
1235                 return NULL;
1236         if (set->n == 0)
1237                 return set;
1238
1239         set = isl_set_cow(set);
1240         if (!set)
1241                 return NULL;
1242         
1243         for (i = 0; i < set->n; ++i) {
1244                 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1245                 if (!set->p[i])
1246                         goto error;
1247         }
1248         return set;
1249 error:
1250         isl_set_free(set);
1251         return NULL;
1252 }
1253
1254 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1255         enum isl_dim_type type, unsigned first, unsigned n)
1256 {
1257         if (!bmap)
1258                 return NULL;
1259         isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1260                         goto error);
1261         if (n == 0)
1262                 return bmap;
1263         bmap = isl_basic_map_eliminate_vars(bmap,
1264                         isl_basic_map_offset(bmap, type) - 1 + first, n);
1265         if (!bmap)
1266                 return bmap;
1267         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1268                 return bmap;
1269         bmap = isl_basic_map_drop(bmap, type, first, n);
1270         return bmap;
1271 error:
1272         isl_basic_map_free(bmap);
1273         return NULL;
1274 }
1275
1276 __isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
1277         enum isl_dim_type type, unsigned first, unsigned n)
1278 {
1279         return (isl_basic_set *)
1280                 isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
1281 }
1282
1283 struct isl_map *isl_map_remove(struct isl_map *map,
1284         enum isl_dim_type type, unsigned first, unsigned n)
1285 {
1286         int i;
1287
1288         if (n == 0)
1289                 return map;
1290
1291         map = isl_map_cow(map);
1292         if (!map)
1293                 return NULL;
1294         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1295         
1296         for (i = 0; i < map->n; ++i) {
1297                 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1298                         isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1299                 if (!map->p[i])
1300                         goto error;
1301         }
1302         map = isl_map_drop(map, type, first, n);
1303         return map;
1304 error:
1305         isl_map_free(map);
1306         return NULL;
1307 }
1308
1309 __isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
1310         enum isl_dim_type type, unsigned first, unsigned n)
1311 {
1312         return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
1313 }
1314
1315 /* Project out n inputs starting at first using Fourier-Motzkin */
1316 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1317         unsigned first, unsigned n)
1318 {
1319         return isl_map_remove(map, isl_dim_in, first, n);
1320 }
1321
1322 /* Project out n dimensions starting at first using Fourier-Motzkin */
1323 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1324         unsigned first, unsigned n)
1325 {
1326         unsigned nparam = isl_basic_set_n_param(bset);
1327         bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1328         bset = isl_basic_set_drop_dims(bset, first, n);
1329         return bset;
1330 }
1331
1332 static void dump_term(struct isl_basic_map *bmap,
1333                         isl_int c, int pos, FILE *out)
1334 {
1335         const char *name;
1336         unsigned in = isl_basic_map_n_in(bmap);
1337         unsigned dim = in + isl_basic_map_n_out(bmap);
1338         unsigned nparam = isl_basic_map_n_param(bmap);
1339         if (!pos)
1340                 isl_int_print(out, c, 0);
1341         else {
1342                 if (!isl_int_is_one(c))
1343                         isl_int_print(out, c, 0);
1344                 if (pos < 1 + nparam) {
1345                         name = isl_dim_get_name(bmap->dim,
1346                                                 isl_dim_param, pos - 1);
1347                         if (name)
1348                                 fprintf(out, "%s", name);
1349                         else
1350                                 fprintf(out, "p%d", pos - 1);
1351                 } else if (pos < 1 + nparam + in)
1352                         fprintf(out, "i%d", pos - 1 - nparam);
1353                 else if (pos < 1 + nparam + dim)
1354                         fprintf(out, "o%d", pos - 1 - nparam - in);
1355                 else
1356                         fprintf(out, "e%d", pos - 1 - nparam - dim);
1357         }
1358 }
1359
1360 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1361                                 int sign, FILE *out)
1362 {
1363         int i;
1364         int first;
1365         unsigned len = 1 + isl_basic_map_total_dim(bmap);
1366         isl_int v;
1367
1368         isl_int_init(v);
1369         for (i = 0, first = 1; i < len; ++i) {
1370                 if (isl_int_sgn(c[i]) * sign <= 0)
1371                         continue;
1372                 if (!first)
1373                         fprintf(out, " + ");
1374                 first = 0;
1375                 isl_int_abs(v, c[i]);
1376                 dump_term(bmap, v, i, out);
1377         }
1378         isl_int_clear(v);
1379         if (first)
1380                 fprintf(out, "0");
1381 }
1382
1383 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1384                                 const char *op, FILE *out, int indent)
1385 {
1386         int i;
1387
1388         fprintf(out, "%*s", indent, "");
1389
1390         dump_constraint_sign(bmap, c, 1, out);
1391         fprintf(out, " %s ", op);
1392         dump_constraint_sign(bmap, c, -1, out);
1393
1394         fprintf(out, "\n");
1395
1396         for (i = bmap->n_div; i < bmap->extra; ++i) {
1397                 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1398                         continue;
1399                 fprintf(out, "%*s", indent, "");
1400                 fprintf(out, "ERROR: unused div coefficient not zero\n");
1401                 abort();
1402         }
1403 }
1404
1405 static void dump_constraints(struct isl_basic_map *bmap,
1406                                 isl_int **c, unsigned n,
1407                                 const char *op, FILE *out, int indent)
1408 {
1409         int i;
1410
1411         for (i = 0; i < n; ++i)
1412                 dump_constraint(bmap, c[i], op, out, indent);
1413 }
1414
1415 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1416 {
1417         int j;
1418         int first = 1;
1419         unsigned total = isl_basic_map_total_dim(bmap);
1420
1421         for (j = 0; j < 1 + total; ++j) {
1422                 if (isl_int_is_zero(exp[j]))
1423                         continue;
1424                 if (!first && isl_int_is_pos(exp[j]))
1425                         fprintf(out, "+");
1426                 dump_term(bmap, exp[j], j, out);
1427                 first = 0;
1428         }
1429 }
1430
1431 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1432 {
1433         int i;
1434
1435         dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1436         dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1437
1438         for (i = 0; i < bmap->n_div; ++i) {
1439                 fprintf(out, "%*s", indent, "");
1440                 fprintf(out, "e%d = [(", i);
1441                 dump_affine(bmap, bmap->div[i]+1, out);
1442                 fprintf(out, ")/");
1443                 isl_int_print(out, bmap->div[i][0], 0);
1444                 fprintf(out, "]\n");
1445         }
1446 }
1447
1448 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1449 {
1450         if (!bset) {
1451                 fprintf(out, "null basic set\n");
1452                 return;
1453         }
1454
1455         fprintf(out, "%*s", indent, "");
1456         fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1457                         bset->ref, bset->dim->nparam, bset->dim->n_out,
1458                         bset->extra, bset->flags);
1459         dump((struct isl_basic_map *)bset, out, indent);
1460 }
1461
1462 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1463 {
1464         if (!bmap) {
1465                 fprintf(out, "null basic map\n");
1466                 return;
1467         }
1468
1469         fprintf(out, "%*s", indent, "");
1470         fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1471                         "flags: %x, n_name: %d\n",
1472                 bmap->ref,
1473                 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1474                 bmap->extra, bmap->flags, bmap->dim->n_name);
1475         dump(bmap, out, indent);
1476 }
1477
1478 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1479 {
1480         unsigned total;
1481         if (!bmap)
1482                 return -1;
1483         total = isl_basic_map_total_dim(bmap);
1484         isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1485         isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1486         isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1487         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1488         return 0;
1489 }
1490
1491 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1492 {
1493         struct isl_set *set;
1494
1495         if (!dim)
1496                 return NULL;
1497         isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1498         isl_assert(dim->ctx, n >= 0, return NULL);
1499         set = isl_alloc(dim->ctx, struct isl_set,
1500                         sizeof(struct isl_set) +
1501                         (n - 1) * sizeof(struct isl_basic_set *));
1502         if (!set)
1503                 goto error;
1504
1505         set->ctx = dim->ctx;
1506         isl_ctx_ref(set->ctx);
1507         set->ref = 1;
1508         set->size = n;
1509         set->n = 0;
1510         set->dim = dim;
1511         set->flags = flags;
1512         return set;
1513 error:
1514         isl_dim_free(dim);
1515         return NULL;
1516 }
1517
1518 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1519                 unsigned nparam, unsigned dim, int n, unsigned flags)
1520 {
1521         struct isl_set *set;
1522         struct isl_dim *dims;
1523
1524         dims = isl_dim_alloc(ctx, nparam, 0, dim);
1525         if (!dims)
1526                 return NULL;
1527
1528         set = isl_set_alloc_dim(dims, n, flags);
1529         return set;
1530 }
1531
1532 /* Make sure "map" has room for at least "n" more basic maps.
1533  */
1534 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1535 {
1536         int i;
1537         struct isl_map *grown = NULL;
1538
1539         if (!map)
1540                 return NULL;
1541         isl_assert(map->ctx, n >= 0, goto error);
1542         if (map->n + n <= map->size)
1543                 return map;
1544         grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1545         if (!grown)
1546                 goto error;
1547         for (i = 0; i < map->n; ++i) {
1548                 grown->p[i] = isl_basic_map_copy(map->p[i]);
1549                 if (!grown->p[i])
1550                         goto error;
1551                 grown->n++;
1552         }
1553         isl_map_free(map);
1554         return grown;
1555 error:
1556         isl_map_free(grown);
1557         isl_map_free(map);
1558         return NULL;
1559 }
1560
1561 /* Make sure "set" has room for at least "n" more basic sets.
1562  */
1563 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1564 {
1565         return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1566 }
1567
1568 struct isl_set *isl_set_dup(struct isl_set *set)
1569 {
1570         int i;
1571         struct isl_set *dup;
1572
1573         if (!set)
1574                 return NULL;
1575
1576         dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1577         if (!dup)
1578                 return NULL;
1579         for (i = 0; i < set->n; ++i)
1580                 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1581         return dup;
1582 }
1583
1584 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1585 {
1586         struct isl_set *set;
1587
1588         if (!bset)
1589                 return NULL;
1590
1591         set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1592         if (!set) {
1593                 isl_basic_set_free(bset);
1594                 return NULL;
1595         }
1596         return isl_set_add_basic_set(set, bset);
1597 }
1598
1599 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1600 {
1601         struct isl_map *map;
1602
1603         if (!bmap)
1604                 return NULL;
1605
1606         map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1607         if (!map) {
1608                 isl_basic_map_free(bmap);
1609                 return NULL;
1610         }
1611         return isl_map_add_basic_map(map, bmap);
1612 }
1613
1614 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1615                                                 __isl_take isl_basic_set *bset)
1616 {
1617         return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1618                                                 (struct isl_basic_map *)bset);
1619 }
1620
1621 void isl_set_free(struct isl_set *set)
1622 {
1623         int i;
1624
1625         if (!set)
1626                 return;
1627
1628         if (--set->ref > 0)
1629                 return;
1630
1631         isl_ctx_deref(set->ctx);
1632         for (i = 0; i < set->n; ++i)
1633                 isl_basic_set_free(set->p[i]);
1634         isl_dim_free(set->dim);
1635         free(set);
1636 }
1637
1638 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1639 {
1640         int i;
1641
1642         if (!set) {
1643                 fprintf(out, "null set\n");
1644                 return;
1645         }
1646
1647         fprintf(out, "%*s", indent, "");
1648         fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1649                         set->ref, set->n, set->dim->nparam, set->dim->n_out,
1650                         set->flags);
1651         for (i = 0; i < set->n; ++i) {
1652                 fprintf(out, "%*s", indent, "");
1653                 fprintf(out, "basic set %d:\n", i);
1654                 isl_basic_set_dump(set->p[i], out, indent+4);
1655         }
1656 }
1657
1658 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1659 {
1660         int i;
1661
1662         if (!map) {
1663                 fprintf(out, "null map\n");
1664                 return;
1665         }
1666
1667         fprintf(out, "%*s", indent, "");
1668         fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1669                      "flags: %x, n_name: %d\n",
1670                         map->ref, map->n, map->dim->nparam, map->dim->n_in,
1671                         map->dim->n_out, map->flags, map->dim->n_name);
1672         for (i = 0; i < map->n; ++i) {
1673                 fprintf(out, "%*s", indent, "");
1674                 fprintf(out, "basic map %d:\n", i);
1675                 isl_basic_map_dump(map->p[i], out, indent+4);
1676         }
1677 }
1678
1679 struct isl_basic_map *isl_basic_map_intersect_domain(
1680                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1681 {
1682         struct isl_basic_map *bmap_domain;
1683         struct isl_dim *dim;
1684
1685         if (!bmap || !bset)
1686                 goto error;
1687
1688         isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1689                                         bset->dim, isl_dim_param), goto error);
1690
1691         if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1692                 isl_assert(bset->ctx,
1693                     isl_basic_map_compatible_domain(bmap, bset), goto error);
1694
1695         bmap = isl_basic_map_cow(bmap);
1696         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1697                         bset->n_div, bset->n_eq, bset->n_ineq);
1698         if (!bmap)
1699                 goto error;
1700         dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1701         bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1702         bmap = add_constraints(bmap, bmap_domain, 0, 0);
1703
1704         bmap = isl_basic_map_simplify(bmap);
1705         return isl_basic_map_finalize(bmap);
1706 error:
1707         isl_basic_map_free(bmap);
1708         isl_basic_set_free(bset);
1709         return NULL;
1710 }
1711
1712 struct isl_basic_map *isl_basic_map_intersect_range(
1713                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1714 {
1715         struct isl_basic_map *bmap_range;
1716
1717         if (!bmap || !bset)
1718                 goto error;
1719
1720         isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1721                                         bset->dim, isl_dim_param), goto error);
1722
1723         if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1724                 isl_assert(bset->ctx,
1725                     isl_basic_map_compatible_range(bmap, bset), goto error);
1726
1727         bmap = isl_basic_map_cow(bmap);
1728         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1729                         bset->n_div, bset->n_eq, bset->n_ineq);
1730         if (!bmap)
1731                 goto error;
1732         bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1733         bmap = add_constraints(bmap, bmap_range, 0, 0);
1734
1735         bmap = isl_basic_map_simplify(bmap);
1736         return isl_basic_map_finalize(bmap);
1737 error:
1738         isl_basic_map_free(bmap);
1739         isl_basic_set_free(bset);
1740         return NULL;
1741 }
1742
1743 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1744 {
1745         int i;
1746         unsigned total;
1747         isl_int s;
1748
1749         total = 1 + isl_basic_map_total_dim(bmap);
1750         if (total != vec->size)
1751                 return -1;
1752
1753         isl_int_init(s);
1754
1755         for (i = 0; i < bmap->n_eq; ++i) {
1756                 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1757                 if (!isl_int_is_zero(s)) {
1758                         isl_int_clear(s);
1759                         return 0;
1760                 }
1761         }
1762
1763         for (i = 0; i < bmap->n_ineq; ++i) {
1764                 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1765                 if (isl_int_is_neg(s)) {
1766                         isl_int_clear(s);
1767                         return 0;
1768                 }
1769         }
1770
1771         isl_int_clear(s);
1772
1773         return 1;
1774 }
1775
1776 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1777 {
1778         return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
1779 }
1780
1781 struct isl_basic_map *isl_basic_map_intersect(
1782                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1783 {
1784         struct isl_vec *sample = NULL;
1785
1786         if (!bmap1 || !bmap2)
1787                 goto error;
1788
1789         isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1790                                      bmap2->dim, isl_dim_param), goto error);
1791         if (isl_dim_total(bmap1->dim) ==
1792                                 isl_dim_size(bmap1->dim, isl_dim_param) &&
1793             isl_dim_total(bmap2->dim) !=
1794                                 isl_dim_size(bmap2->dim, isl_dim_param))
1795                 return isl_basic_map_intersect(bmap2, bmap1);
1796
1797         if (isl_dim_total(bmap2->dim) !=
1798                                         isl_dim_size(bmap2->dim, isl_dim_param))
1799                 isl_assert(bmap1->ctx,
1800                             isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1801
1802         if (bmap1->sample &&
1803             isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
1804             isl_basic_map_contains(bmap2, bmap1->sample) > 0)
1805                 sample = isl_vec_copy(bmap1->sample);
1806         else if (bmap2->sample &&
1807             isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
1808             isl_basic_map_contains(bmap2, bmap2->sample) > 0)
1809                 sample = isl_vec_copy(bmap2->sample);
1810
1811         bmap1 = isl_basic_map_cow(bmap1);
1812         bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1813                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1814         if (!bmap1)
1815                 goto error;
1816         bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1817
1818         if (sample) {
1819                 isl_vec_free(bmap1->sample);
1820                 bmap1->sample = sample;
1821         }
1822
1823         bmap1 = isl_basic_map_simplify(bmap1);
1824         return isl_basic_map_finalize(bmap1);
1825 error:
1826         if (sample)
1827                 isl_vec_free(sample);
1828         isl_basic_map_free(bmap1);
1829         isl_basic_map_free(bmap2);
1830         return NULL;
1831 }
1832
1833 struct isl_basic_set *isl_basic_set_intersect(
1834                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1835 {
1836         return (struct isl_basic_set *)
1837                 isl_basic_map_intersect(
1838                         (struct isl_basic_map *)bset1,
1839                         (struct isl_basic_map *)bset2);
1840 }
1841
1842 /* Special case of isl_map_intersect, where both map1 and map2
1843  * are convex, without any divs and such that either map1 or map2
1844  * contains a single constraint.  This constraint is then simply
1845  * added to the other map.
1846  */
1847 static __isl_give isl_map *map_intersect_add_constraint(
1848         __isl_take isl_map *map1, __isl_take isl_map *map2)
1849 {
1850         struct isl_basic_map *bmap1;
1851         struct isl_basic_map *bmap2;
1852
1853         isl_assert(map1->ctx, map1->n == 1, goto error);
1854         isl_assert(map2->ctx, map1->n == 1, goto error);
1855         isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
1856         isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
1857
1858         if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
1859                 return isl_map_intersect(map2, map1);
1860
1861         isl_assert(map2->ctx,
1862                     map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
1863
1864         map1 = isl_map_cow(map1);
1865         if (!map1)
1866                 goto error;
1867         map1->p[0] = isl_basic_map_cow(map1->p[0]);
1868         if (map2->p[0]->n_eq == 1)
1869                 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
1870         else
1871                 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
1872                                                         map2->p[0]->ineq[0]);
1873
1874         map1->p[0] = isl_basic_map_simplify(map1->p[0]);
1875         map1->p[0] = isl_basic_map_finalize(map1->p[0]);
1876         if (!map1->p[0])
1877                 goto error;
1878
1879         isl_map_free(map2);
1880
1881         return map1;
1882 error:
1883         isl_map_free(map1);
1884         isl_map_free(map2);
1885         return NULL;
1886 }
1887
1888 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1889 {
1890         unsigned flags = 0;
1891         struct isl_map *result;
1892         int i, j;
1893
1894         if (!map1 || !map2)
1895                 goto error;
1896
1897         if (map1->n == 1 && map2->n == 1 &&
1898             map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
1899             isl_dim_equal(map1->dim, map2->dim) &&
1900             (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
1901              map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
1902                 return map_intersect_add_constraint(map1, map2);
1903         isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1904                                          map2->dim, isl_dim_param), goto error);
1905         if (isl_dim_total(map1->dim) ==
1906                                 isl_dim_size(map1->dim, isl_dim_param) &&
1907             isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1908                 return isl_map_intersect(map2, map1);
1909
1910         if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1911                 isl_assert(map1->ctx,
1912                             isl_dim_equal(map1->dim, map2->dim), goto error);
1913
1914         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1915             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1916                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1917
1918         result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1919                                 map1->n * map2->n, flags);
1920         if (!result)
1921                 goto error;
1922         for (i = 0; i < map1->n; ++i)
1923                 for (j = 0; j < map2->n; ++j) {
1924                         struct isl_basic_map *part;
1925                         part = isl_basic_map_intersect(
1926                                     isl_basic_map_copy(map1->p[i]),
1927                                     isl_basic_map_copy(map2->p[j]));
1928                         if (isl_basic_map_is_empty(part))
1929                                 isl_basic_map_free(part);
1930                         else
1931                                 result = isl_map_add_basic_map(result, part);
1932                         if (!result)
1933                                 goto error;
1934                 }
1935         isl_map_free(map1);
1936         isl_map_free(map2);
1937         return result;
1938 error:
1939         isl_map_free(map1);
1940         isl_map_free(map2);
1941         return NULL;
1942 }
1943
1944 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1945 {
1946         return (struct isl_set *)
1947                 isl_map_intersect((struct isl_map *)set1,
1948                                   (struct isl_map *)set2);
1949 }
1950
1951 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1952 {
1953         struct isl_dim *dim;
1954         struct isl_basic_set *bset;
1955         unsigned in;
1956
1957         if (!bmap)
1958                 return NULL;
1959         bmap = isl_basic_map_cow(bmap);
1960         if (!bmap)
1961                 return NULL;
1962         dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1963         in = isl_basic_map_n_in(bmap);
1964         bset = isl_basic_set_from_basic_map(bmap);
1965         bset = isl_basic_set_swap_vars(bset, in);
1966         return isl_basic_map_from_basic_set(bset, dim);
1967 }
1968
1969 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
1970                 enum isl_dim_type type, unsigned n)
1971 {
1972         struct isl_dim *res_dim;
1973         struct isl_basic_map *res;
1974         struct isl_dim_map *dim_map;
1975         unsigned total, pos;
1976
1977         if (n == 0)
1978                 return bmap;
1979
1980         if (!bmap)
1981                 return NULL;
1982
1983         res_dim = isl_dim_add(isl_basic_map_get_dim(bmap), type, n);
1984
1985         total = isl_basic_map_total_dim(bmap) + n;
1986         dim_map = isl_dim_map_alloc(bmap->ctx, total);
1987         pos = 0;
1988         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos);
1989         pos += isl_dim_size(res_dim, isl_dim_param);
1990         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos);
1991         pos += isl_dim_size(res_dim, isl_dim_in);
1992         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos);
1993         pos += isl_dim_size(res_dim, isl_dim_out);
1994         isl_dim_map_div(dim_map, bmap, pos);
1995
1996         res = isl_basic_map_alloc_dim(res_dim,
1997                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
1998         res = add_constraints_dim_map(res, bmap, dim_map);
1999         res = isl_basic_map_simplify(res);
2000         return isl_basic_map_finalize(res);
2001 }
2002
2003 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2004                 enum isl_dim_type type, unsigned n)
2005 {
2006         if (!bset)
2007                 return NULL;
2008         isl_assert(bset->ctx, type != isl_dim_in, goto error);
2009         return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2010 error:
2011         isl_basic_set_free(bset);
2012         return NULL;
2013 }
2014
2015 __isl_give isl_map *isl_map_add(__isl_take isl_map *map,
2016                 enum isl_dim_type type, unsigned n)
2017 {
2018         int i;
2019
2020         if (n == 0)
2021                 return map;
2022
2023         map = isl_map_cow(map);
2024         if (!map)
2025                 return NULL;
2026
2027         map->dim = isl_dim_add(map->dim, type, n);
2028         if (!map->dim)
2029                 goto error;
2030
2031         for (i = 0; i < map->n; ++i) {
2032                 map->p[i] = isl_basic_map_add(map->p[i], type, n);
2033                 if (!map->p[i])
2034                         goto error;
2035         }
2036
2037         return map;
2038 error:
2039         isl_map_free(map);
2040         return NULL;
2041 }
2042
2043 __isl_give isl_set *isl_set_add(__isl_take isl_set *set,
2044                 enum isl_dim_type type, unsigned n)
2045 {
2046         if (!set)
2047                 return NULL;
2048         isl_assert(set->ctx, type != isl_dim_in, goto error);
2049         return (isl_set *)isl_map_add((isl_map *)set, type, n);
2050 error:
2051         isl_set_free(set);
2052         return NULL;
2053 }
2054
2055 /* Move the specified dimensions to the last columns right before
2056  * the divs.  Don't change the dimension specification of bmap.
2057  * That's the responsibility of the caller.
2058  */
2059 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2060         enum isl_dim_type type, unsigned first, unsigned n)
2061 {
2062         struct isl_dim_map *dim_map;
2063         struct isl_basic_map *res;
2064         enum isl_dim_type t;
2065         unsigned total, off;
2066
2067         if (!bmap)
2068                 return NULL;
2069         if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2070                 return bmap;
2071
2072         total = isl_basic_map_total_dim(bmap);
2073         dim_map = isl_dim_map_alloc(bmap->ctx, total);
2074
2075         off = 0;
2076         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2077                 unsigned size = isl_dim_size(bmap->dim, t);
2078                 if (t == type) {
2079                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2080                                             0, first, off);
2081                         off += first;
2082                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2083                                             first, n, total - bmap->n_div - n);
2084                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2085                                             first + n, size - (first + n), off);
2086                         off += size - (first + n);
2087                 } else {
2088                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
2089                         off += size;
2090                 }
2091         }
2092         isl_dim_map_div(dim_map, bmap, off + n);
2093
2094         res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2095                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
2096         res = add_constraints_dim_map(res, bmap, dim_map);
2097         return res;
2098 }
2099
2100 /* Turn the n dimensions of type type, starting at first
2101  * into existentially quantified variables.
2102  */
2103 __isl_give isl_basic_map *isl_basic_map_project_out(
2104                 __isl_take isl_basic_map *bmap,
2105                 enum isl_dim_type type, unsigned first, unsigned n)
2106 {
2107         int i;
2108         size_t row_size;
2109         isl_int **new_div;
2110         isl_int *old;
2111
2112         if (n == 0)
2113                 return bmap;
2114
2115         if (!bmap)
2116                 return NULL;
2117
2118         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2119                 return isl_basic_map_remove(bmap, type, first, n);
2120
2121         isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2122                         goto error);
2123
2124         bmap = move_last(bmap, type, first, n);
2125         bmap = isl_basic_map_cow(bmap);
2126
2127         row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2128         old = bmap->block2.data;
2129         bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2130                                         (bmap->extra + n) * (1 + row_size));
2131         if (!bmap->block2.data)
2132                 goto error;
2133         new_div = isl_alloc_array(ctx, isl_int *, bmap->extra + n);
2134         if (!new_div)
2135                 goto error;
2136         for (i = 0; i < n; ++i) {
2137                 new_div[i] = bmap->block2.data +
2138                                 (bmap->extra + i) * (1 + row_size);
2139                 isl_seq_clr(new_div[i], 1 + row_size);
2140         }
2141         for (i = 0; i < bmap->extra; ++i)
2142                 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2143         free(bmap->div);
2144         bmap->div = new_div;
2145         bmap->n_div += n;
2146         bmap->extra += n;
2147
2148         bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2149         if (!bmap->dim)
2150                 goto error;
2151         bmap = isl_basic_map_simplify(bmap);
2152         bmap = isl_basic_map_drop_redundant_divs(bmap);
2153         return isl_basic_map_finalize(bmap);
2154 error:
2155         isl_basic_map_free(bmap);
2156         return NULL;
2157 }
2158
2159 /* Turn the n dimensions of type type, starting at first
2160  * into existentially quantified variables.
2161  */
2162 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2163                 enum isl_dim_type type, unsigned first, unsigned n)
2164 {
2165         return (isl_basic_set *)isl_basic_map_project_out(
2166                         (isl_basic_map *)bset, type, first, n);
2167 }
2168
2169 /* Turn the n dimensions of type type, starting at first
2170  * into existentially quantified variables.
2171  */
2172 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2173                 enum isl_dim_type type, unsigned first, unsigned n)
2174 {
2175         int i;
2176
2177         if (!map)
2178                 return NULL;
2179
2180         if (n == 0)
2181                 return map;
2182
2183         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2184
2185         map = isl_map_cow(map);
2186         if (!map)
2187                 return NULL;
2188
2189         map->dim = isl_dim_drop(map->dim, type, first, n);
2190         if (!map->dim)
2191                 goto error;
2192
2193         for (i = 0; i < map->n; ++i) {
2194                 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2195                 if (!map->p[i])
2196                         goto error;
2197         }
2198
2199         return map;
2200 error:
2201         isl_map_free(map);
2202         return map;
2203 }
2204
2205 /* Turn the n dimensions of type type, starting at first
2206  * into existentially quantified variables.
2207  */
2208 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2209                 enum isl_dim_type type, unsigned first, unsigned n)
2210 {
2211         return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2212 }
2213
2214 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2215 {
2216         int i, j;
2217
2218         for (i = 0; i < n; ++i) {
2219                 j = isl_basic_map_alloc_div(bmap);
2220                 if (j < 0)
2221                         goto error;
2222                 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2223         }
2224         return bmap;
2225 error:
2226         isl_basic_map_free(bmap);
2227         return NULL;
2228 }
2229
2230 struct isl_basic_map *isl_basic_map_apply_range(
2231                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2232 {
2233         struct isl_dim *dim_result = NULL;
2234         struct isl_basic_map *bmap;
2235         unsigned n_in, n_out, n, nparam, total, pos;
2236         struct isl_dim_map *dim_map1, *dim_map2;
2237
2238         if (!bmap1 || !bmap2)
2239                 goto error;
2240
2241         dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2242                                   isl_dim_copy(bmap2->dim));
2243
2244         n_in = isl_basic_map_n_in(bmap1);
2245         n_out = isl_basic_map_n_out(bmap2);
2246         n = isl_basic_map_n_out(bmap1);
2247         nparam = isl_basic_map_n_param(bmap1);
2248
2249         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2250         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2251         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2252         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2253         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2254         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2255         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2256         isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2257         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2258         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2259         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2260
2261         bmap = isl_basic_map_alloc_dim(dim_result,
2262                         bmap1->n_div + bmap2->n_div + n,
2263                         bmap1->n_eq + bmap2->n_eq,
2264                         bmap1->n_ineq + bmap2->n_ineq);
2265         bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2266         bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2267         bmap = add_divs(bmap, n);
2268         bmap = isl_basic_map_simplify(bmap);
2269         bmap = isl_basic_map_drop_redundant_divs(bmap);
2270         return isl_basic_map_finalize(bmap);
2271 error:
2272         isl_basic_map_free(bmap1);
2273         isl_basic_map_free(bmap2);
2274         return NULL;
2275 }
2276
2277 struct isl_basic_set *isl_basic_set_apply(
2278                 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2279 {
2280         if (!bset || !bmap)
2281                 goto error;
2282
2283         isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2284                     goto error);
2285
2286         return (struct isl_basic_set *)
2287                 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2288 error:
2289         isl_basic_set_free(bset);
2290         isl_basic_map_free(bmap);
2291         return NULL;
2292 }
2293
2294 struct isl_basic_map *isl_basic_map_apply_domain(
2295                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2296 {
2297         if (!bmap1 || !bmap2)
2298                 goto error;
2299
2300         isl_assert(bmap1->ctx,
2301             isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2302         isl_assert(bmap1->ctx,
2303             isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2304             goto error);
2305
2306         bmap1 = isl_basic_map_reverse(bmap1);
2307         bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2308         return isl_basic_map_reverse(bmap1);
2309 error:
2310         isl_basic_map_free(bmap1);
2311         isl_basic_map_free(bmap2);
2312         return NULL;
2313 }
2314
2315 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2316  * A \cap B -> f(A) + f(B)
2317  */
2318 struct isl_basic_map *isl_basic_map_sum(
2319                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2320 {
2321         unsigned n_in, n_out, nparam, total, pos;
2322         struct isl_basic_map *bmap = NULL;
2323         struct isl_dim_map *dim_map1, *dim_map2;
2324         int i;
2325
2326         if (!bmap1 || !bmap2)
2327                 goto error;
2328
2329         isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2330                 goto error);
2331
2332         nparam = isl_basic_map_n_param(bmap1);
2333         n_in = isl_basic_map_n_in(bmap1);
2334         n_out = isl_basic_map_n_out(bmap1);
2335
2336         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2337         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2338         dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2339         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2340         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2341         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2342         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2343         isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2344         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2345         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2346         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2347
2348         bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2349                         bmap1->n_div + bmap2->n_div + 2 * n_out,
2350                         bmap1->n_eq + bmap2->n_eq + n_out,
2351                         bmap1->n_ineq + bmap2->n_ineq);
2352         for (i = 0; i < n_out; ++i) {
2353                 int j = isl_basic_map_alloc_equality(bmap);
2354                 if (j < 0)
2355                         goto error;
2356                 isl_seq_clr(bmap->eq[j], 1+total);
2357                 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2358                 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2359                 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2360         }
2361         bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2362         bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2363         bmap = add_divs(bmap, 2 * n_out);
2364
2365         bmap = isl_basic_map_simplify(bmap);
2366         return isl_basic_map_finalize(bmap);
2367 error:
2368         isl_basic_map_free(bmap);
2369         isl_basic_map_free(bmap1);
2370         isl_basic_map_free(bmap2);
2371         return NULL;
2372 }
2373
2374 /* Given two maps A -> f(A) and B -> g(B), construct a map
2375  * A \cap B -> f(A) + f(B)
2376  */
2377 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2378 {
2379         struct isl_map *result;
2380         int i, j;
2381
2382         if (!map1 || !map2)
2383                 goto error;
2384
2385         isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2386
2387         result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2388                                 map1->n * map2->n, 0);
2389         if (!result)
2390                 goto error;
2391         for (i = 0; i < map1->n; ++i)
2392                 for (j = 0; j < map2->n; ++j) {
2393                         struct isl_basic_map *part;
2394                         part = isl_basic_map_sum(
2395                                     isl_basic_map_copy(map1->p[i]),
2396                                     isl_basic_map_copy(map2->p[j]));
2397                         if (isl_basic_map_is_empty(part))
2398                                 isl_basic_map_free(part);
2399                         else
2400                                 result = isl_map_add_basic_map(result, part);
2401                         if (!result)
2402                                 goto error;
2403                 }
2404         isl_map_free(map1);
2405         isl_map_free(map2);
2406         return result;
2407 error:
2408         isl_map_free(map1);
2409         isl_map_free(map2);
2410         return NULL;
2411 }
2412
2413 /* Given a basic map A -> f(A), construct A -> -f(A).
2414  */
2415 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2416 {
2417         int i, j;
2418         unsigned off, n;
2419
2420         bmap = isl_basic_map_cow(bmap);
2421         if (!bmap)
2422                 return NULL;
2423
2424         n = isl_basic_map_dim(bmap, isl_dim_out);
2425         off = isl_basic_map_offset(bmap, isl_dim_out);
2426         for (i = 0; i < bmap->n_eq; ++i)
2427                 for (j = 0; j < n; ++j)
2428                         isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2429         for (i = 0; i < bmap->n_ineq; ++i)
2430                 for (j = 0; j < n; ++j)
2431                         isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2432         for (i = 0; i < bmap->n_div; ++i)
2433                 for (j = 0; j < n; ++j)
2434                         isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2435         return isl_basic_map_finalize(bmap);
2436 }
2437
2438 /* Given a map A -> f(A), construct A -> -f(A).
2439  */
2440 struct isl_map *isl_map_neg(struct isl_map *map)
2441 {
2442         int i;
2443
2444         map = isl_map_cow(map);
2445         if (!map)
2446                 return NULL;
2447
2448         for (i = 0; i < map->n; ++i) {
2449                 map->p[i] = isl_basic_map_neg(map->p[i]);
2450                 if (!map->p[i])
2451                         goto error;
2452         }
2453
2454         return map;
2455 error:
2456         isl_map_free(map);
2457         return NULL;
2458 }
2459
2460 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2461  * A -> floor(f(A)/d).
2462  */
2463 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2464                 isl_int d)
2465 {
2466         unsigned n_in, n_out, nparam, total, pos;
2467         struct isl_basic_map *result = NULL;
2468         struct isl_dim_map *dim_map;
2469         int i;
2470
2471         if (!bmap)
2472                 return NULL;
2473
2474         nparam = isl_basic_map_n_param(bmap);
2475         n_in = isl_basic_map_n_in(bmap);
2476         n_out = isl_basic_map_n_out(bmap);
2477
2478         total = nparam + n_in + n_out + bmap->n_div + n_out;
2479         dim_map = isl_dim_map_alloc(bmap->ctx, total);
2480         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2481         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2482         isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2483         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2484
2485         result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2486                         bmap->n_div + n_out,
2487                         bmap->n_eq, bmap->n_ineq + 2 * n_out);
2488         result = add_constraints_dim_map(result, bmap, dim_map);
2489         result = add_divs(result, n_out);
2490         for (i = 0; i < n_out; ++i) {
2491                 int j;
2492                 j = isl_basic_map_alloc_inequality(result);
2493                 if (j < 0)
2494                         goto error;
2495                 isl_seq_clr(result->ineq[j], 1+total);
2496                 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2497                 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2498                 j = isl_basic_map_alloc_inequality(result);
2499                 if (j < 0)
2500                         goto error;
2501                 isl_seq_clr(result->ineq[j], 1+total);
2502                 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2503                 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2504                 isl_int_sub_ui(result->ineq[j][0], d, 1);
2505         }
2506
2507         result = isl_basic_map_simplify(result);
2508         return isl_basic_map_finalize(result);
2509 error:
2510         isl_basic_map_free(result);
2511         return NULL;
2512 }
2513
2514 /* Given a map A -> f(A) and an integer d, construct a map
2515  * A -> floor(f(A)/d).
2516  */
2517 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2518 {
2519         int i;
2520
2521         map = isl_map_cow(map);
2522         if (!map)
2523                 return NULL;
2524
2525         ISL_F_CLR(map, ISL_MAP_DISJOINT);
2526         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2527         for (i = 0; i < map->n; ++i) {
2528                 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2529                 if (!map->p[i])
2530                         goto error;
2531         }
2532
2533         return map;
2534 error:
2535         isl_map_free(map);
2536         return NULL;
2537 }
2538
2539 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2540 {
2541         int i;
2542         unsigned nparam;
2543         unsigned n_in;
2544
2545         i = isl_basic_map_alloc_equality(bmap);
2546         if (i < 0)
2547                 goto error;
2548         nparam = isl_basic_map_n_param(bmap);
2549         n_in = isl_basic_map_n_in(bmap);
2550         isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2551         isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2552         isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2553         return isl_basic_map_finalize(bmap);
2554 error:
2555         isl_basic_map_free(bmap);
2556         return NULL;
2557 }
2558
2559 /* Add a constraints to "bmap" expressing i_pos < o_pos
2560  */
2561 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2562 {
2563         int i;
2564         unsigned nparam;
2565         unsigned n_in;
2566
2567         i = isl_basic_map_alloc_inequality(bmap);
2568         if (i < 0)
2569                 goto error;
2570         nparam = isl_basic_map_n_param(bmap);
2571         n_in = isl_basic_map_n_in(bmap);
2572         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2573         isl_int_set_si(bmap->ineq[i][0], -1);
2574         isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2575         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2576         return isl_basic_map_finalize(bmap);
2577 error:
2578         isl_basic_map_free(bmap);
2579         return NULL;
2580 }
2581
2582 /* Add a constraints to "bmap" expressing i_pos > o_pos
2583  */
2584 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2585 {
2586         int i;
2587         unsigned nparam;
2588         unsigned n_in;
2589
2590         i = isl_basic_map_alloc_inequality(bmap);
2591         if (i < 0)
2592                 goto error;
2593         nparam = isl_basic_map_n_param(bmap);
2594         n_in = isl_basic_map_n_in(bmap);
2595         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2596         isl_int_set_si(bmap->ineq[i][0], -1);
2597         isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2598         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2599         return isl_basic_map_finalize(bmap);
2600 error:
2601         isl_basic_map_free(bmap);
2602         return NULL;
2603 }
2604
2605 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2606 {
2607         int i;
2608         struct isl_basic_map *bmap;
2609         bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2610         if (!bmap)
2611                 return NULL;
2612         for (i = 0; i < n_equal && bmap; ++i)
2613                 bmap = var_equal(bmap, i);
2614         return isl_basic_map_finalize(bmap);
2615 }
2616
2617 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos < o_pos
2618  */
2619 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2620 {
2621         int i;
2622         struct isl_basic_map *bmap;
2623         bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2624         if (!bmap)
2625                 return NULL;
2626         for (i = 0; i < pos && bmap; ++i)
2627                 bmap = var_equal(bmap, i);
2628         if (bmap)
2629                 bmap = var_less(bmap, pos);
2630         return isl_basic_map_finalize(bmap);
2631 }
2632
2633 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2634  */
2635 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2636 {
2637         int i;
2638         struct isl_basic_map *bmap;
2639         bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2640         if (!bmap)
2641                 return NULL;
2642         for (i = 0; i < pos && bmap; ++i)
2643                 bmap = var_equal(bmap, i);
2644         if (bmap)
2645                 bmap = var_more(bmap, pos);
2646         return isl_basic_map_finalize(bmap);
2647 }
2648
2649 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
2650 {
2651         struct isl_map *map;
2652         unsigned dim;
2653         int i;
2654
2655         if (!dims)
2656                 return NULL;
2657         dim = dims->n_out;
2658         map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2659
2660         for (i = 0; i < dim; ++i)
2661                 map = isl_map_add_basic_map(map,
2662                                   isl_basic_map_less_at(isl_dim_copy(dims), i));
2663         if (equal)
2664                 map = isl_map_add_basic_map(map,
2665                                   isl_basic_map_equal(isl_dim_copy(dims), dim));
2666
2667         isl_dim_free(dims);
2668         return map;
2669 }
2670
2671 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
2672 {
2673         return map_lex_lte(isl_dim_map(set_dim), 0);
2674 }
2675
2676 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
2677 {
2678         return map_lex_lte(isl_dim_map(set_dim), 1);
2679 }
2680
2681 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
2682 {
2683         struct isl_map *map;
2684         unsigned dim;
2685         int i;
2686
2687         if (!dims)
2688                 return NULL;
2689         dim = dims->n_out;
2690         map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2691
2692         for (i = 0; i < dim; ++i)
2693                 map = isl_map_add_basic_map(map,
2694                                   isl_basic_map_more_at(isl_dim_copy(dims), i));
2695         if (equal)
2696                 map = isl_map_add_basic_map(map,
2697                                   isl_basic_map_equal(isl_dim_copy(dims), dim));
2698
2699         isl_dim_free(dims);
2700         return map;
2701 }
2702
2703 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
2704 {
2705         return map_lex_gte(isl_dim_map(set_dim), 0);
2706 }
2707
2708 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
2709 {
2710         return map_lex_gte(isl_dim_map(set_dim), 1);
2711 }
2712
2713 struct isl_basic_map *isl_basic_map_from_basic_set(
2714                 struct isl_basic_set *bset, struct isl_dim *dim)
2715 {
2716         struct isl_basic_map *bmap;
2717
2718         bset = isl_basic_set_cow(bset);
2719         if (!bset || !dim)
2720                 goto error;
2721
2722         isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2723         isl_dim_free(bset->dim);
2724         bmap = (struct isl_basic_map *) bset;
2725         bmap->dim = dim;
2726         return isl_basic_map_finalize(bmap);
2727 error:
2728         isl_basic_set_free(bset);
2729         isl_dim_free(dim);
2730         return NULL;
2731 }
2732
2733 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2734 {
2735         if (!bmap)
2736                 goto error;
2737         if (bmap->dim->n_in == 0)
2738                 return (struct isl_basic_set *)bmap;
2739         bmap = isl_basic_map_cow(bmap);
2740         if (!bmap)
2741                 goto error;
2742         bmap->dim = isl_dim_cow(bmap->dim);
2743         if (!bmap->dim)
2744                 goto error;
2745         bmap->dim->n_out += bmap->dim->n_in;
2746         bmap->dim->n_in = 0;
2747         bmap = isl_basic_map_finalize(bmap);
2748         return (struct isl_basic_set *)bmap;
2749 error:
2750         isl_basic_map_free(bmap);
2751         return NULL;
2752 }
2753
2754 /* For a div d = floor(f/m), add the constraints
2755  *
2756  *              f - m d >= 0
2757  *              -(f-(n-1)) + m d >= 0
2758  *
2759  * Note that the second constraint is the negation of
2760  *
2761  *              f - m d >= n
2762  */
2763 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2764 {
2765         int i, j;
2766         unsigned total = isl_basic_map_total_dim(bmap);
2767         unsigned div_pos = 1 + total - bmap->n_div + div;
2768
2769         i = isl_basic_map_alloc_inequality(bmap);
2770         if (i < 0)
2771                 return -1;
2772         isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2773         isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2774
2775         j = isl_basic_map_alloc_inequality(bmap);
2776         if (j < 0)
2777                 return -1;
2778         isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2779         isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2780         isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2781         return j;
2782 }
2783
2784 struct isl_basic_set *isl_basic_map_underlying_set(
2785                 struct isl_basic_map *bmap)
2786 {
2787         if (!bmap)
2788                 goto error;
2789         if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2790                 return (struct isl_basic_set *)bmap;
2791         bmap = isl_basic_map_cow(bmap);
2792         if (!bmap)
2793                 goto error;
2794         bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2795         if (!bmap->dim)
2796                 goto error;
2797         bmap->extra -= bmap->n_div;
2798         bmap->n_div = 0;
2799         bmap = isl_basic_map_finalize(bmap);
2800         return (struct isl_basic_set *)bmap;
2801 error:
2802         return NULL;
2803 }
2804
2805 __isl_give isl_basic_set *isl_basic_set_underlying_set(
2806                 __isl_take isl_basic_set *bset)
2807 {
2808         return isl_basic_map_underlying_set((isl_basic_map *)bset);
2809 }
2810
2811 struct isl_basic_map *isl_basic_map_overlying_set(
2812         struct isl_basic_set *bset, struct isl_basic_map *like)
2813 {
2814         struct isl_basic_map *bmap;
2815         struct isl_ctx *ctx;
2816         unsigned total;
2817         int i;
2818
2819         if (!bset || !like)
2820                 goto error;
2821         ctx = bset->ctx;
2822         isl_assert(ctx, bset->n_div == 0, goto error);
2823         isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
2824         isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
2825                         goto error);
2826         if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
2827                 isl_basic_map_free(like);
2828                 return (struct isl_basic_map *)bset;
2829         }
2830         bset = isl_basic_set_cow(bset);
2831         if (!bset)
2832                 goto error;
2833         total = bset->dim->n_out + bset->extra;
2834         bmap = (struct isl_basic_map *)bset;
2835         isl_dim_free(bmap->dim);
2836         bmap->dim = isl_dim_copy(like->dim);
2837         if (!bmap->dim)
2838                 goto error;
2839         bmap->n_div = like->n_div;
2840         bmap->extra += like->n_div;
2841         if (bmap->extra) {
2842                 unsigned ltotal;
2843                 ltotal = total - bmap->extra + like->extra;
2844                 if (ltotal > total)
2845                         ltotal = total;
2846                 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
2847                                         bmap->extra * (1 + 1 + total));
2848                 if (isl_blk_is_error(bmap->block2))
2849                         goto error;
2850                 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
2851                                                 bmap->extra);
2852                 if (!bmap->div)
2853                         goto error;
2854                 for (i = 0; i < bmap->extra; ++i)
2855                         bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
2856                 for (i = 0; i < like->n_div; ++i) {
2857                         isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
2858                         isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
2859                 }
2860                 bmap = isl_basic_map_extend_constraints(bmap, 
2861                                                         0, 2 * like->n_div);
2862                 for (i = 0; i < like->n_div; ++i) {
2863                         if (isl_int_is_zero(bmap->div[i][0]))
2864                                 continue;
2865                         if (isl_basic_map_add_div_constraints(bmap, i) < 0)
2866                                 goto error;
2867                 }
2868         }
2869         isl_basic_map_free(like);
2870         bmap = isl_basic_map_simplify(bmap);
2871         bmap = isl_basic_map_finalize(bmap);
2872         return bmap;
2873 error:
2874         isl_basic_map_free(like);
2875         isl_basic_set_free(bset);
2876         return NULL;
2877 }
2878
2879 struct isl_basic_set *isl_basic_set_from_underlying_set(
2880         struct isl_basic_set *bset, struct isl_basic_set *like)
2881 {
2882         return (struct isl_basic_set *)
2883                 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
2884 }
2885
2886 struct isl_set *isl_set_from_underlying_set(
2887         struct isl_set *set, struct isl_basic_set *like)
2888 {
2889         int i;
2890
2891         if (!set || !like)
2892                 goto error;
2893         isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
2894                     goto error);
2895         if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
2896                 isl_basic_set_free(like);
2897                 return set;
2898         }
2899         set = isl_set_cow(set);
2900         if (!set)
2901                 goto error;
2902         for (i = 0; i < set->n; ++i) {
2903                 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
2904                                                       isl_basic_set_copy(like));
2905                 if (!set->p[i])
2906                         goto error;
2907         }
2908         isl_dim_free(set->dim);
2909         set->dim = isl_dim_copy(like->dim);
2910         if (!set->dim)
2911                 goto error;
2912         isl_basic_set_free(like);
2913         return set;
2914 error:
2915         isl_basic_set_free(like);
2916         isl_set_free(set);
2917         return NULL;
2918 }
2919
2920 struct isl_set *isl_map_underlying_set(struct isl_map *map)
2921 {
2922         int i;
2923
2924         map = isl_map_cow(map);
2925         if (!map)
2926                 return NULL;
2927         map->dim = isl_dim_cow(map->dim);
2928         if (!map->dim)
2929                 goto error;
2930
2931         for (i = 1; i < map->n; ++i)
2932                 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
2933                                 goto error);
2934         for (i = 0; i < map->n; ++i) {
2935                 map->p[i] = (struct isl_basic_map *)
2936                                 isl_basic_map_underlying_set(map->p[i]);
2937                 if (!map->p[i])
2938                         goto error;
2939         }
2940         if (map->n == 0)
2941                 map->dim = isl_dim_underlying(map->dim, 0);
2942         else {
2943                 isl_dim_free(map->dim);
2944                 map->dim = isl_dim_copy(map->p[0]->dim);
2945         }
2946         if (!map->dim)
2947                 goto error;
2948         return (struct isl_set *)map;
2949 error:
2950         isl_map_free(map);
2951         return NULL;
2952 }
2953
2954 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
2955 {
2956         return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
2957 }
2958
2959 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
2960 {
2961         struct isl_basic_set *domain;
2962         unsigned n_in;
2963         unsigned n_out;
2964         if (!bmap)
2965                 return NULL;
2966         n_in = isl_basic_map_n_in(bmap);
2967         n_out = isl_basic_map_n_out(bmap);
2968         domain = isl_basic_set_from_basic_map(bmap);
2969         return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
2970 }
2971
2972 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
2973 {
2974         return isl_basic_map_domain(isl_basic_map_reverse(bmap));
2975 }
2976
2977 struct isl_set *isl_map_range(struct isl_map *map)
2978 {
2979         int i;
2980         struct isl_set *set;
2981
2982         if (!map)
2983                 goto error;
2984         map = isl_map_cow(map);
2985         if (!map)
2986                 goto error;
2987
2988         set = (struct isl_set *) map;
2989         if (set->dim->n_in != 0) {
2990                 set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
2991                 if (!set->dim)
2992                         goto error;
2993         }
2994         for (i = 0; i < map->n; ++i) {
2995                 set->p[i] = isl_basic_map_range(map->p[i]);
2996                 if (!set->p[i])
2997                         goto error;
2998         }
2999         ISL_F_CLR(set, ISL_MAP_DISJOINT);
3000         ISL_F_CLR(set, ISL_SET_NORMALIZED);
3001         return set;
3002 error:
3003         isl_map_free(map);
3004         return NULL;
3005 }
3006
3007 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3008 {
3009         int i;
3010         struct isl_map *map = NULL;
3011
3012         set = isl_set_cow(set);
3013         if (!set || !dim)
3014                 goto error;
3015         isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3016         map = (struct isl_map *)set;
3017         for (i = 0; i < set->n; ++i) {
3018                 map->p[i] = isl_basic_map_from_basic_set(
3019                                 set->p[i], isl_dim_copy(dim));
3020                 if (!map->p[i])
3021                         goto error;
3022         }
3023         isl_dim_free(map->dim);
3024         map->dim = dim;
3025         return map;
3026 error:
3027         isl_dim_free(dim);
3028         isl_set_free(set);
3029         return NULL;
3030 }
3031
3032 struct isl_map *isl_map_from_range(struct isl_set *set)
3033 {
3034         return (struct isl_map *)set;
3035 }
3036
3037 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
3038 {
3039         return isl_map_reverse(isl_map_from_range(set));;
3040 }
3041
3042 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
3043         __isl_take isl_set *range)
3044 {
3045         return isl_map_product(isl_map_from_domain(domain),
3046                                isl_map_from_range(range));
3047 }
3048
3049 struct isl_set *isl_set_from_map(struct isl_map *map)
3050 {
3051         int i;
3052         struct isl_set *set = NULL;
3053
3054         if (!map)
3055                 return NULL;
3056         map = isl_map_cow(map);
3057         if (!map)
3058                 return NULL;
3059         map->dim = isl_dim_cow(map->dim);
3060         if (!map->dim)
3061                 goto error;
3062         map->dim->n_out += map->dim->n_in;
3063         map->dim->n_in = 0;
3064         set = (struct isl_set *)map;
3065         for (i = 0; i < map->n; ++i) {
3066                 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
3067                 if (!set->p[i])
3068                         goto error;
3069         }
3070         return set;
3071 error:
3072         isl_map_free(map);
3073         return NULL;
3074 }
3075
3076 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
3077 {
3078         struct isl_map *map;
3079
3080         if (!dim)
3081                 return NULL;
3082         isl_assert(dim->ctx, n >= 0, return NULL);
3083         map = isl_alloc(dim->ctx, struct isl_map,
3084                         sizeof(struct isl_map) +
3085                         (n - 1) * sizeof(struct isl_basic_map *));
3086         if (!map)
3087                 goto error;
3088
3089         map->ctx = dim->ctx;
3090         isl_ctx_ref(map->ctx);
3091         map->ref = 1;
3092         map->size = n;
3093         map->n = 0;
3094         map->dim = dim;
3095         map->flags = flags;
3096         return map;
3097 error:
3098         isl_dim_free(dim);
3099         return NULL;
3100 }
3101
3102 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
3103                 unsigned nparam, unsigned in, unsigned out, int n,
3104                 unsigned flags)
3105 {
3106         struct isl_map *map;
3107         struct isl_dim *dims;
3108
3109         dims = isl_dim_alloc(ctx, nparam, in, out);
3110         if (!dims)
3111                 return NULL;
3112
3113         map = isl_map_alloc_dim(dims, n, flags);
3114         return map;
3115 }
3116
3117 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
3118 {
3119         struct isl_basic_map *bmap;
3120         bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
3121         bmap = isl_basic_map_set_to_empty(bmap);
3122         return bmap;
3123 }
3124
3125 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
3126 {
3127         struct isl_basic_set *bset;
3128         bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
3129         bset = isl_basic_set_set_to_empty(bset);
3130         return bset;
3131 }
3132
3133 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
3134 {
3135         struct isl_basic_map *bmap;
3136         if (!model)
3137                 return NULL;
3138         bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3139         bmap = isl_basic_map_set_to_empty(bmap);
3140         return bmap;
3141 }
3142
3143 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
3144 {
3145         struct isl_basic_map *bmap;
3146         if (!model)
3147                 return NULL;
3148         bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3149         bmap = isl_basic_map_set_to_empty(bmap);
3150         return bmap;
3151 }
3152
3153 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
3154 {
3155         struct isl_basic_set *bset;
3156         if (!model)
3157                 return NULL;
3158         bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3159         bset = isl_basic_set_set_to_empty(bset);
3160         return bset;
3161 }
3162
3163 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
3164 {
3165         struct isl_basic_map *bmap;
3166         bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
3167         return bmap;
3168 }
3169
3170 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
3171 {
3172         struct isl_basic_set *bset;
3173         bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
3174         return bset;
3175 }
3176
3177 __isl_give isl_basic_map *isl_basic_map_universe_like(
3178                 __isl_keep isl_basic_map *model)
3179 {
3180         if (!model)
3181                 return NULL;
3182         return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3183 }
3184
3185 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
3186 {
3187         if (!model)
3188                 return NULL;
3189         return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3190 }
3191
3192 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
3193         __isl_keep isl_set *model)
3194 {
3195         if (!model)
3196                 return NULL;
3197         return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3198 }
3199
3200 struct isl_map *isl_map_empty(struct isl_dim *dim)
3201 {
3202         return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3203 }
3204
3205 struct isl_map *isl_map_empty_like(struct isl_map *model)
3206 {
3207         if (!model)
3208                 return NULL;
3209         return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3210 }
3211
3212 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
3213 {
3214         if (!model)
3215                 return NULL;
3216         return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3217 }
3218
3219 struct isl_set *isl_set_empty(struct isl_dim *dim)
3220 {
3221         return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3222 }
3223
3224 struct isl_set *isl_set_empty_like(struct isl_set *model)
3225 {
3226         if (!model)
3227                 return NULL;
3228         return isl_set_empty(isl_dim_copy(model->dim));
3229 }
3230
3231 struct isl_map *isl_map_universe(struct isl_dim *dim)
3232 {
3233         struct isl_map *map;
3234         if (!dim)
3235                 return NULL;
3236         map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3237         map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
3238         return map;
3239 }
3240
3241 struct isl_set *isl_set_universe(struct isl_dim *dim)
3242 {
3243         struct isl_set *set;
3244         if (!dim)
3245                 return NULL;
3246         set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3247         set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
3248         return set;
3249 }
3250
3251 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
3252 {
3253         if (!model)
3254                 return NULL;
3255         return isl_set_universe(isl_dim_copy(model->dim));
3256 }
3257
3258 struct isl_map *isl_map_dup(struct isl_map *map)
3259 {
3260         int i;
3261         struct isl_map *dup;
3262
3263         if (!map)
3264                 return NULL;
3265         dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
3266         for (i = 0; i < map->n; ++i)
3267                 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
3268         return dup;
3269 }
3270
3271 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
3272                                                 __isl_take isl_basic_map *bmap)
3273 {
3274         if (!bmap || !map)
3275                 goto error;
3276         if (isl_basic_map_fast_is_empty(bmap)) {
3277                 isl_basic_map_free(bmap);
3278                 return map;
3279         }
3280         isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
3281         isl_assert(map->ctx, map->n < map->size, goto error);
3282         map->p[map->n] = bmap;
3283         map->n++;
3284         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3285         return map;
3286 error:
3287         if (map)
3288                 isl_map_free(map);
3289         if (bmap)
3290                 isl_basic_map_free(bmap);
3291         return NULL;
3292 }
3293
3294 void isl_map_free(struct isl_map *map)
3295 {
3296         int i;
3297
3298         if (!map)
3299                 return;
3300
3301         if (--map->ref > 0)
3302                 return;
3303
3304         isl_ctx_deref(map->ctx);
3305         for (i = 0; i < map->n; ++i)
3306                 isl_basic_map_free(map->p[i]);
3307         isl_dim_free(map->dim);
3308         free(map);
3309 }
3310
3311 struct isl_map *isl_map_extend(struct isl_map *base,
3312                 unsigned nparam, unsigned n_in, unsigned n_out)
3313 {
3314         int i;
3315
3316         base = isl_map_cow(base);
3317         if (!base)
3318                 return NULL;
3319
3320         base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
3321         if (!base->dim)
3322                 goto error;
3323         for (i = 0; i < base->n; ++i) {
3324                 base->p[i] = isl_basic_map_extend_dim(base->p[i],
3325                                 isl_dim_copy(base->dim), 0, 0, 0);
3326                 if (!base->p[i])
3327                         goto error;
3328         }
3329         return base;
3330 error:
3331         isl_map_free(base);
3332         return NULL;
3333 }
3334
3335 struct isl_set *isl_set_extend(struct isl_set *base,
3336                 unsigned nparam, unsigned dim)
3337 {
3338         return (struct isl_set *)isl_map_extend((struct isl_map *)base,
3339                                                         nparam, 0, dim);
3340 }
3341
3342 static struct isl_basic_map *isl_basic_map_fix_pos_si(
3343         struct isl_basic_map *bmap, unsigned pos, int value)
3344 {
3345         int j;
3346
3347         bmap = isl_basic_map_cow(bmap);
3348         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3349         j = isl_basic_map_alloc_equality(bmap);
3350         if (j < 0)
3351                 goto error;
3352         isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3353         isl_int_set_si(bmap->eq[j][pos], -1);
3354         isl_int_set_si(bmap->eq[j][0], value);
3355         bmap = isl_basic_map_simplify(bmap);
3356         return isl_basic_map_finalize(bmap);
3357 error:
3358         isl_basic_map_free(bmap);
3359         return NULL;
3360 }
3361
3362 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
3363         __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
3364 {
3365         int j;
3366
3367         bmap = isl_basic_map_cow(bmap);
3368         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3369         j = isl_basic_map_alloc_equality(bmap);
3370         if (j < 0)
3371                 goto error;
3372         isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3373         isl_int_set_si(bmap->eq[j][pos], -1);
3374         isl_int_set(bmap->eq[j][0], value);
3375         bmap = isl_basic_map_simplify(bmap);
3376         return isl_basic_map_finalize(bmap);
3377 error:
3378         isl_basic_map_free(bmap);
3379         return NULL;
3380 }
3381
3382 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
3383                 enum isl_dim_type type, unsigned pos, int value)
3384 {
3385         if (!bmap)
3386                 return NULL;
3387         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3388         return isl_basic_map_fix_pos_si(bmap,
3389                 isl_basic_map_offset(bmap, type) + pos, value);
3390 error:
3391         isl_basic_map_free(bmap);
3392         return NULL;
3393 }
3394
3395 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
3396                 enum isl_dim_type type, unsigned pos, isl_int value)
3397 {
3398         if (!bmap)
3399                 return NULL;
3400         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3401         return isl_basic_map_fix_pos(bmap,
3402                 isl_basic_map_offset(bmap, type) + pos, value);
3403 error:
3404         isl_basic_map_free(bmap);
3405         return NULL;
3406 }
3407
3408 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
3409                 enum isl_dim_type type, unsigned pos, int value)
3410 {
3411         return (struct isl_basic_set *)
3412                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3413                                         type, pos, value);
3414 }
3415
3416 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
3417                 enum isl_dim_type type, unsigned pos, isl_int value)
3418 {
3419         return (struct isl_basic_set *)
3420                 isl_basic_map_fix((struct isl_basic_map *)bset,
3421                                         type, pos, value);
3422 }
3423
3424 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
3425                 unsigned input, int value)
3426 {
3427         return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
3428 }
3429
3430 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
3431                 unsigned dim, int value)
3432 {
3433         return (struct isl_basic_set *)
3434                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3435                                         isl_dim_set, dim, value);
3436 }
3437
3438 struct isl_map *isl_map_fix_si(struct isl_map *map,
3439                 enum isl_dim_type type, unsigned pos, int value)
3440 {
3441         int i;
3442
3443         map = isl_map_cow(map);
3444         if (!map)
3445                 return NULL;
3446
3447         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3448         for (i = 0; i < map->n; ++i) {
3449                 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
3450                 if (!map->p[i])
3451                         goto error;
3452         }
3453         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3454         return map;
3455 error:
3456         isl_map_free(map);
3457         return NULL;
3458 }
3459
3460 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
3461                 enum isl_dim_type type, unsigned pos, int value)
3462 {
3463         return (struct isl_set *)
3464                 isl_map_fix_si((struct isl_map *)set, type, pos, value);
3465 }
3466
3467 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
3468                 enum isl_dim_type type, unsigned pos, isl_int value)
3469 {
3470         int i;
3471
3472         map = isl_map_cow(map);
3473         if (!map)
3474                 return NULL;
3475
3476         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3477         for (i = 0; i < map->n; ++i) {
3478                 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
3479                 if (!map->p[i])
3480                         goto error;
3481         }
3482         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3483         return map;
3484 error:
3485         isl_map_free(map);
3486         return NULL;
3487 }
3488
3489 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
3490                 enum isl_dim_type type, unsigned pos, isl_int value)
3491 {
3492         return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
3493 }
3494
3495 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
3496                 unsigned input, int value)
3497 {
3498         return isl_map_fix_si(map, isl_dim_in, input, value);
3499 }
3500
3501 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
3502 {
3503         return (struct isl_set *)
3504                 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
3505 }
3506
3507 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
3508                 __isl_take isl_basic_map *bmap,
3509                 enum isl_dim_type type, unsigned pos, int value)
3510 {
3511         int j;
3512
3513         if (!bmap)
3514                 return NULL;
3515         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3516         pos += isl_basic_map_offset(bmap, type);
3517         bmap = isl_basic_map_cow(bmap);
3518         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
3519         j = isl_basic_map_alloc_inequality(bmap);
3520         if (j < 0)
3521                 goto error;
3522         isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
3523         isl_int_set_si(bmap->ineq[j][pos], 1);
3524         isl_int_set_si(bmap->ineq[j][0], -value);
3525         bmap = isl_basic_map_simplify(bmap);
3526         return isl_basic_map_finalize(bmap);
3527 error:
3528         isl_basic_map_free(bmap);
3529         return NULL;
3530 }
3531
3532 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
3533         unsigned dim, isl_int value)
3534 {
3535         int j;
3536
3537         bset = isl_basic_set_cow(bset);
3538         bset = isl_basic_set_extend_constraints(bset, 0, 1);
3539         j = isl_basic_set_alloc_inequality(bset);
3540         if (j < 0)
3541                 goto error;
3542         isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
3543         isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
3544         isl_int_neg(bset->ineq[j][0], value);
3545         bset = isl_basic_set_simplify(bset);
3546         return isl_basic_set_finalize(bset);
3547 error:
3548         isl_basic_set_free(bset);
3549         return NULL;
3550 }
3551
3552 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
3553                 enum isl_dim_type type, unsigned pos, int value)
3554 {
3555         int i;
3556
3557         map = isl_map_cow(map);
3558         if (!map)
3559                 return NULL;
3560
3561         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3562         for (i = 0; i < map->n; ++i) {
3563                 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
3564                                                          type, pos, value);
3565                 if (!map->p[i])
3566                         goto error;
3567         }
3568         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3569         return map;
3570 error:
3571         isl_map_free(map);
3572         return NULL;
3573 }
3574
3575 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
3576                 enum isl_dim_type type, unsigned pos, int value)
3577 {
3578         return (struct isl_set *)
3579                 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
3580 }
3581
3582 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
3583                                         isl_int value)
3584 {
3585         int i;
3586
3587         set = isl_set_cow(set);
3588         if (!set)
3589                 return NULL;
3590
3591         isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
3592         for (i = 0; i < set->n; ++i) {
3593                 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
3594                 if (!set->p[i])
3595                         goto error;
3596         }
3597         return set;
3598 error:
3599         isl_set_free(set);
3600         return NULL;
3601 }
3602
3603 struct isl_map *isl_map_reverse(struct isl_map *map)
3604 {
3605         int i;
3606
3607         map = isl_map_cow(map);
3608         if (!map)
3609                 return NULL;
3610
3611         map->dim = isl_dim_reverse(map->dim);
3612         if (!map->dim)
3613                 goto error;
3614         for (i = 0; i < map->n; ++i) {
3615                 map->p[i] = isl_basic_map_reverse(map->p[i]);
3616                 if (!map->p[i])
3617                         goto error;
3618         }
3619         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3620         return map;
3621 error:
3622         isl_map_free(map);
3623         return NULL;
3624 }
3625
3626 static struct isl_map *isl_basic_map_partial_lexopt(
3627                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3628                 struct isl_set **empty, int max)
3629 {
3630         if (!bmap)
3631                 goto error;
3632         if (bmap->ctx->opt->pip == ISL_PIP_PIP)
3633                 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
3634         else
3635                 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
3636 error:
3637         isl_basic_map_free(bmap);
3638         isl_basic_set_free(dom);
3639         if (empty)
3640                 *empty = NULL;
3641         return NULL;
3642 }
3643
3644 struct isl_map *isl_basic_map_partial_lexmax(
3645                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3646                 struct isl_set **empty)
3647 {
3648         return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
3649 }
3650
3651 struct isl_map *isl_basic_map_partial_lexmin(
3652                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3653                 struct isl_set **empty)
3654 {
3655         return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
3656 }
3657
3658 struct isl_set *isl_basic_set_partial_lexmin(
3659                 struct isl_basic_set *bset, struct isl_basic_set *dom,
3660                 struct isl_set **empty)
3661 {
3662         return (struct isl_set *)
3663                 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
3664                         dom, empty);
3665 }
3666
3667 struct isl_set *isl_basic_set_partial_lexmax(
3668                 struct isl_basic_set *bset, struct isl_basic_set *dom,
3669                 struct isl_set **empty)
3670 {
3671         return (struct isl_set *)
3672                 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
3673                         dom, empty);
3674 }
3675
3676 /* Given a basic map "bmap", compute the lexicograhically minimal
3677  * (or maximal) image element for each domain element in dom.
3678  * Set *empty to those elements in dom that do not have an image element.
3679  *
3680  * We first make sure the basic sets in dom are disjoint and then
3681  * simply collect the results over each of the basic sets separately.
3682  * We could probably improve the efficiency a bit by moving the union
3683  * domain down into the parametric integer programming.
3684  */
3685 static __isl_give isl_map *basic_map_partial_lexopt(
3686                 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
3687                 __isl_give isl_set **empty, int max)
3688 {
3689         int i;
3690         struct isl_map *res;
3691
3692         dom = isl_set_make_disjoint(dom);
3693         if (!dom)
3694                 goto error;
3695
3696         if (isl_set_fast_is_empty(dom)) {
3697                 res = isl_map_empty_like_basic_map(bmap);
3698                 *empty = isl_set_empty_like(dom);
3699                 isl_set_free(dom);
3700                 isl_basic_map_free(bmap);
3701                 return res;
3702         }
3703
3704         res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
3705                         isl_basic_set_copy(dom->p[0]), empty, max);
3706                 
3707         for (i = 1; i < dom->n; ++i) {
3708                 struct isl_map *res_i;
3709                 struct isl_set *empty_i;
3710
3711                 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
3712                                 isl_basic_set_copy(dom->p[i]), &empty_i, max);
3713
3714                 res = isl_map_union_disjoint(res, res_i);
3715                 *empty = isl_set_union_disjoint(*empty, empty_i);
3716         }
3717
3718         isl_set_free(dom);
3719         isl_basic_map_free(bmap);
3720         return res;
3721 error:
3722         *empty = NULL;
3723         isl_set_free(dom);
3724         isl_basic_map_free(bmap);
3725         return NULL;
3726 }
3727
3728 /* Given a map "map", compute the lexicograhically minimal
3729  * (or maximal) image element for each domain element in dom.
3730  * Set *empty to those elements in dom that do not have an image element.
3731  *
3732  * We first compute the lexicographically minimal or maximal element
3733  * in the first basic map.  This results in a partial solution "res"
3734  * and a subset "todo" of dom that still need to be handled.
3735  * We then consider each of the remaining maps in "map" and successively
3736  * improve both "res" and "todo".
3737  *
3738  * Let res^k and todo^k be the results after k steps and let i = k + 1.
3739  * Assume we are computing the lexicographical maximum.
3740  * We first intersect basic map i with a relation that maps elements
3741  * to elements that are lexicographically larger than the image elements
3742  * in res^k and the compute the maximum image element of this intersection.
3743  * The result ("better") corresponds to those image elements in basic map i
3744  * that are better than what we had before.  The remainder ("keep") are the
3745  * domain elements for which the image element in res_k was better.
3746  * We also compute the lexicographical maximum of basic map i in todo^k.
3747  * res^i is the result of the operation + better + those elements in
3748  *              res^k that we should keep
3749  * todo^i is the remainder of the maximum operation on todo^k.
3750  */
3751 static __isl_give isl_map *isl_map_partial_lexopt(
3752                 __isl_take isl_map *map, __isl_take isl_set *dom,
3753                 __isl_give isl_set **empty, int max)
3754 {
3755         int i;
3756         struct isl_map *res;
3757         struct isl_set *todo;
3758
3759         if (!map || !dom)
3760                 goto error;
3761
3762         if (isl_map_fast_is_empty(map)) {
3763                 if (empty)
3764                         *empty = dom;
3765                 else
3766                         isl_set_free(dom);
3767                 return map;
3768         }
3769
3770         res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
3771                                         isl_set_copy(dom), &todo, max);
3772
3773         for (i = 1; i < map->n; ++i) {
3774                 struct isl_map *lt;
3775                 struct isl_map *better;
3776                 struct isl_set *keep;
3777                 struct isl_map *res_i;
3778                 struct isl_set *todo_i;
3779                 struct isl_dim *dim = isl_map_get_dim(res);
3780
3781                 dim = isl_dim_range(dim);
3782                 if (max)
3783                         lt = isl_map_lex_lt(dim);
3784                 else
3785                         lt = isl_map_lex_gt(dim);
3786                 lt = isl_map_apply_range(isl_map_copy(res), lt);
3787                 lt = isl_map_intersect(lt,
3788                         isl_map_from_basic_map(isl_basic_map_copy(map->p[i])));
3789                 better = isl_map_partial_lexopt(lt,
3790                         isl_map_domain(isl_map_copy(res)),
3791                         &keep, max);
3792
3793                 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
3794                                                 todo, &todo_i, max);
3795
3796                 res = isl_map_intersect_domain(res, keep);
3797                 res = isl_map_union_disjoint(res, res_i);
3798                 res = isl_map_union_disjoint(res, better);
3799                 todo = todo_i;
3800         }
3801
3802         isl_set_free(dom);
3803         isl_map_free(map);
3804
3805         if (empty)
3806                 *empty = todo;
3807         else
3808                 isl_set_free(todo);
3809
3810         return res;
3811 error:
3812         if (empty)
3813                 *empty = NULL;
3814         isl_set_free(dom);
3815         isl_map_free(map);
3816         return NULL;
3817 }
3818
3819 __isl_give isl_map *isl_map_partial_lexmax(
3820                 __isl_take isl_map *map, __isl_take isl_set *dom,
3821                 __isl_give isl_set **empty)
3822 {
3823         return isl_map_partial_lexopt(map, dom, empty, 1);
3824 }
3825
3826 __isl_give isl_map *isl_map_partial_lexmin(
3827                 __isl_take isl_map *map, __isl_take isl_set *dom,
3828                 __isl_give isl_set **empty)
3829 {
3830         return isl_map_partial_lexopt(map, dom, empty, 0);
3831 }
3832
3833 __isl_give isl_set *isl_set_partial_lexmin(
3834                 __isl_take isl_set *set, __isl_take isl_set *dom,
3835                 __isl_give isl_set **empty)
3836 {
3837         return (struct isl_set *)
3838                 isl_map_partial_lexmin((struct isl_map *)set,
3839                         dom, empty);
3840 }
3841
3842 __isl_give isl_set *isl_set_partial_lexmax(
3843                 __isl_take isl_set *set, __isl_take isl_set *dom,
3844                 __isl_give isl_set **empty)
3845 {
3846         return (struct isl_set *)
3847                 isl_map_partial_lexmax((struct isl_map *)set,
3848                         dom, empty);
3849 }
3850
3851 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
3852 {
3853         struct isl_basic_set *dom = NULL;
3854         struct isl_dim *dom_dim;
3855
3856         if (!bmap)
3857                 goto error;
3858         dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
3859         dom = isl_basic_set_universe(dom_dim);
3860         return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
3861 error:
3862         isl_basic_map_free(bmap);
3863         return NULL;
3864 }
3865
3866 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
3867 {
3868         return isl_basic_map_lexopt(bmap, 0);
3869 }
3870
3871 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
3872 {
3873         return isl_basic_map_lexopt(bmap, 1);
3874 }
3875
3876 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
3877 {
3878         return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
3879 }
3880
3881 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
3882 {
3883         return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
3884 }
3885
3886 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
3887 {
3888         struct isl_set *dom = NULL;
3889         struct isl_dim *dom_dim;
3890
3891         if (!map)
3892                 goto error;
3893         dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
3894         dom = isl_set_universe(dom_dim);
3895         return isl_map_partial_lexopt(map, dom, NULL, max);
3896 error:
3897         isl_map_free(map);
3898         return NULL;
3899 }
3900
3901 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
3902 {
3903         return isl_map_lexopt(map, 0);
3904 }
3905
3906 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
3907 {
3908         return isl_map_lexopt(map, 1);
3909 }
3910
3911 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
3912 {
3913         return (isl_set *)isl_map_lexmin((isl_map *)set);
3914 }
3915
3916 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
3917 {
3918         return (isl_set *)isl_map_lexmax((isl_map *)set);
3919 }
3920
3921 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
3922         struct isl_dim *dim)
3923 {
3924         int i;
3925
3926         if (!map || !dim)
3927                 goto error;
3928
3929         for (i = 0; i < map->n; ++i) {
3930                 isl_dim_free(map->p[i]->dim);
3931                 map->p[i]->dim = isl_dim_copy(dim);
3932         }
3933         isl_dim_free(map->dim);
3934         map->dim = dim;
3935
3936         return map;
3937 error:
3938         isl_map_free(map);
3939         isl_dim_free(dim);
3940         return NULL;
3941 }
3942
3943 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
3944         struct isl_dim *dim)
3945 {
3946         return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
3947 }
3948
3949 /* Apply a preimage specified by "mat" on the parameters of "bset".
3950  * bset is assumed to have only parameters and divs.
3951  */
3952 static struct isl_basic_set *basic_set_parameter_preimage(
3953         struct isl_basic_set *bset, struct isl_mat *mat)
3954 {
3955         unsigned nparam;
3956
3957         if (!bset || !mat)
3958                 goto error;
3959
3960         bset->dim = isl_dim_cow(bset->dim);
3961         if (!bset->dim)
3962                 goto error;
3963
3964         nparam = isl_basic_set_dim(bset, isl_dim_param);
3965
3966         isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
3967
3968         bset->dim->nparam = 0;
3969         bset->dim->n_out = nparam;
3970         bset = isl_basic_set_preimage(bset, mat);
3971         if (bset) {
3972                 bset->dim->nparam = bset->dim->n_out;
3973                 bset->dim->n_out = 0;
3974         }
3975         return bset;
3976 error:
3977         isl_mat_free(mat);
3978         isl_basic_set_free(bset);
3979         return NULL;
3980 }
3981
3982 /* Apply a preimage specified by "mat" on the parameters of "set".
3983  * set is assumed to have only parameters and divs.
3984  */
3985 static struct isl_set *set_parameter_preimage(
3986         struct isl_set *set, struct isl_mat *mat)
3987 {
3988         struct isl_dim *dim = NULL;
3989         unsigned nparam;
3990
3991         if (!set || !mat)
3992                 goto error;
3993
3994         dim = isl_dim_copy(set->dim);
3995         dim = isl_dim_cow(dim);
3996         if (!dim)
3997                 goto error;
3998
3999         nparam = isl_set_dim(set, isl_dim_param);
4000
4001         isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
4002
4003         dim->nparam = 0;
4004         dim->n_out = nparam;
4005         isl_set_reset_dim(set, dim);
4006         set = isl_set_preimage(set, mat);
4007         if (!set)
4008                 goto error2;
4009         dim = isl_dim_copy(set->dim);
4010         dim = isl_dim_cow(dim);
4011         if (!dim)
4012                 goto error2;
4013         dim->nparam = dim->n_out;
4014         dim->n_out = 0;
4015         isl_set_reset_dim(set, dim);
4016         return set;
4017 error:
4018         isl_dim_free(dim);
4019         isl_mat_free(mat);
4020 error2:
4021         isl_set_free(set);
4022         return NULL;
4023 }
4024
4025 /* Intersect the basic set "bset" with the affine space specified by the
4026  * equalities in "eq".
4027  */
4028 static struct isl_basic_set *basic_set_append_equalities(
4029         struct isl_basic_set *bset, struct isl_mat *eq)
4030 {
4031         int i, k;
4032         unsigned len;
4033
4034         if (!bset || !eq)
4035                 goto error;
4036
4037         bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
4038                                         eq->n_row, 0);
4039         if (!bset)
4040                 goto error;
4041
4042         len = 1 + isl_dim_total(bset->dim) + bset->extra;
4043         for (i = 0; i < eq->n_row; ++i) {
4044                 k = isl_basic_set_alloc_equality(bset);
4045                 if (k < 0)
4046                         goto error;
4047                 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
4048                 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
4049         }
4050         isl_mat_free(eq);
4051
4052         return bset;
4053 error:
4054         isl_mat_free(eq);
4055         isl_basic_set_free(bset);
4056         return NULL;
4057 }
4058
4059 /* Intersect the set "set" with the affine space specified by the
4060  * equalities in "eq".
4061  */
4062 static struct isl_set *set_append_equalities(struct isl_set *set,
4063         struct isl_mat *eq)
4064 {
4065         int i;
4066
4067         if (!set || !eq)
4068                 goto error;
4069
4070         for (i = 0; i < set->n; ++i) {
4071                 set->p[i] = basic_set_append_equalities(set->p[i],
4072                                         isl_mat_copy(eq));
4073                 if (!set->p[i])
4074                         goto error;
4075         }
4076         isl_mat_free(eq);
4077         return set;
4078 error:
4079         isl_mat_free(eq);
4080         isl_set_free(set);
4081         return NULL;
4082 }
4083
4084 /* Project the given basic set onto its parameter domain, possibly introducing
4085  * new, explicit, existential variables in the constraints.
4086  * The input has parameters and (possibly implicit) existential variables.
4087  * The output has the same parameters, but only
4088  * explicit existentially quantified variables.
4089  *
4090  * The actual projection is performed by pip, but pip doesn't seem
4091  * to like equalities very much, so we first remove the equalities
4092  * among the parameters by performing a variable compression on
4093  * the parameters.  Afterward, an inverse transformation is performed
4094  * and the equalities among the parameters are inserted back in.
4095  */
4096 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
4097 {
4098         int i, j;
4099         struct isl_mat *eq;
4100         struct isl_mat *T, *T2;
4101         struct isl_set *set;
4102         unsigned nparam, n_div;
4103
4104         bset = isl_basic_set_cow(bset);
4105         if (!bset)
4106                 return NULL;
4107
4108         if (bset->n_eq == 0)
4109                 return isl_basic_set_lexmin(bset);
4110
4111         isl_basic_set_gauss(bset, NULL);
4112
4113         nparam = isl_basic_set_dim(bset, isl_dim_param);
4114         n_div = isl_basic_set_dim(bset, isl_dim_div);
4115
4116         for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
4117                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
4118                         ++i;
4119         }
4120         if (i == bset->n_eq)
4121                 return isl_basic_set_lexmin(bset);
4122
4123         eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
4124                 0, 1 + nparam);
4125         eq = isl_mat_cow(eq);
4126         T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
4127         bset = basic_set_parameter_preimage(bset, T);
4128
4129         set = isl_basic_set_lexmin(bset);
4130         set = set_parameter_preimage(set, T2);
4131         set = set_append_equalities(set, eq);
4132         return set;
4133 }
4134
4135 /* Compute an explicit representation for all the existentially
4136  * quantified variables.
4137  * The input and output dimensions are first turned into parameters.
4138  * compute_divs then returns a map with the same parameters and
4139  * no input or output dimensions and the dimension specification
4140  * is reset to that of the input.
4141  */
4142 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
4143 {
4144         struct isl_basic_set *bset;
4145         struct isl_set *set;
4146         struct isl_map *map;
4147         struct isl_dim *dim, *orig_dim = NULL;
4148         unsigned         nparam;
4149         unsigned         n_in;
4150         unsigned         n_out;
4151
4152         bmap = isl_basic_map_cow(bmap);
4153         if (!bmap)
4154                 return NULL;
4155
4156         nparam = isl_basic_map_dim(bmap, isl_dim_param);
4157         n_in = isl_basic_map_dim(bmap, isl_dim_in);
4158         n_out = isl_basic_map_dim(bmap, isl_dim_out);
4159         dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
4160         if (!dim)
4161                 goto error;
4162
4163         orig_dim = bmap->dim;
4164         bmap->dim = dim;
4165         bset = (struct isl_basic_set *)bmap;
4166
4167         set = parameter_compute_divs(bset);
4168         map = (struct isl_map *)set;
4169         map = isl_map_reset_dim(map, orig_dim);
4170
4171         return map;
4172 error:
4173         isl_basic_map_free(bmap);
4174         return NULL;
4175 }
4176
4177 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
4178 {
4179         int i;
4180         unsigned off;
4181
4182         if (!bmap)
4183                 return -1;
4184
4185         off = isl_dim_total(bmap->dim);
4186         for (i = 0; i < bmap->n_div; ++i) {
4187                 if (isl_int_is_zero(bmap->div[i][0]))
4188                         return 0;
4189                 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
4190                                 return -1);
4191         }
4192         return 1;
4193 }
4194
4195 static int map_divs_known(__isl_keep isl_map *map)
4196 {
4197         int i;
4198
4199         if (!map)
4200                 return -1;
4201
4202         for (i = 0; i < map->n; ++i) {
4203                 int known = basic_map_divs_known(map->p[i]);
4204                 if (known <= 0)
4205                         return known;
4206         }
4207
4208         return 1;
4209 }
4210
4211 /* If bmap contains any unknown divs, then compute explicit
4212  * expressions for them.  However, this computation may be
4213  * quite expensive, so first try to remove divs that aren't
4214  * strictly needed.
4215  */
4216 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
4217 {
4218         int i;
4219         int known;
4220         struct isl_map *map;
4221
4222         known = basic_map_divs_known(bmap);
4223         if (known < 0)
4224                 goto error;
4225         if (known)
4226                 return isl_map_from_basic_map(bmap);
4227
4228         bmap = isl_basic_map_drop_redundant_divs(bmap);
4229
4230         known = basic_map_divs_known(bmap);
4231         if (known < 0)
4232                 goto error;
4233         if (known)
4234                 return isl_map_from_basic_map(bmap);
4235
4236         map = compute_divs(bmap);
4237         return map;
4238 error:
4239         isl_basic_map_free(bmap);
4240         return NULL;
4241 }
4242
4243 struct isl_map *isl_map_compute_divs(struct isl_map *map)
4244 {
4245         int i;
4246         int known;
4247         struct isl_map *res;
4248
4249         if (!map)
4250                 return NULL;
4251         if (map->n == 0)
4252                 return map;
4253
4254         known = map_divs_known(map);
4255         if (known < 0) {
4256                 isl_map_free(map);
4257                 return NULL;
4258         }
4259         if (known)
4260                 return map;
4261
4262         res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
4263         for (i = 1 ; i < map->n; ++i) {
4264                 struct isl_map *r2;
4265                 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
4266                 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4267                         res = isl_map_union_disjoint(res, r2);
4268                 else
4269                         res = isl_map_union(res, r2);
4270         }
4271         isl_map_free(map);
4272
4273         return res;
4274 }
4275
4276 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
4277 {
4278         return (struct isl_set *)
4279                 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
4280 }
4281
4282 struct isl_set *isl_set_compute_divs(struct isl_set *set)
4283 {
4284         return (struct isl_set *)
4285                 isl_map_compute_divs((struct isl_map *)set);
4286 }
4287
4288 struct isl_set *isl_map_domain(struct isl_map *map)
4289 {
4290         int i;
4291         struct isl_set *set;
4292
4293         if (!map)
4294                 goto error;
4295
4296         map = isl_map_cow(map);
4297         if (!map)
4298                 return NULL;
4299
4300         set = (struct isl_set *)map;
4301         set->dim = isl_dim_domain(set->dim);
4302         if (!set->dim)
4303                 goto error;
4304         for (i = 0; i < map->n; ++i) {
4305                 set->p[i] = isl_basic_map_domain(map->p[i]);
4306                 if (!set->p[i])
4307                         goto error;
4308         }
4309         ISL_F_CLR(set, ISL_MAP_DISJOINT);
4310         ISL_F_CLR(set, ISL_SET_NORMALIZED);
4311         return set;
4312 error:
4313         isl_map_free(map);
4314         return NULL;
4315 }
4316
4317 struct isl_map *isl_map_union_disjoint(
4318                         struct isl_map *map1, struct isl_map *map2)
4319 {
4320         int i;
4321         unsigned flags = 0;
4322         struct isl_map *map = NULL;
4323
4324         if (!map1 || !map2)
4325                 goto error;
4326
4327         if (map1->n == 0) {
4328                 isl_map_free(map1);
4329                 return map2;
4330         }
4331         if (map2->n == 0) {
4332                 isl_map_free(map2);
4333                 return map1;
4334         }
4335
4336         isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4337
4338         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4339             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4340                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4341
4342         map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
4343                                 map1->n + map2->n, flags);
4344         if (!map)
4345                 goto error;
4346         for (i = 0; i < map1->n; ++i) {
4347                 map = isl_map_add_basic_map(map,
4348                                   isl_basic_map_copy(map1->p[i]));
4349                 if (!map)
4350                         goto error;
4351         }
4352         for (i = 0; i < map2->n; ++i) {
4353                 map = isl_map_add_basic_map(map,
4354                                   isl_basic_map_copy(map2->p[i]));
4355                 if (!map)
4356                         goto error;
4357         }
4358         isl_map_free(map1);
4359         isl_map_free(map2);
4360         return map;
4361 error:
4362         isl_map_free(map);
4363         isl_map_free(map1);
4364         isl_map_free(map2);
4365         return NULL;
4366 }
4367
4368 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
4369 {
4370         map1 = isl_map_union_disjoint(map1, map2);
4371         if (!map1)
4372                 return NULL;
4373         if (map1->n > 1)
4374                 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
4375         return map1;
4376 }
4377
4378 struct isl_set *isl_set_union_disjoint(
4379                         struct isl_set *set1, struct isl_set *set2)
4380 {
4381         return (struct isl_set *)
4382                 isl_map_union_disjoint(
4383                         (struct isl_map *)set1, (struct isl_map *)set2);
4384 }
4385
4386 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
4387 {
4388         return (struct isl_set *)
4389                 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
4390 }
4391
4392 struct isl_map *isl_map_intersect_range(
4393                 struct isl_map *map, struct isl_set *set)
4394 {
4395         unsigned flags = 0;
4396         struct isl_map *result;
4397         int i, j;
4398
4399         if (!map || !set)
4400                 goto error;
4401
4402         if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
4403             ISL_F_ISSET(set, ISL_MAP_DISJOINT))
4404                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4405
4406         result = isl_map_alloc_dim(isl_dim_copy(map->dim),
4407                                         map->n * set->n, flags);
4408         if (!result)
4409                 goto error;
4410         for (i = 0; i < map->n; ++i)
4411                 for (j = 0; j < set->n; ++j) {
4412                         result = isl_map_add_basic_map(result,
4413                             isl_basic_map_intersect_range(
4414                                 isl_basic_map_copy(map->p[i]),
4415                                 isl_basic_set_copy(set->p[j])));
4416                         if (!result)
4417                                 goto error;
4418                 }
4419         isl_map_free(map);
4420         isl_set_free(set);
4421         return result;
4422 error:
4423         isl_map_free(map);
4424         isl_set_free(set);
4425         return NULL;
4426 }
4427
4428 struct isl_map *isl_map_intersect_domain(
4429                 struct isl_map *map, struct isl_set *set)
4430 {
4431         return isl_map_reverse(
4432                 isl_map_intersect_range(isl_map_reverse(map), set));
4433 }
4434
4435 struct isl_map *isl_map_apply_domain(
4436                 struct isl_map *map1, struct isl_map *map2)
4437 {
4438         if (!map1 || !map2)
4439                 goto error;
4440         map1 = isl_map_reverse(map1);
4441         map1 = isl_map_apply_range(map1, map2);
4442         return isl_map_reverse(map1);
4443 error:
4444         isl_map_free(map1);
4445         isl_map_free(map2);
4446         return NULL;
4447 }
4448
4449 struct isl_map *isl_map_apply_range(
4450                 struct isl_map *map1, struct isl_map *map2)
4451 {
4452         struct isl_dim *dim_result;
4453         struct isl_map *result;
4454         int i, j;
4455
4456         if (!map1 || !map2)
4457                 goto error;
4458
4459         dim_result = isl_dim_join(isl_dim_copy(map1->dim),
4460                                   isl_dim_copy(map2->dim));
4461
4462         result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
4463         if (!result)
4464                 goto error;
4465         for (i = 0; i < map1->n; ++i)
4466                 for (j = 0; j < map2->n; ++j) {
4467                         result = isl_map_add_basic_map(result,
4468                             isl_basic_map_apply_range(
4469                                 isl_basic_map_copy(map1->p[i]),
4470                                 isl_basic_map_copy(map2->p[j])));
4471                         if (!result)
4472                                 goto error;
4473                 }
4474         isl_map_free(map1);
4475         isl_map_free(map2);
4476         if (result && result->n <= 1)
4477                 ISL_F_SET(result, ISL_MAP_DISJOINT);
4478         return result;
4479 error:
4480         isl_map_free(map1);
4481         isl_map_free(map2);
4482         return NULL;
4483 }
4484
4485 /*
4486  * returns range - domain
4487  */
4488 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
4489 {
4490         struct isl_basic_set *bset;
4491         unsigned dim;
4492         unsigned nparam;
4493         int i;
4494
4495         if (!bmap)
4496                 goto error;
4497         dim = isl_basic_map_n_in(bmap);
4498         nparam = isl_basic_map_n_param(bmap);
4499         isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
4500         bset = isl_basic_set_from_basic_map(bmap);
4501         bset = isl_basic_set_cow(bset);
4502         bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
4503         bset = isl_basic_set_swap_vars(bset, 2*dim);
4504         for (i = 0; i < dim; ++i) {
4505                 int j = isl_basic_map_alloc_equality(
4506                                             (struct isl_basic_map *)bset);
4507                 if (j < 0)
4508                         goto error;
4509                 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
4510                 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
4511                 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
4512                 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
4513         }
4514         return isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
4515 error:
4516         isl_basic_map_free(bmap);
4517         return NULL;
4518 }
4519
4520 /*
4521  * returns range - domain
4522  */
4523 struct isl_set *isl_map_deltas(struct isl_map *map)
4524 {
4525         int i;
4526         struct isl_set *result;
4527
4528         if (!map)
4529                 return NULL;
4530
4531         isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
4532         result = isl_set_alloc(map->ctx, isl_map_n_param(map),
4533                                         isl_map_n_in(map), map->n, map->flags);
4534         if (!result)
4535                 goto error;
4536         for (i = 0; i < map->n; ++i)
4537                 result = isl_set_add_basic_set(result,
4538                           isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
4539         isl_map_free(map);
4540         return result;
4541 error:
4542         isl_map_free(map);
4543         return NULL;
4544 }
4545
4546 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
4547 {
4548         struct isl_basic_map *bmap;
4549         unsigned nparam;
4550         unsigned dim;
4551         int i;
4552
4553         if (!dims)
4554                 return NULL;
4555
4556         nparam = dims->nparam;
4557         dim = dims->n_out;
4558         bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
4559         if (!bmap)
4560                 goto error;
4561
4562         for (i = 0; i < dim; ++i) {
4563                 int j = isl_basic_map_alloc_equality(bmap);
4564                 if (j < 0)
4565                         goto error;
4566                 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
4567                 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
4568                 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
4569         }
4570         return isl_basic_map_finalize(bmap);
4571 error:
4572         isl_basic_map_free(bmap);
4573         return NULL;
4574 }
4575
4576 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
4577 {
4578         struct isl_dim *dim = isl_dim_map(set_dim);
4579         if (!dim)
4580                 return NULL;
4581         return basic_map_identity(dim);
4582 }
4583
4584 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
4585 {
4586         if (!model || !model->dim)
4587                 return NULL;
4588         isl_assert(model->ctx,
4589                         model->dim->n_in == model->dim->n_out, return NULL);
4590         return basic_map_identity(isl_dim_copy(model->dim));
4591 }
4592
4593 static struct isl_map *map_identity(struct isl_dim *dim)
4594 {
4595         struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
4596         return isl_map_add_basic_map(map, basic_map_identity(isl_dim_copy(dim)));
4597 }
4598
4599 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
4600 {
4601         struct isl_dim *dim = isl_dim_map(set_dim);
4602         if (!dim)
4603                 return NULL;
4604         return map_identity(dim);
4605 }
4606
4607 struct isl_map *isl_map_identity_like(struct isl_map *model)
4608 {
4609         if (!model || !model->dim)
4610                 return NULL;
4611         isl_assert(model->ctx,
4612                         model->dim->n_in == model->dim->n_out, return NULL);
4613         return map_identity(isl_dim_copy(model->dim));
4614 }
4615
4616 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
4617 {
4618         if (!model || !model->dim)
4619                 return NULL;
4620         isl_assert(model->ctx,
4621                         model->dim->n_in == model->dim->n_out, return NULL);
4622         return map_identity(isl_dim_copy(model->dim));
4623 }
4624
4625 /* Construct a basic set with all set dimensions having only non-negative
4626  * values.
4627  */
4628 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
4629 {
4630         int i;
4631         unsigned nparam;
4632         unsigned dim;
4633         struct isl_basic_set *bset;
4634
4635         if (!dims)
4636                 return NULL;
4637         nparam = dims->nparam;
4638         dim = dims->n_out;
4639         bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
4640         if (!bset)
4641                 return NULL;
4642         for (i = 0; i < dim; ++i) {
4643                 int k = isl_basic_set_alloc_inequality(bset);
4644                 if (k < 0)
4645                         goto error;
4646                 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
4647                 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
4648         }
4649         return bset;
4650 error:
4651         isl_basic_set_free(bset);
4652         return NULL;
4653 }
4654
4655 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
4656 {
4657         return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
4658 }
4659
4660 int isl_basic_map_is_subset(
4661                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4662 {
4663         int is_subset;
4664         struct isl_map *map1;
4665         struct isl_map *map2;
4666
4667         if (!bmap1 || !bmap2)
4668                 return -1;
4669
4670         map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
4671         map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
4672
4673         is_subset = isl_map_is_subset(map1, map2);
4674
4675         isl_map_free(map1);
4676         isl_map_free(map2);
4677
4678         return is_subset;
4679 }
4680
4681 int isl_basic_map_is_equal(
4682                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4683 {
4684         int is_subset;
4685
4686         if (!bmap1 || !bmap2)
4687                 return -1;
4688         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4689         if (is_subset != 1)
4690                 return is_subset;
4691         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4692         return is_subset;
4693 }
4694
4695 int isl_basic_set_is_equal(
4696                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4697 {
4698         return isl_basic_map_is_equal(
4699                 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
4700 }
4701
4702 int isl_map_is_empty(struct isl_map *map)
4703 {
4704         int i;
4705         int is_empty;
4706
4707         if (!map)
4708                 return -1;
4709         for (i = 0; i < map->n; ++i) {
4710                 is_empty = isl_basic_map_is_empty(map->p[i]);
4711                 if (is_empty < 0)
4712                         return -1;
4713                 if (!is_empty)
4714                         return 0;
4715         }
4716         return 1;
4717 }
4718
4719 int isl_map_fast_is_empty(struct isl_map *map)
4720 {
4721         return map->n == 0;
4722 }
4723
4724 int isl_set_fast_is_empty(struct isl_set *set)
4725 {
4726         return set->n == 0;
4727 }
4728
4729 int isl_set_is_empty(struct isl_set *set)
4730 {
4731         return isl_map_is_empty((struct isl_map *)set);
4732 }
4733
4734 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
4735 {
4736         int is_subset;
4737
4738         if (!map1 || !map2)
4739                 return -1;
4740         is_subset = isl_map_is_subset(map1, map2);
4741         if (is_subset != 1)
4742                 return is_subset;
4743         is_subset = isl_map_is_subset(map2, map1);
4744         return is_subset;
4745 }
4746
4747 int isl_basic_map_is_strict_subset(
4748                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4749 {
4750         int is_subset;
4751
4752         if (!bmap1 || !bmap2)
4753                 return -1;
4754         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4755         if (is_subset != 1)
4756                 return is_subset;
4757         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4758         if (is_subset == -1)
4759                 return is_subset;
4760         return !is_subset;
4761 }
4762
4763 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
4764 {
4765         int is_subset;
4766
4767         if (!map1 || !map2)
4768                 return -1;
4769         is_subset = isl_map_is_subset(map1, map2);
4770         if (is_subset != 1)
4771                 return is_subset;
4772         is_subset = isl_map_is_subset(map2, map1);
4773         if (is_subset == -1)
4774                 return is_subset;
4775         return !is_subset;
4776 }
4777
4778 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
4779 {
4780         return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
4781 }
4782
4783 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
4784 {
4785         if (!bmap)
4786                 return -1;
4787         return bmap->n_eq == 0 && bmap->n_ineq == 0;
4788 }
4789
4790 int isl_basic_set_is_universe(struct isl_basic_set *bset)
4791 {
4792         if (!bset)
4793                 return -1;
4794         return bset->n_eq == 0 && bset->n_ineq == 0;
4795 }
4796
4797 int isl_map_fast_is_universe(__isl_keep isl_map *map)
4798 {
4799         if (!map)
4800                 return -1;
4801
4802         return map->n == 1 && isl_basic_map_is_universe(map->p[0]);
4803 }
4804
4805 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
4806 {
4807         struct isl_basic_set *bset = NULL;
4808         struct isl_vec *sample = NULL;
4809         int empty;
4810         unsigned total;
4811
4812         if (!bmap)
4813                 return -1;
4814
4815         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
4816                 return 1;
4817
4818         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
4819                 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
4820                 copy = isl_basic_map_convex_hull(copy);
4821                 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
4822                 isl_basic_map_free(copy);
4823                 return empty;
4824         }
4825
4826         total = 1 + isl_basic_map_total_dim(bmap);
4827         if (bmap->sample && bmap->sample->size == total) {
4828                 int contains = isl_basic_map_contains(bmap, bmap->sample);
4829                 if (contains < 0)
4830                         return -1;
4831                 if (contains)
4832                         return 0;
4833         }
4834         isl_vec_free(bmap->sample);
4835         bmap->sample = NULL;
4836         bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
4837         if (!bset)
4838                 return -1;
4839         sample = isl_basic_set_sample_vec(bset);
4840         if (!sample)
4841                 return -1;
4842         empty = sample->size == 0;
4843         isl_vec_free(bmap->sample);
4844         bmap->sample = sample;
4845         if (empty)
4846                 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
4847
4848         return empty;
4849 }
4850
4851 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
4852 {
4853         if (!bmap)
4854                 return -1;
4855         return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
4856 }
4857
4858 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
4859 {
4860         if (!bset)
4861                 return -1;
4862         return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
4863 }
4864
4865 int isl_basic_set_is_empty(struct isl_basic_set *bset)
4866 {
4867         return isl_basic_map_is_empty((struct isl_basic_map *)bset);
4868 }
4869
4870 struct isl_map *isl_basic_map_union(
4871         struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4872 {
4873         struct isl_map *map;
4874         if (!bmap1 || !bmap2)
4875                 return NULL;
4876
4877         isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
4878
4879         map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
4880         if (!map)
4881                 goto error;
4882         map = isl_map_add_basic_map(map, bmap1);
4883         map = isl_map_add_basic_map(map, bmap2);
4884         return map;
4885 error:
4886         isl_basic_map_free(bmap1);
4887         isl_basic_map_free(bmap2);
4888         return NULL;
4889 }
4890
4891 struct isl_set *isl_basic_set_union(
4892                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4893 {
4894         return (struct isl_set *)isl_basic_map_union(
4895                                             (struct isl_basic_map *)bset1,
4896                                             (struct isl_basic_map *)bset2);
4897 }
4898
4899 /* Order divs such that any div only depends on previous divs */
4900 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
4901 {
4902         int i;
4903         unsigned off = isl_dim_total(bmap->dim);
4904
4905         for (i = 0; i < bmap->n_div; ++i) {
4906                 int pos;
4907                 if (isl_int_is_zero(bmap->div[i][0]))
4908                         continue;
4909                 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
4910                                                             bmap->n_div-i);
4911                 if (pos == -1)
4912                         continue;
4913                 isl_basic_map_swap_div(bmap, i, i + pos);
4914                 --i;
4915         }
4916         return bmap;
4917 }
4918
4919 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
4920 {
4921         return (struct isl_basic_set *)
4922                 isl_basic_map_order_divs((struct isl_basic_map *)bset);
4923 }
4924
4925 /* Look for a div in dst that corresponds to the div "div" in src.
4926  * The divs before "div" in src and dst are assumed to be the same.
4927  * 
4928  * Returns -1 if no corresponding div was found and the position
4929  * of the corresponding div in dst otherwise.
4930  */
4931 static int find_div(struct isl_basic_map *dst,
4932                         struct isl_basic_map *src, unsigned div)
4933 {
4934         int i;
4935
4936         unsigned total = isl_dim_total(src->dim);
4937
4938         isl_assert(dst->ctx, div <= dst->n_div, return -1);
4939         for (i = div; i < dst->n_div; ++i)
4940                 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
4941                     isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
4942                                                 dst->n_div - div) == -1)
4943                         return i;
4944         return -1;
4945 }
4946
4947 struct isl_basic_map *isl_basic_map_align_divs(
4948                 struct isl_basic_map *dst, struct isl_basic_map *src)
4949 {
4950         int i;
4951         unsigned total = isl_dim_total(src->dim);
4952
4953         if (!dst || !src)
4954                 goto error;
4955
4956         if (src->n_div == 0)
4957                 return dst;
4958
4959         for (i = 0; i < src->n_div; ++i)
4960                 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
4961
4962         src = isl_basic_map_order_divs(src);
4963         dst = isl_basic_map_cow(dst);
4964         dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
4965                         src->n_div, 0, 2 * src->n_div);
4966         if (!dst)
4967                 return NULL;
4968         for (i = 0; i < src->n_div; ++i) {
4969                 int j = find_div(dst, src, i);
4970                 if (j < 0) {
4971                         j = isl_basic_map_alloc_div(dst);
4972                         if (j < 0)
4973                                 goto error;
4974                         isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
4975                         isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
4976                         if (isl_basic_map_add_div_constraints(dst, j) < 0)
4977                                 goto error;
4978                 }
4979                 if (j != i)
4980                         isl_basic_map_swap_div(dst, i, j);
4981         }
4982         return dst;
4983 error:
4984         isl_basic_map_free(dst);
4985         return NULL;
4986 }
4987
4988 struct isl_basic_set *isl_basic_set_align_divs(
4989                 struct isl_basic_set *dst, struct isl_basic_set *src)
4990 {
4991         return (struct isl_basic_set *)isl_basic_map_align_divs(
4992                 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
4993 }
4994
4995 struct isl_map *isl_map_align_divs(struct isl_map *map)
4996 {
4997         int i;
4998
4999         if (!map)
5000                 return NULL;
5001         if (map->n == 0)
5002                 return map;
5003         map = isl_map_compute_divs(map);
5004         map = isl_map_cow(map);
5005         if (!map)
5006                 return NULL;
5007
5008         for (i = 1; i < map->n; ++i)
5009                 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
5010         for (i = 1; i < map->n; ++i)
5011                 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
5012
5013         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5014         return map;
5015 }
5016
5017 struct isl_set *isl_set_align_divs(struct isl_set *set)
5018 {
5019         return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
5020 }
5021
5022 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
5023 {
5024         if (!set || !map)
5025                 goto error;
5026         isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
5027         map = isl_map_intersect_domain(map, set);
5028         set = isl_map_range(map);
5029         return set;
5030 error:
5031         isl_set_free(set);
5032         isl_map_free(map);
5033         return NULL;
5034 }
5035
5036 /* There is no need to cow as removing empty parts doesn't change
5037  * the meaning of the set.
5038  */
5039 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
5040 {
5041         int i;
5042
5043         if (!map)
5044                 return NULL;
5045
5046         for (i = map->n-1; i >= 0; --i) {
5047                 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
5048                         continue;
5049                 isl_basic_map_free(map->p[i]);
5050                 if (i != map->n-1) {
5051                         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5052                         map->p[i] = map->p[map->n-1];
5053                 }
5054                 map->n--;
5055         }
5056
5057         return map;
5058 }
5059
5060 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
5061 {
5062         return (struct isl_set *)
5063                 isl_map_remove_empty_parts((struct isl_map *)set);
5064 }
5065
5066 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
5067 {
5068         struct isl_basic_map *bmap;
5069         if (!map || map->n == 0)
5070                 return NULL;
5071         bmap = map->p[map->n-1];
5072         isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
5073         return isl_basic_map_copy(bmap);
5074 }
5075
5076 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
5077 {
5078         return (struct isl_basic_set *)
5079                 isl_map_copy_basic_map((struct isl_map *)set);
5080 }
5081
5082 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
5083                                                 __isl_keep isl_basic_map *bmap)
5084 {
5085         int i;
5086
5087         if (!map || !bmap)
5088                 goto error;
5089         for (i = map->n-1; i >= 0; --i) {
5090                 if (map->p[i] != bmap)
5091                         continue;
5092                 map = isl_map_cow(map);
5093                 if (!map)
5094                         goto error;
5095                 isl_basic_map_free(map->p[i]);
5096                 if (i != map->n-1) {
5097                         ISL_F_CLR(map, ISL_SET_NORMALIZED);
5098                         map->p[i] = map->p[map->n-1];
5099                 }
5100                 map->n--;
5101                 return map;
5102         }
5103         return map;
5104 error:
5105         isl_map_free(map);
5106         return NULL;
5107 }
5108
5109 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
5110                                                 struct isl_basic_set *bset)
5111 {
5112         return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
5113                                                 (struct isl_basic_map *)bset);
5114 }
5115
5116 /* Given two basic sets bset1 and bset2, compute the maximal difference
5117  * between the values of dimension pos in bset1 and those in bset2
5118  * for any common value of the parameters and dimensions preceding pos.
5119  */
5120 static enum isl_lp_result basic_set_maximal_difference_at(
5121         __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
5122         int pos, isl_int *opt)
5123 {
5124         struct isl_dim *dims;
5125         struct isl_basic_map *bmap1 = NULL;
5126         struct isl_basic_map *bmap2 = NULL;
5127         struct isl_ctx *ctx;
5128         struct isl_vec *obj;
5129         unsigned total;
5130         unsigned nparam;
5131         unsigned dim1, dim2;
5132         enum isl_lp_result res;
5133
5134         if (!bset1 || !bset2)
5135                 return isl_lp_error;
5136
5137         nparam = isl_basic_set_n_param(bset1);
5138         dim1 = isl_basic_set_n_dim(bset1);
5139         dim2 = isl_basic_set_n_dim(bset2);
5140         dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
5141         bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
5142         dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
5143         bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
5144         if (!bmap1 || !bmap2)
5145                 goto error;
5146         bmap1 = isl_basic_map_cow(bmap1);
5147         bmap1 = isl_basic_map_extend(bmap1, nparam,
5148                         pos, (dim1 - pos) + (dim2 - pos),
5149                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
5150         bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
5151         if (!bmap1)
5152                 goto error;
5153         total = isl_basic_map_total_dim(bmap1);
5154         ctx = bmap1->ctx;
5155         obj = isl_vec_alloc(ctx, 1 + total);
5156         isl_seq_clr(obj->block.data, 1 + total);
5157         isl_int_set_si(obj->block.data[1+nparam+pos], 1);
5158         isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
5159         if (!obj)
5160                 goto error;
5161         res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
5162                                         opt, NULL, NULL);
5163         isl_basic_map_free(bmap1);
5164         isl_vec_free(obj);
5165         return res;
5166 error:
5167         isl_basic_map_free(bmap1);
5168         isl_basic_map_free(bmap2);
5169         return isl_lp_error;
5170 }
5171
5172 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
5173  * for any common value of the parameters and dimensions preceding pos
5174  * in both basic sets, the values of dimension pos in bset1 are
5175  * smaller or larger than those in bset2.
5176  *
5177  * Returns
5178  *       1 if bset1 follows bset2
5179  *      -1 if bset1 precedes bset2
5180  *       0 if bset1 and bset2 are incomparable
5181  *      -2 if some error occurred.
5182  */
5183 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
5184         struct isl_basic_set *bset2, int pos)
5185 {
5186         isl_int opt;
5187         enum isl_lp_result res;
5188         int cmp;
5189
5190         isl_int_init(opt);
5191
5192         res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5193
5194         if (res == isl_lp_empty)
5195                 cmp = 0;
5196         else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5197                   res == isl_lp_unbounded)
5198                 cmp = 1;
5199         else if (res == isl_lp_ok && isl_int_is_neg(opt))
5200                 cmp = -1;
5201         else
5202                 cmp = -2;
5203
5204         isl_int_clear(opt);
5205         return cmp;
5206 }
5207
5208 /* Given two basic sets bset1 and bset2, check whether
5209  * for any common value of the parameters and dimensions preceding pos
5210  * there is a value of dimension pos in bset1 that is larger
5211  * than a value of the same dimension in bset2.
5212  *
5213  * Return
5214  *       1 if there exists such a pair
5215  *       0 if there is no such pair, but there is a pair of equal values
5216  *      -1 otherwise
5217  *      -2 if some error occurred.
5218  */
5219 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
5220         __isl_keep isl_basic_set *bset2, int pos)
5221 {
5222         isl_int opt;
5223         enum isl_lp_result res;
5224         int cmp;
5225
5226         isl_int_init(opt);
5227
5228         res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5229
5230         if (res == isl_lp_empty)
5231                 cmp = -1;
5232         else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5233                   res == isl_lp_unbounded)
5234                 cmp = 1;
5235         else if (res == isl_lp_ok && isl_int_is_neg(opt))
5236                 cmp = -1;
5237         else if (res == isl_lp_ok)
5238                 cmp = 0;
5239         else
5240                 cmp = -2;
5241
5242         isl_int_clear(opt);
5243         return cmp;
5244 }
5245
5246 /* Given two sets set1 and set2, check whether
5247  * for any common value of the parameters and dimensions preceding pos
5248  * there is a value of dimension pos in set1 that is larger
5249  * than a value of the same dimension in set2.
5250  *
5251  * Return
5252  *       1 if there exists such a pair
5253  *       0 if there is no such pair, but there is a pair of equal values
5254  *      -1 otherwise
5255  *      -2 if some error occurred.
5256  */
5257 int isl_set_follows_at(__isl_keep isl_set *set1,
5258         __isl_keep isl_set *set2, int pos)
5259 {
5260         int i, j;
5261         int follows = -1;
5262
5263         if (!set1 || !set2)
5264                 return -2;
5265
5266         for (i = 0; i < set1->n; ++i)
5267                 for (j = 0; j < set2->n; ++j) {
5268                         int f;
5269                         f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
5270                         if (f == 1 || f == -2)
5271                                 return f;
5272                         if (f > follows)
5273                                 follows = f;
5274                 }
5275
5276         return follows;
5277 }
5278
5279 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
5280         unsigned pos, isl_int *val)
5281 {
5282         int i;
5283         int d;
5284         unsigned total;
5285
5286         if (!bmap)
5287                 return -1;
5288         total = isl_basic_map_total_dim(bmap);
5289         for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
5290                 for (; d+1 > pos; --d)
5291                         if (!isl_int_is_zero(bmap->eq[i][1+d]))
5292                                 break;
5293                 if (d != pos)
5294                         continue;
5295                 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
5296                         return 0;
5297                 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
5298                         return 0;
5299                 if (!isl_int_is_one(bmap->eq[i][1+d]))
5300                         return 0;
5301                 if (val)
5302                         isl_int_neg(*val, bmap->eq[i][0]);
5303                 return 1;
5304         }
5305         return 0;
5306 }
5307
5308 static int isl_map_fast_has_fixed_var(struct isl_map *map,
5309         unsigned pos, isl_int *val)
5310 {
5311         int i;
5312         isl_int v;
5313         isl_int tmp;
5314         int fixed;
5315
5316         if (!map)
5317                 return -1;
5318         if (map->n == 0)
5319                 return 0;
5320         if (map->n == 1)
5321                 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val); 
5322         isl_int_init(v);
5323         isl_int_init(tmp);
5324         fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v); 
5325         for (i = 1; fixed == 1 && i < map->n; ++i) {
5326                 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp); 
5327                 if (fixed == 1 && isl_int_ne(tmp, v))
5328                         fixed = 0;
5329         }
5330         if (val)
5331                 isl_int_set(*val, v);
5332         isl_int_clear(tmp);
5333         isl_int_clear(v);
5334         return fixed;
5335 }
5336
5337 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
5338         unsigned pos, isl_int *val)
5339 {
5340         return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
5341                                                 pos, val);
5342 }
5343
5344 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
5345         isl_int *val)
5346 {
5347         return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
5348 }
5349
5350 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
5351         enum isl_dim_type type, unsigned pos, isl_int *val)
5352 {
5353         if (pos >= isl_basic_map_dim(bmap, type))
5354                 return -1;
5355         return isl_basic_map_fast_has_fixed_var(bmap,
5356                 isl_basic_map_offset(bmap, type) - 1 + pos, val);
5357 }
5358
5359 int isl_map_fast_is_fixed(struct isl_map *map,
5360         enum isl_dim_type type, unsigned pos, isl_int *val)
5361 {
5362         if (pos >= isl_map_dim(map, type))
5363                 return -1;
5364         return isl_map_fast_has_fixed_var(map,
5365                 map_offset(map, type) - 1 + pos, val);
5366 }
5367
5368 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5369  * then return this fixed value in *val.
5370  */
5371 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
5372         isl_int *val)
5373 {
5374         return isl_basic_set_fast_has_fixed_var(bset,
5375                                         isl_basic_set_n_param(bset) + dim, val);
5376 }
5377
5378 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5379  * then return this fixed value in *val.
5380  */
5381 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
5382 {
5383         return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
5384 }
5385
5386 /* Check if input variable in has fixed value and if so and if val is not NULL,
5387  * then return this fixed value in *val.
5388  */
5389 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
5390 {
5391         return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
5392 }
5393
5394 /* Check if dimension dim has an (obvious) fixed lower bound and if so
5395  * and if val is not NULL, then return this lower bound in *val.
5396  */
5397 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
5398         unsigned dim, isl_int *val)
5399 {
5400         int i, i_eq = -1, i_ineq = -1;
5401         isl_int *c;
5402         unsigned total;
5403         unsigned nparam;
5404
5405         if (!bset)
5406                 return -1;
5407         total = isl_basic_set_total_dim(bset);
5408         nparam = isl_basic_set_n_param(bset);
5409         for (i = 0; i < bset->n_eq; ++i) {
5410                 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
5411                         continue;
5412                 if (i_eq != -1)
5413                         return 0;
5414                 i_eq = i;
5415         }
5416         for (i = 0; i < bset->n_ineq; ++i) {
5417                 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
5418                         continue;
5419                 if (i_eq != -1 || i_ineq != -1)
5420                         return 0;
5421                 i_ineq = i;
5422         }
5423         if (i_eq == -1 && i_ineq == -1)
5424                 return 0;
5425         c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
5426         /* The coefficient should always be one due to normalization. */
5427         if (!isl_int_is_one(c[1+nparam+dim]))
5428                 return 0;
5429         if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
5430                 return 0;
5431         if (isl_seq_first_non_zero(c+1+nparam+dim+1,
5432                                         total - nparam - dim - 1) != -1)
5433                 return 0;
5434         if (val)
5435                 isl_int_neg(*val, c[0]);
5436         return 1;
5437 }
5438
5439 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
5440         unsigned dim, isl_int *val)
5441 {
5442         int i;
5443         isl_int v;
5444         isl_int tmp;
5445         int fixed;
5446
5447         if (!set)
5448                 return -1;
5449         if (set->n == 0)
5450                 return 0;
5451         if (set->n == 1)
5452                 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5453                                                                 dim, val);
5454         isl_int_init(v);
5455         isl_int_init(tmp);
5456         fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5457                                                                 dim, &v);
5458         for (i = 1; fixed == 1 && i < set->n; ++i) {
5459                 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
5460                                                                 dim, &tmp);
5461                 if (fixed == 1 && isl_int_ne(tmp, v))
5462                         fixed = 0;
5463         }
5464         if (val)
5465                 isl_int_set(*val, v);
5466         isl_int_clear(tmp);
5467         isl_int_clear(v);
5468         return fixed;
5469 }
5470
5471 struct constraint {
5472         unsigned        size;
5473         isl_int         *c;
5474 };
5475
5476 static int qsort_constraint_cmp(const void *p1, const void *p2)
5477 {
5478         const struct constraint *c1 = (const struct constraint *)p1;
5479         const struct constraint *c2 = (const struct constraint *)p2;
5480         unsigned size = isl_min(c1->size, c2->size);
5481         return isl_seq_cmp(c1->c, c2->c, size);
5482 }
5483
5484 static struct isl_basic_map *isl_basic_map_sort_constraints(
5485         struct isl_basic_map *bmap)
5486 {
5487         int i;
5488         struct constraint *c;
5489         unsigned total;
5490
5491         if (!bmap)
5492                 return NULL;
5493         total = isl_basic_map_total_dim(bmap);
5494         c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
5495         if (!c)
5496                 goto error;
5497         for (i = 0; i < bmap->n_ineq; ++i) {
5498                 c[i].size = total;
5499                 c[i].c = bmap->ineq[i];
5500         }
5501         qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
5502         for (i = 0; i < bmap->n_ineq; ++i)
5503                 bmap->ineq[i] = c[i].c;
5504         free(c);
5505         return bmap;
5506 error:
5507         isl_basic_map_free(bmap);
5508         return NULL;
5509 }
5510
5511 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
5512 {
5513         if (!bmap)
5514                 return NULL;
5515         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
5516                 return bmap;
5517         bmap = isl_basic_map_convex_hull(bmap);
5518         bmap = isl_basic_map_sort_constraints(bmap);
5519         ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
5520         return bmap;
5521 }
5522
5523 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
5524 {
5525         return (struct isl_basic_set *)isl_basic_map_normalize(
5526                                                 (struct isl_basic_map *)bset);
5527 }
5528
5529 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
5530         const struct isl_basic_map *bmap2)
5531 {
5532         int i, cmp;
5533         unsigned total;
5534
5535         if (bmap1 == bmap2)
5536                 return 0;
5537         if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
5538                 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
5539         if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
5540                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5541         if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
5542                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5543         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
5544             ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5545                 return 0;
5546         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
5547                 return 1;
5548         if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5549                 return -1;
5550         if (bmap1->n_eq != bmap2->n_eq)
5551                 return bmap1->n_eq - bmap2->n_eq;
5552         if (bmap1->n_ineq != bmap2->n_ineq)
5553                 return bmap1->n_ineq - bmap2->n_ineq;
5554         if (bmap1->n_div != bmap2->n_div)
5555                 return bmap1->n_div - bmap2->n_div;
5556         total = isl_basic_map_total_dim(bmap1);
5557         for (i = 0; i < bmap1->n_eq; ++i) {
5558                 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
5559                 if (cmp)
5560                         return cmp;
5561         }
5562         for (i = 0; i < bmap1->n_ineq; ++i) {
5563                 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
5564                 if (cmp)
5565                         return cmp;
5566         }
5567         for (i = 0; i < bmap1->n_div; ++i) {
5568                 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
5569                 if (cmp)
5570                         return cmp;
5571         }
5572         return 0;
5573 }
5574
5575 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
5576         struct isl_basic_map *bmap2)
5577 {
5578         return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
5579 }
5580
5581 static int qsort_bmap_cmp(const void *p1, const void *p2)
5582 {
5583         const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
5584         const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
5585
5586         return isl_basic_map_fast_cmp(bmap1, bmap2);
5587 }
5588
5589 /* We normalize in place, but if anything goes wrong we need
5590  * to return NULL, so we need to make sure we don't change the
5591  * meaning of any possible other copies of map.
5592  */
5593 struct isl_map *isl_map_normalize(struct isl_map *map)
5594 {
5595         int i, j;
5596         struct isl_basic_map *bmap;
5597
5598         if (!map)
5599                 return NULL;
5600         if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
5601                 return map;
5602         for (i = 0; i < map->n; ++i) {
5603                 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
5604                 if (!bmap)
5605                         goto error;
5606                 isl_basic_map_free(map->p[i]);
5607                 map->p[i] = bmap;
5608         }
5609         qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
5610         ISL_F_SET(map, ISL_MAP_NORMALIZED);
5611         map = isl_map_remove_empty_parts(map);
5612         if (!map)
5613                 return NULL;
5614         for (i = map->n - 1; i >= 1; --i) {
5615                 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
5616                         continue;
5617                 isl_basic_map_free(map->p[i-1]);
5618                 for (j = i; j < map->n; ++j)
5619                         map->p[j-1] = map->p[j];
5620                 map->n--;
5621         }
5622         return map;
5623 error:
5624         isl_map_free(map);
5625         return NULL;
5626
5627 }
5628
5629 struct isl_set *isl_set_normalize(struct isl_set *set)
5630 {
5631         return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
5632 }
5633
5634 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
5635 {
5636         int i;
5637         int equal;
5638
5639         if (!map1 || !map2)
5640                 return -1;
5641
5642         if (map1 == map2)
5643                 return 1;
5644         if (!isl_dim_equal(map1->dim, map2->dim))
5645                 return 0;
5646
5647         map1 = isl_map_copy(map1);
5648         map2 = isl_map_copy(map2);
5649         map1 = isl_map_normalize(map1);
5650         map2 = isl_map_normalize(map2);
5651         if (!map1 || !map2)
5652                 goto error;
5653         equal = map1->n == map2->n;
5654         for (i = 0; equal && i < map1->n; ++i) {
5655                 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
5656                 if (equal < 0)
5657                         goto error;
5658         }
5659         isl_map_free(map1);
5660         isl_map_free(map2);
5661         return equal;
5662 error:
5663         isl_map_free(map1);
5664         isl_map_free(map2);
5665         return -1;
5666 }
5667
5668 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
5669 {
5670         return isl_map_fast_is_equal((struct isl_map *)set1,
5671                                                 (struct isl_map *)set2);
5672 }
5673
5674 /* Return an interval that ranges from min to max (inclusive)
5675  */
5676 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
5677         isl_int min, isl_int max)
5678 {
5679         int k;
5680         struct isl_basic_set *bset = NULL;
5681
5682         bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
5683         if (!bset)
5684                 goto error;
5685
5686         k = isl_basic_set_alloc_inequality(bset);
5687         if (k < 0)
5688                 goto error;
5689         isl_int_set_si(bset->ineq[k][1], 1);
5690         isl_int_neg(bset->ineq[k][0], min);
5691
5692         k = isl_basic_set_alloc_inequality(bset);
5693         if (k < 0)
5694                 goto error;
5695         isl_int_set_si(bset->ineq[k][1], -1);
5696         isl_int_set(bset->ineq[k][0], max);
5697
5698         return bset;
5699 error:
5700         isl_basic_set_free(bset);
5701         return NULL;
5702 }
5703
5704 /* Return the Cartesian product of the basic sets in list (in the given order).
5705  */
5706 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
5707 {
5708         int i;
5709         unsigned dim;
5710         unsigned nparam;
5711         unsigned extra;
5712         unsigned n_eq;
5713         unsigned n_ineq;
5714         struct isl_basic_set *product = NULL;
5715
5716         if (!list)
5717                 goto error;
5718         isl_assert(list->ctx, list->n > 0, goto error);
5719         isl_assert(list->ctx, list->p[0], goto error);
5720         nparam = isl_basic_set_n_param(list->p[0]);
5721         dim = isl_basic_set_n_dim(list->p[0]);
5722         extra = list->p[0]->n_div;
5723         n_eq = list->p[0]->n_eq;
5724         n_ineq = list->p[0]->n_ineq;
5725         for (i = 1; i < list->n; ++i) {
5726                 isl_assert(list->ctx, list->p[i], goto error);
5727                 isl_assert(list->ctx,
5728                     nparam == isl_basic_set_n_param(list->p[i]), goto error);
5729                 dim += isl_basic_set_n_dim(list->p[i]);
5730                 extra += list->p[i]->n_div;
5731                 n_eq += list->p[i]->n_eq;
5732                 n_ineq += list->p[i]->n_ineq;
5733         }
5734         product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
5735                                         n_eq, n_ineq);
5736         if (!product)
5737                 goto error;
5738         dim = 0;
5739         for (i = 0; i < list->n; ++i) {
5740                 isl_basic_set_add_constraints(product,
5741                                         isl_basic_set_copy(list->p[i]), dim);
5742                 dim += isl_basic_set_n_dim(list->p[i]);
5743         }
5744         isl_basic_set_list_free(list);
5745         return product;
5746 error:
5747         isl_basic_set_free(product);
5748         isl_basic_set_list_free(list);
5749         return NULL;
5750 }
5751
5752 struct isl_basic_map *isl_basic_map_product(
5753                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5754 {
5755         struct isl_dim *dim_result = NULL;
5756         struct isl_basic_map *bmap;
5757         unsigned in1, in2, out1, out2, nparam, total, pos;
5758         struct isl_dim_map *dim_map1, *dim_map2;
5759
5760         if (!bmap1 || !bmap2)
5761                 goto error;
5762
5763         isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
5764                                      bmap2->dim, isl_dim_param), goto error);
5765         dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
5766                                                    isl_dim_copy(bmap2->dim));
5767
5768         in1 = isl_basic_map_n_in(bmap1);
5769         in2 = isl_basic_map_n_in(bmap2);
5770         out1 = isl_basic_map_n_out(bmap1);
5771         out2 = isl_basic_map_n_out(bmap2);
5772         nparam = isl_basic_map_n_param(bmap1);
5773
5774         total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
5775         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
5776         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
5777         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
5778         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
5779         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
5780         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
5781         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
5782         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
5783         isl_dim_map_div(dim_map1, bmap1, pos += out2);
5784         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
5785
5786         bmap = isl_basic_map_alloc_dim(dim_result,
5787                         bmap1->n_div + bmap2->n_div,
5788                         bmap1->n_eq + bmap2->n_eq,
5789                         bmap1->n_ineq + bmap2->n_ineq);
5790         bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
5791         bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
5792         bmap = isl_basic_map_simplify(bmap);
5793         return isl_basic_map_finalize(bmap);
5794 error:
5795         isl_basic_map_free(bmap1);
5796         isl_basic_map_free(bmap2);
5797         return NULL;
5798 }
5799
5800 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
5801  */
5802 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
5803 {
5804         unsigned flags = 0;
5805         struct isl_map *result;
5806         int i, j;
5807
5808         if (!map1 || !map2)
5809                 goto error;
5810
5811         isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
5812                                          map2->dim, isl_dim_param), goto error);
5813
5814         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
5815             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
5816                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
5817
5818         result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
5819                                                    isl_dim_copy(map2->dim)),
5820                                 map1->n * map2->n, flags);
5821         if (!result)
5822                 goto error;
5823         for (i = 0; i < map1->n; ++i)
5824                 for (j = 0; j < map2->n; ++j) {
5825                         struct isl_basic_map *part;
5826                         part = isl_basic_map_product(
5827                                     isl_basic_map_copy(map1->p[i]),
5828                                     isl_basic_map_copy(map2->p[j]));
5829                         if (isl_basic_map_is_empty(part))
5830                                 isl_basic_map_free(part);
5831                         else
5832                                 result = isl_map_add_basic_map(result, part);
5833                         if (!result)
5834                                 goto error;
5835                 }
5836         isl_map_free(map1);
5837         isl_map_free(map2);
5838         return result;
5839 error:
5840         isl_map_free(map1);
5841         isl_map_free(map2);
5842         return NULL;
5843 }
5844
5845 /* Given two set A and B, construct its Cartesian product A x B.
5846  */
5847 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
5848 {
5849         return (struct isl_set *)isl_map_product((struct isl_map *)set1,
5850                                                  (struct isl_map *)set2);
5851 }
5852
5853 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
5854 {
5855         int i;
5856         uint32_t hash = isl_hash_init();
5857         unsigned total;
5858
5859         if (!bset)
5860                 return 0;
5861         bset = isl_basic_set_copy(bset);
5862         bset = isl_basic_set_normalize(bset);
5863         if (!bset)
5864                 return 0;
5865         total = isl_basic_set_total_dim(bset);
5866         isl_hash_byte(hash, bset->n_eq & 0xFF);
5867         for (i = 0; i < bset->n_eq; ++i) {
5868                 uint32_t c_hash;
5869                 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
5870                 isl_hash_hash(hash, c_hash);
5871         }
5872         isl_hash_byte(hash, bset->n_ineq & 0xFF);
5873         for (i = 0; i < bset->n_ineq; ++i) {
5874                 uint32_t c_hash;
5875                 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
5876                 isl_hash_hash(hash, c_hash);
5877         }
5878         isl_hash_byte(hash, bset->n_div & 0xFF);
5879         for (i = 0; i < bset->n_div; ++i) {
5880                 uint32_t c_hash;
5881                 if (isl_int_is_zero(bset->div[i][0]))
5882                         continue;
5883                 isl_hash_byte(hash, i & 0xFF);
5884                 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
5885                 isl_hash_hash(hash, c_hash);
5886         }
5887         isl_basic_set_free(bset);
5888         return hash;
5889 }
5890
5891 uint32_t isl_set_get_hash(struct isl_set *set)
5892 {
5893         int i;
5894         uint32_t hash;
5895
5896         if (!set)
5897                 return 0;
5898         set = isl_set_copy(set);
5899         set = isl_set_normalize(set);
5900         if (!set)
5901                 return 0;
5902
5903         hash = isl_hash_init();
5904         for (i = 0; i < set->n; ++i) {
5905                 uint32_t bset_hash;
5906                 bset_hash = isl_basic_set_get_hash(set->p[i]);
5907                 isl_hash_hash(hash, bset_hash);
5908         }
5909                 
5910         isl_set_free(set);
5911
5912         return hash;
5913 }
5914
5915 /* Check if the value for dimension dim is completely determined
5916  * by the values of the other parameters and variables.
5917  * That is, check if dimension dim is involved in an equality.
5918  */
5919 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
5920 {
5921         int i;
5922         unsigned nparam;
5923
5924         if (!bset)
5925                 return -1;
5926         nparam = isl_basic_set_n_param(bset);
5927         for (i = 0; i < bset->n_eq; ++i)
5928                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
5929                         return 1;
5930         return 0;
5931 }
5932
5933 /* Check if the value for dimension dim is completely determined
5934  * by the values of the other parameters and variables.
5935  * That is, check if dimension dim is involved in an equality
5936  * for each of the subsets.
5937  */
5938 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
5939 {
5940         int i;
5941
5942         if (!set)
5943                 return -1;
5944         for (i = 0; i < set->n; ++i) {
5945                 int unique;
5946                 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
5947                 if (unique != 1)
5948                         return unique;
5949         }
5950         return 1;
5951 }
5952
5953 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
5954         int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
5955 {
5956         int i;
5957
5958         if (!map)
5959                 return -1;
5960
5961         for (i = 0; i < map->n; ++i)
5962                 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
5963                         return -1;
5964
5965         return 0;
5966 }
5967
5968 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
5969         int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
5970 {
5971         int i;
5972
5973         if (!set)
5974                 return -1;
5975
5976         for (i = 0; i < set->n; ++i)
5977                 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
5978                         return -1;
5979
5980         return 0;
5981 }
5982
5983 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
5984 {
5985         struct isl_dim *dim;
5986         struct isl_basic_map *bmap;
5987         unsigned n_set;
5988         unsigned n_div;
5989         unsigned n_param;
5990         unsigned total;
5991         int i, k, l;
5992
5993         set = isl_set_align_divs(set);
5994
5995         if (!set)
5996                 return NULL;
5997
5998         dim = isl_set_get_dim(set);
5999         if (set->n == 0 || set->p[0]->n_div == 0) {
6000                 isl_set_free(set);
6001                 return isl_map_identity(dim);
6002         }
6003
6004         n_div = set->p[0]->n_div;
6005         dim = isl_dim_map(dim);
6006         n_param = isl_dim_size(dim, isl_dim_param);
6007         n_set = isl_dim_size(dim, isl_dim_in);
6008         dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
6009         bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
6010         for (i = 0; i < n_set; ++i)
6011                 bmap = var_equal(bmap, i);
6012
6013         total = n_param + n_set + n_set + n_div;
6014         for (i = 0; i < n_div; ++i) {
6015                 k = isl_basic_map_alloc_inequality(bmap);
6016                 if (k < 0)
6017                         goto error;
6018                 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
6019                 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
6020                 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
6021                             set->p[0]->div[i]+1+1+n_param, n_set + n_div);
6022                 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
6023                             set->p[0]->div[i][0]);
6024
6025                 l = isl_basic_map_alloc_inequality(bmap);
6026                 if (l < 0)
6027                         goto error;
6028                 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
6029                 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
6030                             set->p[0]->div[i][0]);
6031                 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
6032         }
6033
6034         isl_set_free(set);
6035         return isl_map_from_basic_map(bmap);
6036 error:
6037         isl_set_free(set);
6038         isl_basic_map_free(bmap);
6039         return NULL;
6040 }
6041
6042 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
6043 {
6044         unsigned dim;
6045         int size = 0;
6046
6047         if (!bset)
6048                 return -1;
6049
6050         dim = isl_basic_set_total_dim(bset);
6051         size += bset->n_eq * (1 + dim);
6052         size += bset->n_ineq * (1 + dim);
6053         size += bset->n_div * (2 + dim);
6054
6055         return size;
6056 }
6057
6058 int isl_set_size(__isl_keep isl_set *set)
6059 {
6060         int i;
6061         int size = 0;
6062
6063         if (!set)
6064                 return -1;
6065
6066         for (i = 0; i < set->n; ++i)
6067                 size += isl_basic_set_size(set->p[i]);
6068
6069         return size;
6070 }