b383cfeb0771cc709d51430a847e46f92776ffe6
[platform/upstream/isl.git] / isl_map.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  * Copyright 2012      Ecole Normale Superieure
5  *
6  * Use of this software is governed by the MIT license
7  *
8  * Written by Sven Verdoolaege, K.U.Leuven, Departement
9  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
12  * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
13  */
14
15 #include <string.h>
16 #include <isl_ctx_private.h>
17 #include <isl_map_private.h>
18 #include <isl/blk.h>
19 #include "isl_space_private.h"
20 #include "isl_equalities.h"
21 #include <isl_list_private.h>
22 #include <isl/lp.h>
23 #include <isl/seq.h>
24 #include <isl/set.h>
25 #include <isl/map.h>
26 #include "isl_map_piplib.h"
27 #include <isl_reordering.h>
28 #include "isl_sample.h"
29 #include "isl_tab.h"
30 #include <isl/vec.h>
31 #include <isl_mat_private.h>
32 #include <isl_dim_map.h>
33 #include <isl_local_space_private.h>
34 #include <isl_aff_private.h>
35 #include <isl_options_private.h>
36
37 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
38 {
39         switch (type) {
40         case isl_dim_param:     return dim->nparam;
41         case isl_dim_in:        return dim->n_in;
42         case isl_dim_out:       return dim->n_out;
43         case isl_dim_all:       return dim->nparam + dim->n_in + dim->n_out;
44         default:                return 0;
45         }
46 }
47
48 static unsigned pos(__isl_keep isl_space *dim, enum isl_dim_type type)
49 {
50         switch (type) {
51         case isl_dim_param:     return 1;
52         case isl_dim_in:        return 1 + dim->nparam;
53         case isl_dim_out:       return 1 + dim->nparam + dim->n_in;
54         default:                return 0;
55         }
56 }
57
58 unsigned isl_basic_map_dim(__isl_keep isl_basic_map *bmap,
59                                 enum isl_dim_type type)
60 {
61         if (!bmap)
62                 return 0;
63         switch (type) {
64         case isl_dim_cst:       return 1;
65         case isl_dim_param:
66         case isl_dim_in:
67         case isl_dim_out:       return isl_space_dim(bmap->dim, type);
68         case isl_dim_div:       return bmap->n_div;
69         case isl_dim_all:       return isl_basic_map_total_dim(bmap);
70         default:                return 0;
71         }
72 }
73
74 unsigned isl_map_dim(__isl_keep isl_map *map, enum isl_dim_type type)
75 {
76         return map ? n(map->dim, type) : 0;
77 }
78
79 unsigned isl_set_dim(__isl_keep isl_set *set, enum isl_dim_type type)
80 {
81         return set ? n(set->dim, type) : 0;
82 }
83
84 unsigned isl_basic_map_offset(struct isl_basic_map *bmap,
85                                         enum isl_dim_type type)
86 {
87         isl_space *dim = bmap->dim;
88         switch (type) {
89         case isl_dim_cst:       return 0;
90         case isl_dim_param:     return 1;
91         case isl_dim_in:        return 1 + dim->nparam;
92         case isl_dim_out:       return 1 + dim->nparam + dim->n_in;
93         case isl_dim_div:       return 1 + dim->nparam + dim->n_in + dim->n_out;
94         default:                return 0;
95         }
96 }
97
98 unsigned isl_basic_set_offset(struct isl_basic_set *bset,
99                                         enum isl_dim_type type)
100 {
101         return isl_basic_map_offset(bset, type);
102 }
103
104 static unsigned map_offset(struct isl_map *map, enum isl_dim_type type)
105 {
106         return pos(map->dim, type);
107 }
108
109 unsigned isl_basic_set_dim(__isl_keep isl_basic_set *bset,
110                                 enum isl_dim_type type)
111 {
112         return isl_basic_map_dim(bset, type);
113 }
114
115 unsigned isl_basic_set_n_dim(__isl_keep isl_basic_set *bset)
116 {
117         return isl_basic_set_dim(bset, isl_dim_set);
118 }
119
120 unsigned isl_basic_set_n_param(__isl_keep isl_basic_set *bset)
121 {
122         return isl_basic_set_dim(bset, isl_dim_param);
123 }
124
125 unsigned isl_basic_set_total_dim(const struct isl_basic_set *bset)
126 {
127         if (!bset)
128                 return 0;
129         return isl_space_dim(bset->dim, isl_dim_all) + bset->n_div;
130 }
131
132 unsigned isl_set_n_dim(__isl_keep isl_set *set)
133 {
134         return isl_set_dim(set, isl_dim_set);
135 }
136
137 unsigned isl_set_n_param(__isl_keep isl_set *set)
138 {
139         return isl_set_dim(set, isl_dim_param);
140 }
141
142 unsigned isl_basic_map_n_in(const struct isl_basic_map *bmap)
143 {
144         return bmap ? bmap->dim->n_in : 0;
145 }
146
147 unsigned isl_basic_map_n_out(const struct isl_basic_map *bmap)
148 {
149         return bmap ? bmap->dim->n_out : 0;
150 }
151
152 unsigned isl_basic_map_n_param(const struct isl_basic_map *bmap)
153 {
154         return bmap ? bmap->dim->nparam : 0;
155 }
156
157 unsigned isl_basic_map_n_div(const struct isl_basic_map *bmap)
158 {
159         return bmap ? bmap->n_div : 0;
160 }
161
162 unsigned isl_basic_map_total_dim(const struct isl_basic_map *bmap)
163 {
164         return bmap ? isl_space_dim(bmap->dim, isl_dim_all) + bmap->n_div : 0;
165 }
166
167 unsigned isl_map_n_in(const struct isl_map *map)
168 {
169         return map ? map->dim->n_in : 0;
170 }
171
172 unsigned isl_map_n_out(const struct isl_map *map)
173 {
174         return map ? map->dim->n_out : 0;
175 }
176
177 unsigned isl_map_n_param(const struct isl_map *map)
178 {
179         return map ? map->dim->nparam : 0;
180 }
181
182 int isl_map_compatible_domain(struct isl_map *map, struct isl_set *set)
183 {
184         int m;
185         if (!map || !set)
186                 return -1;
187         m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
188         if (m < 0 || !m)
189                 return m;
190         return isl_space_tuple_match(map->dim, isl_dim_in, set->dim, isl_dim_set);
191 }
192
193 int isl_basic_map_compatible_domain(struct isl_basic_map *bmap,
194                 struct isl_basic_set *bset)
195 {
196         int m;
197         if (!bmap || !bset)
198                 return -1;
199         m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
200         if (m < 0 || !m)
201                 return m;
202         return isl_space_tuple_match(bmap->dim, isl_dim_in, bset->dim, isl_dim_set);
203 }
204
205 int isl_map_compatible_range(__isl_keep isl_map *map, __isl_keep isl_set *set)
206 {
207         int m;
208         if (!map || !set)
209                 return -1;
210         m = isl_space_match(map->dim, isl_dim_param, set->dim, isl_dim_param);
211         if (m < 0 || !m)
212                 return m;
213         return isl_space_tuple_match(map->dim, isl_dim_out, set->dim, isl_dim_set);
214 }
215
216 int isl_basic_map_compatible_range(struct isl_basic_map *bmap,
217                 struct isl_basic_set *bset)
218 {
219         int m;
220         if (!bmap || !bset)
221                 return -1;
222         m = isl_space_match(bmap->dim, isl_dim_param, bset->dim, isl_dim_param);
223         if (m < 0 || !m)
224                 return m;
225         return isl_space_tuple_match(bmap->dim, isl_dim_out, bset->dim, isl_dim_set);
226 }
227
228 isl_ctx *isl_basic_map_get_ctx(__isl_keep isl_basic_map *bmap)
229 {
230         return bmap ? bmap->ctx : NULL;
231 }
232
233 isl_ctx *isl_basic_set_get_ctx(__isl_keep isl_basic_set *bset)
234 {
235         return bset ? bset->ctx : NULL;
236 }
237
238 isl_ctx *isl_map_get_ctx(__isl_keep isl_map *map)
239 {
240         return map ? map->ctx : NULL;
241 }
242
243 isl_ctx *isl_set_get_ctx(__isl_keep isl_set *set)
244 {
245         return set ? set->ctx : NULL;
246 }
247
248 __isl_give isl_space *isl_basic_map_get_space(__isl_keep isl_basic_map *bmap)
249 {
250         if (!bmap)
251                 return NULL;
252         return isl_space_copy(bmap->dim);
253 }
254
255 __isl_give isl_space *isl_basic_set_get_space(__isl_keep isl_basic_set *bset)
256 {
257         if (!bset)
258                 return NULL;
259         return isl_space_copy(bset->dim);
260 }
261
262 /* Extract the divs in "bmap" as a matrix.
263  */
264 __isl_give isl_mat *isl_basic_map_get_divs(__isl_keep isl_basic_map *bmap)
265 {
266         int i;
267         isl_ctx *ctx;
268         isl_mat *div;
269         unsigned total;
270         unsigned cols;
271
272         if (!bmap)
273                 return NULL;
274
275         ctx = isl_basic_map_get_ctx(bmap);
276         total = isl_space_dim(bmap->dim, isl_dim_all);
277         cols = 1 + 1 + total + bmap->n_div;
278         div = isl_mat_alloc(ctx, bmap->n_div, cols);
279         if (!div)
280                 return NULL;
281
282         for (i = 0; i < bmap->n_div; ++i)
283                 isl_seq_cpy(div->row[i], bmap->div[i], cols);
284
285         return div;
286 }
287
288 __isl_give isl_local_space *isl_basic_map_get_local_space(
289         __isl_keep isl_basic_map *bmap)
290 {
291         isl_mat *div;
292
293         if (!bmap)
294                 return NULL;
295
296         div = isl_basic_map_get_divs(bmap);
297         return isl_local_space_alloc_div(isl_space_copy(bmap->dim), div);
298 }
299
300 __isl_give isl_local_space *isl_basic_set_get_local_space(
301         __isl_keep isl_basic_set *bset)
302 {
303         return isl_basic_map_get_local_space(bset);
304 }
305
306 __isl_give isl_basic_map *isl_basic_map_from_local_space(
307         __isl_take isl_local_space *ls)
308 {
309         int i;
310         int n_div;
311         isl_basic_map *bmap;
312
313         if (!ls)
314                 return NULL;
315
316         n_div = isl_local_space_dim(ls, isl_dim_div);
317         bmap = isl_basic_map_alloc_space(isl_local_space_get_space(ls),
318                                         n_div, 0, 2 * n_div);
319
320         for (i = 0; i < n_div; ++i)
321                 if (isl_basic_map_alloc_div(bmap) < 0)
322                         goto error;
323
324         for (i = 0; i < n_div; ++i) {
325                 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
326                 if (isl_basic_map_add_div_constraints(bmap, i) < 0)
327                         goto error;
328         }
329                                         
330         isl_local_space_free(ls);
331         return bmap;
332 error:
333         isl_local_space_free(ls);
334         isl_basic_map_free(bmap);
335         return NULL;
336 }
337
338 __isl_give isl_basic_set *isl_basic_set_from_local_space(
339         __isl_take isl_local_space *ls)
340 {
341         return isl_basic_map_from_local_space(ls);
342 }
343
344 __isl_give isl_space *isl_map_get_space(__isl_keep isl_map *map)
345 {
346         if (!map)
347                 return NULL;
348         return isl_space_copy(map->dim);
349 }
350
351 __isl_give isl_space *isl_set_get_space(__isl_keep isl_set *set)
352 {
353         if (!set)
354                 return NULL;
355         return isl_space_copy(set->dim);
356 }
357
358 __isl_give isl_basic_map *isl_basic_map_set_tuple_name(
359         __isl_take isl_basic_map *bmap, enum isl_dim_type type, const char *s)
360 {
361         bmap = isl_basic_map_cow(bmap);
362         if (!bmap)
363                 return NULL;
364         bmap->dim = isl_space_set_tuple_name(bmap->dim, type, s);
365         if (!bmap->dim)
366                 goto error;
367         bmap = isl_basic_map_finalize(bmap);
368         return bmap;
369 error:
370         isl_basic_map_free(bmap);
371         return NULL;
372 }
373
374 __isl_give isl_basic_set *isl_basic_set_set_tuple_name(
375         __isl_take isl_basic_set *bset, const char *s)
376 {
377         return isl_basic_map_set_tuple_name(bset, isl_dim_set, s);
378 }
379
380 const char *isl_basic_map_get_tuple_name(__isl_keep isl_basic_map *bmap,
381         enum isl_dim_type type)
382 {
383         return bmap ? isl_space_get_tuple_name(bmap->dim, type) : NULL;
384 }
385
386 __isl_give isl_map *isl_map_set_tuple_name(__isl_take isl_map *map,
387         enum isl_dim_type type, const char *s)
388 {
389         int i;
390
391         map = isl_map_cow(map);
392         if (!map)
393                 return NULL;
394
395         map->dim = isl_space_set_tuple_name(map->dim, type, s);
396         if (!map->dim)
397                 goto error;
398
399         for (i = 0; i < map->n; ++i) {
400                 map->p[i] = isl_basic_map_set_tuple_name(map->p[i], type, s);
401                 if (!map->p[i])
402                         goto error;
403         }
404
405         return map;
406 error:
407         isl_map_free(map);
408         return NULL;
409 }
410
411 /* Does the input or output tuple have a name?
412  */
413 int isl_map_has_tuple_name(__isl_keep isl_map *map, enum isl_dim_type type)
414 {
415         return map ? isl_space_has_tuple_name(map->dim, type) : -1;
416 }
417
418 const char *isl_map_get_tuple_name(__isl_keep isl_map *map,
419         enum isl_dim_type type)
420 {
421         return map ? isl_space_get_tuple_name(map->dim, type) : NULL;
422 }
423
424 __isl_give isl_set *isl_set_set_tuple_name(__isl_take isl_set *set,
425         const char *s)
426 {
427         return (isl_set *)isl_map_set_tuple_name((isl_map *)set, isl_dim_set, s);
428 }
429
430 __isl_give isl_map *isl_map_set_tuple_id(__isl_take isl_map *map,
431         enum isl_dim_type type, __isl_take isl_id *id)
432 {
433         map = isl_map_cow(map);
434         if (!map)
435                 return isl_id_free(id);
436
437         map->dim = isl_space_set_tuple_id(map->dim, type, id);
438
439         return isl_map_reset_space(map, isl_space_copy(map->dim));
440 }
441
442 __isl_give isl_set *isl_set_set_tuple_id(__isl_take isl_set *set,
443         __isl_take isl_id *id)
444 {
445         return isl_map_set_tuple_id(set, isl_dim_set, id);
446 }
447
448 __isl_give isl_map *isl_map_reset_tuple_id(__isl_take isl_map *map,
449         enum isl_dim_type type)
450 {
451         map = isl_map_cow(map);
452         if (!map)
453                 return NULL;
454
455         map->dim = isl_space_reset_tuple_id(map->dim, type);
456
457         return isl_map_reset_space(map, isl_space_copy(map->dim));
458 }
459
460 __isl_give isl_set *isl_set_reset_tuple_id(__isl_take isl_set *set)
461 {
462         return isl_map_reset_tuple_id(set, isl_dim_set);
463 }
464
465 int isl_map_has_tuple_id(__isl_keep isl_map *map, enum isl_dim_type type)
466 {
467         return map ? isl_space_has_tuple_id(map->dim, type) : -1;
468 }
469
470 __isl_give isl_id *isl_map_get_tuple_id(__isl_keep isl_map *map,
471         enum isl_dim_type type)
472 {
473         return map ? isl_space_get_tuple_id(map->dim, type) : NULL;
474 }
475
476 int isl_set_has_tuple_id(__isl_keep isl_set *set)
477 {
478         return isl_map_has_tuple_id(set, isl_dim_set);
479 }
480
481 __isl_give isl_id *isl_set_get_tuple_id(__isl_keep isl_set *set)
482 {
483         return isl_map_get_tuple_id(set, isl_dim_set);
484 }
485
486 /* Does the set tuple have a name?
487  */
488 int isl_set_has_tuple_name(__isl_keep isl_set *set)
489 {
490         return set ? isl_space_has_tuple_name(set->dim, isl_dim_set) : -1;
491 }
492
493
494 const char *isl_basic_set_get_tuple_name(__isl_keep isl_basic_set *bset)
495 {
496         return bset ? isl_space_get_tuple_name(bset->dim, isl_dim_set) : NULL;
497 }
498
499 const char *isl_set_get_tuple_name(__isl_keep isl_set *set)
500 {
501         return set ? isl_space_get_tuple_name(set->dim, isl_dim_set) : NULL;
502 }
503
504 const char *isl_basic_map_get_dim_name(__isl_keep isl_basic_map *bmap,
505         enum isl_dim_type type, unsigned pos)
506 {
507         return bmap ? isl_space_get_dim_name(bmap->dim, type, pos) : NULL;
508 }
509
510 const char *isl_basic_set_get_dim_name(__isl_keep isl_basic_set *bset,
511         enum isl_dim_type type, unsigned pos)
512 {
513         return bset ? isl_space_get_dim_name(bset->dim, type, pos) : NULL;
514 }
515
516 /* Does the given dimension have a name?
517  */
518 int isl_map_has_dim_name(__isl_keep isl_map *map,
519         enum isl_dim_type type, unsigned pos)
520 {
521         return map ? isl_space_has_dim_name(map->dim, type, pos) : -1;
522 }
523
524 const char *isl_map_get_dim_name(__isl_keep isl_map *map,
525         enum isl_dim_type type, unsigned pos)
526 {
527         return map ? isl_space_get_dim_name(map->dim, type, pos) : NULL;
528 }
529
530 const char *isl_set_get_dim_name(__isl_keep isl_set *set,
531         enum isl_dim_type type, unsigned pos)
532 {
533         return set ? isl_space_get_dim_name(set->dim, type, pos) : NULL;
534 }
535
536 /* Does the given dimension have a name?
537  */
538 int isl_set_has_dim_name(__isl_keep isl_set *set,
539         enum isl_dim_type type, unsigned pos)
540 {
541         return set ? isl_space_has_dim_name(set->dim, type, pos) : -1;
542 }
543
544 __isl_give isl_basic_map *isl_basic_map_set_dim_name(
545         __isl_take isl_basic_map *bmap,
546         enum isl_dim_type type, unsigned pos, const char *s)
547 {
548         bmap = isl_basic_map_cow(bmap);
549         if (!bmap)
550                 return NULL;
551         bmap->dim = isl_space_set_dim_name(bmap->dim, type, pos, s);
552         if (!bmap->dim)
553                 goto error;
554         return isl_basic_map_finalize(bmap);
555 error:
556         isl_basic_map_free(bmap);
557         return NULL;
558 }
559
560 __isl_give isl_map *isl_map_set_dim_name(__isl_take isl_map *map,
561         enum isl_dim_type type, unsigned pos, const char *s)
562 {
563         int i;
564
565         map = isl_map_cow(map);
566         if (!map)
567                 return NULL;
568
569         map->dim = isl_space_set_dim_name(map->dim, type, pos, s);
570         if (!map->dim)
571                 goto error;
572
573         for (i = 0; i < map->n; ++i) {
574                 map->p[i] = isl_basic_map_set_dim_name(map->p[i], type, pos, s);
575                 if (!map->p[i])
576                         goto error;
577         }
578
579         return map;
580 error:
581         isl_map_free(map);
582         return NULL;
583 }
584
585 __isl_give isl_basic_set *isl_basic_set_set_dim_name(
586         __isl_take isl_basic_set *bset,
587         enum isl_dim_type type, unsigned pos, const char *s)
588 {
589         return (isl_basic_set *)isl_basic_map_set_dim_name(
590                 (isl_basic_map *)bset, type, pos, s);
591 }
592
593 __isl_give isl_set *isl_set_set_dim_name(__isl_take isl_set *set,
594         enum isl_dim_type type, unsigned pos, const char *s)
595 {
596         return (isl_set *)isl_map_set_dim_name((isl_map *)set, type, pos, s);
597 }
598
599 int isl_basic_map_has_dim_id(__isl_keep isl_basic_map *bmap,
600         enum isl_dim_type type, unsigned pos)
601 {
602         return bmap ? isl_space_has_dim_id(bmap->dim, type, pos) : -1;
603 }
604
605 __isl_give isl_id *isl_basic_set_get_dim_id(__isl_keep isl_basic_set *bset,
606         enum isl_dim_type type, unsigned pos)
607 {
608         return bset ? isl_space_get_dim_id(bset->dim, type, pos) : NULL;
609 }
610
611 int isl_map_has_dim_id(__isl_keep isl_map *map,
612         enum isl_dim_type type, unsigned pos)
613 {
614         return map ? isl_space_has_dim_id(map->dim, type, pos) : -1;
615 }
616
617 __isl_give isl_id *isl_map_get_dim_id(__isl_keep isl_map *map,
618         enum isl_dim_type type, unsigned pos)
619 {
620         return map ? isl_space_get_dim_id(map->dim, type, pos) : NULL;
621 }
622
623 int isl_set_has_dim_id(__isl_keep isl_set *set,
624         enum isl_dim_type type, unsigned pos)
625 {
626         return isl_map_has_dim_id(set, type, pos);
627 }
628
629 __isl_give isl_id *isl_set_get_dim_id(__isl_keep isl_set *set,
630         enum isl_dim_type type, unsigned pos)
631 {
632         return isl_map_get_dim_id(set, type, pos);
633 }
634
635 __isl_give isl_map *isl_map_set_dim_id(__isl_take isl_map *map,
636         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
637 {
638         map = isl_map_cow(map);
639         if (!map)
640                 return isl_id_free(id);
641
642         map->dim = isl_space_set_dim_id(map->dim, type, pos, id);
643
644         return isl_map_reset_space(map, isl_space_copy(map->dim));
645 }
646
647 __isl_give isl_set *isl_set_set_dim_id(__isl_take isl_set *set,
648         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
649 {
650         return isl_map_set_dim_id(set, type, pos, id);
651 }
652
653 int isl_map_find_dim_by_id(__isl_keep isl_map *map, enum isl_dim_type type,
654         __isl_keep isl_id *id)
655 {
656         if (!map)
657                 return -1;
658         return isl_space_find_dim_by_id(map->dim, type, id);
659 }
660
661 int isl_set_find_dim_by_id(__isl_keep isl_set *set, enum isl_dim_type type,
662         __isl_keep isl_id *id)
663 {
664         return isl_map_find_dim_by_id(set, type, id);
665 }
666
667 int isl_map_find_dim_by_name(__isl_keep isl_map *map, enum isl_dim_type type,
668         const char *name)
669 {
670         if (!map)
671                 return -1;
672         return isl_space_find_dim_by_name(map->dim, type, name);
673 }
674
675 int isl_set_find_dim_by_name(__isl_keep isl_set *set, enum isl_dim_type type,
676         const char *name)
677 {
678         return isl_map_find_dim_by_name(set, type, name);
679 }
680
681 int isl_basic_map_is_rational(__isl_keep isl_basic_map *bmap)
682 {
683         if (!bmap)
684                 return -1;
685         return ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL);
686 }
687
688 int isl_basic_set_is_rational(__isl_keep isl_basic_set *bset)
689 {
690         return isl_basic_map_is_rational(bset);
691 }
692
693 /* Does "bmap" contain any rational points?
694  *
695  * If "bmap" has an equality for each dimension, equating the dimension
696  * to an integer constant, then it has no rational points, even if it
697  * is marked as rational.
698  */
699 int isl_basic_map_has_rational(__isl_keep isl_basic_map *bmap)
700 {
701         int has_rational = 1;
702         unsigned total;
703
704         if (!bmap)
705                 return -1;
706         if (isl_basic_map_plain_is_empty(bmap))
707                 return 0;
708         if (!isl_basic_map_is_rational(bmap))
709                 return 0;
710         bmap = isl_basic_map_copy(bmap);
711         bmap = isl_basic_map_implicit_equalities(bmap);
712         if (!bmap)
713                 return -1;
714         total = isl_basic_map_total_dim(bmap);
715         if (bmap->n_eq == total) {
716                 int i, j;
717                 for (i = 0; i < bmap->n_eq; ++i) {
718                         j = isl_seq_first_non_zero(bmap->eq[i] + 1, total);
719                         if (j < 0)
720                                 break;
721                         if (!isl_int_is_one(bmap->eq[i][1 + j]) &&
722                             !isl_int_is_negone(bmap->eq[i][1 + j]))
723                                 break;
724                         j = isl_seq_first_non_zero(bmap->eq[i] + 1 + j + 1,
725                                                     total - j - 1);
726                         if (j >= 0)
727                                 break;
728                 }
729                 if (i == bmap->n_eq)
730                         has_rational = 0;
731         }
732         isl_basic_map_free(bmap);
733
734         return has_rational;
735 }
736
737 /* Does "map" contain any rational points?
738  */
739 int isl_map_has_rational(__isl_keep isl_map *map)
740 {
741         int i;
742         int has_rational;
743
744         if (!map)
745                 return -1;
746         for (i = 0; i < map->n; ++i) {
747                 has_rational = isl_basic_map_has_rational(map->p[i]);
748                 if (has_rational < 0)
749                         return -1;
750                 if (has_rational)
751                         return 1;
752         }
753         return 0;
754 }
755
756 /* Does "set" contain any rational points?
757  */
758 int isl_set_has_rational(__isl_keep isl_set *set)
759 {
760         return isl_map_has_rational(set);
761 }
762
763 /* Is this basic set a parameter domain?
764  */
765 int isl_basic_set_is_params(__isl_keep isl_basic_set *bset)
766 {
767         if (!bset)
768                 return -1;
769         return isl_space_is_params(bset->dim);
770 }
771
772 /* Is this set a parameter domain?
773  */
774 int isl_set_is_params(__isl_keep isl_set *set)
775 {
776         if (!set)
777                 return -1;
778         return isl_space_is_params(set->dim);
779 }
780
781 /* Is this map actually a parameter domain?
782  * Users should never call this function.  Outside of isl,
783  * a map can never be a parameter domain.
784  */
785 int isl_map_is_params(__isl_keep isl_map *map)
786 {
787         if (!map)
788                 return -1;
789         return isl_space_is_params(map->dim);
790 }
791
792 static struct isl_basic_map *basic_map_init(struct isl_ctx *ctx,
793                 struct isl_basic_map *bmap, unsigned extra,
794                 unsigned n_eq, unsigned n_ineq)
795 {
796         int i;
797         size_t row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + extra;
798
799         bmap->ctx = ctx;
800         isl_ctx_ref(ctx);
801
802         bmap->block = isl_blk_alloc(ctx, (n_ineq + n_eq) * row_size);
803         if (isl_blk_is_error(bmap->block))
804                 goto error;
805
806         bmap->ineq = isl_alloc_array(ctx, isl_int *, n_ineq + n_eq);
807         if (!bmap->ineq)
808                 goto error;
809
810         if (extra == 0) {
811                 bmap->block2 = isl_blk_empty();
812                 bmap->div = NULL;
813         } else {
814                 bmap->block2 = isl_blk_alloc(ctx, extra * (1 + row_size));
815                 if (isl_blk_is_error(bmap->block2))
816                         goto error;
817
818                 bmap->div = isl_alloc_array(ctx, isl_int *, extra);
819                 if (!bmap->div)
820                         goto error;
821         }
822
823         for (i = 0; i < n_ineq + n_eq; ++i)
824                 bmap->ineq[i] = bmap->block.data + i * row_size;
825
826         for (i = 0; i < extra; ++i)
827                 bmap->div[i] = bmap->block2.data + i * (1 + row_size);
828
829         bmap->ref = 1;
830         bmap->flags = 0;
831         bmap->c_size = n_eq + n_ineq;
832         bmap->eq = bmap->ineq + n_ineq;
833         bmap->extra = extra;
834         bmap->n_eq = 0;
835         bmap->n_ineq = 0;
836         bmap->n_div = 0;
837         bmap->sample = NULL;
838
839         return bmap;
840 error:
841         isl_basic_map_free(bmap);
842         return NULL;
843 }
844
845 struct isl_basic_set *isl_basic_set_alloc(struct isl_ctx *ctx,
846                 unsigned nparam, unsigned dim, unsigned extra,
847                 unsigned n_eq, unsigned n_ineq)
848 {
849         struct isl_basic_map *bmap;
850         isl_space *space;
851
852         space = isl_space_set_alloc(ctx, nparam, dim);
853         if (!space)
854                 return NULL;
855
856         bmap = isl_basic_map_alloc_space(space, extra, n_eq, n_ineq);
857         return (struct isl_basic_set *)bmap;
858 }
859
860 struct isl_basic_set *isl_basic_set_alloc_space(__isl_take isl_space *dim,
861                 unsigned extra, unsigned n_eq, unsigned n_ineq)
862 {
863         struct isl_basic_map *bmap;
864         if (!dim)
865                 return NULL;
866         isl_assert(dim->ctx, dim->n_in == 0, goto error);
867         bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
868         return (struct isl_basic_set *)bmap;
869 error:
870         isl_space_free(dim);
871         return NULL;
872 }
873
874 struct isl_basic_map *isl_basic_map_alloc_space(__isl_take isl_space *dim,
875                 unsigned extra, unsigned n_eq, unsigned n_ineq)
876 {
877         struct isl_basic_map *bmap;
878
879         if (!dim)
880                 return NULL;
881         bmap = isl_calloc_type(dim->ctx, struct isl_basic_map);
882         if (!bmap)
883                 goto error;
884         bmap->dim = dim;
885
886         return basic_map_init(dim->ctx, bmap, extra, n_eq, n_ineq);
887 error:
888         isl_space_free(dim);
889         return NULL;
890 }
891
892 struct isl_basic_map *isl_basic_map_alloc(struct isl_ctx *ctx,
893                 unsigned nparam, unsigned in, unsigned out, unsigned extra,
894                 unsigned n_eq, unsigned n_ineq)
895 {
896         struct isl_basic_map *bmap;
897         isl_space *dim;
898
899         dim = isl_space_alloc(ctx, nparam, in, out);
900         if (!dim)
901                 return NULL;
902
903         bmap = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
904         return bmap;
905 }
906
907 static void dup_constraints(
908                 struct isl_basic_map *dst, struct isl_basic_map *src)
909 {
910         int i;
911         unsigned total = isl_basic_map_total_dim(src);
912
913         for (i = 0; i < src->n_eq; ++i) {
914                 int j = isl_basic_map_alloc_equality(dst);
915                 isl_seq_cpy(dst->eq[j], src->eq[i], 1+total);
916         }
917
918         for (i = 0; i < src->n_ineq; ++i) {
919                 int j = isl_basic_map_alloc_inequality(dst);
920                 isl_seq_cpy(dst->ineq[j], src->ineq[i], 1+total);
921         }
922
923         for (i = 0; i < src->n_div; ++i) {
924                 int j = isl_basic_map_alloc_div(dst);
925                 isl_seq_cpy(dst->div[j], src->div[i], 1+1+total);
926         }
927         ISL_F_SET(dst, ISL_BASIC_SET_FINAL);
928 }
929
930 struct isl_basic_map *isl_basic_map_dup(struct isl_basic_map *bmap)
931 {
932         struct isl_basic_map *dup;
933
934         if (!bmap)
935                 return NULL;
936         dup = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
937                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
938         if (!dup)
939                 return NULL;
940         dup_constraints(dup, bmap);
941         dup->flags = bmap->flags;
942         dup->sample = isl_vec_copy(bmap->sample);
943         return dup;
944 }
945
946 struct isl_basic_set *isl_basic_set_dup(struct isl_basic_set *bset)
947 {
948         struct isl_basic_map *dup;
949
950         dup = isl_basic_map_dup((struct isl_basic_map *)bset);
951         return (struct isl_basic_set *)dup;
952 }
953
954 struct isl_basic_set *isl_basic_set_copy(struct isl_basic_set *bset)
955 {
956         if (!bset)
957                 return NULL;
958
959         if (ISL_F_ISSET(bset, ISL_BASIC_SET_FINAL)) {
960                 bset->ref++;
961                 return bset;
962         }
963         return isl_basic_set_dup(bset);
964 }
965
966 struct isl_set *isl_set_copy(struct isl_set *set)
967 {
968         if (!set)
969                 return NULL;
970
971         set->ref++;
972         return set;
973 }
974
975 struct isl_basic_map *isl_basic_map_copy(struct isl_basic_map *bmap)
976 {
977         if (!bmap)
978                 return NULL;
979
980         if (ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL)) {
981                 bmap->ref++;
982                 return bmap;
983         }
984         bmap = isl_basic_map_dup(bmap);
985         if (bmap)
986                 ISL_F_SET(bmap, ISL_BASIC_SET_FINAL);
987         return bmap;
988 }
989
990 struct isl_map *isl_map_copy(struct isl_map *map)
991 {
992         if (!map)
993                 return NULL;
994
995         map->ref++;
996         return map;
997 }
998
999 void *isl_basic_map_free(__isl_take isl_basic_map *bmap)
1000 {
1001         if (!bmap)
1002                 return NULL;
1003
1004         if (--bmap->ref > 0)
1005                 return NULL;
1006
1007         isl_ctx_deref(bmap->ctx);
1008         free(bmap->div);
1009         isl_blk_free(bmap->ctx, bmap->block2);
1010         free(bmap->ineq);
1011         isl_blk_free(bmap->ctx, bmap->block);
1012         isl_vec_free(bmap->sample);
1013         isl_space_free(bmap->dim);
1014         free(bmap);
1015
1016         return NULL;
1017 }
1018
1019 void *isl_basic_set_free(struct isl_basic_set *bset)
1020 {
1021         return isl_basic_map_free((struct isl_basic_map *)bset);
1022 }
1023
1024 static int room_for_con(struct isl_basic_map *bmap, unsigned n)
1025 {
1026         return bmap->n_eq + bmap->n_ineq + n <= bmap->c_size;
1027 }
1028
1029 __isl_give isl_map *isl_map_align_params_map_map_and(
1030         __isl_take isl_map *map1, __isl_take isl_map *map2,
1031         __isl_give isl_map *(*fn)(__isl_take isl_map *map1,
1032                                     __isl_take isl_map *map2))
1033 {
1034         if (!map1 || !map2)
1035                 goto error;
1036         if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1037                 return fn(map1, map2);
1038         if (!isl_space_has_named_params(map1->dim) ||
1039             !isl_space_has_named_params(map2->dim))
1040                 isl_die(map1->ctx, isl_error_invalid,
1041                         "unaligned unnamed parameters", goto error);
1042         map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1043         map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1044         return fn(map1, map2);
1045 error:
1046         isl_map_free(map1);
1047         isl_map_free(map2);
1048         return NULL;
1049 }
1050
1051 int isl_map_align_params_map_map_and_test(__isl_keep isl_map *map1,
1052         __isl_keep isl_map *map2,
1053         int (*fn)(__isl_keep isl_map *map1, __isl_keep isl_map *map2))
1054 {
1055         int r;
1056
1057         if (!map1 || !map2)
1058                 return -1;
1059         if (isl_space_match(map1->dim, isl_dim_param, map2->dim, isl_dim_param))
1060                 return fn(map1, map2);
1061         if (!isl_space_has_named_params(map1->dim) ||
1062             !isl_space_has_named_params(map2->dim))
1063                 isl_die(map1->ctx, isl_error_invalid,
1064                         "unaligned unnamed parameters", return -1);
1065         map1 = isl_map_copy(map1);
1066         map2 = isl_map_copy(map2);
1067         map1 = isl_map_align_params(map1, isl_map_get_space(map2));
1068         map2 = isl_map_align_params(map2, isl_map_get_space(map1));
1069         r = fn(map1, map2);
1070         isl_map_free(map1);
1071         isl_map_free(map2);
1072         return r;
1073 }
1074
1075 int isl_basic_map_alloc_equality(struct isl_basic_map *bmap)
1076 {
1077         struct isl_ctx *ctx;
1078         if (!bmap)
1079                 return -1;
1080         ctx = bmap->ctx;
1081         isl_assert(ctx, room_for_con(bmap, 1), return -1);
1082         isl_assert(ctx, (bmap->eq - bmap->ineq) + bmap->n_eq <= bmap->c_size,
1083                         return -1);
1084         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1085         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1086         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1087         ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1088         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1089         if ((bmap->eq - bmap->ineq) + bmap->n_eq == bmap->c_size) {
1090                 isl_int *t;
1091                 int j = isl_basic_map_alloc_inequality(bmap);
1092                 if (j < 0)
1093                         return -1;
1094                 t = bmap->ineq[j];
1095                 bmap->ineq[j] = bmap->ineq[bmap->n_ineq - 1];
1096                 bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1097                 bmap->eq[-1] = t;
1098                 bmap->n_eq++;
1099                 bmap->n_ineq--;
1100                 bmap->eq--;
1101                 return 0;
1102         }
1103         isl_seq_clr(bmap->eq[bmap->n_eq] + 1 + isl_basic_map_total_dim(bmap),
1104                       bmap->extra - bmap->n_div);
1105         return bmap->n_eq++;
1106 }
1107
1108 int isl_basic_set_alloc_equality(struct isl_basic_set *bset)
1109 {
1110         return isl_basic_map_alloc_equality((struct isl_basic_map *)bset);
1111 }
1112
1113 int isl_basic_map_free_equality(struct isl_basic_map *bmap, unsigned n)
1114 {
1115         if (!bmap)
1116                 return -1;
1117         isl_assert(bmap->ctx, n <= bmap->n_eq, return -1);
1118         bmap->n_eq -= n;
1119         return 0;
1120 }
1121
1122 int isl_basic_set_free_equality(struct isl_basic_set *bset, unsigned n)
1123 {
1124         return isl_basic_map_free_equality((struct isl_basic_map *)bset, n);
1125 }
1126
1127 int isl_basic_map_drop_equality(struct isl_basic_map *bmap, unsigned pos)
1128 {
1129         isl_int *t;
1130         if (!bmap)
1131                 return -1;
1132         isl_assert(bmap->ctx, pos < bmap->n_eq, return -1);
1133
1134         if (pos != bmap->n_eq - 1) {
1135                 t = bmap->eq[pos];
1136                 bmap->eq[pos] = bmap->eq[bmap->n_eq - 1];
1137                 bmap->eq[bmap->n_eq - 1] = t;
1138         }
1139         bmap->n_eq--;
1140         return 0;
1141 }
1142
1143 int isl_basic_set_drop_equality(struct isl_basic_set *bset, unsigned pos)
1144 {
1145         return isl_basic_map_drop_equality((struct isl_basic_map *)bset, pos);
1146 }
1147
1148 void isl_basic_map_inequality_to_equality(
1149                 struct isl_basic_map *bmap, unsigned pos)
1150 {
1151         isl_int *t;
1152
1153         t = bmap->ineq[pos];
1154         bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1155         bmap->ineq[bmap->n_ineq - 1] = bmap->eq[-1];
1156         bmap->eq[-1] = t;
1157         bmap->n_eq++;
1158         bmap->n_ineq--;
1159         bmap->eq--;
1160         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1161         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1162         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1163         ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1164 }
1165
1166 static int room_for_ineq(struct isl_basic_map *bmap, unsigned n)
1167 {
1168         return bmap->n_ineq + n <= bmap->eq - bmap->ineq;
1169 }
1170
1171 int isl_basic_map_alloc_inequality(struct isl_basic_map *bmap)
1172 {
1173         struct isl_ctx *ctx;
1174         if (!bmap)
1175                 return -1;
1176         ctx = bmap->ctx;
1177         isl_assert(ctx, room_for_ineq(bmap, 1), return -1);
1178         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_IMPLICIT);
1179         ISL_F_CLR(bmap, ISL_BASIC_MAP_NO_REDUNDANT);
1180         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1181         ISL_F_CLR(bmap, ISL_BASIC_MAP_ALL_EQUALITIES);
1182         isl_seq_clr(bmap->ineq[bmap->n_ineq] +
1183                       1 + isl_basic_map_total_dim(bmap),
1184                       bmap->extra - bmap->n_div);
1185         return bmap->n_ineq++;
1186 }
1187
1188 int isl_basic_set_alloc_inequality(struct isl_basic_set *bset)
1189 {
1190         return isl_basic_map_alloc_inequality((struct isl_basic_map *)bset);
1191 }
1192
1193 int isl_basic_map_free_inequality(struct isl_basic_map *bmap, unsigned n)
1194 {
1195         if (!bmap)
1196                 return -1;
1197         isl_assert(bmap->ctx, n <= bmap->n_ineq, return -1);
1198         bmap->n_ineq -= n;
1199         return 0;
1200 }
1201
1202 int isl_basic_set_free_inequality(struct isl_basic_set *bset, unsigned n)
1203 {
1204         return isl_basic_map_free_inequality((struct isl_basic_map *)bset, n);
1205 }
1206
1207 int isl_basic_map_drop_inequality(struct isl_basic_map *bmap, unsigned pos)
1208 {
1209         isl_int *t;
1210         if (!bmap)
1211                 return -1;
1212         isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
1213
1214         if (pos != bmap->n_ineq - 1) {
1215                 t = bmap->ineq[pos];
1216                 bmap->ineq[pos] = bmap->ineq[bmap->n_ineq - 1];
1217                 bmap->ineq[bmap->n_ineq - 1] = t;
1218                 ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1219         }
1220         bmap->n_ineq--;
1221         return 0;
1222 }
1223
1224 int isl_basic_set_drop_inequality(struct isl_basic_set *bset, unsigned pos)
1225 {
1226         return isl_basic_map_drop_inequality((struct isl_basic_map *)bset, pos);
1227 }
1228
1229 __isl_give isl_basic_map *isl_basic_map_add_eq(__isl_take isl_basic_map *bmap,
1230         isl_int *eq)
1231 {
1232         int k;
1233
1234         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
1235         if (!bmap)
1236                 return NULL;
1237         k = isl_basic_map_alloc_equality(bmap);
1238         if (k < 0)
1239                 goto error;
1240         isl_seq_cpy(bmap->eq[k], eq, 1 + isl_basic_map_total_dim(bmap));
1241         return bmap;
1242 error:
1243         isl_basic_map_free(bmap);
1244         return NULL;
1245 }
1246
1247 __isl_give isl_basic_set *isl_basic_set_add_eq(__isl_take isl_basic_set *bset,
1248         isl_int *eq)
1249 {
1250         return (isl_basic_set *)
1251                 isl_basic_map_add_eq((isl_basic_map *)bset, eq);
1252 }
1253
1254 __isl_give isl_basic_map *isl_basic_map_add_ineq(__isl_take isl_basic_map *bmap,
1255         isl_int *ineq)
1256 {
1257         int k;
1258
1259         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
1260         if (!bmap)
1261                 return NULL;
1262         k = isl_basic_map_alloc_inequality(bmap);
1263         if (k < 0)
1264                 goto error;
1265         isl_seq_cpy(bmap->ineq[k], ineq, 1 + isl_basic_map_total_dim(bmap));
1266         return bmap;
1267 error:
1268         isl_basic_map_free(bmap);
1269         return NULL;
1270 }
1271
1272 __isl_give isl_basic_set *isl_basic_set_add_ineq(__isl_take isl_basic_set *bset,
1273         isl_int *ineq)
1274 {
1275         return (isl_basic_set *)
1276                 isl_basic_map_add_ineq((isl_basic_map *)bset, ineq);
1277 }
1278
1279 int isl_basic_map_alloc_div(struct isl_basic_map *bmap)
1280 {
1281         if (!bmap)
1282                 return -1;
1283         isl_assert(bmap->ctx, bmap->n_div < bmap->extra, return -1);
1284         isl_seq_clr(bmap->div[bmap->n_div] +
1285                       1 + 1 + isl_basic_map_total_dim(bmap),
1286                       bmap->extra - bmap->n_div);
1287         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED_DIVS);
1288         return bmap->n_div++;
1289 }
1290
1291 int isl_basic_set_alloc_div(struct isl_basic_set *bset)
1292 {
1293         return isl_basic_map_alloc_div((struct isl_basic_map *)bset);
1294 }
1295
1296 int isl_basic_map_free_div(struct isl_basic_map *bmap, unsigned n)
1297 {
1298         if (!bmap)
1299                 return -1;
1300         isl_assert(bmap->ctx, n <= bmap->n_div, return -1);
1301         bmap->n_div -= n;
1302         return 0;
1303 }
1304
1305 int isl_basic_set_free_div(struct isl_basic_set *bset, unsigned n)
1306 {
1307         return isl_basic_map_free_div((struct isl_basic_map *)bset, n);
1308 }
1309
1310 /* Copy constraint from src to dst, putting the vars of src at offset
1311  * dim_off in dst and the divs of src at offset div_off in dst.
1312  * If both sets are actually map, then dim_off applies to the input
1313  * variables.
1314  */
1315 static void copy_constraint(struct isl_basic_map *dst_map, isl_int *dst,
1316                             struct isl_basic_map *src_map, isl_int *src,
1317                             unsigned in_off, unsigned out_off, unsigned div_off)
1318 {
1319         unsigned src_nparam = isl_basic_map_n_param(src_map);
1320         unsigned dst_nparam = isl_basic_map_n_param(dst_map);
1321         unsigned src_in = isl_basic_map_n_in(src_map);
1322         unsigned dst_in = isl_basic_map_n_in(dst_map);
1323         unsigned src_out = isl_basic_map_n_out(src_map);
1324         unsigned dst_out = isl_basic_map_n_out(dst_map);
1325         isl_int_set(dst[0], src[0]);
1326         isl_seq_cpy(dst+1, src+1, isl_min(dst_nparam, src_nparam));
1327         if (dst_nparam > src_nparam)
1328                 isl_seq_clr(dst+1+src_nparam,
1329                                 dst_nparam - src_nparam);
1330         isl_seq_clr(dst+1+dst_nparam, in_off);
1331         isl_seq_cpy(dst+1+dst_nparam+in_off,
1332                     src+1+src_nparam,
1333                     isl_min(dst_in-in_off, src_in));
1334         if (dst_in-in_off > src_in)
1335                 isl_seq_clr(dst+1+dst_nparam+in_off+src_in,
1336                                 dst_in - in_off - src_in);
1337         isl_seq_clr(dst+1+dst_nparam+dst_in, out_off);
1338         isl_seq_cpy(dst+1+dst_nparam+dst_in+out_off,
1339                     src+1+src_nparam+src_in,
1340                     isl_min(dst_out-out_off, src_out));
1341         if (dst_out-out_off > src_out)
1342                 isl_seq_clr(dst+1+dst_nparam+dst_in+out_off+src_out,
1343                                 dst_out - out_off - src_out);
1344         isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out, div_off);
1345         isl_seq_cpy(dst+1+dst_nparam+dst_in+dst_out+div_off,
1346                     src+1+src_nparam+src_in+src_out,
1347                     isl_min(dst_map->extra-div_off, src_map->n_div));
1348         if (dst_map->n_div-div_off > src_map->n_div)
1349                 isl_seq_clr(dst+1+dst_nparam+dst_in+dst_out+
1350                                 div_off+src_map->n_div,
1351                                 dst_map->n_div - div_off - src_map->n_div);
1352 }
1353
1354 static void copy_div(struct isl_basic_map *dst_map, isl_int *dst,
1355                      struct isl_basic_map *src_map, isl_int *src,
1356                      unsigned in_off, unsigned out_off, unsigned div_off)
1357 {
1358         isl_int_set(dst[0], src[0]);
1359         copy_constraint(dst_map, dst+1, src_map, src+1, in_off, out_off, div_off);
1360 }
1361
1362 static struct isl_basic_map *add_constraints(struct isl_basic_map *bmap1,
1363                 struct isl_basic_map *bmap2, unsigned i_pos, unsigned o_pos)
1364 {
1365         int i;
1366         unsigned div_off;
1367
1368         if (!bmap1 || !bmap2)
1369                 goto error;
1370
1371         div_off = bmap1->n_div;
1372
1373         for (i = 0; i < bmap2->n_eq; ++i) {
1374                 int i1 = isl_basic_map_alloc_equality(bmap1);
1375                 if (i1 < 0)
1376                         goto error;
1377                 copy_constraint(bmap1, bmap1->eq[i1], bmap2, bmap2->eq[i],
1378                                 i_pos, o_pos, div_off);
1379         }
1380
1381         for (i = 0; i < bmap2->n_ineq; ++i) {
1382                 int i1 = isl_basic_map_alloc_inequality(bmap1);
1383                 if (i1 < 0)
1384                         goto error;
1385                 copy_constraint(bmap1, bmap1->ineq[i1], bmap2, bmap2->ineq[i],
1386                                 i_pos, o_pos, div_off);
1387         }
1388
1389         for (i = 0; i < bmap2->n_div; ++i) {
1390                 int i1 = isl_basic_map_alloc_div(bmap1);
1391                 if (i1 < 0)
1392                         goto error;
1393                 copy_div(bmap1, bmap1->div[i1], bmap2, bmap2->div[i],
1394                          i_pos, o_pos, div_off);
1395         }
1396
1397         isl_basic_map_free(bmap2);
1398
1399         return bmap1;
1400
1401 error:
1402         isl_basic_map_free(bmap1);
1403         isl_basic_map_free(bmap2);
1404         return NULL;
1405 }
1406
1407 struct isl_basic_set *isl_basic_set_add_constraints(struct isl_basic_set *bset1,
1408                 struct isl_basic_set *bset2, unsigned pos)
1409 {
1410         return (struct isl_basic_set *)
1411                 add_constraints((struct isl_basic_map *)bset1,
1412                                 (struct isl_basic_map *)bset2, 0, pos);
1413 }
1414
1415 struct isl_basic_map *isl_basic_map_extend_space(struct isl_basic_map *base,
1416                 __isl_take isl_space *dim, unsigned extra,
1417                 unsigned n_eq, unsigned n_ineq)
1418 {
1419         struct isl_basic_map *ext;
1420         unsigned flags;
1421         int dims_ok;
1422
1423         if (!dim)
1424                 goto error;
1425
1426         if (!base)
1427                 goto error;
1428
1429         dims_ok = isl_space_is_equal(base->dim, dim) &&
1430                   base->extra >= base->n_div + extra;
1431
1432         if (dims_ok && room_for_con(base, n_eq + n_ineq) &&
1433                        room_for_ineq(base, n_ineq)) {
1434                 isl_space_free(dim);
1435                 return base;
1436         }
1437
1438         isl_assert(base->ctx, base->dim->nparam <= dim->nparam, goto error);
1439         isl_assert(base->ctx, base->dim->n_in <= dim->n_in, goto error);
1440         isl_assert(base->ctx, base->dim->n_out <= dim->n_out, goto error);
1441         extra += base->extra;
1442         n_eq += base->n_eq;
1443         n_ineq += base->n_ineq;
1444
1445         ext = isl_basic_map_alloc_space(dim, extra, n_eq, n_ineq);
1446         dim = NULL;
1447         if (!ext)
1448                 goto error;
1449
1450         if (dims_ok)
1451                 ext->sample = isl_vec_copy(base->sample);
1452         flags = base->flags;
1453         ext = add_constraints(ext, base, 0, 0);
1454         if (ext) {
1455                 ext->flags = flags;
1456                 ISL_F_CLR(ext, ISL_BASIC_SET_FINAL);
1457         }
1458
1459         return ext;
1460
1461 error:
1462         isl_space_free(dim);
1463         isl_basic_map_free(base);
1464         return NULL;
1465 }
1466
1467 struct isl_basic_set *isl_basic_set_extend_space(struct isl_basic_set *base,
1468                 __isl_take isl_space *dim, unsigned extra,
1469                 unsigned n_eq, unsigned n_ineq)
1470 {
1471         return (struct isl_basic_set *)
1472                 isl_basic_map_extend_space((struct isl_basic_map *)base, dim,
1473                                                         extra, n_eq, n_ineq);
1474 }
1475
1476 struct isl_basic_map *isl_basic_map_extend_constraints(
1477                 struct isl_basic_map *base, unsigned n_eq, unsigned n_ineq)
1478 {
1479         if (!base)
1480                 return NULL;
1481         return isl_basic_map_extend_space(base, isl_space_copy(base->dim),
1482                                         0, n_eq, n_ineq);
1483 }
1484
1485 struct isl_basic_map *isl_basic_map_extend(struct isl_basic_map *base,
1486                 unsigned nparam, unsigned n_in, unsigned n_out, unsigned extra,
1487                 unsigned n_eq, unsigned n_ineq)
1488 {
1489         struct isl_basic_map *bmap;
1490         isl_space *dim;
1491
1492         if (!base)
1493                 return NULL;
1494         dim = isl_space_alloc(base->ctx, nparam, n_in, n_out);
1495         if (!dim)
1496                 goto error;
1497
1498         bmap = isl_basic_map_extend_space(base, dim, extra, n_eq, n_ineq);
1499         return bmap;
1500 error:
1501         isl_basic_map_free(base);
1502         return NULL;
1503 }
1504
1505 struct isl_basic_set *isl_basic_set_extend(struct isl_basic_set *base,
1506                 unsigned nparam, unsigned dim, unsigned extra,
1507                 unsigned n_eq, unsigned n_ineq)
1508 {
1509         return (struct isl_basic_set *)
1510                 isl_basic_map_extend((struct isl_basic_map *)base,
1511                                         nparam, 0, dim, extra, n_eq, n_ineq);
1512 }
1513
1514 struct isl_basic_set *isl_basic_set_extend_constraints(
1515                 struct isl_basic_set *base, unsigned n_eq, unsigned n_ineq)
1516 {
1517         return (struct isl_basic_set *)
1518                 isl_basic_map_extend_constraints((struct isl_basic_map *)base,
1519                                                     n_eq, n_ineq);
1520 }
1521
1522 struct isl_basic_set *isl_basic_set_cow(struct isl_basic_set *bset)
1523 {
1524         return (struct isl_basic_set *)
1525                 isl_basic_map_cow((struct isl_basic_map *)bset);
1526 }
1527
1528 struct isl_basic_map *isl_basic_map_cow(struct isl_basic_map *bmap)
1529 {
1530         if (!bmap)
1531                 return NULL;
1532
1533         if (bmap->ref > 1) {
1534                 bmap->ref--;
1535                 bmap = isl_basic_map_dup(bmap);
1536         }
1537         if (bmap)
1538                 ISL_F_CLR(bmap, ISL_BASIC_SET_FINAL);
1539         return bmap;
1540 }
1541
1542 struct isl_set *isl_set_cow(struct isl_set *set)
1543 {
1544         if (!set)
1545                 return NULL;
1546
1547         if (set->ref == 1)
1548                 return set;
1549         set->ref--;
1550         return isl_set_dup(set);
1551 }
1552
1553 struct isl_map *isl_map_cow(struct isl_map *map)
1554 {
1555         if (!map)
1556                 return NULL;
1557
1558         if (map->ref == 1)
1559                 return map;
1560         map->ref--;
1561         return isl_map_dup(map);
1562 }
1563
1564 static void swap_vars(struct isl_blk blk, isl_int *a,
1565                         unsigned a_len, unsigned b_len)
1566 {
1567         isl_seq_cpy(blk.data, a+a_len, b_len);
1568         isl_seq_cpy(blk.data+b_len, a, a_len);
1569         isl_seq_cpy(a, blk.data, b_len+a_len);
1570 }
1571
1572 static __isl_give isl_basic_map *isl_basic_map_swap_vars(
1573         __isl_take isl_basic_map *bmap, unsigned pos, unsigned n1, unsigned n2)
1574 {
1575         int i;
1576         struct isl_blk blk;
1577
1578         if (!bmap)
1579                 goto error;
1580
1581         isl_assert(bmap->ctx,
1582                 pos + n1 + n2 <= 1 + isl_basic_map_total_dim(bmap), goto error);
1583
1584         if (n1 == 0 || n2 == 0)
1585                 return bmap;
1586
1587         bmap = isl_basic_map_cow(bmap);
1588         if (!bmap)
1589                 return NULL;
1590
1591         blk = isl_blk_alloc(bmap->ctx, n1 + n2);
1592         if (isl_blk_is_error(blk))
1593                 goto error;
1594
1595         for (i = 0; i < bmap->n_eq; ++i)
1596                 swap_vars(blk,
1597                           bmap->eq[i] + pos, n1, n2);
1598
1599         for (i = 0; i < bmap->n_ineq; ++i)
1600                 swap_vars(blk,
1601                           bmap->ineq[i] + pos, n1, n2);
1602
1603         for (i = 0; i < bmap->n_div; ++i)
1604                 swap_vars(blk,
1605                           bmap->div[i]+1 + pos, n1, n2);
1606
1607         isl_blk_free(bmap->ctx, blk);
1608
1609         ISL_F_CLR(bmap, ISL_BASIC_SET_NORMALIZED);
1610         bmap = isl_basic_map_gauss(bmap, NULL);
1611         return isl_basic_map_finalize(bmap);
1612 error:
1613         isl_basic_map_free(bmap);
1614         return NULL;
1615 }
1616
1617 static __isl_give isl_basic_set *isl_basic_set_swap_vars(
1618         __isl_take isl_basic_set *bset, unsigned n)
1619 {
1620         unsigned dim;
1621         unsigned nparam;
1622
1623         if (!bset)
1624                 return NULL;
1625
1626         nparam = isl_basic_set_n_param(bset);
1627         dim = isl_basic_set_n_dim(bset);
1628         isl_assert(bset->ctx, n <= dim, goto error);
1629
1630         return isl_basic_map_swap_vars(bset, 1 + nparam, n, dim - n);
1631 error:
1632         isl_basic_set_free(bset);
1633         return NULL;
1634 }
1635
1636 struct isl_basic_map *isl_basic_map_set_to_empty(struct isl_basic_map *bmap)
1637 {
1638         int i = 0;
1639         unsigned total;
1640         if (!bmap)
1641                 goto error;
1642         total = isl_basic_map_total_dim(bmap);
1643         isl_basic_map_free_div(bmap, bmap->n_div);
1644         isl_basic_map_free_inequality(bmap, bmap->n_ineq);
1645         if (bmap->n_eq > 0)
1646                 isl_basic_map_free_equality(bmap, bmap->n_eq-1);
1647         else {
1648                 i = isl_basic_map_alloc_equality(bmap);
1649                 if (i < 0)
1650                         goto error;
1651         }
1652         isl_int_set_si(bmap->eq[i][0], 1);
1653         isl_seq_clr(bmap->eq[i]+1, total);
1654         ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
1655         isl_vec_free(bmap->sample);
1656         bmap->sample = NULL;
1657         return isl_basic_map_finalize(bmap);
1658 error:
1659         isl_basic_map_free(bmap);
1660         return NULL;
1661 }
1662
1663 struct isl_basic_set *isl_basic_set_set_to_empty(struct isl_basic_set *bset)
1664 {
1665         return (struct isl_basic_set *)
1666                 isl_basic_map_set_to_empty((struct isl_basic_map *)bset);
1667 }
1668
1669 void isl_basic_map_swap_div(struct isl_basic_map *bmap, int a, int b)
1670 {
1671         int i;
1672         unsigned off = isl_space_dim(bmap->dim, isl_dim_all);
1673         isl_int *t = bmap->div[a];
1674         bmap->div[a] = bmap->div[b];
1675         bmap->div[b] = t;
1676
1677         for (i = 0; i < bmap->n_eq; ++i)
1678                 isl_int_swap(bmap->eq[i][1+off+a], bmap->eq[i][1+off+b]);
1679
1680         for (i = 0; i < bmap->n_ineq; ++i)
1681                 isl_int_swap(bmap->ineq[i][1+off+a], bmap->ineq[i][1+off+b]);
1682
1683         for (i = 0; i < bmap->n_div; ++i)
1684                 isl_int_swap(bmap->div[i][1+1+off+a], bmap->div[i][1+1+off+b]);
1685         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
1686 }
1687
1688 /* Eliminate the specified n dimensions starting at first from the
1689  * constraints, without removing the dimensions from the space.
1690  * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1691  */
1692 __isl_give isl_map *isl_map_eliminate(__isl_take isl_map *map,
1693         enum isl_dim_type type, unsigned first, unsigned n)
1694 {
1695         int i;
1696
1697         if (!map)
1698                 return NULL;
1699         if (n == 0)
1700                 return map;
1701
1702         if (first + n > isl_map_dim(map, type) || first + n < first)
1703                 isl_die(map->ctx, isl_error_invalid,
1704                         "index out of bounds", goto error);
1705
1706         map = isl_map_cow(map);
1707         if (!map)
1708                 return NULL;
1709
1710         for (i = 0; i < map->n; ++i) {
1711                 map->p[i] = isl_basic_map_eliminate(map->p[i], type, first, n);
1712                 if (!map->p[i])
1713                         goto error;
1714         }
1715         return map;
1716 error:
1717         isl_map_free(map);
1718         return NULL;
1719 }
1720
1721 /* Eliminate the specified n dimensions starting at first from the
1722  * constraints, without removing the dimensions from the space.
1723  * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1724  */
1725 __isl_give isl_set *isl_set_eliminate(__isl_take isl_set *set,
1726         enum isl_dim_type type, unsigned first, unsigned n)
1727 {
1728         return (isl_set *)isl_map_eliminate((isl_map *)set, type, first, n);
1729 }
1730
1731 /* Eliminate the specified n dimensions starting at first from the
1732  * constraints, without removing the dimensions from the space.
1733  * If the set is rational, the dimensions are eliminated using Fourier-Motzkin.
1734  */
1735 __isl_give isl_set *isl_set_eliminate_dims(__isl_take isl_set *set,
1736         unsigned first, unsigned n)
1737 {
1738         return isl_set_eliminate(set, isl_dim_set, first, n);
1739 }
1740
1741 __isl_give isl_basic_map *isl_basic_map_remove_divs(
1742         __isl_take isl_basic_map *bmap)
1743 {
1744         if (!bmap)
1745                 return NULL;
1746         bmap = isl_basic_map_eliminate_vars(bmap,
1747                             isl_space_dim(bmap->dim, isl_dim_all), bmap->n_div);
1748         if (!bmap)
1749                 return NULL;
1750         bmap->n_div = 0;
1751         return isl_basic_map_finalize(bmap);
1752 }
1753
1754 __isl_give isl_basic_set *isl_basic_set_remove_divs(
1755         __isl_take isl_basic_set *bset)
1756 {
1757         return (struct isl_basic_set *)isl_basic_map_remove_divs(
1758                         (struct isl_basic_map *)bset);
1759 }
1760
1761 __isl_give isl_map *isl_map_remove_divs(__isl_take isl_map *map)
1762 {
1763         int i;
1764
1765         if (!map)
1766                 return NULL;
1767         if (map->n == 0)
1768                 return map;
1769
1770         map = isl_map_cow(map);
1771         if (!map)
1772                 return NULL;
1773         
1774         for (i = 0; i < map->n; ++i) {
1775                 map->p[i] = isl_basic_map_remove_divs(map->p[i]);
1776                 if (!map->p[i])
1777                         goto error;
1778         }
1779         return map;
1780 error:
1781         isl_map_free(map);
1782         return NULL;
1783 }
1784
1785 __isl_give isl_set *isl_set_remove_divs(__isl_take isl_set *set)
1786 {
1787         return isl_map_remove_divs(set);
1788 }
1789
1790 struct isl_basic_map *isl_basic_map_remove_dims(struct isl_basic_map *bmap,
1791         enum isl_dim_type type, unsigned first, unsigned n)
1792 {
1793         if (!bmap)
1794                 return NULL;
1795         isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
1796                         goto error);
1797         if (n == 0 && !isl_space_is_named_or_nested(bmap->dim, type))
1798                 return bmap;
1799         bmap = isl_basic_map_eliminate_vars(bmap,
1800                         isl_basic_map_offset(bmap, type) - 1 + first, n);
1801         if (!bmap)
1802                 return bmap;
1803         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY) && type == isl_dim_div)
1804                 return bmap;
1805         bmap = isl_basic_map_drop(bmap, type, first, n);
1806         return bmap;
1807 error:
1808         isl_basic_map_free(bmap);
1809         return NULL;
1810 }
1811
1812 /* Return true if the definition of the given div (recursively) involves
1813  * any of the given variables.
1814  */
1815 static int div_involves_vars(__isl_keep isl_basic_map *bmap, int div,
1816         unsigned first, unsigned n)
1817 {
1818         int i;
1819         unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
1820
1821         if (isl_int_is_zero(bmap->div[div][0]))
1822                 return 0;
1823         if (isl_seq_first_non_zero(bmap->div[div] + 1 + first, n) >= 0)
1824                 return 1;
1825
1826         for (i = bmap->n_div - 1; i >= 0; --i) {
1827                 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
1828                         continue;
1829                 if (div_involves_vars(bmap, i, first, n))
1830                         return 1;
1831         }
1832
1833         return 0;
1834 }
1835
1836 /* Try and add a lower and/or upper bound on "div" to "bmap"
1837  * based on inequality "i".
1838  * "total" is the total number of variables (excluding the divs).
1839  * "v" is a temporary object that can be used during the calculations.
1840  * If "lb" is set, then a lower bound should be constructed.
1841  * If "ub" is set, then an upper bound should be constructed.
1842  *
1843  * The calling function has already checked that the inequality does not
1844  * reference "div", but we still need to check that the inequality is
1845  * of the right form.  We'll consider the case where we want to construct
1846  * a lower bound.  The construction of upper bounds is similar.
1847  *
1848  * Let "div" be of the form
1849  *
1850  *      q = floor((a + f(x))/d)
1851  *
1852  * We essentially check if constraint "i" is of the form
1853  *
1854  *      b + f(x) >= 0
1855  *
1856  * so that we can use it to derive a lower bound on "div".
1857  * However, we allow a slightly more general form
1858  *
1859  *      b + g(x) >= 0
1860  *
1861  * with the condition that the coefficients of g(x) - f(x) are all
1862  * divisible by d.
1863  * Rewriting this constraint as
1864  *
1865  *      0 >= -b - g(x)
1866  *
1867  * adding a + f(x) to both sides and dividing by d, we obtain
1868  *
1869  *      (a + f(x))/d >= (a-b)/d + (f(x)-g(x))/d
1870  *
1871  * Taking the floor on both sides, we obtain
1872  *
1873  *      q >= floor((a-b)/d) + (f(x)-g(x))/d
1874  *
1875  * or
1876  *
1877  *      (g(x)-f(x))/d + ceil((b-a)/d) + q >= 0
1878  *
1879  * In the case of an upper bound, we construct the constraint
1880  *
1881  *      (g(x)+f(x))/d + floor((b+a)/d) - q >= 0
1882  *
1883  */
1884 static __isl_give isl_basic_map *insert_bounds_on_div_from_ineq(
1885         __isl_take isl_basic_map *bmap, int div, int i,
1886         unsigned total, isl_int v, int lb, int ub)
1887 {
1888         int j;
1889
1890         for (j = 0; (lb || ub) && j < total + bmap->n_div; ++j) {
1891                 if (lb) {
1892                         isl_int_sub(v, bmap->ineq[i][1 + j],
1893                                         bmap->div[div][1 + 1 + j]);
1894                         lb = isl_int_is_divisible_by(v, bmap->div[div][0]);
1895                 }
1896                 if (ub) {
1897                         isl_int_add(v, bmap->ineq[i][1 + j],
1898                                         bmap->div[div][1 + 1 + j]);
1899                         ub = isl_int_is_divisible_by(v, bmap->div[div][0]);
1900                 }
1901         }
1902         if (!lb && !ub)
1903                 return bmap;
1904
1905         bmap = isl_basic_map_extend_constraints(bmap, 0, lb + ub);
1906         if (lb) {
1907                 int k = isl_basic_map_alloc_inequality(bmap);
1908                 if (k < 0)
1909                         goto error;
1910                 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1911                         isl_int_sub(bmap->ineq[k][j], bmap->ineq[i][j],
1912                                         bmap->div[div][1 + j]);
1913                         isl_int_cdiv_q(bmap->ineq[k][j],
1914                                         bmap->ineq[k][j], bmap->div[div][0]);
1915                 }
1916                 isl_int_set_si(bmap->ineq[k][1 + total + div], 1);
1917         }
1918         if (ub) {
1919                 int k = isl_basic_map_alloc_inequality(bmap);
1920                 if (k < 0)
1921                         goto error;
1922                 for (j = 0; j < 1 + total + bmap->n_div; ++j) {
1923                         isl_int_add(bmap->ineq[k][j], bmap->ineq[i][j],
1924                                         bmap->div[div][1 + j]);
1925                         isl_int_fdiv_q(bmap->ineq[k][j],
1926                                         bmap->ineq[k][j], bmap->div[div][0]);
1927                 }
1928                 isl_int_set_si(bmap->ineq[k][1 + total + div], -1);
1929         }
1930
1931         return bmap;
1932 error:
1933         isl_basic_map_free(bmap);
1934         return NULL;
1935 }
1936
1937 /* This function is called right before "div" is eliminated from "bmap"
1938  * using Fourier-Motzkin.
1939  * Look through the constraints of "bmap" for constraints on the argument
1940  * of the integer division and use them to construct constraints on the
1941  * integer division itself.  These constraints can then be combined
1942  * during the Fourier-Motzkin elimination.
1943  * Note that it is only useful to introduce lower bounds on "div"
1944  * if "bmap" already contains upper bounds on "div" as the newly
1945  * introduce lower bounds can then be combined with the pre-existing
1946  * upper bounds.  Similarly for upper bounds.
1947  * We therefore first check if "bmap" contains any lower and/or upper bounds
1948  * on "div".
1949  *
1950  * It is interesting to note that the introduction of these constraints
1951  * can indeed lead to more accurate results, even when compared to
1952  * deriving constraints on the argument of "div" from constraints on "div".
1953  * Consider, for example, the set
1954  *
1955  *      { [i,j,k] : 3 + i + 2j >= 0 and 2 * [(i+2j)/4] <= k }
1956  *
1957  * The second constraint can be rewritten as
1958  *
1959  *      2 * [(-i-2j+3)/4] + k >= 0
1960  *
1961  * from which we can derive
1962  *
1963  *      -i - 2j + 3 >= -2k
1964  *
1965  * or
1966  *
1967  *      i + 2j <= 3 + 2k
1968  *
1969  * Combined with the first constraint, we obtain
1970  *
1971  *      -3 <= 3 + 2k    or      k >= -3
1972  *
1973  * If, on the other hand we derive a constraint on [(i+2j)/4] from
1974  * the first constraint, we obtain
1975  *
1976  *      [(i + 2j)/4] >= [-3/4] = -1
1977  *
1978  * Combining this constraint with the second constraint, we obtain
1979  *
1980  *      k >= -2
1981  */
1982 static __isl_give isl_basic_map *insert_bounds_on_div(
1983         __isl_take isl_basic_map *bmap, int div)
1984 {
1985         int i;
1986         int check_lb, check_ub;
1987         isl_int v;
1988         unsigned total;
1989
1990         if (!bmap)
1991                 return NULL;
1992
1993         if (isl_int_is_zero(bmap->div[div][0]))
1994                 return bmap;
1995
1996         total = isl_space_dim(bmap->dim, isl_dim_all);
1997
1998         check_lb = 0;
1999         check_ub = 0;
2000         for (i = 0; (!check_lb || !check_ub) && i < bmap->n_ineq; ++i) {
2001                 int s = isl_int_sgn(bmap->ineq[i][1 + total + div]);
2002                 if (s > 0)
2003                         check_ub = 1;
2004                 if (s < 0)
2005                         check_lb = 1;
2006         }
2007
2008         if (!check_lb && !check_ub)
2009                 return bmap;
2010
2011         isl_int_init(v);
2012
2013         for (i = 0; bmap && i < bmap->n_ineq; ++i) {
2014                 if (!isl_int_is_zero(bmap->ineq[i][1 + total + div]))
2015                         continue;
2016
2017                 bmap = insert_bounds_on_div_from_ineq(bmap, div, i, total, v,
2018                                                         check_lb, check_ub);
2019         }
2020
2021         isl_int_clear(v);
2022
2023         return bmap;
2024 }
2025
2026 /* Remove all divs (recursively) involving any of the given dimensions
2027  * in their definitions.
2028  */
2029 __isl_give isl_basic_map *isl_basic_map_remove_divs_involving_dims(
2030         __isl_take isl_basic_map *bmap,
2031         enum isl_dim_type type, unsigned first, unsigned n)
2032 {
2033         int i;
2034
2035         if (!bmap)
2036                 return NULL;
2037         isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
2038                         goto error);
2039         first += isl_basic_map_offset(bmap, type);
2040
2041         for (i = bmap->n_div - 1; i >= 0; --i) {
2042                 if (!div_involves_vars(bmap, i, first, n))
2043                         continue;
2044                 bmap = insert_bounds_on_div(bmap, i);
2045                 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2046                 if (!bmap)
2047                         return NULL;
2048                 i = bmap->n_div;
2049         }
2050
2051         return bmap;
2052 error:
2053         isl_basic_map_free(bmap);
2054         return NULL;
2055 }
2056
2057 __isl_give isl_basic_set *isl_basic_set_remove_divs_involving_dims(
2058         __isl_take isl_basic_set *bset,
2059         enum isl_dim_type type, unsigned first, unsigned n)
2060 {
2061         return isl_basic_map_remove_divs_involving_dims(bset, type, first, n);
2062 }
2063
2064 __isl_give isl_map *isl_map_remove_divs_involving_dims(__isl_take isl_map *map,
2065         enum isl_dim_type type, unsigned first, unsigned n)
2066 {
2067         int i;
2068
2069         if (!map)
2070                 return NULL;
2071         if (map->n == 0)
2072                 return map;
2073
2074         map = isl_map_cow(map);
2075         if (!map)
2076                 return NULL;
2077
2078         for (i = 0; i < map->n; ++i) {
2079                 map->p[i] = isl_basic_map_remove_divs_involving_dims(map->p[i],
2080                                                                 type, first, n);
2081                 if (!map->p[i])
2082                         goto error;
2083         }
2084         return map;
2085 error:
2086         isl_map_free(map);
2087         return NULL;
2088 }
2089
2090 __isl_give isl_set *isl_set_remove_divs_involving_dims(__isl_take isl_set *set,
2091         enum isl_dim_type type, unsigned first, unsigned n)
2092 {
2093         return (isl_set *)isl_map_remove_divs_involving_dims((isl_map *)set,
2094                                                               type, first, n);
2095 }
2096
2097 /* Does the desciption of "bmap" depend on the specified dimensions?
2098  * We also check whether the dimensions appear in any of the div definitions.
2099  * In principle there is no need for this check.  If the dimensions appear
2100  * in a div definition, they also appear in the defining constraints of that
2101  * div.
2102  */
2103 int isl_basic_map_involves_dims(__isl_keep isl_basic_map *bmap,
2104         enum isl_dim_type type, unsigned first, unsigned n)
2105 {
2106         int i;
2107
2108         if (!bmap)
2109                 return -1;
2110
2111         if (first + n > isl_basic_map_dim(bmap, type))
2112                 isl_die(bmap->ctx, isl_error_invalid,
2113                         "index out of bounds", return -1);
2114
2115         first += isl_basic_map_offset(bmap, type);
2116         for (i = 0; i < bmap->n_eq; ++i)
2117                 if (isl_seq_first_non_zero(bmap->eq[i] + first, n) >= 0)
2118                         return 1;
2119         for (i = 0; i < bmap->n_ineq; ++i)
2120                 if (isl_seq_first_non_zero(bmap->ineq[i] + first, n) >= 0)
2121                         return 1;
2122         for (i = 0; i < bmap->n_div; ++i) {
2123                 if (isl_int_is_zero(bmap->div[i][0]))
2124                         continue;
2125                 if (isl_seq_first_non_zero(bmap->div[i] + 1 + first, n) >= 0)
2126                         return 1;
2127         }
2128
2129         return 0;
2130 }
2131
2132 int isl_map_involves_dims(__isl_keep isl_map *map,
2133         enum isl_dim_type type, unsigned first, unsigned n)
2134 {
2135         int i;
2136
2137         if (!map)
2138                 return -1;
2139
2140         if (first + n > isl_map_dim(map, type))
2141                 isl_die(map->ctx, isl_error_invalid,
2142                         "index out of bounds", return -1);
2143
2144         for (i = 0; i < map->n; ++i) {
2145                 int involves = isl_basic_map_involves_dims(map->p[i],
2146                                                             type, first, n);
2147                 if (involves < 0 || involves)
2148                         return involves;
2149         }
2150
2151         return 0;
2152 }
2153
2154 int isl_basic_set_involves_dims(__isl_keep isl_basic_set *bset,
2155         enum isl_dim_type type, unsigned first, unsigned n)
2156 {
2157         return isl_basic_map_involves_dims(bset, type, first, n);
2158 }
2159
2160 int isl_set_involves_dims(__isl_keep isl_set *set,
2161         enum isl_dim_type type, unsigned first, unsigned n)
2162 {
2163         return isl_map_involves_dims(set, type, first, n);
2164 }
2165
2166 /* Return true if the definition of the given div is unknown or depends
2167  * on unknown divs.
2168  */
2169 static int div_is_unknown(__isl_keep isl_basic_map *bmap, int div)
2170 {
2171         int i;
2172         unsigned div_offset = isl_basic_map_offset(bmap, isl_dim_div);
2173
2174         if (isl_int_is_zero(bmap->div[div][0]))
2175                 return 1;
2176
2177         for (i = bmap->n_div - 1; i >= 0; --i) {
2178                 if (isl_int_is_zero(bmap->div[div][1 + div_offset + i]))
2179                         continue;
2180                 if (div_is_unknown(bmap, i))
2181                         return 1;
2182         }
2183
2184         return 0;
2185 }
2186
2187 /* Remove all divs that are unknown or defined in terms of unknown divs.
2188  */
2189 __isl_give isl_basic_map *isl_basic_map_remove_unknown_divs(
2190         __isl_take isl_basic_map *bmap)
2191 {
2192         int i;
2193
2194         if (!bmap)
2195                 return NULL;
2196
2197         for (i = bmap->n_div - 1; i >= 0; --i) {
2198                 if (!div_is_unknown(bmap, i))
2199                         continue;
2200                 bmap = isl_basic_map_remove_dims(bmap, isl_dim_div, i, 1);
2201                 if (!bmap)
2202                         return NULL;
2203                 i = bmap->n_div;
2204         }
2205
2206         return bmap;
2207 }
2208
2209 /* Remove all divs that are unknown or defined in terms of unknown divs.
2210  */
2211 __isl_give isl_basic_set *isl_basic_set_remove_unknown_divs(
2212         __isl_take isl_basic_set *bset)
2213 {
2214         return isl_basic_map_remove_unknown_divs(bset);
2215 }
2216
2217 __isl_give isl_map *isl_map_remove_unknown_divs(__isl_take isl_map *map)
2218 {
2219         int i;
2220
2221         if (!map)
2222                 return NULL;
2223         if (map->n == 0)
2224                 return map;
2225
2226         map = isl_map_cow(map);
2227         if (!map)
2228                 return NULL;
2229
2230         for (i = 0; i < map->n; ++i) {
2231                 map->p[i] = isl_basic_map_remove_unknown_divs(map->p[i]);
2232                 if (!map->p[i])
2233                         goto error;
2234         }
2235         return map;
2236 error:
2237         isl_map_free(map);
2238         return NULL;
2239 }
2240
2241 __isl_give isl_set *isl_set_remove_unknown_divs(__isl_take isl_set *set)
2242 {
2243         return (isl_set *)isl_map_remove_unknown_divs((isl_map *)set);
2244 }
2245
2246 __isl_give isl_basic_set *isl_basic_set_remove_dims(
2247         __isl_take isl_basic_set *bset,
2248         enum isl_dim_type type, unsigned first, unsigned n)
2249 {
2250         return (isl_basic_set *)
2251             isl_basic_map_remove_dims((isl_basic_map *)bset, type, first, n);
2252 }
2253
2254 struct isl_map *isl_map_remove_dims(struct isl_map *map,
2255         enum isl_dim_type type, unsigned first, unsigned n)
2256 {
2257         int i;
2258
2259         if (n == 0)
2260                 return map;
2261
2262         map = isl_map_cow(map);
2263         if (!map)
2264                 return NULL;
2265         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
2266         
2267         for (i = 0; i < map->n; ++i) {
2268                 map->p[i] = isl_basic_map_eliminate_vars(map->p[i],
2269                         isl_basic_map_offset(map->p[i], type) - 1 + first, n);
2270                 if (!map->p[i])
2271                         goto error;
2272         }
2273         map = isl_map_drop(map, type, first, n);
2274         return map;
2275 error:
2276         isl_map_free(map);
2277         return NULL;
2278 }
2279
2280 __isl_give isl_set *isl_set_remove_dims(__isl_take isl_set *bset,
2281         enum isl_dim_type type, unsigned first, unsigned n)
2282 {
2283         return (isl_set *)isl_map_remove_dims((isl_map *)bset, type, first, n);
2284 }
2285
2286 /* Project out n inputs starting at first using Fourier-Motzkin */
2287 struct isl_map *isl_map_remove_inputs(struct isl_map *map,
2288         unsigned first, unsigned n)
2289 {
2290         return isl_map_remove_dims(map, isl_dim_in, first, n);
2291 }
2292
2293 static void dump_term(struct isl_basic_map *bmap,
2294                         isl_int c, int pos, FILE *out)
2295 {
2296         const char *name;
2297         unsigned in = isl_basic_map_n_in(bmap);
2298         unsigned dim = in + isl_basic_map_n_out(bmap);
2299         unsigned nparam = isl_basic_map_n_param(bmap);
2300         if (!pos)
2301                 isl_int_print(out, c, 0);
2302         else {
2303                 if (!isl_int_is_one(c))
2304                         isl_int_print(out, c, 0);
2305                 if (pos < 1 + nparam) {
2306                         name = isl_space_get_dim_name(bmap->dim,
2307                                                 isl_dim_param, pos - 1);
2308                         if (name)
2309                                 fprintf(out, "%s", name);
2310                         else
2311                                 fprintf(out, "p%d", pos - 1);
2312                 } else if (pos < 1 + nparam + in)
2313                         fprintf(out, "i%d", pos - 1 - nparam);
2314                 else if (pos < 1 + nparam + dim)
2315                         fprintf(out, "o%d", pos - 1 - nparam - in);
2316                 else
2317                         fprintf(out, "e%d", pos - 1 - nparam - dim);
2318         }
2319 }
2320
2321 static void dump_constraint_sign(struct isl_basic_map *bmap, isl_int *c,
2322                                 int sign, FILE *out)
2323 {
2324         int i;
2325         int first;
2326         unsigned len = 1 + isl_basic_map_total_dim(bmap);
2327         isl_int v;
2328
2329         isl_int_init(v);
2330         for (i = 0, first = 1; i < len; ++i) {
2331                 if (isl_int_sgn(c[i]) * sign <= 0)
2332                         continue;
2333                 if (!first)
2334                         fprintf(out, " + ");
2335                 first = 0;
2336                 isl_int_abs(v, c[i]);
2337                 dump_term(bmap, v, i, out);
2338         }
2339         isl_int_clear(v);
2340         if (first)
2341                 fprintf(out, "0");
2342 }
2343
2344 static void dump_constraint(struct isl_basic_map *bmap, isl_int *c,
2345                                 const char *op, FILE *out, int indent)
2346 {
2347         int i;
2348
2349         fprintf(out, "%*s", indent, "");
2350
2351         dump_constraint_sign(bmap, c, 1, out);
2352         fprintf(out, " %s ", op);
2353         dump_constraint_sign(bmap, c, -1, out);
2354
2355         fprintf(out, "\n");
2356
2357         for (i = bmap->n_div; i < bmap->extra; ++i) {
2358                 if (isl_int_is_zero(c[1+isl_space_dim(bmap->dim, isl_dim_all)+i]))
2359                         continue;
2360                 fprintf(out, "%*s", indent, "");
2361                 fprintf(out, "ERROR: unused div coefficient not zero\n");
2362                 abort();
2363         }
2364 }
2365
2366 static void dump_constraints(struct isl_basic_map *bmap,
2367                                 isl_int **c, unsigned n,
2368                                 const char *op, FILE *out, int indent)
2369 {
2370         int i;
2371
2372         for (i = 0; i < n; ++i)
2373                 dump_constraint(bmap, c[i], op, out, indent);
2374 }
2375
2376 static void dump_affine(struct isl_basic_map *bmap, isl_int *exp, FILE *out)
2377 {
2378         int j;
2379         int first = 1;
2380         unsigned total = isl_basic_map_total_dim(bmap);
2381
2382         for (j = 0; j < 1 + total; ++j) {
2383                 if (isl_int_is_zero(exp[j]))
2384                         continue;
2385                 if (!first && isl_int_is_pos(exp[j]))
2386                         fprintf(out, "+");
2387                 dump_term(bmap, exp[j], j, out);
2388                 first = 0;
2389         }
2390 }
2391
2392 static void dump(struct isl_basic_map *bmap, FILE *out, int indent)
2393 {
2394         int i;
2395
2396         dump_constraints(bmap, bmap->eq, bmap->n_eq, "=", out, indent);
2397         dump_constraints(bmap, bmap->ineq, bmap->n_ineq, ">=", out, indent);
2398
2399         for (i = 0; i < bmap->n_div; ++i) {
2400                 fprintf(out, "%*s", indent, "");
2401                 fprintf(out, "e%d = [(", i);
2402                 dump_affine(bmap, bmap->div[i]+1, out);
2403                 fprintf(out, ")/");
2404                 isl_int_print(out, bmap->div[i][0], 0);
2405                 fprintf(out, "]\n");
2406         }
2407 }
2408
2409 void isl_basic_set_print_internal(struct isl_basic_set *bset,
2410         FILE *out, int indent)
2411 {
2412         if (!bset) {
2413                 fprintf(out, "null basic set\n");
2414                 return;
2415         }
2416
2417         fprintf(out, "%*s", indent, "");
2418         fprintf(out, "ref: %d, nparam: %d, dim: %d, extra: %d, flags: %x\n",
2419                         bset->ref, bset->dim->nparam, bset->dim->n_out,
2420                         bset->extra, bset->flags);
2421         dump((struct isl_basic_map *)bset, out, indent);
2422 }
2423
2424 void isl_basic_map_print_internal(struct isl_basic_map *bmap,
2425         FILE *out, int indent)
2426 {
2427         if (!bmap) {
2428                 fprintf(out, "null basic map\n");
2429                 return;
2430         }
2431
2432         fprintf(out, "%*s", indent, "");
2433         fprintf(out, "ref: %d, nparam: %d, in: %d, out: %d, extra: %d, "
2434                         "flags: %x, n_name: %d\n",
2435                 bmap->ref,
2436                 bmap->dim->nparam, bmap->dim->n_in, bmap->dim->n_out,
2437                 bmap->extra, bmap->flags, bmap->dim->n_id);
2438         dump(bmap, out, indent);
2439 }
2440
2441 int isl_inequality_negate(struct isl_basic_map *bmap, unsigned pos)
2442 {
2443         unsigned total;
2444         if (!bmap)
2445                 return -1;
2446         total = isl_basic_map_total_dim(bmap);
2447         isl_assert(bmap->ctx, pos < bmap->n_ineq, return -1);
2448         isl_seq_neg(bmap->ineq[pos], bmap->ineq[pos], 1 + total);
2449         isl_int_sub_ui(bmap->ineq[pos][0], bmap->ineq[pos][0], 1);
2450         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
2451         return 0;
2452 }
2453
2454 __isl_give isl_set *isl_set_alloc_space(__isl_take isl_space *dim, int n,
2455         unsigned flags)
2456 {
2457         struct isl_set *set;
2458
2459         if (!dim)
2460                 return NULL;
2461         isl_assert(dim->ctx, dim->n_in == 0, goto error);
2462         isl_assert(dim->ctx, n >= 0, goto error);
2463         set = isl_alloc(dim->ctx, struct isl_set,
2464                         sizeof(struct isl_set) +
2465                         (n - 1) * sizeof(struct isl_basic_set *));
2466         if (!set)
2467                 goto error;
2468
2469         set->ctx = dim->ctx;
2470         isl_ctx_ref(set->ctx);
2471         set->ref = 1;
2472         set->size = n;
2473         set->n = 0;
2474         set->dim = dim;
2475         set->flags = flags;
2476         return set;
2477 error:
2478         isl_space_free(dim);
2479         return NULL;
2480 }
2481
2482 struct isl_set *isl_set_alloc(struct isl_ctx *ctx,
2483                 unsigned nparam, unsigned dim, int n, unsigned flags)
2484 {
2485         struct isl_set *set;
2486         isl_space *dims;
2487
2488         dims = isl_space_alloc(ctx, nparam, 0, dim);
2489         if (!dims)
2490                 return NULL;
2491
2492         set = isl_set_alloc_space(dims, n, flags);
2493         return set;
2494 }
2495
2496 /* Make sure "map" has room for at least "n" more basic maps.
2497  */
2498 struct isl_map *isl_map_grow(struct isl_map *map, int n)
2499 {
2500         int i;
2501         struct isl_map *grown = NULL;
2502
2503         if (!map)
2504                 return NULL;
2505         isl_assert(map->ctx, n >= 0, goto error);
2506         if (map->n + n <= map->size)
2507                 return map;
2508         grown = isl_map_alloc_space(isl_map_get_space(map), map->n + n, map->flags);
2509         if (!grown)
2510                 goto error;
2511         for (i = 0; i < map->n; ++i) {
2512                 grown->p[i] = isl_basic_map_copy(map->p[i]);
2513                 if (!grown->p[i])
2514                         goto error;
2515                 grown->n++;
2516         }
2517         isl_map_free(map);
2518         return grown;
2519 error:
2520         isl_map_free(grown);
2521         isl_map_free(map);
2522         return NULL;
2523 }
2524
2525 /* Make sure "set" has room for at least "n" more basic sets.
2526  */
2527 struct isl_set *isl_set_grow(struct isl_set *set, int n)
2528 {
2529         return (struct isl_set *)isl_map_grow((struct isl_map *)set, n);
2530 }
2531
2532 struct isl_set *isl_set_dup(struct isl_set *set)
2533 {
2534         int i;
2535         struct isl_set *dup;
2536
2537         if (!set)
2538                 return NULL;
2539
2540         dup = isl_set_alloc_space(isl_space_copy(set->dim), set->n, set->flags);
2541         if (!dup)
2542                 return NULL;
2543         for (i = 0; i < set->n; ++i)
2544                 dup = isl_set_add_basic_set(dup, isl_basic_set_copy(set->p[i]));
2545         return dup;
2546 }
2547
2548 struct isl_set *isl_set_from_basic_set(struct isl_basic_set *bset)
2549 {
2550         return isl_map_from_basic_map(bset);
2551 }
2552
2553 struct isl_map *isl_map_from_basic_map(struct isl_basic_map *bmap)
2554 {
2555         struct isl_map *map;
2556
2557         if (!bmap)
2558                 return NULL;
2559
2560         map = isl_map_alloc_space(isl_space_copy(bmap->dim), 1, ISL_MAP_DISJOINT);
2561         return isl_map_add_basic_map(map, bmap);
2562 }
2563
2564 __isl_give isl_set *isl_set_add_basic_set(__isl_take isl_set *set,
2565                                                 __isl_take isl_basic_set *bset)
2566 {
2567         return (struct isl_set *)isl_map_add_basic_map((struct isl_map *)set,
2568                                                 (struct isl_basic_map *)bset);
2569 }
2570
2571 void *isl_set_free(__isl_take isl_set *set)
2572 {
2573         int i;
2574
2575         if (!set)
2576                 return NULL;
2577
2578         if (--set->ref > 0)
2579                 return NULL;
2580
2581         isl_ctx_deref(set->ctx);
2582         for (i = 0; i < set->n; ++i)
2583                 isl_basic_set_free(set->p[i]);
2584         isl_space_free(set->dim);
2585         free(set);
2586
2587         return NULL;
2588 }
2589
2590 void isl_set_print_internal(struct isl_set *set, FILE *out, int indent)
2591 {
2592         int i;
2593
2594         if (!set) {
2595                 fprintf(out, "null set\n");
2596                 return;
2597         }
2598
2599         fprintf(out, "%*s", indent, "");
2600         fprintf(out, "ref: %d, n: %d, nparam: %d, dim: %d, flags: %x\n",
2601                         set->ref, set->n, set->dim->nparam, set->dim->n_out,
2602                         set->flags);
2603         for (i = 0; i < set->n; ++i) {
2604                 fprintf(out, "%*s", indent, "");
2605                 fprintf(out, "basic set %d:\n", i);
2606                 isl_basic_set_print_internal(set->p[i], out, indent+4);
2607         }
2608 }
2609
2610 void isl_map_print_internal(struct isl_map *map, FILE *out, int indent)
2611 {
2612         int i;
2613
2614         if (!map) {
2615                 fprintf(out, "null map\n");
2616                 return;
2617         }
2618
2619         fprintf(out, "%*s", indent, "");
2620         fprintf(out, "ref: %d, n: %d, nparam: %d, in: %d, out: %d, "
2621                      "flags: %x, n_name: %d\n",
2622                         map->ref, map->n, map->dim->nparam, map->dim->n_in,
2623                         map->dim->n_out, map->flags, map->dim->n_id);
2624         for (i = 0; i < map->n; ++i) {
2625                 fprintf(out, "%*s", indent, "");
2626                 fprintf(out, "basic map %d:\n", i);
2627                 isl_basic_map_print_internal(map->p[i], out, indent+4);
2628         }
2629 }
2630
2631 struct isl_basic_map *isl_basic_map_intersect_domain(
2632                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2633 {
2634         struct isl_basic_map *bmap_domain;
2635
2636         if (!bmap || !bset)
2637                 goto error;
2638
2639         isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2640                                         bset->dim, isl_dim_param), goto error);
2641
2642         if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2643                 isl_assert(bset->ctx,
2644                     isl_basic_map_compatible_domain(bmap, bset), goto error);
2645
2646         bmap = isl_basic_map_cow(bmap);
2647         if (!bmap)
2648                 goto error;
2649         bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2650                         bset->n_div, bset->n_eq, bset->n_ineq);
2651         bmap_domain = isl_basic_map_from_domain(bset);
2652         bmap = add_constraints(bmap, bmap_domain, 0, 0);
2653
2654         bmap = isl_basic_map_simplify(bmap);
2655         return isl_basic_map_finalize(bmap);
2656 error:
2657         isl_basic_map_free(bmap);
2658         isl_basic_set_free(bset);
2659         return NULL;
2660 }
2661
2662 struct isl_basic_map *isl_basic_map_intersect_range(
2663                 struct isl_basic_map *bmap, struct isl_basic_set *bset)
2664 {
2665         struct isl_basic_map *bmap_range;
2666
2667         if (!bmap || !bset)
2668                 goto error;
2669
2670         isl_assert(bset->ctx, isl_space_match(bmap->dim, isl_dim_param,
2671                                         bset->dim, isl_dim_param), goto error);
2672
2673         if (isl_space_dim(bset->dim, isl_dim_set) != 0)
2674                 isl_assert(bset->ctx,
2675                     isl_basic_map_compatible_range(bmap, bset), goto error);
2676
2677         if (isl_basic_set_is_universe(bset)) {
2678                 isl_basic_set_free(bset);
2679                 return bmap;
2680         }
2681
2682         bmap = isl_basic_map_cow(bmap);
2683         if (!bmap)
2684                 goto error;
2685         bmap = isl_basic_map_extend_space(bmap, isl_space_copy(bmap->dim),
2686                         bset->n_div, bset->n_eq, bset->n_ineq);
2687         bmap_range = isl_basic_map_from_basic_set(bset, isl_space_copy(bset->dim));
2688         bmap = add_constraints(bmap, bmap_range, 0, 0);
2689
2690         bmap = isl_basic_map_simplify(bmap);
2691         return isl_basic_map_finalize(bmap);
2692 error:
2693         isl_basic_map_free(bmap);
2694         isl_basic_set_free(bset);
2695         return NULL;
2696 }
2697
2698 int isl_basic_map_contains(struct isl_basic_map *bmap, struct isl_vec *vec)
2699 {
2700         int i;
2701         unsigned total;
2702         isl_int s;
2703
2704         total = 1 + isl_basic_map_total_dim(bmap);
2705         if (total != vec->size)
2706                 return -1;
2707
2708         isl_int_init(s);
2709
2710         for (i = 0; i < bmap->n_eq; ++i) {
2711                 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2712                 if (!isl_int_is_zero(s)) {
2713                         isl_int_clear(s);
2714                         return 0;
2715                 }
2716         }
2717
2718         for (i = 0; i < bmap->n_ineq; ++i) {
2719                 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2720                 if (isl_int_is_neg(s)) {
2721                         isl_int_clear(s);
2722                         return 0;
2723                 }
2724         }
2725
2726         isl_int_clear(s);
2727
2728         return 1;
2729 }
2730
2731 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2732 {
2733         return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2734 }
2735
2736 struct isl_basic_map *isl_basic_map_intersect(
2737                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2738 {
2739         struct isl_vec *sample = NULL;
2740
2741         if (!bmap1 || !bmap2)
2742                 goto error;
2743
2744         isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2745                                      bmap2->dim, isl_dim_param), goto error);
2746         if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2747                                 isl_space_dim(bmap1->dim, isl_dim_param) &&
2748             isl_space_dim(bmap2->dim, isl_dim_all) !=
2749                                 isl_space_dim(bmap2->dim, isl_dim_param))
2750                 return isl_basic_map_intersect(bmap2, bmap1);
2751
2752         if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2753                                         isl_space_dim(bmap2->dim, isl_dim_param))
2754                 isl_assert(bmap1->ctx,
2755                             isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2756
2757         if (bmap1->sample &&
2758             isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2759             isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2760                 sample = isl_vec_copy(bmap1->sample);
2761         else if (bmap2->sample &&
2762             isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2763             isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2764                 sample = isl_vec_copy(bmap2->sample);
2765
2766         bmap1 = isl_basic_map_cow(bmap1);
2767         if (!bmap1)
2768                 goto error;
2769         bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2770                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2771         bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2772
2773         if (!bmap1)
2774                 isl_vec_free(sample);
2775         else if (sample) {
2776                 isl_vec_free(bmap1->sample);
2777                 bmap1->sample = sample;
2778         }
2779
2780         bmap1 = isl_basic_map_simplify(bmap1);
2781         return isl_basic_map_finalize(bmap1);
2782 error:
2783         if (sample)
2784                 isl_vec_free(sample);
2785         isl_basic_map_free(bmap1);
2786         isl_basic_map_free(bmap2);
2787         return NULL;
2788 }
2789
2790 struct isl_basic_set *isl_basic_set_intersect(
2791                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2792 {
2793         return (struct isl_basic_set *)
2794                 isl_basic_map_intersect(
2795                         (struct isl_basic_map *)bset1,
2796                         (struct isl_basic_map *)bset2);
2797 }
2798
2799 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2800         __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2801 {
2802         return isl_basic_set_intersect(bset1, bset2);
2803 }
2804
2805 /* Special case of isl_map_intersect, where both map1 and map2
2806  * are convex, without any divs and such that either map1 or map2
2807  * contains a single constraint.  This constraint is then simply
2808  * added to the other map.
2809  */
2810 static __isl_give isl_map *map_intersect_add_constraint(
2811         __isl_take isl_map *map1, __isl_take isl_map *map2)
2812 {
2813         isl_assert(map1->ctx, map1->n == 1, goto error);
2814         isl_assert(map2->ctx, map1->n == 1, goto error);
2815         isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2816         isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2817
2818         if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2819                 return isl_map_intersect(map2, map1);
2820
2821         isl_assert(map2->ctx,
2822                     map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2823
2824         map1 = isl_map_cow(map1);
2825         if (!map1)
2826                 goto error;
2827         if (isl_map_plain_is_empty(map1)) {
2828                 isl_map_free(map2);
2829                 return map1;
2830         }
2831         map1->p[0] = isl_basic_map_cow(map1->p[0]);
2832         if (map2->p[0]->n_eq == 1)
2833                 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2834         else
2835                 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2836                                                         map2->p[0]->ineq[0]);
2837
2838         map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2839         map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2840         if (!map1->p[0])
2841                 goto error;
2842
2843         if (isl_basic_map_plain_is_empty(map1->p[0])) {
2844                 isl_basic_map_free(map1->p[0]);
2845                 map1->n = 0;
2846         }
2847
2848         isl_map_free(map2);
2849
2850         return map1;
2851 error:
2852         isl_map_free(map1);
2853         isl_map_free(map2);
2854         return NULL;
2855 }
2856
2857 /* map2 may be either a parameter domain or a map living in the same
2858  * space as map1.
2859  */
2860 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2861         __isl_take isl_map *map2)
2862 {
2863         unsigned flags = 0;
2864         isl_map *result;
2865         int i, j;
2866
2867         if (!map1 || !map2)
2868                 goto error;
2869
2870         if ((isl_map_plain_is_empty(map1) ||
2871              isl_map_plain_is_universe(map2)) &&
2872             isl_space_is_equal(map1->dim, map2->dim)) {
2873                 isl_map_free(map2);
2874                 return map1;
2875         }
2876         if ((isl_map_plain_is_empty(map2) ||
2877              isl_map_plain_is_universe(map1)) &&
2878             isl_space_is_equal(map1->dim, map2->dim)) {
2879                 isl_map_free(map1);
2880                 return map2;
2881         }
2882
2883         if (map1->n == 1 && map2->n == 1 &&
2884             map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2885             isl_space_is_equal(map1->dim, map2->dim) &&
2886             (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2887              map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2888                 return map_intersect_add_constraint(map1, map2);
2889
2890         if (isl_space_dim(map2->dim, isl_dim_all) !=
2891                                 isl_space_dim(map2->dim, isl_dim_param))
2892                 isl_assert(map1->ctx,
2893                             isl_space_is_equal(map1->dim, map2->dim), goto error);
2894
2895         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2896             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2897                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2898
2899         result = isl_map_alloc_space(isl_space_copy(map1->dim),
2900                                 map1->n * map2->n, flags);
2901         if (!result)
2902                 goto error;
2903         for (i = 0; i < map1->n; ++i)
2904                 for (j = 0; j < map2->n; ++j) {
2905                         struct isl_basic_map *part;
2906                         part = isl_basic_map_intersect(
2907                                     isl_basic_map_copy(map1->p[i]),
2908                                     isl_basic_map_copy(map2->p[j]));
2909                         if (isl_basic_map_is_empty(part) < 0)
2910                                 part = isl_basic_map_free(part);
2911                         result = isl_map_add_basic_map(result, part);
2912                         if (!result)
2913                                 goto error;
2914                 }
2915         isl_map_free(map1);
2916         isl_map_free(map2);
2917         return result;
2918 error:
2919         isl_map_free(map1);
2920         isl_map_free(map2);
2921         return NULL;
2922 }
2923
2924 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2925         __isl_take isl_map *map2)
2926 {
2927         if (!map1 || !map2)
2928                 goto error;
2929         if (!isl_space_is_equal(map1->dim, map2->dim))
2930                 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2931                         "spaces don't match", goto error);
2932         return map_intersect_internal(map1, map2);
2933 error:
2934         isl_map_free(map1);
2935         isl_map_free(map2);
2936         return NULL;
2937 }
2938
2939 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2940         __isl_take isl_map *map2)
2941 {
2942         return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2943 }
2944
2945 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2946 {
2947         return (struct isl_set *)
2948                 isl_map_intersect((struct isl_map *)set1,
2949                                   (struct isl_map *)set2);
2950 }
2951
2952 /* map_intersect_internal accepts intersections
2953  * with parameter domains, so we can just call that function.
2954  */
2955 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2956                 __isl_take isl_set *params)
2957 {
2958         return map_intersect_internal(map, params);
2959 }
2960
2961 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2962         __isl_take isl_map *map2)
2963 {
2964         return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2965 }
2966
2967 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2968                 __isl_take isl_set *params)
2969 {
2970         return isl_map_intersect_params(set, params);
2971 }
2972
2973 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
2974 {
2975         isl_space *dim;
2976         struct isl_basic_set *bset;
2977         unsigned in;
2978
2979         if (!bmap)
2980                 return NULL;
2981         bmap = isl_basic_map_cow(bmap);
2982         if (!bmap)
2983                 return NULL;
2984         dim = isl_space_reverse(isl_space_copy(bmap->dim));
2985         in = isl_basic_map_n_in(bmap);
2986         bset = isl_basic_set_from_basic_map(bmap);
2987         bset = isl_basic_set_swap_vars(bset, in);
2988         return isl_basic_map_from_basic_set(bset, dim);
2989 }
2990
2991 static __isl_give isl_basic_map *basic_map_space_reset(
2992         __isl_take isl_basic_map *bmap, enum isl_dim_type type)
2993 {
2994         isl_space *space;
2995
2996         if (!bmap)
2997                 return NULL;
2998         if (!isl_space_is_named_or_nested(bmap->dim, type))
2999                 return bmap;
3000
3001         space = isl_basic_map_get_space(bmap);
3002         space = isl_space_reset(space, type);
3003         bmap = isl_basic_map_reset_space(bmap, space);
3004         return bmap;
3005 }
3006
3007 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3008         __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3009         unsigned pos, unsigned n)
3010 {
3011         isl_space *res_dim;
3012         struct isl_basic_map *res;
3013         struct isl_dim_map *dim_map;
3014         unsigned total, off;
3015         enum isl_dim_type t;
3016
3017         if (n == 0)
3018                 return basic_map_space_reset(bmap, type);
3019
3020         if (!bmap)
3021                 return NULL;
3022
3023         res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3024
3025         total = isl_basic_map_total_dim(bmap) + n;
3026         dim_map = isl_dim_map_alloc(bmap->ctx, total);
3027         off = 0;
3028         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3029                 if (t != type) {
3030                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
3031                 } else {
3032                         unsigned size = isl_basic_map_dim(bmap, t);
3033                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3034                                                 0, pos, off);
3035                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3036                                                 pos, size - pos, off + pos + n);
3037                 }
3038                 off += isl_space_dim(res_dim, t);
3039         }
3040         isl_dim_map_div(dim_map, bmap, off);
3041
3042         res = isl_basic_map_alloc_space(res_dim,
3043                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
3044         if (isl_basic_map_is_rational(bmap))
3045                 res = isl_basic_map_set_rational(res);
3046         if (isl_basic_map_plain_is_empty(bmap)) {
3047                 isl_basic_map_free(bmap);
3048                 return isl_basic_map_set_to_empty(res);
3049         }
3050         res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3051         return isl_basic_map_finalize(res);
3052 }
3053
3054 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3055         __isl_take isl_basic_set *bset,
3056         enum isl_dim_type type, unsigned pos, unsigned n)
3057 {
3058         return isl_basic_map_insert_dims(bset, type, pos, n);
3059 }
3060
3061 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3062                 enum isl_dim_type type, unsigned n)
3063 {
3064         if (!bmap)
3065                 return NULL;
3066         return isl_basic_map_insert_dims(bmap, type,
3067                                         isl_basic_map_dim(bmap, type), n);
3068 }
3069
3070 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3071                 enum isl_dim_type type, unsigned n)
3072 {
3073         if (!bset)
3074                 return NULL;
3075         isl_assert(bset->ctx, type != isl_dim_in, goto error);
3076         return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3077 error:
3078         isl_basic_set_free(bset);
3079         return NULL;
3080 }
3081
3082 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3083         enum isl_dim_type type)
3084 {
3085         isl_space *space;
3086
3087         if (!map || !isl_space_is_named_or_nested(map->dim, type))
3088                 return map;
3089
3090         space = isl_map_get_space(map);
3091         space = isl_space_reset(space, type);
3092         map = isl_map_reset_space(map, space);
3093         return map;
3094 }
3095
3096 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3097                 enum isl_dim_type type, unsigned pos, unsigned n)
3098 {
3099         int i;
3100
3101         if (n == 0)
3102                 return map_space_reset(map, type);
3103
3104         map = isl_map_cow(map);
3105         if (!map)
3106                 return NULL;
3107
3108         map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3109         if (!map->dim)
3110                 goto error;
3111
3112         for (i = 0; i < map->n; ++i) {
3113                 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3114                 if (!map->p[i])
3115                         goto error;
3116         }
3117
3118         return map;
3119 error:
3120         isl_map_free(map);
3121         return NULL;
3122 }
3123
3124 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3125                 enum isl_dim_type type, unsigned pos, unsigned n)
3126 {
3127         return isl_map_insert_dims(set, type, pos, n);
3128 }
3129
3130 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3131                 enum isl_dim_type type, unsigned n)
3132 {
3133         if (!map)
3134                 return NULL;
3135         return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3136 }
3137
3138 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3139                 enum isl_dim_type type, unsigned n)
3140 {
3141         if (!set)
3142                 return NULL;
3143         isl_assert(set->ctx, type != isl_dim_in, goto error);
3144         return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3145 error:
3146         isl_set_free(set);
3147         return NULL;
3148 }
3149
3150 __isl_give isl_basic_map *isl_basic_map_move_dims(
3151         __isl_take isl_basic_map *bmap,
3152         enum isl_dim_type dst_type, unsigned dst_pos,
3153         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3154 {
3155         struct isl_dim_map *dim_map;
3156         struct isl_basic_map *res;
3157         enum isl_dim_type t;
3158         unsigned total, off;
3159
3160         if (!bmap)
3161                 return NULL;
3162         if (n == 0)
3163                 return bmap;
3164
3165         isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3166                 goto error);
3167
3168         if (dst_type == src_type && dst_pos == src_pos)
3169                 return bmap;
3170
3171         isl_assert(bmap->ctx, dst_type != src_type, goto error);
3172
3173         if (pos(bmap->dim, dst_type) + dst_pos ==
3174             pos(bmap->dim, src_type) + src_pos +
3175                                             ((src_type < dst_type) ? n : 0)) {
3176                 bmap = isl_basic_map_cow(bmap);
3177                 if (!bmap)
3178                         return NULL;
3179
3180                 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3181                                                 src_type, src_pos, n);
3182                 if (!bmap->dim)
3183                         goto error;
3184
3185                 bmap = isl_basic_map_finalize(bmap);
3186
3187                 return bmap;
3188         }
3189
3190         total = isl_basic_map_total_dim(bmap);
3191         dim_map = isl_dim_map_alloc(bmap->ctx, total);
3192
3193         off = 0;
3194         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3195                 unsigned size = isl_space_dim(bmap->dim, t);
3196                 if (t == dst_type) {
3197                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3198                                             0, dst_pos, off);
3199                         off += dst_pos;
3200                         isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3201                                             src_pos, n, off);
3202                         off += n;
3203                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3204                                             dst_pos, size - dst_pos, off);
3205                         off += size - dst_pos;
3206                 } else if (t == src_type) {
3207                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3208                                             0, src_pos, off);
3209                         off += src_pos;
3210                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3211                                         src_pos + n, size - src_pos - n, off);
3212                         off += size - src_pos - n;
3213                 } else {
3214                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
3215                         off += size;
3216                 }
3217         }
3218         isl_dim_map_div(dim_map, bmap, off);
3219
3220         res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3221                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
3222         bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3223
3224         bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3225                                         src_type, src_pos, n);
3226         if (!bmap->dim)
3227                 goto error;
3228
3229         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3230         bmap = isl_basic_map_gauss(bmap, NULL);
3231         bmap = isl_basic_map_finalize(bmap);
3232
3233         return bmap;
3234 error:
3235         isl_basic_map_free(bmap);
3236         return NULL;
3237 }
3238
3239 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3240         enum isl_dim_type dst_type, unsigned dst_pos,
3241         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3242 {
3243         return (isl_basic_set *)isl_basic_map_move_dims(
3244                 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3245 }
3246
3247 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3248         enum isl_dim_type dst_type, unsigned dst_pos,
3249         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3250 {
3251         if (!set)
3252                 return NULL;
3253         isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3254         return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3255                                         src_type, src_pos, n);
3256 error:
3257         isl_set_free(set);
3258         return NULL;
3259 }
3260
3261 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3262         enum isl_dim_type dst_type, unsigned dst_pos,
3263         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3264 {
3265         int i;
3266
3267         if (!map)
3268                 return NULL;
3269         if (n == 0)
3270                 return map;
3271
3272         isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3273                 goto error);
3274
3275         if (dst_type == src_type && dst_pos == src_pos)
3276                 return map;
3277
3278         isl_assert(map->ctx, dst_type != src_type, goto error);
3279
3280         map = isl_map_cow(map);
3281         if (!map)
3282                 return NULL;
3283
3284         map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3285         if (!map->dim)
3286                 goto error;
3287
3288         for (i = 0; i < map->n; ++i) {
3289                 map->p[i] = isl_basic_map_move_dims(map->p[i],
3290                                                 dst_type, dst_pos,
3291                                                 src_type, src_pos, n);
3292                 if (!map->p[i])
3293                         goto error;
3294         }
3295
3296         return map;
3297 error:
3298         isl_map_free(map);
3299         return NULL;
3300 }
3301
3302 /* Move the specified dimensions to the last columns right before
3303  * the divs.  Don't change the dimension specification of bmap.
3304  * That's the responsibility of the caller.
3305  */
3306 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3307         enum isl_dim_type type, unsigned first, unsigned n)
3308 {
3309         struct isl_dim_map *dim_map;
3310         struct isl_basic_map *res;
3311         enum isl_dim_type t;
3312         unsigned total, off;
3313
3314         if (!bmap)
3315                 return NULL;
3316         if (pos(bmap->dim, type) + first + n ==
3317                                 1 + isl_space_dim(bmap->dim, isl_dim_all))
3318                 return bmap;
3319
3320         total = isl_basic_map_total_dim(bmap);
3321         dim_map = isl_dim_map_alloc(bmap->ctx, total);
3322
3323         off = 0;
3324         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3325                 unsigned size = isl_space_dim(bmap->dim, t);
3326                 if (t == type) {
3327                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3328                                             0, first, off);
3329                         off += first;
3330                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3331                                             first, n, total - bmap->n_div - n);
3332                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3333                                             first + n, size - (first + n), off);
3334                         off += size - (first + n);
3335                 } else {
3336                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
3337                         off += size;
3338                 }
3339         }
3340         isl_dim_map_div(dim_map, bmap, off + n);
3341
3342         res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3343                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
3344         res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3345         return res;
3346 }
3347
3348 /* Turn the n dimensions of type type, starting at first
3349  * into existentially quantified variables.
3350  */
3351 __isl_give isl_basic_map *isl_basic_map_project_out(
3352                 __isl_take isl_basic_map *bmap,
3353                 enum isl_dim_type type, unsigned first, unsigned n)
3354 {
3355         int i;
3356         size_t row_size;
3357         isl_int **new_div;
3358         isl_int *old;
3359
3360         if (n == 0)
3361                 return basic_map_space_reset(bmap, type);
3362
3363         if (!bmap)
3364                 return NULL;
3365
3366         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3367                 return isl_basic_map_remove_dims(bmap, type, first, n);
3368
3369         isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3370                         goto error);
3371
3372         bmap = move_last(bmap, type, first, n);
3373         bmap = isl_basic_map_cow(bmap);
3374         if (!bmap)
3375                 return NULL;
3376
3377         row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3378         old = bmap->block2.data;
3379         bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3380                                         (bmap->extra + n) * (1 + row_size));
3381         if (!bmap->block2.data)
3382                 goto error;
3383         new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3384         if (!new_div)
3385                 goto error;
3386         for (i = 0; i < n; ++i) {
3387                 new_div[i] = bmap->block2.data +
3388                                 (bmap->extra + i) * (1 + row_size);
3389                 isl_seq_clr(new_div[i], 1 + row_size);
3390         }
3391         for (i = 0; i < bmap->extra; ++i)
3392                 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3393         free(bmap->div);
3394         bmap->div = new_div;
3395         bmap->n_div += n;
3396         bmap->extra += n;
3397
3398         bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3399         if (!bmap->dim)
3400                 goto error;
3401         bmap = isl_basic_map_simplify(bmap);
3402         bmap = isl_basic_map_drop_redundant_divs(bmap);
3403         return isl_basic_map_finalize(bmap);
3404 error:
3405         isl_basic_map_free(bmap);
3406         return NULL;
3407 }
3408
3409 /* Turn the n dimensions of type type, starting at first
3410  * into existentially quantified variables.
3411  */
3412 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3413                 enum isl_dim_type type, unsigned first, unsigned n)
3414 {
3415         return (isl_basic_set *)isl_basic_map_project_out(
3416                         (isl_basic_map *)bset, type, first, n);
3417 }
3418
3419 /* Turn the n dimensions of type type, starting at first
3420  * into existentially quantified variables.
3421  */
3422 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3423                 enum isl_dim_type type, unsigned first, unsigned n)
3424 {
3425         int i;
3426
3427         if (!map)
3428                 return NULL;
3429
3430         if (n == 0)
3431                 return map_space_reset(map, type);
3432
3433         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3434
3435         map = isl_map_cow(map);
3436         if (!map)
3437                 return NULL;
3438
3439         map->dim = isl_space_drop_dims(map->dim, type, first, n);
3440         if (!map->dim)
3441                 goto error;
3442
3443         for (i = 0; i < map->n; ++i) {
3444                 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3445                 if (!map->p[i])
3446                         goto error;
3447         }
3448
3449         return map;
3450 error:
3451         isl_map_free(map);
3452         return NULL;
3453 }
3454
3455 /* Turn the n dimensions of type type, starting at first
3456  * into existentially quantified variables.
3457  */
3458 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3459                 enum isl_dim_type type, unsigned first, unsigned n)
3460 {
3461         return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3462 }
3463
3464 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3465 {
3466         int i, j;
3467
3468         for (i = 0; i < n; ++i) {
3469                 j = isl_basic_map_alloc_div(bmap);
3470                 if (j < 0)
3471                         goto error;
3472                 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3473         }
3474         return bmap;
3475 error:
3476         isl_basic_map_free(bmap);
3477         return NULL;
3478 }
3479
3480 struct isl_basic_map *isl_basic_map_apply_range(
3481                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3482 {
3483         isl_space *dim_result = NULL;
3484         struct isl_basic_map *bmap;
3485         unsigned n_in, n_out, n, nparam, total, pos;
3486         struct isl_dim_map *dim_map1, *dim_map2;
3487
3488         if (!bmap1 || !bmap2)
3489                 goto error;
3490
3491         dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3492                                   isl_space_copy(bmap2->dim));
3493
3494         n_in = isl_basic_map_n_in(bmap1);
3495         n_out = isl_basic_map_n_out(bmap2);
3496         n = isl_basic_map_n_out(bmap1);
3497         nparam = isl_basic_map_n_param(bmap1);
3498
3499         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3500         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3501         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3502         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3503         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3504         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3505         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3506         isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3507         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3508         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3509         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3510
3511         bmap = isl_basic_map_alloc_space(dim_result,
3512                         bmap1->n_div + bmap2->n_div + n,
3513                         bmap1->n_eq + bmap2->n_eq,
3514                         bmap1->n_ineq + bmap2->n_ineq);
3515         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3516         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3517         bmap = add_divs(bmap, n);
3518         bmap = isl_basic_map_simplify(bmap);
3519         bmap = isl_basic_map_drop_redundant_divs(bmap);
3520         return isl_basic_map_finalize(bmap);
3521 error:
3522         isl_basic_map_free(bmap1);
3523         isl_basic_map_free(bmap2);
3524         return NULL;
3525 }
3526
3527 struct isl_basic_set *isl_basic_set_apply(
3528                 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3529 {
3530         if (!bset || !bmap)
3531                 goto error;
3532
3533         isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3534                     goto error);
3535
3536         return (struct isl_basic_set *)
3537                 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3538 error:
3539         isl_basic_set_free(bset);
3540         isl_basic_map_free(bmap);
3541         return NULL;
3542 }
3543
3544 struct isl_basic_map *isl_basic_map_apply_domain(
3545                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3546 {
3547         if (!bmap1 || !bmap2)
3548                 goto error;
3549
3550         isl_assert(bmap1->ctx,
3551             isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3552         isl_assert(bmap1->ctx,
3553             isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3554             goto error);
3555
3556         bmap1 = isl_basic_map_reverse(bmap1);
3557         bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3558         return isl_basic_map_reverse(bmap1);
3559 error:
3560         isl_basic_map_free(bmap1);
3561         isl_basic_map_free(bmap2);
3562         return NULL;
3563 }
3564
3565 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3566  * A \cap B -> f(A) + f(B)
3567  */
3568 struct isl_basic_map *isl_basic_map_sum(
3569                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3570 {
3571         unsigned n_in, n_out, nparam, total, pos;
3572         struct isl_basic_map *bmap = NULL;
3573         struct isl_dim_map *dim_map1, *dim_map2;
3574         int i;
3575
3576         if (!bmap1 || !bmap2)
3577                 goto error;
3578
3579         isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3580                 goto error);
3581
3582         nparam = isl_basic_map_n_param(bmap1);
3583         n_in = isl_basic_map_n_in(bmap1);
3584         n_out = isl_basic_map_n_out(bmap1);
3585
3586         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3587         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3588         dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3589         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3590         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3591         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3592         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3593         isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3594         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3595         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3596         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3597
3598         bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3599                         bmap1->n_div + bmap2->n_div + 2 * n_out,
3600                         bmap1->n_eq + bmap2->n_eq + n_out,
3601                         bmap1->n_ineq + bmap2->n_ineq);
3602         for (i = 0; i < n_out; ++i) {
3603                 int j = isl_basic_map_alloc_equality(bmap);
3604                 if (j < 0)
3605                         goto error;
3606                 isl_seq_clr(bmap->eq[j], 1+total);
3607                 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3608                 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3609                 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3610         }
3611         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3612         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3613         bmap = add_divs(bmap, 2 * n_out);
3614
3615         bmap = isl_basic_map_simplify(bmap);
3616         return isl_basic_map_finalize(bmap);
3617 error:
3618         isl_basic_map_free(bmap);
3619         isl_basic_map_free(bmap1);
3620         isl_basic_map_free(bmap2);
3621         return NULL;
3622 }
3623
3624 /* Given two maps A -> f(A) and B -> g(B), construct a map
3625  * A \cap B -> f(A) + f(B)
3626  */
3627 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3628 {
3629         struct isl_map *result;
3630         int i, j;
3631
3632         if (!map1 || !map2)
3633                 goto error;
3634
3635         isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3636
3637         result = isl_map_alloc_space(isl_space_copy(map1->dim),
3638                                 map1->n * map2->n, 0);
3639         if (!result)
3640                 goto error;
3641         for (i = 0; i < map1->n; ++i)
3642                 for (j = 0; j < map2->n; ++j) {
3643                         struct isl_basic_map *part;
3644                         part = isl_basic_map_sum(
3645                                     isl_basic_map_copy(map1->p[i]),
3646                                     isl_basic_map_copy(map2->p[j]));
3647                         if (isl_basic_map_is_empty(part))
3648                                 isl_basic_map_free(part);
3649                         else
3650                                 result = isl_map_add_basic_map(result, part);
3651                         if (!result)
3652                                 goto error;
3653                 }
3654         isl_map_free(map1);
3655         isl_map_free(map2);
3656         return result;
3657 error:
3658         isl_map_free(map1);
3659         isl_map_free(map2);
3660         return NULL;
3661 }
3662
3663 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3664         __isl_take isl_set *set2)
3665 {
3666         return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3667 }
3668
3669 /* Given a basic map A -> f(A), construct A -> -f(A).
3670  */
3671 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3672 {
3673         int i, j;
3674         unsigned off, n;
3675
3676         bmap = isl_basic_map_cow(bmap);
3677         if (!bmap)
3678                 return NULL;
3679
3680         n = isl_basic_map_dim(bmap, isl_dim_out);
3681         off = isl_basic_map_offset(bmap, isl_dim_out);
3682         for (i = 0; i < bmap->n_eq; ++i)
3683                 for (j = 0; j < n; ++j)
3684                         isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3685         for (i = 0; i < bmap->n_ineq; ++i)
3686                 for (j = 0; j < n; ++j)
3687                         isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3688         for (i = 0; i < bmap->n_div; ++i)
3689                 for (j = 0; j < n; ++j)
3690                         isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3691         bmap = isl_basic_map_gauss(bmap, NULL);
3692         return isl_basic_map_finalize(bmap);
3693 }
3694
3695 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3696 {
3697         return isl_basic_map_neg(bset);
3698 }
3699
3700 /* Given a map A -> f(A), construct A -> -f(A).
3701  */
3702 struct isl_map *isl_map_neg(struct isl_map *map)
3703 {
3704         int i;
3705
3706         map = isl_map_cow(map);
3707         if (!map)
3708                 return NULL;
3709
3710         for (i = 0; i < map->n; ++i) {
3711                 map->p[i] = isl_basic_map_neg(map->p[i]);
3712                 if (!map->p[i])
3713                         goto error;
3714         }
3715
3716         return map;
3717 error:
3718         isl_map_free(map);
3719         return NULL;
3720 }
3721
3722 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3723 {
3724         return (isl_set *)isl_map_neg((isl_map *)set);
3725 }
3726
3727 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3728  * A -> floor(f(A)/d).
3729  */
3730 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3731                 isl_int d)
3732 {
3733         unsigned n_in, n_out, nparam, total, pos;
3734         struct isl_basic_map *result = NULL;
3735         struct isl_dim_map *dim_map;
3736         int i;
3737
3738         if (!bmap)
3739                 return NULL;
3740
3741         nparam = isl_basic_map_n_param(bmap);
3742         n_in = isl_basic_map_n_in(bmap);
3743         n_out = isl_basic_map_n_out(bmap);
3744
3745         total = nparam + n_in + n_out + bmap->n_div + n_out;
3746         dim_map = isl_dim_map_alloc(bmap->ctx, total);
3747         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3748         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3749         isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3750         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3751
3752         result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3753                         bmap->n_div + n_out,
3754                         bmap->n_eq, bmap->n_ineq + 2 * n_out);
3755         result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3756         result = add_divs(result, n_out);
3757         for (i = 0; i < n_out; ++i) {
3758                 int j;
3759                 j = isl_basic_map_alloc_inequality(result);
3760                 if (j < 0)
3761                         goto error;
3762                 isl_seq_clr(result->ineq[j], 1+total);
3763                 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3764                 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3765                 j = isl_basic_map_alloc_inequality(result);
3766                 if (j < 0)
3767                         goto error;
3768                 isl_seq_clr(result->ineq[j], 1+total);
3769                 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3770                 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3771                 isl_int_sub_ui(result->ineq[j][0], d, 1);
3772         }
3773
3774         result = isl_basic_map_simplify(result);
3775         return isl_basic_map_finalize(result);
3776 error:
3777         isl_basic_map_free(result);
3778         return NULL;
3779 }
3780
3781 /* Given a map A -> f(A) and an integer d, construct a map
3782  * A -> floor(f(A)/d).
3783  */
3784 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3785 {
3786         int i;
3787
3788         map = isl_map_cow(map);
3789         if (!map)
3790                 return NULL;
3791
3792         ISL_F_CLR(map, ISL_MAP_DISJOINT);
3793         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3794         for (i = 0; i < map->n; ++i) {
3795                 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3796                 if (!map->p[i])
3797                         goto error;
3798         }
3799
3800         return map;
3801 error:
3802         isl_map_free(map);
3803         return NULL;
3804 }
3805
3806 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3807 {
3808         int i;
3809         unsigned nparam;
3810         unsigned n_in;
3811
3812         i = isl_basic_map_alloc_equality(bmap);
3813         if (i < 0)
3814                 goto error;
3815         nparam = isl_basic_map_n_param(bmap);
3816         n_in = isl_basic_map_n_in(bmap);
3817         isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3818         isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3819         isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3820         return isl_basic_map_finalize(bmap);
3821 error:
3822         isl_basic_map_free(bmap);
3823         return NULL;
3824 }
3825
3826 /* Add a constraints to "bmap" expressing i_pos < o_pos
3827  */
3828 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3829 {
3830         int i;
3831         unsigned nparam;
3832         unsigned n_in;
3833
3834         i = isl_basic_map_alloc_inequality(bmap);
3835         if (i < 0)
3836                 goto error;
3837         nparam = isl_basic_map_n_param(bmap);
3838         n_in = isl_basic_map_n_in(bmap);
3839         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3840         isl_int_set_si(bmap->ineq[i][0], -1);
3841         isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3842         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3843         return isl_basic_map_finalize(bmap);
3844 error:
3845         isl_basic_map_free(bmap);
3846         return NULL;
3847 }
3848
3849 /* Add a constraint to "bmap" expressing i_pos <= o_pos
3850  */
3851 static __isl_give isl_basic_map *var_less_or_equal(
3852         __isl_take isl_basic_map *bmap, unsigned pos)
3853 {
3854         int i;
3855         unsigned nparam;
3856         unsigned n_in;
3857
3858         i = isl_basic_map_alloc_inequality(bmap);
3859         if (i < 0)
3860                 goto error;
3861         nparam = isl_basic_map_n_param(bmap);
3862         n_in = isl_basic_map_n_in(bmap);
3863         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3864         isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3865         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], 1);
3866         return isl_basic_map_finalize(bmap);
3867 error:
3868         isl_basic_map_free(bmap);
3869         return NULL;
3870 }
3871
3872 /* Add a constraints to "bmap" expressing i_pos > o_pos
3873  */
3874 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3875 {
3876         int i;
3877         unsigned nparam;
3878         unsigned n_in;
3879
3880         i = isl_basic_map_alloc_inequality(bmap);
3881         if (i < 0)
3882                 goto error;
3883         nparam = isl_basic_map_n_param(bmap);
3884         n_in = isl_basic_map_n_in(bmap);
3885         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3886         isl_int_set_si(bmap->ineq[i][0], -1);
3887         isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3888         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3889         return isl_basic_map_finalize(bmap);
3890 error:
3891         isl_basic_map_free(bmap);
3892         return NULL;
3893 }
3894
3895 /* Add a constraint to "bmap" expressing i_pos >= o_pos
3896  */
3897 static __isl_give isl_basic_map *var_more_or_equal(
3898         __isl_take isl_basic_map *bmap, unsigned pos)
3899 {
3900         int i;
3901         unsigned nparam;
3902         unsigned n_in;
3903
3904         i = isl_basic_map_alloc_inequality(bmap);
3905         if (i < 0)
3906                 goto error;
3907         nparam = isl_basic_map_n_param(bmap);
3908         n_in = isl_basic_map_n_in(bmap);
3909         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3910         isl_int_set_si(bmap->ineq[i][1+nparam+pos], 1);
3911         isl_int_set_si(bmap->ineq[i][1+nparam+n_in+pos], -1);
3912         return isl_basic_map_finalize(bmap);
3913 error:
3914         isl_basic_map_free(bmap);
3915         return NULL;
3916 }
3917
3918 __isl_give isl_basic_map *isl_basic_map_equal(
3919         __isl_take isl_space *dim, unsigned n_equal)
3920 {
3921         int i;
3922         struct isl_basic_map *bmap;
3923         bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3924         if (!bmap)
3925                 return NULL;
3926         for (i = 0; i < n_equal && bmap; ++i)
3927                 bmap = var_equal(bmap, i);
3928         return isl_basic_map_finalize(bmap);
3929 }
3930
3931 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3932  */
3933 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3934         unsigned pos)
3935 {
3936         int i;
3937         struct isl_basic_map *bmap;
3938         bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3939         if (!bmap)
3940                 return NULL;
3941         for (i = 0; i < pos && bmap; ++i)
3942                 bmap = var_equal(bmap, i);
3943         if (bmap)
3944                 bmap = var_less(bmap, pos);
3945         return isl_basic_map_finalize(bmap);
3946 }
3947
3948 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3949  */
3950 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
3951         __isl_take isl_space *dim, unsigned pos)
3952 {
3953         int i;
3954         isl_basic_map *bmap;
3955
3956         bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3957         for (i = 0; i < pos; ++i)
3958                 bmap = var_equal(bmap, i);
3959         bmap = var_less_or_equal(bmap, pos);
3960         return isl_basic_map_finalize(bmap);
3961 }
3962
3963 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
3964  */
3965 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
3966         unsigned pos)
3967 {
3968         int i;
3969         struct isl_basic_map *bmap;
3970         bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3971         if (!bmap)
3972                 return NULL;
3973         for (i = 0; i < pos && bmap; ++i)
3974                 bmap = var_equal(bmap, i);
3975         if (bmap)
3976                 bmap = var_more(bmap, pos);
3977         return isl_basic_map_finalize(bmap);
3978 }
3979
3980 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
3981  */
3982 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
3983         __isl_take isl_space *dim, unsigned pos)
3984 {
3985         int i;
3986         isl_basic_map *bmap;
3987
3988         bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3989         for (i = 0; i < pos; ++i)
3990                 bmap = var_equal(bmap, i);
3991         bmap = var_more_or_equal(bmap, pos);
3992         return isl_basic_map_finalize(bmap);
3993 }
3994
3995 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
3996         unsigned n, int equal)
3997 {
3998         struct isl_map *map;
3999         int i;
4000
4001         if (n == 0 && equal)
4002                 return isl_map_universe(dims);
4003
4004         map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4005
4006         for (i = 0; i + 1 < n; ++i)
4007                 map = isl_map_add_basic_map(map,
4008                                   isl_basic_map_less_at(isl_space_copy(dims), i));
4009         if (n > 0) {
4010                 if (equal)
4011                         map = isl_map_add_basic_map(map,
4012                               isl_basic_map_less_or_equal_at(dims, n - 1));
4013                 else
4014                         map = isl_map_add_basic_map(map,
4015                               isl_basic_map_less_at(dims, n - 1));
4016         } else
4017                 isl_space_free(dims);
4018
4019         return map;
4020 }
4021
4022 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4023 {
4024         if (!dims)
4025                 return NULL;
4026         return map_lex_lte_first(dims, dims->n_out, equal);
4027 }
4028
4029 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4030 {
4031         return map_lex_lte_first(dim, n, 0);
4032 }
4033
4034 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4035 {
4036         return map_lex_lte_first(dim, n, 1);
4037 }
4038
4039 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4040 {
4041         return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4042 }
4043
4044 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4045 {
4046         return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4047 }
4048
4049 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4050         unsigned n, int equal)
4051 {
4052         struct isl_map *map;
4053         int i;
4054
4055         if (n == 0 && equal)
4056                 return isl_map_universe(dims);
4057
4058         map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4059
4060         for (i = 0; i + 1 < n; ++i)
4061                 map = isl_map_add_basic_map(map,
4062                                   isl_basic_map_more_at(isl_space_copy(dims), i));
4063         if (n > 0) {
4064                 if (equal)
4065                         map = isl_map_add_basic_map(map,
4066                               isl_basic_map_more_or_equal_at(dims, n - 1));
4067                 else
4068                         map = isl_map_add_basic_map(map,
4069                               isl_basic_map_more_at(dims, n - 1));
4070         } else
4071                 isl_space_free(dims);
4072
4073         return map;
4074 }
4075
4076 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4077 {
4078         if (!dims)
4079                 return NULL;
4080         return map_lex_gte_first(dims, dims->n_out, equal);
4081 }
4082
4083 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4084 {
4085         return map_lex_gte_first(dim, n, 0);
4086 }
4087
4088 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4089 {
4090         return map_lex_gte_first(dim, n, 1);
4091 }
4092
4093 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4094 {
4095         return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4096 }
4097
4098 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4099 {
4100         return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4101 }
4102
4103 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4104         __isl_take isl_set *set2)
4105 {
4106         isl_map *map;
4107         map = isl_map_lex_le(isl_set_get_space(set1));
4108         map = isl_map_intersect_domain(map, set1);
4109         map = isl_map_intersect_range(map, set2);
4110         return map;
4111 }
4112
4113 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4114         __isl_take isl_set *set2)
4115 {
4116         isl_map *map;
4117         map = isl_map_lex_lt(isl_set_get_space(set1));
4118         map = isl_map_intersect_domain(map, set1);
4119         map = isl_map_intersect_range(map, set2);
4120         return map;
4121 }
4122
4123 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4124         __isl_take isl_set *set2)
4125 {
4126         isl_map *map;
4127         map = isl_map_lex_ge(isl_set_get_space(set1));
4128         map = isl_map_intersect_domain(map, set1);
4129         map = isl_map_intersect_range(map, set2);
4130         return map;
4131 }
4132
4133 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4134         __isl_take isl_set *set2)
4135 {
4136         isl_map *map;
4137         map = isl_map_lex_gt(isl_set_get_space(set1));
4138         map = isl_map_intersect_domain(map, set1);
4139         map = isl_map_intersect_range(map, set2);
4140         return map;
4141 }
4142
4143 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4144         __isl_take isl_map *map2)
4145 {
4146         isl_map *map;
4147         map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4148         map = isl_map_apply_domain(map, isl_map_reverse(map1));
4149         map = isl_map_apply_range(map, isl_map_reverse(map2));
4150         return map;
4151 }
4152
4153 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4154         __isl_take isl_map *map2)
4155 {
4156         isl_map *map;
4157         map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4158         map = isl_map_apply_domain(map, isl_map_reverse(map1));
4159         map = isl_map_apply_range(map, isl_map_reverse(map2));
4160         return map;
4161 }
4162
4163 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4164         __isl_take isl_map *map2)
4165 {
4166         isl_map *map;
4167         map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4168         map = isl_map_apply_domain(map, isl_map_reverse(map1));
4169         map = isl_map_apply_range(map, isl_map_reverse(map2));
4170         return map;
4171 }
4172
4173 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4174         __isl_take isl_map *map2)
4175 {
4176         isl_map *map;
4177         map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4178         map = isl_map_apply_domain(map, isl_map_reverse(map1));
4179         map = isl_map_apply_range(map, isl_map_reverse(map2));
4180         return map;
4181 }
4182
4183 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4184         __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4185 {
4186         struct isl_basic_map *bmap;
4187
4188         bset = isl_basic_set_cow(bset);
4189         if (!bset || !dim)
4190                 goto error;
4191
4192         isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4193         isl_space_free(bset->dim);
4194         bmap = (struct isl_basic_map *) bset;
4195         bmap->dim = dim;
4196         return isl_basic_map_finalize(bmap);
4197 error:
4198         isl_basic_set_free(bset);
4199         isl_space_free(dim);
4200         return NULL;
4201 }
4202
4203 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4204 {
4205         if (!bmap)
4206                 goto error;
4207         if (bmap->dim->n_in == 0)
4208                 return (struct isl_basic_set *)bmap;
4209         bmap = isl_basic_map_cow(bmap);
4210         if (!bmap)
4211                 goto error;
4212         bmap->dim = isl_space_as_set_space(bmap->dim);
4213         if (!bmap->dim)
4214                 goto error;
4215         bmap = isl_basic_map_finalize(bmap);
4216         return (struct isl_basic_set *)bmap;
4217 error:
4218         isl_basic_map_free(bmap);
4219         return NULL;
4220 }
4221
4222 /* For a div d = floor(f/m), add the constraints
4223  *
4224  *              f - m d >= 0
4225  *              -(f-(n-1)) + m d >= 0
4226  *
4227  * Note that the second constraint is the negation of
4228  *
4229  *              f - m d >= n
4230  */
4231 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4232         unsigned pos, isl_int *div)
4233 {
4234         int i, j;
4235         unsigned total = isl_basic_map_total_dim(bmap);
4236
4237         i = isl_basic_map_alloc_inequality(bmap);
4238         if (i < 0)
4239                 return -1;
4240         isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4241         isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4242
4243         j = isl_basic_map_alloc_inequality(bmap);
4244         if (j < 0)
4245                 return -1;
4246         isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
4247         isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
4248         isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
4249         return j;
4250 }
4251
4252 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4253         unsigned pos, isl_int *div)
4254 {
4255         return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4256                                                         pos, div);
4257 }
4258
4259 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4260 {
4261         unsigned total = isl_basic_map_total_dim(bmap);
4262         unsigned div_pos = total - bmap->n_div + div;
4263
4264         return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4265                                                         bmap->div[div]);
4266 }
4267
4268 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4269 {
4270         return isl_basic_map_add_div_constraints(bset, div);
4271 }
4272
4273 struct isl_basic_set *isl_basic_map_underlying_set(
4274                 struct isl_basic_map *bmap)
4275 {
4276         if (!bmap)
4277                 goto error;
4278         if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4279             bmap->n_div == 0 &&
4280             !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4281             !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4282                 return (struct isl_basic_set *)bmap;
4283         bmap = isl_basic_map_cow(bmap);
4284         if (!bmap)
4285                 goto error;
4286         bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4287         if (!bmap->dim)
4288                 goto error;
4289         bmap->extra -= bmap->n_div;
4290         bmap->n_div = 0;
4291         bmap = isl_basic_map_finalize(bmap);
4292         return (struct isl_basic_set *)bmap;
4293 error:
4294         isl_basic_map_free(bmap);
4295         return NULL;
4296 }
4297
4298 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4299                 __isl_take isl_basic_set *bset)
4300 {
4301         return isl_basic_map_underlying_set((isl_basic_map *)bset);
4302 }
4303
4304 struct isl_basic_map *isl_basic_map_overlying_set(
4305         struct isl_basic_set *bset, struct isl_basic_map *like)
4306 {
4307         struct isl_basic_map *bmap;
4308         struct isl_ctx *ctx;
4309         unsigned total;
4310         int i;
4311
4312         if (!bset || !like)
4313                 goto error;
4314         ctx = bset->ctx;
4315         isl_assert(ctx, bset->n_div == 0, goto error);
4316         isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4317         isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4318                         goto error);
4319         if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4320                 isl_basic_map_free(like);
4321                 return (struct isl_basic_map *)bset;
4322         }
4323         bset = isl_basic_set_cow(bset);
4324         if (!bset)
4325                 goto error;
4326         total = bset->dim->n_out + bset->extra;
4327         bmap = (struct isl_basic_map *)bset;
4328         isl_space_free(bmap->dim);
4329         bmap->dim = isl_space_copy(like->dim);
4330         if (!bmap->dim)
4331                 goto error;
4332         bmap->n_div = like->n_div;
4333         bmap->extra += like->n_div;
4334         if (bmap->extra) {
4335                 unsigned ltotal;
4336                 isl_int **div;
4337                 ltotal = total - bmap->extra + like->extra;
4338                 if (ltotal > total)
4339                         ltotal = total;
4340                 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4341                                         bmap->extra * (1 + 1 + total));
4342                 if (isl_blk_is_error(bmap->block2))
4343                         goto error;
4344                 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4345                 if (!div)
4346                         goto error;
4347                 bmap->div = div;
4348                 for (i = 0; i < bmap->extra; ++i)
4349                         bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4350                 for (i = 0; i < like->n_div; ++i) {
4351                         isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4352                         isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4353                 }
4354                 bmap = isl_basic_map_extend_constraints(bmap, 
4355                                                         0, 2 * like->n_div);
4356                 for (i = 0; i < like->n_div; ++i) {
4357                         if (!bmap)
4358                                 break;
4359                         if (isl_int_is_zero(bmap->div[i][0]))
4360                                 continue;
4361                         if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4362                                 bmap = isl_basic_map_free(bmap);
4363                 }
4364         }
4365         isl_basic_map_free(like);
4366         bmap = isl_basic_map_simplify(bmap);
4367         bmap = isl_basic_map_finalize(bmap);
4368         return bmap;
4369 error:
4370         isl_basic_map_free(like);
4371         isl_basic_set_free(bset);
4372         return NULL;
4373 }
4374
4375 struct isl_basic_set *isl_basic_set_from_underlying_set(
4376         struct isl_basic_set *bset, struct isl_basic_set *like)
4377 {
4378         return (struct isl_basic_set *)
4379                 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4380 }
4381
4382 struct isl_set *isl_set_from_underlying_set(
4383         struct isl_set *set, struct isl_basic_set *like)
4384 {
4385         int i;
4386
4387         if (!set || !like)
4388                 goto error;
4389         isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4390                     goto error);
4391         if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4392                 isl_basic_set_free(like);
4393                 return set;
4394         }
4395         set = isl_set_cow(set);
4396         if (!set)
4397                 goto error;
4398         for (i = 0; i < set->n; ++i) {
4399                 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4400                                                       isl_basic_set_copy(like));
4401                 if (!set->p[i])
4402                         goto error;
4403         }
4404         isl_space_free(set->dim);
4405         set->dim = isl_space_copy(like->dim);
4406         if (!set->dim)
4407                 goto error;
4408         isl_basic_set_free(like);
4409         return set;
4410 error:
4411         isl_basic_set_free(like);
4412         isl_set_free(set);
4413         return NULL;
4414 }
4415
4416 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4417 {
4418         int i;
4419
4420         map = isl_map_cow(map);
4421         if (!map)
4422                 return NULL;
4423         map->dim = isl_space_cow(map->dim);
4424         if (!map->dim)
4425                 goto error;
4426
4427         for (i = 1; i < map->n; ++i)
4428                 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4429                                 goto error);
4430         for (i = 0; i < map->n; ++i) {
4431                 map->p[i] = (struct isl_basic_map *)
4432                                 isl_basic_map_underlying_set(map->p[i]);
4433                 if (!map->p[i])
4434                         goto error;
4435         }
4436         if (map->n == 0)
4437                 map->dim = isl_space_underlying(map->dim, 0);
4438         else {
4439                 isl_space_free(map->dim);
4440                 map->dim = isl_space_copy(map->p[0]->dim);
4441         }
4442         if (!map->dim)
4443                 goto error;
4444         return (struct isl_set *)map;
4445 error:
4446         isl_map_free(map);
4447         return NULL;
4448 }
4449
4450 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4451 {
4452         return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4453 }
4454
4455 __isl_give isl_basic_map *isl_basic_map_reset_space(
4456         __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4457 {
4458         bmap = isl_basic_map_cow(bmap);
4459         if (!bmap || !dim)
4460                 goto error;
4461
4462         isl_space_free(bmap->dim);
4463         bmap->dim = dim;
4464
4465         bmap = isl_basic_map_finalize(bmap);
4466
4467         return bmap;
4468 error:
4469         isl_basic_map_free(bmap);
4470         isl_space_free(dim);
4471         return NULL;
4472 }
4473
4474 __isl_give isl_basic_set *isl_basic_set_reset_space(
4475         __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4476 {
4477         return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4478                                                         dim);
4479 }
4480
4481 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4482         __isl_take isl_space *dim)
4483 {
4484         int i;
4485
4486         map = isl_map_cow(map);
4487         if (!map || !dim)
4488                 goto error;
4489
4490         for (i = 0; i < map->n; ++i) {
4491                 map->p[i] = isl_basic_map_reset_space(map->p[i],
4492                                                     isl_space_copy(dim));
4493                 if (!map->p[i])
4494                         goto error;
4495         }
4496         isl_space_free(map->dim);
4497         map->dim = dim;
4498
4499         return map;
4500 error:
4501         isl_map_free(map);
4502         isl_space_free(dim);
4503         return NULL;
4504 }
4505
4506 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4507         __isl_take isl_space *dim)
4508 {
4509         return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4510 }
4511
4512 /* Compute the parameter domain of the given basic set.
4513  */
4514 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4515 {
4516         isl_space *space;
4517         unsigned n;
4518
4519         if (isl_basic_set_is_params(bset))
4520                 return bset;
4521
4522         n = isl_basic_set_dim(bset, isl_dim_set);
4523         bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4524         space = isl_basic_set_get_space(bset);
4525         space = isl_space_params(space);
4526         bset = isl_basic_set_reset_space(bset, space);
4527         return bset;
4528 }
4529
4530 /* Construct a zero-dimensional basic set with the given parameter domain.
4531  */
4532 __isl_give isl_basic_set *isl_basic_set_from_params(
4533         __isl_take isl_basic_set *bset)
4534 {
4535         isl_space *space;
4536         space = isl_basic_set_get_space(bset);
4537         space = isl_space_set_from_params(space);
4538         bset = isl_basic_set_reset_space(bset, space);
4539         return bset;
4540 }
4541
4542 /* Compute the parameter domain of the given set.
4543  */
4544 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4545 {
4546         isl_space *space;
4547         unsigned n;
4548
4549         if (isl_set_is_params(set))
4550                 return set;
4551
4552         n = isl_set_dim(set, isl_dim_set);
4553         set = isl_set_project_out(set, isl_dim_set, 0, n);
4554         space = isl_set_get_space(set);
4555         space = isl_space_params(space);
4556         set = isl_set_reset_space(set, space);
4557         return set;
4558 }
4559
4560 /* Construct a zero-dimensional set with the given parameter domain.
4561  */
4562 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4563 {
4564         isl_space *space;
4565         space = isl_set_get_space(set);
4566         space = isl_space_set_from_params(space);
4567         set = isl_set_reset_space(set, space);
4568         return set;
4569 }
4570
4571 /* Compute the parameter domain of the given map.
4572  */
4573 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4574 {
4575         isl_space *space;
4576         unsigned n;
4577
4578         n = isl_map_dim(map, isl_dim_in);
4579         map = isl_map_project_out(map, isl_dim_in, 0, n);
4580         n = isl_map_dim(map, isl_dim_out);
4581         map = isl_map_project_out(map, isl_dim_out, 0, n);
4582         space = isl_map_get_space(map);
4583         space = isl_space_params(space);
4584         map = isl_map_reset_space(map, space);
4585         return map;
4586 }
4587
4588 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4589 {
4590         isl_space *dim;
4591         struct isl_basic_set *domain;
4592         unsigned n_in;
4593         unsigned n_out;
4594
4595         if (!bmap)
4596                 return NULL;
4597         dim = isl_space_domain(isl_basic_map_get_space(bmap));
4598
4599         n_in = isl_basic_map_n_in(bmap);
4600         n_out = isl_basic_map_n_out(bmap);
4601         domain = isl_basic_set_from_basic_map(bmap);
4602         domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4603
4604         domain = isl_basic_set_reset_space(domain, dim);
4605
4606         return domain;
4607 }
4608
4609 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4610 {
4611         if (!bmap)
4612                 return -1;
4613         return isl_space_may_be_set(bmap->dim);
4614 }
4615
4616 /* Is this basic map actually a set?
4617  * Users should never call this function.  Outside of isl,
4618  * the type should indicate whether something is a set or a map.
4619  */
4620 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4621 {
4622         if (!bmap)
4623                 return -1;
4624         return isl_space_is_set(bmap->dim);
4625 }
4626
4627 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4628 {
4629         if (!bmap)
4630                 return NULL;
4631         if (isl_basic_map_is_set(bmap))
4632                 return bmap;
4633         return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4634 }
4635
4636 __isl_give isl_basic_map *isl_basic_map_domain_map(
4637         __isl_take isl_basic_map *bmap)
4638 {
4639         int i, k;
4640         isl_space *dim;
4641         isl_basic_map *domain;
4642         int nparam, n_in, n_out;
4643         unsigned total;
4644
4645         nparam = isl_basic_map_dim(bmap, isl_dim_param);
4646         n_in = isl_basic_map_dim(bmap, isl_dim_in);
4647         n_out = isl_basic_map_dim(bmap, isl_dim_out);
4648
4649         dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4650         domain = isl_basic_map_universe(dim);
4651
4652         bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4653         bmap = isl_basic_map_apply_range(bmap, domain);
4654         bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4655
4656         total = isl_basic_map_total_dim(bmap);
4657
4658         for (i = 0; i < n_in; ++i) {
4659                 k = isl_basic_map_alloc_equality(bmap);
4660                 if (k < 0)
4661                         goto error;
4662                 isl_seq_clr(bmap->eq[k], 1 + total);
4663                 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4664                 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4665         }
4666
4667         bmap = isl_basic_map_gauss(bmap, NULL);
4668         return isl_basic_map_finalize(bmap);
4669 error:
4670         isl_basic_map_free(bmap);
4671         return NULL;
4672 }
4673
4674 __isl_give isl_basic_map *isl_basic_map_range_map(
4675         __isl_take isl_basic_map *bmap)
4676 {
4677         int i, k;
4678         isl_space *dim;
4679         isl_basic_map *range;
4680         int nparam, n_in, n_out;
4681         unsigned total;
4682
4683         nparam = isl_basic_map_dim(bmap, isl_dim_param);
4684         n_in = isl_basic_map_dim(bmap, isl_dim_in);
4685         n_out = isl_basic_map_dim(bmap, isl_dim_out);
4686
4687         dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4688         range = isl_basic_map_universe(dim);
4689
4690         bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4691         bmap = isl_basic_map_apply_range(bmap, range);
4692         bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4693
4694         total = isl_basic_map_total_dim(bmap);
4695
4696         for (i = 0; i < n_out; ++i) {
4697                 k = isl_basic_map_alloc_equality(bmap);
4698                 if (k < 0)
4699                         goto error;
4700                 isl_seq_clr(bmap->eq[k], 1 + total);
4701                 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4702                 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4703         }
4704
4705         bmap = isl_basic_map_gauss(bmap, NULL);
4706         return isl_basic_map_finalize(bmap);
4707 error:
4708         isl_basic_map_free(bmap);
4709         return NULL;
4710 }
4711
4712 int isl_map_may_be_set(__isl_keep isl_map *map)
4713 {
4714         if (!map)
4715                 return -1;
4716         return isl_space_may_be_set(map->dim);
4717 }
4718
4719 /* Is this map actually a set?
4720  * Users should never call this function.  Outside of isl,
4721  * the type should indicate whether something is a set or a map.
4722  */
4723 int isl_map_is_set(__isl_keep isl_map *map)
4724 {
4725         if (!map)
4726                 return -1;
4727         return isl_space_is_set(map->dim);
4728 }
4729
4730 struct isl_set *isl_map_range(struct isl_map *map)
4731 {
4732         int i;
4733         struct isl_set *set;
4734
4735         if (!map)
4736                 goto error;
4737         if (isl_map_is_set(map))
4738                 return (isl_set *)map;
4739
4740         map = isl_map_cow(map);
4741         if (!map)
4742                 goto error;
4743
4744         set = (struct isl_set *) map;
4745         set->dim = isl_space_range(set->dim);
4746         if (!set->dim)
4747                 goto error;
4748         for (i = 0; i < map->n; ++i) {
4749                 set->p[i] = isl_basic_map_range(map->p[i]);
4750                 if (!set->p[i])
4751                         goto error;
4752         }
4753         ISL_F_CLR(set, ISL_MAP_DISJOINT);
4754         ISL_F_CLR(set, ISL_SET_NORMALIZED);
4755         return set;
4756 error:
4757         isl_map_free(map);
4758         return NULL;
4759 }
4760
4761 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4762 {
4763         int i;
4764         isl_space *domain_dim;
4765
4766         map = isl_map_cow(map);
4767         if (!map)
4768                 return NULL;
4769
4770         domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4771         map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4772         map->dim = isl_space_join(map->dim, domain_dim);
4773         if (!map->dim)
4774                 goto error;
4775         for (i = 0; i < map->n; ++i) {
4776                 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4777                 if (!map->p[i])
4778                         goto error;
4779         }
4780         ISL_F_CLR(map, ISL_MAP_DISJOINT);
4781         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4782         return map;
4783 error:
4784         isl_map_free(map);
4785         return NULL;
4786 }
4787
4788 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4789 {
4790         int i;
4791         isl_space *range_dim;
4792
4793         map = isl_map_cow(map);
4794         if (!map)
4795                 return NULL;
4796
4797         range_dim = isl_space_range(isl_map_get_space(map));
4798         range_dim = isl_space_from_range(range_dim);
4799         map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4800         map->dim = isl_space_join(map->dim, range_dim);
4801         if (!map->dim)
4802                 goto error;
4803         for (i = 0; i < map->n; ++i) {
4804                 map->p[i] = isl_basic_map_range_map(map->p[i]);
4805                 if (!map->p[i])
4806                         goto error;
4807         }
4808         ISL_F_CLR(map, ISL_MAP_DISJOINT);
4809         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4810         return map;
4811 error:
4812         isl_map_free(map);
4813         return NULL;
4814 }
4815
4816 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4817         __isl_take isl_space *dim)
4818 {
4819         int i;
4820         struct isl_map *map = NULL;
4821
4822         set = isl_set_cow(set);
4823         if (!set || !dim)
4824                 goto error;
4825         isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4826         map = (struct isl_map *)set;
4827         for (i = 0; i < set->n; ++i) {
4828                 map->p[i] = isl_basic_map_from_basic_set(
4829                                 set->p[i], isl_space_copy(dim));
4830                 if (!map->p[i])
4831                         goto error;
4832         }
4833         isl_space_free(map->dim);
4834         map->dim = dim;
4835         return map;
4836 error:
4837         isl_space_free(dim);
4838         isl_set_free(set);
4839         return NULL;
4840 }
4841
4842 __isl_give isl_basic_map *isl_basic_map_from_domain(
4843         __isl_take isl_basic_set *bset)
4844 {
4845         return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4846 }
4847
4848 __isl_give isl_basic_map *isl_basic_map_from_range(
4849         __isl_take isl_basic_set *bset)
4850 {
4851         isl_space *space;
4852         space = isl_basic_set_get_space(bset);
4853         space = isl_space_from_range(space);
4854         bset = isl_basic_set_reset_space(bset, space);
4855         return (isl_basic_map *)bset;
4856 }
4857
4858 struct isl_map *isl_map_from_range(struct isl_set *set)
4859 {
4860         isl_space *space;
4861         space = isl_set_get_space(set);
4862         space = isl_space_from_range(space);
4863         set = isl_set_reset_space(set, space);
4864         return (struct isl_map *)set;
4865 }
4866
4867 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4868 {
4869         return isl_map_reverse(isl_map_from_range(set));
4870 }
4871
4872 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4873         __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4874 {
4875         return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4876 }
4877
4878 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4879         __isl_take isl_set *range)
4880 {
4881         return isl_map_apply_range(isl_map_reverse(domain), range);
4882 }
4883
4884 struct isl_set *isl_set_from_map(struct isl_map *map)
4885 {
4886         int i;
4887         struct isl_set *set = NULL;
4888
4889         if (!map)
4890                 return NULL;
4891         map = isl_map_cow(map);
4892         if (!map)
4893                 return NULL;
4894         map->dim = isl_space_as_set_space(map->dim);
4895         if (!map->dim)
4896                 goto error;
4897         set = (struct isl_set *)map;
4898         for (i = 0; i < map->n; ++i) {
4899                 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4900                 if (!set->p[i])
4901                         goto error;
4902         }
4903         return set;
4904 error:
4905         isl_map_free(map);
4906         return NULL;
4907 }
4908
4909 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4910         unsigned flags)
4911 {
4912         struct isl_map *map;
4913
4914         if (!dim)
4915                 return NULL;
4916         if (n < 0)
4917                 isl_die(dim->ctx, isl_error_internal,
4918                         "negative number of basic maps", goto error);
4919         map = isl_alloc(dim->ctx, struct isl_map,
4920                         sizeof(struct isl_map) +
4921                         (n - 1) * sizeof(struct isl_basic_map *));
4922         if (!map)
4923                 goto error;
4924
4925         map->ctx = dim->ctx;
4926         isl_ctx_ref(map->ctx);
4927         map->ref = 1;
4928         map->size = n;
4929         map->n = 0;
4930         map->dim = dim;
4931         map->flags = flags;
4932         return map;
4933 error:
4934         isl_space_free(dim);
4935         return NULL;
4936 }
4937
4938 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4939                 unsigned nparam, unsigned in, unsigned out, int n,
4940                 unsigned flags)
4941 {
4942         struct isl_map *map;
4943         isl_space *dims;
4944
4945         dims = isl_space_alloc(ctx, nparam, in, out);
4946         if (!dims)
4947                 return NULL;
4948
4949         map = isl_map_alloc_space(dims, n, flags);
4950         return map;
4951 }
4952
4953 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
4954 {
4955         struct isl_basic_map *bmap;
4956         bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
4957         bmap = isl_basic_map_set_to_empty(bmap);
4958         return bmap;
4959 }
4960
4961 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
4962 {
4963         struct isl_basic_set *bset;
4964         bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
4965         bset = isl_basic_set_set_to_empty(bset);
4966         return bset;
4967 }
4968
4969 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
4970 {
4971         struct isl_basic_map *bmap;
4972         if (!model)
4973                 return NULL;
4974         bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4975         bmap = isl_basic_map_set_to_empty(bmap);
4976         return bmap;
4977 }
4978
4979 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
4980 {
4981         struct isl_basic_map *bmap;
4982         if (!model)
4983                 return NULL;
4984         bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4985         bmap = isl_basic_map_set_to_empty(bmap);
4986         return bmap;
4987 }
4988
4989 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
4990 {
4991         struct isl_basic_set *bset;
4992         if (!model)
4993                 return NULL;
4994         bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
4995         bset = isl_basic_set_set_to_empty(bset);
4996         return bset;
4997 }
4998
4999 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5000 {
5001         struct isl_basic_map *bmap;
5002         bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5003         bmap = isl_basic_map_finalize(bmap);
5004         return bmap;
5005 }
5006
5007 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5008 {
5009         struct isl_basic_set *bset;
5010         bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5011         bset = isl_basic_set_finalize(bset);
5012         return bset;
5013 }
5014
5015 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5016 {
5017         int i;
5018         unsigned total = isl_space_dim(dim, isl_dim_all);
5019         isl_basic_map *bmap;
5020
5021         bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5022         for (i = 0; i < total; ++i) {
5023                 int k = isl_basic_map_alloc_inequality(bmap);
5024                 if (k < 0)
5025                         goto error;
5026                 isl_seq_clr(bmap->ineq[k], 1 + total);
5027                 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5028         }
5029         return bmap;
5030 error:
5031         isl_basic_map_free(bmap);
5032         return NULL;
5033 }
5034
5035 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5036 {
5037         return isl_basic_map_nat_universe(dim);
5038 }
5039
5040 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5041 {
5042         return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5043 }
5044
5045 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5046 {
5047         return isl_map_nat_universe(dim);
5048 }
5049
5050 __isl_give isl_basic_map *isl_basic_map_universe_like(
5051                 __isl_keep isl_basic_map *model)
5052 {
5053         if (!model)
5054                 return NULL;
5055         return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5056 }
5057
5058 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5059 {
5060         if (!model)
5061                 return NULL;
5062         return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5063 }
5064
5065 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5066         __isl_keep isl_set *model)
5067 {
5068         if (!model)
5069                 return NULL;
5070         return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5071 }
5072
5073 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5074 {
5075         return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5076 }
5077
5078 struct isl_map *isl_map_empty_like(struct isl_map *model)
5079 {
5080         if (!model)
5081                 return NULL;
5082         return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5083 }
5084
5085 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5086 {
5087         if (!model)
5088                 return NULL;
5089         return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5090 }
5091
5092 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5093 {
5094         return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5095 }
5096
5097 struct isl_set *isl_set_empty_like(struct isl_set *model)
5098 {
5099         if (!model)
5100                 return NULL;
5101         return isl_set_empty(isl_space_copy(model->dim));
5102 }
5103
5104 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5105 {
5106         struct isl_map *map;
5107         if (!dim)
5108                 return NULL;
5109         map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5110         map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5111         return map;
5112 }
5113
5114 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5115 {
5116         struct isl_set *set;
5117         if (!dim)
5118                 return NULL;
5119         set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5120         set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5121         return set;
5122 }
5123
5124 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5125 {
5126         if (!model)
5127                 return NULL;
5128         return isl_set_universe(isl_space_copy(model->dim));
5129 }
5130
5131 struct isl_map *isl_map_dup(struct isl_map *map)
5132 {
5133         int i;
5134         struct isl_map *dup;
5135
5136         if (!map)
5137                 return NULL;
5138         dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5139         for (i = 0; i < map->n; ++i)
5140                 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5141         return dup;
5142 }
5143
5144 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5145                                                 __isl_take isl_basic_map *bmap)
5146 {
5147         if (!bmap || !map)
5148                 goto error;
5149         if (isl_basic_map_plain_is_empty(bmap)) {
5150                 isl_basic_map_free(bmap);
5151                 return map;
5152         }
5153         isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5154         isl_assert(map->ctx, map->n < map->size, goto error);
5155         map->p[map->n] = bmap;
5156         map->n++;
5157         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5158         return map;
5159 error:
5160         if (map)
5161                 isl_map_free(map);
5162         if (bmap)
5163                 isl_basic_map_free(bmap);
5164         return NULL;
5165 }
5166
5167 void *isl_map_free(struct isl_map *map)
5168 {
5169         int i;
5170
5171         if (!map)
5172                 return NULL;
5173
5174         if (--map->ref > 0)
5175                 return NULL;
5176
5177         isl_ctx_deref(map->ctx);
5178         for (i = 0; i < map->n; ++i)
5179                 isl_basic_map_free(map->p[i]);
5180         isl_space_free(map->dim);
5181         free(map);
5182
5183         return NULL;
5184 }
5185
5186 struct isl_map *isl_map_extend(struct isl_map *base,
5187                 unsigned nparam, unsigned n_in, unsigned n_out)
5188 {
5189         int i;
5190
5191         base = isl_map_cow(base);
5192         if (!base)
5193                 return NULL;
5194
5195         base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5196         if (!base->dim)
5197                 goto error;
5198         for (i = 0; i < base->n; ++i) {
5199                 base->p[i] = isl_basic_map_extend_space(base->p[i],
5200                                 isl_space_copy(base->dim), 0, 0, 0);
5201                 if (!base->p[i])
5202                         goto error;
5203         }
5204         return base;
5205 error:
5206         isl_map_free(base);
5207         return NULL;
5208 }
5209
5210 struct isl_set *isl_set_extend(struct isl_set *base,
5211                 unsigned nparam, unsigned dim)
5212 {
5213         return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5214                                                         nparam, 0, dim);
5215 }
5216
5217 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5218         struct isl_basic_map *bmap, unsigned pos, int value)
5219 {
5220         int j;
5221
5222         bmap = isl_basic_map_cow(bmap);
5223         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5224         j = isl_basic_map_alloc_equality(bmap);
5225         if (j < 0)
5226                 goto error;
5227         isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5228         isl_int_set_si(bmap->eq[j][pos], -1);
5229         isl_int_set_si(bmap->eq[j][0], value);
5230         bmap = isl_basic_map_simplify(bmap);
5231         return isl_basic_map_finalize(bmap);
5232 error:
5233         isl_basic_map_free(bmap);
5234         return NULL;
5235 }
5236
5237 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5238         __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5239 {
5240         int j;
5241
5242         bmap = isl_basic_map_cow(bmap);
5243         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5244         j = isl_basic_map_alloc_equality(bmap);
5245         if (j < 0)
5246                 goto error;
5247         isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5248         isl_int_set_si(bmap->eq[j][pos], -1);
5249         isl_int_set(bmap->eq[j][0], value);
5250         bmap = isl_basic_map_simplify(bmap);
5251         return isl_basic_map_finalize(bmap);
5252 error:
5253         isl_basic_map_free(bmap);
5254         return NULL;
5255 }
5256
5257 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5258                 enum isl_dim_type type, unsigned pos, int value)
5259 {
5260         if (!bmap)
5261                 return NULL;
5262         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5263         return isl_basic_map_fix_pos_si(bmap,
5264                 isl_basic_map_offset(bmap, type) + pos, value);
5265 error:
5266         isl_basic_map_free(bmap);
5267         return NULL;
5268 }
5269
5270 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5271                 enum isl_dim_type type, unsigned pos, isl_int value)
5272 {
5273         if (!bmap)
5274                 return NULL;
5275         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5276         return isl_basic_map_fix_pos(bmap,
5277                 isl_basic_map_offset(bmap, type) + pos, value);
5278 error:
5279         isl_basic_map_free(bmap);
5280         return NULL;
5281 }
5282
5283 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5284                 enum isl_dim_type type, unsigned pos, int value)
5285 {
5286         return (struct isl_basic_set *)
5287                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5288                                         type, pos, value);
5289 }
5290
5291 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5292                 enum isl_dim_type type, unsigned pos, isl_int value)
5293 {
5294         return (struct isl_basic_set *)
5295                 isl_basic_map_fix((struct isl_basic_map *)bset,
5296                                         type, pos, value);
5297 }
5298
5299 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5300                 unsigned input, int value)
5301 {
5302         return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5303 }
5304
5305 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5306                 unsigned dim, int value)
5307 {
5308         return (struct isl_basic_set *)
5309                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5310                                         isl_dim_set, dim, value);
5311 }
5312
5313 static int remove_if_empty(__isl_keep isl_map *map, int i)
5314 {
5315         int empty = isl_basic_map_plain_is_empty(map->p[i]);
5316
5317         if (empty < 0)
5318                 return -1;
5319         if (!empty)
5320                 return 0;
5321
5322         isl_basic_map_free(map->p[i]);
5323         if (i != map->n - 1) {
5324                 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5325                 map->p[i] = map->p[map->n - 1];
5326         }
5327         map->n--;
5328
5329         return 0;
5330 }
5331
5332 struct isl_map *isl_map_fix_si(struct isl_map *map,
5333                 enum isl_dim_type type, unsigned pos, int value)
5334 {
5335         int i;
5336
5337         map = isl_map_cow(map);
5338         if (!map)
5339                 return NULL;
5340
5341         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5342         for (i = map->n - 1; i >= 0; --i) {
5343                 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5344                 if (remove_if_empty(map, i) < 0)
5345                         goto error;
5346         }
5347         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5348         return map;
5349 error:
5350         isl_map_free(map);
5351         return NULL;
5352 }
5353
5354 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5355                 enum isl_dim_type type, unsigned pos, int value)
5356 {
5357         return (struct isl_set *)
5358                 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5359 }
5360
5361 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5362                 enum isl_dim_type type, unsigned pos, isl_int value)
5363 {
5364         int i;
5365
5366         map = isl_map_cow(map);
5367         if (!map)
5368                 return NULL;
5369
5370         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5371         for (i = 0; i < map->n; ++i) {
5372                 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5373                 if (!map->p[i])
5374                         goto error;
5375         }
5376         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5377         return map;
5378 error:
5379         isl_map_free(map);
5380         return NULL;
5381 }
5382
5383 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5384                 enum isl_dim_type type, unsigned pos, isl_int value)
5385 {
5386         return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5387 }
5388
5389 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5390                 unsigned input, int value)
5391 {
5392         return isl_map_fix_si(map, isl_dim_in, input, value);
5393 }
5394
5395 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5396 {
5397         return (struct isl_set *)
5398                 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5399 }
5400
5401 static __isl_give isl_basic_map *basic_map_bound_si(
5402         __isl_take isl_basic_map *bmap,
5403         enum isl_dim_type type, unsigned pos, int value, int upper)
5404 {
5405         int j;
5406
5407         if (!bmap)
5408                 return NULL;
5409         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5410         pos += isl_basic_map_offset(bmap, type);
5411         bmap = isl_basic_map_cow(bmap);
5412         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5413         j = isl_basic_map_alloc_inequality(bmap);
5414         if (j < 0)
5415                 goto error;
5416         isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5417         if (upper) {
5418                 isl_int_set_si(bmap->ineq[j][pos], -1);
5419                 isl_int_set_si(bmap->ineq[j][0], value);
5420         } else {
5421                 isl_int_set_si(bmap->ineq[j][pos], 1);
5422                 isl_int_set_si(bmap->ineq[j][0], -value);
5423         }
5424         bmap = isl_basic_map_simplify(bmap);
5425         return isl_basic_map_finalize(bmap);
5426 error:
5427         isl_basic_map_free(bmap);
5428         return NULL;
5429 }
5430
5431 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5432         __isl_take isl_basic_map *bmap,
5433         enum isl_dim_type type, unsigned pos, int value)
5434 {
5435         return basic_map_bound_si(bmap, type, pos, value, 0);
5436 }
5437
5438 /* Constrain the values of the given dimension to be no greater than "value".
5439  */
5440 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5441         __isl_take isl_basic_map *bmap,
5442         enum isl_dim_type type, unsigned pos, int value)
5443 {
5444         return basic_map_bound_si(bmap, type, pos, value, 1);
5445 }
5446
5447 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5448         unsigned dim, isl_int value)
5449 {
5450         int j;
5451
5452         bset = isl_basic_set_cow(bset);
5453         bset = isl_basic_set_extend_constraints(bset, 0, 1);
5454         j = isl_basic_set_alloc_inequality(bset);
5455         if (j < 0)
5456                 goto error;
5457         isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5458         isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5459         isl_int_neg(bset->ineq[j][0], value);
5460         bset = isl_basic_set_simplify(bset);
5461         return isl_basic_set_finalize(bset);
5462 error:
5463         isl_basic_set_free(bset);
5464         return NULL;
5465 }
5466
5467 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5468         enum isl_dim_type type, unsigned pos, int value, int upper)
5469 {
5470         int i;
5471
5472         map = isl_map_cow(map);
5473         if (!map)
5474                 return NULL;
5475
5476         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5477         for (i = 0; i < map->n; ++i) {
5478                 map->p[i] = basic_map_bound_si(map->p[i],
5479                                                  type, pos, value, upper);
5480                 if (!map->p[i])
5481                         goto error;
5482         }
5483         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5484         return map;
5485 error:
5486         isl_map_free(map);
5487         return NULL;
5488 }
5489
5490 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5491         enum isl_dim_type type, unsigned pos, int value)
5492 {
5493         return map_bound_si(map, type, pos, value, 0);
5494 }
5495
5496 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5497         enum isl_dim_type type, unsigned pos, int value)
5498 {
5499         return map_bound_si(map, type, pos, value, 1);
5500 }
5501
5502 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5503                 enum isl_dim_type type, unsigned pos, int value)
5504 {
5505         return (struct isl_set *)
5506                 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5507 }
5508
5509 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5510         enum isl_dim_type type, unsigned pos, int value)
5511 {
5512         return isl_map_upper_bound_si(set, type, pos, value);
5513 }
5514
5515 /* Bound the given variable of "bmap" from below (or above is "upper"
5516  * is set) to "value".
5517  */
5518 static __isl_give isl_basic_map *basic_map_bound(
5519         __isl_take isl_basic_map *bmap,
5520         enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5521 {
5522         int j;
5523
5524         if (!bmap)
5525                 return NULL;
5526         if (pos >= isl_basic_map_dim(bmap, type))
5527                 isl_die(bmap->ctx, isl_error_invalid,
5528                         "index out of bounds", goto error);
5529         pos += isl_basic_map_offset(bmap, type);
5530         bmap = isl_basic_map_cow(bmap);
5531         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5532         j = isl_basic_map_alloc_inequality(bmap);
5533         if (j < 0)
5534                 goto error;
5535         isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5536         if (upper) {
5537                 isl_int_set_si(bmap->ineq[j][pos], -1);
5538                 isl_int_set(bmap->ineq[j][0], value);
5539         } else {
5540                 isl_int_set_si(bmap->ineq[j][pos], 1);
5541                 isl_int_neg(bmap->ineq[j][0], value);
5542         }
5543         bmap = isl_basic_map_simplify(bmap);
5544         return isl_basic_map_finalize(bmap);
5545 error:
5546         isl_basic_map_free(bmap);
5547         return NULL;
5548 }
5549
5550 /* Bound the given variable of "map" from below (or above is "upper"
5551  * is set) to "value".
5552  */
5553 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5554         enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5555 {
5556         int i;
5557
5558         map = isl_map_cow(map);
5559         if (!map)
5560                 return NULL;
5561
5562         if (pos >= isl_map_dim(map, type))
5563                 isl_die(map->ctx, isl_error_invalid,
5564                         "index out of bounds", goto error);
5565         for (i = map->n - 1; i >= 0; --i) {
5566                 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5567                 if (remove_if_empty(map, i) < 0)
5568                         goto error;
5569         }
5570         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5571         return map;
5572 error:
5573         isl_map_free(map);
5574         return NULL;
5575 }
5576
5577 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5578         enum isl_dim_type type, unsigned pos, isl_int value)
5579 {
5580         return map_bound(map, type, pos, value, 0);
5581 }
5582
5583 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5584         enum isl_dim_type type, unsigned pos, isl_int value)
5585 {
5586         return map_bound(map, type, pos, value, 1);
5587 }
5588
5589 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5590         enum isl_dim_type type, unsigned pos, isl_int value)
5591 {
5592         return isl_map_lower_bound(set, type, pos, value);
5593 }
5594
5595 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5596         enum isl_dim_type type, unsigned pos, isl_int value)
5597 {
5598         return isl_map_upper_bound(set, type, pos, value);
5599 }
5600
5601 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5602                                         isl_int value)
5603 {
5604         int i;
5605
5606         set = isl_set_cow(set);
5607         if (!set)
5608                 return NULL;
5609
5610         isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5611         for (i = 0; i < set->n; ++i) {
5612                 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5613                 if (!set->p[i])
5614                         goto error;
5615         }
5616         return set;
5617 error:
5618         isl_set_free(set);
5619         return NULL;
5620 }
5621
5622 struct isl_map *isl_map_reverse(struct isl_map *map)
5623 {
5624         int i;
5625
5626         map = isl_map_cow(map);
5627         if (!map)
5628                 return NULL;
5629
5630         map->dim = isl_space_reverse(map->dim);
5631         if (!map->dim)
5632                 goto error;
5633         for (i = 0; i < map->n; ++i) {
5634                 map->p[i] = isl_basic_map_reverse(map->p[i]);
5635                 if (!map->p[i])
5636                         goto error;
5637         }
5638         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5639         return map;
5640 error:
5641         isl_map_free(map);
5642         return NULL;
5643 }
5644
5645 static struct isl_map *isl_basic_map_partial_lexopt(
5646                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5647                 struct isl_set **empty, int max)
5648 {
5649         if (!bmap)
5650                 goto error;
5651         if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5652                 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5653         else
5654                 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5655 error:
5656         isl_basic_map_free(bmap);
5657         isl_basic_set_free(dom);
5658         if (empty)
5659                 *empty = NULL;
5660         return NULL;
5661 }
5662
5663 struct isl_map *isl_basic_map_partial_lexmax(
5664                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5665                 struct isl_set **empty)
5666 {
5667         return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5668 }
5669
5670 struct isl_map *isl_basic_map_partial_lexmin(
5671                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5672                 struct isl_set **empty)
5673 {
5674         return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5675 }
5676
5677 struct isl_set *isl_basic_set_partial_lexmin(
5678                 struct isl_basic_set *bset, struct isl_basic_set *dom,
5679                 struct isl_set **empty)
5680 {
5681         return (struct isl_set *)
5682                 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5683                         dom, empty);
5684 }
5685
5686 struct isl_set *isl_basic_set_partial_lexmax(
5687                 struct isl_basic_set *bset, struct isl_basic_set *dom,
5688                 struct isl_set **empty)
5689 {
5690         return (struct isl_set *)
5691                 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5692                         dom, empty);
5693 }
5694
5695 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5696         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5697         __isl_give isl_set **empty)
5698 {
5699         return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5700 }
5701
5702 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5703         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5704         __isl_give isl_set **empty)
5705 {
5706         return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5707 }
5708
5709 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5710         __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5711         __isl_give isl_set **empty)
5712 {
5713         return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5714 }
5715
5716 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5717         __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5718         __isl_give isl_set **empty)
5719 {
5720         return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5721 }
5722
5723 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5724         __isl_take isl_basic_map *bmap, int max)
5725 {
5726         isl_basic_set *dom = NULL;
5727         isl_space *dom_space;
5728
5729         if (!bmap)
5730                 goto error;
5731         dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5732         dom = isl_basic_set_universe(dom_space);
5733         return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5734 error:
5735         isl_basic_map_free(bmap);
5736         return NULL;
5737 }
5738
5739 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5740         __isl_take isl_basic_map *bmap)
5741 {
5742         return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5743 }
5744
5745 #undef TYPE
5746 #define TYPE    isl_pw_multi_aff
5747 #undef SUFFIX
5748 #define SUFFIX  _pw_multi_aff
5749 #undef EMPTY
5750 #define EMPTY   isl_pw_multi_aff_empty
5751 #undef ADD
5752 #define ADD     isl_pw_multi_aff_union_add
5753 #include "isl_map_lexopt_templ.c"
5754
5755 /* Given a map "map", compute the lexicographically minimal
5756  * (or maximal) image element for each domain element in dom,
5757  * in the form of an isl_pw_multi_aff.
5758  * Set *empty to those elements in dom that do not have an image element.
5759  *
5760  * We first compute the lexicographically minimal or maximal element
5761  * in the first basic map.  This results in a partial solution "res"
5762  * and a subset "todo" of dom that still need to be handled.
5763  * We then consider each of the remaining maps in "map" and successively
5764  * update both "res" and "todo".
5765  */
5766 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
5767         __isl_take isl_map *map, __isl_take isl_set *dom,
5768         __isl_give isl_set **empty, int max)
5769 {
5770         int i;
5771         isl_pw_multi_aff *res;
5772         isl_set *todo;
5773
5774         if (!map || !dom)
5775                 goto error;
5776
5777         if (isl_map_plain_is_empty(map)) {
5778                 if (empty)
5779                         *empty = dom;
5780                 else
5781                         isl_set_free(dom);
5782                 return isl_pw_multi_aff_from_map(map);
5783         }
5784
5785         res = basic_map_partial_lexopt_pw_multi_aff(
5786                                             isl_basic_map_copy(map->p[0]),
5787                                             isl_set_copy(dom), &todo, max);
5788
5789         for (i = 1; i < map->n; ++i) {
5790                 isl_pw_multi_aff *res_i;
5791                 isl_set *todo_i;
5792
5793                 res_i = basic_map_partial_lexopt_pw_multi_aff(
5794                                             isl_basic_map_copy(map->p[i]),
5795                                             isl_set_copy(dom), &todo_i, max);
5796
5797                 if (max)
5798                         res = isl_pw_multi_aff_union_lexmax(res, res_i);
5799                 else
5800                         res = isl_pw_multi_aff_union_lexmin(res, res_i);
5801
5802                 todo = isl_set_intersect(todo, todo_i);
5803         }
5804
5805         isl_set_free(dom);
5806         isl_map_free(map);
5807
5808         if (empty)
5809                 *empty = todo;
5810         else
5811                 isl_set_free(todo);
5812
5813         return res;
5814 error:
5815         if (empty)
5816                 *empty = NULL;
5817         isl_set_free(dom);
5818         isl_map_free(map);
5819         return NULL;
5820 }
5821
5822 #undef TYPE
5823 #define TYPE    isl_map
5824 #undef SUFFIX
5825 #define SUFFIX
5826 #undef EMPTY
5827 #define EMPTY   isl_map_empty
5828 #undef ADD
5829 #define ADD     isl_map_union_disjoint
5830 #include "isl_map_lexopt_templ.c"
5831
5832 /* Given a map "map", compute the lexicographically minimal
5833  * (or maximal) image element for each domain element in dom.
5834  * Set *empty to those elements in dom that do not have an image element.
5835  *
5836  * We first compute the lexicographically minimal or maximal element
5837  * in the first basic map.  This results in a partial solution "res"
5838  * and a subset "todo" of dom that still need to be handled.
5839  * We then consider each of the remaining maps in "map" and successively
5840  * update both "res" and "todo".
5841  *
5842  * Let res^k and todo^k be the results after k steps and let i = k + 1.
5843  * Assume we are computing the lexicographical maximum.
5844  * We first compute the lexicographically maximal element in basic map i.
5845  * This results in a partial solution res_i and a subset todo_i.
5846  * Then we combine these results with those obtain for the first k basic maps
5847  * to obtain a result that is valid for the first k+1 basic maps.
5848  * In particular, the set where there is no solution is the set where
5849  * there is no solution for the first k basic maps and also no solution
5850  * for the ith basic map, i.e.,
5851  *
5852  *      todo^i = todo^k * todo_i
5853  *
5854  * On dom(res^k) * dom(res_i), we need to pick the larger of the two
5855  * solutions, arbitrarily breaking ties in favor of res^k.
5856  * That is, when res^k(a) >= res_i(a), we pick res^k and
5857  * when res^k(a) < res_i(a), we pick res_i.  (Here, ">=" and "<" denote
5858  * the lexicographic order.)
5859  * In practice, we compute
5860  *
5861  *      res^k * (res_i . "<=")
5862  *
5863  * and
5864  *
5865  *      res_i * (res^k . "<")
5866  *
5867  * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
5868  * where only one of res^k and res_i provides a solution and we simply pick
5869  * that one, i.e.,
5870  *
5871  *      res^k * todo_i
5872  * and
5873  *      res_i * todo^k
5874  *
5875  * Note that we only compute these intersections when dom(res^k) intersects
5876  * dom(res_i).  Otherwise, the only effect of these intersections is to
5877  * potentially break up res^k and res_i into smaller pieces.
5878  * We want to avoid such splintering as much as possible.
5879  * In fact, an earlier implementation of this function would look for
5880  * better results in the domain of res^k and for extra results in todo^k,
5881  * but this would always result in a splintering according to todo^k,
5882  * even when the domain of basic map i is disjoint from the domains of
5883  * the previous basic maps.
5884  */
5885 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
5886                 __isl_take isl_map *map, __isl_take isl_set *dom,
5887                 __isl_give isl_set **empty, int max)
5888 {
5889         int i;
5890         struct isl_map *res;
5891         struct isl_set *todo;
5892
5893         if (!map || !dom)
5894                 goto error;
5895
5896         if (isl_map_plain_is_empty(map)) {
5897                 if (empty)
5898                         *empty = dom;
5899                 else
5900                         isl_set_free(dom);
5901                 return map;
5902         }
5903
5904         res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
5905                                         isl_set_copy(dom), &todo, max);
5906
5907         for (i = 1; i < map->n; ++i) {
5908                 isl_map *lt, *le;
5909                 isl_map *res_i;
5910                 isl_set *todo_i;
5911                 isl_space *dim = isl_space_range(isl_map_get_space(res));
5912
5913                 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
5914                                         isl_set_copy(dom), &todo_i, max);
5915
5916                 if (max) {
5917                         lt = isl_map_lex_lt(isl_space_copy(dim));
5918                         le = isl_map_lex_le(dim);
5919                 } else {
5920                         lt = isl_map_lex_gt(isl_space_copy(dim));
5921                         le = isl_map_lex_ge(dim);
5922                 }
5923                 lt = isl_map_apply_range(isl_map_copy(res), lt);
5924                 lt = isl_map_intersect(lt, isl_map_copy(res_i));
5925                 le = isl_map_apply_range(isl_map_copy(res_i), le);
5926                 le = isl_map_intersect(le, isl_map_copy(res));
5927
5928                 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
5929                         res = isl_map_intersect_domain(res,
5930                                                         isl_set_copy(todo_i));
5931                         res_i = isl_map_intersect_domain(res_i,
5932                                                         isl_set_copy(todo));
5933                 }
5934
5935                 res = isl_map_union_disjoint(res, res_i);
5936                 res = isl_map_union_disjoint(res, lt);
5937                 res = isl_map_union_disjoint(res, le);
5938
5939                 todo = isl_set_intersect(todo, todo_i);
5940         }
5941
5942         isl_set_free(dom);
5943         isl_map_free(map);
5944
5945         if (empty)
5946                 *empty = todo;
5947         else
5948                 isl_set_free(todo);
5949
5950         return res;
5951 error:
5952         if (empty)
5953                 *empty = NULL;
5954         isl_set_free(dom);
5955         isl_map_free(map);
5956         return NULL;
5957 }
5958
5959 __isl_give isl_map *isl_map_partial_lexmax(
5960                 __isl_take isl_map *map, __isl_take isl_set *dom,
5961                 __isl_give isl_set **empty)
5962 {
5963         return isl_map_partial_lexopt(map, dom, empty, 1);
5964 }
5965
5966 __isl_give isl_map *isl_map_partial_lexmin(
5967                 __isl_take isl_map *map, __isl_take isl_set *dom,
5968                 __isl_give isl_set **empty)
5969 {
5970         return isl_map_partial_lexopt(map, dom, empty, 0);
5971 }
5972
5973 __isl_give isl_set *isl_set_partial_lexmin(
5974                 __isl_take isl_set *set, __isl_take isl_set *dom,
5975                 __isl_give isl_set **empty)
5976 {
5977         return (struct isl_set *)
5978                 isl_map_partial_lexmin((struct isl_map *)set,
5979                         dom, empty);
5980 }
5981
5982 __isl_give isl_set *isl_set_partial_lexmax(
5983                 __isl_take isl_set *set, __isl_take isl_set *dom,
5984                 __isl_give isl_set **empty)
5985 {
5986         return (struct isl_set *)
5987                 isl_map_partial_lexmax((struct isl_map *)set,
5988                         dom, empty);
5989 }
5990
5991 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
5992 {
5993         struct isl_basic_set *dom = NULL;
5994         isl_space *dom_dim;
5995
5996         if (!bmap)
5997                 goto error;
5998         dom_dim = isl_space_domain(isl_space_copy(bmap->dim));
5999         dom = isl_basic_set_universe(dom_dim);
6000         return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6001 error:
6002         isl_basic_map_free(bmap);
6003         return NULL;
6004 }
6005
6006 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6007 {
6008         return isl_basic_map_lexopt(bmap, 0);
6009 }
6010
6011 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6012 {
6013         return isl_basic_map_lexopt(bmap, 1);
6014 }
6015
6016 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6017 {
6018         return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6019 }
6020
6021 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6022 {
6023         return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6024 }
6025
6026 __isl_give isl_set *isl_set_lexmin(__isl_take isl_set *set)
6027 {
6028         return (isl_set *)isl_map_lexmin((isl_map *)set);
6029 }
6030
6031 __isl_give isl_set *isl_set_lexmax(__isl_take isl_set *set)
6032 {
6033         return (isl_set *)isl_map_lexmax((isl_map *)set);
6034 }
6035
6036 /* Extract the first and only affine expression from list
6037  * and then add it to *pwaff with the given dom.
6038  * This domain is known to be disjoint from other domains
6039  * because of the way isl_basic_map_foreach_lexmax works.
6040  */
6041 static int update_dim_opt(__isl_take isl_basic_set *dom,
6042         __isl_take isl_aff_list *list, void *user)
6043 {
6044         isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6045         isl_aff *aff;
6046         isl_pw_aff **pwaff = user;
6047         isl_pw_aff *pwaff_i;
6048
6049         if (!list)
6050                 goto error;
6051         if (isl_aff_list_n_aff(list) != 1)
6052                 isl_die(ctx, isl_error_internal,
6053                         "expecting single element list", goto error);
6054
6055         aff = isl_aff_list_get_aff(list, 0);
6056         pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6057
6058         *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6059
6060         isl_aff_list_free(list);
6061
6062         return 0;
6063 error:
6064         isl_basic_set_free(dom);
6065         isl_aff_list_free(list);
6066         return -1;
6067 }
6068
6069 /* Given a basic map with one output dimension, compute the minimum or
6070  * maximum of that dimension as an isl_pw_aff.
6071  *
6072  * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6073  * call update_dim_opt on each leaf of the result.
6074  */
6075 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6076         int max)
6077 {
6078         isl_space *dim = isl_basic_map_get_space(bmap);
6079         isl_pw_aff *pwaff;
6080         int r;
6081
6082         dim = isl_space_from_domain(isl_space_domain(dim));
6083         dim = isl_space_add_dims(dim, isl_dim_out, 1);
6084         pwaff = isl_pw_aff_empty(dim);
6085
6086         r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6087         if (r < 0)
6088                 return isl_pw_aff_free(pwaff);
6089
6090         return pwaff;
6091 }
6092
6093 /* Compute the minimum or maximum of the given output dimension
6094  * as a function of the parameters and the input dimensions,
6095  * but independently of the other output dimensions.
6096  *
6097  * We first project out the other output dimension and then compute
6098  * the "lexicographic" maximum in each basic map, combining the results
6099  * using isl_pw_aff_union_max.
6100  */
6101 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6102         int max)
6103 {
6104         int i;
6105         isl_pw_aff *pwaff;
6106         unsigned n_out;
6107
6108         n_out = isl_map_dim(map, isl_dim_out);
6109         map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6110         map = isl_map_project_out(map, isl_dim_out, 0, pos);
6111         if (!map)
6112                 return NULL;
6113
6114         if (map->n == 0) {
6115                 isl_space *dim = isl_map_get_space(map);
6116                 dim = isl_space_domain(isl_space_from_range(dim));
6117                 isl_map_free(map);
6118                 return isl_pw_aff_empty(dim);
6119         }
6120
6121         pwaff = basic_map_dim_opt(map->p[0], max);
6122         for (i = 1; i < map->n; ++i) {
6123                 isl_pw_aff *pwaff_i;
6124
6125                 pwaff_i = basic_map_dim_opt(map->p[i], max);
6126                 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6127         }
6128
6129         isl_map_free(map);
6130
6131         return pwaff;
6132 }
6133
6134 /* Compute the maximum of the given output dimension as a function of the
6135  * parameters and input dimensions, but independently of
6136  * the other output dimensions.
6137  */
6138 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6139 {
6140         return map_dim_opt(map, pos, 1);
6141 }
6142
6143 /* Compute the minimum or maximum of the given set dimension
6144  * as a function of the parameters,
6145  * but independently of the other set dimensions.
6146  */
6147 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6148         int max)
6149 {
6150         return map_dim_opt(set, pos, max);
6151 }
6152
6153 /* Compute the maximum of the given set dimension as a function of the
6154  * parameters, but independently of the other set dimensions.
6155  */
6156 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6157 {
6158         return set_dim_opt(set, pos, 1);
6159 }
6160
6161 /* Compute the minimum of the given set dimension as a function of the
6162  * parameters, but independently of the other set dimensions.
6163  */
6164 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6165 {
6166         return set_dim_opt(set, pos, 0);
6167 }
6168
6169 /* Apply a preimage specified by "mat" on the parameters of "bset".
6170  * bset is assumed to have only parameters and divs.
6171  */
6172 static struct isl_basic_set *basic_set_parameter_preimage(
6173         struct isl_basic_set *bset, struct isl_mat *mat)
6174 {
6175         unsigned nparam;
6176
6177         if (!bset || !mat)
6178                 goto error;
6179
6180         bset->dim = isl_space_cow(bset->dim);
6181         if (!bset->dim)
6182                 goto error;
6183
6184         nparam = isl_basic_set_dim(bset, isl_dim_param);
6185
6186         isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6187
6188         bset->dim->nparam = 0;
6189         bset->dim->n_out = nparam;
6190         bset = isl_basic_set_preimage(bset, mat);
6191         if (bset) {
6192                 bset->dim->nparam = bset->dim->n_out;
6193                 bset->dim->n_out = 0;
6194         }
6195         return bset;
6196 error:
6197         isl_mat_free(mat);
6198         isl_basic_set_free(bset);
6199         return NULL;
6200 }
6201
6202 /* Apply a preimage specified by "mat" on the parameters of "set".
6203  * set is assumed to have only parameters and divs.
6204  */
6205 static struct isl_set *set_parameter_preimage(
6206         struct isl_set *set, struct isl_mat *mat)
6207 {
6208         isl_space *dim = NULL;
6209         unsigned nparam;
6210
6211         if (!set || !mat)
6212                 goto error;
6213
6214         dim = isl_space_copy(set->dim);
6215         dim = isl_space_cow(dim);
6216         if (!dim)
6217                 goto error;
6218
6219         nparam = isl_set_dim(set, isl_dim_param);
6220
6221         isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6222
6223         dim->nparam = 0;
6224         dim->n_out = nparam;
6225         isl_set_reset_space(set, dim);
6226         set = isl_set_preimage(set, mat);
6227         if (!set)
6228                 goto error2;
6229         dim = isl_space_copy(set->dim);
6230         dim = isl_space_cow(dim);
6231         if (!dim)
6232                 goto error2;
6233         dim->nparam = dim->n_out;
6234         dim->n_out = 0;
6235         isl_set_reset_space(set, dim);
6236         return set;
6237 error:
6238         isl_space_free(dim);
6239         isl_mat_free(mat);
6240 error2:
6241         isl_set_free(set);
6242         return NULL;
6243 }
6244
6245 /* Intersect the basic set "bset" with the affine space specified by the
6246  * equalities in "eq".
6247  */
6248 static struct isl_basic_set *basic_set_append_equalities(
6249         struct isl_basic_set *bset, struct isl_mat *eq)
6250 {
6251         int i, k;
6252         unsigned len;
6253
6254         if (!bset || !eq)
6255                 goto error;
6256
6257         bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6258                                         eq->n_row, 0);
6259         if (!bset)
6260                 goto error;
6261
6262         len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6263         for (i = 0; i < eq->n_row; ++i) {
6264                 k = isl_basic_set_alloc_equality(bset);
6265                 if (k < 0)
6266                         goto error;
6267                 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6268                 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6269         }
6270         isl_mat_free(eq);
6271
6272         bset = isl_basic_set_gauss(bset, NULL);
6273         bset = isl_basic_set_finalize(bset);
6274
6275         return bset;
6276 error:
6277         isl_mat_free(eq);
6278         isl_basic_set_free(bset);
6279         return NULL;
6280 }
6281
6282 /* Intersect the set "set" with the affine space specified by the
6283  * equalities in "eq".
6284  */
6285 static struct isl_set *set_append_equalities(struct isl_set *set,
6286         struct isl_mat *eq)
6287 {
6288         int i;
6289
6290         if (!set || !eq)
6291                 goto error;
6292
6293         for (i = 0; i < set->n; ++i) {
6294                 set->p[i] = basic_set_append_equalities(set->p[i],
6295                                         isl_mat_copy(eq));
6296                 if (!set->p[i])
6297                         goto error;
6298         }
6299         isl_mat_free(eq);
6300         return set;
6301 error:
6302         isl_mat_free(eq);
6303         isl_set_free(set);
6304         return NULL;
6305 }
6306
6307 /* Project the given basic set onto its parameter domain, possibly introducing
6308  * new, explicit, existential variables in the constraints.
6309  * The input has parameters and (possibly implicit) existential variables.
6310  * The output has the same parameters, but only
6311  * explicit existentially quantified variables.
6312  *
6313  * The actual projection is performed by pip, but pip doesn't seem
6314  * to like equalities very much, so we first remove the equalities
6315  * among the parameters by performing a variable compression on
6316  * the parameters.  Afterward, an inverse transformation is performed
6317  * and the equalities among the parameters are inserted back in.
6318  */
6319 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6320 {
6321         int i, j;
6322         struct isl_mat *eq;
6323         struct isl_mat *T, *T2;
6324         struct isl_set *set;
6325         unsigned nparam, n_div;
6326
6327         bset = isl_basic_set_cow(bset);
6328         if (!bset)
6329                 return NULL;
6330
6331         if (bset->n_eq == 0)
6332                 return isl_basic_set_lexmin(bset);
6333
6334         isl_basic_set_gauss(bset, NULL);
6335
6336         nparam = isl_basic_set_dim(bset, isl_dim_param);
6337         n_div = isl_basic_set_dim(bset, isl_dim_div);
6338
6339         for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6340                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6341                         ++i;
6342         }
6343         if (i == bset->n_eq)
6344                 return isl_basic_set_lexmin(bset);
6345
6346         eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6347                 0, 1 + nparam);
6348         eq = isl_mat_cow(eq);
6349         T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6350         if (T && T->n_col == 0) {
6351                 isl_mat_free(T);
6352                 isl_mat_free(T2);
6353                 isl_mat_free(eq);
6354                 bset = isl_basic_set_set_to_empty(bset);
6355                 return isl_set_from_basic_set(bset);
6356         }
6357         bset = basic_set_parameter_preimage(bset, T);
6358
6359         set = isl_basic_set_lexmin(bset);
6360         set = set_parameter_preimage(set, T2);
6361         set = set_append_equalities(set, eq);
6362         return set;
6363 }
6364
6365 /* Compute an explicit representation for all the existentially
6366  * quantified variables.
6367  * The input and output dimensions are first turned into parameters.
6368  * compute_divs then returns a map with the same parameters and
6369  * no input or output dimensions and the dimension specification
6370  * is reset to that of the input.
6371  */
6372 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6373 {
6374         struct isl_basic_set *bset;
6375         struct isl_set *set;
6376         struct isl_map *map;
6377         isl_space *dim, *orig_dim = NULL;
6378         unsigned         nparam;
6379         unsigned         n_in;
6380         unsigned         n_out;
6381
6382         bmap = isl_basic_map_cow(bmap);
6383         if (!bmap)
6384                 return NULL;
6385
6386         nparam = isl_basic_map_dim(bmap, isl_dim_param);
6387         n_in = isl_basic_map_dim(bmap, isl_dim_in);
6388         n_out = isl_basic_map_dim(bmap, isl_dim_out);
6389         dim = isl_space_set_alloc(bmap->ctx, nparam + n_in + n_out, 0);
6390         if (!dim)
6391                 goto error;
6392
6393         orig_dim = bmap->dim;
6394         bmap->dim = dim;
6395         bset = (struct isl_basic_set *)bmap;
6396
6397         set = parameter_compute_divs(bset);
6398         map = (struct isl_map *)set;
6399         map = isl_map_reset_space(map, orig_dim);
6400
6401         return map;
6402 error:
6403         isl_basic_map_free(bmap);
6404         return NULL;
6405 }
6406
6407 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6408 {
6409         int i;
6410         unsigned off;
6411
6412         if (!bmap)
6413                 return -1;
6414
6415         off = isl_space_dim(bmap->dim, isl_dim_all);
6416         for (i = 0; i < bmap->n_div; ++i) {
6417                 if (isl_int_is_zero(bmap->div[i][0]))
6418                         return 0;
6419                 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6420                                 return -1);
6421         }
6422         return 1;
6423 }
6424
6425 static int map_divs_known(__isl_keep isl_map *map)
6426 {
6427         int i;
6428
6429         if (!map)
6430                 return -1;
6431
6432         for (i = 0; i < map->n; ++i) {
6433                 int known = isl_basic_map_divs_known(map->p[i]);
6434                 if (known <= 0)
6435                         return known;
6436         }
6437
6438         return 1;
6439 }
6440
6441 /* If bmap contains any unknown divs, then compute explicit
6442  * expressions for them.  However, this computation may be
6443  * quite expensive, so first try to remove divs that aren't
6444  * strictly needed.
6445  */
6446 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6447 {
6448         int known;
6449         struct isl_map *map;
6450
6451         known = isl_basic_map_divs_known(bmap);
6452         if (known < 0)
6453                 goto error;
6454         if (known)
6455                 return isl_map_from_basic_map(bmap);
6456
6457         bmap = isl_basic_map_drop_redundant_divs(bmap);
6458
6459         known = isl_basic_map_divs_known(bmap);
6460         if (known < 0)
6461                 goto error;
6462         if (known)
6463                 return isl_map_from_basic_map(bmap);
6464
6465         map = compute_divs(bmap);
6466         return map;
6467 error:
6468         isl_basic_map_free(bmap);
6469         return NULL;
6470 }
6471
6472 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6473 {
6474         int i;
6475         int known;
6476         struct isl_map *res;
6477
6478         if (!map)
6479                 return NULL;
6480         if (map->n == 0)
6481                 return map;
6482
6483         known = map_divs_known(map);
6484         if (known < 0) {
6485                 isl_map_free(map);
6486                 return NULL;
6487         }
6488         if (known)
6489                 return map;
6490
6491         res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6492         for (i = 1 ; i < map->n; ++i) {
6493                 struct isl_map *r2;
6494                 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6495                 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6496                         res = isl_map_union_disjoint(res, r2);
6497                 else
6498                         res = isl_map_union(res, r2);
6499         }
6500         isl_map_free(map);
6501
6502         return res;
6503 }
6504
6505 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6506 {
6507         return (struct isl_set *)
6508                 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6509 }
6510
6511 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6512 {
6513         return (struct isl_set *)
6514                 isl_map_compute_divs((struct isl_map *)set);
6515 }
6516
6517 struct isl_set *isl_map_domain(struct isl_map *map)
6518 {
6519         int i;
6520         struct isl_set *set;
6521
6522         if (!map)
6523                 goto error;
6524
6525         map = isl_map_cow(map);
6526         if (!map)
6527                 return NULL;
6528
6529         set = (struct isl_set *)map;
6530         set->dim = isl_space_domain(set->dim);
6531         if (!set->dim)
6532                 goto error;
6533         for (i = 0; i < map->n; ++i) {
6534                 set->p[i] = isl_basic_map_domain(map->p[i]);
6535                 if (!set->p[i])
6536                         goto error;
6537         }
6538         ISL_F_CLR(set, ISL_MAP_DISJOINT);
6539         ISL_F_CLR(set, ISL_SET_NORMALIZED);
6540         return set;
6541 error:
6542         isl_map_free(map);
6543         return NULL;
6544 }
6545
6546 /* Return the union of "map1" and "map2", where we assume for now that
6547  * "map1" and "map2" are disjoint.  Note that the basic maps inside
6548  * "map1" or "map2" may not be disjoint from each other.
6549  * Also note that this function is also called from isl_map_union,
6550  * which takes care of handling the situation where "map1" and "map2"
6551  * may not be disjoint.
6552  *
6553  * If one of the inputs is empty, we can simply return the other input.
6554  * Similarly, if one of the inputs is universal, then it is equal to the union.
6555  */
6556 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6557         __isl_take isl_map *map2)
6558 {
6559         int i;
6560         unsigned flags = 0;
6561         struct isl_map *map = NULL;
6562         int is_universe;
6563
6564         if (!map1 || !map2)
6565                 goto error;
6566
6567         if (map1->n == 0) {
6568                 isl_map_free(map1);
6569                 return map2;
6570         }
6571         if (map2->n == 0) {
6572                 isl_map_free(map2);
6573                 return map1;
6574         }
6575
6576         is_universe = isl_map_plain_is_universe(map1);
6577         if (is_universe < 0)
6578                 goto error;
6579         if (is_universe) {
6580                 isl_map_free(map2);
6581                 return map1;
6582         }
6583
6584         is_universe = isl_map_plain_is_universe(map2);
6585         if (is_universe < 0)
6586                 goto error;
6587         if (is_universe) {
6588                 isl_map_free(map1);
6589                 return map2;
6590         }
6591
6592         isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6593
6594         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6595             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6596                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6597
6598         map = isl_map_alloc_space(isl_space_copy(map1->dim),
6599                                 map1->n + map2->n, flags);
6600         if (!map)
6601                 goto error;
6602         for (i = 0; i < map1->n; ++i) {
6603                 map = isl_map_add_basic_map(map,
6604                                   isl_basic_map_copy(map1->p[i]));
6605                 if (!map)
6606                         goto error;
6607         }
6608         for (i = 0; i < map2->n; ++i) {
6609                 map = isl_map_add_basic_map(map,
6610                                   isl_basic_map_copy(map2->p[i]));
6611                 if (!map)
6612                         goto error;
6613         }
6614         isl_map_free(map1);
6615         isl_map_free(map2);
6616         return map;
6617 error:
6618         isl_map_free(map);
6619         isl_map_free(map1);
6620         isl_map_free(map2);
6621         return NULL;
6622 }
6623
6624 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
6625         __isl_take isl_map *map2)
6626 {
6627         return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
6628 }
6629
6630 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
6631 {
6632         map1 = isl_map_union_disjoint(map1, map2);
6633         if (!map1)
6634                 return NULL;
6635         if (map1->n > 1)
6636                 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
6637         return map1;
6638 }
6639
6640 struct isl_set *isl_set_union_disjoint(
6641                         struct isl_set *set1, struct isl_set *set2)
6642 {
6643         return (struct isl_set *)
6644                 isl_map_union_disjoint(
6645                         (struct isl_map *)set1, (struct isl_map *)set2);
6646 }
6647
6648 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
6649 {
6650         return (struct isl_set *)
6651                 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
6652 }
6653
6654 /* Apply "fn" to pairs of elements from "map" and "set" and collect
6655  * the results.
6656  *
6657  * "map" and "set" are assumed to be compatible and non-NULL.
6658  */
6659 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
6660         __isl_take isl_set *set,
6661         __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
6662                 __isl_take isl_basic_set *bset))
6663 {
6664         unsigned flags = 0;
6665         struct isl_map *result;
6666         int i, j;
6667
6668         if (isl_set_plain_is_universe(set)) {
6669                 isl_set_free(set);
6670                 return map;
6671         }
6672
6673         if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
6674             ISL_F_ISSET(set, ISL_MAP_DISJOINT))
6675                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6676
6677         result = isl_map_alloc_space(isl_space_copy(map->dim),
6678                                         map->n * set->n, flags);
6679         for (i = 0; result && i < map->n; ++i)
6680                 for (j = 0; j < set->n; ++j) {
6681                         result = isl_map_add_basic_map(result,
6682                                         fn(isl_basic_map_copy(map->p[i]),
6683                                             isl_basic_set_copy(set->p[j])));
6684                         if (!result)
6685                                 break;
6686                 }
6687
6688         isl_map_free(map);
6689         isl_set_free(set);
6690         return result;
6691 }
6692
6693 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
6694         __isl_take isl_set *set)
6695 {
6696         if (!map || !set)
6697                 goto error;
6698
6699         if (!isl_map_compatible_range(map, set))
6700                 isl_die(set->ctx, isl_error_invalid,
6701                         "incompatible spaces", goto error);
6702
6703         return map_intersect_set(map, set, &isl_basic_map_intersect_range);
6704 error:
6705         isl_map_free(map);
6706         isl_set_free(set);
6707         return NULL;
6708 }
6709
6710 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
6711         __isl_take isl_set *set)
6712 {
6713         return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
6714 }
6715
6716 static __isl_give isl_map *map_intersect_domain(__isl_take isl_map *map,
6717         __isl_take isl_set *set)
6718 {
6719         if (!map || !set)
6720                 goto error;
6721
6722         if (!isl_map_compatible_domain(map, set))
6723                 isl_die(set->ctx, isl_error_invalid,
6724                         "incompatible spaces", goto error);
6725
6726         return map_intersect_set(map, set, &isl_basic_map_intersect_domain);
6727 error:
6728         isl_map_free(map);
6729         isl_set_free(set);
6730         return NULL;
6731 }
6732
6733 __isl_give isl_map *isl_map_intersect_domain(__isl_take isl_map *map,
6734         __isl_take isl_set *set)
6735 {
6736         return isl_map_align_params_map_map_and(map, set,
6737                                                 &map_intersect_domain);
6738 }
6739
6740 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
6741         __isl_take isl_map *map2)
6742 {
6743         if (!map1 || !map2)
6744                 goto error;
6745         map1 = isl_map_reverse(map1);
6746         map1 = isl_map_apply_range(map1, map2);
6747         return isl_map_reverse(map1);
6748 error:
6749         isl_map_free(map1);
6750         isl_map_free(map2);
6751         return NULL;
6752 }
6753
6754 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
6755         __isl_take isl_map *map2)
6756 {
6757         return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
6758 }
6759
6760 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
6761         __isl_take isl_map *map2)
6762 {
6763         isl_space *dim_result;
6764         struct isl_map *result;
6765         int i, j;
6766
6767         if (!map1 || !map2)
6768                 goto error;
6769
6770         dim_result = isl_space_join(isl_space_copy(map1->dim),
6771                                   isl_space_copy(map2->dim));
6772
6773         result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
6774         if (!result)
6775                 goto error;
6776         for (i = 0; i < map1->n; ++i)
6777                 for (j = 0; j < map2->n; ++j) {
6778                         result = isl_map_add_basic_map(result,
6779                             isl_basic_map_apply_range(
6780                                 isl_basic_map_copy(map1->p[i]),
6781                                 isl_basic_map_copy(map2->p[j])));
6782                         if (!result)
6783                                 goto error;
6784                 }
6785         isl_map_free(map1);
6786         isl_map_free(map2);
6787         if (result && result->n <= 1)
6788                 ISL_F_SET(result, ISL_MAP_DISJOINT);
6789         return result;
6790 error:
6791         isl_map_free(map1);
6792         isl_map_free(map2);
6793         return NULL;
6794 }
6795
6796 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
6797         __isl_take isl_map *map2)
6798 {
6799         return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
6800 }
6801
6802 /*
6803  * returns range - domain
6804  */
6805 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
6806 {
6807         isl_space *dims, *target_dim;
6808         struct isl_basic_set *bset;
6809         unsigned dim;
6810         unsigned nparam;
6811         int i;
6812
6813         if (!bmap)
6814                 goto error;
6815         isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
6816                                                   bmap->dim, isl_dim_out),
6817                    goto error);
6818         target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
6819         dim = isl_basic_map_n_in(bmap);
6820         nparam = isl_basic_map_n_param(bmap);
6821         bset = isl_basic_set_from_basic_map(bmap);
6822         bset = isl_basic_set_cow(bset);
6823         dims = isl_basic_set_get_space(bset);
6824         dims = isl_space_add_dims(dims, isl_dim_set, dim);
6825         bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
6826         bset = isl_basic_set_swap_vars(bset, 2*dim);
6827         for (i = 0; i < dim; ++i) {
6828                 int j = isl_basic_map_alloc_equality(
6829                                             (struct isl_basic_map *)bset);
6830                 if (j < 0) {
6831                         bset = isl_basic_set_free(bset);
6832                         break;
6833                 }
6834                 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
6835                 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
6836                 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
6837                 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
6838         }
6839         bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
6840         bset = isl_basic_set_reset_space(bset, target_dim);
6841         return bset;
6842 error:
6843         isl_basic_map_free(bmap);
6844         return NULL;
6845 }
6846
6847 /*
6848  * returns range - domain
6849  */
6850 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
6851 {
6852         int i;
6853         isl_space *dim;
6854         struct isl_set *result;
6855
6856         if (!map)
6857                 return NULL;
6858
6859         isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
6860                                                  map->dim, isl_dim_out),
6861                    goto error);
6862         dim = isl_map_get_space(map);
6863         dim = isl_space_domain(dim);
6864         result = isl_set_alloc_space(dim, map->n, 0);
6865         if (!result)
6866                 goto error;
6867         for (i = 0; i < map->n; ++i)
6868                 result = isl_set_add_basic_set(result,
6869                           isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
6870         isl_map_free(map);
6871         return result;
6872 error:
6873         isl_map_free(map);
6874         return NULL;
6875 }
6876
6877 /*
6878  * returns [domain -> range] -> range - domain
6879  */
6880 __isl_give isl_basic_map *isl_basic_map_deltas_map(
6881         __isl_take isl_basic_map *bmap)
6882 {
6883         int i, k;
6884         isl_space *dim;
6885         isl_basic_map *domain;
6886         int nparam, n;
6887         unsigned total;
6888
6889         if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
6890                 isl_die(bmap->ctx, isl_error_invalid,
6891                         "domain and range don't match", goto error);
6892
6893         nparam = isl_basic_map_dim(bmap, isl_dim_param);
6894         n = isl_basic_map_dim(bmap, isl_dim_in);
6895
6896         dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
6897         domain = isl_basic_map_universe(dim);
6898
6899         bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
6900         bmap = isl_basic_map_apply_range(bmap, domain);
6901         bmap = isl_basic_map_extend_constraints(bmap, n, 0);
6902
6903         total = isl_basic_map_total_dim(bmap);
6904
6905         for (i = 0; i < n; ++i) {
6906                 k = isl_basic_map_alloc_equality(bmap);
6907                 if (k < 0)
6908                         goto error;
6909                 isl_seq_clr(bmap->eq[k], 1 + total);
6910                 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
6911                 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
6912                 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
6913         }
6914
6915         bmap = isl_basic_map_gauss(bmap, NULL);
6916         return isl_basic_map_finalize(bmap);
6917 error:
6918         isl_basic_map_free(bmap);
6919         return NULL;
6920 }
6921
6922 /*
6923  * returns [domain -> range] -> range - domain
6924  */
6925 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
6926 {
6927         int i;
6928         isl_space *domain_dim;
6929
6930         if (!map)
6931                 return NULL;
6932
6933         if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
6934                 isl_die(map->ctx, isl_error_invalid,
6935                         "domain and range don't match", goto error);
6936
6937         map = isl_map_cow(map);
6938         if (!map)
6939                 return NULL;
6940
6941         domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
6942         map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
6943         map->dim = isl_space_join(map->dim, domain_dim);
6944         if (!map->dim)
6945                 goto error;
6946         for (i = 0; i < map->n; ++i) {
6947                 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
6948                 if (!map->p[i])
6949                         goto error;
6950         }
6951         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
6952         return map;
6953 error:
6954         isl_map_free(map);
6955         return NULL;
6956 }
6957
6958 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
6959 {
6960         struct isl_basic_map *bmap;
6961         unsigned nparam;
6962         unsigned dim;
6963         int i;
6964
6965         if (!dims)
6966                 return NULL;
6967
6968         nparam = dims->nparam;
6969         dim = dims->n_out;
6970         bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
6971         if (!bmap)
6972                 goto error;
6973
6974         for (i = 0; i < dim; ++i) {
6975                 int j = isl_basic_map_alloc_equality(bmap);
6976                 if (j < 0)
6977                         goto error;
6978                 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
6979                 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
6980                 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
6981         }
6982         return isl_basic_map_finalize(bmap);
6983 error:
6984         isl_basic_map_free(bmap);
6985         return NULL;
6986 }
6987
6988 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
6989 {
6990         if (!dim)
6991                 return NULL;
6992         if (dim->n_in != dim->n_out)
6993                 isl_die(dim->ctx, isl_error_invalid,
6994                         "number of input and output dimensions needs to be "
6995                         "the same", goto error);
6996         return basic_map_identity(dim);
6997 error:
6998         isl_space_free(dim);
6999         return NULL;
7000 }
7001
7002 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7003 {
7004         if (!model || !model->dim)
7005                 return NULL;
7006         return isl_basic_map_identity(isl_space_copy(model->dim));
7007 }
7008
7009 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7010 {
7011         return isl_map_from_basic_map(isl_basic_map_identity(dim));
7012 }
7013
7014 struct isl_map *isl_map_identity_like(struct isl_map *model)
7015 {
7016         if (!model || !model->dim)
7017                 return NULL;
7018         return isl_map_identity(isl_space_copy(model->dim));
7019 }
7020
7021 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7022 {
7023         if (!model || !model->dim)
7024                 return NULL;
7025         return isl_map_identity(isl_space_copy(model->dim));
7026 }
7027
7028 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7029 {
7030         isl_space *dim = isl_set_get_space(set);
7031         isl_map *id;
7032         id = isl_map_identity(isl_space_map_from_set(dim));
7033         return isl_map_intersect_range(id, set);
7034 }
7035
7036 /* Construct a basic set with all set dimensions having only non-negative
7037  * values.
7038  */
7039 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7040         __isl_take isl_space *space)
7041 {
7042         int i;
7043         unsigned nparam;
7044         unsigned dim;
7045         struct isl_basic_set *bset;
7046
7047         if (!space)
7048                 return NULL;
7049         nparam = space->nparam;
7050         dim = space->n_out;
7051         bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7052         if (!bset)
7053                 return NULL;
7054         for (i = 0; i < dim; ++i) {
7055                 int k = isl_basic_set_alloc_inequality(bset);
7056                 if (k < 0)
7057                         goto error;
7058                 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7059                 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7060         }
7061         return bset;
7062 error:
7063         isl_basic_set_free(bset);
7064         return NULL;
7065 }
7066
7067 /* Construct the half-space x_pos >= 0.
7068  */
7069 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7070         int pos)
7071 {
7072         int k;
7073         isl_basic_set *nonneg;
7074
7075         nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7076         k = isl_basic_set_alloc_inequality(nonneg);
7077         if (k < 0)
7078                 goto error;
7079         isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7080         isl_int_set_si(nonneg->ineq[k][pos], 1);
7081
7082         return isl_basic_set_finalize(nonneg);
7083 error:
7084         isl_basic_set_free(nonneg);
7085         return NULL;
7086 }
7087
7088 /* Construct the half-space x_pos <= -1.
7089  */
7090 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7091 {
7092         int k;
7093         isl_basic_set *neg;
7094
7095         neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7096         k = isl_basic_set_alloc_inequality(neg);
7097         if (k < 0)
7098                 goto error;
7099         isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7100         isl_int_set_si(neg->ineq[k][0], -1);
7101         isl_int_set_si(neg->ineq[k][pos], -1);
7102
7103         return isl_basic_set_finalize(neg);
7104 error:
7105         isl_basic_set_free(neg);
7106         return NULL;
7107 }
7108
7109 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7110         enum isl_dim_type type, unsigned first, unsigned n)
7111 {
7112         int i;
7113         isl_basic_set *nonneg;
7114         isl_basic_set *neg;
7115
7116         if (!set)
7117                 return NULL;
7118         if (n == 0)
7119                 return set;
7120
7121         isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7122
7123         for (i = 0; i < n; ++i) {
7124                 nonneg = nonneg_halfspace(isl_set_get_space(set),
7125                                           pos(set->dim, type) + first + i);
7126                 neg = neg_halfspace(isl_set_get_space(set),
7127                                           pos(set->dim, type) + first + i);
7128
7129                 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7130         }
7131
7132         return set;
7133 error:
7134         isl_set_free(set);
7135         return NULL;
7136 }
7137
7138 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7139         int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7140         void *user)
7141 {
7142         isl_set *half;
7143
7144         if (!set)
7145                 return -1;
7146         if (isl_set_plain_is_empty(set)) {
7147                 isl_set_free(set);
7148                 return 0;
7149         }
7150         if (first == len)
7151                 return fn(set, signs, user);
7152
7153         signs[first] = 1;
7154         half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7155                                                         1 + first));
7156         half = isl_set_intersect(half, isl_set_copy(set));
7157         if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7158                 goto error;
7159
7160         signs[first] = -1;
7161         half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7162                                                         1 + first));
7163         half = isl_set_intersect(half, set);
7164         return foreach_orthant(half, signs, first + 1, len, fn, user);
7165 error:
7166         isl_set_free(set);
7167         return -1;
7168 }
7169
7170 /* Call "fn" on the intersections of "set" with each of the orthants
7171  * (except for obviously empty intersections).  The orthant is identified
7172  * by the signs array, with each entry having value 1 or -1 according
7173  * to the sign of the corresponding variable.
7174  */
7175 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7176         int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7177         void *user)
7178 {
7179         unsigned nparam;
7180         unsigned nvar;
7181         int *signs;
7182         int r;
7183
7184         if (!set)
7185                 return -1;
7186         if (isl_set_plain_is_empty(set))
7187                 return 0;
7188
7189         nparam = isl_set_dim(set, isl_dim_param);
7190         nvar = isl_set_dim(set, isl_dim_set);
7191
7192         signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7193
7194         r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7195                             fn, user);
7196
7197         free(signs);
7198
7199         return r;
7200 }
7201
7202 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7203 {
7204         return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7205 }
7206
7207 int isl_basic_map_is_subset(
7208                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7209 {
7210         int is_subset;
7211         struct isl_map *map1;
7212         struct isl_map *map2;
7213
7214         if (!bmap1 || !bmap2)
7215                 return -1;
7216
7217         map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7218         map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7219
7220         is_subset = isl_map_is_subset(map1, map2);
7221
7222         isl_map_free(map1);
7223         isl_map_free(map2);
7224
7225         return is_subset;
7226 }
7227
7228 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7229         __isl_keep isl_basic_set *bset2)
7230 {
7231         return isl_basic_map_is_subset(bset1, bset2);
7232 }
7233
7234 int isl_basic_map_is_equal(
7235                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7236 {
7237         int is_subset;
7238
7239         if (!bmap1 || !bmap2)
7240                 return -1;
7241         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7242         if (is_subset != 1)
7243                 return is_subset;
7244         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7245         return is_subset;
7246 }
7247
7248 int isl_basic_set_is_equal(
7249                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7250 {
7251         return isl_basic_map_is_equal(
7252                 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7253 }
7254
7255 int isl_map_is_empty(struct isl_map *map)
7256 {
7257         int i;
7258         int is_empty;
7259
7260         if (!map)
7261                 return -1;
7262         for (i = 0; i < map->n; ++i) {
7263                 is_empty = isl_basic_map_is_empty(map->p[i]);
7264                 if (is_empty < 0)
7265                         return -1;
7266                 if (!is_empty)
7267                         return 0;
7268         }
7269         return 1;
7270 }
7271
7272 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7273 {
7274         return map ? map->n == 0 : -1;
7275 }
7276
7277 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7278 {
7279         return isl_map_plain_is_empty(map);
7280 }
7281
7282 int isl_set_plain_is_empty(struct isl_set *set)
7283 {
7284         return set ? set->n == 0 : -1;
7285 }
7286
7287 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7288 {
7289         return isl_set_plain_is_empty(set);
7290 }
7291
7292 int isl_set_is_empty(struct isl_set *set)
7293 {
7294         return isl_map_is_empty((struct isl_map *)set);
7295 }
7296
7297 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7298 {
7299         if (!map1 || !map2)
7300                 return -1;
7301
7302         return isl_space_is_equal(map1->dim, map2->dim);
7303 }
7304
7305 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7306 {
7307         if (!set1 || !set2)
7308                 return -1;
7309
7310         return isl_space_is_equal(set1->dim, set2->dim);
7311 }
7312
7313 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7314 {
7315         int is_subset;
7316
7317         if (!map1 || !map2)
7318                 return -1;
7319         is_subset = isl_map_is_subset(map1, map2);
7320         if (is_subset != 1)
7321                 return is_subset;
7322         is_subset = isl_map_is_subset(map2, map1);
7323         return is_subset;
7324 }
7325
7326 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7327 {
7328         return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7329 }
7330
7331 int isl_basic_map_is_strict_subset(
7332                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7333 {
7334         int is_subset;
7335
7336         if (!bmap1 || !bmap2)
7337                 return -1;
7338         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7339         if (is_subset != 1)
7340                 return is_subset;
7341         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7342         if (is_subset == -1)
7343                 return is_subset;
7344         return !is_subset;
7345 }
7346
7347 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7348 {
7349         int is_subset;
7350
7351         if (!map1 || !map2)
7352                 return -1;
7353         is_subset = isl_map_is_subset(map1, map2);
7354         if (is_subset != 1)
7355                 return is_subset;
7356         is_subset = isl_map_is_subset(map2, map1);
7357         if (is_subset == -1)
7358                 return is_subset;
7359         return !is_subset;
7360 }
7361
7362 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7363 {
7364         return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7365 }
7366
7367 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7368 {
7369         if (!bmap)
7370                 return -1;
7371         return bmap->n_eq == 0 && bmap->n_ineq == 0;
7372 }
7373
7374 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7375 {
7376         if (!bset)
7377                 return -1;
7378         return bset->n_eq == 0 && bset->n_ineq == 0;
7379 }
7380
7381 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7382 {
7383         int i;
7384
7385         if (!map)
7386                 return -1;
7387
7388         for (i = 0; i < map->n; ++i) {
7389                 int r = isl_basic_map_is_universe(map->p[i]);
7390                 if (r < 0 || r)
7391                         return r;
7392         }
7393
7394         return 0;
7395 }
7396
7397 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7398 {
7399         return isl_map_plain_is_universe((isl_map *) set);
7400 }
7401
7402 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7403 {
7404         return isl_set_plain_is_universe(set);
7405 }
7406
7407 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7408 {
7409         struct isl_basic_set *bset = NULL;
7410         struct isl_vec *sample = NULL;
7411         int empty;
7412         unsigned total;
7413
7414         if (!bmap)
7415                 return -1;
7416
7417         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7418                 return 1;
7419
7420         if (isl_basic_map_is_universe(bmap))
7421                 return 0;
7422
7423         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7424                 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7425                 copy = isl_basic_map_remove_redundancies(copy);
7426                 empty = isl_basic_map_plain_is_empty(copy);
7427                 isl_basic_map_free(copy);
7428                 return empty;
7429         }
7430
7431         total = 1 + isl_basic_map_total_dim(bmap);
7432         if (bmap->sample && bmap->sample->size == total) {
7433                 int contains = isl_basic_map_contains(bmap, bmap->sample);
7434                 if (contains < 0)
7435                         return -1;
7436                 if (contains)
7437                         return 0;
7438         }
7439         isl_vec_free(bmap->sample);
7440         bmap->sample = NULL;
7441         bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7442         if (!bset)
7443                 return -1;
7444         sample = isl_basic_set_sample_vec(bset);
7445         if (!sample)
7446                 return -1;
7447         empty = sample->size == 0;
7448         isl_vec_free(bmap->sample);
7449         bmap->sample = sample;
7450         if (empty)
7451                 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7452
7453         return empty;
7454 }
7455
7456 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7457 {
7458         if (!bmap)
7459                 return -1;
7460         return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7461 }
7462
7463 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7464 {
7465         return isl_basic_map_plain_is_empty(bmap);
7466 }
7467
7468 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7469 {
7470         if (!bset)
7471                 return -1;
7472         return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7473 }
7474
7475 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7476 {
7477         return isl_basic_set_plain_is_empty(bset);
7478 }
7479
7480 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7481 {
7482         return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7483 }
7484
7485 struct isl_map *isl_basic_map_union(
7486         struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7487 {
7488         struct isl_map *map;
7489         if (!bmap1 || !bmap2)
7490                 goto error;
7491
7492         isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7493
7494         map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7495         if (!map)
7496                 goto error;
7497         map = isl_map_add_basic_map(map, bmap1);
7498         map = isl_map_add_basic_map(map, bmap2);
7499         return map;
7500 error:
7501         isl_basic_map_free(bmap1);
7502         isl_basic_map_free(bmap2);
7503         return NULL;
7504 }
7505
7506 struct isl_set *isl_basic_set_union(
7507                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7508 {
7509         return (struct isl_set *)isl_basic_map_union(
7510                                             (struct isl_basic_map *)bset1,
7511                                             (struct isl_basic_map *)bset2);
7512 }
7513
7514 /* Order divs such that any div only depends on previous divs */
7515 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7516 {
7517         int i;
7518         unsigned off;
7519
7520         if (!bmap)
7521                 return NULL;
7522
7523         off = isl_space_dim(bmap->dim, isl_dim_all);
7524
7525         for (i = 0; i < bmap->n_div; ++i) {
7526                 int pos;
7527                 if (isl_int_is_zero(bmap->div[i][0]))
7528                         continue;
7529                 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7530                                                             bmap->n_div-i);
7531                 if (pos == -1)
7532                         continue;
7533                 isl_basic_map_swap_div(bmap, i, i + pos);
7534                 --i;
7535         }
7536         return bmap;
7537 }
7538
7539 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7540 {
7541         return (struct isl_basic_set *)
7542                 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7543 }
7544
7545 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7546 {
7547         int i;
7548
7549         if (!map)
7550                 return 0;
7551
7552         for (i = 0; i < map->n; ++i) {
7553                 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7554                 if (!map->p[i])
7555                         goto error;
7556         }
7557
7558         return map;
7559 error:
7560         isl_map_free(map);
7561         return NULL;
7562 }
7563
7564 /* Apply the expansion computed by isl_merge_divs.
7565  * The expansion itself is given by "exp" while the resulting
7566  * list of divs is given by "div".
7567  */
7568 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7569         __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7570 {
7571         int i, j;
7572         int n_div;
7573
7574         bset = isl_basic_set_cow(bset);
7575         if (!bset || !div)
7576                 goto error;
7577
7578         if (div->n_row < bset->n_div)
7579                 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7580                         "not an expansion", goto error);
7581
7582         n_div = bset->n_div;
7583         bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
7584                                             div->n_row - n_div, 0,
7585                                             2 * (div->n_row - n_div));
7586
7587         for (i = n_div; i < div->n_row; ++i)
7588                 if (isl_basic_set_alloc_div(bset) < 0)
7589                         goto error;
7590
7591         j = n_div - 1;
7592         for (i = div->n_row - 1; i >= 0; --i) {
7593                 if (j >= 0 && exp[j] == i) {
7594                         if (i != j)
7595                                 isl_basic_map_swap_div(bset, i, j);
7596                         j--;
7597                 } else {
7598                         isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
7599                         if (isl_basic_map_add_div_constraints(bset, i) < 0)
7600                                 goto error;
7601                 }
7602         }
7603
7604         isl_mat_free(div);
7605         return bset;
7606 error:
7607         isl_basic_set_free(bset);
7608         isl_mat_free(div);
7609         return NULL;
7610 }
7611
7612 /* Look for a div in dst that corresponds to the div "div" in src.
7613  * The divs before "div" in src and dst are assumed to be the same.
7614  * 
7615  * Returns -1 if no corresponding div was found and the position
7616  * of the corresponding div in dst otherwise.
7617  */
7618 static int find_div(struct isl_basic_map *dst,
7619                         struct isl_basic_map *src, unsigned div)
7620 {
7621         int i;
7622
7623         unsigned total = isl_space_dim(src->dim, isl_dim_all);
7624
7625         isl_assert(dst->ctx, div <= dst->n_div, return -1);
7626         for (i = div; i < dst->n_div; ++i)
7627                 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
7628                     isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
7629                                                 dst->n_div - div) == -1)
7630                         return i;
7631         return -1;
7632 }
7633
7634 struct isl_basic_map *isl_basic_map_align_divs(
7635                 struct isl_basic_map *dst, struct isl_basic_map *src)
7636 {
7637         int i;
7638         unsigned total;
7639
7640         if (!dst || !src)
7641                 goto error;
7642
7643         if (src->n_div == 0)
7644                 return dst;
7645
7646         for (i = 0; i < src->n_div; ++i)
7647                 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
7648
7649         src = isl_basic_map_order_divs(src);
7650         dst = isl_basic_map_cow(dst);
7651         dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
7652                         src->n_div, 0, 2 * src->n_div);
7653         if (!dst)
7654                 return NULL;
7655         total = isl_space_dim(src->dim, isl_dim_all);
7656         for (i = 0; i < src->n_div; ++i) {
7657                 int j = find_div(dst, src, i);
7658                 if (j < 0) {
7659                         j = isl_basic_map_alloc_div(dst);
7660                         if (j < 0)
7661                                 goto error;
7662                         isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
7663                         isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
7664                         if (isl_basic_map_add_div_constraints(dst, j) < 0)
7665                                 goto error;
7666                 }
7667                 if (j != i)
7668                         isl_basic_map_swap_div(dst, i, j);
7669         }
7670         return dst;
7671 error:
7672         isl_basic_map_free(dst);
7673         return NULL;
7674 }
7675
7676 struct isl_basic_set *isl_basic_set_align_divs(
7677                 struct isl_basic_set *dst, struct isl_basic_set *src)
7678 {
7679         return (struct isl_basic_set *)isl_basic_map_align_divs(
7680                 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
7681 }
7682
7683 struct isl_map *isl_map_align_divs(struct isl_map *map)
7684 {
7685         int i;
7686
7687         if (!map)
7688                 return NULL;
7689         if (map->n == 0)
7690                 return map;
7691         map = isl_map_compute_divs(map);
7692         map = isl_map_cow(map);
7693         if (!map)
7694                 return NULL;
7695
7696         for (i = 1; i < map->n; ++i)
7697                 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
7698         for (i = 1; i < map->n; ++i)
7699                 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
7700
7701         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7702         return map;
7703 }
7704
7705 struct isl_set *isl_set_align_divs(struct isl_set *set)
7706 {
7707         return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
7708 }
7709
7710 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
7711         __isl_take isl_map *map)
7712 {
7713         if (!set || !map)
7714                 goto error;
7715         isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
7716         map = isl_map_intersect_domain(map, set);
7717         set = isl_map_range(map);
7718         return set;
7719 error:
7720         isl_set_free(set);
7721         isl_map_free(map);
7722         return NULL;
7723 }
7724
7725 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
7726         __isl_take isl_map *map)
7727 {
7728         return isl_map_align_params_map_map_and(set, map, &set_apply);
7729 }
7730
7731 /* There is no need to cow as removing empty parts doesn't change
7732  * the meaning of the set.
7733  */
7734 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
7735 {
7736         int i;
7737
7738         if (!map)
7739                 return NULL;
7740
7741         for (i = map->n - 1; i >= 0; --i)
7742                 remove_if_empty(map, i);
7743
7744         return map;
7745 }
7746
7747 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
7748 {
7749         return (struct isl_set *)
7750                 isl_map_remove_empty_parts((struct isl_map *)set);
7751 }
7752
7753 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
7754 {
7755         struct isl_basic_map *bmap;
7756         if (!map || map->n == 0)
7757                 return NULL;
7758         bmap = map->p[map->n-1];
7759         isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
7760         return isl_basic_map_copy(bmap);
7761 }
7762
7763 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
7764 {
7765         return (struct isl_basic_set *)
7766                 isl_map_copy_basic_map((struct isl_map *)set);
7767 }
7768
7769 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
7770                                                 __isl_keep isl_basic_map *bmap)
7771 {
7772         int i;
7773
7774         if (!map || !bmap)
7775                 goto error;
7776         for (i = map->n-1; i >= 0; --i) {
7777                 if (map->p[i] != bmap)
7778                         continue;
7779                 map = isl_map_cow(map);
7780                 if (!map)
7781                         goto error;
7782                 isl_basic_map_free(map->p[i]);
7783                 if (i != map->n-1) {
7784                         ISL_F_CLR(map, ISL_SET_NORMALIZED);
7785                         map->p[i] = map->p[map->n-1];
7786                 }
7787                 map->n--;
7788                 return map;
7789         }
7790         return map;
7791 error:
7792         isl_map_free(map);
7793         return NULL;
7794 }
7795
7796 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
7797                                                 struct isl_basic_set *bset)
7798 {
7799         return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
7800                                                 (struct isl_basic_map *)bset);
7801 }
7802
7803 /* Given two basic sets bset1 and bset2, compute the maximal difference
7804  * between the values of dimension pos in bset1 and those in bset2
7805  * for any common value of the parameters and dimensions preceding pos.
7806  */
7807 static enum isl_lp_result basic_set_maximal_difference_at(
7808         __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
7809         int pos, isl_int *opt)
7810 {
7811         isl_space *dims;
7812         struct isl_basic_map *bmap1 = NULL;
7813         struct isl_basic_map *bmap2 = NULL;
7814         struct isl_ctx *ctx;
7815         struct isl_vec *obj;
7816         unsigned total;
7817         unsigned nparam;
7818         unsigned dim1, dim2;
7819         enum isl_lp_result res;
7820
7821         if (!bset1 || !bset2)
7822                 return isl_lp_error;
7823
7824         nparam = isl_basic_set_n_param(bset1);
7825         dim1 = isl_basic_set_n_dim(bset1);
7826         dim2 = isl_basic_set_n_dim(bset2);
7827         dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
7828         bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
7829         dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
7830         bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
7831         if (!bmap1 || !bmap2)
7832                 goto error;
7833         bmap1 = isl_basic_map_cow(bmap1);
7834         bmap1 = isl_basic_map_extend(bmap1, nparam,
7835                         pos, (dim1 - pos) + (dim2 - pos),
7836                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
7837         bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
7838         if (!bmap1)
7839                 goto error;
7840         total = isl_basic_map_total_dim(bmap1);
7841         ctx = bmap1->ctx;
7842         obj = isl_vec_alloc(ctx, 1 + total);
7843         if (!obj)
7844                 goto error2;
7845         isl_seq_clr(obj->block.data, 1 + total);
7846         isl_int_set_si(obj->block.data[1+nparam+pos], 1);
7847         isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
7848         res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
7849                                         opt, NULL, NULL);
7850         isl_basic_map_free(bmap1);
7851         isl_vec_free(obj);
7852         return res;
7853 error:
7854         isl_basic_map_free(bmap2);
7855 error2:
7856         isl_basic_map_free(bmap1);
7857         return isl_lp_error;
7858 }
7859
7860 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
7861  * for any common value of the parameters and dimensions preceding pos
7862  * in both basic sets, the values of dimension pos in bset1 are
7863  * smaller or larger than those in bset2.
7864  *
7865  * Returns
7866  *       1 if bset1 follows bset2
7867  *      -1 if bset1 precedes bset2
7868  *       0 if bset1 and bset2 are incomparable
7869  *      -2 if some error occurred.
7870  */
7871 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
7872         struct isl_basic_set *bset2, int pos)
7873 {
7874         isl_int opt;
7875         enum isl_lp_result res;
7876         int cmp;
7877
7878         isl_int_init(opt);
7879
7880         res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7881
7882         if (res == isl_lp_empty)
7883                 cmp = 0;
7884         else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7885                   res == isl_lp_unbounded)
7886                 cmp = 1;
7887         else if (res == isl_lp_ok && isl_int_is_neg(opt))
7888                 cmp = -1;
7889         else
7890                 cmp = -2;
7891
7892         isl_int_clear(opt);
7893         return cmp;
7894 }
7895
7896 /* Given two basic sets bset1 and bset2, check whether
7897  * for any common value of the parameters and dimensions preceding pos
7898  * there is a value of dimension pos in bset1 that is larger
7899  * than a value of the same dimension in bset2.
7900  *
7901  * Return
7902  *       1 if there exists such a pair
7903  *       0 if there is no such pair, but there is a pair of equal values
7904  *      -1 otherwise
7905  *      -2 if some error occurred.
7906  */
7907 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
7908         __isl_keep isl_basic_set *bset2, int pos)
7909 {
7910         isl_int opt;
7911         enum isl_lp_result res;
7912         int cmp;
7913
7914         isl_int_init(opt);
7915
7916         res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
7917
7918         if (res == isl_lp_empty)
7919                 cmp = -1;
7920         else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
7921                   res == isl_lp_unbounded)
7922                 cmp = 1;
7923         else if (res == isl_lp_ok && isl_int_is_neg(opt))
7924                 cmp = -1;
7925         else if (res == isl_lp_ok)
7926                 cmp = 0;
7927         else
7928                 cmp = -2;
7929
7930         isl_int_clear(opt);
7931         return cmp;
7932 }
7933
7934 /* Given two sets set1 and set2, check whether
7935  * for any common value of the parameters and dimensions preceding pos
7936  * there is a value of dimension pos in set1 that is larger
7937  * than a value of the same dimension in set2.
7938  *
7939  * Return
7940  *       1 if there exists such a pair
7941  *       0 if there is no such pair, but there is a pair of equal values
7942  *      -1 otherwise
7943  *      -2 if some error occurred.
7944  */
7945 int isl_set_follows_at(__isl_keep isl_set *set1,
7946         __isl_keep isl_set *set2, int pos)
7947 {
7948         int i, j;
7949         int follows = -1;
7950
7951         if (!set1 || !set2)
7952                 return -2;
7953
7954         for (i = 0; i < set1->n; ++i)
7955                 for (j = 0; j < set2->n; ++j) {
7956                         int f;
7957                         f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
7958                         if (f == 1 || f == -2)
7959                                 return f;
7960                         if (f > follows)
7961                                 follows = f;
7962                 }
7963
7964         return follows;
7965 }
7966
7967 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
7968         unsigned pos, isl_int *val)
7969 {
7970         int i;
7971         int d;
7972         unsigned total;
7973
7974         if (!bmap)
7975                 return -1;
7976         total = isl_basic_map_total_dim(bmap);
7977         for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
7978                 for (; d+1 > pos; --d)
7979                         if (!isl_int_is_zero(bmap->eq[i][1+d]))
7980                                 break;
7981                 if (d != pos)
7982                         continue;
7983                 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
7984                         return 0;
7985                 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
7986                         return 0;
7987                 if (!isl_int_is_one(bmap->eq[i][1+d]))
7988                         return 0;
7989                 if (val)
7990                         isl_int_neg(*val, bmap->eq[i][0]);
7991                 return 1;
7992         }
7993         return 0;
7994 }
7995
7996 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
7997         unsigned pos, isl_int *val)
7998 {
7999         int i;
8000         isl_int v;
8001         isl_int tmp;
8002         int fixed;
8003
8004         if (!map)
8005                 return -1;
8006         if (map->n == 0)
8007                 return 0;
8008         if (map->n == 1)
8009                 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val); 
8010         isl_int_init(v);
8011         isl_int_init(tmp);
8012         fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v); 
8013         for (i = 1; fixed == 1 && i < map->n; ++i) {
8014                 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp); 
8015                 if (fixed == 1 && isl_int_ne(tmp, v))
8016                         fixed = 0;
8017         }
8018         if (val)
8019                 isl_int_set(*val, v);
8020         isl_int_clear(tmp);
8021         isl_int_clear(v);
8022         return fixed;
8023 }
8024
8025 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8026         unsigned pos, isl_int *val)
8027 {
8028         return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8029                                                 pos, val);
8030 }
8031
8032 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8033         isl_int *val)
8034 {
8035         return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8036 }
8037
8038 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8039         enum isl_dim_type type, unsigned pos, isl_int *val)
8040 {
8041         if (pos >= isl_basic_map_dim(bmap, type))
8042                 return -1;
8043         return isl_basic_map_plain_has_fixed_var(bmap,
8044                 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8045 }
8046
8047 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8048         enum isl_dim_type type, unsigned pos, isl_int *val)
8049 {
8050         if (pos >= isl_map_dim(map, type))
8051                 return -1;
8052         return isl_map_plain_has_fixed_var(map,
8053                 map_offset(map, type) - 1 + pos, val);
8054 }
8055
8056 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8057         enum isl_dim_type type, unsigned pos, isl_int *val)
8058 {
8059         return isl_map_plain_is_fixed(set, type, pos, val);
8060 }
8061
8062 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8063         enum isl_dim_type type, unsigned pos, isl_int *val)
8064 {
8065         return isl_map_plain_is_fixed(map, type, pos, val);
8066 }
8067
8068 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8069  * then return this fixed value in *val.
8070  */
8071 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8072         unsigned dim, isl_int *val)
8073 {
8074         return isl_basic_set_plain_has_fixed_var(bset,
8075                                         isl_basic_set_n_param(bset) + dim, val);
8076 }
8077
8078 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8079  * then return this fixed value in *val.
8080  */
8081 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8082         unsigned dim, isl_int *val)
8083 {
8084         return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8085 }
8086
8087 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8088         unsigned dim, isl_int *val)
8089 {
8090         return isl_set_plain_dim_is_fixed(set, dim, val);
8091 }
8092
8093 /* Check if input variable in has fixed value and if so and if val is not NULL,
8094  * then return this fixed value in *val.
8095  */
8096 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8097         unsigned in, isl_int *val)
8098 {
8099         return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8100 }
8101
8102 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8103  * and if val is not NULL, then return this lower bound in *val.
8104  */
8105 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8106         __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8107 {
8108         int i, i_eq = -1, i_ineq = -1;
8109         isl_int *c;
8110         unsigned total;
8111         unsigned nparam;
8112
8113         if (!bset)
8114                 return -1;
8115         total = isl_basic_set_total_dim(bset);
8116         nparam = isl_basic_set_n_param(bset);
8117         for (i = 0; i < bset->n_eq; ++i) {
8118                 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8119                         continue;
8120                 if (i_eq != -1)
8121                         return 0;
8122                 i_eq = i;
8123         }
8124         for (i = 0; i < bset->n_ineq; ++i) {
8125                 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8126                         continue;
8127                 if (i_eq != -1 || i_ineq != -1)
8128                         return 0;
8129                 i_ineq = i;
8130         }
8131         if (i_eq == -1 && i_ineq == -1)
8132                 return 0;
8133         c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8134         /* The coefficient should always be one due to normalization. */
8135         if (!isl_int_is_one(c[1+nparam+dim]))
8136                 return 0;
8137         if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8138                 return 0;
8139         if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8140                                         total - nparam - dim - 1) != -1)
8141                 return 0;
8142         if (val)
8143                 isl_int_neg(*val, c[0]);
8144         return 1;
8145 }
8146
8147 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8148         unsigned dim, isl_int *val)
8149 {
8150         int i;
8151         isl_int v;
8152         isl_int tmp;
8153         int fixed;
8154
8155         if (!set)
8156                 return -1;
8157         if (set->n == 0)
8158                 return 0;
8159         if (set->n == 1)
8160                 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8161                                                                 dim, val);
8162         isl_int_init(v);
8163         isl_int_init(tmp);
8164         fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8165                                                                 dim, &v);
8166         for (i = 1; fixed == 1 && i < set->n; ++i) {
8167                 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8168                                                                 dim, &tmp);
8169                 if (fixed == 1 && isl_int_ne(tmp, v))
8170                         fixed = 0;
8171         }
8172         if (val)
8173                 isl_int_set(*val, v);
8174         isl_int_clear(tmp);
8175         isl_int_clear(v);
8176         return fixed;
8177 }
8178
8179 struct constraint {
8180         unsigned        size;
8181         isl_int         *c;
8182 };
8183
8184 /* uset_gist depends on constraints without existentially quantified
8185  * variables sorting first.
8186  */
8187 static int qsort_constraint_cmp(const void *p1, const void *p2)
8188 {
8189         const struct constraint *c1 = (const struct constraint *)p1;
8190         const struct constraint *c2 = (const struct constraint *)p2;
8191         int l1, l2;
8192         unsigned size = isl_min(c1->size, c2->size);
8193
8194         l1 = isl_seq_last_non_zero(c1->c, size);
8195         l2 = isl_seq_last_non_zero(c2->c, size);
8196
8197         if (l1 != l2)
8198                 return l1 - l2;
8199
8200         return isl_seq_cmp(c1->c, c2->c, size);
8201 }
8202
8203 static struct isl_basic_map *isl_basic_map_sort_constraints(
8204         struct isl_basic_map *bmap)
8205 {
8206         int i;
8207         struct constraint *c;
8208         unsigned total;
8209
8210         if (!bmap)
8211                 return NULL;
8212         total = isl_basic_map_total_dim(bmap);
8213         c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8214         if (!c)
8215                 goto error;
8216         for (i = 0; i < bmap->n_ineq; ++i) {
8217                 c[i].size = total;
8218                 c[i].c = bmap->ineq[i];
8219         }
8220         qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8221         for (i = 0; i < bmap->n_ineq; ++i)
8222                 bmap->ineq[i] = c[i].c;
8223         free(c);
8224         return bmap;
8225 error:
8226         isl_basic_map_free(bmap);
8227         return NULL;
8228 }
8229
8230 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8231         __isl_take isl_basic_set *bset)
8232 {
8233         return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8234                                                 (struct isl_basic_map *)bset);
8235 }
8236
8237 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8238 {
8239         if (!bmap)
8240                 return NULL;
8241         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8242                 return bmap;
8243         bmap = isl_basic_map_remove_redundancies(bmap);
8244         bmap = isl_basic_map_sort_constraints(bmap);
8245         if (bmap)
8246                 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8247         return bmap;
8248 }
8249
8250 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8251 {
8252         return (struct isl_basic_set *)isl_basic_map_normalize(
8253                                                 (struct isl_basic_map *)bset);
8254 }
8255
8256 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8257         const __isl_keep isl_basic_map *bmap2)
8258 {
8259         int i, cmp;
8260         unsigned total;
8261
8262         if (bmap1 == bmap2)
8263                 return 0;
8264         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8265             ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8266                 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8267         if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8268                 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8269         if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8270                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8271         if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8272                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8273         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8274             ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8275                 return 0;
8276         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8277                 return 1;
8278         if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8279                 return -1;
8280         if (bmap1->n_eq != bmap2->n_eq)
8281                 return bmap1->n_eq - bmap2->n_eq;
8282         if (bmap1->n_ineq != bmap2->n_ineq)
8283                 return bmap1->n_ineq - bmap2->n_ineq;
8284         if (bmap1->n_div != bmap2->n_div)
8285                 return bmap1->n_div - bmap2->n_div;
8286         total = isl_basic_map_total_dim(bmap1);
8287         for (i = 0; i < bmap1->n_eq; ++i) {
8288                 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8289                 if (cmp)
8290                         return cmp;
8291         }
8292         for (i = 0; i < bmap1->n_ineq; ++i) {
8293                 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8294                 if (cmp)
8295                         return cmp;
8296         }
8297         for (i = 0; i < bmap1->n_div; ++i) {
8298                 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8299                 if (cmp)
8300                         return cmp;
8301         }
8302         return 0;
8303 }
8304
8305 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8306         const __isl_keep isl_basic_set *bset2)
8307 {
8308         return isl_basic_map_plain_cmp(bset1, bset2);
8309 }
8310
8311 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8312 {
8313         int i, cmp;
8314
8315         if (set1 == set2)
8316                 return 0;
8317         if (set1->n != set2->n)
8318                 return set1->n - set2->n;
8319
8320         for (i = 0; i < set1->n; ++i) {
8321                 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8322                 if (cmp)
8323                         return cmp;
8324         }
8325
8326         return 0;
8327 }
8328
8329 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8330         __isl_keep isl_basic_map *bmap2)
8331 {
8332         return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8333 }
8334
8335 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8336         __isl_keep isl_basic_set *bset2)
8337 {
8338         return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8339                                             (isl_basic_map *)bset2);
8340 }
8341
8342 static int qsort_bmap_cmp(const void *p1, const void *p2)
8343 {
8344         const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8345         const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8346
8347         return isl_basic_map_plain_cmp(bmap1, bmap2);
8348 }
8349
8350 /* We normalize in place, but if anything goes wrong we need
8351  * to return NULL, so we need to make sure we don't change the
8352  * meaning of any possible other copies of map.
8353  */
8354 struct isl_map *isl_map_normalize(struct isl_map *map)
8355 {
8356         int i, j;
8357         struct isl_basic_map *bmap;
8358
8359         if (!map)
8360                 return NULL;
8361         if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8362                 return map;
8363         for (i = 0; i < map->n; ++i) {
8364                 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8365                 if (!bmap)
8366                         goto error;
8367                 isl_basic_map_free(map->p[i]);
8368                 map->p[i] = bmap;
8369         }
8370         qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8371         ISL_F_SET(map, ISL_MAP_NORMALIZED);
8372         map = isl_map_remove_empty_parts(map);
8373         if (!map)
8374                 return NULL;
8375         for (i = map->n - 1; i >= 1; --i) {
8376                 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8377                         continue;
8378                 isl_basic_map_free(map->p[i-1]);
8379                 for (j = i; j < map->n; ++j)
8380                         map->p[j-1] = map->p[j];
8381                 map->n--;
8382         }
8383         return map;
8384 error:
8385         isl_map_free(map);
8386         return NULL;
8387
8388 }
8389
8390 struct isl_set *isl_set_normalize(struct isl_set *set)
8391 {
8392         return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8393 }
8394
8395 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8396 {
8397         int i;
8398         int equal;
8399
8400         if (!map1 || !map2)
8401                 return -1;
8402
8403         if (map1 == map2)
8404                 return 1;
8405         if (!isl_space_is_equal(map1->dim, map2->dim))
8406                 return 0;
8407
8408         map1 = isl_map_copy(map1);
8409         map2 = isl_map_copy(map2);
8410         map1 = isl_map_normalize(map1);
8411         map2 = isl_map_normalize(map2);
8412         if (!map1 || !map2)
8413                 goto error;
8414         equal = map1->n == map2->n;
8415         for (i = 0; equal && i < map1->n; ++i) {
8416                 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8417                 if (equal < 0)
8418                         goto error;
8419         }
8420         isl_map_free(map1);
8421         isl_map_free(map2);
8422         return equal;
8423 error:
8424         isl_map_free(map1);
8425         isl_map_free(map2);
8426         return -1;
8427 }
8428
8429 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8430 {
8431         return isl_map_plain_is_equal(map1, map2);
8432 }
8433
8434 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8435 {
8436         return isl_map_plain_is_equal((struct isl_map *)set1,
8437                                                 (struct isl_map *)set2);
8438 }
8439
8440 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8441 {
8442         return isl_set_plain_is_equal(set1, set2);
8443 }
8444
8445 /* Return an interval that ranges from min to max (inclusive)
8446  */
8447 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8448         isl_int min, isl_int max)
8449 {
8450         int k;
8451         struct isl_basic_set *bset = NULL;
8452
8453         bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8454         if (!bset)
8455                 goto error;
8456
8457         k = isl_basic_set_alloc_inequality(bset);
8458         if (k < 0)
8459                 goto error;
8460         isl_int_set_si(bset->ineq[k][1], 1);
8461         isl_int_neg(bset->ineq[k][0], min);
8462
8463         k = isl_basic_set_alloc_inequality(bset);
8464         if (k < 0)
8465                 goto error;
8466         isl_int_set_si(bset->ineq[k][1], -1);
8467         isl_int_set(bset->ineq[k][0], max);
8468
8469         return bset;
8470 error:
8471         isl_basic_set_free(bset);
8472         return NULL;
8473 }
8474
8475 /* Return the Cartesian product of the basic sets in list (in the given order).
8476  */
8477 __isl_give isl_basic_set *isl_basic_set_list_product(
8478         __isl_take struct isl_basic_set_list *list)
8479 {
8480         int i;
8481         unsigned dim;
8482         unsigned nparam;
8483         unsigned extra;
8484         unsigned n_eq;
8485         unsigned n_ineq;
8486         struct isl_basic_set *product = NULL;
8487
8488         if (!list)
8489                 goto error;
8490         isl_assert(list->ctx, list->n > 0, goto error);
8491         isl_assert(list->ctx, list->p[0], goto error);
8492         nparam = isl_basic_set_n_param(list->p[0]);
8493         dim = isl_basic_set_n_dim(list->p[0]);
8494         extra = list->p[0]->n_div;
8495         n_eq = list->p[0]->n_eq;
8496         n_ineq = list->p[0]->n_ineq;
8497         for (i = 1; i < list->n; ++i) {
8498                 isl_assert(list->ctx, list->p[i], goto error);
8499                 isl_assert(list->ctx,
8500                     nparam == isl_basic_set_n_param(list->p[i]), goto error);
8501                 dim += isl_basic_set_n_dim(list->p[i]);
8502                 extra += list->p[i]->n_div;
8503                 n_eq += list->p[i]->n_eq;
8504                 n_ineq += list->p[i]->n_ineq;
8505         }
8506         product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
8507                                         n_eq, n_ineq);
8508         if (!product)
8509                 goto error;
8510         dim = 0;
8511         for (i = 0; i < list->n; ++i) {
8512                 isl_basic_set_add_constraints(product,
8513                                         isl_basic_set_copy(list->p[i]), dim);
8514                 dim += isl_basic_set_n_dim(list->p[i]);
8515         }
8516         isl_basic_set_list_free(list);
8517         return product;
8518 error:
8519         isl_basic_set_free(product);
8520         isl_basic_set_list_free(list);
8521         return NULL;
8522 }
8523
8524 struct isl_basic_map *isl_basic_map_product(
8525                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8526 {
8527         isl_space *dim_result = NULL;
8528         struct isl_basic_map *bmap;
8529         unsigned in1, in2, out1, out2, nparam, total, pos;
8530         struct isl_dim_map *dim_map1, *dim_map2;
8531
8532         if (!bmap1 || !bmap2)
8533                 goto error;
8534
8535         isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
8536                                      bmap2->dim, isl_dim_param), goto error);
8537         dim_result = isl_space_product(isl_space_copy(bmap1->dim),
8538                                                    isl_space_copy(bmap2->dim));
8539
8540         in1 = isl_basic_map_n_in(bmap1);
8541         in2 = isl_basic_map_n_in(bmap2);
8542         out1 = isl_basic_map_n_out(bmap1);
8543         out2 = isl_basic_map_n_out(bmap2);
8544         nparam = isl_basic_map_n_param(bmap1);
8545
8546         total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
8547         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8548         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8549         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8550         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8551         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8552         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8553         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8554         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8555         isl_dim_map_div(dim_map1, bmap1, pos += out2);
8556         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8557
8558         bmap = isl_basic_map_alloc_space(dim_result,
8559                         bmap1->n_div + bmap2->n_div,
8560                         bmap1->n_eq + bmap2->n_eq,
8561                         bmap1->n_ineq + bmap2->n_ineq);
8562         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8563         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8564         bmap = isl_basic_map_simplify(bmap);
8565         return isl_basic_map_finalize(bmap);
8566 error:
8567         isl_basic_map_free(bmap1);
8568         isl_basic_map_free(bmap2);
8569         return NULL;
8570 }
8571
8572 __isl_give isl_basic_map *isl_basic_map_flat_product(
8573         __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8574 {
8575         isl_basic_map *prod;
8576
8577         prod = isl_basic_map_product(bmap1, bmap2);
8578         prod = isl_basic_map_flatten(prod);
8579         return prod;
8580 }
8581
8582 __isl_give isl_basic_set *isl_basic_set_flat_product(
8583         __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
8584 {
8585         return isl_basic_map_flat_range_product(bset1, bset2);
8586 }
8587
8588 __isl_give isl_basic_map *isl_basic_map_domain_product(
8589         __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8590 {
8591         isl_space *space_result = NULL;
8592         isl_basic_map *bmap;
8593         unsigned in1, in2, out, nparam, total, pos;
8594         struct isl_dim_map *dim_map1, *dim_map2;
8595
8596         if (!bmap1 || !bmap2)
8597                 goto error;
8598
8599         space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
8600                                                 isl_space_copy(bmap2->dim));
8601
8602         in1 = isl_basic_map_dim(bmap1, isl_dim_in);
8603         in2 = isl_basic_map_dim(bmap2, isl_dim_in);
8604         out = isl_basic_map_dim(bmap1, isl_dim_out);
8605         nparam = isl_basic_map_dim(bmap1, isl_dim_param);
8606
8607         total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
8608         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8609         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8610         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8611         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8612         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8613         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8614         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8615         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
8616         isl_dim_map_div(dim_map1, bmap1, pos += out);
8617         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8618
8619         bmap = isl_basic_map_alloc_space(space_result,
8620                         bmap1->n_div + bmap2->n_div,
8621                         bmap1->n_eq + bmap2->n_eq,
8622                         bmap1->n_ineq + bmap2->n_ineq);
8623         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8624         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8625         bmap = isl_basic_map_simplify(bmap);
8626         return isl_basic_map_finalize(bmap);
8627 error:
8628         isl_basic_map_free(bmap1);
8629         isl_basic_map_free(bmap2);
8630         return NULL;
8631 }
8632
8633 __isl_give isl_basic_map *isl_basic_map_range_product(
8634         __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8635 {
8636         isl_space *dim_result = NULL;
8637         isl_basic_map *bmap;
8638         unsigned in, out1, out2, nparam, total, pos;
8639         struct isl_dim_map *dim_map1, *dim_map2;
8640
8641         if (!bmap1 || !bmap2)
8642                 goto error;
8643
8644         if (!isl_space_match(bmap1->dim, isl_dim_param,
8645                             bmap2->dim, isl_dim_param))
8646                 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
8647                         "parameters don't match", goto error);
8648
8649         dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
8650                                            isl_space_copy(bmap2->dim));
8651
8652         in = isl_basic_map_dim(bmap1, isl_dim_in);
8653         out1 = isl_basic_map_n_out(bmap1);
8654         out2 = isl_basic_map_n_out(bmap2);
8655         nparam = isl_basic_map_n_param(bmap1);
8656
8657         total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
8658         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8659         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8660         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8661         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8662         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8663         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
8664         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
8665         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8666         isl_dim_map_div(dim_map1, bmap1, pos += out2);
8667         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8668
8669         bmap = isl_basic_map_alloc_space(dim_result,
8670                         bmap1->n_div + bmap2->n_div,
8671                         bmap1->n_eq + bmap2->n_eq,
8672                         bmap1->n_ineq + bmap2->n_ineq);
8673         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8674         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8675         bmap = isl_basic_map_simplify(bmap);
8676         return isl_basic_map_finalize(bmap);
8677 error:
8678         isl_basic_map_free(bmap1);
8679         isl_basic_map_free(bmap2);
8680         return NULL;
8681 }
8682
8683 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
8684         __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
8685 {
8686         isl_basic_map *prod;
8687
8688         prod = isl_basic_map_range_product(bmap1, bmap2);
8689         prod = isl_basic_map_flatten_range(prod);
8690         return prod;
8691 }
8692
8693 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
8694         __isl_take isl_map *map2,
8695         __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
8696                                            __isl_take isl_space *right),
8697         __isl_give isl_basic_map *(*basic_map_product)(
8698                 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
8699 {
8700         unsigned flags = 0;
8701         struct isl_map *result;
8702         int i, j;
8703
8704         if (!map1 || !map2)
8705                 goto error;
8706
8707         isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
8708                                          map2->dim, isl_dim_param), goto error);
8709
8710         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
8711             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
8712                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
8713
8714         result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
8715                                                isl_space_copy(map2->dim)),
8716                                 map1->n * map2->n, flags);
8717         if (!result)
8718                 goto error;
8719         for (i = 0; i < map1->n; ++i)
8720                 for (j = 0; j < map2->n; ++j) {
8721                         struct isl_basic_map *part;
8722                         part = basic_map_product(isl_basic_map_copy(map1->p[i]),
8723                                                  isl_basic_map_copy(map2->p[j]));
8724                         if (isl_basic_map_is_empty(part))
8725                                 isl_basic_map_free(part);
8726                         else
8727                                 result = isl_map_add_basic_map(result, part);
8728                         if (!result)
8729                                 goto error;
8730                 }
8731         isl_map_free(map1);
8732         isl_map_free(map2);
8733         return result;
8734 error:
8735         isl_map_free(map1);
8736         isl_map_free(map2);
8737         return NULL;
8738 }
8739
8740 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
8741  */
8742 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
8743         __isl_take isl_map *map2)
8744 {
8745         return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
8746 }
8747
8748 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
8749         __isl_take isl_map *map2)
8750 {
8751         return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
8752 }
8753
8754 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
8755  */
8756 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
8757         __isl_take isl_map *map2)
8758 {
8759         isl_map *prod;
8760
8761         prod = isl_map_product(map1, map2);
8762         prod = isl_map_flatten(prod);
8763         return prod;
8764 }
8765
8766 /* Given two set A and B, construct its Cartesian product A x B.
8767  */
8768 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
8769 {
8770         return isl_map_range_product(set1, set2);
8771 }
8772
8773 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
8774         __isl_take isl_set *set2)
8775 {
8776         return isl_map_flat_range_product(set1, set2);
8777 }
8778
8779 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
8780  */
8781 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
8782         __isl_take isl_map *map2)
8783 {
8784         return map_product(map1, map2, &isl_space_domain_product,
8785                                 &isl_basic_map_domain_product);
8786 }
8787
8788 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
8789  */
8790 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
8791         __isl_take isl_map *map2)
8792 {
8793         return map_product(map1, map2, &isl_space_range_product,
8794                                 &isl_basic_map_range_product);
8795 }
8796
8797 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
8798         __isl_take isl_map *map2)
8799 {
8800         return isl_map_align_params_map_map_and(map1, map2,
8801                                                 &map_domain_product_aligned);
8802 }
8803
8804 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
8805         __isl_take isl_map *map2)
8806 {
8807         return isl_map_align_params_map_map_and(map1, map2,
8808                                                 &map_range_product_aligned);
8809 }
8810
8811 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
8812  */
8813 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
8814         __isl_take isl_map *map2)
8815 {
8816         isl_map *prod;
8817
8818         prod = isl_map_domain_product(map1, map2);
8819         prod = isl_map_flatten_domain(prod);
8820         return prod;
8821 }
8822
8823 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
8824  */
8825 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
8826         __isl_take isl_map *map2)
8827 {
8828         isl_map *prod;
8829
8830         prod = isl_map_range_product(map1, map2);
8831         prod = isl_map_flatten_range(prod);
8832         return prod;
8833 }
8834
8835 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
8836 {
8837         int i;
8838         uint32_t hash = isl_hash_init();
8839         unsigned total;
8840
8841         if (!bmap)
8842                 return 0;
8843         bmap = isl_basic_map_copy(bmap);
8844         bmap = isl_basic_map_normalize(bmap);
8845         if (!bmap)
8846                 return 0;
8847         total = isl_basic_map_total_dim(bmap);
8848         isl_hash_byte(hash, bmap->n_eq & 0xFF);
8849         for (i = 0; i < bmap->n_eq; ++i) {
8850                 uint32_t c_hash;
8851                 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
8852                 isl_hash_hash(hash, c_hash);
8853         }
8854         isl_hash_byte(hash, bmap->n_ineq & 0xFF);
8855         for (i = 0; i < bmap->n_ineq; ++i) {
8856                 uint32_t c_hash;
8857                 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
8858                 isl_hash_hash(hash, c_hash);
8859         }
8860         isl_hash_byte(hash, bmap->n_div & 0xFF);
8861         for (i = 0; i < bmap->n_div; ++i) {
8862                 uint32_t c_hash;
8863                 if (isl_int_is_zero(bmap->div[i][0]))
8864                         continue;
8865                 isl_hash_byte(hash, i & 0xFF);
8866                 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
8867                 isl_hash_hash(hash, c_hash);
8868         }
8869         isl_basic_map_free(bmap);
8870         return hash;
8871 }
8872
8873 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
8874 {
8875         return isl_basic_map_get_hash((isl_basic_map *)bset);
8876 }
8877
8878 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
8879 {
8880         int i;
8881         uint32_t hash;
8882
8883         if (!map)
8884                 return 0;
8885         map = isl_map_copy(map);
8886         map = isl_map_normalize(map);
8887         if (!map)
8888                 return 0;
8889
8890         hash = isl_hash_init();
8891         for (i = 0; i < map->n; ++i) {
8892                 uint32_t bmap_hash;
8893                 bmap_hash = isl_basic_map_get_hash(map->p[i]);
8894                 isl_hash_hash(hash, bmap_hash);
8895         }
8896                 
8897         isl_map_free(map);
8898
8899         return hash;
8900 }
8901
8902 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
8903 {
8904         return isl_map_get_hash((isl_map *)set);
8905 }
8906
8907 /* Check if the value for dimension dim is completely determined
8908  * by the values of the other parameters and variables.
8909  * That is, check if dimension dim is involved in an equality.
8910  */
8911 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
8912 {
8913         int i;
8914         unsigned nparam;
8915
8916         if (!bset)
8917                 return -1;
8918         nparam = isl_basic_set_n_param(bset);
8919         for (i = 0; i < bset->n_eq; ++i)
8920                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
8921                         return 1;
8922         return 0;
8923 }
8924
8925 /* Check if the value for dimension dim is completely determined
8926  * by the values of the other parameters and variables.
8927  * That is, check if dimension dim is involved in an equality
8928  * for each of the subsets.
8929  */
8930 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
8931 {
8932         int i;
8933
8934         if (!set)
8935                 return -1;
8936         for (i = 0; i < set->n; ++i) {
8937                 int unique;
8938                 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
8939                 if (unique != 1)
8940                         return unique;
8941         }
8942         return 1;
8943 }
8944
8945 int isl_set_n_basic_set(__isl_keep isl_set *set)
8946 {
8947         return set ? set->n : 0;
8948 }
8949
8950 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
8951         int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
8952 {
8953         int i;
8954
8955         if (!map)
8956                 return -1;
8957
8958         for (i = 0; i < map->n; ++i)
8959                 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
8960                         return -1;
8961
8962         return 0;
8963 }
8964
8965 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
8966         int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
8967 {
8968         int i;
8969
8970         if (!set)
8971                 return -1;
8972
8973         for (i = 0; i < set->n; ++i)
8974                 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
8975                         return -1;
8976
8977         return 0;
8978 }
8979
8980 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
8981 {
8982         isl_space *dim;
8983
8984         if (!bset)
8985                 return NULL;
8986
8987         bset = isl_basic_set_cow(bset);
8988         if (!bset)
8989                 return NULL;
8990
8991         dim = isl_basic_set_get_space(bset);
8992         dim = isl_space_lift(dim, bset->n_div);
8993         if (!dim)
8994                 goto error;
8995         isl_space_free(bset->dim);
8996         bset->dim = dim;
8997         bset->extra -= bset->n_div;
8998         bset->n_div = 0;
8999
9000         bset = isl_basic_set_finalize(bset);
9001
9002         return bset;
9003 error:
9004         isl_basic_set_free(bset);
9005         return NULL;
9006 }
9007
9008 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9009 {
9010         int i;
9011         isl_space *dim;
9012         unsigned n_div;
9013
9014         set = isl_set_align_divs(set);
9015
9016         if (!set)
9017                 return NULL;
9018
9019         set = isl_set_cow(set);
9020         if (!set)
9021                 return NULL;
9022
9023         n_div = set->p[0]->n_div;
9024         dim = isl_set_get_space(set);
9025         dim = isl_space_lift(dim, n_div);
9026         if (!dim)
9027                 goto error;
9028         isl_space_free(set->dim);
9029         set->dim = dim;
9030
9031         for (i = 0; i < set->n; ++i) {
9032                 set->p[i] = isl_basic_set_lift(set->p[i]);
9033                 if (!set->p[i])
9034                         goto error;
9035         }
9036
9037         return set;
9038 error:
9039         isl_set_free(set);
9040         return NULL;
9041 }
9042
9043 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9044 {
9045         isl_space *dim;
9046         struct isl_basic_map *bmap;
9047         unsigned n_set;
9048         unsigned n_div;
9049         unsigned n_param;
9050         unsigned total;
9051         int i, k, l;
9052
9053         set = isl_set_align_divs(set);
9054
9055         if (!set)
9056                 return NULL;
9057
9058         dim = isl_set_get_space(set);
9059         if (set->n == 0 || set->p[0]->n_div == 0) {
9060                 isl_set_free(set);
9061                 return isl_map_identity(isl_space_map_from_set(dim));
9062         }
9063
9064         n_div = set->p[0]->n_div;
9065         dim = isl_space_map_from_set(dim);
9066         n_param = isl_space_dim(dim, isl_dim_param);
9067         n_set = isl_space_dim(dim, isl_dim_in);
9068         dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9069         bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9070         for (i = 0; i < n_set; ++i)
9071                 bmap = var_equal(bmap, i);
9072
9073         total = n_param + n_set + n_set + n_div;
9074         for (i = 0; i < n_div; ++i) {
9075                 k = isl_basic_map_alloc_inequality(bmap);
9076                 if (k < 0)
9077                         goto error;
9078                 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9079                 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9080                 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9081                             set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9082                 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9083                             set->p[0]->div[i][0]);
9084
9085                 l = isl_basic_map_alloc_inequality(bmap);
9086                 if (l < 0)
9087                         goto error;
9088                 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9089                 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9090                             set->p[0]->div[i][0]);
9091                 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9092         }
9093
9094         isl_set_free(set);
9095         bmap = isl_basic_map_simplify(bmap);
9096         bmap = isl_basic_map_finalize(bmap);
9097         return isl_map_from_basic_map(bmap);
9098 error:
9099         isl_set_free(set);
9100         isl_basic_map_free(bmap);
9101         return NULL;
9102 }
9103
9104 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9105 {
9106         unsigned dim;
9107         int size = 0;
9108
9109         if (!bset)
9110                 return -1;
9111
9112         dim = isl_basic_set_total_dim(bset);
9113         size += bset->n_eq * (1 + dim);
9114         size += bset->n_ineq * (1 + dim);
9115         size += bset->n_div * (2 + dim);
9116
9117         return size;
9118 }
9119
9120 int isl_set_size(__isl_keep isl_set *set)
9121 {
9122         int i;
9123         int size = 0;
9124
9125         if (!set)
9126                 return -1;
9127
9128         for (i = 0; i < set->n; ++i)
9129                 size += isl_basic_set_size(set->p[i]);
9130
9131         return size;
9132 }
9133
9134 /* Check if there is any lower bound (if lower == 0) and/or upper
9135  * bound (if upper == 0) on the specified dim.
9136  */
9137 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9138         enum isl_dim_type type, unsigned pos, int lower, int upper)
9139 {
9140         int i;
9141
9142         if (!bmap)
9143                 return -1;
9144
9145         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9146
9147         pos += isl_basic_map_offset(bmap, type);
9148
9149         for (i = 0; i < bmap->n_div; ++i) {
9150                 if (isl_int_is_zero(bmap->div[i][0]))
9151                         continue;
9152                 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9153                         return 1;
9154         }
9155
9156         for (i = 0; i < bmap->n_eq; ++i)
9157                 if (!isl_int_is_zero(bmap->eq[i][pos]))
9158                         return 1;
9159
9160         for (i = 0; i < bmap->n_ineq; ++i) {
9161                 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
9162                 if (sgn > 0)
9163                         lower = 1;
9164                 if (sgn < 0)
9165                         upper = 1;
9166         }
9167
9168         return lower && upper;
9169 }
9170
9171 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9172         enum isl_dim_type type, unsigned pos)
9173 {
9174         return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9175 }
9176
9177 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9178         enum isl_dim_type type, unsigned pos)
9179 {
9180         return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9181 }
9182
9183 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9184         enum isl_dim_type type, unsigned pos)
9185 {
9186         return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9187 }
9188
9189 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9190         enum isl_dim_type type, unsigned pos)
9191 {
9192         int i;
9193
9194         if (!map)
9195                 return -1;
9196
9197         for (i = 0; i < map->n; ++i) {
9198                 int bounded;
9199                 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9200                 if (bounded < 0 || !bounded)
9201                         return bounded;
9202         }
9203
9204         return 1;
9205 }
9206
9207 /* Return 1 if the specified dim is involved in both an upper bound
9208  * and a lower bound.
9209  */
9210 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9211         enum isl_dim_type type, unsigned pos)
9212 {
9213         return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9214 }
9215
9216 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9217  */
9218 static int has_any_bound(__isl_keep isl_map *map,
9219         enum isl_dim_type type, unsigned pos,
9220         int (*fn)(__isl_keep isl_basic_map *bmap,
9221                   enum isl_dim_type type, unsigned pos))
9222 {
9223         int i;
9224
9225         if (!map)
9226                 return -1;
9227
9228         for (i = 0; i < map->n; ++i) {
9229                 int bounded;
9230                 bounded = fn(map->p[i], type, pos);
9231                 if (bounded < 0 || bounded)
9232                         return bounded;
9233         }
9234
9235         return 0;
9236 }
9237
9238 /* Return 1 if the specified dim is involved in any lower bound.
9239  */
9240 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
9241         enum isl_dim_type type, unsigned pos)
9242 {
9243         return has_any_bound(set, type, pos,
9244                                 &isl_basic_map_dim_has_lower_bound);
9245 }
9246
9247 /* Return 1 if the specified dim is involved in any upper bound.
9248  */
9249 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
9250         enum isl_dim_type type, unsigned pos)
9251 {
9252         return has_any_bound(set, type, pos,
9253                                 &isl_basic_map_dim_has_upper_bound);
9254 }
9255
9256 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9257  */
9258 static int has_bound(__isl_keep isl_map *map,
9259         enum isl_dim_type type, unsigned pos,
9260         int (*fn)(__isl_keep isl_basic_map *bmap,
9261                   enum isl_dim_type type, unsigned pos))
9262 {
9263         int i;
9264
9265         if (!map)
9266                 return -1;
9267
9268         for (i = 0; i < map->n; ++i) {
9269                 int bounded;
9270                 bounded = fn(map->p[i], type, pos);
9271                 if (bounded < 0 || !bounded)
9272                         return bounded;
9273         }
9274
9275         return 1;
9276 }
9277
9278 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9279  */
9280 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9281         enum isl_dim_type type, unsigned pos)
9282 {
9283         return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9284 }
9285
9286 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9287  */
9288 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9289         enum isl_dim_type type, unsigned pos)
9290 {
9291         return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9292 }
9293
9294 /* For each of the "n" variables starting at "first", determine
9295  * the sign of the variable and put the results in the first "n"
9296  * elements of the array "signs".
9297  * Sign
9298  *      1 means that the variable is non-negative
9299  *      -1 means that the variable is non-positive
9300  *      0 means the variable attains both positive and negative values.
9301  */
9302 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9303         unsigned first, unsigned n, int *signs)
9304 {
9305         isl_vec *bound = NULL;
9306         struct isl_tab *tab = NULL;
9307         struct isl_tab_undo *snap;
9308         int i;
9309
9310         if (!bset || !signs)
9311                 return -1;
9312
9313         bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9314         tab = isl_tab_from_basic_set(bset, 0);
9315         if (!bound || !tab)
9316                 goto error;
9317
9318         isl_seq_clr(bound->el, bound->size);
9319         isl_int_set_si(bound->el[0], -1);
9320
9321         snap = isl_tab_snap(tab);
9322         for (i = 0; i < n; ++i) {
9323                 int empty;
9324
9325                 isl_int_set_si(bound->el[1 + first + i], -1);
9326                 if (isl_tab_add_ineq(tab, bound->el) < 0)
9327                         goto error;
9328                 empty = tab->empty;
9329                 isl_int_set_si(bound->el[1 + first + i], 0);
9330                 if (isl_tab_rollback(tab, snap) < 0)
9331                         goto error;
9332
9333                 if (empty) {
9334                         signs[i] = 1;
9335                         continue;
9336                 }
9337
9338                 isl_int_set_si(bound->el[1 + first + i], 1);
9339                 if (isl_tab_add_ineq(tab, bound->el) < 0)
9340                         goto error;
9341                 empty = tab->empty;
9342                 isl_int_set_si(bound->el[1 + first + i], 0);
9343                 if (isl_tab_rollback(tab, snap) < 0)
9344                         goto error;
9345
9346                 signs[i] = empty ? -1 : 0;
9347         }
9348
9349         isl_tab_free(tab);
9350         isl_vec_free(bound);
9351         return 0;
9352 error:
9353         isl_tab_free(tab);
9354         isl_vec_free(bound);
9355         return -1;
9356 }
9357
9358 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
9359         enum isl_dim_type type, unsigned first, unsigned n, int *signs)
9360 {
9361         if (!bset || !signs)
9362                 return -1;
9363         isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
9364                 return -1);
9365
9366         first += pos(bset->dim, type) - 1;
9367         return isl_basic_set_vars_get_sign(bset, first, n, signs);
9368 }
9369
9370 /* Check if the given basic map is obviously single-valued.
9371  * In particular, for each output dimension, check that there is
9372  * an equality that defines the output dimension in terms of
9373  * earlier dimensions.
9374  */
9375 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
9376 {
9377         int i, j;
9378         unsigned total;
9379         unsigned n_out;
9380         unsigned o_out;
9381
9382         if (!bmap)
9383                 return -1;
9384
9385         total = 1 + isl_basic_map_total_dim(bmap);
9386         n_out = isl_basic_map_dim(bmap, isl_dim_out);
9387         o_out = isl_basic_map_offset(bmap, isl_dim_out);
9388
9389         for (i = 0; i < n_out; ++i) {
9390                 for (j = 0; j < bmap->n_eq; ++j) {
9391                         if (isl_int_is_zero(bmap->eq[j][o_out + i]))
9392                                 continue;
9393                         if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
9394                                                 total - (o_out + i + 1)) == -1)
9395                                 break;
9396                 }
9397                 if (j >= bmap->n_eq)
9398                         return 0;
9399         }
9400
9401         return 1;
9402 }
9403
9404 /* Check if the given basic map is single-valued.
9405  * We simply compute
9406  *
9407  *      M \circ M^-1
9408  *
9409  * and check if the result is a subset of the identity mapping.
9410  */
9411 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
9412 {
9413         isl_space *space;
9414         isl_basic_map *test;
9415         isl_basic_map *id;
9416         int sv;
9417
9418         sv = isl_basic_map_plain_is_single_valued(bmap);
9419         if (sv < 0 || sv)
9420                 return sv;
9421
9422         test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
9423         test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
9424
9425         space = isl_basic_map_get_space(bmap);
9426         space = isl_space_map_from_set(isl_space_range(space));
9427         id = isl_basic_map_identity(space);
9428
9429         sv = isl_basic_map_is_subset(test, id);
9430
9431         isl_basic_map_free(test);
9432         isl_basic_map_free(id);
9433
9434         return sv;
9435 }
9436
9437 /* Check if the given map is obviously single-valued.
9438  */
9439 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
9440 {
9441         if (!map)
9442                 return -1;
9443         if (map->n == 0)
9444                 return 1;
9445         if (map->n >= 2)
9446                 return 0;
9447
9448         return isl_basic_map_plain_is_single_valued(map->p[0]);
9449 }
9450
9451 /* Check if the given map is single-valued.
9452  * We simply compute
9453  *
9454  *      M \circ M^-1
9455  *
9456  * and check if the result is a subset of the identity mapping.
9457  */
9458 int isl_map_is_single_valued(__isl_keep isl_map *map)
9459 {
9460         isl_space *dim;
9461         isl_map *test;
9462         isl_map *id;
9463         int sv;
9464
9465         sv = isl_map_plain_is_single_valued(map);
9466         if (sv < 0 || sv)
9467                 return sv;
9468
9469         test = isl_map_reverse(isl_map_copy(map));
9470         test = isl_map_apply_range(test, isl_map_copy(map));
9471
9472         dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
9473         id = isl_map_identity(dim);
9474
9475         sv = isl_map_is_subset(test, id);
9476
9477         isl_map_free(test);
9478         isl_map_free(id);
9479
9480         return sv;
9481 }
9482
9483 int isl_map_is_injective(__isl_keep isl_map *map)
9484 {
9485         int in;
9486
9487         map = isl_map_copy(map);
9488         map = isl_map_reverse(map);
9489         in = isl_map_is_single_valued(map);
9490         isl_map_free(map);
9491
9492         return in;
9493 }
9494
9495 /* Check if the given map is obviously injective.
9496  */
9497 int isl_map_plain_is_injective(__isl_keep isl_map *map)
9498 {
9499         int in;
9500
9501         map = isl_map_copy(map);
9502         map = isl_map_reverse(map);
9503         in = isl_map_plain_is_single_valued(map);
9504         isl_map_free(map);
9505
9506         return in;
9507 }
9508
9509 int isl_map_is_bijective(__isl_keep isl_map *map)
9510 {
9511         int sv;
9512
9513         sv = isl_map_is_single_valued(map);
9514         if (sv < 0 || !sv)
9515                 return sv;
9516
9517         return isl_map_is_injective(map);
9518 }
9519
9520 int isl_set_is_singleton(__isl_keep isl_set *set)
9521 {
9522         return isl_map_is_single_valued((isl_map *)set);
9523 }
9524
9525 int isl_map_is_translation(__isl_keep isl_map *map)
9526 {
9527         int ok;
9528         isl_set *delta;
9529
9530         delta = isl_map_deltas(isl_map_copy(map));
9531         ok = isl_set_is_singleton(delta);
9532         isl_set_free(delta);
9533
9534         return ok;
9535 }
9536
9537 static int unique(isl_int *p, unsigned pos, unsigned len)
9538 {
9539         if (isl_seq_first_non_zero(p, pos) != -1)
9540                 return 0;
9541         if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
9542                 return 0;
9543         return 1;
9544 }
9545
9546 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
9547 {
9548         int i, j;
9549         unsigned nvar;
9550         unsigned ovar;
9551
9552         if (!bset)
9553                 return -1;
9554
9555         if (isl_basic_set_dim(bset, isl_dim_div) != 0)
9556                 return 0;
9557
9558         nvar = isl_basic_set_dim(bset, isl_dim_set);
9559         ovar = isl_space_offset(bset->dim, isl_dim_set);
9560         for (j = 0; j < nvar; ++j) {
9561                 int lower = 0, upper = 0;
9562                 for (i = 0; i < bset->n_eq; ++i) {
9563                         if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
9564                                 continue;
9565                         if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
9566                                 return 0;
9567                         break;
9568                 }
9569                 if (i < bset->n_eq)
9570                         continue;
9571                 for (i = 0; i < bset->n_ineq; ++i) {
9572                         if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
9573                                 continue;
9574                         if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
9575                                 return 0;
9576                         if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
9577                                 lower = 1;
9578                         else
9579                                 upper = 1;
9580                 }
9581                 if (!lower || !upper)
9582                         return 0;
9583         }
9584
9585         return 1;
9586 }
9587
9588 int isl_set_is_box(__isl_keep isl_set *set)
9589 {
9590         if (!set)
9591                 return -1;
9592         if (set->n != 1)
9593                 return 0;
9594
9595         return isl_basic_set_is_box(set->p[0]);
9596 }
9597
9598 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
9599 {
9600         if (!bset)
9601                 return -1;
9602         
9603         return isl_space_is_wrapping(bset->dim);
9604 }
9605
9606 int isl_set_is_wrapping(__isl_keep isl_set *set)
9607 {
9608         if (!set)
9609                 return -1;
9610         
9611         return isl_space_is_wrapping(set->dim);
9612 }
9613
9614 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
9615 {
9616         bmap = isl_basic_map_cow(bmap);
9617         if (!bmap)
9618                 return NULL;
9619
9620         bmap->dim = isl_space_wrap(bmap->dim);
9621         if (!bmap->dim)
9622                 goto error;
9623
9624         bmap = isl_basic_map_finalize(bmap);
9625
9626         return (isl_basic_set *)bmap;
9627 error:
9628         isl_basic_map_free(bmap);
9629         return NULL;
9630 }
9631
9632 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
9633 {
9634         int i;
9635
9636         map = isl_map_cow(map);
9637         if (!map)
9638                 return NULL;
9639
9640         for (i = 0; i < map->n; ++i) {
9641                 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
9642                 if (!map->p[i])
9643                         goto error;
9644         }
9645         map->dim = isl_space_wrap(map->dim);
9646         if (!map->dim)
9647                 goto error;
9648
9649         return (isl_set *)map;
9650 error:
9651         isl_map_free(map);
9652         return NULL;
9653 }
9654
9655 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
9656 {
9657         bset = isl_basic_set_cow(bset);
9658         if (!bset)
9659                 return NULL;
9660
9661         bset->dim = isl_space_unwrap(bset->dim);
9662         if (!bset->dim)
9663                 goto error;
9664
9665         bset = isl_basic_set_finalize(bset);
9666
9667         return (isl_basic_map *)bset;
9668 error:
9669         isl_basic_set_free(bset);
9670         return NULL;
9671 }
9672
9673 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
9674 {
9675         int i;
9676
9677         if (!set)
9678                 return NULL;
9679
9680         if (!isl_set_is_wrapping(set))
9681                 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
9682                         goto error);
9683
9684         set = isl_set_cow(set);
9685         if (!set)
9686                 return NULL;
9687
9688         for (i = 0; i < set->n; ++i) {
9689                 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
9690                 if (!set->p[i])
9691                         goto error;
9692         }
9693
9694         set->dim = isl_space_unwrap(set->dim);
9695         if (!set->dim)
9696                 goto error;
9697
9698         return (isl_map *)set;
9699 error:
9700         isl_set_free(set);
9701         return NULL;
9702 }
9703
9704 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
9705         enum isl_dim_type type)
9706 {
9707         if (!bmap)
9708                 return NULL;
9709
9710         if (!isl_space_is_named_or_nested(bmap->dim, type))
9711                 return bmap;
9712
9713         bmap = isl_basic_map_cow(bmap);
9714         if (!bmap)
9715                 return NULL;
9716
9717         bmap->dim = isl_space_reset(bmap->dim, type);
9718         if (!bmap->dim)
9719                 goto error;
9720
9721         bmap = isl_basic_map_finalize(bmap);
9722
9723         return bmap;
9724 error:
9725         isl_basic_map_free(bmap);
9726         return NULL;
9727 }
9728
9729 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
9730         enum isl_dim_type type)
9731 {
9732         int i;
9733
9734         if (!map)
9735                 return NULL;
9736
9737         if (!isl_space_is_named_or_nested(map->dim, type))
9738                 return map;
9739
9740         map = isl_map_cow(map);
9741         if (!map)
9742                 return NULL;
9743
9744         for (i = 0; i < map->n; ++i) {
9745                 map->p[i] = isl_basic_map_reset(map->p[i], type);
9746                 if (!map->p[i])
9747                         goto error;
9748         }
9749         map->dim = isl_space_reset(map->dim, type);
9750         if (!map->dim)
9751                 goto error;
9752
9753         return map;
9754 error:
9755         isl_map_free(map);
9756         return NULL;
9757 }
9758
9759 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
9760 {
9761         if (!bmap)
9762                 return NULL;
9763
9764         if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
9765                 return bmap;
9766
9767         bmap = isl_basic_map_cow(bmap);
9768         if (!bmap)
9769                 return NULL;
9770
9771         bmap->dim = isl_space_flatten(bmap->dim);
9772         if (!bmap->dim)
9773                 goto error;
9774
9775         bmap = isl_basic_map_finalize(bmap);
9776
9777         return bmap;
9778 error:
9779         isl_basic_map_free(bmap);
9780         return NULL;
9781 }
9782
9783 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
9784 {
9785         return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
9786 }
9787
9788 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
9789         __isl_take isl_basic_map *bmap)
9790 {
9791         if (!bmap)
9792                 return NULL;
9793
9794         if (!bmap->dim->nested[0])
9795                 return bmap;
9796
9797         bmap = isl_basic_map_cow(bmap);
9798         if (!bmap)
9799                 return NULL;
9800
9801         bmap->dim = isl_space_flatten_domain(bmap->dim);
9802         if (!bmap->dim)
9803                 goto error;
9804
9805         bmap = isl_basic_map_finalize(bmap);
9806
9807         return bmap;
9808 error:
9809         isl_basic_map_free(bmap);
9810         return NULL;
9811 }
9812
9813 __isl_give isl_basic_map *isl_basic_map_flatten_range(
9814         __isl_take isl_basic_map *bmap)
9815 {
9816         if (!bmap)
9817                 return NULL;
9818
9819         if (!bmap->dim->nested[1])
9820                 return bmap;
9821
9822         bmap = isl_basic_map_cow(bmap);
9823         if (!bmap)
9824                 return NULL;
9825
9826         bmap->dim = isl_space_flatten_range(bmap->dim);
9827         if (!bmap->dim)
9828                 goto error;
9829
9830         bmap = isl_basic_map_finalize(bmap);
9831
9832         return bmap;
9833 error:
9834         isl_basic_map_free(bmap);
9835         return NULL;
9836 }
9837
9838 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
9839 {
9840         int i;
9841
9842         if (!map)
9843                 return NULL;
9844
9845         if (!map->dim->nested[0] && !map->dim->nested[1])
9846                 return map;
9847
9848         map = isl_map_cow(map);
9849         if (!map)
9850                 return NULL;
9851
9852         for (i = 0; i < map->n; ++i) {
9853                 map->p[i] = isl_basic_map_flatten(map->p[i]);
9854                 if (!map->p[i])
9855                         goto error;
9856         }
9857         map->dim = isl_space_flatten(map->dim);
9858         if (!map->dim)
9859                 goto error;
9860
9861         return map;
9862 error:
9863         isl_map_free(map);
9864         return NULL;
9865 }
9866
9867 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
9868 {
9869         return (isl_set *)isl_map_flatten((isl_map *)set);
9870 }
9871
9872 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
9873 {
9874         isl_space *dim, *flat_dim;
9875         isl_map *map;
9876
9877         dim = isl_set_get_space(set);
9878         flat_dim = isl_space_flatten(isl_space_copy(dim));
9879         map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
9880         map = isl_map_intersect_domain(map, set);
9881
9882         return map;
9883 }
9884
9885 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
9886 {
9887         int i;
9888
9889         if (!map)
9890                 return NULL;
9891
9892         if (!map->dim->nested[0])
9893                 return map;
9894
9895         map = isl_map_cow(map);
9896         if (!map)
9897                 return NULL;
9898
9899         for (i = 0; i < map->n; ++i) {
9900                 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
9901                 if (!map->p[i])
9902                         goto error;
9903         }
9904         map->dim = isl_space_flatten_domain(map->dim);
9905         if (!map->dim)
9906                 goto error;
9907
9908         return map;
9909 error:
9910         isl_map_free(map);
9911         return NULL;
9912 }
9913
9914 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
9915 {
9916         int i;
9917
9918         if (!map)
9919                 return NULL;
9920
9921         if (!map->dim->nested[1])
9922                 return map;
9923
9924         map = isl_map_cow(map);
9925         if (!map)
9926                 return NULL;
9927
9928         for (i = 0; i < map->n; ++i) {
9929                 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
9930                 if (!map->p[i])
9931                         goto error;
9932         }
9933         map->dim = isl_space_flatten_range(map->dim);
9934         if (!map->dim)
9935                 goto error;
9936
9937         return map;
9938 error:
9939         isl_map_free(map);
9940         return NULL;
9941 }
9942
9943 /* Reorder the dimensions of "bmap" according to the given dim_map
9944  * and set the dimension specification to "dim".
9945  */
9946 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
9947         __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
9948 {
9949         isl_basic_map *res;
9950         unsigned flags;
9951
9952         bmap = isl_basic_map_cow(bmap);
9953         if (!bmap || !dim || !dim_map)
9954                 goto error;
9955
9956         flags = bmap->flags;
9957         ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
9958         ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
9959         ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
9960         res = isl_basic_map_alloc_space(dim,
9961                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
9962         res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
9963         if (res)
9964                 res->flags = flags;
9965         res = isl_basic_map_finalize(res);
9966         return res;
9967 error:
9968         free(dim_map);
9969         isl_basic_map_free(bmap);
9970         isl_space_free(dim);
9971         return NULL;
9972 }
9973
9974 /* Reorder the dimensions of "map" according to given reordering.
9975  */
9976 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
9977         __isl_take isl_reordering *r)
9978 {
9979         int i;
9980         struct isl_dim_map *dim_map;
9981
9982         map = isl_map_cow(map);
9983         dim_map = isl_dim_map_from_reordering(r);
9984         if (!map || !r || !dim_map)
9985                 goto error;
9986
9987         for (i = 0; i < map->n; ++i) {
9988                 struct isl_dim_map *dim_map_i;
9989
9990                 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
9991
9992                 map->p[i] = isl_basic_map_realign(map->p[i],
9993                                             isl_space_copy(r->dim), dim_map_i);
9994
9995                 if (!map->p[i])
9996                         goto error;
9997         }
9998
9999         map = isl_map_reset_space(map, isl_space_copy(r->dim));
10000
10001         isl_reordering_free(r);
10002         free(dim_map);
10003         return map;
10004 error:
10005         free(dim_map);
10006         isl_map_free(map);
10007         isl_reordering_free(r);
10008         return NULL;
10009 }
10010
10011 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10012         __isl_take isl_reordering *r)
10013 {
10014         return (isl_set *)isl_map_realign((isl_map *)set, r);
10015 }
10016
10017 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10018         __isl_take isl_space *model)
10019 {
10020         isl_ctx *ctx;
10021
10022         if (!map || !model)
10023                 goto error;
10024
10025         ctx = isl_space_get_ctx(model);
10026         if (!isl_space_has_named_params(model))
10027                 isl_die(ctx, isl_error_invalid,
10028                         "model has unnamed parameters", goto error);
10029         if (!isl_space_has_named_params(map->dim))
10030                 isl_die(ctx, isl_error_invalid,
10031                         "relation has unnamed parameters", goto error);
10032         if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10033                 isl_reordering *exp;
10034
10035                 model = isl_space_drop_dims(model, isl_dim_in,
10036                                         0, isl_space_dim(model, isl_dim_in));
10037                 model = isl_space_drop_dims(model, isl_dim_out,
10038                                         0, isl_space_dim(model, isl_dim_out));
10039                 exp = isl_parameter_alignment_reordering(map->dim, model);
10040                 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10041                 map = isl_map_realign(map, exp);
10042         }
10043
10044         isl_space_free(model);
10045         return map;
10046 error:
10047         isl_space_free(model);
10048         isl_map_free(map);
10049         return NULL;
10050 }
10051
10052 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10053         __isl_take isl_space *model)
10054 {
10055         return isl_map_align_params(set, model);
10056 }
10057
10058 /* Align the parameters of "bmap" to those of "model", introducing
10059  * additional parameters if needed.
10060  */
10061 __isl_give isl_basic_map *isl_basic_map_align_params(
10062         __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10063 {
10064         isl_ctx *ctx;
10065
10066         if (!bmap || !model)
10067                 goto error;
10068
10069         ctx = isl_space_get_ctx(model);
10070         if (!isl_space_has_named_params(model))
10071                 isl_die(ctx, isl_error_invalid,
10072                         "model has unnamed parameters", goto error);
10073         if (!isl_space_has_named_params(bmap->dim))
10074                 isl_die(ctx, isl_error_invalid,
10075                         "relation has unnamed parameters", goto error);
10076         if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10077                 isl_reordering *exp;
10078                 struct isl_dim_map *dim_map;
10079
10080                 model = isl_space_drop_dims(model, isl_dim_in,
10081                                         0, isl_space_dim(model, isl_dim_in));
10082                 model = isl_space_drop_dims(model, isl_dim_out,
10083                                         0, isl_space_dim(model, isl_dim_out));
10084                 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10085                 exp = isl_reordering_extend_space(exp,
10086                                         isl_basic_map_get_space(bmap));
10087                 dim_map = isl_dim_map_from_reordering(exp);
10088                 bmap = isl_basic_map_realign(bmap,
10089                                     exp ? isl_space_copy(exp->dim) : NULL,
10090                                     isl_dim_map_extend(dim_map, bmap));
10091                 isl_reordering_free(exp);
10092                 free(dim_map);
10093         }
10094
10095         isl_space_free(model);
10096         return bmap;
10097 error:
10098         isl_space_free(model);
10099         isl_basic_map_free(bmap);
10100         return NULL;
10101 }
10102
10103 /* Align the parameters of "bset" to those of "model", introducing
10104  * additional parameters if needed.
10105  */
10106 __isl_give isl_basic_set *isl_basic_set_align_params(
10107         __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10108 {
10109         return isl_basic_map_align_params(bset, model);
10110 }
10111
10112 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10113                 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10114                 enum isl_dim_type c2, enum isl_dim_type c3,
10115                 enum isl_dim_type c4, enum isl_dim_type c5)
10116 {
10117         enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10118         struct isl_mat *mat;
10119         int i, j, k;
10120         int pos;
10121
10122         if (!bmap)
10123                 return NULL;
10124         mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
10125                                 isl_basic_map_total_dim(bmap) + 1);
10126         if (!mat)
10127                 return NULL;
10128         for (i = 0; i < bmap->n_eq; ++i)
10129                 for (j = 0, pos = 0; j < 5; ++j) {
10130                         int off = isl_basic_map_offset(bmap, c[j]);
10131                         for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10132                                 isl_int_set(mat->row[i][pos],
10133                                             bmap->eq[i][off + k]);
10134                                 ++pos;
10135                         }
10136                 }
10137
10138         return mat;
10139 }
10140
10141 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
10142                 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10143                 enum isl_dim_type c2, enum isl_dim_type c3,
10144                 enum isl_dim_type c4, enum isl_dim_type c5)
10145 {
10146         enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10147         struct isl_mat *mat;
10148         int i, j, k;
10149         int pos;
10150
10151         if (!bmap)
10152                 return NULL;
10153         mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
10154                                 isl_basic_map_total_dim(bmap) + 1);
10155         if (!mat)
10156                 return NULL;
10157         for (i = 0; i < bmap->n_ineq; ++i)
10158                 for (j = 0, pos = 0; j < 5; ++j) {
10159                         int off = isl_basic_map_offset(bmap, c[j]);
10160                         for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10161                                 isl_int_set(mat->row[i][pos],
10162                                             bmap->ineq[i][off + k]);
10163                                 ++pos;
10164                         }
10165                 }
10166
10167         return mat;
10168 }
10169
10170 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
10171         __isl_take isl_space *dim,
10172         __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10173         enum isl_dim_type c2, enum isl_dim_type c3,
10174         enum isl_dim_type c4, enum isl_dim_type c5)
10175 {
10176         enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10177         isl_basic_map *bmap;
10178         unsigned total;
10179         unsigned extra;
10180         int i, j, k, l;
10181         int pos;
10182
10183         if (!dim || !eq || !ineq)
10184                 goto error;
10185
10186         if (eq->n_col != ineq->n_col)
10187                 isl_die(dim->ctx, isl_error_invalid,
10188                         "equalities and inequalities matrices should have "
10189                         "same number of columns", goto error);
10190
10191         total = 1 + isl_space_dim(dim, isl_dim_all);
10192
10193         if (eq->n_col < total)
10194                 isl_die(dim->ctx, isl_error_invalid,
10195                         "number of columns too small", goto error);
10196
10197         extra = eq->n_col - total;
10198
10199         bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
10200                                        eq->n_row, ineq->n_row);
10201         if (!bmap)
10202                 goto error;
10203         for (i = 0; i < extra; ++i) {
10204                 k = isl_basic_map_alloc_div(bmap);
10205                 if (k < 0)
10206                         goto error;
10207                 isl_int_set_si(bmap->div[k][0], 0);
10208         }
10209         for (i = 0; i < eq->n_row; ++i) {
10210                 l = isl_basic_map_alloc_equality(bmap);
10211                 if (l < 0)
10212                         goto error;
10213                 for (j = 0, pos = 0; j < 5; ++j) {
10214                         int off = isl_basic_map_offset(bmap, c[j]);
10215                         for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10216                                 isl_int_set(bmap->eq[l][off + k], 
10217                                             eq->row[i][pos]);
10218                                 ++pos;
10219                         }
10220                 }
10221         }
10222         for (i = 0; i < ineq->n_row; ++i) {
10223                 l = isl_basic_map_alloc_inequality(bmap);
10224                 if (l < 0)
10225                         goto error;
10226                 for (j = 0, pos = 0; j < 5; ++j) {
10227                         int off = isl_basic_map_offset(bmap, c[j]);
10228                         for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10229                                 isl_int_set(bmap->ineq[l][off + k], 
10230                                             ineq->row[i][pos]);
10231                                 ++pos;
10232                         }
10233                 }
10234         }
10235
10236         isl_space_free(dim);
10237         isl_mat_free(eq);
10238         isl_mat_free(ineq);
10239
10240         return bmap;
10241 error:
10242         isl_space_free(dim);
10243         isl_mat_free(eq);
10244         isl_mat_free(ineq);
10245         return NULL;
10246 }
10247
10248 __isl_give isl_mat *isl_basic_set_equalities_matrix(
10249         __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10250         enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10251 {
10252         return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
10253                                                 c1, c2, c3, c4, isl_dim_in);
10254 }
10255
10256 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
10257         __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10258         enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10259 {
10260         return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
10261                                                  c1, c2, c3, c4, isl_dim_in);
10262 }
10263
10264 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
10265         __isl_take isl_space *dim,
10266         __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10267         enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10268 {
10269         return (isl_basic_set*)
10270             isl_basic_map_from_constraint_matrices(dim, eq, ineq,
10271                                                    c1, c2, c3, c4, isl_dim_in);
10272 }
10273
10274 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
10275 {
10276         if (!bmap)
10277                 return -1;
10278         
10279         return isl_space_can_zip(bmap->dim);
10280 }
10281
10282 int isl_map_can_zip(__isl_keep isl_map *map)
10283 {
10284         if (!map)
10285                 return -1;
10286         
10287         return isl_space_can_zip(map->dim);
10288 }
10289
10290 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10291  * (A -> C) -> (B -> D).
10292  */
10293 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10294 {
10295         unsigned pos;
10296         unsigned n1;
10297         unsigned n2;
10298
10299         if (!bmap)
10300                 return NULL;
10301
10302         if (!isl_basic_map_can_zip(bmap))
10303                 isl_die(bmap->ctx, isl_error_invalid,
10304                         "basic map cannot be zipped", goto error);
10305         pos = isl_basic_map_offset(bmap, isl_dim_in) +
10306                 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10307         n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10308         n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10309         bmap = isl_basic_map_cow(bmap);
10310         bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10311         if (!bmap)
10312                 return NULL;
10313         bmap->dim = isl_space_zip(bmap->dim);
10314         if (!bmap->dim)
10315                 goto error;
10316         return bmap;
10317 error:
10318         isl_basic_map_free(bmap);
10319         return NULL;
10320 }
10321
10322 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10323  * (A -> C) -> (B -> D).
10324  */
10325 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
10326 {
10327         int i;
10328
10329         if (!map)
10330                 return NULL;
10331
10332         if (!isl_map_can_zip(map))
10333                 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
10334                         goto error);
10335
10336         map = isl_map_cow(map);
10337         if (!map)
10338                 return NULL;
10339
10340         for (i = 0; i < map->n; ++i) {
10341                 map->p[i] = isl_basic_map_zip(map->p[i]);
10342                 if (!map->p[i])
10343                         goto error;
10344         }
10345
10346         map->dim = isl_space_zip(map->dim);
10347         if (!map->dim)
10348                 goto error;
10349
10350         return map;
10351 error:
10352         isl_map_free(map);
10353         return NULL;
10354 }
10355
10356 /* Can we apply isl_basic_map_curry to "bmap"?
10357  * That is, does it have a nested relation in its domain?
10358  */
10359 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
10360 {
10361         if (!bmap)
10362                 return -1;
10363
10364         return isl_space_can_curry(bmap->dim);
10365 }
10366
10367 /* Can we apply isl_map_curry to "map"?
10368  * That is, does it have a nested relation in its domain?
10369  */
10370 int isl_map_can_curry(__isl_keep isl_map *map)
10371 {
10372         if (!map)
10373                 return -1;
10374
10375         return isl_space_can_curry(map->dim);
10376 }
10377
10378 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10379  * A -> (B -> C).
10380  */
10381 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
10382 {
10383
10384         if (!bmap)
10385                 return NULL;
10386
10387         if (!isl_basic_map_can_curry(bmap))
10388                 isl_die(bmap->ctx, isl_error_invalid,
10389                         "basic map cannot be curried", goto error);
10390         bmap->dim = isl_space_curry(bmap->dim);
10391         if (!bmap->dim)
10392                 goto error;
10393         return bmap;
10394 error:
10395         isl_basic_map_free(bmap);
10396         return NULL;
10397 }
10398
10399 /* Given a map (A -> B) -> C, return the corresponding map
10400  * A -> (B -> C).
10401  */
10402 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
10403 {
10404         int i;
10405
10406         if (!map)
10407                 return NULL;
10408
10409         if (!isl_map_can_curry(map))
10410                 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
10411                         goto error);
10412
10413         map = isl_map_cow(map);
10414         if (!map)
10415                 return NULL;
10416
10417         for (i = 0; i < map->n; ++i) {
10418                 map->p[i] = isl_basic_map_curry(map->p[i]);
10419                 if (!map->p[i])
10420                         goto error;
10421         }
10422
10423         map->dim = isl_space_curry(map->dim);
10424         if (!map->dim)
10425                 goto error;
10426
10427         return map;
10428 error:
10429         isl_map_free(map);
10430         return NULL;
10431 }
10432
10433 /* Can we apply isl_basic_map_uncurry to "bmap"?
10434  * That is, does it have a nested relation in its domain?
10435  */
10436 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
10437 {
10438         if (!bmap)
10439                 return -1;
10440
10441         return isl_space_can_uncurry(bmap->dim);
10442 }
10443
10444 /* Can we apply isl_map_uncurry to "map"?
10445  * That is, does it have a nested relation in its domain?
10446  */
10447 int isl_map_can_uncurry(__isl_keep isl_map *map)
10448 {
10449         if (!map)
10450                 return -1;
10451
10452         return isl_space_can_uncurry(map->dim);
10453 }
10454
10455 /* Given a basic map A -> (B -> C), return the corresponding basic map
10456  * (A -> B) -> C.
10457  */
10458 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
10459 {
10460
10461         if (!bmap)
10462                 return NULL;
10463
10464         if (!isl_basic_map_can_uncurry(bmap))
10465                 isl_die(bmap->ctx, isl_error_invalid,
10466                         "basic map cannot be uncurried",
10467                         return isl_basic_map_free(bmap));
10468         bmap->dim = isl_space_uncurry(bmap->dim);
10469         if (!bmap->dim)
10470                 return isl_basic_map_free(bmap);
10471         return bmap;
10472 }
10473
10474 /* Given a map A -> (B -> C), return the corresponding map
10475  * (A -> B) -> C.
10476  */
10477 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
10478 {
10479         int i;
10480
10481         if (!map)
10482                 return NULL;
10483
10484         if (!isl_map_can_uncurry(map))
10485                 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
10486                         return isl_map_free(map));
10487
10488         map = isl_map_cow(map);
10489         if (!map)
10490                 return NULL;
10491
10492         for (i = 0; i < map->n; ++i) {
10493                 map->p[i] = isl_basic_map_uncurry(map->p[i]);
10494                 if (!map->p[i])
10495                         return isl_map_free(map);
10496         }
10497
10498         map->dim = isl_space_uncurry(map->dim);
10499         if (!map->dim)
10500                 return isl_map_free(map);
10501
10502         return map;
10503 }
10504
10505 /* Construct a basic map mapping the domain of the affine expression
10506  * to a one-dimensional range prescribed by the affine expression.
10507  */
10508 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
10509 {
10510         int k;
10511         int pos;
10512         isl_local_space *ls;
10513         isl_basic_map *bmap;
10514
10515         if (!aff)
10516                 return NULL;
10517
10518         ls = isl_aff_get_local_space(aff);
10519         bmap = isl_basic_map_from_local_space(ls);
10520         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
10521         k = isl_basic_map_alloc_equality(bmap);
10522         if (k < 0)
10523                 goto error;
10524
10525         pos = isl_basic_map_offset(bmap, isl_dim_out);
10526         isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
10527         isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
10528         isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
10529                     aff->v->size - (pos + 1));
10530
10531         isl_aff_free(aff);
10532         bmap = isl_basic_map_finalize(bmap);
10533         return bmap;
10534 error:
10535         isl_aff_free(aff);
10536         isl_basic_map_free(bmap);
10537         return NULL;
10538 }
10539
10540 /* Construct a map mapping the domain of the affine expression
10541  * to a one-dimensional range prescribed by the affine expression.
10542  */
10543 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
10544 {
10545         isl_basic_map *bmap;
10546
10547         bmap = isl_basic_map_from_aff(aff);
10548         return isl_map_from_basic_map(bmap);
10549 }
10550
10551 /* Construct a basic map mapping the domain the multi-affine expression
10552  * to its range, with each dimension in the range equated to the
10553  * corresponding affine expression.
10554  */
10555 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
10556         __isl_take isl_multi_aff *maff)
10557 {
10558         int i;
10559         isl_space *space;
10560         isl_basic_map *bmap;
10561
10562         if (!maff)
10563                 return NULL;
10564
10565         if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
10566                 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
10567                         "invalid space", return isl_multi_aff_free(maff));
10568
10569         space = isl_space_domain(isl_multi_aff_get_space(maff));
10570         bmap = isl_basic_map_universe(isl_space_from_domain(space));
10571
10572         for (i = 0; i < maff->n; ++i) {
10573                 isl_aff *aff;
10574                 isl_basic_map *bmap_i;
10575
10576                 aff = isl_aff_copy(maff->p[i]);
10577                 bmap_i = isl_basic_map_from_aff(aff);
10578
10579                 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10580         }
10581
10582         bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
10583
10584         isl_multi_aff_free(maff);
10585         return bmap;
10586 }
10587
10588 /* Construct a map mapping the domain the multi-affine expression
10589  * to its range, with each dimension in the range equated to the
10590  * corresponding affine expression.
10591  */
10592 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
10593 {
10594         isl_basic_map *bmap;
10595
10596         bmap = isl_basic_map_from_multi_aff(maff);
10597         return isl_map_from_basic_map(bmap);
10598 }
10599
10600 /* Construct a basic map mapping a domain in the given space to
10601  * to an n-dimensional range, with n the number of elements in the list,
10602  * where each coordinate in the range is prescribed by the
10603  * corresponding affine expression.
10604  * The domains of all affine expressions in the list are assumed to match
10605  * domain_dim.
10606  */
10607 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
10608         __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
10609 {
10610         int i;
10611         isl_space *dim;
10612         isl_basic_map *bmap;
10613
10614         if (!list)
10615                 return NULL;
10616
10617         dim = isl_space_from_domain(domain_dim);
10618         bmap = isl_basic_map_universe(dim);
10619
10620         for (i = 0; i < list->n; ++i) {
10621                 isl_aff *aff;
10622                 isl_basic_map *bmap_i;
10623
10624                 aff = isl_aff_copy(list->p[i]);
10625                 bmap_i = isl_basic_map_from_aff(aff);
10626
10627                 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
10628         }
10629
10630         isl_aff_list_free(list);
10631         return bmap;
10632 }
10633
10634 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
10635         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10636 {
10637         return isl_map_equate(set, type1, pos1, type2, pos2);
10638 }
10639
10640 /* Construct a basic map where the given dimensions are equal to each other.
10641  */
10642 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
10643         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10644 {
10645         isl_basic_map *bmap = NULL;
10646         int i;
10647
10648         if (!space)
10649                 return NULL;
10650
10651         if (pos1 >= isl_space_dim(space, type1))
10652                 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10653                         "index out of bounds", goto error);
10654         if (pos2 >= isl_space_dim(space, type2))
10655                 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10656                         "index out of bounds", goto error);
10657
10658         if (type1 == type2 && pos1 == pos2)
10659                 return isl_basic_map_universe(space);
10660
10661         bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
10662         i = isl_basic_map_alloc_equality(bmap);
10663         if (i < 0)
10664                 goto error;
10665         isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10666         pos1 += isl_basic_map_offset(bmap, type1);
10667         pos2 += isl_basic_map_offset(bmap, type2);
10668         isl_int_set_si(bmap->eq[i][pos1], -1);
10669         isl_int_set_si(bmap->eq[i][pos2], 1);
10670         bmap = isl_basic_map_finalize(bmap);
10671         isl_space_free(space);
10672         return bmap;
10673 error:
10674         isl_space_free(space);
10675         isl_basic_map_free(bmap);
10676         return NULL;
10677 }
10678
10679 /* Add a constraint imposing that the given two dimensions are equal.
10680  */
10681 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
10682         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10683 {
10684         isl_basic_map *eq;
10685
10686         eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
10687
10688         bmap = isl_basic_map_intersect(bmap, eq);
10689
10690         return bmap;
10691 }
10692
10693 /* Add a constraint imposing that the given two dimensions are equal.
10694  */
10695 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
10696         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10697 {
10698         isl_basic_map *bmap;
10699
10700         bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
10701
10702         map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10703
10704         return map;
10705 }
10706
10707 /* Add a constraint imposing that the given two dimensions have opposite values.
10708  */
10709 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
10710         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10711 {
10712         isl_basic_map *bmap = NULL;
10713         int i;
10714
10715         if (!map)
10716                 return NULL;
10717
10718         if (pos1 >= isl_map_dim(map, type1))
10719                 isl_die(map->ctx, isl_error_invalid,
10720                         "index out of bounds", goto error);
10721         if (pos2 >= isl_map_dim(map, type2))
10722                 isl_die(map->ctx, isl_error_invalid,
10723                         "index out of bounds", goto error);
10724
10725         bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
10726         i = isl_basic_map_alloc_equality(bmap);
10727         if (i < 0)
10728                 goto error;
10729         isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
10730         pos1 += isl_basic_map_offset(bmap, type1);
10731         pos2 += isl_basic_map_offset(bmap, type2);
10732         isl_int_set_si(bmap->eq[i][pos1], 1);
10733         isl_int_set_si(bmap->eq[i][pos2], 1);
10734         bmap = isl_basic_map_finalize(bmap);
10735
10736         map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10737
10738         return map;
10739 error:
10740         isl_basic_map_free(bmap);
10741         isl_map_free(map);
10742         return NULL;
10743 }
10744
10745 /* Add a constraint imposing that the value of the first dimension is
10746  * greater than or equal to that of the second.
10747  */
10748 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
10749         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10750 {
10751         isl_constraint *c;
10752         isl_local_space *ls;
10753
10754         if (!bmap)
10755                 return NULL;
10756
10757         if (pos1 >= isl_basic_map_dim(bmap, type1))
10758                 isl_die(bmap->ctx, isl_error_invalid,
10759                         "index out of bounds", return isl_basic_map_free(bmap));
10760         if (pos2 >= isl_basic_map_dim(bmap, type2))
10761                 isl_die(bmap->ctx, isl_error_invalid,
10762                         "index out of bounds", return isl_basic_map_free(bmap));
10763
10764         if (type1 == type2 && pos1 == pos2)
10765                 return bmap;
10766
10767         ls = isl_local_space_from_space(isl_basic_map_get_space(bmap));
10768         c = isl_inequality_alloc(ls);
10769         c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
10770         c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
10771         bmap = isl_basic_map_add_constraint(bmap, c);
10772
10773         return bmap;
10774 }
10775
10776 /* Add a constraint imposing that the value of the first dimension is
10777  * greater than that of the second.
10778  */
10779 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
10780         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10781 {
10782         isl_basic_map *bmap = NULL;
10783         int i;
10784
10785         if (!map)
10786                 return NULL;
10787
10788         if (pos1 >= isl_map_dim(map, type1))
10789                 isl_die(map->ctx, isl_error_invalid,
10790                         "index out of bounds", goto error);
10791         if (pos2 >= isl_map_dim(map, type2))
10792                 isl_die(map->ctx, isl_error_invalid,
10793                         "index out of bounds", goto error);
10794
10795         if (type1 == type2 && pos1 == pos2) {
10796                 isl_space *space = isl_map_get_space(map);
10797                 isl_map_free(map);
10798                 return isl_map_empty(space);
10799         }
10800
10801         bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 0, 1);
10802         i = isl_basic_map_alloc_inequality(bmap);
10803         if (i < 0)
10804                 goto error;
10805         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
10806         pos1 += isl_basic_map_offset(bmap, type1);
10807         pos2 += isl_basic_map_offset(bmap, type2);
10808         isl_int_set_si(bmap->ineq[i][pos1], 1);
10809         isl_int_set_si(bmap->ineq[i][pos2], -1);
10810         isl_int_set_si(bmap->ineq[i][0], -1);
10811         bmap = isl_basic_map_finalize(bmap);
10812
10813         map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
10814
10815         return map;
10816 error:
10817         isl_basic_map_free(bmap);
10818         isl_map_free(map);
10819         return NULL;
10820 }
10821
10822 /* Add a constraint imposing that the value of the first dimension is
10823  * smaller than that of the second.
10824  */
10825 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
10826         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
10827 {
10828         return isl_map_order_gt(map, type2, pos2, type1, pos1);
10829 }
10830
10831 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
10832         int pos)
10833 {
10834         isl_aff *div;
10835         isl_local_space *ls;
10836
10837         if (!bmap)
10838                 return NULL;
10839
10840         if (!isl_basic_map_divs_known(bmap))
10841                 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
10842                         "some divs are unknown", return NULL);
10843
10844         ls = isl_basic_map_get_local_space(bmap);
10845         div = isl_local_space_get_div(ls, pos);
10846         isl_local_space_free(ls);
10847
10848         return div;
10849 }
10850
10851 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
10852         int pos)
10853 {
10854         return isl_basic_map_get_div(bset, pos);
10855 }
10856
10857 /* Plug in "subs" for dimension "type", "pos" of "bset".
10858  *
10859  * Let i be the dimension to replace and let "subs" be of the form
10860  *
10861  *      f/d
10862  *
10863  * Any integer division with a non-zero coefficient for i,
10864  *
10865  *      floor((a i + g)/m)
10866  *
10867  * is replaced by
10868  *
10869  *      floor((a f + d g)/(m d))
10870  *
10871  * Constraints of the form
10872  *
10873  *      a i + g
10874  *
10875  * are replaced by
10876  *
10877  *      a f + d g
10878  *
10879  * We currently require that "subs" is an integral expression.
10880  * Handling rational expressions may require us to add stride constraints
10881  * as we do in isl_basic_set_preimage_multi_aff.
10882  */
10883 __isl_give isl_basic_set *isl_basic_set_substitute(
10884         __isl_take isl_basic_set *bset,
10885         enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10886 {
10887         int i;
10888         isl_int v;
10889         isl_ctx *ctx;
10890
10891         if (bset && isl_basic_set_plain_is_empty(bset))
10892                 return bset;
10893
10894         bset = isl_basic_set_cow(bset);
10895         if (!bset || !subs)
10896                 goto error;
10897
10898         ctx = isl_basic_set_get_ctx(bset);
10899         if (!isl_space_is_equal(bset->dim, subs->ls->dim))
10900                 isl_die(ctx, isl_error_invalid,
10901                         "spaces don't match", goto error);
10902         if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
10903                 isl_die(ctx, isl_error_unsupported,
10904                         "cannot handle divs yet", goto error);
10905         if (!isl_int_is_one(subs->v->el[0]))
10906                 isl_die(ctx, isl_error_invalid,
10907                         "can only substitute integer expressions", goto error);
10908
10909         pos += isl_basic_set_offset(bset, type);
10910
10911         isl_int_init(v);
10912
10913         for (i = 0; i < bset->n_eq; ++i) {
10914                 if (isl_int_is_zero(bset->eq[i][pos]))
10915                         continue;
10916                 isl_int_set(v, bset->eq[i][pos]);
10917                 isl_int_set_si(bset->eq[i][pos], 0);
10918                 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
10919                                 v, subs->v->el + 1, subs->v->size - 1);
10920         }
10921
10922         for (i = 0; i < bset->n_ineq; ++i) {
10923                 if (isl_int_is_zero(bset->ineq[i][pos]))
10924                         continue;
10925                 isl_int_set(v, bset->ineq[i][pos]);
10926                 isl_int_set_si(bset->ineq[i][pos], 0);
10927                 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
10928                                 v, subs->v->el + 1, subs->v->size - 1);
10929         }
10930
10931         for (i = 0; i < bset->n_div; ++i) {
10932                 if (isl_int_is_zero(bset->div[i][1 + pos]))
10933                         continue;
10934                 isl_int_set(v, bset->div[i][1 + pos]);
10935                 isl_int_set_si(bset->div[i][1 + pos], 0);
10936                 isl_seq_combine(bset->div[i] + 1,
10937                                 subs->v->el[0], bset->div[i] + 1,
10938                                 v, subs->v->el + 1, subs->v->size - 1);
10939                 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
10940         }
10941
10942         isl_int_clear(v);
10943
10944         bset = isl_basic_set_simplify(bset);
10945         return isl_basic_set_finalize(bset);
10946 error:
10947         isl_basic_set_free(bset);
10948         return NULL;
10949 }
10950
10951 /* Plug in "subs" for dimension "type", "pos" of "set".
10952  */
10953 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
10954         enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
10955 {
10956         int i;
10957
10958         if (set && isl_set_plain_is_empty(set))
10959                 return set;
10960
10961         set = isl_set_cow(set);
10962         if (!set || !subs)
10963                 goto error;
10964
10965         for (i = set->n - 1; i >= 0; --i) {
10966                 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
10967                 if (remove_if_empty(set, i) < 0)
10968                         goto error;
10969         }
10970
10971         return set;
10972 error:
10973         isl_set_free(set);
10974         return NULL;
10975 }
10976
10977 /* Check if the range of "ma" is compatible with "space".
10978  * Return -1 if anything is wrong.
10979  */
10980 static int check_space_compatible_range_multi_aff(
10981         __isl_keep isl_space *space, __isl_keep isl_multi_aff *ma)
10982 {
10983         int m;
10984         isl_space *ma_space;
10985
10986         ma_space = isl_multi_aff_get_space(ma);
10987         m = isl_space_is_range_internal(space, ma_space);
10988         isl_space_free(ma_space);
10989         if (m >= 0 && !m)
10990                 isl_die(isl_space_get_ctx(space), isl_error_invalid,
10991                         "spaces don't match", return -1);
10992         return m;
10993 }
10994
10995 /* Check if the range of "ma" is compatible with "bset".
10996  * Return -1 if anything is wrong.
10997  */
10998 static int check_basic_set_compatible_range_multi_aff(
10999         __isl_keep isl_basic_set *bset, __isl_keep isl_multi_aff *ma)
11000 {
11001         return check_space_compatible_range_multi_aff(bset->dim, ma);
11002 }
11003
11004 /* Copy the divs from "ma" to "bset", adding zeros for the coefficients
11005  * of the other divs in "bset".
11006  */
11007 static int set_ma_divs(__isl_keep isl_basic_set *bset,
11008         __isl_keep isl_multi_aff *ma, int n_div)
11009 {
11010         int i;
11011         isl_local_space *ls;
11012
11013         if (n_div == 0)
11014                 return 0;
11015
11016         ls = isl_aff_get_domain_local_space(ma->p[0]);
11017         if (!ls)
11018                 return -1;
11019
11020         for (i = 0; i < n_div; ++i) {
11021                 isl_seq_cpy(bset->div[i], ls->div->row[i], ls->div->n_col);
11022                 isl_seq_clr(bset->div[i] + ls->div->n_col, bset->n_div - n_div);
11023                 if (isl_basic_set_add_div_constraints(bset, i) < 0)
11024                         goto error;
11025         }
11026
11027         isl_local_space_free(ls);
11028         return 0;
11029 error:
11030         isl_local_space_free(ls);
11031         return -1;
11032 }
11033
11034 /* How many stride constraints does "ma" enforce?
11035  * That is, how many of the affine expressions have a denominator
11036  * different from one?
11037  */
11038 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
11039 {
11040         int i;
11041         int strides = 0;
11042
11043         for (i = 0; i < ma->n; ++i)
11044                 if (!isl_int_is_one(ma->p[i]->v->el[0]))
11045                         strides++;
11046
11047         return strides;
11048 }
11049
11050 /* For each affine expression in ma of the form
11051  *
11052  *      x_i = (f_i y + h_i)/m_i
11053  *
11054  * with m_i different from one, add a constraint to "bset"
11055  * of the form
11056  *
11057  *      f_i y + h_i = m_i alpha_i
11058  *
11059  * with alpha_i an additional existentially quantified variable.
11060  */
11061 static __isl_give isl_basic_set *add_ma_strides(
11062         __isl_take isl_basic_set *bset, __isl_keep isl_multi_aff *ma)
11063 {
11064         int i, k;
11065         int div;
11066         int total;
11067
11068         total = isl_basic_set_total_dim(bset);
11069         for (i = 0; i < ma->n; ++i) {
11070                 int len;
11071
11072                 if (isl_int_is_one(ma->p[i]->v->el[0]))
11073                         continue;
11074                 div = isl_basic_set_alloc_div(bset);
11075                 k = isl_basic_set_alloc_equality(bset);
11076                 if (div < 0 || k < 0)
11077                         goto error;
11078                 isl_int_set_si(bset->div[div][0], 0);
11079                 len = ma->p[i]->v->size;
11080                 isl_seq_cpy(bset->eq[k], ma->p[i]->v->el + 1, len - 1);
11081                 isl_seq_clr(bset->eq[k] + len - 1, 1 + total - (len - 1));
11082                 isl_int_neg(bset->eq[k][1 + total], ma->p[i]->v->el[0]);
11083                 total++;
11084         }
11085
11086         return bset;
11087 error:
11088         isl_basic_set_free(bset);
11089         return NULL;
11090 }
11091
11092 /* Compute the preimage of "bset" under the function represented by "ma".
11093  * In other words, plug in "ma" in "bset".  The result is a basic set
11094  * that lives in the domain space of "ma".
11095  *
11096  * If bset is represented by
11097  *
11098  *      A(p) + B x + C(divs) >= 0
11099  *
11100  * and ma is represented by
11101  *
11102  *      x = D(p) + F(y) + G(divs')
11103  *
11104  * then the result is
11105  *
11106  *      A(p) + B D(p) + B F(y) + B G(divs') + C(divs) >= 0
11107  *
11108  * The divs in the input set are similarly adjusted.
11109  * In particular
11110  *
11111  *      floor((a_i(p) + b_i x + c_i(divs))/n_i)
11112  *
11113  * becomes
11114  *
11115  *      floor((a_i(p) + b_i D(p) + b_i F(y) + B_i G(divs') + c_i(divs))/n_i)
11116  *
11117  * If bset is not a rational set and if F(y) involves any denominators
11118  *
11119  *      x_i = (f_i y + h_i)/m_i
11120  *
11121  * the additional constraints are added to ensure that we only
11122  * map back integer points.  That is we enforce
11123  *
11124  *      f_i y + h_i = m_i alpha_i
11125  *
11126  * with alpha_i an additional existentially quantified variable.
11127  *
11128  * We first copy over the divs from "ma".
11129  * Then we add the modified constraints and divs from "bset".
11130  * Finally, we add the stride constraints, if needed.
11131  */
11132 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
11133         __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
11134 {
11135         int i, k;
11136         isl_space *space;
11137         isl_basic_set *res = NULL;
11138         int n_div_bset, n_div_ma;
11139         isl_int f, c1, c2, g;
11140         int rational, strides;
11141
11142         isl_int_init(f);
11143         isl_int_init(c1);
11144         isl_int_init(c2);
11145         isl_int_init(g);
11146
11147         ma = isl_multi_aff_align_divs(ma);
11148         if (!bset || !ma)
11149                 goto error;
11150         if (check_basic_set_compatible_range_multi_aff(bset, ma) < 0)
11151                 goto error;
11152
11153         n_div_bset = isl_basic_set_dim(bset, isl_dim_div);
11154         n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
11155
11156         space = isl_space_domain(isl_multi_aff_get_space(ma));
11157         rational = isl_basic_set_is_rational(bset);
11158         strides = rational ? 0 : multi_aff_strides(ma);
11159         res = isl_basic_set_alloc_space(space, n_div_ma + n_div_bset + strides,
11160                             bset->n_eq + strides, bset->n_ineq + 2 * n_div_ma);
11161         if (rational)
11162                 res = isl_basic_set_set_rational(res);
11163
11164         for (i = 0; i < n_div_ma + n_div_bset; ++i)
11165                 if (isl_basic_set_alloc_div(res) < 0)
11166                         goto error;
11167
11168         if (set_ma_divs(res, ma, n_div_ma) < 0)
11169                 goto error;
11170
11171         for (i = 0; i < bset->n_eq; ++i) {
11172                 k = isl_basic_set_alloc_equality(res);
11173                 if (k < 0)
11174                         goto error;
11175                 isl_seq_preimage(res->eq[k], bset->eq[i], ma, n_div_ma,
11176                                         n_div_bset, f, c1, c2, g, 0);
11177         }
11178
11179         for (i = 0; i < bset->n_ineq; ++i) {
11180                 k = isl_basic_set_alloc_inequality(res);
11181                 if (k < 0)
11182                         goto error;
11183                 isl_seq_preimage(res->ineq[k], bset->ineq[i], ma, n_div_ma,
11184                                         n_div_bset, f, c1, c2, g, 0);
11185         }
11186
11187         for (i = 0; i < bset->n_div; ++i) {
11188                 if (isl_int_is_zero(bset->div[i][0])) {
11189                         isl_int_set_si(res->div[n_div_ma + i][0], 0);
11190                         continue;
11191                 }
11192                 isl_seq_preimage(res->div[n_div_ma + i], bset->div[i],
11193                                     ma, n_div_ma, n_div_bset, f, c1, c2, g, 1);
11194         }
11195
11196         if (strides)
11197                 res = add_ma_strides(res, ma);
11198
11199         isl_int_clear(f);
11200         isl_int_clear(c1);
11201         isl_int_clear(c2);
11202         isl_int_clear(g);
11203         isl_basic_set_free(bset);
11204         isl_multi_aff_free(ma);
11205         res = isl_basic_set_simplify(res);
11206         return isl_basic_set_finalize(res);
11207 error:
11208         isl_int_clear(f);
11209         isl_int_clear(c1);
11210         isl_int_clear(c2);
11211         isl_int_clear(g);
11212         isl_basic_set_free(bset);
11213         isl_multi_aff_free(ma);
11214         isl_basic_set_free(res);
11215         return NULL;
11216 }
11217
11218 /* Check if the range of "ma" is compatible with "set".
11219  * Return -1 if anything is wrong.
11220  */
11221 static int check_set_compatible_range_multi_aff(
11222         __isl_keep isl_set *set, __isl_keep isl_multi_aff *ma)
11223 {
11224         return check_space_compatible_range_multi_aff(set->dim, ma);
11225 }
11226
11227 /* Compute the preimage of "set" under the function represented by "ma".
11228  * In other words, plug in "ma" in "set.  The result is a set
11229  * that lives in the domain space of "ma".
11230  */
11231 static __isl_give isl_set *set_preimage_multi_aff(__isl_take isl_set *set,
11232         __isl_take isl_multi_aff *ma)
11233 {
11234         int i;
11235
11236         set = isl_set_cow(set);
11237         ma = isl_multi_aff_align_divs(ma);
11238         if (!set || !ma)
11239                 goto error;
11240         if (check_set_compatible_range_multi_aff(set, ma) < 0)
11241                 goto error;
11242
11243         for (i = 0; i < set->n; ++i) {
11244                 set->p[i] = isl_basic_set_preimage_multi_aff(set->p[i],
11245                                                         isl_multi_aff_copy(ma));
11246                 if (!set->p[i])
11247                         goto error;
11248         }
11249
11250         isl_space_free(set->dim);
11251         set->dim = isl_multi_aff_get_domain_space(ma);
11252         if (!set->dim)
11253                 goto error;
11254
11255         isl_multi_aff_free(ma);
11256         if (set->n > 1)
11257                 ISL_F_CLR(set, ISL_MAP_DISJOINT);
11258         ISL_F_CLR(set, ISL_SET_NORMALIZED);
11259         return set;
11260 error:
11261         isl_multi_aff_free(ma);
11262         isl_set_free(set);
11263         return NULL;
11264 }
11265
11266 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
11267         __isl_take isl_multi_aff *ma)
11268 {
11269         if (!set || !ma)
11270                 goto error;
11271
11272         if (isl_space_match(set->dim, isl_dim_param, ma->space, isl_dim_param))
11273                 return set_preimage_multi_aff(set, ma);
11274
11275         if (!isl_space_has_named_params(set->dim) ||
11276             !isl_space_has_named_params(ma->space))
11277                 isl_die(set->ctx, isl_error_invalid,
11278                         "unaligned unnamed parameters", goto error);
11279         set = isl_set_align_params(set, isl_multi_aff_get_space(ma));
11280         ma = isl_multi_aff_align_params(ma, isl_set_get_space(set));
11281
11282         return set_preimage_multi_aff(set, ma);
11283 error:
11284         isl_multi_aff_free(ma);
11285         return isl_set_free(set);
11286 }
11287
11288 /* Compute the preimage of "set" under the function represented by "pma".
11289  * In other words, plug in "pma" in "set.  The result is a set
11290  * that lives in the domain space of "pma".
11291  */
11292 static __isl_give isl_set *set_preimage_pw_multi_aff(__isl_take isl_set *set,
11293         __isl_take isl_pw_multi_aff *pma)
11294 {
11295         int i;
11296         isl_set *res;
11297
11298         if (!pma)
11299                 goto error;
11300
11301         if (pma->n == 0) {
11302                 isl_pw_multi_aff_free(pma);
11303                 res = isl_set_empty(isl_set_get_space(set));
11304                 isl_set_free(set);
11305                 return res;
11306         }
11307
11308         res = isl_set_preimage_multi_aff(isl_set_copy(set),
11309                                         isl_multi_aff_copy(pma->p[0].maff));
11310         res = isl_set_intersect(res, isl_set_copy(pma->p[0].set));
11311
11312         for (i = 1; i < pma->n; ++i) {
11313                 isl_set *res_i;
11314
11315                 res_i = isl_set_preimage_multi_aff(isl_set_copy(set),
11316                                         isl_multi_aff_copy(pma->p[i].maff));
11317                 res_i = isl_set_intersect(res_i, isl_set_copy(pma->p[i].set));
11318                 res = isl_set_union(res, res_i);
11319         }
11320
11321         isl_pw_multi_aff_free(pma);
11322         isl_set_free(set);
11323         return res;
11324 error:
11325         isl_pw_multi_aff_free(pma);
11326         isl_set_free(set);
11327         return NULL;
11328 }
11329
11330 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
11331         __isl_take isl_pw_multi_aff *pma)
11332 {
11333         if (!set || !pma)
11334                 goto error;
11335
11336         if (isl_space_match(set->dim, isl_dim_param, pma->dim, isl_dim_param))
11337                 return set_preimage_pw_multi_aff(set, pma);
11338
11339         if (!isl_space_has_named_params(set->dim) ||
11340             !isl_space_has_named_params(pma->dim))
11341                 isl_die(set->ctx, isl_error_invalid,
11342                         "unaligned unnamed parameters", goto error);
11343         set = isl_set_align_params(set, isl_pw_multi_aff_get_space(pma));
11344         pma = isl_pw_multi_aff_align_params(pma, isl_set_get_space(set));
11345
11346         return set_preimage_pw_multi_aff(set, pma);
11347 error:
11348         isl_pw_multi_aff_free(pma);
11349         return isl_set_free(set);
11350 }