add isl_pw_qpolynomial_split_dims
[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 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1183         enum isl_dim_type type, unsigned first, unsigned n)
1184 {
1185         int i;
1186
1187         if (!map)
1188                 return NULL;
1189         if (n == 0)
1190                 return map;
1191
1192         map = isl_map_cow(map);
1193         if (!map)
1194                 return NULL;
1195         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1196         first += pos(map->dim, type) - 1;
1197         
1198         for (i = 0; i < map->n; ++i) {
1199                 map->p[i] = isl_basic_map_eliminate_vars(map->p[i], first, n);
1200                 if (!map->p[i])
1201                         goto error;
1202         }
1203         return map;
1204 error:
1205         isl_map_free(map);
1206         return NULL;
1207 }
1208
1209 /* Eliminate the specified n dimensions starting at first from the
1210  * constraints using Fourier-Motzkin.  The dimensions themselves
1211  * are not removed.
1212  */
1213 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1214         enum isl_dim_type type, unsigned first, unsigned n)
1215 {
1216         return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1217 }
1218
1219 /* Eliminate the specified n dimensions starting at first from the
1220  * constraints using Fourier-Motzkin.  The dimensions themselves
1221  * are not removed.
1222  */
1223 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1224         unsigned first, unsigned n)
1225 {
1226         return isl_set_eliminate(set, isl_dim_set, first, n);
1227 }
1228
1229 /* Project out n dimensions starting at first using Fourier-Motzkin */
1230 struct isl_set *isl_set_remove_dims(struct isl_set *set,
1231         unsigned first, unsigned n)
1232 {
1233         set = isl_set_eliminate_dims(set, first, n);
1234         set = isl_set_drop_dims(set, first, n);
1235         return set;
1236 }
1237
1238 struct isl_basic_set *isl_basic_set_remove_divs(struct isl_basic_set *bset)
1239 {
1240         bset = isl_basic_set_eliminate_vars(bset, isl_dim_total(bset->dim),
1241                                                 bset->n_div);
1242         if (!bset)
1243                 return NULL;
1244         bset->n_div = 0;
1245         return bset;
1246 }
1247
1248 struct isl_set *isl_set_remove_divs(struct isl_set *set)
1249 {
1250         int i;
1251
1252         if (!set)
1253                 return NULL;
1254         if (set->n == 0)
1255                 return set;
1256
1257         set = isl_set_cow(set);
1258         if (!set)
1259                 return NULL;
1260         
1261         for (i = 0; i < set->n; ++i) {
1262                 set->p[i] = isl_basic_set_remove_divs(set->p[i]);
1263                 if (!set->p[i])
1264                         goto error;
1265         }
1266         return set;
1267 error:
1268         isl_set_free(set);
1269         return NULL;
1270 }
1271
1272 struct isl_basic_map *isl_basic_map_remove(struct isl_basic_map *bmap,
1273         enum isl_dim_type type, unsigned first, unsigned n)
1274 {
1275         if (!bmap)
1276                 return NULL;
1277         isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1278                         goto error);
1279         if (n == 0)
1280                 return bmap;
1281         bmap = isl_basic_map_eliminate_vars(bmap,
1282                         isl_basic_map_offset(bmap, type) - 1 + first, n);
1283         if (!bmap)
1284                 return bmap;
1285         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1286                 return bmap;
1287         bmap = isl_basic_map_drop(bmap, type, first, n);
1288         return bmap;
1289 error:
1290         isl_basic_map_free(bmap);
1291         return NULL;
1292 }
1293
1294 __isl_give isl_basic_set *isl_basic_set_remove(__isl_take isl_basic_set *bset,
1295         enum isl_dim_type type, unsigned first, unsigned n)
1296 {
1297         return (isl_basic_set *)
1298                 isl_basic_map_remove((isl_basic_map *)bset, type, first, n);
1299 }
1300
1301 struct isl_map *isl_map_remove(struct isl_map *map,
1302         enum isl_dim_type type, unsigned first, unsigned n)
1303 {
1304         int i;
1305
1306         if (n == 0)
1307                 return map;
1308
1309         map = isl_map_cow(map);
1310         if (!map)
1311                 return NULL;
1312         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
1313         
1314         for (i = 0; i < map->n; ++i) {
1315                 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
1316                         isl_basic_map_offset(map->p[i], type) - 1 + first, n);
1317                 if (!map->p[i])
1318                         goto error;
1319         }
1320         map = isl_map_drop(map, type, first, n);
1321         return map;
1322 error:
1323         isl_map_free(map);
1324         return NULL;
1325 }
1326
1327 __isl_give isl_set *isl_set_remove(__isl_take isl_set *bset,
1328         enum isl_dim_type type, unsigned first, unsigned n)
1329 {
1330         return (isl_set *)isl_map_remove((isl_map *)bset, type, first, n);
1331 }
1332
1333 /* Project out n inputs starting at first using Fourier-Motzkin */
1334 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
1335         unsigned first, unsigned n)
1336 {
1337         return isl_map_remove(map, isl_dim_in, first, n);
1338 }
1339
1340 /* Project out n dimensions starting at first using Fourier-Motzkin */
1341 struct isl_basic_set *isl_basic_set_remove_dims(struct isl_basic_set *bset,
1342         unsigned first, unsigned n)
1343 {
1344         unsigned nparam = isl_basic_set_n_param(bset);
1345         bset = isl_basic_set_eliminate_vars(bset, nparam + first, n);
1346         bset = isl_basic_set_drop_dims(bset, first, n);
1347         return bset;
1348 }
1349
1350 static void dump_term(struct isl_basic_map *bmap,
1351                         isl_int c, int pos, FILE *out)
1352 {
1353         const char *name;
1354         unsigned in = isl_basic_map_n_in(bmap);
1355         unsigned dim = in + isl_basic_map_n_out(bmap);
1356         unsigned nparam = isl_basic_map_n_param(bmap);
1357         if (!pos)
1358                 isl_int_print(out, c, 0);
1359         else {
1360                 if (!isl_int_is_one(c))
1361                         isl_int_print(out, c, 0);
1362                 if (pos < 1 + nparam) {
1363                         name = isl_dim_get_name(bmap->dim,
1364                                                 isl_dim_param, pos - 1);
1365                         if (name)
1366                                 fprintf(out, "%s", name);
1367                         else
1368                                 fprintf(out, "p%d", pos - 1);
1369                 } else if (pos < 1 + nparam + in)
1370                         fprintf(out, "i%d", pos - 1 - nparam);
1371                 else if (pos < 1 + nparam + dim)
1372                         fprintf(out, "o%d", pos - 1 - nparam - in);
1373                 else
1374                         fprintf(out, "e%d", pos - 1 - nparam - dim);
1375         }
1376 }
1377
1378 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
1379                                 int sign, FILE *out)
1380 {
1381         int i;
1382         int first;
1383         unsigned len = 1 + isl_basic_map_total_dim(bmap);
1384         isl_int v;
1385
1386         isl_int_init(v);
1387         for (i = 0, first = 1; i < len; ++i) {
1388                 if (isl_int_sgn(c[i]) * sign <= 0)
1389                         continue;
1390                 if (!first)
1391                         fprintf(out, " + ");
1392                 first = 0;
1393                 isl_int_abs(v, c[i]);
1394                 dump_term(bmap, v, i, out);
1395         }
1396         isl_int_clear(v);
1397         if (first)
1398                 fprintf(out, "0");
1399 }
1400
1401 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
1402                                 const char *op, FILE *out, int indent)
1403 {
1404         int i;
1405
1406         fprintf(out, "%*s", indent, "");
1407
1408         dump_constraint_sign(bmap, c, 1, out);
1409         fprintf(out, " %s ", op);
1410         dump_constraint_sign(bmap, c, -1, out);
1411
1412         fprintf(out, "\n");
1413
1414         for (i = bmap->n_div; i < bmap->extra; ++i) {
1415                 if (isl_int_is_zero(c[1+isl_dim_total(bmap->dim)+i]))
1416                         continue;
1417                 fprintf(out, "%*s", indent, "");
1418                 fprintf(out, "ERROR: unused div coefficient not zero\n");
1419                 abort();
1420         }
1421 }
1422
1423 static void dump_constraints(struct isl_basic_map *bmap,
1424                                 isl_int **c, unsigned n,
1425                                 const char *op, FILE *out, int indent)
1426 {
1427         int i;
1428
1429         for (i = 0; i < n; ++i)
1430                 dump_constraint(bmap, c[i], op, out, indent);
1431 }
1432
1433 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
1434 {
1435         int j;
1436         int first = 1;
1437         unsigned total = isl_basic_map_total_dim(bmap);
1438
1439         for (j = 0; j < 1 + total; ++j) {
1440                 if (isl_int_is_zero(exp[j]))
1441                         continue;
1442                 if (!first && isl_int_is_pos(exp[j]))
1443                         fprintf(out, "+");
1444                 dump_term(bmap, exp[j], j, out);
1445                 first = 0;
1446         }
1447 }
1448
1449 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
1450 {
1451         int i;
1452
1453         dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
1454         dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
1455
1456         for (i = 0; i < bmap->n_div; ++i) {
1457                 fprintf(out, "%*s", indent, "");
1458                 fprintf(out, "e%d = [(", i);
1459                 dump_affine(bmap, bmap->div[i]+1, out);
1460                 fprintf(out, ")/");
1461                 isl_int_print(out, bmap->div[i][0], 0);
1462                 fprintf(out, "]\n");
1463         }
1464 }
1465
1466 void isl_basic_set_dump(struct isl_basic_set *bset, FILE *out, int indent)
1467 {
1468         if (!bset) {
1469                 fprintf(out, "null basic set\n");
1470                 return;
1471         }
1472
1473         fprintf(out, "%*s", indent, "");
1474         fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
1475                         bset->ref, bset->dim->nparam, bset->dim->n_out,
1476                         bset->extra, bset->flags);
1477         dump((struct isl_basic_map *)bset, out, indent);
1478 }
1479
1480 void isl_basic_map_dump(struct isl_basic_map *bmap, FILE *out, int indent)
1481 {
1482         if (!bmap) {
1483                 fprintf(out, "null basic map\n");
1484                 return;
1485         }
1486
1487         fprintf(out, "%*s", indent, "");
1488         fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
1489                         "flags: %x, n_name: %d\n",
1490                 bmap->ref,
1491                 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
1492                 bmap->extra, bmap->flags, bmap->dim->n_name);
1493         dump(bmap, out, indent);
1494 }
1495
1496 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
1497 {
1498         unsigned total;
1499         if (!bmap)
1500                 return -1;
1501         total = isl_basic_map_total_dim(bmap);
1502         isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1503         isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
1504         isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
1505         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1506         return 0;
1507 }
1508
1509 struct isl_set *isl_set_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
1510 {
1511         struct isl_set *set;
1512
1513         if (!dim)
1514                 return NULL;
1515         isl_assert(dim->ctx, dim->n_in == 0, return NULL);
1516         isl_assert(dim->ctx, n >= 0, return NULL);
1517         set = isl_alloc(dim->ctx, struct isl_set,
1518                         sizeof(struct isl_set) +
1519                         (n - 1) * sizeof(struct isl_basic_set *));
1520         if (!set)
1521                 goto error;
1522
1523         set->ctx = dim->ctx;
1524         isl_ctx_ref(set->ctx);
1525         set->ref = 1;
1526         set->size = n;
1527         set->n = 0;
1528         set->dim = dim;
1529         set->flags = flags;
1530         return set;
1531 error:
1532         isl_dim_free(dim);
1533         return NULL;
1534 }
1535
1536 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
1537                 unsigned nparam, unsigned dim, int n, unsigned flags)
1538 {
1539         struct isl_set *set;
1540         struct isl_dim *dims;
1541
1542         dims = isl_dim_alloc(ctx, nparam, 0, dim);
1543         if (!dims)
1544                 return NULL;
1545
1546         set = isl_set_alloc_dim(dims, n, flags);
1547         return set;
1548 }
1549
1550 /* Make sure "map" has room for at least "n" more basic maps.
1551  */
1552 struct isl_map *isl_map_grow(struct isl_map *map, int n)
1553 {
1554         int i;
1555         struct isl_map *grown = NULL;
1556
1557         if (!map)
1558                 return NULL;
1559         isl_assert(map->ctx, n >= 0, goto error);
1560         if (map->n + n <= map->size)
1561                 return map;
1562         grown = isl_map_alloc_dim(isl_map_get_dim(map), map->n + n, map->flags);
1563         if (!grown)
1564                 goto error;
1565         for (i = 0; i < map->n; ++i) {
1566                 grown->p[i] = isl_basic_map_copy(map->p[i]);
1567                 if (!grown->p[i])
1568                         goto error;
1569                 grown->n++;
1570         }
1571         isl_map_free(map);
1572         return grown;
1573 error:
1574         isl_map_free(grown);
1575         isl_map_free(map);
1576         return NULL;
1577 }
1578
1579 /* Make sure "set" has room for at least "n" more basic sets.
1580  */
1581 struct isl_set *isl_set_grow(struct isl_set *set, int n)
1582 {
1583         return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
1584 }
1585
1586 struct isl_set *isl_set_dup(struct isl_set *set)
1587 {
1588         int i;
1589         struct isl_set *dup;
1590
1591         if (!set)
1592                 return NULL;
1593
1594         dup = isl_set_alloc_dim(isl_dim_copy(set->dim), set->n, set->flags);
1595         if (!dup)
1596                 return NULL;
1597         for (i = 0; i < set->n; ++i)
1598                 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
1599         return dup;
1600 }
1601
1602 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
1603 {
1604         struct isl_set *set;
1605
1606         if (!bset)
1607                 return NULL;
1608
1609         set = isl_set_alloc_dim(isl_dim_copy(bset->dim), 1, ISL_MAP_DISJOINT);
1610         if (!set) {
1611                 isl_basic_set_free(bset);
1612                 return NULL;
1613         }
1614         return isl_set_add_basic_set(set, bset);
1615 }
1616
1617 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
1618 {
1619         struct isl_map *map;
1620
1621         if (!bmap)
1622                 return NULL;
1623
1624         map = isl_map_alloc_dim(isl_dim_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
1625         if (!map) {
1626                 isl_basic_map_free(bmap);
1627                 return NULL;
1628         }
1629         return isl_map_add_basic_map(map, bmap);
1630 }
1631
1632 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
1633                                                 __isl_take isl_basic_set *bset)
1634 {
1635         return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
1636                                                 (struct isl_basic_map *)bset);
1637 }
1638
1639 void isl_set_free(struct isl_set *set)
1640 {
1641         int i;
1642
1643         if (!set)
1644                 return;
1645
1646         if (--set->ref > 0)
1647                 return;
1648
1649         isl_ctx_deref(set->ctx);
1650         for (i = 0; i < set->n; ++i)
1651                 isl_basic_set_free(set->p[i]);
1652         isl_dim_free(set->dim);
1653         free(set);
1654 }
1655
1656 void isl_set_dump(struct isl_set *set, FILE *out, int indent)
1657 {
1658         int i;
1659
1660         if (!set) {
1661                 fprintf(out, "null set\n");
1662                 return;
1663         }
1664
1665         fprintf(out, "%*s", indent, "");
1666         fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
1667                         set->ref, set->n, set->dim->nparam, set->dim->n_out,
1668                         set->flags);
1669         for (i = 0; i < set->n; ++i) {
1670                 fprintf(out, "%*s", indent, "");
1671                 fprintf(out, "basic set %d:\n", i);
1672                 isl_basic_set_dump(set->p[i], out, indent+4);
1673         }
1674 }
1675
1676 void isl_map_dump(struct isl_map *map, FILE *out, int indent)
1677 {
1678         int i;
1679
1680         if (!map) {
1681                 fprintf(out, "null map\n");
1682                 return;
1683         }
1684
1685         fprintf(out, "%*s", indent, "");
1686         fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
1687                      "flags: %x, n_name: %d\n",
1688                         map->ref, map->n, map->dim->nparam, map->dim->n_in,
1689                         map->dim->n_out, map->flags, map->dim->n_name);
1690         for (i = 0; i < map->n; ++i) {
1691                 fprintf(out, "%*s", indent, "");
1692                 fprintf(out, "basic map %d:\n", i);
1693                 isl_basic_map_dump(map->p[i], out, indent+4);
1694         }
1695 }
1696
1697 struct isl_basic_map *isl_basic_map_intersect_domain(
1698                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1699 {
1700         struct isl_basic_map *bmap_domain;
1701         struct isl_dim *dim;
1702
1703         if (!bmap || !bset)
1704                 goto error;
1705
1706         isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1707                                         bset->dim, isl_dim_param), goto error);
1708
1709         if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1710                 isl_assert(bset->ctx,
1711                     isl_basic_map_compatible_domain(bmap, bset), goto error);
1712
1713         bmap = isl_basic_map_cow(bmap);
1714         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1715                         bset->n_div, bset->n_eq, bset->n_ineq);
1716         if (!bmap)
1717                 goto error;
1718         dim = isl_dim_reverse(isl_dim_copy(bset->dim));
1719         bmap_domain = isl_basic_map_from_basic_set(bset, dim);
1720         bmap = add_constraints(bmap, bmap_domain, 0, 0);
1721
1722         bmap = isl_basic_map_simplify(bmap);
1723         return isl_basic_map_finalize(bmap);
1724 error:
1725         isl_basic_map_free(bmap);
1726         isl_basic_set_free(bset);
1727         return NULL;
1728 }
1729
1730 struct isl_basic_map *isl_basic_map_intersect_range(
1731                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
1732 {
1733         struct isl_basic_map *bmap_range;
1734
1735         if (!bmap || !bset)
1736                 goto error;
1737
1738         isl_assert(bset->ctx, isl_dim_match(bmap->dim, isl_dim_param,
1739                                         bset->dim, isl_dim_param), goto error);
1740
1741         if (isl_dim_size(bset->dim, isl_dim_set) != 0)
1742                 isl_assert(bset->ctx,
1743                     isl_basic_map_compatible_range(bmap, bset), goto error);
1744
1745         bmap = isl_basic_map_cow(bmap);
1746         bmap = isl_basic_map_extend_dim(bmap, isl_dim_copy(bmap->dim),
1747                         bset->n_div, bset->n_eq, bset->n_ineq);
1748         if (!bmap)
1749                 goto error;
1750         bmap_range = isl_basic_map_from_basic_set(bset, isl_dim_copy(bset->dim));
1751         bmap = add_constraints(bmap, bmap_range, 0, 0);
1752
1753         bmap = isl_basic_map_simplify(bmap);
1754         return isl_basic_map_finalize(bmap);
1755 error:
1756         isl_basic_map_free(bmap);
1757         isl_basic_set_free(bset);
1758         return NULL;
1759 }
1760
1761 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
1762 {
1763         int i;
1764         unsigned total;
1765         isl_int s;
1766
1767         total = 1 + isl_basic_map_total_dim(bmap);
1768         if (total != vec->size)
1769                 return -1;
1770
1771         isl_int_init(s);
1772
1773         for (i = 0; i < bmap->n_eq; ++i) {
1774                 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
1775                 if (!isl_int_is_zero(s)) {
1776                         isl_int_clear(s);
1777                         return 0;
1778                 }
1779         }
1780
1781         for (i = 0; i < bmap->n_ineq; ++i) {
1782                 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
1783                 if (isl_int_is_neg(s)) {
1784                         isl_int_clear(s);
1785                         return 0;
1786                 }
1787         }
1788
1789         isl_int_clear(s);
1790
1791         return 1;
1792 }
1793
1794 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
1795 {
1796         return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
1797 }
1798
1799 struct isl_basic_map *isl_basic_map_intersect(
1800                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
1801 {
1802         struct isl_vec *sample = NULL;
1803
1804         if (!bmap1 || !bmap2)
1805                 goto error;
1806
1807         isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
1808                                      bmap2->dim, isl_dim_param), goto error);
1809         if (isl_dim_total(bmap1->dim) ==
1810                                 isl_dim_size(bmap1->dim, isl_dim_param) &&
1811             isl_dim_total(bmap2->dim) !=
1812                                 isl_dim_size(bmap2->dim, isl_dim_param))
1813                 return isl_basic_map_intersect(bmap2, bmap1);
1814
1815         if (isl_dim_total(bmap2->dim) !=
1816                                         isl_dim_size(bmap2->dim, isl_dim_param))
1817                 isl_assert(bmap1->ctx,
1818                             isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
1819
1820         if (bmap1->sample &&
1821             isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
1822             isl_basic_map_contains(bmap2, bmap1->sample) > 0)
1823                 sample = isl_vec_copy(bmap1->sample);
1824         else if (bmap2->sample &&
1825             isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
1826             isl_basic_map_contains(bmap2, bmap2->sample) > 0)
1827                 sample = isl_vec_copy(bmap2->sample);
1828
1829         bmap1 = isl_basic_map_cow(bmap1);
1830         bmap1 = isl_basic_map_extend_dim(bmap1, isl_dim_copy(bmap1->dim),
1831                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
1832         if (!bmap1)
1833                 goto error;
1834         bmap1 = add_constraints(bmap1, bmap2, 0, 0);
1835
1836         if (sample) {
1837                 isl_vec_free(bmap1->sample);
1838                 bmap1->sample = sample;
1839         }
1840
1841         bmap1 = isl_basic_map_simplify(bmap1);
1842         return isl_basic_map_finalize(bmap1);
1843 error:
1844         if (sample)
1845                 isl_vec_free(sample);
1846         isl_basic_map_free(bmap1);
1847         isl_basic_map_free(bmap2);
1848         return NULL;
1849 }
1850
1851 struct isl_basic_set *isl_basic_set_intersect(
1852                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
1853 {
1854         return (struct isl_basic_set *)
1855                 isl_basic_map_intersect(
1856                         (struct isl_basic_map *)bset1,
1857                         (struct isl_basic_map *)bset2);
1858 }
1859
1860 /* Special case of isl_map_intersect, where both map1 and map2
1861  * are convex, without any divs and such that either map1 or map2
1862  * contains a single constraint.  This constraint is then simply
1863  * added to the other map.
1864  */
1865 static __isl_give isl_map *map_intersect_add_constraint(
1866         __isl_take isl_map *map1, __isl_take isl_map *map2)
1867 {
1868         struct isl_basic_map *bmap1;
1869         struct isl_basic_map *bmap2;
1870
1871         isl_assert(map1->ctx, map1->n == 1, goto error);
1872         isl_assert(map2->ctx, map1->n == 1, goto error);
1873         isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
1874         isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
1875
1876         if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
1877                 return isl_map_intersect(map2, map1);
1878
1879         isl_assert(map2->ctx,
1880                     map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
1881
1882         map1 = isl_map_cow(map1);
1883         if (!map1)
1884                 goto error;
1885         map1->p[0] = isl_basic_map_cow(map1->p[0]);
1886         if (map2->p[0]->n_eq == 1)
1887                 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
1888         else
1889                 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
1890                                                         map2->p[0]->ineq[0]);
1891
1892         map1->p[0] = isl_basic_map_simplify(map1->p[0]);
1893         map1->p[0] = isl_basic_map_finalize(map1->p[0]);
1894         if (!map1->p[0])
1895                 goto error;
1896
1897         if (isl_basic_map_fast_is_empty(map1->p[0])) {
1898                 isl_basic_map_free(map1->p[0]);
1899                 map1->n = 0;
1900         }
1901
1902         isl_map_free(map2);
1903
1904         return map1;
1905 error:
1906         isl_map_free(map1);
1907         isl_map_free(map2);
1908         return NULL;
1909 }
1910
1911 struct isl_map *isl_map_intersect(struct isl_map *map1, struct isl_map *map2)
1912 {
1913         unsigned flags = 0;
1914         struct isl_map *result;
1915         int i, j;
1916
1917         if (!map1 || !map2)
1918                 goto error;
1919
1920         if (map1->n == 1 && map2->n == 1 &&
1921             map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
1922             isl_dim_equal(map1->dim, map2->dim) &&
1923             (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
1924              map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
1925                 return map_intersect_add_constraint(map1, map2);
1926         isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
1927                                          map2->dim, isl_dim_param), goto error);
1928         if (isl_dim_total(map1->dim) ==
1929                                 isl_dim_size(map1->dim, isl_dim_param) &&
1930             isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1931                 return isl_map_intersect(map2, map1);
1932
1933         if (isl_dim_total(map2->dim) != isl_dim_size(map2->dim, isl_dim_param))
1934                 isl_assert(map1->ctx,
1935                             isl_dim_equal(map1->dim, map2->dim), goto error);
1936
1937         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
1938             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
1939                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
1940
1941         result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
1942                                 map1->n * map2->n, flags);
1943         if (!result)
1944                 goto error;
1945         for (i = 0; i < map1->n; ++i)
1946                 for (j = 0; j < map2->n; ++j) {
1947                         struct isl_basic_map *part;
1948                         part = isl_basic_map_intersect(
1949                                     isl_basic_map_copy(map1->p[i]),
1950                                     isl_basic_map_copy(map2->p[j]));
1951                         if (isl_basic_map_is_empty(part))
1952                                 isl_basic_map_free(part);
1953                         else
1954                                 result = isl_map_add_basic_map(result, part);
1955                         if (!result)
1956                                 goto error;
1957                 }
1958         isl_map_free(map1);
1959         isl_map_free(map2);
1960         return result;
1961 error:
1962         isl_map_free(map1);
1963         isl_map_free(map2);
1964         return NULL;
1965 }
1966
1967 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
1968 {
1969         return (struct isl_set *)
1970                 isl_map_intersect((struct isl_map *)set1,
1971                                   (struct isl_map *)set2);
1972 }
1973
1974 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
1975 {
1976         struct isl_dim *dim;
1977         struct isl_basic_set *bset;
1978         unsigned in;
1979
1980         if (!bmap)
1981                 return NULL;
1982         bmap = isl_basic_map_cow(bmap);
1983         if (!bmap)
1984                 return NULL;
1985         dim = isl_dim_reverse(isl_dim_copy(bmap->dim));
1986         in = isl_basic_map_n_in(bmap);
1987         bset = isl_basic_set_from_basic_map(bmap);
1988         bset = isl_basic_set_swap_vars(bset, in);
1989         return isl_basic_map_from_basic_set(bset, dim);
1990 }
1991
1992 __isl_give isl_basic_map *isl_basic_map_insert(__isl_take isl_basic_map *bmap,
1993                 enum isl_dim_type type, unsigned pos, unsigned n)
1994 {
1995         struct isl_dim *res_dim;
1996         struct isl_basic_map *res;
1997         struct isl_dim_map *dim_map;
1998         unsigned total, off;
1999         enum isl_dim_type t;
2000
2001         if (n == 0)
2002                 return bmap;
2003
2004         if (!bmap)
2005                 return NULL;
2006
2007         res_dim = isl_dim_insert(isl_basic_map_get_dim(bmap), type, pos, n);
2008
2009         total = isl_basic_map_total_dim(bmap) + n;
2010         dim_map = isl_dim_map_alloc(bmap->ctx, total);
2011         off = 0;
2012         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2013                 if (t != type) {
2014                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
2015                 } else {
2016                         unsigned size = isl_basic_map_dim(bmap, t);
2017                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2018                                                 0, pos, off);
2019                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2020                                                 pos, size - pos, off + pos + n);
2021                 }
2022                 off += isl_dim_size(res_dim, t);
2023         }
2024         isl_dim_map_div(dim_map, bmap, off);
2025
2026         res = isl_basic_map_alloc_dim(res_dim,
2027                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
2028         res = add_constraints_dim_map(res, bmap, dim_map);
2029         res = isl_basic_map_simplify(res);
2030         return isl_basic_map_finalize(res);
2031 }
2032
2033 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
2034                 enum isl_dim_type type, unsigned n)
2035 {
2036         if (!bmap)
2037                 return NULL;
2038         return isl_basic_map_insert(bmap, type,
2039                                         isl_basic_map_dim(bmap, type), n);
2040 }
2041
2042 __isl_give isl_basic_set *isl_basic_set_add(__isl_take isl_basic_set *bset,
2043                 enum isl_dim_type type, unsigned n)
2044 {
2045         if (!bset)
2046                 return NULL;
2047         isl_assert(bset->ctx, type != isl_dim_in, goto error);
2048         return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
2049 error:
2050         isl_basic_set_free(bset);
2051         return NULL;
2052 }
2053
2054 __isl_give isl_map *isl_map_insert(__isl_take isl_map *map,
2055                 enum isl_dim_type type, unsigned pos, unsigned n)
2056 {
2057         int i;
2058
2059         if (n == 0)
2060                 return map;
2061
2062         map = isl_map_cow(map);
2063         if (!map)
2064                 return NULL;
2065
2066         map->dim = isl_dim_insert(map->dim, type, pos, n);
2067         if (!map->dim)
2068                 goto error;
2069
2070         for (i = 0; i < map->n; ++i) {
2071                 map->p[i] = isl_basic_map_insert(map->p[i], type, pos, n);
2072                 if (!map->p[i])
2073                         goto error;
2074         }
2075
2076         return map;
2077 error:
2078         isl_map_free(map);
2079         return NULL;
2080 }
2081
2082 __isl_give isl_map *isl_map_add(__isl_take isl_map *map,
2083                 enum isl_dim_type type, unsigned n)
2084 {
2085         if (!map)
2086                 return NULL;
2087         return isl_map_insert(map, type, isl_map_dim(map, type), n);
2088 }
2089
2090 __isl_give isl_set *isl_set_add(__isl_take isl_set *set,
2091                 enum isl_dim_type type, unsigned n)
2092 {
2093         if (!set)
2094                 return NULL;
2095         isl_assert(set->ctx, type != isl_dim_in, goto error);
2096         return (isl_set *)isl_map_add((isl_map *)set, type, n);
2097 error:
2098         isl_set_free(set);
2099         return NULL;
2100 }
2101
2102 __isl_give isl_basic_map *isl_basic_map_move(__isl_take isl_basic_map *bmap,
2103         enum isl_dim_type dst_type, unsigned dst_pos,
2104         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2105 {
2106         int i;
2107         struct isl_dim_map *dim_map;
2108         struct isl_basic_map *res;
2109         enum isl_dim_type t;
2110         unsigned total, off;
2111
2112         if (!bmap)
2113                 return NULL;
2114         if (n == 0)
2115                 return bmap;
2116
2117         isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
2118                 goto error);
2119
2120         if (dst_type == src_type && dst_pos == src_pos)
2121                 return bmap;
2122
2123         isl_assert(bmap->ctx, dst_type != src_type, goto error);
2124
2125         if (pos(bmap->dim, dst_type) + dst_pos ==
2126             pos(bmap->dim, src_type) + src_pos +
2127                                             ((src_type < dst_type) ? n : 0)) {
2128                 bmap = isl_basic_map_cow(bmap);
2129                 if (!bmap)
2130                         return NULL;
2131
2132                 bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2133                                                 src_type, src_pos, n);
2134                 if (!bmap->dim)
2135                         goto error;
2136
2137                 return bmap;
2138         }
2139
2140         total = isl_basic_map_total_dim(bmap);
2141         dim_map = isl_dim_map_alloc(bmap->ctx, total);
2142
2143         off = 0;
2144         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2145                 unsigned size = isl_dim_size(bmap->dim, t);
2146                 if (t == dst_type) {
2147                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2148                                             0, dst_pos, off);
2149                         off += dst_pos;
2150                         isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
2151                                             src_pos, n, off);
2152                         off += n;
2153                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2154                                             dst_pos, size - dst_pos, off);
2155                         off += size - dst_pos;
2156                 } else if (t == src_type) {
2157                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2158                                             0, src_pos, off);
2159                         off += src_pos;
2160                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2161                                         src_pos + n, size - src_pos - n, off);
2162                         off += size - src_pos - n;
2163                 } else {
2164                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
2165                         off += size;
2166                 }
2167         }
2168         isl_dim_map_div(dim_map, bmap, off + n);
2169
2170         res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2171                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
2172         bmap = add_constraints_dim_map(res, bmap, dim_map);
2173
2174         bmap->dim = isl_dim_move(bmap->dim, dst_type, dst_pos,
2175                                         src_type, src_pos, n);
2176         if (!bmap->dim)
2177                 goto error;
2178
2179         return bmap;
2180 error:
2181         isl_basic_map_free(bmap);
2182         return NULL;
2183 }
2184
2185 __isl_give isl_set *isl_set_move(__isl_take isl_set *set,
2186         enum isl_dim_type dst_type, unsigned dst_pos,
2187         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2188 {
2189         if (!set)
2190                 return NULL;
2191         isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
2192         return (isl_set *)isl_map_move((isl_map *)set, dst_type, dst_pos,
2193                                         src_type, src_pos, n);
2194 error:
2195         isl_set_free(set);
2196         return NULL;
2197 }
2198
2199 __isl_give isl_map *isl_map_move(__isl_take isl_map *map,
2200         enum isl_dim_type dst_type, unsigned dst_pos,
2201         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
2202 {
2203         int i;
2204
2205         if (!map)
2206                 return NULL;
2207         if (n == 0)
2208                 return map;
2209
2210         isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
2211                 goto error);
2212
2213         if (dst_type == src_type && dst_pos == src_pos)
2214                 return map;
2215
2216         isl_assert(map->ctx, dst_type != src_type, goto error);
2217
2218         map = isl_map_cow(map);
2219         if (!map)
2220                 return NULL;
2221
2222         map->dim = isl_dim_move(map->dim, dst_type, dst_pos, src_type, src_pos, n);
2223         if (!map->dim)
2224                 goto error;
2225
2226         for (i = 0; i < map->n; ++i) {
2227                 map->p[i] = isl_basic_map_move(map->p[i], dst_type, dst_pos,
2228                                                 src_type, src_pos, n);
2229                 if (!map->p[i])
2230                         goto error;
2231         }
2232
2233         return map;
2234 error:
2235         isl_map_free(map);
2236         return NULL;
2237 }
2238
2239 /* Move the specified dimensions to the last columns right before
2240  * the divs.  Don't change the dimension specification of bmap.
2241  * That's the responsibility of the caller.
2242  */
2243 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
2244         enum isl_dim_type type, unsigned first, unsigned n)
2245 {
2246         struct isl_dim_map *dim_map;
2247         struct isl_basic_map *res;
2248         enum isl_dim_type t;
2249         unsigned total, off;
2250
2251         if (!bmap)
2252                 return NULL;
2253         if (pos(bmap->dim, type) + first + n == 1 + isl_dim_total(bmap->dim))
2254                 return bmap;
2255
2256         total = isl_basic_map_total_dim(bmap);
2257         dim_map = isl_dim_map_alloc(bmap->ctx, total);
2258
2259         off = 0;
2260         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
2261                 unsigned size = isl_dim_size(bmap->dim, t);
2262                 if (t == type) {
2263                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2264                                             0, first, off);
2265                         off += first;
2266                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2267                                             first, n, total - bmap->n_div - n);
2268                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
2269                                             first + n, size - (first + n), off);
2270                         off += size - (first + n);
2271                 } else {
2272                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
2273                         off += size;
2274                 }
2275         }
2276         isl_dim_map_div(dim_map, bmap, off + n);
2277
2278         res = isl_basic_map_alloc_dim(isl_basic_map_get_dim(bmap),
2279                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
2280         res = add_constraints_dim_map(res, bmap, dim_map);
2281         return res;
2282 }
2283
2284 /* Turn the n dimensions of type type, starting at first
2285  * into existentially quantified variables.
2286  */
2287 __isl_give isl_basic_map *isl_basic_map_project_out(
2288                 __isl_take isl_basic_map *bmap,
2289                 enum isl_dim_type type, unsigned first, unsigned n)
2290 {
2291         int i;
2292         size_t row_size;
2293         isl_int **new_div;
2294         isl_int *old;
2295
2296         if (n == 0)
2297                 return bmap;
2298
2299         if (!bmap)
2300                 return NULL;
2301
2302         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
2303                 return isl_basic_map_remove(bmap, type, first, n);
2304
2305         isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2306                         goto error);
2307
2308         bmap = move_last(bmap, type, first, n);
2309         bmap = isl_basic_map_cow(bmap);
2310
2311         row_size = 1 + isl_dim_total(bmap->dim) + bmap->extra;
2312         old = bmap->block2.data;
2313         bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
2314                                         (bmap->extra + n) * (1 + row_size));
2315         if (!bmap->block2.data)
2316                 goto error;
2317         new_div = isl_alloc_array(ctx, isl_int *, bmap->extra + n);
2318         if (!new_div)
2319                 goto error;
2320         for (i = 0; i < n; ++i) {
2321                 new_div[i] = bmap->block2.data +
2322                                 (bmap->extra + i) * (1 + row_size);
2323                 isl_seq_clr(new_div[i], 1 + row_size);
2324         }
2325         for (i = 0; i < bmap->extra; ++i)
2326                 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
2327         free(bmap->div);
2328         bmap->div = new_div;
2329         bmap->n_div += n;
2330         bmap->extra += n;
2331
2332         bmap->dim = isl_dim_drop(bmap->dim, type, first, n);
2333         if (!bmap->dim)
2334                 goto error;
2335         bmap = isl_basic_map_simplify(bmap);
2336         bmap = isl_basic_map_drop_redundant_divs(bmap);
2337         return isl_basic_map_finalize(bmap);
2338 error:
2339         isl_basic_map_free(bmap);
2340         return NULL;
2341 }
2342
2343 /* Turn the n dimensions of type type, starting at first
2344  * into existentially quantified variables.
2345  */
2346 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
2347                 enum isl_dim_type type, unsigned first, unsigned n)
2348 {
2349         return (isl_basic_set *)isl_basic_map_project_out(
2350                         (isl_basic_map *)bset, type, first, n);
2351 }
2352
2353 /* Turn the n dimensions of type type, starting at first
2354  * into existentially quantified variables.
2355  */
2356 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
2357                 enum isl_dim_type type, unsigned first, unsigned n)
2358 {
2359         int i;
2360
2361         if (!map)
2362                 return NULL;
2363
2364         if (n == 0)
2365                 return map;
2366
2367         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2368
2369         map = isl_map_cow(map);
2370         if (!map)
2371                 return NULL;
2372
2373         map->dim = isl_dim_drop(map->dim, type, first, n);
2374         if (!map->dim)
2375                 goto error;
2376
2377         for (i = 0; i < map->n; ++i) {
2378                 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
2379                 if (!map->p[i])
2380                         goto error;
2381         }
2382
2383         return map;
2384 error:
2385         isl_map_free(map);
2386         return map;
2387 }
2388
2389 /* Turn the n dimensions of type type, starting at first
2390  * into existentially quantified variables.
2391  */
2392 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
2393                 enum isl_dim_type type, unsigned first, unsigned n)
2394 {
2395         return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
2396 }
2397
2398 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
2399 {
2400         int i, j;
2401
2402         for (i = 0; i < n; ++i) {
2403                 j = isl_basic_map_alloc_div(bmap);
2404                 if (j < 0)
2405                         goto error;
2406                 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
2407         }
2408         return bmap;
2409 error:
2410         isl_basic_map_free(bmap);
2411         return NULL;
2412 }
2413
2414 struct isl_basic_map *isl_basic_map_apply_range(
2415                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2416 {
2417         struct isl_dim *dim_result = NULL;
2418         struct isl_basic_map *bmap;
2419         unsigned n_in, n_out, n, nparam, total, pos;
2420         struct isl_dim_map *dim_map1, *dim_map2;
2421
2422         if (!bmap1 || !bmap2)
2423                 goto error;
2424
2425         dim_result = isl_dim_join(isl_dim_copy(bmap1->dim),
2426                                   isl_dim_copy(bmap2->dim));
2427
2428         n_in = isl_basic_map_n_in(bmap1);
2429         n_out = isl_basic_map_n_out(bmap2);
2430         n = isl_basic_map_n_out(bmap1);
2431         nparam = isl_basic_map_n_param(bmap1);
2432
2433         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
2434         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2435         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
2436         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2437         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
2438         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2439         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
2440         isl_dim_map_div(dim_map1, bmap1, pos += n_out);
2441         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2442         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2443         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2444
2445         bmap = isl_basic_map_alloc_dim(dim_result,
2446                         bmap1->n_div + bmap2->n_div + n,
2447                         bmap1->n_eq + bmap2->n_eq,
2448                         bmap1->n_ineq + bmap2->n_ineq);
2449         bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2450         bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2451         bmap = add_divs(bmap, n);
2452         bmap = isl_basic_map_simplify(bmap);
2453         bmap = isl_basic_map_drop_redundant_divs(bmap);
2454         return isl_basic_map_finalize(bmap);
2455 error:
2456         isl_basic_map_free(bmap1);
2457         isl_basic_map_free(bmap2);
2458         return NULL;
2459 }
2460
2461 struct isl_basic_set *isl_basic_set_apply(
2462                 struct isl_basic_set *bset, struct isl_basic_map *bmap)
2463 {
2464         if (!bset || !bmap)
2465                 goto error;
2466
2467         isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
2468                     goto error);
2469
2470         return (struct isl_basic_set *)
2471                 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
2472 error:
2473         isl_basic_set_free(bset);
2474         isl_basic_map_free(bmap);
2475         return NULL;
2476 }
2477
2478 struct isl_basic_map *isl_basic_map_apply_domain(
2479                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2480 {
2481         if (!bmap1 || !bmap2)
2482                 goto error;
2483
2484         isl_assert(bmap1->ctx,
2485             isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
2486         isl_assert(bmap1->ctx,
2487             isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
2488             goto error);
2489
2490         bmap1 = isl_basic_map_reverse(bmap1);
2491         bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
2492         return isl_basic_map_reverse(bmap1);
2493 error:
2494         isl_basic_map_free(bmap1);
2495         isl_basic_map_free(bmap2);
2496         return NULL;
2497 }
2498
2499 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
2500  * A \cap B -> f(A) + f(B)
2501  */
2502 struct isl_basic_map *isl_basic_map_sum(
2503                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2504 {
2505         unsigned n_in, n_out, nparam, total, pos;
2506         struct isl_basic_map *bmap = NULL;
2507         struct isl_dim_map *dim_map1, *dim_map2;
2508         int i;
2509
2510         if (!bmap1 || !bmap2)
2511                 goto error;
2512
2513         isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim),
2514                 goto error);
2515
2516         nparam = isl_basic_map_n_param(bmap1);
2517         n_in = isl_basic_map_n_in(bmap1);
2518         n_out = isl_basic_map_n_out(bmap1);
2519
2520         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
2521         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
2522         dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
2523         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
2524         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
2525         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
2526         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
2527         isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
2528         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
2529         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
2530         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
2531
2532         bmap = isl_basic_map_alloc_dim(isl_dim_copy(bmap1->dim),
2533                         bmap1->n_div + bmap2->n_div + 2 * n_out,
2534                         bmap1->n_eq + bmap2->n_eq + n_out,
2535                         bmap1->n_ineq + bmap2->n_ineq);
2536         for (i = 0; i < n_out; ++i) {
2537                 int j = isl_basic_map_alloc_equality(bmap);
2538                 if (j < 0)
2539                         goto error;
2540                 isl_seq_clr(bmap->eq[j], 1+total);
2541                 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
2542                 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
2543                 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
2544         }
2545         bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
2546         bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
2547         bmap = add_divs(bmap, 2 * n_out);
2548
2549         bmap = isl_basic_map_simplify(bmap);
2550         return isl_basic_map_finalize(bmap);
2551 error:
2552         isl_basic_map_free(bmap);
2553         isl_basic_map_free(bmap1);
2554         isl_basic_map_free(bmap2);
2555         return NULL;
2556 }
2557
2558 /* Given two maps A -> f(A) and B -> g(B), construct a map
2559  * A \cap B -> f(A) + f(B)
2560  */
2561 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
2562 {
2563         struct isl_map *result;
2564         int i, j;
2565
2566         if (!map1 || !map2)
2567                 goto error;
2568
2569         isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
2570
2571         result = isl_map_alloc_dim(isl_dim_copy(map1->dim),
2572                                 map1->n * map2->n, 0);
2573         if (!result)
2574                 goto error;
2575         for (i = 0; i < map1->n; ++i)
2576                 for (j = 0; j < map2->n; ++j) {
2577                         struct isl_basic_map *part;
2578                         part = isl_basic_map_sum(
2579                                     isl_basic_map_copy(map1->p[i]),
2580                                     isl_basic_map_copy(map2->p[j]));
2581                         if (isl_basic_map_is_empty(part))
2582                                 isl_basic_map_free(part);
2583                         else
2584                                 result = isl_map_add_basic_map(result, part);
2585                         if (!result)
2586                                 goto error;
2587                 }
2588         isl_map_free(map1);
2589         isl_map_free(map2);
2590         return result;
2591 error:
2592         isl_map_free(map1);
2593         isl_map_free(map2);
2594         return NULL;
2595 }
2596
2597 /* Given a basic map A -> f(A), construct A -> -f(A).
2598  */
2599 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
2600 {
2601         int i, j;
2602         unsigned off, n;
2603
2604         bmap = isl_basic_map_cow(bmap);
2605         if (!bmap)
2606                 return NULL;
2607
2608         n = isl_basic_map_dim(bmap, isl_dim_out);
2609         off = isl_basic_map_offset(bmap, isl_dim_out);
2610         for (i = 0; i < bmap->n_eq; ++i)
2611                 for (j = 0; j < n; ++j)
2612                         isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
2613         for (i = 0; i < bmap->n_ineq; ++i)
2614                 for (j = 0; j < n; ++j)
2615                         isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
2616         for (i = 0; i < bmap->n_div; ++i)
2617                 for (j = 0; j < n; ++j)
2618                         isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
2619         return isl_basic_map_finalize(bmap);
2620 }
2621
2622 /* Given a map A -> f(A), construct A -> -f(A).
2623  */
2624 struct isl_map *isl_map_neg(struct isl_map *map)
2625 {
2626         int i;
2627
2628         map = isl_map_cow(map);
2629         if (!map)
2630                 return NULL;
2631
2632         for (i = 0; i < map->n; ++i) {
2633                 map->p[i] = isl_basic_map_neg(map->p[i]);
2634                 if (!map->p[i])
2635                         goto error;
2636         }
2637
2638         return map;
2639 error:
2640         isl_map_free(map);
2641         return NULL;
2642 }
2643
2644 /* Given a basic map A -> f(A) and an integer d, construct a basic map
2645  * A -> floor(f(A)/d).
2646  */
2647 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
2648                 isl_int d)
2649 {
2650         unsigned n_in, n_out, nparam, total, pos;
2651         struct isl_basic_map *result = NULL;
2652         struct isl_dim_map *dim_map;
2653         int i;
2654
2655         if (!bmap)
2656                 return NULL;
2657
2658         nparam = isl_basic_map_n_param(bmap);
2659         n_in = isl_basic_map_n_in(bmap);
2660         n_out = isl_basic_map_n_out(bmap);
2661
2662         total = nparam + n_in + n_out + bmap->n_div + n_out;
2663         dim_map = isl_dim_map_alloc(bmap->ctx, total);
2664         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
2665         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
2666         isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
2667         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
2668
2669         result = isl_basic_map_alloc_dim(isl_dim_copy(bmap->dim),
2670                         bmap->n_div + n_out,
2671                         bmap->n_eq, bmap->n_ineq + 2 * n_out);
2672         result = add_constraints_dim_map(result, bmap, dim_map);
2673         result = add_divs(result, n_out);
2674         for (i = 0; i < n_out; ++i) {
2675                 int j;
2676                 j = isl_basic_map_alloc_inequality(result);
2677                 if (j < 0)
2678                         goto error;
2679                 isl_seq_clr(result->ineq[j], 1+total);
2680                 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
2681                 isl_int_set_si(result->ineq[j][1+pos+i], 1);
2682                 j = isl_basic_map_alloc_inequality(result);
2683                 if (j < 0)
2684                         goto error;
2685                 isl_seq_clr(result->ineq[j], 1+total);
2686                 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
2687                 isl_int_set_si(result->ineq[j][1+pos+i], -1);
2688                 isl_int_sub_ui(result->ineq[j][0], d, 1);
2689         }
2690
2691         result = isl_basic_map_simplify(result);
2692         return isl_basic_map_finalize(result);
2693 error:
2694         isl_basic_map_free(result);
2695         return NULL;
2696 }
2697
2698 /* Given a map A -> f(A) and an integer d, construct a map
2699  * A -> floor(f(A)/d).
2700  */
2701 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
2702 {
2703         int i;
2704
2705         map = isl_map_cow(map);
2706         if (!map)
2707                 return NULL;
2708
2709         ISL_F_CLR(map, ISL_MAP_DISJOINT);
2710         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
2711         for (i = 0; i < map->n; ++i) {
2712                 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
2713                 if (!map->p[i])
2714                         goto error;
2715         }
2716
2717         return map;
2718 error:
2719         isl_map_free(map);
2720         return NULL;
2721 }
2722
2723 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
2724 {
2725         int i;
2726         unsigned nparam;
2727         unsigned n_in;
2728
2729         i = isl_basic_map_alloc_equality(bmap);
2730         if (i < 0)
2731                 goto error;
2732         nparam = isl_basic_map_n_param(bmap);
2733         n_in = isl_basic_map_n_in(bmap);
2734         isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
2735         isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
2736         isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
2737         return isl_basic_map_finalize(bmap);
2738 error:
2739         isl_basic_map_free(bmap);
2740         return NULL;
2741 }
2742
2743 /* Add a constraints to "bmap" expressing i_pos < o_pos
2744  */
2745 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
2746 {
2747         int i;
2748         unsigned nparam;
2749         unsigned n_in;
2750
2751         i = isl_basic_map_alloc_inequality(bmap);
2752         if (i < 0)
2753                 goto error;
2754         nparam = isl_basic_map_n_param(bmap);
2755         n_in = isl_basic_map_n_in(bmap);
2756         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2757         isl_int_set_si(bmap->ineq[i][0], -1);
2758         isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
2759         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
2760         return isl_basic_map_finalize(bmap);
2761 error:
2762         isl_basic_map_free(bmap);
2763         return NULL;
2764 }
2765
2766 /* Add a constraints to "bmap" expressing i_pos > o_pos
2767  */
2768 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
2769 {
2770         int i;
2771         unsigned nparam;
2772         unsigned n_in;
2773
2774         i = isl_basic_map_alloc_inequality(bmap);
2775         if (i < 0)
2776                 goto error;
2777         nparam = isl_basic_map_n_param(bmap);
2778         n_in = isl_basic_map_n_in(bmap);
2779         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
2780         isl_int_set_si(bmap->ineq[i][0], -1);
2781         isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
2782         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
2783         return isl_basic_map_finalize(bmap);
2784 error:
2785         isl_basic_map_free(bmap);
2786         return NULL;
2787 }
2788
2789 struct isl_basic_map *isl_basic_map_equal(struct isl_dim *dim, unsigned n_equal)
2790 {
2791         int i;
2792         struct isl_basic_map *bmap;
2793         bmap = isl_basic_map_alloc_dim(dim, 0, n_equal, 0);
2794         if (!bmap)
2795                 return NULL;
2796         for (i = 0; i < n_equal && bmap; ++i)
2797                 bmap = var_equal(bmap, i);
2798         return isl_basic_map_finalize(bmap);
2799 }
2800
2801 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos < o_pos
2802  */
2803 struct isl_basic_map *isl_basic_map_less_at(struct isl_dim *dim, unsigned pos)
2804 {
2805         int i;
2806         struct isl_basic_map *bmap;
2807         bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2808         if (!bmap)
2809                 return NULL;
2810         for (i = 0; i < pos && bmap; ++i)
2811                 bmap = var_equal(bmap, i);
2812         if (bmap)
2813                 bmap = var_less(bmap, pos);
2814         return isl_basic_map_finalize(bmap);
2815 }
2816
2817 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
2818  */
2819 struct isl_basic_map *isl_basic_map_more_at(struct isl_dim *dim, unsigned pos)
2820 {
2821         int i;
2822         struct isl_basic_map *bmap;
2823         bmap = isl_basic_map_alloc_dim(dim, 0, pos, 1);
2824         if (!bmap)
2825                 return NULL;
2826         for (i = 0; i < pos && bmap; ++i)
2827                 bmap = var_equal(bmap, i);
2828         if (bmap)
2829                 bmap = var_more(bmap, pos);
2830         return isl_basic_map_finalize(bmap);
2831 }
2832
2833 static __isl_give isl_map *map_lex_lte(__isl_take isl_dim *dims, int equal)
2834 {
2835         struct isl_map *map;
2836         unsigned dim;
2837         int i;
2838
2839         if (!dims)
2840                 return NULL;
2841         dim = dims->n_out;
2842         map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2843
2844         for (i = 0; i < dim; ++i)
2845                 map = isl_map_add_basic_map(map,
2846                                   isl_basic_map_less_at(isl_dim_copy(dims), i));
2847         if (equal)
2848                 map = isl_map_add_basic_map(map,
2849                                   isl_basic_map_equal(isl_dim_copy(dims), dim));
2850
2851         isl_dim_free(dims);
2852         return map;
2853 }
2854
2855 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_dim *set_dim)
2856 {
2857         return map_lex_lte(isl_dim_map(set_dim), 0);
2858 }
2859
2860 __isl_give isl_map *isl_map_lex_le(__isl_take isl_dim *set_dim)
2861 {
2862         return map_lex_lte(isl_dim_map(set_dim), 1);
2863 }
2864
2865 static __isl_give isl_map *map_lex_gte(__isl_take isl_dim *dims, int equal)
2866 {
2867         struct isl_map *map;
2868         unsigned dim;
2869         int i;
2870
2871         if (!dims)
2872                 return NULL;
2873         dim = dims->n_out;
2874         map = isl_map_alloc_dim(isl_dim_copy(dims), dim + equal, ISL_MAP_DISJOINT);
2875
2876         for (i = 0; i < dim; ++i)
2877                 map = isl_map_add_basic_map(map,
2878                                   isl_basic_map_more_at(isl_dim_copy(dims), i));
2879         if (equal)
2880                 map = isl_map_add_basic_map(map,
2881                                   isl_basic_map_equal(isl_dim_copy(dims), dim));
2882
2883         isl_dim_free(dims);
2884         return map;
2885 }
2886
2887 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_dim *set_dim)
2888 {
2889         return map_lex_gte(isl_dim_map(set_dim), 0);
2890 }
2891
2892 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_dim *set_dim)
2893 {
2894         return map_lex_gte(isl_dim_map(set_dim), 1);
2895 }
2896
2897 struct isl_basic_map *isl_basic_map_from_basic_set(
2898                 struct isl_basic_set *bset, struct isl_dim *dim)
2899 {
2900         struct isl_basic_map *bmap;
2901
2902         bset = isl_basic_set_cow(bset);
2903         if (!bset || !dim)
2904                 goto error;
2905
2906         isl_assert(bset->ctx, isl_dim_compatible(bset->dim, dim), goto error);
2907         isl_dim_free(bset->dim);
2908         bmap = (struct isl_basic_map *) bset;
2909         bmap->dim = dim;
2910         return isl_basic_map_finalize(bmap);
2911 error:
2912         isl_basic_set_free(bset);
2913         isl_dim_free(dim);
2914         return NULL;
2915 }
2916
2917 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
2918 {
2919         if (!bmap)
2920                 goto error;
2921         if (bmap->dim->n_in == 0)
2922                 return (struct isl_basic_set *)bmap;
2923         bmap = isl_basic_map_cow(bmap);
2924         if (!bmap)
2925                 goto error;
2926         bmap->dim = isl_dim_cow(bmap->dim);
2927         if (!bmap->dim)
2928                 goto error;
2929         bmap->dim->n_out += bmap->dim->n_in;
2930         bmap->dim->n_in = 0;
2931         bmap = isl_basic_map_finalize(bmap);
2932         return (struct isl_basic_set *)bmap;
2933 error:
2934         isl_basic_map_free(bmap);
2935         return NULL;
2936 }
2937
2938 /* For a div d = floor(f/m), add the constraints
2939  *
2940  *              f - m d >= 0
2941  *              -(f-(n-1)) + m d >= 0
2942  *
2943  * Note that the second constraint is the negation of
2944  *
2945  *              f - m d >= n
2946  */
2947 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
2948 {
2949         int i, j;
2950         unsigned total = isl_basic_map_total_dim(bmap);
2951         unsigned div_pos = 1 + total - bmap->n_div + div;
2952
2953         i = isl_basic_map_alloc_inequality(bmap);
2954         if (i < 0)
2955                 return -1;
2956         isl_seq_cpy(bmap->ineq[i], bmap->div[div]+1, 1+total);
2957         isl_int_neg(bmap->ineq[i][div_pos], bmap->div[div][0]);
2958
2959         j = isl_basic_map_alloc_inequality(bmap);
2960         if (j < 0)
2961                 return -1;
2962         isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
2963         isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][div_pos]);
2964         isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
2965         return j;
2966 }
2967
2968 struct isl_basic_set *isl_basic_map_underlying_set(
2969                 struct isl_basic_map *bmap)
2970 {
2971         if (!bmap)
2972                 goto error;
2973         if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 && bmap->n_div == 0)
2974                 return (struct isl_basic_set *)bmap;
2975         bmap = isl_basic_map_cow(bmap);
2976         if (!bmap)
2977                 goto error;
2978         bmap->dim = isl_dim_underlying(bmap->dim, bmap->n_div);
2979         if (!bmap->dim)
2980                 goto error;
2981         bmap->extra -= bmap->n_div;
2982         bmap->n_div = 0;
2983         bmap = isl_basic_map_finalize(bmap);
2984         return (struct isl_basic_set *)bmap;
2985 error:
2986         return NULL;
2987 }
2988
2989 __isl_give isl_basic_set *isl_basic_set_underlying_set(
2990                 __isl_take isl_basic_set *bset)
2991 {
2992         return isl_basic_map_underlying_set((isl_basic_map *)bset);
2993 }
2994
2995 struct isl_basic_map *isl_basic_map_overlying_set(
2996         struct isl_basic_set *bset, struct isl_basic_map *like)
2997 {
2998         struct isl_basic_map *bmap;
2999         struct isl_ctx *ctx;
3000         unsigned total;
3001         int i;
3002
3003         if (!bset || !like)
3004                 goto error;
3005         ctx = bset->ctx;
3006         isl_assert(ctx, bset->n_div == 0, goto error);
3007         isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
3008         isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
3009                         goto error);
3010         if (isl_dim_equal(bset->dim, like->dim) && like->n_div == 0) {
3011                 isl_basic_map_free(like);
3012                 return (struct isl_basic_map *)bset;
3013         }
3014         bset = isl_basic_set_cow(bset);
3015         if (!bset)
3016                 goto error;
3017         total = bset->dim->n_out + bset->extra;
3018         bmap = (struct isl_basic_map *)bset;
3019         isl_dim_free(bmap->dim);
3020         bmap->dim = isl_dim_copy(like->dim);
3021         if (!bmap->dim)
3022                 goto error;
3023         bmap->n_div = like->n_div;
3024         bmap->extra += like->n_div;
3025         if (bmap->extra) {
3026                 unsigned ltotal;
3027                 ltotal = total - bmap->extra + like->extra;
3028                 if (ltotal > total)
3029                         ltotal = total;
3030                 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
3031                                         bmap->extra * (1 + 1 + total));
3032                 if (isl_blk_is_error(bmap->block2))
3033                         goto error;
3034                 bmap->div = isl_realloc_array(ctx, bmap->div, isl_int *,
3035                                                 bmap->extra);
3036                 if (!bmap->div)
3037                         goto error;
3038                 for (i = 0; i < bmap->extra; ++i)
3039                         bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
3040                 for (i = 0; i < like->n_div; ++i) {
3041                         isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
3042                         isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
3043                 }
3044                 bmap = isl_basic_map_extend_constraints(bmap, 
3045                                                         0, 2 * like->n_div);
3046                 for (i = 0; i < like->n_div; ++i) {
3047                         if (isl_int_is_zero(bmap->div[i][0]))
3048                                 continue;
3049                         if (isl_basic_map_add_div_constraints(bmap, i) < 0)
3050                                 goto error;
3051                 }
3052         }
3053         isl_basic_map_free(like);
3054         bmap = isl_basic_map_simplify(bmap);
3055         bmap = isl_basic_map_finalize(bmap);
3056         return bmap;
3057 error:
3058         isl_basic_map_free(like);
3059         isl_basic_set_free(bset);
3060         return NULL;
3061 }
3062
3063 struct isl_basic_set *isl_basic_set_from_underlying_set(
3064         struct isl_basic_set *bset, struct isl_basic_set *like)
3065 {
3066         return (struct isl_basic_set *)
3067                 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
3068 }
3069
3070 struct isl_set *isl_set_from_underlying_set(
3071         struct isl_set *set, struct isl_basic_set *like)
3072 {
3073         int i;
3074
3075         if (!set || !like)
3076                 goto error;
3077         isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
3078                     goto error);
3079         if (isl_dim_equal(set->dim, like->dim) && like->n_div == 0) {
3080                 isl_basic_set_free(like);
3081                 return set;
3082         }
3083         set = isl_set_cow(set);
3084         if (!set)
3085                 goto error;
3086         for (i = 0; i < set->n; ++i) {
3087                 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
3088                                                       isl_basic_set_copy(like));
3089                 if (!set->p[i])
3090                         goto error;
3091         }
3092         isl_dim_free(set->dim);
3093         set->dim = isl_dim_copy(like->dim);
3094         if (!set->dim)
3095                 goto error;
3096         isl_basic_set_free(like);
3097         return set;
3098 error:
3099         isl_basic_set_free(like);
3100         isl_set_free(set);
3101         return NULL;
3102 }
3103
3104 struct isl_set *isl_map_underlying_set(struct isl_map *map)
3105 {
3106         int i;
3107
3108         map = isl_map_cow(map);
3109         if (!map)
3110                 return NULL;
3111         map->dim = isl_dim_cow(map->dim);
3112         if (!map->dim)
3113                 goto error;
3114
3115         for (i = 1; i < map->n; ++i)
3116                 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
3117                                 goto error);
3118         for (i = 0; i < map->n; ++i) {
3119                 map->p[i] = (struct isl_basic_map *)
3120                                 isl_basic_map_underlying_set(map->p[i]);
3121                 if (!map->p[i])
3122                         goto error;
3123         }
3124         if (map->n == 0)
3125                 map->dim = isl_dim_underlying(map->dim, 0);
3126         else {
3127                 isl_dim_free(map->dim);
3128                 map->dim = isl_dim_copy(map->p[0]->dim);
3129         }
3130         if (!map->dim)
3131                 goto error;
3132         return (struct isl_set *)map;
3133 error:
3134         isl_map_free(map);
3135         return NULL;
3136 }
3137
3138 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
3139 {
3140         return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
3141 }
3142
3143 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
3144 {
3145         struct isl_basic_set *domain;
3146         unsigned n_in;
3147         unsigned n_out;
3148         if (!bmap)
3149                 return NULL;
3150         n_in = isl_basic_map_n_in(bmap);
3151         n_out = isl_basic_map_n_out(bmap);
3152         domain = isl_basic_set_from_basic_map(bmap);
3153         return isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
3154 }
3155
3156 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
3157 {
3158         return isl_basic_map_domain(isl_basic_map_reverse(bmap));
3159 }
3160
3161 struct isl_set *isl_map_range(struct isl_map *map)
3162 {
3163         int i;
3164         struct isl_set *set;
3165
3166         if (!map)
3167                 goto error;
3168         if (isl_map_dim(map, isl_dim_in) == 0)
3169                 return (isl_set *)map;
3170
3171         map = isl_map_cow(map);
3172         if (!map)
3173                 goto error;
3174
3175         set = (struct isl_set *) map;
3176         set->dim = isl_dim_drop_inputs(set->dim, 0, set->dim->n_in);
3177         if (!set->dim)
3178                 goto error;
3179         for (i = 0; i < map->n; ++i) {
3180                 set->p[i] = isl_basic_map_range(map->p[i]);
3181                 if (!set->p[i])
3182                         goto error;
3183         }
3184         ISL_F_CLR(set, ISL_MAP_DISJOINT);
3185         ISL_F_CLR(set, ISL_SET_NORMALIZED);
3186         return set;
3187 error:
3188         isl_map_free(map);
3189         return NULL;
3190 }
3191
3192 struct isl_map *isl_map_from_set(struct isl_set *set, struct isl_dim *dim)
3193 {
3194         int i;
3195         struct isl_map *map = NULL;
3196
3197         set = isl_set_cow(set);
3198         if (!set || !dim)
3199                 goto error;
3200         isl_assert(set->ctx, isl_dim_compatible(set->dim, dim), goto error);
3201         map = (struct isl_map *)set;
3202         for (i = 0; i < set->n; ++i) {
3203                 map->p[i] = isl_basic_map_from_basic_set(
3204                                 set->p[i], isl_dim_copy(dim));
3205                 if (!map->p[i])
3206                         goto error;
3207         }
3208         isl_dim_free(map->dim);
3209         map->dim = dim;
3210         return map;
3211 error:
3212         isl_dim_free(dim);
3213         isl_set_free(set);
3214         return NULL;
3215 }
3216
3217 struct isl_map *isl_map_from_range(struct isl_set *set)
3218 {
3219         return (struct isl_map *)set;
3220 }
3221
3222 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
3223 {
3224         return isl_map_reverse(isl_map_from_range(set));;
3225 }
3226
3227 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
3228         __isl_take isl_set *range)
3229 {
3230         return isl_map_product(isl_map_from_domain(domain),
3231                                isl_map_from_range(range));
3232 }
3233
3234 struct isl_set *isl_set_from_map(struct isl_map *map)
3235 {
3236         int i;
3237         struct isl_set *set = NULL;
3238
3239         if (!map)
3240                 return NULL;
3241         map = isl_map_cow(map);
3242         if (!map)
3243                 return NULL;
3244         map->dim = isl_dim_cow(map->dim);
3245         if (!map->dim)
3246                 goto error;
3247         map->dim->n_out += map->dim->n_in;
3248         map->dim->n_in = 0;
3249         set = (struct isl_set *)map;
3250         for (i = 0; i < map->n; ++i) {
3251                 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
3252                 if (!set->p[i])
3253                         goto error;
3254         }
3255         return set;
3256 error:
3257         isl_map_free(map);
3258         return NULL;
3259 }
3260
3261 struct isl_map *isl_map_alloc_dim(struct isl_dim *dim, int n, unsigned flags)
3262 {
3263         struct isl_map *map;
3264
3265         if (!dim)
3266                 return NULL;
3267         isl_assert(dim->ctx, n >= 0, return NULL);
3268         map = isl_alloc(dim->ctx, struct isl_map,
3269                         sizeof(struct isl_map) +
3270                         (n - 1) * sizeof(struct isl_basic_map *));
3271         if (!map)
3272                 goto error;
3273
3274         map->ctx = dim->ctx;
3275         isl_ctx_ref(map->ctx);
3276         map->ref = 1;
3277         map->size = n;
3278         map->n = 0;
3279         map->dim = dim;
3280         map->flags = flags;
3281         return map;
3282 error:
3283         isl_dim_free(dim);
3284         return NULL;
3285 }
3286
3287 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
3288                 unsigned nparam, unsigned in, unsigned out, int n,
3289                 unsigned flags)
3290 {
3291         struct isl_map *map;
3292         struct isl_dim *dims;
3293
3294         dims = isl_dim_alloc(ctx, nparam, in, out);
3295         if (!dims)
3296                 return NULL;
3297
3298         map = isl_map_alloc_dim(dims, n, flags);
3299         return map;
3300 }
3301
3302 struct isl_basic_map *isl_basic_map_empty(struct isl_dim *dim)
3303 {
3304         struct isl_basic_map *bmap;
3305         bmap = isl_basic_map_alloc_dim(dim, 0, 1, 0);
3306         bmap = isl_basic_map_set_to_empty(bmap);
3307         return bmap;
3308 }
3309
3310 struct isl_basic_set *isl_basic_set_empty(struct isl_dim *dim)
3311 {
3312         struct isl_basic_set *bset;
3313         bset = isl_basic_set_alloc_dim(dim, 0, 1, 0);
3314         bset = isl_basic_set_set_to_empty(bset);
3315         return bset;
3316 }
3317
3318 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
3319 {
3320         struct isl_basic_map *bmap;
3321         if (!model)
3322                 return NULL;
3323         bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3324         bmap = isl_basic_map_set_to_empty(bmap);
3325         return bmap;
3326 }
3327
3328 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
3329 {
3330         struct isl_basic_map *bmap;
3331         if (!model)
3332                 return NULL;
3333         bmap = isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3334         bmap = isl_basic_map_set_to_empty(bmap);
3335         return bmap;
3336 }
3337
3338 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
3339 {
3340         struct isl_basic_set *bset;
3341         if (!model)
3342                 return NULL;
3343         bset = isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 1, 0);
3344         bset = isl_basic_set_set_to_empty(bset);
3345         return bset;
3346 }
3347
3348 struct isl_basic_map *isl_basic_map_universe(struct isl_dim *dim)
3349 {
3350         struct isl_basic_map *bmap;
3351         bmap = isl_basic_map_alloc_dim(dim, 0, 0, 0);
3352         return bmap;
3353 }
3354
3355 struct isl_basic_set *isl_basic_set_universe(struct isl_dim *dim)
3356 {
3357         struct isl_basic_set *bset;
3358         bset = isl_basic_set_alloc_dim(dim, 0, 0, 0);
3359         return bset;
3360 }
3361
3362 __isl_give isl_basic_map *isl_basic_map_universe_like(
3363                 __isl_keep isl_basic_map *model)
3364 {
3365         if (!model)
3366                 return NULL;
3367         return isl_basic_map_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3368 }
3369
3370 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
3371 {
3372         if (!model)
3373                 return NULL;
3374         return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3375 }
3376
3377 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
3378         __isl_keep isl_set *model)
3379 {
3380         if (!model)
3381                 return NULL;
3382         return isl_basic_set_alloc_dim(isl_dim_copy(model->dim), 0, 0, 0);
3383 }
3384
3385 struct isl_map *isl_map_empty(struct isl_dim *dim)
3386 {
3387         return isl_map_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3388 }
3389
3390 struct isl_map *isl_map_empty_like(struct isl_map *model)
3391 {
3392         if (!model)
3393                 return NULL;
3394         return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3395 }
3396
3397 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
3398 {
3399         if (!model)
3400                 return NULL;
3401         return isl_map_alloc_dim(isl_dim_copy(model->dim), 0, ISL_MAP_DISJOINT);
3402 }
3403
3404 struct isl_set *isl_set_empty(struct isl_dim *dim)
3405 {
3406         return isl_set_alloc_dim(dim, 0, ISL_MAP_DISJOINT);
3407 }
3408
3409 struct isl_set *isl_set_empty_like(struct isl_set *model)
3410 {
3411         if (!model)
3412                 return NULL;
3413         return isl_set_empty(isl_dim_copy(model->dim));
3414 }
3415
3416 struct isl_map *isl_map_universe(struct isl_dim *dim)
3417 {
3418         struct isl_map *map;
3419         if (!dim)
3420                 return NULL;
3421         map = isl_map_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3422         map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
3423         return map;
3424 }
3425
3426 struct isl_set *isl_set_universe(struct isl_dim *dim)
3427 {
3428         struct isl_set *set;
3429         if (!dim)
3430                 return NULL;
3431         set = isl_set_alloc_dim(isl_dim_copy(dim), 1, ISL_MAP_DISJOINT);
3432         set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
3433         return set;
3434 }
3435
3436 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
3437 {
3438         if (!model)
3439                 return NULL;
3440         return isl_set_universe(isl_dim_copy(model->dim));
3441 }
3442
3443 struct isl_map *isl_map_dup(struct isl_map *map)
3444 {
3445         int i;
3446         struct isl_map *dup;
3447
3448         if (!map)
3449                 return NULL;
3450         dup = isl_map_alloc_dim(isl_dim_copy(map->dim), map->n, map->flags);
3451         for (i = 0; i < map->n; ++i)
3452                 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
3453         return dup;
3454 }
3455
3456 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
3457                                                 __isl_take isl_basic_map *bmap)
3458 {
3459         if (!bmap || !map)
3460                 goto error;
3461         if (isl_basic_map_fast_is_empty(bmap)) {
3462                 isl_basic_map_free(bmap);
3463                 return map;
3464         }
3465         isl_assert(map->ctx, isl_dim_equal(map->dim, bmap->dim), goto error);
3466         isl_assert(map->ctx, map->n < map->size, goto error);
3467         map->p[map->n] = bmap;
3468         map->n++;
3469         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3470         return map;
3471 error:
3472         if (map)
3473                 isl_map_free(map);
3474         if (bmap)
3475                 isl_basic_map_free(bmap);
3476         return NULL;
3477 }
3478
3479 void isl_map_free(struct isl_map *map)
3480 {
3481         int i;
3482
3483         if (!map)
3484                 return;
3485
3486         if (--map->ref > 0)
3487                 return;
3488
3489         isl_ctx_deref(map->ctx);
3490         for (i = 0; i < map->n; ++i)
3491                 isl_basic_map_free(map->p[i]);
3492         isl_dim_free(map->dim);
3493         free(map);
3494 }
3495
3496 struct isl_map *isl_map_extend(struct isl_map *base,
3497                 unsigned nparam, unsigned n_in, unsigned n_out)
3498 {
3499         int i;
3500
3501         base = isl_map_cow(base);
3502         if (!base)
3503                 return NULL;
3504
3505         base->dim = isl_dim_extend(base->dim, nparam, n_in, n_out);
3506         if (!base->dim)
3507                 goto error;
3508         for (i = 0; i < base->n; ++i) {
3509                 base->p[i] = isl_basic_map_extend_dim(base->p[i],
3510                                 isl_dim_copy(base->dim), 0, 0, 0);
3511                 if (!base->p[i])
3512                         goto error;
3513         }
3514         return base;
3515 error:
3516         isl_map_free(base);
3517         return NULL;
3518 }
3519
3520 struct isl_set *isl_set_extend(struct isl_set *base,
3521                 unsigned nparam, unsigned dim)
3522 {
3523         return (struct isl_set *)isl_map_extend((struct isl_map *)base,
3524                                                         nparam, 0, dim);
3525 }
3526
3527 static struct isl_basic_map *isl_basic_map_fix_pos_si(
3528         struct isl_basic_map *bmap, unsigned pos, int value)
3529 {
3530         int j;
3531
3532         bmap = isl_basic_map_cow(bmap);
3533         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3534         j = isl_basic_map_alloc_equality(bmap);
3535         if (j < 0)
3536                 goto error;
3537         isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3538         isl_int_set_si(bmap->eq[j][pos], -1);
3539         isl_int_set_si(bmap->eq[j][0], value);
3540         bmap = isl_basic_map_simplify(bmap);
3541         return isl_basic_map_finalize(bmap);
3542 error:
3543         isl_basic_map_free(bmap);
3544         return NULL;
3545 }
3546
3547 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
3548         __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
3549 {
3550         int j;
3551
3552         bmap = isl_basic_map_cow(bmap);
3553         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
3554         j = isl_basic_map_alloc_equality(bmap);
3555         if (j < 0)
3556                 goto error;
3557         isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
3558         isl_int_set_si(bmap->eq[j][pos], -1);
3559         isl_int_set(bmap->eq[j][0], value);
3560         bmap = isl_basic_map_simplify(bmap);
3561         return isl_basic_map_finalize(bmap);
3562 error:
3563         isl_basic_map_free(bmap);
3564         return NULL;
3565 }
3566
3567 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
3568                 enum isl_dim_type type, unsigned pos, int value)
3569 {
3570         if (!bmap)
3571                 return NULL;
3572         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3573         return isl_basic_map_fix_pos_si(bmap,
3574                 isl_basic_map_offset(bmap, type) + pos, value);
3575 error:
3576         isl_basic_map_free(bmap);
3577         return NULL;
3578 }
3579
3580 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
3581                 enum isl_dim_type type, unsigned pos, isl_int value)
3582 {
3583         if (!bmap)
3584                 return NULL;
3585         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3586         return isl_basic_map_fix_pos(bmap,
3587                 isl_basic_map_offset(bmap, type) + pos, value);
3588 error:
3589         isl_basic_map_free(bmap);
3590         return NULL;
3591 }
3592
3593 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
3594                 enum isl_dim_type type, unsigned pos, int value)
3595 {
3596         return (struct isl_basic_set *)
3597                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3598                                         type, pos, value);
3599 }
3600
3601 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
3602                 enum isl_dim_type type, unsigned pos, isl_int value)
3603 {
3604         return (struct isl_basic_set *)
3605                 isl_basic_map_fix((struct isl_basic_map *)bset,
3606                                         type, pos, value);
3607 }
3608
3609 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
3610                 unsigned input, int value)
3611 {
3612         return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
3613 }
3614
3615 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
3616                 unsigned dim, int value)
3617 {
3618         return (struct isl_basic_set *)
3619                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
3620                                         isl_dim_set, dim, value);
3621 }
3622
3623 struct isl_map *isl_map_fix_si(struct isl_map *map,
3624                 enum isl_dim_type type, unsigned pos, int value)
3625 {
3626         int i;
3627
3628         map = isl_map_cow(map);
3629         if (!map)
3630                 return NULL;
3631
3632         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3633         for (i = 0; i < map->n; ++i) {
3634                 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
3635                 if (!map->p[i])
3636                         goto error;
3637         }
3638         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3639         return map;
3640 error:
3641         isl_map_free(map);
3642         return NULL;
3643 }
3644
3645 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
3646                 enum isl_dim_type type, unsigned pos, int value)
3647 {
3648         return (struct isl_set *)
3649                 isl_map_fix_si((struct isl_map *)set, type, pos, value);
3650 }
3651
3652 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
3653                 enum isl_dim_type type, unsigned pos, isl_int value)
3654 {
3655         int i;
3656
3657         map = isl_map_cow(map);
3658         if (!map)
3659                 return NULL;
3660
3661         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3662         for (i = 0; i < map->n; ++i) {
3663                 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
3664                 if (!map->p[i])
3665                         goto error;
3666         }
3667         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3668         return map;
3669 error:
3670         isl_map_free(map);
3671         return NULL;
3672 }
3673
3674 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
3675                 enum isl_dim_type type, unsigned pos, isl_int value)
3676 {
3677         return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
3678 }
3679
3680 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
3681                 unsigned input, int value)
3682 {
3683         return isl_map_fix_si(map, isl_dim_in, input, value);
3684 }
3685
3686 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
3687 {
3688         return (struct isl_set *)
3689                 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
3690 }
3691
3692 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
3693                 __isl_take isl_basic_map *bmap,
3694                 enum isl_dim_type type, unsigned pos, int value)
3695 {
3696         int j;
3697
3698         if (!bmap)
3699                 return NULL;
3700         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
3701         pos += isl_basic_map_offset(bmap, type);
3702         bmap = isl_basic_map_cow(bmap);
3703         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
3704         j = isl_basic_map_alloc_inequality(bmap);
3705         if (j < 0)
3706                 goto error;
3707         isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
3708         isl_int_set_si(bmap->ineq[j][pos], 1);
3709         isl_int_set_si(bmap->ineq[j][0], -value);
3710         bmap = isl_basic_map_simplify(bmap);
3711         return isl_basic_map_finalize(bmap);
3712 error:
3713         isl_basic_map_free(bmap);
3714         return NULL;
3715 }
3716
3717 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
3718         unsigned dim, isl_int value)
3719 {
3720         int j;
3721
3722         bset = isl_basic_set_cow(bset);
3723         bset = isl_basic_set_extend_constraints(bset, 0, 1);
3724         j = isl_basic_set_alloc_inequality(bset);
3725         if (j < 0)
3726                 goto error;
3727         isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
3728         isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
3729         isl_int_neg(bset->ineq[j][0], value);
3730         bset = isl_basic_set_simplify(bset);
3731         return isl_basic_set_finalize(bset);
3732 error:
3733         isl_basic_set_free(bset);
3734         return NULL;
3735 }
3736
3737 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
3738                 enum isl_dim_type type, unsigned pos, int value)
3739 {
3740         int i;
3741
3742         map = isl_map_cow(map);
3743         if (!map)
3744                 return NULL;
3745
3746         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
3747         for (i = 0; i < map->n; ++i) {
3748                 map->p[i] = isl_basic_map_lower_bound_si(map->p[i],
3749                                                          type, pos, value);
3750                 if (!map->p[i])
3751                         goto error;
3752         }
3753         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3754         return map;
3755 error:
3756         isl_map_free(map);
3757         return NULL;
3758 }
3759
3760 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
3761                 enum isl_dim_type type, unsigned pos, int value)
3762 {
3763         return (struct isl_set *)
3764                 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
3765 }
3766
3767 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
3768                                         isl_int value)
3769 {
3770         int i;
3771
3772         set = isl_set_cow(set);
3773         if (!set)
3774                 return NULL;
3775
3776         isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
3777         for (i = 0; i < set->n; ++i) {
3778                 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
3779                 if (!set->p[i])
3780                         goto error;
3781         }
3782         return set;
3783 error:
3784         isl_set_free(set);
3785         return NULL;
3786 }
3787
3788 struct isl_map *isl_map_reverse(struct isl_map *map)
3789 {
3790         int i;
3791
3792         map = isl_map_cow(map);
3793         if (!map)
3794                 return NULL;
3795
3796         map->dim = isl_dim_reverse(map->dim);
3797         if (!map->dim)
3798                 goto error;
3799         for (i = 0; i < map->n; ++i) {
3800                 map->p[i] = isl_basic_map_reverse(map->p[i]);
3801                 if (!map->p[i])
3802                         goto error;
3803         }
3804         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3805         return map;
3806 error:
3807         isl_map_free(map);
3808         return NULL;
3809 }
3810
3811 static struct isl_map *isl_basic_map_partial_lexopt(
3812                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3813                 struct isl_set **empty, int max)
3814 {
3815         if (!bmap)
3816                 goto error;
3817         if (bmap->ctx->opt->pip == ISL_PIP_PIP)
3818                 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
3819         else
3820                 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
3821 error:
3822         isl_basic_map_free(bmap);
3823         isl_basic_set_free(dom);
3824         if (empty)
3825                 *empty = NULL;
3826         return NULL;
3827 }
3828
3829 struct isl_map *isl_basic_map_partial_lexmax(
3830                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3831                 struct isl_set **empty)
3832 {
3833         return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
3834 }
3835
3836 struct isl_map *isl_basic_map_partial_lexmin(
3837                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
3838                 struct isl_set **empty)
3839 {
3840         return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
3841 }
3842
3843 struct isl_set *isl_basic_set_partial_lexmin(
3844                 struct isl_basic_set *bset, struct isl_basic_set *dom,
3845                 struct isl_set **empty)
3846 {
3847         return (struct isl_set *)
3848                 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
3849                         dom, empty);
3850 }
3851
3852 struct isl_set *isl_basic_set_partial_lexmax(
3853                 struct isl_basic_set *bset, struct isl_basic_set *dom,
3854                 struct isl_set **empty)
3855 {
3856         return (struct isl_set *)
3857                 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
3858                         dom, empty);
3859 }
3860
3861 /* Given a basic map "bmap", compute the lexicograhically minimal
3862  * (or maximal) image element for each domain element in dom.
3863  * Set *empty to those elements in dom that do not have an image element.
3864  *
3865  * We first make sure the basic sets in dom are disjoint and then
3866  * simply collect the results over each of the basic sets separately.
3867  * We could probably improve the efficiency a bit by moving the union
3868  * domain down into the parametric integer programming.
3869  */
3870 static __isl_give isl_map *basic_map_partial_lexopt(
3871                 __isl_take isl_basic_map *bmap, __isl_take isl_set *dom,
3872                 __isl_give isl_set **empty, int max)
3873 {
3874         int i;
3875         struct isl_map *res;
3876
3877         dom = isl_set_make_disjoint(dom);
3878         if (!dom)
3879                 goto error;
3880
3881         if (isl_set_fast_is_empty(dom)) {
3882                 res = isl_map_empty_like_basic_map(bmap);
3883                 *empty = isl_set_empty_like(dom);
3884                 isl_set_free(dom);
3885                 isl_basic_map_free(bmap);
3886                 return res;
3887         }
3888
3889         res = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
3890                         isl_basic_set_copy(dom->p[0]), empty, max);
3891                 
3892         for (i = 1; i < dom->n; ++i) {
3893                 struct isl_map *res_i;
3894                 struct isl_set *empty_i;
3895
3896                 res_i = isl_basic_map_partial_lexopt(isl_basic_map_copy(bmap),
3897                                 isl_basic_set_copy(dom->p[i]), &empty_i, max);
3898
3899                 res = isl_map_union_disjoint(res, res_i);
3900                 *empty = isl_set_union_disjoint(*empty, empty_i);
3901         }
3902
3903         isl_set_free(dom);
3904         isl_basic_map_free(bmap);
3905         return res;
3906 error:
3907         *empty = NULL;
3908         isl_set_free(dom);
3909         isl_basic_map_free(bmap);
3910         return NULL;
3911 }
3912
3913 /* Given a map "map", compute the lexicograhically minimal
3914  * (or maximal) image element for each domain element in dom.
3915  * Set *empty to those elements in dom that do not have an image element.
3916  *
3917  * We first compute the lexicographically minimal or maximal element
3918  * in the first basic map.  This results in a partial solution "res"
3919  * and a subset "todo" of dom that still need to be handled.
3920  * We then consider each of the remaining maps in "map" and successively
3921  * improve both "res" and "todo".
3922  *
3923  * Let res^k and todo^k be the results after k steps and let i = k + 1.
3924  * Assume we are computing the lexicographical maximum.
3925  * We first intersect basic map i with a relation that maps elements
3926  * to elements that are lexicographically larger than the image elements
3927  * in res^k and the compute the maximum image element of this intersection.
3928  * The result ("better") corresponds to those image elements in basic map i
3929  * that are better than what we had before.  The remainder ("keep") are the
3930  * domain elements for which the image element in res_k was better.
3931  * We also compute the lexicographical maximum of basic map i in todo^k.
3932  * res^i is the result of the operation + better + those elements in
3933  *              res^k that we should keep
3934  * todo^i is the remainder of the maximum operation on todo^k.
3935  */
3936 static __isl_give isl_map *isl_map_partial_lexopt(
3937                 __isl_take isl_map *map, __isl_take isl_set *dom,
3938                 __isl_give isl_set **empty, int max)
3939 {
3940         int i;
3941         struct isl_map *res;
3942         struct isl_set *todo;
3943
3944         if (!map || !dom)
3945                 goto error;
3946
3947         if (isl_map_fast_is_empty(map)) {
3948                 if (empty)
3949                         *empty = dom;
3950                 else
3951                         isl_set_free(dom);
3952                 return map;
3953         }
3954
3955         res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
3956                                         isl_set_copy(dom), &todo, max);
3957
3958         for (i = 1; i < map->n; ++i) {
3959                 struct isl_map *lt;
3960                 struct isl_map *better;
3961                 struct isl_set *keep;
3962                 struct isl_map *res_i;
3963                 struct isl_set *todo_i;
3964                 struct isl_dim *dim = isl_map_get_dim(res);
3965
3966                 dim = isl_dim_range(dim);
3967                 if (max)
3968                         lt = isl_map_lex_lt(dim);
3969                 else
3970                         lt = isl_map_lex_gt(dim);
3971                 lt = isl_map_apply_range(isl_map_copy(res), lt);
3972                 lt = isl_map_intersect(lt,
3973                         isl_map_from_basic_map(isl_basic_map_copy(map->p[i])));
3974                 better = isl_map_partial_lexopt(lt,
3975                         isl_map_domain(isl_map_copy(res)),
3976                         &keep, max);
3977
3978                 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
3979                                                 todo, &todo_i, max);
3980
3981                 res = isl_map_intersect_domain(res, keep);
3982                 res = isl_map_union_disjoint(res, res_i);
3983                 res = isl_map_union_disjoint(res, better);
3984                 todo = todo_i;
3985         }
3986
3987         isl_set_free(dom);
3988         isl_map_free(map);
3989
3990         if (empty)
3991                 *empty = todo;
3992         else
3993                 isl_set_free(todo);
3994
3995         return res;
3996 error:
3997         if (empty)
3998                 *empty = NULL;
3999         isl_set_free(dom);
4000         isl_map_free(map);
4001         return NULL;
4002 }
4003
4004 __isl_give isl_map *isl_map_partial_lexmax(
4005                 __isl_take isl_map *map, __isl_take isl_set *dom,
4006                 __isl_give isl_set **empty)
4007 {
4008         return isl_map_partial_lexopt(map, dom, empty, 1);
4009 }
4010
4011 __isl_give isl_map *isl_map_partial_lexmin(
4012                 __isl_take isl_map *map, __isl_take isl_set *dom,
4013                 __isl_give isl_set **empty)
4014 {
4015         return isl_map_partial_lexopt(map, dom, empty, 0);
4016 }
4017
4018 __isl_give isl_set *isl_set_partial_lexmin(
4019                 __isl_take isl_set *set, __isl_take isl_set *dom,
4020                 __isl_give isl_set **empty)
4021 {
4022         return (struct isl_set *)
4023                 isl_map_partial_lexmin((struct isl_map *)set,
4024                         dom, empty);
4025 }
4026
4027 __isl_give isl_set *isl_set_partial_lexmax(
4028                 __isl_take isl_set *set, __isl_take isl_set *dom,
4029                 __isl_give isl_set **empty)
4030 {
4031         return (struct isl_set *)
4032                 isl_map_partial_lexmax((struct isl_map *)set,
4033                         dom, empty);
4034 }
4035
4036 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
4037 {
4038         struct isl_basic_set *dom = NULL;
4039         struct isl_dim *dom_dim;
4040
4041         if (!bmap)
4042                 goto error;
4043         dom_dim = isl_dim_domain(isl_dim_copy(bmap->dim));
4044         dom = isl_basic_set_universe(dom_dim);
4045         return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
4046 error:
4047         isl_basic_map_free(bmap);
4048         return NULL;
4049 }
4050
4051 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
4052 {
4053         return isl_basic_map_lexopt(bmap, 0);
4054 }
4055
4056 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
4057 {
4058         return isl_basic_map_lexopt(bmap, 1);
4059 }
4060
4061 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
4062 {
4063         return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
4064 }
4065
4066 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
4067 {
4068         return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
4069 }
4070
4071 __isl_give isl_map *isl_map_lexopt(__isl_take isl_map *map, int max)
4072 {
4073         struct isl_set *dom = NULL;
4074         struct isl_dim *dom_dim;
4075
4076         if (!map)
4077                 goto error;
4078         dom_dim = isl_dim_domain(isl_dim_copy(map->dim));
4079         dom = isl_set_universe(dom_dim);
4080         return isl_map_partial_lexopt(map, dom, NULL, max);
4081 error:
4082         isl_map_free(map);
4083         return NULL;
4084 }
4085
4086 __isl_give isl_map *isl_map_lexmin(__isl_take isl_map *map)
4087 {
4088         return isl_map_lexopt(map, 0);
4089 }
4090
4091 __isl_give isl_map *isl_map_lexmax(__isl_take isl_map *map)
4092 {
4093         return isl_map_lexopt(map, 1);
4094 }
4095
4096 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
4097 {
4098         return (isl_set *)isl_map_lexmin((isl_map *)set);
4099 }
4100
4101 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
4102 {
4103         return (isl_set *)isl_map_lexmax((isl_map *)set);
4104 }
4105
4106 static struct isl_map *isl_map_reset_dim(struct isl_map *map,
4107         struct isl_dim *dim)
4108 {
4109         int i;
4110
4111         if (!map || !dim)
4112                 goto error;
4113
4114         for (i = 0; i < map->n; ++i) {
4115                 isl_dim_free(map->p[i]->dim);
4116                 map->p[i]->dim = isl_dim_copy(dim);
4117         }
4118         isl_dim_free(map->dim);
4119         map->dim = dim;
4120
4121         return map;
4122 error:
4123         isl_map_free(map);
4124         isl_dim_free(dim);
4125         return NULL;
4126 }
4127
4128 static struct isl_set *isl_set_reset_dim(struct isl_set *set,
4129         struct isl_dim *dim)
4130 {
4131         return (struct isl_set *) isl_map_reset_dim((struct isl_map *)set, dim);
4132 }
4133
4134 /* Apply a preimage specified by "mat" on the parameters of "bset".
4135  * bset is assumed to have only parameters and divs.
4136  */
4137 static struct isl_basic_set *basic_set_parameter_preimage(
4138         struct isl_basic_set *bset, struct isl_mat *mat)
4139 {
4140         unsigned nparam;
4141
4142         if (!bset || !mat)
4143                 goto error;
4144
4145         bset->dim = isl_dim_cow(bset->dim);
4146         if (!bset->dim)
4147                 goto error;
4148
4149         nparam = isl_basic_set_dim(bset, isl_dim_param);
4150
4151         isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
4152
4153         bset->dim->nparam = 0;
4154         bset->dim->n_out = nparam;
4155         bset = isl_basic_set_preimage(bset, mat);
4156         if (bset) {
4157                 bset->dim->nparam = bset->dim->n_out;
4158                 bset->dim->n_out = 0;
4159         }
4160         return bset;
4161 error:
4162         isl_mat_free(mat);
4163         isl_basic_set_free(bset);
4164         return NULL;
4165 }
4166
4167 /* Apply a preimage specified by "mat" on the parameters of "set".
4168  * set is assumed to have only parameters and divs.
4169  */
4170 static struct isl_set *set_parameter_preimage(
4171         struct isl_set *set, struct isl_mat *mat)
4172 {
4173         struct isl_dim *dim = NULL;
4174         unsigned nparam;
4175
4176         if (!set || !mat)
4177                 goto error;
4178
4179         dim = isl_dim_copy(set->dim);
4180         dim = isl_dim_cow(dim);
4181         if (!dim)
4182                 goto error;
4183
4184         nparam = isl_set_dim(set, isl_dim_param);
4185
4186         isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
4187
4188         dim->nparam = 0;
4189         dim->n_out = nparam;
4190         isl_set_reset_dim(set, dim);
4191         set = isl_set_preimage(set, mat);
4192         if (!set)
4193                 goto error2;
4194         dim = isl_dim_copy(set->dim);
4195         dim = isl_dim_cow(dim);
4196         if (!dim)
4197                 goto error2;
4198         dim->nparam = dim->n_out;
4199         dim->n_out = 0;
4200         isl_set_reset_dim(set, dim);
4201         return set;
4202 error:
4203         isl_dim_free(dim);
4204         isl_mat_free(mat);
4205 error2:
4206         isl_set_free(set);
4207         return NULL;
4208 }
4209
4210 /* Intersect the basic set "bset" with the affine space specified by the
4211  * equalities in "eq".
4212  */
4213 static struct isl_basic_set *basic_set_append_equalities(
4214         struct isl_basic_set *bset, struct isl_mat *eq)
4215 {
4216         int i, k;
4217         unsigned len;
4218
4219         if (!bset || !eq)
4220                 goto error;
4221
4222         bset = isl_basic_set_extend_dim(bset, isl_dim_copy(bset->dim), 0,
4223                                         eq->n_row, 0);
4224         if (!bset)
4225                 goto error;
4226
4227         len = 1 + isl_dim_total(bset->dim) + bset->extra;
4228         for (i = 0; i < eq->n_row; ++i) {
4229                 k = isl_basic_set_alloc_equality(bset);
4230                 if (k < 0)
4231                         goto error;
4232                 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
4233                 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
4234         }
4235         isl_mat_free(eq);
4236
4237         return bset;
4238 error:
4239         isl_mat_free(eq);
4240         isl_basic_set_free(bset);
4241         return NULL;
4242 }
4243
4244 /* Intersect the set "set" with the affine space specified by the
4245  * equalities in "eq".
4246  */
4247 static struct isl_set *set_append_equalities(struct isl_set *set,
4248         struct isl_mat *eq)
4249 {
4250         int i;
4251
4252         if (!set || !eq)
4253                 goto error;
4254
4255         for (i = 0; i < set->n; ++i) {
4256                 set->p[i] = basic_set_append_equalities(set->p[i],
4257                                         isl_mat_copy(eq));
4258                 if (!set->p[i])
4259                         goto error;
4260         }
4261         isl_mat_free(eq);
4262         return set;
4263 error:
4264         isl_mat_free(eq);
4265         isl_set_free(set);
4266         return NULL;
4267 }
4268
4269 /* Project the given basic set onto its parameter domain, possibly introducing
4270  * new, explicit, existential variables in the constraints.
4271  * The input has parameters and (possibly implicit) existential variables.
4272  * The output has the same parameters, but only
4273  * explicit existentially quantified variables.
4274  *
4275  * The actual projection is performed by pip, but pip doesn't seem
4276  * to like equalities very much, so we first remove the equalities
4277  * among the parameters by performing a variable compression on
4278  * the parameters.  Afterward, an inverse transformation is performed
4279  * and the equalities among the parameters are inserted back in.
4280  */
4281 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
4282 {
4283         int i, j;
4284         struct isl_mat *eq;
4285         struct isl_mat *T, *T2;
4286         struct isl_set *set;
4287         unsigned nparam, n_div;
4288
4289         bset = isl_basic_set_cow(bset);
4290         if (!bset)
4291                 return NULL;
4292
4293         if (bset->n_eq == 0)
4294                 return isl_basic_set_lexmin(bset);
4295
4296         isl_basic_set_gauss(bset, NULL);
4297
4298         nparam = isl_basic_set_dim(bset, isl_dim_param);
4299         n_div = isl_basic_set_dim(bset, isl_dim_div);
4300
4301         for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
4302                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
4303                         ++i;
4304         }
4305         if (i == bset->n_eq)
4306                 return isl_basic_set_lexmin(bset);
4307
4308         eq = isl_mat_sub_alloc(bset->ctx, bset->eq, i, bset->n_eq - i,
4309                 0, 1 + nparam);
4310         eq = isl_mat_cow(eq);
4311         T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
4312         bset = basic_set_parameter_preimage(bset, T);
4313
4314         set = isl_basic_set_lexmin(bset);
4315         set = set_parameter_preimage(set, T2);
4316         set = set_append_equalities(set, eq);
4317         return set;
4318 }
4319
4320 /* Compute an explicit representation for all the existentially
4321  * quantified variables.
4322  * The input and output dimensions are first turned into parameters.
4323  * compute_divs then returns a map with the same parameters and
4324  * no input or output dimensions and the dimension specification
4325  * is reset to that of the input.
4326  */
4327 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
4328 {
4329         struct isl_basic_set *bset;
4330         struct isl_set *set;
4331         struct isl_map *map;
4332         struct isl_dim *dim, *orig_dim = NULL;
4333         unsigned         nparam;
4334         unsigned         n_in;
4335         unsigned         n_out;
4336
4337         bmap = isl_basic_map_cow(bmap);
4338         if (!bmap)
4339                 return NULL;
4340
4341         nparam = isl_basic_map_dim(bmap, isl_dim_param);
4342         n_in = isl_basic_map_dim(bmap, isl_dim_in);
4343         n_out = isl_basic_map_dim(bmap, isl_dim_out);
4344         dim = isl_dim_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
4345         if (!dim)
4346                 goto error;
4347
4348         orig_dim = bmap->dim;
4349         bmap->dim = dim;
4350         bset = (struct isl_basic_set *)bmap;
4351
4352         set = parameter_compute_divs(bset);
4353         map = (struct isl_map *)set;
4354         map = isl_map_reset_dim(map, orig_dim);
4355
4356         return map;
4357 error:
4358         isl_basic_map_free(bmap);
4359         return NULL;
4360 }
4361
4362 static int basic_map_divs_known(__isl_keep isl_basic_map *bmap)
4363 {
4364         int i;
4365         unsigned off;
4366
4367         if (!bmap)
4368                 return -1;
4369
4370         off = isl_dim_total(bmap->dim);
4371         for (i = 0; i < bmap->n_div; ++i) {
4372                 if (isl_int_is_zero(bmap->div[i][0]))
4373                         return 0;
4374                 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
4375                                 return -1);
4376         }
4377         return 1;
4378 }
4379
4380 static int map_divs_known(__isl_keep isl_map *map)
4381 {
4382         int i;
4383
4384         if (!map)
4385                 return -1;
4386
4387         for (i = 0; i < map->n; ++i) {
4388                 int known = basic_map_divs_known(map->p[i]);
4389                 if (known <= 0)
4390                         return known;
4391         }
4392
4393         return 1;
4394 }
4395
4396 /* If bmap contains any unknown divs, then compute explicit
4397  * expressions for them.  However, this computation may be
4398  * quite expensive, so first try to remove divs that aren't
4399  * strictly needed.
4400  */
4401 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
4402 {
4403         int i;
4404         int known;
4405         struct isl_map *map;
4406
4407         known = basic_map_divs_known(bmap);
4408         if (known < 0)
4409                 goto error;
4410         if (known)
4411                 return isl_map_from_basic_map(bmap);
4412
4413         bmap = isl_basic_map_drop_redundant_divs(bmap);
4414
4415         known = basic_map_divs_known(bmap);
4416         if (known < 0)
4417                 goto error;
4418         if (known)
4419                 return isl_map_from_basic_map(bmap);
4420
4421         map = compute_divs(bmap);
4422         return map;
4423 error:
4424         isl_basic_map_free(bmap);
4425         return NULL;
4426 }
4427
4428 struct isl_map *isl_map_compute_divs(struct isl_map *map)
4429 {
4430         int i;
4431         int known;
4432         struct isl_map *res;
4433
4434         if (!map)
4435                 return NULL;
4436         if (map->n == 0)
4437                 return map;
4438
4439         known = map_divs_known(map);
4440         if (known < 0) {
4441                 isl_map_free(map);
4442                 return NULL;
4443         }
4444         if (known)
4445                 return map;
4446
4447         res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
4448         for (i = 1 ; i < map->n; ++i) {
4449                 struct isl_map *r2;
4450                 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
4451                 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
4452                         res = isl_map_union_disjoint(res, r2);
4453                 else
4454                         res = isl_map_union(res, r2);
4455         }
4456         isl_map_free(map);
4457
4458         return res;
4459 }
4460
4461 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
4462 {
4463         return (struct isl_set *)
4464                 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
4465 }
4466
4467 struct isl_set *isl_set_compute_divs(struct isl_set *set)
4468 {
4469         return (struct isl_set *)
4470                 isl_map_compute_divs((struct isl_map *)set);
4471 }
4472
4473 struct isl_set *isl_map_domain(struct isl_map *map)
4474 {
4475         int i;
4476         struct isl_set *set;
4477
4478         if (!map)
4479                 goto error;
4480
4481         map = isl_map_cow(map);
4482         if (!map)
4483                 return NULL;
4484
4485         set = (struct isl_set *)map;
4486         set->dim = isl_dim_domain(set->dim);
4487         if (!set->dim)
4488                 goto error;
4489         for (i = 0; i < map->n; ++i) {
4490                 set->p[i] = isl_basic_map_domain(map->p[i]);
4491                 if (!set->p[i])
4492                         goto error;
4493         }
4494         ISL_F_CLR(set, ISL_MAP_DISJOINT);
4495         ISL_F_CLR(set, ISL_SET_NORMALIZED);
4496         return set;
4497 error:
4498         isl_map_free(map);
4499         return NULL;
4500 }
4501
4502 struct isl_map *isl_map_union_disjoint(
4503                         struct isl_map *map1, struct isl_map *map2)
4504 {
4505         int i;
4506         unsigned flags = 0;
4507         struct isl_map *map = NULL;
4508
4509         if (!map1 || !map2)
4510                 goto error;
4511
4512         if (map1->n == 0) {
4513                 isl_map_free(map1);
4514                 return map2;
4515         }
4516         if (map2->n == 0) {
4517                 isl_map_free(map2);
4518                 return map1;
4519         }
4520
4521         isl_assert(map1->ctx, isl_dim_equal(map1->dim, map2->dim), goto error);
4522
4523         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
4524             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
4525                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4526
4527         map = isl_map_alloc_dim(isl_dim_copy(map1->dim),
4528                                 map1->n + map2->n, flags);
4529         if (!map)
4530                 goto error;
4531         for (i = 0; i < map1->n; ++i) {
4532                 map = isl_map_add_basic_map(map,
4533                                   isl_basic_map_copy(map1->p[i]));
4534                 if (!map)
4535                         goto error;
4536         }
4537         for (i = 0; i < map2->n; ++i) {
4538                 map = isl_map_add_basic_map(map,
4539                                   isl_basic_map_copy(map2->p[i]));
4540                 if (!map)
4541                         goto error;
4542         }
4543         isl_map_free(map1);
4544         isl_map_free(map2);
4545         return map;
4546 error:
4547         isl_map_free(map);
4548         isl_map_free(map1);
4549         isl_map_free(map2);
4550         return NULL;
4551 }
4552
4553 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
4554 {
4555         map1 = isl_map_union_disjoint(map1, map2);
4556         if (!map1)
4557                 return NULL;
4558         if (map1->n > 1)
4559                 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
4560         return map1;
4561 }
4562
4563 struct isl_set *isl_set_union_disjoint(
4564                         struct isl_set *set1, struct isl_set *set2)
4565 {
4566         return (struct isl_set *)
4567                 isl_map_union_disjoint(
4568                         (struct isl_map *)set1, (struct isl_map *)set2);
4569 }
4570
4571 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
4572 {
4573         return (struct isl_set *)
4574                 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
4575 }
4576
4577 struct isl_map *isl_map_intersect_range(
4578                 struct isl_map *map, struct isl_set *set)
4579 {
4580         unsigned flags = 0;
4581         struct isl_map *result;
4582         int i, j;
4583
4584         if (!map || !set)
4585                 goto error;
4586
4587         if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
4588             ISL_F_ISSET(set, ISL_MAP_DISJOINT))
4589                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
4590
4591         result = isl_map_alloc_dim(isl_dim_copy(map->dim),
4592                                         map->n * set->n, flags);
4593         if (!result)
4594                 goto error;
4595         for (i = 0; i < map->n; ++i)
4596                 for (j = 0; j < set->n; ++j) {
4597                         result = isl_map_add_basic_map(result,
4598                             isl_basic_map_intersect_range(
4599                                 isl_basic_map_copy(map->p[i]),
4600                                 isl_basic_set_copy(set->p[j])));
4601                         if (!result)
4602                                 goto error;
4603                 }
4604         isl_map_free(map);
4605         isl_set_free(set);
4606         return result;
4607 error:
4608         isl_map_free(map);
4609         isl_set_free(set);
4610         return NULL;
4611 }
4612
4613 struct isl_map *isl_map_intersect_domain(
4614                 struct isl_map *map, struct isl_set *set)
4615 {
4616         return isl_map_reverse(
4617                 isl_map_intersect_range(isl_map_reverse(map), set));
4618 }
4619
4620 struct isl_map *isl_map_apply_domain(
4621                 struct isl_map *map1, struct isl_map *map2)
4622 {
4623         if (!map1 || !map2)
4624                 goto error;
4625         map1 = isl_map_reverse(map1);
4626         map1 = isl_map_apply_range(map1, map2);
4627         return isl_map_reverse(map1);
4628 error:
4629         isl_map_free(map1);
4630         isl_map_free(map2);
4631         return NULL;
4632 }
4633
4634 struct isl_map *isl_map_apply_range(
4635                 struct isl_map *map1, struct isl_map *map2)
4636 {
4637         struct isl_dim *dim_result;
4638         struct isl_map *result;
4639         int i, j;
4640
4641         if (!map1 || !map2)
4642                 goto error;
4643
4644         dim_result = isl_dim_join(isl_dim_copy(map1->dim),
4645                                   isl_dim_copy(map2->dim));
4646
4647         result = isl_map_alloc_dim(dim_result, map1->n * map2->n, 0);
4648         if (!result)
4649                 goto error;
4650         for (i = 0; i < map1->n; ++i)
4651                 for (j = 0; j < map2->n; ++j) {
4652                         result = isl_map_add_basic_map(result,
4653                             isl_basic_map_apply_range(
4654                                 isl_basic_map_copy(map1->p[i]),
4655                                 isl_basic_map_copy(map2->p[j])));
4656                         if (!result)
4657                                 goto error;
4658                 }
4659         isl_map_free(map1);
4660         isl_map_free(map2);
4661         if (result && result->n <= 1)
4662                 ISL_F_SET(result, ISL_MAP_DISJOINT);
4663         return result;
4664 error:
4665         isl_map_free(map1);
4666         isl_map_free(map2);
4667         return NULL;
4668 }
4669
4670 /*
4671  * returns range - domain
4672  */
4673 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
4674 {
4675         struct isl_basic_set *bset;
4676         unsigned dim;
4677         unsigned nparam;
4678         int i;
4679
4680         if (!bmap)
4681                 goto error;
4682         dim = isl_basic_map_n_in(bmap);
4683         nparam = isl_basic_map_n_param(bmap);
4684         isl_assert(bmap->ctx, dim == isl_basic_map_n_out(bmap), goto error);
4685         bset = isl_basic_set_from_basic_map(bmap);
4686         bset = isl_basic_set_cow(bset);
4687         bset = isl_basic_set_extend(bset, nparam, 3*dim, 0, dim, 0);
4688         bset = isl_basic_set_swap_vars(bset, 2*dim);
4689         for (i = 0; i < dim; ++i) {
4690                 int j = isl_basic_map_alloc_equality(
4691                                             (struct isl_basic_map *)bset);
4692                 if (j < 0)
4693                         goto error;
4694                 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
4695                 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
4696                 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
4697                 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
4698         }
4699         return isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
4700 error:
4701         isl_basic_map_free(bmap);
4702         return NULL;
4703 }
4704
4705 /*
4706  * returns range - domain
4707  */
4708 struct isl_set *isl_map_deltas(struct isl_map *map)
4709 {
4710         int i;
4711         struct isl_set *result;
4712
4713         if (!map)
4714                 return NULL;
4715
4716         isl_assert(map->ctx, isl_map_n_in(map) == isl_map_n_out(map), goto error);
4717         result = isl_set_alloc(map->ctx, isl_map_n_param(map),
4718                                         isl_map_n_in(map), map->n, map->flags);
4719         if (!result)
4720                 goto error;
4721         for (i = 0; i < map->n; ++i)
4722                 result = isl_set_add_basic_set(result,
4723                           isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
4724         isl_map_free(map);
4725         return result;
4726 error:
4727         isl_map_free(map);
4728         return NULL;
4729 }
4730
4731 static struct isl_basic_map *basic_map_identity(struct isl_dim *dims)
4732 {
4733         struct isl_basic_map *bmap;
4734         unsigned nparam;
4735         unsigned dim;
4736         int i;
4737
4738         if (!dims)
4739                 return NULL;
4740
4741         nparam = dims->nparam;
4742         dim = dims->n_out;
4743         bmap = isl_basic_map_alloc_dim(dims, 0, dim, 0);
4744         if (!bmap)
4745                 goto error;
4746
4747         for (i = 0; i < dim; ++i) {
4748                 int j = isl_basic_map_alloc_equality(bmap);
4749                 if (j < 0)
4750                         goto error;
4751                 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
4752                 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
4753                 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
4754         }
4755         return isl_basic_map_finalize(bmap);
4756 error:
4757         isl_basic_map_free(bmap);
4758         return NULL;
4759 }
4760
4761 struct isl_basic_map *isl_basic_map_identity(struct isl_dim *set_dim)
4762 {
4763         struct isl_dim *dim = isl_dim_map(set_dim);
4764         if (!dim)
4765                 return NULL;
4766         return basic_map_identity(dim);
4767 }
4768
4769 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
4770 {
4771         if (!model || !model->dim)
4772                 return NULL;
4773         isl_assert(model->ctx,
4774                         model->dim->n_in == model->dim->n_out, return NULL);
4775         return basic_map_identity(isl_dim_copy(model->dim));
4776 }
4777
4778 static struct isl_map *map_identity(struct isl_dim *dim)
4779 {
4780         struct isl_map *map = isl_map_alloc_dim(dim, 1, ISL_MAP_DISJOINT);
4781         return isl_map_add_basic_map(map, basic_map_identity(isl_dim_copy(dim)));
4782 }
4783
4784 struct isl_map *isl_map_identity(struct isl_dim *set_dim)
4785 {
4786         struct isl_dim *dim = isl_dim_map(set_dim);
4787         if (!dim)
4788                 return NULL;
4789         return map_identity(dim);
4790 }
4791
4792 struct isl_map *isl_map_identity_like(struct isl_map *model)
4793 {
4794         if (!model || !model->dim)
4795                 return NULL;
4796         isl_assert(model->ctx,
4797                         model->dim->n_in == model->dim->n_out, return NULL);
4798         return map_identity(isl_dim_copy(model->dim));
4799 }
4800
4801 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
4802 {
4803         if (!model || !model->dim)
4804                 return NULL;
4805         isl_assert(model->ctx,
4806                         model->dim->n_in == model->dim->n_out, return NULL);
4807         return map_identity(isl_dim_copy(model->dim));
4808 }
4809
4810 /* Construct a basic set with all set dimensions having only non-negative
4811  * values.
4812  */
4813 struct isl_basic_set *isl_basic_set_positive_orthant(struct isl_dim *dims)
4814 {
4815         int i;
4816         unsigned nparam;
4817         unsigned dim;
4818         struct isl_basic_set *bset;
4819
4820         if (!dims)
4821                 return NULL;
4822         nparam = dims->nparam;
4823         dim = dims->n_out;
4824         bset = isl_basic_set_alloc_dim(dims, 0, 0, dim);
4825         if (!bset)
4826                 return NULL;
4827         for (i = 0; i < dim; ++i) {
4828                 int k = isl_basic_set_alloc_inequality(bset);
4829                 if (k < 0)
4830                         goto error;
4831                 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
4832                 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
4833         }
4834         return bset;
4835 error:
4836         isl_basic_set_free(bset);
4837         return NULL;
4838 }
4839
4840 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
4841         enum isl_dim_type type, unsigned first, unsigned n)
4842 {
4843         int i;
4844         isl_basic_set *nonneg = NULL;
4845         isl_basic_set *neg = NULL;
4846
4847         if (!set)
4848                 return NULL;
4849         if (n == 0)
4850                 return set;
4851
4852         isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
4853
4854         for (i = 0; i < n; ++i) {
4855                 int k;
4856
4857                 neg = NULL;
4858                 nonneg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
4859                 k = isl_basic_set_alloc_inequality(nonneg);
4860                 if (k < 0)
4861                         goto error;
4862                 isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
4863                 isl_int_set_si(nonneg->ineq[k][pos(set->dim, type) + first + i], 1);
4864
4865                 neg = isl_basic_set_alloc_dim(isl_set_get_dim(set), 0, 0, 1);
4866                 k = isl_basic_set_alloc_inequality(neg);
4867                 if (k < 0)
4868                         goto error;
4869                 isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
4870                 isl_int_set_si(neg->ineq[k][0], -1);
4871                 isl_int_set_si(neg->ineq[k][pos(set->dim, type) + first + i], -1);
4872
4873                 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
4874         }
4875
4876         return set;
4877 error:
4878         isl_basic_set_free(nonneg);
4879         isl_basic_set_free(neg);
4880         isl_set_free(set);
4881         return NULL;
4882 }
4883
4884 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
4885 {
4886         return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
4887 }
4888
4889 int isl_basic_map_is_subset(
4890                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4891 {
4892         int is_subset;
4893         struct isl_map *map1;
4894         struct isl_map *map2;
4895
4896         if (!bmap1 || !bmap2)
4897                 return -1;
4898
4899         map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
4900         map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
4901
4902         is_subset = isl_map_is_subset(map1, map2);
4903
4904         isl_map_free(map1);
4905         isl_map_free(map2);
4906
4907         return is_subset;
4908 }
4909
4910 int isl_basic_map_is_equal(
4911                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4912 {
4913         int is_subset;
4914
4915         if (!bmap1 || !bmap2)
4916                 return -1;
4917         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4918         if (is_subset != 1)
4919                 return is_subset;
4920         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4921         return is_subset;
4922 }
4923
4924 int isl_basic_set_is_equal(
4925                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
4926 {
4927         return isl_basic_map_is_equal(
4928                 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
4929 }
4930
4931 int isl_map_is_empty(struct isl_map *map)
4932 {
4933         int i;
4934         int is_empty;
4935
4936         if (!map)
4937                 return -1;
4938         for (i = 0; i < map->n; ++i) {
4939                 is_empty = isl_basic_map_is_empty(map->p[i]);
4940                 if (is_empty < 0)
4941                         return -1;
4942                 if (!is_empty)
4943                         return 0;
4944         }
4945         return 1;
4946 }
4947
4948 int isl_map_fast_is_empty(struct isl_map *map)
4949 {
4950         return map->n == 0;
4951 }
4952
4953 int isl_set_fast_is_empty(struct isl_set *set)
4954 {
4955         return set->n == 0;
4956 }
4957
4958 int isl_set_is_empty(struct isl_set *set)
4959 {
4960         return isl_map_is_empty((struct isl_map *)set);
4961 }
4962
4963 int isl_map_is_equal(struct isl_map *map1, struct isl_map *map2)
4964 {
4965         int is_subset;
4966
4967         if (!map1 || !map2)
4968                 return -1;
4969         is_subset = isl_map_is_subset(map1, map2);
4970         if (is_subset != 1)
4971                 return is_subset;
4972         is_subset = isl_map_is_subset(map2, map1);
4973         return is_subset;
4974 }
4975
4976 int isl_basic_map_is_strict_subset(
4977                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
4978 {
4979         int is_subset;
4980
4981         if (!bmap1 || !bmap2)
4982                 return -1;
4983         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
4984         if (is_subset != 1)
4985                 return is_subset;
4986         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
4987         if (is_subset == -1)
4988                 return is_subset;
4989         return !is_subset;
4990 }
4991
4992 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
4993 {
4994         int is_subset;
4995
4996         if (!map1 || !map2)
4997                 return -1;
4998         is_subset = isl_map_is_subset(map1, map2);
4999         if (is_subset != 1)
5000                 return is_subset;
5001         is_subset = isl_map_is_subset(map2, map1);
5002         if (is_subset == -1)
5003                 return is_subset;
5004         return !is_subset;
5005 }
5006
5007 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
5008 {
5009         return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
5010 }
5011
5012 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
5013 {
5014         if (!bmap)
5015                 return -1;
5016         return bmap->n_eq == 0 && bmap->n_ineq == 0;
5017 }
5018
5019 int isl_basic_set_is_universe(struct isl_basic_set *bset)
5020 {
5021         if (!bset)
5022                 return -1;
5023         return bset->n_eq == 0 && bset->n_ineq == 0;
5024 }
5025
5026 int isl_map_fast_is_universe(__isl_keep isl_map *map)
5027 {
5028         if (!map)
5029                 return -1;
5030
5031         return map->n == 1 && isl_basic_map_is_universe(map->p[0]);
5032 }
5033
5034 int isl_set_fast_is_universe(__isl_keep isl_set *set)
5035 {
5036         return isl_map_fast_is_universe((isl_map *) set);
5037 }
5038
5039 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
5040 {
5041         struct isl_basic_set *bset = NULL;
5042         struct isl_vec *sample = NULL;
5043         int empty;
5044         unsigned total;
5045
5046         if (!bmap)
5047                 return -1;
5048
5049         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
5050                 return 1;
5051
5052         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
5053                 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
5054                 copy = isl_basic_map_convex_hull(copy);
5055                 empty = ISL_F_ISSET(copy, ISL_BASIC_MAP_EMPTY);
5056                 isl_basic_map_free(copy);
5057                 return empty;
5058         }
5059
5060         total = 1 + isl_basic_map_total_dim(bmap);
5061         if (bmap->sample && bmap->sample->size == total) {
5062                 int contains = isl_basic_map_contains(bmap, bmap->sample);
5063                 if (contains < 0)
5064                         return -1;
5065                 if (contains)
5066                         return 0;
5067         }
5068         isl_vec_free(bmap->sample);
5069         bmap->sample = NULL;
5070         bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
5071         if (!bset)
5072                 return -1;
5073         sample = isl_basic_set_sample_vec(bset);
5074         if (!sample)
5075                 return -1;
5076         empty = sample->size == 0;
5077         isl_vec_free(bmap->sample);
5078         bmap->sample = sample;
5079         if (empty)
5080                 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
5081
5082         return empty;
5083 }
5084
5085 int isl_basic_map_fast_is_empty(struct isl_basic_map *bmap)
5086 {
5087         if (!bmap)
5088                 return -1;
5089         return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
5090 }
5091
5092 int isl_basic_set_fast_is_empty(struct isl_basic_set *bset)
5093 {
5094         if (!bset)
5095                 return -1;
5096         return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
5097 }
5098
5099 int isl_basic_set_is_empty(struct isl_basic_set *bset)
5100 {
5101         return isl_basic_map_is_empty((struct isl_basic_map *)bset);
5102 }
5103
5104 struct isl_map *isl_basic_map_union(
5105         struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
5106 {
5107         struct isl_map *map;
5108         if (!bmap1 || !bmap2)
5109                 return NULL;
5110
5111         isl_assert(bmap1->ctx, isl_dim_equal(bmap1->dim, bmap2->dim), goto error);
5112
5113         map = isl_map_alloc_dim(isl_dim_copy(bmap1->dim), 2, 0);
5114         if (!map)
5115                 goto error;
5116         map = isl_map_add_basic_map(map, bmap1);
5117         map = isl_map_add_basic_map(map, bmap2);
5118         return map;
5119 error:
5120         isl_basic_map_free(bmap1);
5121         isl_basic_map_free(bmap2);
5122         return NULL;
5123 }
5124
5125 struct isl_set *isl_basic_set_union(
5126                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
5127 {
5128         return (struct isl_set *)isl_basic_map_union(
5129                                             (struct isl_basic_map *)bset1,
5130                                             (struct isl_basic_map *)bset2);
5131 }
5132
5133 /* Order divs such that any div only depends on previous divs */
5134 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
5135 {
5136         int i;
5137         unsigned off = isl_dim_total(bmap->dim);
5138
5139         for (i = 0; i < bmap->n_div; ++i) {
5140                 int pos;
5141                 if (isl_int_is_zero(bmap->div[i][0]))
5142                         continue;
5143                 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
5144                                                             bmap->n_div-i);
5145                 if (pos == -1)
5146                         continue;
5147                 isl_basic_map_swap_div(bmap, i, i + pos);
5148                 --i;
5149         }
5150         return bmap;
5151 }
5152
5153 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
5154 {
5155         return (struct isl_basic_set *)
5156                 isl_basic_map_order_divs((struct isl_basic_map *)bset);
5157 }
5158
5159 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
5160 {
5161         int i;
5162
5163         if (!map)
5164                 return 0;
5165
5166         for (i = 0; i < map->n; ++i) {
5167                 map->p[i] = isl_basic_map_order_divs(map->p[i]);
5168                 if (!map->p[i])
5169                         goto error;
5170         }
5171
5172         return map;
5173 error:
5174         isl_map_free(map);
5175         return NULL;
5176 }
5177
5178 /* Look for a div in dst that corresponds to the div "div" in src.
5179  * The divs before "div" in src and dst are assumed to be the same.
5180  * 
5181  * Returns -1 if no corresponding div was found and the position
5182  * of the corresponding div in dst otherwise.
5183  */
5184 static int find_div(struct isl_basic_map *dst,
5185                         struct isl_basic_map *src, unsigned div)
5186 {
5187         int i;
5188
5189         unsigned total = isl_dim_total(src->dim);
5190
5191         isl_assert(dst->ctx, div <= dst->n_div, return -1);
5192         for (i = div; i < dst->n_div; ++i)
5193                 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
5194                     isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
5195                                                 dst->n_div - div) == -1)
5196                         return i;
5197         return -1;
5198 }
5199
5200 struct isl_basic_map *isl_basic_map_align_divs(
5201                 struct isl_basic_map *dst, struct isl_basic_map *src)
5202 {
5203         int i;
5204         unsigned total = isl_dim_total(src->dim);
5205
5206         if (!dst || !src)
5207                 goto error;
5208
5209         if (src->n_div == 0)
5210                 return dst;
5211
5212         for (i = 0; i < src->n_div; ++i)
5213                 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
5214
5215         src = isl_basic_map_order_divs(src);
5216         dst = isl_basic_map_cow(dst);
5217         dst = isl_basic_map_extend_dim(dst, isl_dim_copy(dst->dim),
5218                         src->n_div, 0, 2 * src->n_div);
5219         if (!dst)
5220                 return NULL;
5221         for (i = 0; i < src->n_div; ++i) {
5222                 int j = find_div(dst, src, i);
5223                 if (j < 0) {
5224                         j = isl_basic_map_alloc_div(dst);
5225                         if (j < 0)
5226                                 goto error;
5227                         isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
5228                         isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
5229                         if (isl_basic_map_add_div_constraints(dst, j) < 0)
5230                                 goto error;
5231                 }
5232                 if (j != i)
5233                         isl_basic_map_swap_div(dst, i, j);
5234         }
5235         return dst;
5236 error:
5237         isl_basic_map_free(dst);
5238         return NULL;
5239 }
5240
5241 struct isl_basic_set *isl_basic_set_align_divs(
5242                 struct isl_basic_set *dst, struct isl_basic_set *src)
5243 {
5244         return (struct isl_basic_set *)isl_basic_map_align_divs(
5245                 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
5246 }
5247
5248 struct isl_map *isl_map_align_divs(struct isl_map *map)
5249 {
5250         int i;
5251
5252         if (!map)
5253                 return NULL;
5254         if (map->n == 0)
5255                 return map;
5256         map = isl_map_compute_divs(map);
5257         map = isl_map_cow(map);
5258         if (!map)
5259                 return NULL;
5260
5261         for (i = 1; i < map->n; ++i)
5262                 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
5263         for (i = 1; i < map->n; ++i)
5264                 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
5265
5266         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5267         return map;
5268 }
5269
5270 struct isl_set *isl_set_align_divs(struct isl_set *set)
5271 {
5272         return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
5273 }
5274
5275 struct isl_set *isl_set_apply(struct isl_set *set, struct isl_map *map)
5276 {
5277         if (!set || !map)
5278                 goto error;
5279         isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
5280         map = isl_map_intersect_domain(map, set);
5281         set = isl_map_range(map);
5282         return set;
5283 error:
5284         isl_set_free(set);
5285         isl_map_free(map);
5286         return NULL;
5287 }
5288
5289 /* There is no need to cow as removing empty parts doesn't change
5290  * the meaning of the set.
5291  */
5292 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
5293 {
5294         int i;
5295
5296         if (!map)
5297                 return NULL;
5298
5299         for (i = map->n-1; i >= 0; --i) {
5300                 if (!ISL_F_ISSET(map->p[i], ISL_BASIC_MAP_EMPTY))
5301                         continue;
5302                 isl_basic_map_free(map->p[i]);
5303                 if (i != map->n-1) {
5304                         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5305                         map->p[i] = map->p[map->n-1];
5306                 }
5307                 map->n--;
5308         }
5309
5310         return map;
5311 }
5312
5313 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
5314 {
5315         return (struct isl_set *)
5316                 isl_map_remove_empty_parts((struct isl_map *)set);
5317 }
5318
5319 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
5320 {
5321         struct isl_basic_map *bmap;
5322         if (!map || map->n == 0)
5323                 return NULL;
5324         bmap = map->p[map->n-1];
5325         isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
5326         return isl_basic_map_copy(bmap);
5327 }
5328
5329 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
5330 {
5331         return (struct isl_basic_set *)
5332                 isl_map_copy_basic_map((struct isl_map *)set);
5333 }
5334
5335 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
5336                                                 __isl_keep isl_basic_map *bmap)
5337 {
5338         int i;
5339
5340         if (!map || !bmap)
5341                 goto error;
5342         for (i = map->n-1; i >= 0; --i) {
5343                 if (map->p[i] != bmap)
5344                         continue;
5345                 map = isl_map_cow(map);
5346                 if (!map)
5347                         goto error;
5348                 isl_basic_map_free(map->p[i]);
5349                 if (i != map->n-1) {
5350                         ISL_F_CLR(map, ISL_SET_NORMALIZED);
5351                         map->p[i] = map->p[map->n-1];
5352                 }
5353                 map->n--;
5354                 return map;
5355         }
5356         return map;
5357 error:
5358         isl_map_free(map);
5359         return NULL;
5360 }
5361
5362 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
5363                                                 struct isl_basic_set *bset)
5364 {
5365         return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
5366                                                 (struct isl_basic_map *)bset);
5367 }
5368
5369 /* Given two basic sets bset1 and bset2, compute the maximal difference
5370  * between the values of dimension pos in bset1 and those in bset2
5371  * for any common value of the parameters and dimensions preceding pos.
5372  */
5373 static enum isl_lp_result basic_set_maximal_difference_at(
5374         __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
5375         int pos, isl_int *opt)
5376 {
5377         struct isl_dim *dims;
5378         struct isl_basic_map *bmap1 = NULL;
5379         struct isl_basic_map *bmap2 = NULL;
5380         struct isl_ctx *ctx;
5381         struct isl_vec *obj;
5382         unsigned total;
5383         unsigned nparam;
5384         unsigned dim1, dim2;
5385         enum isl_lp_result res;
5386
5387         if (!bset1 || !bset2)
5388                 return isl_lp_error;
5389
5390         nparam = isl_basic_set_n_param(bset1);
5391         dim1 = isl_basic_set_n_dim(bset1);
5392         dim2 = isl_basic_set_n_dim(bset2);
5393         dims = isl_dim_alloc(bset1->ctx, nparam, pos, dim1 - pos);
5394         bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
5395         dims = isl_dim_alloc(bset2->ctx, nparam, pos, dim2 - pos);
5396         bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
5397         if (!bmap1 || !bmap2)
5398                 goto error;
5399         bmap1 = isl_basic_map_cow(bmap1);
5400         bmap1 = isl_basic_map_extend(bmap1, nparam,
5401                         pos, (dim1 - pos) + (dim2 - pos),
5402                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
5403         bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
5404         if (!bmap1)
5405                 goto error;
5406         total = isl_basic_map_total_dim(bmap1);
5407         ctx = bmap1->ctx;
5408         obj = isl_vec_alloc(ctx, 1 + total);
5409         isl_seq_clr(obj->block.data, 1 + total);
5410         isl_int_set_si(obj->block.data[1+nparam+pos], 1);
5411         isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
5412         if (!obj)
5413                 goto error;
5414         res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
5415                                         opt, NULL, NULL);
5416         isl_basic_map_free(bmap1);
5417         isl_vec_free(obj);
5418         return res;
5419 error:
5420         isl_basic_map_free(bmap1);
5421         isl_basic_map_free(bmap2);
5422         return isl_lp_error;
5423 }
5424
5425 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
5426  * for any common value of the parameters and dimensions preceding pos
5427  * in both basic sets, the values of dimension pos in bset1 are
5428  * smaller or larger than those in bset2.
5429  *
5430  * Returns
5431  *       1 if bset1 follows bset2
5432  *      -1 if bset1 precedes bset2
5433  *       0 if bset1 and bset2 are incomparable
5434  *      -2 if some error occurred.
5435  */
5436 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
5437         struct isl_basic_set *bset2, int pos)
5438 {
5439         isl_int opt;
5440         enum isl_lp_result res;
5441         int cmp;
5442
5443         isl_int_init(opt);
5444
5445         res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5446
5447         if (res == isl_lp_empty)
5448                 cmp = 0;
5449         else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5450                   res == isl_lp_unbounded)
5451                 cmp = 1;
5452         else if (res == isl_lp_ok && isl_int_is_neg(opt))
5453                 cmp = -1;
5454         else
5455                 cmp = -2;
5456
5457         isl_int_clear(opt);
5458         return cmp;
5459 }
5460
5461 /* Given two basic sets bset1 and bset2, check whether
5462  * for any common value of the parameters and dimensions preceding pos
5463  * there is a value of dimension pos in bset1 that is larger
5464  * than a value of the same dimension in bset2.
5465  *
5466  * Return
5467  *       1 if there exists such a pair
5468  *       0 if there is no such pair, but there is a pair of equal values
5469  *      -1 otherwise
5470  *      -2 if some error occurred.
5471  */
5472 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
5473         __isl_keep isl_basic_set *bset2, int pos)
5474 {
5475         isl_int opt;
5476         enum isl_lp_result res;
5477         int cmp;
5478
5479         isl_int_init(opt);
5480
5481         res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
5482
5483         if (res == isl_lp_empty)
5484                 cmp = -1;
5485         else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
5486                   res == isl_lp_unbounded)
5487                 cmp = 1;
5488         else if (res == isl_lp_ok && isl_int_is_neg(opt))
5489                 cmp = -1;
5490         else if (res == isl_lp_ok)
5491                 cmp = 0;
5492         else
5493                 cmp = -2;
5494
5495         isl_int_clear(opt);
5496         return cmp;
5497 }
5498
5499 /* Given two sets set1 and set2, check whether
5500  * for any common value of the parameters and dimensions preceding pos
5501  * there is a value of dimension pos in set1 that is larger
5502  * than a value of the same dimension in set2.
5503  *
5504  * Return
5505  *       1 if there exists such a pair
5506  *       0 if there is no such pair, but there is a pair of equal values
5507  *      -1 otherwise
5508  *      -2 if some error occurred.
5509  */
5510 int isl_set_follows_at(__isl_keep isl_set *set1,
5511         __isl_keep isl_set *set2, int pos)
5512 {
5513         int i, j;
5514         int follows = -1;
5515
5516         if (!set1 || !set2)
5517                 return -2;
5518
5519         for (i = 0; i < set1->n; ++i)
5520                 for (j = 0; j < set2->n; ++j) {
5521                         int f;
5522                         f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
5523                         if (f == 1 || f == -2)
5524                                 return f;
5525                         if (f > follows)
5526                                 follows = f;
5527                 }
5528
5529         return follows;
5530 }
5531
5532 static int isl_basic_map_fast_has_fixed_var(struct isl_basic_map *bmap,
5533         unsigned pos, isl_int *val)
5534 {
5535         int i;
5536         int d;
5537         unsigned total;
5538
5539         if (!bmap)
5540                 return -1;
5541         total = isl_basic_map_total_dim(bmap);
5542         for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
5543                 for (; d+1 > pos; --d)
5544                         if (!isl_int_is_zero(bmap->eq[i][1+d]))
5545                                 break;
5546                 if (d != pos)
5547                         continue;
5548                 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
5549                         return 0;
5550                 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
5551                         return 0;
5552                 if (!isl_int_is_one(bmap->eq[i][1+d]))
5553                         return 0;
5554                 if (val)
5555                         isl_int_neg(*val, bmap->eq[i][0]);
5556                 return 1;
5557         }
5558         return 0;
5559 }
5560
5561 static int isl_map_fast_has_fixed_var(struct isl_map *map,
5562         unsigned pos, isl_int *val)
5563 {
5564         int i;
5565         isl_int v;
5566         isl_int tmp;
5567         int fixed;
5568
5569         if (!map)
5570                 return -1;
5571         if (map->n == 0)
5572                 return 0;
5573         if (map->n == 1)
5574                 return isl_basic_map_fast_has_fixed_var(map->p[0], pos, val); 
5575         isl_int_init(v);
5576         isl_int_init(tmp);
5577         fixed = isl_basic_map_fast_has_fixed_var(map->p[0], pos, &v); 
5578         for (i = 1; fixed == 1 && i < map->n; ++i) {
5579                 fixed = isl_basic_map_fast_has_fixed_var(map->p[i], pos, &tmp); 
5580                 if (fixed == 1 && isl_int_ne(tmp, v))
5581                         fixed = 0;
5582         }
5583         if (val)
5584                 isl_int_set(*val, v);
5585         isl_int_clear(tmp);
5586         isl_int_clear(v);
5587         return fixed;
5588 }
5589
5590 static int isl_basic_set_fast_has_fixed_var(struct isl_basic_set *bset,
5591         unsigned pos, isl_int *val)
5592 {
5593         return isl_basic_map_fast_has_fixed_var((struct isl_basic_map *)bset,
5594                                                 pos, val);
5595 }
5596
5597 static int isl_set_fast_has_fixed_var(struct isl_set *set, unsigned pos,
5598         isl_int *val)
5599 {
5600         return isl_map_fast_has_fixed_var((struct isl_map *)set, pos, val);
5601 }
5602
5603 int isl_basic_map_fast_is_fixed(struct isl_basic_map *bmap,
5604         enum isl_dim_type type, unsigned pos, isl_int *val)
5605 {
5606         if (pos >= isl_basic_map_dim(bmap, type))
5607                 return -1;
5608         return isl_basic_map_fast_has_fixed_var(bmap,
5609                 isl_basic_map_offset(bmap, type) - 1 + pos, val);
5610 }
5611
5612 int isl_map_fast_is_fixed(struct isl_map *map,
5613         enum isl_dim_type type, unsigned pos, isl_int *val)
5614 {
5615         if (pos >= isl_map_dim(map, type))
5616                 return -1;
5617         return isl_map_fast_has_fixed_var(map,
5618                 map_offset(map, type) - 1 + pos, val);
5619 }
5620
5621 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5622  * then return this fixed value in *val.
5623  */
5624 int isl_basic_set_fast_dim_is_fixed(struct isl_basic_set *bset, unsigned dim,
5625         isl_int *val)
5626 {
5627         return isl_basic_set_fast_has_fixed_var(bset,
5628                                         isl_basic_set_n_param(bset) + dim, val);
5629 }
5630
5631 /* Check if dimension dim has fixed value and if so and if val is not NULL,
5632  * then return this fixed value in *val.
5633  */
5634 int isl_set_fast_dim_is_fixed(struct isl_set *set, unsigned dim, isl_int *val)
5635 {
5636         return isl_set_fast_has_fixed_var(set, isl_set_n_param(set) + dim, val);
5637 }
5638
5639 /* Check if input variable in has fixed value and if so and if val is not NULL,
5640  * then return this fixed value in *val.
5641  */
5642 int isl_map_fast_input_is_fixed(struct isl_map *map, unsigned in, isl_int *val)
5643 {
5644         return isl_map_fast_has_fixed_var(map, isl_map_n_param(map) + in, val);
5645 }
5646
5647 /* Check if dimension dim has an (obvious) fixed lower bound and if so
5648  * and if val is not NULL, then return this lower bound in *val.
5649  */
5650 int isl_basic_set_fast_dim_has_fixed_lower_bound(struct isl_basic_set *bset,
5651         unsigned dim, isl_int *val)
5652 {
5653         int i, i_eq = -1, i_ineq = -1;
5654         isl_int *c;
5655         unsigned total;
5656         unsigned nparam;
5657
5658         if (!bset)
5659                 return -1;
5660         total = isl_basic_set_total_dim(bset);
5661         nparam = isl_basic_set_n_param(bset);
5662         for (i = 0; i < bset->n_eq; ++i) {
5663                 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
5664                         continue;
5665                 if (i_eq != -1)
5666                         return 0;
5667                 i_eq = i;
5668         }
5669         for (i = 0; i < bset->n_ineq; ++i) {
5670                 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
5671                         continue;
5672                 if (i_eq != -1 || i_ineq != -1)
5673                         return 0;
5674                 i_ineq = i;
5675         }
5676         if (i_eq == -1 && i_ineq == -1)
5677                 return 0;
5678         c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
5679         /* The coefficient should always be one due to normalization. */
5680         if (!isl_int_is_one(c[1+nparam+dim]))
5681                 return 0;
5682         if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
5683                 return 0;
5684         if (isl_seq_first_non_zero(c+1+nparam+dim+1,
5685                                         total - nparam - dim - 1) != -1)
5686                 return 0;
5687         if (val)
5688                 isl_int_neg(*val, c[0]);
5689         return 1;
5690 }
5691
5692 int isl_set_fast_dim_has_fixed_lower_bound(struct isl_set *set,
5693         unsigned dim, isl_int *val)
5694 {
5695         int i;
5696         isl_int v;
5697         isl_int tmp;
5698         int fixed;
5699
5700         if (!set)
5701                 return -1;
5702         if (set->n == 0)
5703                 return 0;
5704         if (set->n == 1)
5705                 return isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5706                                                                 dim, val);
5707         isl_int_init(v);
5708         isl_int_init(tmp);
5709         fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[0],
5710                                                                 dim, &v);
5711         for (i = 1; fixed == 1 && i < set->n; ++i) {
5712                 fixed = isl_basic_set_fast_dim_has_fixed_lower_bound(set->p[i],
5713                                                                 dim, &tmp);
5714                 if (fixed == 1 && isl_int_ne(tmp, v))
5715                         fixed = 0;
5716         }
5717         if (val)
5718                 isl_int_set(*val, v);
5719         isl_int_clear(tmp);
5720         isl_int_clear(v);
5721         return fixed;
5722 }
5723
5724 struct constraint {
5725         unsigned        size;
5726         isl_int         *c;
5727 };
5728
5729 static int qsort_constraint_cmp(const void *p1, const void *p2)
5730 {
5731         const struct constraint *c1 = (const struct constraint *)p1;
5732         const struct constraint *c2 = (const struct constraint *)p2;
5733         unsigned size = isl_min(c1->size, c2->size);
5734         return isl_seq_cmp(c1->c, c2->c, size);
5735 }
5736
5737 static struct isl_basic_map *isl_basic_map_sort_constraints(
5738         struct isl_basic_map *bmap)
5739 {
5740         int i;
5741         struct constraint *c;
5742         unsigned total;
5743
5744         if (!bmap)
5745                 return NULL;
5746         total = isl_basic_map_total_dim(bmap);
5747         c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
5748         if (!c)
5749                 goto error;
5750         for (i = 0; i < bmap->n_ineq; ++i) {
5751                 c[i].size = total;
5752                 c[i].c = bmap->ineq[i];
5753         }
5754         qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
5755         for (i = 0; i < bmap->n_ineq; ++i)
5756                 bmap->ineq[i] = c[i].c;
5757         free(c);
5758         return bmap;
5759 error:
5760         isl_basic_map_free(bmap);
5761         return NULL;
5762 }
5763
5764 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
5765 {
5766         if (!bmap)
5767                 return NULL;
5768         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
5769                 return bmap;
5770         bmap = isl_basic_map_convex_hull(bmap);
5771         bmap = isl_basic_map_sort_constraints(bmap);
5772         ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
5773         return bmap;
5774 }
5775
5776 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
5777 {
5778         return (struct isl_basic_set *)isl_basic_map_normalize(
5779                                                 (struct isl_basic_map *)bset);
5780 }
5781
5782 static int isl_basic_map_fast_cmp(const struct isl_basic_map *bmap1,
5783         const struct isl_basic_map *bmap2)
5784 {
5785         int i, cmp;
5786         unsigned total;
5787
5788         if (bmap1 == bmap2)
5789                 return 0;
5790         if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
5791                 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
5792         if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
5793                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5794         if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
5795                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
5796         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
5797             ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5798                 return 0;
5799         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
5800                 return 1;
5801         if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
5802                 return -1;
5803         if (bmap1->n_eq != bmap2->n_eq)
5804                 return bmap1->n_eq - bmap2->n_eq;
5805         if (bmap1->n_ineq != bmap2->n_ineq)
5806                 return bmap1->n_ineq - bmap2->n_ineq;
5807         if (bmap1->n_div != bmap2->n_div)
5808                 return bmap1->n_div - bmap2->n_div;
5809         total = isl_basic_map_total_dim(bmap1);
5810         for (i = 0; i < bmap1->n_eq; ++i) {
5811                 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
5812                 if (cmp)
5813                         return cmp;
5814         }
5815         for (i = 0; i < bmap1->n_ineq; ++i) {
5816                 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
5817                 if (cmp)
5818                         return cmp;
5819         }
5820         for (i = 0; i < bmap1->n_div; ++i) {
5821                 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
5822                 if (cmp)
5823                         return cmp;
5824         }
5825         return 0;
5826 }
5827
5828 static int isl_basic_map_fast_is_equal(struct isl_basic_map *bmap1,
5829         struct isl_basic_map *bmap2)
5830 {
5831         return isl_basic_map_fast_cmp(bmap1, bmap2) == 0;
5832 }
5833
5834 static int qsort_bmap_cmp(const void *p1, const void *p2)
5835 {
5836         const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
5837         const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
5838
5839         return isl_basic_map_fast_cmp(bmap1, bmap2);
5840 }
5841
5842 /* We normalize in place, but if anything goes wrong we need
5843  * to return NULL, so we need to make sure we don't change the
5844  * meaning of any possible other copies of map.
5845  */
5846 struct isl_map *isl_map_normalize(struct isl_map *map)
5847 {
5848         int i, j;
5849         struct isl_basic_map *bmap;
5850
5851         if (!map)
5852                 return NULL;
5853         if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
5854                 return map;
5855         for (i = 0; i < map->n; ++i) {
5856                 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
5857                 if (!bmap)
5858                         goto error;
5859                 isl_basic_map_free(map->p[i]);
5860                 map->p[i] = bmap;
5861         }
5862         qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
5863         ISL_F_SET(map, ISL_MAP_NORMALIZED);
5864         map = isl_map_remove_empty_parts(map);
5865         if (!map)
5866                 return NULL;
5867         for (i = map->n - 1; i >= 1; --i) {
5868                 if (!isl_basic_map_fast_is_equal(map->p[i-1], map->p[i]))
5869                         continue;
5870                 isl_basic_map_free(map->p[i-1]);
5871                 for (j = i; j < map->n; ++j)
5872                         map->p[j-1] = map->p[j];
5873                 map->n--;
5874         }
5875         return map;
5876 error:
5877         isl_map_free(map);
5878         return NULL;
5879
5880 }
5881
5882 struct isl_set *isl_set_normalize(struct isl_set *set)
5883 {
5884         return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
5885 }
5886
5887 int isl_map_fast_is_equal(struct isl_map *map1, struct isl_map *map2)
5888 {
5889         int i;
5890         int equal;
5891
5892         if (!map1 || !map2)
5893                 return -1;
5894
5895         if (map1 == map2)
5896                 return 1;
5897         if (!isl_dim_equal(map1->dim, map2->dim))
5898                 return 0;
5899
5900         map1 = isl_map_copy(map1);
5901         map2 = isl_map_copy(map2);
5902         map1 = isl_map_normalize(map1);
5903         map2 = isl_map_normalize(map2);
5904         if (!map1 || !map2)
5905                 goto error;
5906         equal = map1->n == map2->n;
5907         for (i = 0; equal && i < map1->n; ++i) {
5908                 equal = isl_basic_map_fast_is_equal(map1->p[i], map2->p[i]);
5909                 if (equal < 0)
5910                         goto error;
5911         }
5912         isl_map_free(map1);
5913         isl_map_free(map2);
5914         return equal;
5915 error:
5916         isl_map_free(map1);
5917         isl_map_free(map2);
5918         return -1;
5919 }
5920
5921 int isl_set_fast_is_equal(struct isl_set *set1, struct isl_set *set2)
5922 {
5923         return isl_map_fast_is_equal((struct isl_map *)set1,
5924                                                 (struct isl_map *)set2);
5925 }
5926
5927 /* Return an interval that ranges from min to max (inclusive)
5928  */
5929 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
5930         isl_int min, isl_int max)
5931 {
5932         int k;
5933         struct isl_basic_set *bset = NULL;
5934
5935         bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
5936         if (!bset)
5937                 goto error;
5938
5939         k = isl_basic_set_alloc_inequality(bset);
5940         if (k < 0)
5941                 goto error;
5942         isl_int_set_si(bset->ineq[k][1], 1);
5943         isl_int_neg(bset->ineq[k][0], min);
5944
5945         k = isl_basic_set_alloc_inequality(bset);
5946         if (k < 0)
5947                 goto error;
5948         isl_int_set_si(bset->ineq[k][1], -1);
5949         isl_int_set(bset->ineq[k][0], max);
5950
5951         return bset;
5952 error:
5953         isl_basic_set_free(bset);
5954         return NULL;
5955 }
5956
5957 /* Return the Cartesian product of the basic sets in list (in the given order).
5958  */
5959 struct isl_basic_set *isl_basic_set_product(struct isl_basic_set_list *list)
5960 {
5961         int i;
5962         unsigned dim;
5963         unsigned nparam;
5964         unsigned extra;
5965         unsigned n_eq;
5966         unsigned n_ineq;
5967         struct isl_basic_set *product = NULL;
5968
5969         if (!list)
5970                 goto error;
5971         isl_assert(list->ctx, list->n > 0, goto error);
5972         isl_assert(list->ctx, list->p[0], goto error);
5973         nparam = isl_basic_set_n_param(list->p[0]);
5974         dim = isl_basic_set_n_dim(list->p[0]);
5975         extra = list->p[0]->n_div;
5976         n_eq = list->p[0]->n_eq;
5977         n_ineq = list->p[0]->n_ineq;
5978         for (i = 1; i < list->n; ++i) {
5979                 isl_assert(list->ctx, list->p[i], goto error);
5980                 isl_assert(list->ctx,
5981                     nparam == isl_basic_set_n_param(list->p[i]), goto error);
5982                 dim += isl_basic_set_n_dim(list->p[i]);
5983                 extra += list->p[i]->n_div;
5984                 n_eq += list->p[i]->n_eq;
5985                 n_ineq += list->p[i]->n_ineq;
5986         }
5987         product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
5988                                         n_eq, n_ineq);
5989         if (!product)
5990                 goto error;
5991         dim = 0;
5992         for (i = 0; i < list->n; ++i) {
5993                 isl_basic_set_add_constraints(product,
5994                                         isl_basic_set_copy(list->p[i]), dim);
5995                 dim += isl_basic_set_n_dim(list->p[i]);
5996         }
5997         isl_basic_set_list_free(list);
5998         return product;
5999 error:
6000         isl_basic_set_free(product);
6001         isl_basic_set_list_free(list);
6002         return NULL;
6003 }
6004
6005 struct isl_basic_map *isl_basic_map_product(
6006                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
6007 {
6008         struct isl_dim *dim_result = NULL;
6009         struct isl_basic_map *bmap;
6010         unsigned in1, in2, out1, out2, nparam, total, pos;
6011         struct isl_dim_map *dim_map1, *dim_map2;
6012
6013         if (!bmap1 || !bmap2)
6014                 goto error;
6015
6016         isl_assert(bmap1->ctx, isl_dim_match(bmap1->dim, isl_dim_param,
6017                                      bmap2->dim, isl_dim_param), goto error);
6018         dim_result = isl_dim_product(isl_dim_copy(bmap1->dim),
6019                                                    isl_dim_copy(bmap2->dim));
6020
6021         in1 = isl_basic_map_n_in(bmap1);
6022         in2 = isl_basic_map_n_in(bmap2);
6023         out1 = isl_basic_map_n_out(bmap1);
6024         out2 = isl_basic_map_n_out(bmap2);
6025         nparam = isl_basic_map_n_param(bmap1);
6026
6027         total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
6028         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
6029         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
6030         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
6031         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
6032         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
6033         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
6034         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
6035         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
6036         isl_dim_map_div(dim_map1, bmap1, pos += out2);
6037         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
6038
6039         bmap = isl_basic_map_alloc_dim(dim_result,
6040                         bmap1->n_div + bmap2->n_div,
6041                         bmap1->n_eq + bmap2->n_eq,
6042                         bmap1->n_ineq + bmap2->n_ineq);
6043         bmap = add_constraints_dim_map(bmap, bmap1, dim_map1);
6044         bmap = add_constraints_dim_map(bmap, bmap2, dim_map2);
6045         bmap = isl_basic_map_simplify(bmap);
6046         return isl_basic_map_finalize(bmap);
6047 error:
6048         isl_basic_map_free(bmap1);
6049         isl_basic_map_free(bmap2);
6050         return NULL;
6051 }
6052
6053 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
6054  */
6055 struct isl_map *isl_map_product(struct isl_map *map1, struct isl_map *map2)
6056 {
6057         unsigned flags = 0;
6058         struct isl_map *result;
6059         int i, j;
6060
6061         if (!map1 || !map2)
6062                 goto error;
6063
6064         isl_assert(map1->ctx, isl_dim_match(map1->dim, isl_dim_param,
6065                                          map2->dim, isl_dim_param), goto error);
6066
6067         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6068             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6069                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6070
6071         result = isl_map_alloc_dim(isl_dim_product(isl_dim_copy(map1->dim),
6072                                                    isl_dim_copy(map2->dim)),
6073                                 map1->n * map2->n, flags);
6074         if (!result)
6075                 goto error;
6076         for (i = 0; i < map1->n; ++i)
6077                 for (j = 0; j < map2->n; ++j) {
6078                         struct isl_basic_map *part;
6079                         part = isl_basic_map_product(
6080                                     isl_basic_map_copy(map1->p[i]),
6081                                     isl_basic_map_copy(map2->p[j]));
6082                         if (isl_basic_map_is_empty(part))
6083                                 isl_basic_map_free(part);
6084                         else
6085                                 result = isl_map_add_basic_map(result, part);
6086                         if (!result)
6087                                 goto error;
6088                 }
6089         isl_map_free(map1);
6090         isl_map_free(map2);
6091         return result;
6092 error:
6093         isl_map_free(map1);
6094         isl_map_free(map2);
6095         return NULL;
6096 }
6097
6098 /* Given two set A and B, construct its Cartesian product A x B.
6099  */
6100 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
6101 {
6102         return (struct isl_set *)isl_map_product((struct isl_map *)set1,
6103                                                  (struct isl_map *)set2);
6104 }
6105
6106 uint32_t isl_basic_set_get_hash(struct isl_basic_set *bset)
6107 {
6108         int i;
6109         uint32_t hash = isl_hash_init();
6110         unsigned total;
6111
6112         if (!bset)
6113                 return 0;
6114         bset = isl_basic_set_copy(bset);
6115         bset = isl_basic_set_normalize(bset);
6116         if (!bset)
6117                 return 0;
6118         total = isl_basic_set_total_dim(bset);
6119         isl_hash_byte(hash, bset->n_eq & 0xFF);
6120         for (i = 0; i < bset->n_eq; ++i) {
6121                 uint32_t c_hash;
6122                 c_hash = isl_seq_get_hash(bset->eq[i], 1 + total);
6123                 isl_hash_hash(hash, c_hash);
6124         }
6125         isl_hash_byte(hash, bset->n_ineq & 0xFF);
6126         for (i = 0; i < bset->n_ineq; ++i) {
6127                 uint32_t c_hash;
6128                 c_hash = isl_seq_get_hash(bset->ineq[i], 1 + total);
6129                 isl_hash_hash(hash, c_hash);
6130         }
6131         isl_hash_byte(hash, bset->n_div & 0xFF);
6132         for (i = 0; i < bset->n_div; ++i) {
6133                 uint32_t c_hash;
6134                 if (isl_int_is_zero(bset->div[i][0]))
6135                         continue;
6136                 isl_hash_byte(hash, i & 0xFF);
6137                 c_hash = isl_seq_get_hash(bset->div[i], 1 + 1 + total);
6138                 isl_hash_hash(hash, c_hash);
6139         }
6140         isl_basic_set_free(bset);
6141         return hash;
6142 }
6143
6144 uint32_t isl_set_get_hash(struct isl_set *set)
6145 {
6146         int i;
6147         uint32_t hash;
6148
6149         if (!set)
6150                 return 0;
6151         set = isl_set_copy(set);
6152         set = isl_set_normalize(set);
6153         if (!set)
6154                 return 0;
6155
6156         hash = isl_hash_init();
6157         for (i = 0; i < set->n; ++i) {
6158                 uint32_t bset_hash;
6159                 bset_hash = isl_basic_set_get_hash(set->p[i]);
6160                 isl_hash_hash(hash, bset_hash);
6161         }
6162                 
6163         isl_set_free(set);
6164
6165         return hash;
6166 }
6167
6168 /* Check if the value for dimension dim is completely determined
6169  * by the values of the other parameters and variables.
6170  * That is, check if dimension dim is involved in an equality.
6171  */
6172 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
6173 {
6174         int i;
6175         unsigned nparam;
6176
6177         if (!bset)
6178                 return -1;
6179         nparam = isl_basic_set_n_param(bset);
6180         for (i = 0; i < bset->n_eq; ++i)
6181                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
6182                         return 1;
6183         return 0;
6184 }
6185
6186 /* Check if the value for dimension dim is completely determined
6187  * by the values of the other parameters and variables.
6188  * That is, check if dimension dim is involved in an equality
6189  * for each of the subsets.
6190  */
6191 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
6192 {
6193         int i;
6194
6195         if (!set)
6196                 return -1;
6197         for (i = 0; i < set->n; ++i) {
6198                 int unique;
6199                 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
6200                 if (unique != 1)
6201                         return unique;
6202         }
6203         return 1;
6204 }
6205
6206 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
6207         int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
6208 {
6209         int i;
6210
6211         if (!map)
6212                 return -1;
6213
6214         for (i = 0; i < map->n; ++i)
6215                 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
6216                         return -1;
6217
6218         return 0;
6219 }
6220
6221 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
6222         int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
6223 {
6224         int i;
6225
6226         if (!set)
6227                 return -1;
6228
6229         for (i = 0; i < set->n; ++i)
6230                 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
6231                         return -1;
6232
6233         return 0;
6234 }
6235
6236 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
6237 {
6238         struct isl_dim *dim;
6239
6240         if (!bset)
6241                 return NULL;
6242
6243         if (bset->n_div == 0)
6244                 return bset;
6245
6246         bset = isl_basic_set_cow(bset);
6247         if (!bset)
6248                 return NULL;
6249
6250         dim = isl_basic_set_get_dim(bset);
6251         dim = isl_dim_add(dim, isl_dim_set, bset->n_div);
6252         if (!dim)
6253                 goto error;
6254         isl_dim_free(bset->dim);
6255         bset->dim = dim;
6256         bset->n_div = 0;
6257
6258         return bset;
6259 error:
6260         isl_basic_set_free(bset);
6261         return NULL;
6262 }
6263
6264 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
6265 {
6266         int i;
6267         struct isl_dim *dim;
6268         unsigned n_div;
6269
6270         set = isl_set_align_divs(set);
6271
6272         if (!set)
6273                 return NULL;
6274         if (set->n == 0 || set->p[0]->n_div == 0)
6275                 return set;
6276
6277         set = isl_set_cow(set);
6278         if (!set)
6279                 return NULL;
6280
6281         n_div = set->p[0]->n_div;
6282         dim = isl_set_get_dim(set);
6283         dim = isl_dim_add(dim, isl_dim_set, n_div);
6284         if (!dim)
6285                 goto error;
6286         isl_dim_free(set->dim);
6287         set->dim = dim;
6288
6289         for (i = 0; i < set->n; ++i) {
6290                 set->p[i] = isl_basic_set_lift(set->p[i]);
6291                 if (!set->p[i])
6292                         goto error;
6293         }
6294
6295         return set;
6296 error:
6297         isl_set_free(set);
6298         return NULL;
6299 }
6300
6301 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
6302 {
6303         struct isl_dim *dim;
6304         struct isl_basic_map *bmap;
6305         unsigned n_set;
6306         unsigned n_div;
6307         unsigned n_param;
6308         unsigned total;
6309         int i, k, l;
6310
6311         set = isl_set_align_divs(set);
6312
6313         if (!set)
6314                 return NULL;
6315
6316         dim = isl_set_get_dim(set);
6317         if (set->n == 0 || set->p[0]->n_div == 0) {
6318                 isl_set_free(set);
6319                 return isl_map_identity(dim);
6320         }
6321
6322         n_div = set->p[0]->n_div;
6323         dim = isl_dim_map(dim);
6324         n_param = isl_dim_size(dim, isl_dim_param);
6325         n_set = isl_dim_size(dim, isl_dim_in);
6326         dim = isl_dim_extend(dim, n_param, n_set, n_set + n_div);
6327         bmap = isl_basic_map_alloc_dim(dim, 0, n_set, 2 * n_div);
6328         for (i = 0; i < n_set; ++i)
6329                 bmap = var_equal(bmap, i);
6330
6331         total = n_param + n_set + n_set + n_div;
6332         for (i = 0; i < n_div; ++i) {
6333                 k = isl_basic_map_alloc_inequality(bmap);
6334                 if (k < 0)
6335                         goto error;
6336                 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
6337                 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
6338                 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
6339                             set->p[0]->div[i]+1+1+n_param, n_set + n_div);
6340                 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
6341                             set->p[0]->div[i][0]);
6342
6343                 l = isl_basic_map_alloc_inequality(bmap);
6344                 if (l < 0)
6345                         goto error;
6346                 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
6347                 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
6348                             set->p[0]->div[i][0]);
6349                 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
6350         }
6351
6352         isl_set_free(set);
6353         return isl_map_from_basic_map(bmap);
6354 error:
6355         isl_set_free(set);
6356         isl_basic_map_free(bmap);
6357         return NULL;
6358 }
6359
6360 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
6361 {
6362         unsigned dim;
6363         int size = 0;
6364
6365         if (!bset)
6366                 return -1;
6367
6368         dim = isl_basic_set_total_dim(bset);
6369         size += bset->n_eq * (1 + dim);
6370         size += bset->n_ineq * (1 + dim);
6371         size += bset->n_div * (2 + dim);
6372
6373         return size;
6374 }
6375
6376 int isl_set_size(__isl_keep isl_set *set)
6377 {
6378         int i;
6379         int size = 0;
6380
6381         if (!set)
6382                 return -1;
6383
6384         for (i = 0; i < set->n; ++i)
6385                 size += isl_basic_set_size(set->p[i]);
6386
6387         return size;
6388 }