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