add isl_map_plain_get_val_if_fixed
[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         total = 1 + isl_basic_map_total_dim(bmap);
2732         if (total != vec->size)
2733                 return -1;
2734
2735         isl_int_init(s);
2736
2737         for (i = 0; i < bmap->n_eq; ++i) {
2738                 isl_seq_inner_product(vec->el, bmap->eq[i], total, &s);
2739                 if (!isl_int_is_zero(s)) {
2740                         isl_int_clear(s);
2741                         return 0;
2742                 }
2743         }
2744
2745         for (i = 0; i < bmap->n_ineq; ++i) {
2746                 isl_seq_inner_product(vec->el, bmap->ineq[i], total, &s);
2747                 if (isl_int_is_neg(s)) {
2748                         isl_int_clear(s);
2749                         return 0;
2750                 }
2751         }
2752
2753         isl_int_clear(s);
2754
2755         return 1;
2756 }
2757
2758 int isl_basic_set_contains(struct isl_basic_set *bset, struct isl_vec *vec)
2759 {
2760         return isl_basic_map_contains((struct isl_basic_map *)bset, vec);
2761 }
2762
2763 struct isl_basic_map *isl_basic_map_intersect(
2764                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
2765 {
2766         struct isl_vec *sample = NULL;
2767
2768         if (!bmap1 || !bmap2)
2769                 goto error;
2770
2771         isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
2772                                      bmap2->dim, isl_dim_param), goto error);
2773         if (isl_space_dim(bmap1->dim, isl_dim_all) ==
2774                                 isl_space_dim(bmap1->dim, isl_dim_param) &&
2775             isl_space_dim(bmap2->dim, isl_dim_all) !=
2776                                 isl_space_dim(bmap2->dim, isl_dim_param))
2777                 return isl_basic_map_intersect(bmap2, bmap1);
2778
2779         if (isl_space_dim(bmap2->dim, isl_dim_all) !=
2780                                         isl_space_dim(bmap2->dim, isl_dim_param))
2781                 isl_assert(bmap1->ctx,
2782                             isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
2783
2784         if (bmap1->sample &&
2785             isl_basic_map_contains(bmap1, bmap1->sample) > 0 &&
2786             isl_basic_map_contains(bmap2, bmap1->sample) > 0)
2787                 sample = isl_vec_copy(bmap1->sample);
2788         else if (bmap2->sample &&
2789             isl_basic_map_contains(bmap1, bmap2->sample) > 0 &&
2790             isl_basic_map_contains(bmap2, bmap2->sample) > 0)
2791                 sample = isl_vec_copy(bmap2->sample);
2792
2793         bmap1 = isl_basic_map_cow(bmap1);
2794         if (!bmap1)
2795                 goto error;
2796         bmap1 = isl_basic_map_extend_space(bmap1, isl_space_copy(bmap1->dim),
2797                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
2798         bmap1 = add_constraints(bmap1, bmap2, 0, 0);
2799
2800         if (!bmap1)
2801                 isl_vec_free(sample);
2802         else if (sample) {
2803                 isl_vec_free(bmap1->sample);
2804                 bmap1->sample = sample;
2805         }
2806
2807         bmap1 = isl_basic_map_simplify(bmap1);
2808         return isl_basic_map_finalize(bmap1);
2809 error:
2810         if (sample)
2811                 isl_vec_free(sample);
2812         isl_basic_map_free(bmap1);
2813         isl_basic_map_free(bmap2);
2814         return NULL;
2815 }
2816
2817 struct isl_basic_set *isl_basic_set_intersect(
2818                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
2819 {
2820         return (struct isl_basic_set *)
2821                 isl_basic_map_intersect(
2822                         (struct isl_basic_map *)bset1,
2823                         (struct isl_basic_map *)bset2);
2824 }
2825
2826 __isl_give isl_basic_set *isl_basic_set_intersect_params(
2827         __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
2828 {
2829         return isl_basic_set_intersect(bset1, bset2);
2830 }
2831
2832 /* Special case of isl_map_intersect, where both map1 and map2
2833  * are convex, without any divs and such that either map1 or map2
2834  * contains a single constraint.  This constraint is then simply
2835  * added to the other map.
2836  */
2837 static __isl_give isl_map *map_intersect_add_constraint(
2838         __isl_take isl_map *map1, __isl_take isl_map *map2)
2839 {
2840         isl_assert(map1->ctx, map1->n == 1, goto error);
2841         isl_assert(map2->ctx, map1->n == 1, goto error);
2842         isl_assert(map1->ctx, map1->p[0]->n_div == 0, goto error);
2843         isl_assert(map2->ctx, map1->p[0]->n_div == 0, goto error);
2844
2845         if (map2->p[0]->n_eq + map2->p[0]->n_ineq != 1)
2846                 return isl_map_intersect(map2, map1);
2847
2848         isl_assert(map2->ctx,
2849                     map2->p[0]->n_eq + map2->p[0]->n_ineq == 1, goto error);
2850
2851         map1 = isl_map_cow(map1);
2852         if (!map1)
2853                 goto error;
2854         if (isl_map_plain_is_empty(map1)) {
2855                 isl_map_free(map2);
2856                 return map1;
2857         }
2858         map1->p[0] = isl_basic_map_cow(map1->p[0]);
2859         if (map2->p[0]->n_eq == 1)
2860                 map1->p[0] = isl_basic_map_add_eq(map1->p[0], map2->p[0]->eq[0]);
2861         else
2862                 map1->p[0] = isl_basic_map_add_ineq(map1->p[0],
2863                                                         map2->p[0]->ineq[0]);
2864
2865         map1->p[0] = isl_basic_map_simplify(map1->p[0]);
2866         map1->p[0] = isl_basic_map_finalize(map1->p[0]);
2867         if (!map1->p[0])
2868                 goto error;
2869
2870         if (isl_basic_map_plain_is_empty(map1->p[0])) {
2871                 isl_basic_map_free(map1->p[0]);
2872                 map1->n = 0;
2873         }
2874
2875         isl_map_free(map2);
2876
2877         return map1;
2878 error:
2879         isl_map_free(map1);
2880         isl_map_free(map2);
2881         return NULL;
2882 }
2883
2884 /* map2 may be either a parameter domain or a map living in the same
2885  * space as map1.
2886  */
2887 static __isl_give isl_map *map_intersect_internal(__isl_take isl_map *map1,
2888         __isl_take isl_map *map2)
2889 {
2890         unsigned flags = 0;
2891         isl_map *result;
2892         int i, j;
2893
2894         if (!map1 || !map2)
2895                 goto error;
2896
2897         if ((isl_map_plain_is_empty(map1) ||
2898              isl_map_plain_is_universe(map2)) &&
2899             isl_space_is_equal(map1->dim, map2->dim)) {
2900                 isl_map_free(map2);
2901                 return map1;
2902         }
2903         if ((isl_map_plain_is_empty(map2) ||
2904              isl_map_plain_is_universe(map1)) &&
2905             isl_space_is_equal(map1->dim, map2->dim)) {
2906                 isl_map_free(map1);
2907                 return map2;
2908         }
2909
2910         if (map1->n == 1 && map2->n == 1 &&
2911             map1->p[0]->n_div == 0 && map2->p[0]->n_div == 0 &&
2912             isl_space_is_equal(map1->dim, map2->dim) &&
2913             (map1->p[0]->n_eq + map1->p[0]->n_ineq == 1 ||
2914              map2->p[0]->n_eq + map2->p[0]->n_ineq == 1))
2915                 return map_intersect_add_constraint(map1, map2);
2916
2917         if (isl_space_dim(map2->dim, isl_dim_all) !=
2918                                 isl_space_dim(map2->dim, isl_dim_param))
2919                 isl_assert(map1->ctx,
2920                             isl_space_is_equal(map1->dim, map2->dim), goto error);
2921
2922         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
2923             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
2924                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
2925
2926         result = isl_map_alloc_space(isl_space_copy(map1->dim),
2927                                 map1->n * map2->n, flags);
2928         if (!result)
2929                 goto error;
2930         for (i = 0; i < map1->n; ++i)
2931                 for (j = 0; j < map2->n; ++j) {
2932                         struct isl_basic_map *part;
2933                         part = isl_basic_map_intersect(
2934                                     isl_basic_map_copy(map1->p[i]),
2935                                     isl_basic_map_copy(map2->p[j]));
2936                         if (isl_basic_map_is_empty(part) < 0)
2937                                 part = isl_basic_map_free(part);
2938                         result = isl_map_add_basic_map(result, part);
2939                         if (!result)
2940                                 goto error;
2941                 }
2942         isl_map_free(map1);
2943         isl_map_free(map2);
2944         return result;
2945 error:
2946         isl_map_free(map1);
2947         isl_map_free(map2);
2948         return NULL;
2949 }
2950
2951 static __isl_give isl_map *map_intersect(__isl_take isl_map *map1,
2952         __isl_take isl_map *map2)
2953 {
2954         if (!map1 || !map2)
2955                 goto error;
2956         if (!isl_space_is_equal(map1->dim, map2->dim))
2957                 isl_die(isl_map_get_ctx(map1), isl_error_invalid,
2958                         "spaces don't match", goto error);
2959         return map_intersect_internal(map1, map2);
2960 error:
2961         isl_map_free(map1);
2962         isl_map_free(map2);
2963         return NULL;
2964 }
2965
2966 __isl_give isl_map *isl_map_intersect(__isl_take isl_map *map1,
2967         __isl_take isl_map *map2)
2968 {
2969         return isl_map_align_params_map_map_and(map1, map2, &map_intersect);
2970 }
2971
2972 struct isl_set *isl_set_intersect(struct isl_set *set1, struct isl_set *set2)
2973 {
2974         return (struct isl_set *)
2975                 isl_map_intersect((struct isl_map *)set1,
2976                                   (struct isl_map *)set2);
2977 }
2978
2979 /* map_intersect_internal accepts intersections
2980  * with parameter domains, so we can just call that function.
2981  */
2982 static __isl_give isl_map *map_intersect_params(__isl_take isl_map *map,
2983                 __isl_take isl_set *params)
2984 {
2985         return map_intersect_internal(map, params);
2986 }
2987
2988 __isl_give isl_map *isl_map_intersect_params(__isl_take isl_map *map1,
2989         __isl_take isl_map *map2)
2990 {
2991         return isl_map_align_params_map_map_and(map1, map2, &map_intersect_params);
2992 }
2993
2994 __isl_give isl_set *isl_set_intersect_params(__isl_take isl_set *set,
2995                 __isl_take isl_set *params)
2996 {
2997         return isl_map_intersect_params(set, params);
2998 }
2999
3000 struct isl_basic_map *isl_basic_map_reverse(struct isl_basic_map *bmap)
3001 {
3002         isl_space *dim;
3003         struct isl_basic_set *bset;
3004         unsigned in;
3005
3006         if (!bmap)
3007                 return NULL;
3008         bmap = isl_basic_map_cow(bmap);
3009         if (!bmap)
3010                 return NULL;
3011         dim = isl_space_reverse(isl_space_copy(bmap->dim));
3012         in = isl_basic_map_n_in(bmap);
3013         bset = isl_basic_set_from_basic_map(bmap);
3014         bset = isl_basic_set_swap_vars(bset, in);
3015         return isl_basic_map_from_basic_set(bset, dim);
3016 }
3017
3018 static __isl_give isl_basic_map *basic_map_space_reset(
3019         __isl_take isl_basic_map *bmap, enum isl_dim_type type)
3020 {
3021         isl_space *space;
3022
3023         if (!bmap)
3024                 return NULL;
3025         if (!isl_space_is_named_or_nested(bmap->dim, type))
3026                 return bmap;
3027
3028         space = isl_basic_map_get_space(bmap);
3029         space = isl_space_reset(space, type);
3030         bmap = isl_basic_map_reset_space(bmap, space);
3031         return bmap;
3032 }
3033
3034 __isl_give isl_basic_map *isl_basic_map_insert_dims(
3035         __isl_take isl_basic_map *bmap, enum isl_dim_type type,
3036         unsigned pos, unsigned n)
3037 {
3038         isl_space *res_dim;
3039         struct isl_basic_map *res;
3040         struct isl_dim_map *dim_map;
3041         unsigned total, off;
3042         enum isl_dim_type t;
3043
3044         if (n == 0)
3045                 return basic_map_space_reset(bmap, type);
3046
3047         if (!bmap)
3048                 return NULL;
3049
3050         res_dim = isl_space_insert_dims(isl_basic_map_get_space(bmap), type, pos, n);
3051
3052         total = isl_basic_map_total_dim(bmap) + n;
3053         dim_map = isl_dim_map_alloc(bmap->ctx, total);
3054         off = 0;
3055         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3056                 if (t != type) {
3057                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
3058                 } else {
3059                         unsigned size = isl_basic_map_dim(bmap, t);
3060                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3061                                                 0, pos, off);
3062                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3063                                                 pos, size - pos, off + pos + n);
3064                 }
3065                 off += isl_space_dim(res_dim, t);
3066         }
3067         isl_dim_map_div(dim_map, bmap, off);
3068
3069         res = isl_basic_map_alloc_space(res_dim,
3070                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
3071         if (isl_basic_map_is_rational(bmap))
3072                 res = isl_basic_map_set_rational(res);
3073         if (isl_basic_map_plain_is_empty(bmap)) {
3074                 isl_basic_map_free(bmap);
3075                 free(dim_map);
3076                 return isl_basic_map_set_to_empty(res);
3077         }
3078         res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3079         return isl_basic_map_finalize(res);
3080 }
3081
3082 __isl_give isl_basic_set *isl_basic_set_insert_dims(
3083         __isl_take isl_basic_set *bset,
3084         enum isl_dim_type type, unsigned pos, unsigned n)
3085 {
3086         return isl_basic_map_insert_dims(bset, type, pos, n);
3087 }
3088
3089 __isl_give isl_basic_map *isl_basic_map_add(__isl_take isl_basic_map *bmap,
3090                 enum isl_dim_type type, unsigned n)
3091 {
3092         if (!bmap)
3093                 return NULL;
3094         return isl_basic_map_insert_dims(bmap, type,
3095                                         isl_basic_map_dim(bmap, type), n);
3096 }
3097
3098 __isl_give isl_basic_set *isl_basic_set_add_dims(__isl_take isl_basic_set *bset,
3099                 enum isl_dim_type type, unsigned n)
3100 {
3101         if (!bset)
3102                 return NULL;
3103         isl_assert(bset->ctx, type != isl_dim_in, goto error);
3104         return (isl_basic_set *)isl_basic_map_add((isl_basic_map *)bset, type, n);
3105 error:
3106         isl_basic_set_free(bset);
3107         return NULL;
3108 }
3109
3110 static __isl_give isl_map *map_space_reset(__isl_take isl_map *map,
3111         enum isl_dim_type type)
3112 {
3113         isl_space *space;
3114
3115         if (!map || !isl_space_is_named_or_nested(map->dim, type))
3116                 return map;
3117
3118         space = isl_map_get_space(map);
3119         space = isl_space_reset(space, type);
3120         map = isl_map_reset_space(map, space);
3121         return map;
3122 }
3123
3124 __isl_give isl_map *isl_map_insert_dims(__isl_take isl_map *map,
3125                 enum isl_dim_type type, unsigned pos, unsigned n)
3126 {
3127         int i;
3128
3129         if (n == 0)
3130                 return map_space_reset(map, type);
3131
3132         map = isl_map_cow(map);
3133         if (!map)
3134                 return NULL;
3135
3136         map->dim = isl_space_insert_dims(map->dim, type, pos, n);
3137         if (!map->dim)
3138                 goto error;
3139
3140         for (i = 0; i < map->n; ++i) {
3141                 map->p[i] = isl_basic_map_insert_dims(map->p[i], type, pos, n);
3142                 if (!map->p[i])
3143                         goto error;
3144         }
3145
3146         return map;
3147 error:
3148         isl_map_free(map);
3149         return NULL;
3150 }
3151
3152 __isl_give isl_set *isl_set_insert_dims(__isl_take isl_set *set,
3153                 enum isl_dim_type type, unsigned pos, unsigned n)
3154 {
3155         return isl_map_insert_dims(set, type, pos, n);
3156 }
3157
3158 __isl_give isl_map *isl_map_add_dims(__isl_take isl_map *map,
3159                 enum isl_dim_type type, unsigned n)
3160 {
3161         if (!map)
3162                 return NULL;
3163         return isl_map_insert_dims(map, type, isl_map_dim(map, type), n);
3164 }
3165
3166 __isl_give isl_set *isl_set_add_dims(__isl_take isl_set *set,
3167                 enum isl_dim_type type, unsigned n)
3168 {
3169         if (!set)
3170                 return NULL;
3171         isl_assert(set->ctx, type != isl_dim_in, goto error);
3172         return (isl_set *)isl_map_add_dims((isl_map *)set, type, n);
3173 error:
3174         isl_set_free(set);
3175         return NULL;
3176 }
3177
3178 __isl_give isl_basic_map *isl_basic_map_move_dims(
3179         __isl_take isl_basic_map *bmap,
3180         enum isl_dim_type dst_type, unsigned dst_pos,
3181         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3182 {
3183         struct isl_dim_map *dim_map;
3184         struct isl_basic_map *res;
3185         enum isl_dim_type t;
3186         unsigned total, off;
3187
3188         if (!bmap)
3189                 return NULL;
3190         if (n == 0)
3191                 return bmap;
3192
3193         isl_assert(bmap->ctx, src_pos + n <= isl_basic_map_dim(bmap, src_type),
3194                 goto error);
3195
3196         if (dst_type == src_type && dst_pos == src_pos)
3197                 return bmap;
3198
3199         isl_assert(bmap->ctx, dst_type != src_type, goto error);
3200
3201         if (pos(bmap->dim, dst_type) + dst_pos ==
3202             pos(bmap->dim, src_type) + src_pos +
3203                                             ((src_type < dst_type) ? n : 0)) {
3204                 bmap = isl_basic_map_cow(bmap);
3205                 if (!bmap)
3206                         return NULL;
3207
3208                 bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3209                                                 src_type, src_pos, n);
3210                 if (!bmap->dim)
3211                         goto error;
3212
3213                 bmap = isl_basic_map_finalize(bmap);
3214
3215                 return bmap;
3216         }
3217
3218         total = isl_basic_map_total_dim(bmap);
3219         dim_map = isl_dim_map_alloc(bmap->ctx, total);
3220
3221         off = 0;
3222         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3223                 unsigned size = isl_space_dim(bmap->dim, t);
3224                 if (t == dst_type) {
3225                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3226                                             0, dst_pos, off);
3227                         off += dst_pos;
3228                         isl_dim_map_dim_range(dim_map, bmap->dim, src_type,
3229                                             src_pos, n, off);
3230                         off += n;
3231                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3232                                             dst_pos, size - dst_pos, off);
3233                         off += size - dst_pos;
3234                 } else if (t == src_type) {
3235                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3236                                             0, src_pos, off);
3237                         off += src_pos;
3238                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3239                                         src_pos + n, size - src_pos - n, off);
3240                         off += size - src_pos - n;
3241                 } else {
3242                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
3243                         off += size;
3244                 }
3245         }
3246         isl_dim_map_div(dim_map, bmap, off);
3247
3248         res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3249                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
3250         bmap = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3251         if (!bmap)
3252                 goto error;
3253
3254         bmap->dim = isl_space_move_dims(bmap->dim, dst_type, dst_pos,
3255                                         src_type, src_pos, n);
3256         if (!bmap->dim)
3257                 goto error;
3258
3259         ISL_F_CLR(bmap, ISL_BASIC_MAP_NORMALIZED);
3260         bmap = isl_basic_map_gauss(bmap, NULL);
3261         bmap = isl_basic_map_finalize(bmap);
3262
3263         return bmap;
3264 error:
3265         isl_basic_map_free(bmap);
3266         return NULL;
3267 }
3268
3269 __isl_give isl_basic_set *isl_basic_set_move_dims(__isl_take isl_basic_set *bset,
3270         enum isl_dim_type dst_type, unsigned dst_pos,
3271         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3272 {
3273         return (isl_basic_set *)isl_basic_map_move_dims(
3274                 (isl_basic_map *)bset, dst_type, dst_pos, src_type, src_pos, n);
3275 }
3276
3277 __isl_give isl_set *isl_set_move_dims(__isl_take isl_set *set,
3278         enum isl_dim_type dst_type, unsigned dst_pos,
3279         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3280 {
3281         if (!set)
3282                 return NULL;
3283         isl_assert(set->ctx, dst_type != isl_dim_in, goto error);
3284         return (isl_set *)isl_map_move_dims((isl_map *)set, dst_type, dst_pos,
3285                                         src_type, src_pos, n);
3286 error:
3287         isl_set_free(set);
3288         return NULL;
3289 }
3290
3291 __isl_give isl_map *isl_map_move_dims(__isl_take isl_map *map,
3292         enum isl_dim_type dst_type, unsigned dst_pos,
3293         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
3294 {
3295         int i;
3296
3297         if (!map)
3298                 return NULL;
3299         if (n == 0)
3300                 return map;
3301
3302         isl_assert(map->ctx, src_pos + n <= isl_map_dim(map, src_type),
3303                 goto error);
3304
3305         if (dst_type == src_type && dst_pos == src_pos)
3306                 return map;
3307
3308         isl_assert(map->ctx, dst_type != src_type, goto error);
3309
3310         map = isl_map_cow(map);
3311         if (!map)
3312                 return NULL;
3313
3314         map->dim = isl_space_move_dims(map->dim, dst_type, dst_pos, src_type, src_pos, n);
3315         if (!map->dim)
3316                 goto error;
3317
3318         for (i = 0; i < map->n; ++i) {
3319                 map->p[i] = isl_basic_map_move_dims(map->p[i],
3320                                                 dst_type, dst_pos,
3321                                                 src_type, src_pos, n);
3322                 if (!map->p[i])
3323                         goto error;
3324         }
3325
3326         return map;
3327 error:
3328         isl_map_free(map);
3329         return NULL;
3330 }
3331
3332 /* Move the specified dimensions to the last columns right before
3333  * the divs.  Don't change the dimension specification of bmap.
3334  * That's the responsibility of the caller.
3335  */
3336 static __isl_give isl_basic_map *move_last(__isl_take isl_basic_map *bmap,
3337         enum isl_dim_type type, unsigned first, unsigned n)
3338 {
3339         struct isl_dim_map *dim_map;
3340         struct isl_basic_map *res;
3341         enum isl_dim_type t;
3342         unsigned total, off;
3343
3344         if (!bmap)
3345                 return NULL;
3346         if (pos(bmap->dim, type) + first + n ==
3347                                 1 + isl_space_dim(bmap->dim, isl_dim_all))
3348                 return bmap;
3349
3350         total = isl_basic_map_total_dim(bmap);
3351         dim_map = isl_dim_map_alloc(bmap->ctx, total);
3352
3353         off = 0;
3354         for (t = isl_dim_param; t <= isl_dim_out; ++t) {
3355                 unsigned size = isl_space_dim(bmap->dim, t);
3356                 if (t == type) {
3357                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3358                                             0, first, off);
3359                         off += first;
3360                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3361                                             first, n, total - bmap->n_div - n);
3362                         isl_dim_map_dim_range(dim_map, bmap->dim, t,
3363                                             first + n, size - (first + n), off);
3364                         off += size - (first + n);
3365                 } else {
3366                         isl_dim_map_dim(dim_map, bmap->dim, t, off);
3367                         off += size;
3368                 }
3369         }
3370         isl_dim_map_div(dim_map, bmap, off + n);
3371
3372         res = isl_basic_map_alloc_space(isl_basic_map_get_space(bmap),
3373                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
3374         res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
3375         return res;
3376 }
3377
3378 /* Insert "n" rows in the divs of "bmap".
3379  *
3380  * The number of columns is not changed, which means that the last
3381  * dimensions of "bmap" are being reintepreted as the new divs.
3382  * The space of "bmap" is not adjusted, however, which means
3383  * that "bmap" is left in an inconsistent state.  Removing "n" dimensions
3384  * from the space of "bmap" is the responsibility of the caller.
3385  */
3386 static __isl_give isl_basic_map *insert_div_rows(__isl_take isl_basic_map *bmap,
3387         int n)
3388 {
3389         int i;
3390         size_t row_size;
3391         isl_int **new_div;
3392         isl_int *old;
3393
3394         bmap = isl_basic_map_cow(bmap);
3395         if (!bmap)
3396                 return NULL;
3397
3398         row_size = 1 + isl_space_dim(bmap->dim, isl_dim_all) + bmap->extra;
3399         old = bmap->block2.data;
3400         bmap->block2 = isl_blk_extend(bmap->ctx, bmap->block2,
3401                                         (bmap->extra + n) * (1 + row_size));
3402         if (!bmap->block2.data)
3403                 return isl_basic_map_free(bmap);
3404         new_div = isl_alloc_array(bmap->ctx, isl_int *, bmap->extra + n);
3405         if (!new_div)
3406                 return isl_basic_map_free(bmap);
3407         for (i = 0; i < n; ++i) {
3408                 new_div[i] = bmap->block2.data +
3409                                 (bmap->extra + i) * (1 + row_size);
3410                 isl_seq_clr(new_div[i], 1 + row_size);
3411         }
3412         for (i = 0; i < bmap->extra; ++i)
3413                 new_div[n + i] = bmap->block2.data + (bmap->div[i] - old);
3414         free(bmap->div);
3415         bmap->div = new_div;
3416         bmap->n_div += n;
3417         bmap->extra += n;
3418
3419         return bmap;
3420 }
3421
3422 /* Turn the n dimensions of type type, starting at first
3423  * into existentially quantified variables.
3424  */
3425 __isl_give isl_basic_map *isl_basic_map_project_out(
3426                 __isl_take isl_basic_map *bmap,
3427                 enum isl_dim_type type, unsigned first, unsigned n)
3428 {
3429         if (n == 0)
3430                 return basic_map_space_reset(bmap, type);
3431
3432         if (!bmap)
3433                 return NULL;
3434
3435         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL))
3436                 return isl_basic_map_remove_dims(bmap, type, first, n);
3437
3438         isl_assert(bmap->ctx, first + n <= isl_basic_map_dim(bmap, type),
3439                         goto error);
3440
3441         bmap = move_last(bmap, type, first, n);
3442         bmap = isl_basic_map_cow(bmap);
3443         bmap = insert_div_rows(bmap, n);
3444         if (!bmap)
3445                 return NULL;
3446
3447         bmap->dim = isl_space_drop_dims(bmap->dim, type, first, n);
3448         if (!bmap->dim)
3449                 goto error;
3450         bmap = isl_basic_map_simplify(bmap);
3451         bmap = isl_basic_map_drop_redundant_divs(bmap);
3452         return isl_basic_map_finalize(bmap);
3453 error:
3454         isl_basic_map_free(bmap);
3455         return NULL;
3456 }
3457
3458 /* Turn the n dimensions of type type, starting at first
3459  * into existentially quantified variables.
3460  */
3461 struct isl_basic_set *isl_basic_set_project_out(struct isl_basic_set *bset,
3462                 enum isl_dim_type type, unsigned first, unsigned n)
3463 {
3464         return (isl_basic_set *)isl_basic_map_project_out(
3465                         (isl_basic_map *)bset, type, first, n);
3466 }
3467
3468 /* Turn the n dimensions of type type, starting at first
3469  * into existentially quantified variables.
3470  */
3471 __isl_give isl_map *isl_map_project_out(__isl_take isl_map *map,
3472                 enum isl_dim_type type, unsigned first, unsigned n)
3473 {
3474         int i;
3475
3476         if (!map)
3477                 return NULL;
3478
3479         if (n == 0)
3480                 return map_space_reset(map, type);
3481
3482         isl_assert(map->ctx, first + n <= isl_map_dim(map, type), goto error);
3483
3484         map = isl_map_cow(map);
3485         if (!map)
3486                 return NULL;
3487
3488         map->dim = isl_space_drop_dims(map->dim, type, first, n);
3489         if (!map->dim)
3490                 goto error;
3491
3492         for (i = 0; i < map->n; ++i) {
3493                 map->p[i] = isl_basic_map_project_out(map->p[i], type, first, n);
3494                 if (!map->p[i])
3495                         goto error;
3496         }
3497
3498         return map;
3499 error:
3500         isl_map_free(map);
3501         return NULL;
3502 }
3503
3504 /* Turn the n dimensions of type type, starting at first
3505  * into existentially quantified variables.
3506  */
3507 __isl_give isl_set *isl_set_project_out(__isl_take isl_set *set,
3508                 enum isl_dim_type type, unsigned first, unsigned n)
3509 {
3510         return (isl_set *)isl_map_project_out((isl_map *)set, type, first, n);
3511 }
3512
3513 static struct isl_basic_map *add_divs(struct isl_basic_map *bmap, unsigned n)
3514 {
3515         int i, j;
3516
3517         for (i = 0; i < n; ++i) {
3518                 j = isl_basic_map_alloc_div(bmap);
3519                 if (j < 0)
3520                         goto error;
3521                 isl_seq_clr(bmap->div[j], 1+1+isl_basic_map_total_dim(bmap));
3522         }
3523         return bmap;
3524 error:
3525         isl_basic_map_free(bmap);
3526         return NULL;
3527 }
3528
3529 struct isl_basic_map *isl_basic_map_apply_range(
3530                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3531 {
3532         isl_space *dim_result = NULL;
3533         struct isl_basic_map *bmap;
3534         unsigned n_in, n_out, n, nparam, total, pos;
3535         struct isl_dim_map *dim_map1, *dim_map2;
3536
3537         if (!bmap1 || !bmap2)
3538                 goto error;
3539
3540         dim_result = isl_space_join(isl_space_copy(bmap1->dim),
3541                                   isl_space_copy(bmap2->dim));
3542
3543         n_in = isl_basic_map_n_in(bmap1);
3544         n_out = isl_basic_map_n_out(bmap2);
3545         n = isl_basic_map_n_out(bmap1);
3546         nparam = isl_basic_map_n_param(bmap1);
3547
3548         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + n;
3549         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3550         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
3551         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3552         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
3553         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3554         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_in);
3555         isl_dim_map_div(dim_map1, bmap1, pos += n_out);
3556         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3557         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3558         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3559
3560         bmap = isl_basic_map_alloc_space(dim_result,
3561                         bmap1->n_div + bmap2->n_div + n,
3562                         bmap1->n_eq + bmap2->n_eq,
3563                         bmap1->n_ineq + bmap2->n_ineq);
3564         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3565         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3566         bmap = add_divs(bmap, n);
3567         bmap = isl_basic_map_simplify(bmap);
3568         bmap = isl_basic_map_drop_redundant_divs(bmap);
3569         return isl_basic_map_finalize(bmap);
3570 error:
3571         isl_basic_map_free(bmap1);
3572         isl_basic_map_free(bmap2);
3573         return NULL;
3574 }
3575
3576 struct isl_basic_set *isl_basic_set_apply(
3577                 struct isl_basic_set *bset, struct isl_basic_map *bmap)
3578 {
3579         if (!bset || !bmap)
3580                 goto error;
3581
3582         isl_assert(bset->ctx, isl_basic_map_compatible_domain(bmap, bset),
3583                     goto error);
3584
3585         return (struct isl_basic_set *)
3586                 isl_basic_map_apply_range((struct isl_basic_map *)bset, bmap);
3587 error:
3588         isl_basic_set_free(bset);
3589         isl_basic_map_free(bmap);
3590         return NULL;
3591 }
3592
3593 struct isl_basic_map *isl_basic_map_apply_domain(
3594                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3595 {
3596         if (!bmap1 || !bmap2)
3597                 goto error;
3598
3599         isl_assert(bmap1->ctx,
3600             isl_basic_map_n_in(bmap1) == isl_basic_map_n_in(bmap2), goto error);
3601         isl_assert(bmap1->ctx,
3602             isl_basic_map_n_param(bmap1) == isl_basic_map_n_param(bmap2),
3603             goto error);
3604
3605         bmap1 = isl_basic_map_reverse(bmap1);
3606         bmap1 = isl_basic_map_apply_range(bmap1, bmap2);
3607         return isl_basic_map_reverse(bmap1);
3608 error:
3609         isl_basic_map_free(bmap1);
3610         isl_basic_map_free(bmap2);
3611         return NULL;
3612 }
3613
3614 /* Given two basic maps A -> f(A) and B -> g(B), construct a basic map
3615  * A \cap B -> f(A) + f(B)
3616  */
3617 struct isl_basic_map *isl_basic_map_sum(
3618                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
3619 {
3620         unsigned n_in, n_out, nparam, total, pos;
3621         struct isl_basic_map *bmap = NULL;
3622         struct isl_dim_map *dim_map1, *dim_map2;
3623         int i;
3624
3625         if (!bmap1 || !bmap2)
3626                 goto error;
3627
3628         isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim),
3629                 goto error);
3630
3631         nparam = isl_basic_map_n_param(bmap1);
3632         n_in = isl_basic_map_n_in(bmap1);
3633         n_out = isl_basic_map_n_out(bmap1);
3634
3635         total = nparam + n_in + n_out + bmap1->n_div + bmap2->n_div + 2 * n_out;
3636         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
3637         dim_map2 = isl_dim_map_alloc(bmap2->ctx, total);
3638         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
3639         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos);
3640         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
3641         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
3642         isl_dim_map_div(dim_map1, bmap1, pos += n_in + n_out);
3643         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
3644         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += bmap2->n_div);
3645         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += n_out);
3646
3647         bmap = isl_basic_map_alloc_space(isl_space_copy(bmap1->dim),
3648                         bmap1->n_div + bmap2->n_div + 2 * n_out,
3649                         bmap1->n_eq + bmap2->n_eq + n_out,
3650                         bmap1->n_ineq + bmap2->n_ineq);
3651         for (i = 0; i < n_out; ++i) {
3652                 int j = isl_basic_map_alloc_equality(bmap);
3653                 if (j < 0)
3654                         goto error;
3655                 isl_seq_clr(bmap->eq[j], 1+total);
3656                 isl_int_set_si(bmap->eq[j][1+nparam+n_in+i], -1);
3657                 isl_int_set_si(bmap->eq[j][1+pos+i], 1);
3658                 isl_int_set_si(bmap->eq[j][1+pos-n_out+i], 1);
3659         }
3660         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
3661         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
3662         bmap = add_divs(bmap, 2 * n_out);
3663
3664         bmap = isl_basic_map_simplify(bmap);
3665         return isl_basic_map_finalize(bmap);
3666 error:
3667         isl_basic_map_free(bmap);
3668         isl_basic_map_free(bmap1);
3669         isl_basic_map_free(bmap2);
3670         return NULL;
3671 }
3672
3673 /* Given two maps A -> f(A) and B -> g(B), construct a map
3674  * A \cap B -> f(A) + f(B)
3675  */
3676 struct isl_map *isl_map_sum(struct isl_map *map1, struct isl_map *map2)
3677 {
3678         struct isl_map *result;
3679         int i, j;
3680
3681         if (!map1 || !map2)
3682                 goto error;
3683
3684         isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
3685
3686         result = isl_map_alloc_space(isl_space_copy(map1->dim),
3687                                 map1->n * map2->n, 0);
3688         if (!result)
3689                 goto error;
3690         for (i = 0; i < map1->n; ++i)
3691                 for (j = 0; j < map2->n; ++j) {
3692                         struct isl_basic_map *part;
3693                         part = isl_basic_map_sum(
3694                                     isl_basic_map_copy(map1->p[i]),
3695                                     isl_basic_map_copy(map2->p[j]));
3696                         if (isl_basic_map_is_empty(part))
3697                                 isl_basic_map_free(part);
3698                         else
3699                                 result = isl_map_add_basic_map(result, part);
3700                         if (!result)
3701                                 goto error;
3702                 }
3703         isl_map_free(map1);
3704         isl_map_free(map2);
3705         return result;
3706 error:
3707         isl_map_free(map1);
3708         isl_map_free(map2);
3709         return NULL;
3710 }
3711
3712 __isl_give isl_set *isl_set_sum(__isl_take isl_set *set1,
3713         __isl_take isl_set *set2)
3714 {
3715         return (isl_set *)isl_map_sum((isl_map *)set1, (isl_map *)set2);
3716 }
3717
3718 /* Given a basic map A -> f(A), construct A -> -f(A).
3719  */
3720 struct isl_basic_map *isl_basic_map_neg(struct isl_basic_map *bmap)
3721 {
3722         int i, j;
3723         unsigned off, n;
3724
3725         bmap = isl_basic_map_cow(bmap);
3726         if (!bmap)
3727                 return NULL;
3728
3729         n = isl_basic_map_dim(bmap, isl_dim_out);
3730         off = isl_basic_map_offset(bmap, isl_dim_out);
3731         for (i = 0; i < bmap->n_eq; ++i)
3732                 for (j = 0; j < n; ++j)
3733                         isl_int_neg(bmap->eq[i][off+j], bmap->eq[i][off+j]);
3734         for (i = 0; i < bmap->n_ineq; ++i)
3735                 for (j = 0; j < n; ++j)
3736                         isl_int_neg(bmap->ineq[i][off+j], bmap->ineq[i][off+j]);
3737         for (i = 0; i < bmap->n_div; ++i)
3738                 for (j = 0; j < n; ++j)
3739                         isl_int_neg(bmap->div[i][1+off+j], bmap->div[i][1+off+j]);
3740         bmap = isl_basic_map_gauss(bmap, NULL);
3741         return isl_basic_map_finalize(bmap);
3742 }
3743
3744 __isl_give isl_basic_set *isl_basic_set_neg(__isl_take isl_basic_set *bset)
3745 {
3746         return isl_basic_map_neg(bset);
3747 }
3748
3749 /* Given a map A -> f(A), construct A -> -f(A).
3750  */
3751 struct isl_map *isl_map_neg(struct isl_map *map)
3752 {
3753         int i;
3754
3755         map = isl_map_cow(map);
3756         if (!map)
3757                 return NULL;
3758
3759         for (i = 0; i < map->n; ++i) {
3760                 map->p[i] = isl_basic_map_neg(map->p[i]);
3761                 if (!map->p[i])
3762                         goto error;
3763         }
3764
3765         return map;
3766 error:
3767         isl_map_free(map);
3768         return NULL;
3769 }
3770
3771 __isl_give isl_set *isl_set_neg(__isl_take isl_set *set)
3772 {
3773         return (isl_set *)isl_map_neg((isl_map *)set);
3774 }
3775
3776 /* Given a basic map A -> f(A) and an integer d, construct a basic map
3777  * A -> floor(f(A)/d).
3778  */
3779 struct isl_basic_map *isl_basic_map_floordiv(struct isl_basic_map *bmap,
3780                 isl_int d)
3781 {
3782         unsigned n_in, n_out, nparam, total, pos;
3783         struct isl_basic_map *result = NULL;
3784         struct isl_dim_map *dim_map;
3785         int i;
3786
3787         if (!bmap)
3788                 return NULL;
3789
3790         nparam = isl_basic_map_n_param(bmap);
3791         n_in = isl_basic_map_n_in(bmap);
3792         n_out = isl_basic_map_n_out(bmap);
3793
3794         total = nparam + n_in + n_out + bmap->n_div + n_out;
3795         dim_map = isl_dim_map_alloc(bmap->ctx, total);
3796         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_param, pos = 0);
3797         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_in, pos += nparam);
3798         isl_dim_map_div(dim_map, bmap, pos += n_in + n_out);
3799         isl_dim_map_dim(dim_map, bmap->dim, isl_dim_out, pos += bmap->n_div);
3800
3801         result = isl_basic_map_alloc_space(isl_space_copy(bmap->dim),
3802                         bmap->n_div + n_out,
3803                         bmap->n_eq, bmap->n_ineq + 2 * n_out);
3804         result = isl_basic_map_add_constraints_dim_map(result, bmap, dim_map);
3805         result = add_divs(result, n_out);
3806         for (i = 0; i < n_out; ++i) {
3807                 int j;
3808                 j = isl_basic_map_alloc_inequality(result);
3809                 if (j < 0)
3810                         goto error;
3811                 isl_seq_clr(result->ineq[j], 1+total);
3812                 isl_int_neg(result->ineq[j][1+nparam+n_in+i], d);
3813                 isl_int_set_si(result->ineq[j][1+pos+i], 1);
3814                 j = isl_basic_map_alloc_inequality(result);
3815                 if (j < 0)
3816                         goto error;
3817                 isl_seq_clr(result->ineq[j], 1+total);
3818                 isl_int_set(result->ineq[j][1+nparam+n_in+i], d);
3819                 isl_int_set_si(result->ineq[j][1+pos+i], -1);
3820                 isl_int_sub_ui(result->ineq[j][0], d, 1);
3821         }
3822
3823         result = isl_basic_map_simplify(result);
3824         return isl_basic_map_finalize(result);
3825 error:
3826         isl_basic_map_free(result);
3827         return NULL;
3828 }
3829
3830 /* Given a map A -> f(A) and an integer d, construct a map
3831  * A -> floor(f(A)/d).
3832  */
3833 struct isl_map *isl_map_floordiv(struct isl_map *map, isl_int d)
3834 {
3835         int i;
3836
3837         map = isl_map_cow(map);
3838         if (!map)
3839                 return NULL;
3840
3841         ISL_F_CLR(map, ISL_MAP_DISJOINT);
3842         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
3843         for (i = 0; i < map->n; ++i) {
3844                 map->p[i] = isl_basic_map_floordiv(map->p[i], d);
3845                 if (!map->p[i])
3846                         goto error;
3847         }
3848
3849         return map;
3850 error:
3851         isl_map_free(map);
3852         return NULL;
3853 }
3854
3855 static struct isl_basic_map *var_equal(struct isl_basic_map *bmap, unsigned pos)
3856 {
3857         int i;
3858         unsigned nparam;
3859         unsigned n_in;
3860
3861         i = isl_basic_map_alloc_equality(bmap);
3862         if (i < 0)
3863                 goto error;
3864         nparam = isl_basic_map_n_param(bmap);
3865         n_in = isl_basic_map_n_in(bmap);
3866         isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
3867         isl_int_set_si(bmap->eq[i][1+nparam+pos], -1);
3868         isl_int_set_si(bmap->eq[i][1+nparam+n_in+pos], 1);
3869         return isl_basic_map_finalize(bmap);
3870 error:
3871         isl_basic_map_free(bmap);
3872         return NULL;
3873 }
3874
3875 /* Add a constraints to "bmap" expressing i_pos < o_pos
3876  */
3877 static struct isl_basic_map *var_less(struct isl_basic_map *bmap, unsigned pos)
3878 {
3879         int i;
3880         unsigned nparam;
3881         unsigned n_in;
3882
3883         i = isl_basic_map_alloc_inequality(bmap);
3884         if (i < 0)
3885                 goto error;
3886         nparam = isl_basic_map_n_param(bmap);
3887         n_in = isl_basic_map_n_in(bmap);
3888         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3889         isl_int_set_si(bmap->ineq[i][0], -1);
3890         isl_int_set_si(bmap->ineq[i][1+nparam+pos], -1);
3891         isl_int_set_si(bmap->ineq[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 constraint to "bmap" expressing i_pos <= o_pos
3899  */
3900 static __isl_give isl_basic_map *var_less_or_equal(
3901         __isl_take isl_basic_map *bmap, unsigned pos)
3902 {
3903         int i;
3904         unsigned nparam;
3905         unsigned n_in;
3906
3907         i = isl_basic_map_alloc_inequality(bmap);
3908         if (i < 0)
3909                 goto error;
3910         nparam = isl_basic_map_n_param(bmap);
3911         n_in = isl_basic_map_n_in(bmap);
3912         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
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 constraints to "bmap" expressing i_pos > o_pos
3922  */
3923 static struct isl_basic_map *var_more(struct isl_basic_map *bmap, unsigned pos)
3924 {
3925         int i;
3926         unsigned nparam;
3927         unsigned n_in;
3928
3929         i = isl_basic_map_alloc_inequality(bmap);
3930         if (i < 0)
3931                 goto error;
3932         nparam = isl_basic_map_n_param(bmap);
3933         n_in = isl_basic_map_n_in(bmap);
3934         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
3935         isl_int_set_si(bmap->ineq[i][0], -1);
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 constraint to "bmap" expressing i_pos >= o_pos
3945  */
3946 static __isl_give isl_basic_map *var_more_or_equal(
3947         __isl_take isl_basic_map *bmap, unsigned pos)
3948 {
3949         int i;
3950         unsigned nparam;
3951         unsigned n_in;
3952
3953         i = isl_basic_map_alloc_inequality(bmap);
3954         if (i < 0)
3955                 goto error;
3956         nparam = isl_basic_map_n_param(bmap);
3957         n_in = isl_basic_map_n_in(bmap);
3958         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
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 __isl_give isl_basic_map *isl_basic_map_equal(
3968         __isl_take isl_space *dim, unsigned n_equal)
3969 {
3970         int i;
3971         struct isl_basic_map *bmap;
3972         bmap = isl_basic_map_alloc_space(dim, 0, n_equal, 0);
3973         if (!bmap)
3974                 return NULL;
3975         for (i = 0; i < n_equal && bmap; ++i)
3976                 bmap = var_equal(bmap, i);
3977         return isl_basic_map_finalize(bmap);
3978 }
3979
3980 /* Return a relation on of dimension "dim" expressing i_[0..pos] << o_[0..pos]
3981  */
3982 __isl_give isl_basic_map *isl_basic_map_less_at(__isl_take isl_space *dim,
3983         unsigned pos)
3984 {
3985         int i;
3986         struct isl_basic_map *bmap;
3987         bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
3988         if (!bmap)
3989                 return NULL;
3990         for (i = 0; i < pos && bmap; ++i)
3991                 bmap = var_equal(bmap, i);
3992         if (bmap)
3993                 bmap = var_less(bmap, pos);
3994         return isl_basic_map_finalize(bmap);
3995 }
3996
3997 /* Return a relation on of dimension "dim" expressing i_[0..pos] <<= o_[0..pos]
3998  */
3999 __isl_give isl_basic_map *isl_basic_map_less_or_equal_at(
4000         __isl_take isl_space *dim, unsigned pos)
4001 {
4002         int i;
4003         isl_basic_map *bmap;
4004
4005         bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4006         for (i = 0; i < pos; ++i)
4007                 bmap = var_equal(bmap, i);
4008         bmap = var_less_or_equal(bmap, pos);
4009         return isl_basic_map_finalize(bmap);
4010 }
4011
4012 /* Return a relation on pairs of sets of dimension "dim" expressing i_pos > o_pos
4013  */
4014 __isl_give isl_basic_map *isl_basic_map_more_at(__isl_take isl_space *dim,
4015         unsigned pos)
4016 {
4017         int i;
4018         struct isl_basic_map *bmap;
4019         bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4020         if (!bmap)
4021                 return NULL;
4022         for (i = 0; i < pos && bmap; ++i)
4023                 bmap = var_equal(bmap, i);
4024         if (bmap)
4025                 bmap = var_more(bmap, pos);
4026         return isl_basic_map_finalize(bmap);
4027 }
4028
4029 /* Return a relation on of dimension "dim" expressing i_[0..pos] >>= o_[0..pos]
4030  */
4031 __isl_give isl_basic_map *isl_basic_map_more_or_equal_at(
4032         __isl_take isl_space *dim, unsigned pos)
4033 {
4034         int i;
4035         isl_basic_map *bmap;
4036
4037         bmap = isl_basic_map_alloc_space(dim, 0, pos, 1);
4038         for (i = 0; i < pos; ++i)
4039                 bmap = var_equal(bmap, i);
4040         bmap = var_more_or_equal(bmap, pos);
4041         return isl_basic_map_finalize(bmap);
4042 }
4043
4044 static __isl_give isl_map *map_lex_lte_first(__isl_take isl_space *dims,
4045         unsigned n, int equal)
4046 {
4047         struct isl_map *map;
4048         int i;
4049
4050         if (n == 0 && equal)
4051                 return isl_map_universe(dims);
4052
4053         map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4054
4055         for (i = 0; i + 1 < n; ++i)
4056                 map = isl_map_add_basic_map(map,
4057                                   isl_basic_map_less_at(isl_space_copy(dims), i));
4058         if (n > 0) {
4059                 if (equal)
4060                         map = isl_map_add_basic_map(map,
4061                               isl_basic_map_less_or_equal_at(dims, n - 1));
4062                 else
4063                         map = isl_map_add_basic_map(map,
4064                               isl_basic_map_less_at(dims, n - 1));
4065         } else
4066                 isl_space_free(dims);
4067
4068         return map;
4069 }
4070
4071 static __isl_give isl_map *map_lex_lte(__isl_take isl_space *dims, int equal)
4072 {
4073         if (!dims)
4074                 return NULL;
4075         return map_lex_lte_first(dims, dims->n_out, equal);
4076 }
4077
4078 __isl_give isl_map *isl_map_lex_lt_first(__isl_take isl_space *dim, unsigned n)
4079 {
4080         return map_lex_lte_first(dim, n, 0);
4081 }
4082
4083 __isl_give isl_map *isl_map_lex_le_first(__isl_take isl_space *dim, unsigned n)
4084 {
4085         return map_lex_lte_first(dim, n, 1);
4086 }
4087
4088 __isl_give isl_map *isl_map_lex_lt(__isl_take isl_space *set_dim)
4089 {
4090         return map_lex_lte(isl_space_map_from_set(set_dim), 0);
4091 }
4092
4093 __isl_give isl_map *isl_map_lex_le(__isl_take isl_space *set_dim)
4094 {
4095         return map_lex_lte(isl_space_map_from_set(set_dim), 1);
4096 }
4097
4098 static __isl_give isl_map *map_lex_gte_first(__isl_take isl_space *dims,
4099         unsigned n, int equal)
4100 {
4101         struct isl_map *map;
4102         int i;
4103
4104         if (n == 0 && equal)
4105                 return isl_map_universe(dims);
4106
4107         map = isl_map_alloc_space(isl_space_copy(dims), n, ISL_MAP_DISJOINT);
4108
4109         for (i = 0; i + 1 < n; ++i)
4110                 map = isl_map_add_basic_map(map,
4111                                   isl_basic_map_more_at(isl_space_copy(dims), i));
4112         if (n > 0) {
4113                 if (equal)
4114                         map = isl_map_add_basic_map(map,
4115                               isl_basic_map_more_or_equal_at(dims, n - 1));
4116                 else
4117                         map = isl_map_add_basic_map(map,
4118                               isl_basic_map_more_at(dims, n - 1));
4119         } else
4120                 isl_space_free(dims);
4121
4122         return map;
4123 }
4124
4125 static __isl_give isl_map *map_lex_gte(__isl_take isl_space *dims, int equal)
4126 {
4127         if (!dims)
4128                 return NULL;
4129         return map_lex_gte_first(dims, dims->n_out, equal);
4130 }
4131
4132 __isl_give isl_map *isl_map_lex_gt_first(__isl_take isl_space *dim, unsigned n)
4133 {
4134         return map_lex_gte_first(dim, n, 0);
4135 }
4136
4137 __isl_give isl_map *isl_map_lex_ge_first(__isl_take isl_space *dim, unsigned n)
4138 {
4139         return map_lex_gte_first(dim, n, 1);
4140 }
4141
4142 __isl_give isl_map *isl_map_lex_gt(__isl_take isl_space *set_dim)
4143 {
4144         return map_lex_gte(isl_space_map_from_set(set_dim), 0);
4145 }
4146
4147 __isl_give isl_map *isl_map_lex_ge(__isl_take isl_space *set_dim)
4148 {
4149         return map_lex_gte(isl_space_map_from_set(set_dim), 1);
4150 }
4151
4152 __isl_give isl_map *isl_set_lex_le_set(__isl_take isl_set *set1,
4153         __isl_take isl_set *set2)
4154 {
4155         isl_map *map;
4156         map = isl_map_lex_le(isl_set_get_space(set1));
4157         map = isl_map_intersect_domain(map, set1);
4158         map = isl_map_intersect_range(map, set2);
4159         return map;
4160 }
4161
4162 __isl_give isl_map *isl_set_lex_lt_set(__isl_take isl_set *set1,
4163         __isl_take isl_set *set2)
4164 {
4165         isl_map *map;
4166         map = isl_map_lex_lt(isl_set_get_space(set1));
4167         map = isl_map_intersect_domain(map, set1);
4168         map = isl_map_intersect_range(map, set2);
4169         return map;
4170 }
4171
4172 __isl_give isl_map *isl_set_lex_ge_set(__isl_take isl_set *set1,
4173         __isl_take isl_set *set2)
4174 {
4175         isl_map *map;
4176         map = isl_map_lex_ge(isl_set_get_space(set1));
4177         map = isl_map_intersect_domain(map, set1);
4178         map = isl_map_intersect_range(map, set2);
4179         return map;
4180 }
4181
4182 __isl_give isl_map *isl_set_lex_gt_set(__isl_take isl_set *set1,
4183         __isl_take isl_set *set2)
4184 {
4185         isl_map *map;
4186         map = isl_map_lex_gt(isl_set_get_space(set1));
4187         map = isl_map_intersect_domain(map, set1);
4188         map = isl_map_intersect_range(map, set2);
4189         return map;
4190 }
4191
4192 __isl_give isl_map *isl_map_lex_le_map(__isl_take isl_map *map1,
4193         __isl_take isl_map *map2)
4194 {
4195         isl_map *map;
4196         map = isl_map_lex_le(isl_space_range(isl_map_get_space(map1)));
4197         map = isl_map_apply_domain(map, isl_map_reverse(map1));
4198         map = isl_map_apply_range(map, isl_map_reverse(map2));
4199         return map;
4200 }
4201
4202 __isl_give isl_map *isl_map_lex_lt_map(__isl_take isl_map *map1,
4203         __isl_take isl_map *map2)
4204 {
4205         isl_map *map;
4206         map = isl_map_lex_lt(isl_space_range(isl_map_get_space(map1)));
4207         map = isl_map_apply_domain(map, isl_map_reverse(map1));
4208         map = isl_map_apply_range(map, isl_map_reverse(map2));
4209         return map;
4210 }
4211
4212 __isl_give isl_map *isl_map_lex_ge_map(__isl_take isl_map *map1,
4213         __isl_take isl_map *map2)
4214 {
4215         isl_map *map;
4216         map = isl_map_lex_ge(isl_space_range(isl_map_get_space(map1)));
4217         map = isl_map_apply_domain(map, isl_map_reverse(map1));
4218         map = isl_map_apply_range(map, isl_map_reverse(map2));
4219         return map;
4220 }
4221
4222 __isl_give isl_map *isl_map_lex_gt_map(__isl_take isl_map *map1,
4223         __isl_take isl_map *map2)
4224 {
4225         isl_map *map;
4226         map = isl_map_lex_gt(isl_space_range(isl_map_get_space(map1)));
4227         map = isl_map_apply_domain(map, isl_map_reverse(map1));
4228         map = isl_map_apply_range(map, isl_map_reverse(map2));
4229         return map;
4230 }
4231
4232 __isl_give isl_basic_map *isl_basic_map_from_basic_set(
4233         __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4234 {
4235         struct isl_basic_map *bmap;
4236
4237         bset = isl_basic_set_cow(bset);
4238         if (!bset || !dim)
4239                 goto error;
4240
4241         isl_assert(bset->ctx, isl_space_compatible(bset->dim, dim), goto error);
4242         isl_space_free(bset->dim);
4243         bmap = (struct isl_basic_map *) bset;
4244         bmap->dim = dim;
4245         return isl_basic_map_finalize(bmap);
4246 error:
4247         isl_basic_set_free(bset);
4248         isl_space_free(dim);
4249         return NULL;
4250 }
4251
4252 struct isl_basic_set *isl_basic_set_from_basic_map(struct isl_basic_map *bmap)
4253 {
4254         if (!bmap)
4255                 goto error;
4256         if (bmap->dim->n_in == 0)
4257                 return (struct isl_basic_set *)bmap;
4258         bmap = isl_basic_map_cow(bmap);
4259         if (!bmap)
4260                 goto error;
4261         bmap->dim = isl_space_as_set_space(bmap->dim);
4262         if (!bmap->dim)
4263                 goto error;
4264         bmap = isl_basic_map_finalize(bmap);
4265         return (struct isl_basic_set *)bmap;
4266 error:
4267         isl_basic_map_free(bmap);
4268         return NULL;
4269 }
4270
4271 /* For a div d = floor(f/m), add the constraints
4272  *
4273  *              f - m d >= 0
4274  *              -(f-(n-1)) + m d >= 0
4275  *
4276  * Note that the second constraint is the negation of
4277  *
4278  *              f - m d >= n
4279  */
4280 int isl_basic_map_add_div_constraints_var(__isl_keep isl_basic_map *bmap,
4281         unsigned pos, isl_int *div)
4282 {
4283         int i, j;
4284         unsigned total = isl_basic_map_total_dim(bmap);
4285
4286         i = isl_basic_map_alloc_inequality(bmap);
4287         if (i < 0)
4288                 return -1;
4289         isl_seq_cpy(bmap->ineq[i], div + 1, 1 + total);
4290         isl_int_neg(bmap->ineq[i][1 + pos], div[0]);
4291
4292         j = isl_basic_map_alloc_inequality(bmap);
4293         if (j < 0)
4294                 return -1;
4295         isl_seq_neg(bmap->ineq[j], bmap->ineq[i], 1 + total);
4296         isl_int_add(bmap->ineq[j][0], bmap->ineq[j][0], bmap->ineq[j][1 + pos]);
4297         isl_int_sub_ui(bmap->ineq[j][0], bmap->ineq[j][0], 1);
4298         return j;
4299 }
4300
4301 int isl_basic_set_add_div_constraints_var(__isl_keep isl_basic_set *bset,
4302         unsigned pos, isl_int *div)
4303 {
4304         return isl_basic_map_add_div_constraints_var((isl_basic_map *)bset,
4305                                                         pos, div);
4306 }
4307
4308 int isl_basic_map_add_div_constraints(struct isl_basic_map *bmap, unsigned div)
4309 {
4310         unsigned total = isl_basic_map_total_dim(bmap);
4311         unsigned div_pos = total - bmap->n_div + div;
4312
4313         return isl_basic_map_add_div_constraints_var(bmap, div_pos,
4314                                                         bmap->div[div]);
4315 }
4316
4317 int isl_basic_set_add_div_constraints(struct isl_basic_set *bset, unsigned div)
4318 {
4319         return isl_basic_map_add_div_constraints(bset, div);
4320 }
4321
4322 struct isl_basic_set *isl_basic_map_underlying_set(
4323                 struct isl_basic_map *bmap)
4324 {
4325         if (!bmap)
4326                 goto error;
4327         if (bmap->dim->nparam == 0 && bmap->dim->n_in == 0 &&
4328             bmap->n_div == 0 &&
4329             !isl_space_is_named_or_nested(bmap->dim, isl_dim_in) &&
4330             !isl_space_is_named_or_nested(bmap->dim, isl_dim_out))
4331                 return (struct isl_basic_set *)bmap;
4332         bmap = isl_basic_map_cow(bmap);
4333         if (!bmap)
4334                 goto error;
4335         bmap->dim = isl_space_underlying(bmap->dim, bmap->n_div);
4336         if (!bmap->dim)
4337                 goto error;
4338         bmap->extra -= bmap->n_div;
4339         bmap->n_div = 0;
4340         bmap = isl_basic_map_finalize(bmap);
4341         return (struct isl_basic_set *)bmap;
4342 error:
4343         isl_basic_map_free(bmap);
4344         return NULL;
4345 }
4346
4347 __isl_give isl_basic_set *isl_basic_set_underlying_set(
4348                 __isl_take isl_basic_set *bset)
4349 {
4350         return isl_basic_map_underlying_set((isl_basic_map *)bset);
4351 }
4352
4353 struct isl_basic_map *isl_basic_map_overlying_set(
4354         struct isl_basic_set *bset, struct isl_basic_map *like)
4355 {
4356         struct isl_basic_map *bmap;
4357         struct isl_ctx *ctx;
4358         unsigned total;
4359         int i;
4360
4361         if (!bset || !like)
4362                 goto error;
4363         ctx = bset->ctx;
4364         isl_assert(ctx, bset->n_div == 0, goto error);
4365         isl_assert(ctx, isl_basic_set_n_param(bset) == 0, goto error);
4366         isl_assert(ctx, bset->dim->n_out == isl_basic_map_total_dim(like),
4367                         goto error);
4368         if (isl_space_is_equal(bset->dim, like->dim) && like->n_div == 0) {
4369                 isl_basic_map_free(like);
4370                 return (struct isl_basic_map *)bset;
4371         }
4372         bset = isl_basic_set_cow(bset);
4373         if (!bset)
4374                 goto error;
4375         total = bset->dim->n_out + bset->extra;
4376         bmap = (struct isl_basic_map *)bset;
4377         isl_space_free(bmap->dim);
4378         bmap->dim = isl_space_copy(like->dim);
4379         if (!bmap->dim)
4380                 goto error;
4381         bmap->n_div = like->n_div;
4382         bmap->extra += like->n_div;
4383         if (bmap->extra) {
4384                 unsigned ltotal;
4385                 isl_int **div;
4386                 ltotal = total - bmap->extra + like->extra;
4387                 if (ltotal > total)
4388                         ltotal = total;
4389                 bmap->block2 = isl_blk_extend(ctx, bmap->block2,
4390                                         bmap->extra * (1 + 1 + total));
4391                 if (isl_blk_is_error(bmap->block2))
4392                         goto error;
4393                 div = isl_realloc_array(ctx, bmap->div, isl_int *, bmap->extra);
4394                 if (!div)
4395                         goto error;
4396                 bmap->div = div;
4397                 for (i = 0; i < bmap->extra; ++i)
4398                         bmap->div[i] = bmap->block2.data + i * (1 + 1 + total);
4399                 for (i = 0; i < like->n_div; ++i) {
4400                         isl_seq_cpy(bmap->div[i], like->div[i], 1 + 1 + ltotal);
4401                         isl_seq_clr(bmap->div[i]+1+1+ltotal, total - ltotal);
4402                 }
4403                 bmap = isl_basic_map_extend_constraints(bmap, 
4404                                                         0, 2 * like->n_div);
4405                 for (i = 0; i < like->n_div; ++i) {
4406                         if (!bmap)
4407                                 break;
4408                         if (isl_int_is_zero(bmap->div[i][0]))
4409                                 continue;
4410                         if (isl_basic_map_add_div_constraints(bmap, i) < 0)
4411                                 bmap = isl_basic_map_free(bmap);
4412                 }
4413         }
4414         isl_basic_map_free(like);
4415         bmap = isl_basic_map_simplify(bmap);
4416         bmap = isl_basic_map_finalize(bmap);
4417         return bmap;
4418 error:
4419         isl_basic_map_free(like);
4420         isl_basic_set_free(bset);
4421         return NULL;
4422 }
4423
4424 struct isl_basic_set *isl_basic_set_from_underlying_set(
4425         struct isl_basic_set *bset, struct isl_basic_set *like)
4426 {
4427         return (struct isl_basic_set *)
4428                 isl_basic_map_overlying_set(bset, (struct isl_basic_map *)like);
4429 }
4430
4431 struct isl_set *isl_set_from_underlying_set(
4432         struct isl_set *set, struct isl_basic_set *like)
4433 {
4434         int i;
4435
4436         if (!set || !like)
4437                 goto error;
4438         isl_assert(set->ctx, set->dim->n_out == isl_basic_set_total_dim(like),
4439                     goto error);
4440         if (isl_space_is_equal(set->dim, like->dim) && like->n_div == 0) {
4441                 isl_basic_set_free(like);
4442                 return set;
4443         }
4444         set = isl_set_cow(set);
4445         if (!set)
4446                 goto error;
4447         for (i = 0; i < set->n; ++i) {
4448                 set->p[i] = isl_basic_set_from_underlying_set(set->p[i],
4449                                                       isl_basic_set_copy(like));
4450                 if (!set->p[i])
4451                         goto error;
4452         }
4453         isl_space_free(set->dim);
4454         set->dim = isl_space_copy(like->dim);
4455         if (!set->dim)
4456                 goto error;
4457         isl_basic_set_free(like);
4458         return set;
4459 error:
4460         isl_basic_set_free(like);
4461         isl_set_free(set);
4462         return NULL;
4463 }
4464
4465 struct isl_set *isl_map_underlying_set(struct isl_map *map)
4466 {
4467         int i;
4468
4469         map = isl_map_cow(map);
4470         if (!map)
4471                 return NULL;
4472         map->dim = isl_space_cow(map->dim);
4473         if (!map->dim)
4474                 goto error;
4475
4476         for (i = 1; i < map->n; ++i)
4477                 isl_assert(map->ctx, map->p[0]->n_div == map->p[i]->n_div,
4478                                 goto error);
4479         for (i = 0; i < map->n; ++i) {
4480                 map->p[i] = (struct isl_basic_map *)
4481                                 isl_basic_map_underlying_set(map->p[i]);
4482                 if (!map->p[i])
4483                         goto error;
4484         }
4485         if (map->n == 0)
4486                 map->dim = isl_space_underlying(map->dim, 0);
4487         else {
4488                 isl_space_free(map->dim);
4489                 map->dim = isl_space_copy(map->p[0]->dim);
4490         }
4491         if (!map->dim)
4492                 goto error;
4493         return (struct isl_set *)map;
4494 error:
4495         isl_map_free(map);
4496         return NULL;
4497 }
4498
4499 struct isl_set *isl_set_to_underlying_set(struct isl_set *set)
4500 {
4501         return (struct isl_set *)isl_map_underlying_set((struct isl_map *)set);
4502 }
4503
4504 __isl_give isl_basic_map *isl_basic_map_reset_space(
4505         __isl_take isl_basic_map *bmap, __isl_take isl_space *dim)
4506 {
4507         bmap = isl_basic_map_cow(bmap);
4508         if (!bmap || !dim)
4509                 goto error;
4510
4511         isl_space_free(bmap->dim);
4512         bmap->dim = dim;
4513
4514         bmap = isl_basic_map_finalize(bmap);
4515
4516         return bmap;
4517 error:
4518         isl_basic_map_free(bmap);
4519         isl_space_free(dim);
4520         return NULL;
4521 }
4522
4523 __isl_give isl_basic_set *isl_basic_set_reset_space(
4524         __isl_take isl_basic_set *bset, __isl_take isl_space *dim)
4525 {
4526         return (isl_basic_set *)isl_basic_map_reset_space((isl_basic_map *)bset,
4527                                                         dim);
4528 }
4529
4530 __isl_give isl_map *isl_map_reset_space(__isl_take isl_map *map,
4531         __isl_take isl_space *dim)
4532 {
4533         int i;
4534
4535         map = isl_map_cow(map);
4536         if (!map || !dim)
4537                 goto error;
4538
4539         for (i = 0; i < map->n; ++i) {
4540                 map->p[i] = isl_basic_map_reset_space(map->p[i],
4541                                                     isl_space_copy(dim));
4542                 if (!map->p[i])
4543                         goto error;
4544         }
4545         isl_space_free(map->dim);
4546         map->dim = dim;
4547
4548         return map;
4549 error:
4550         isl_map_free(map);
4551         isl_space_free(dim);
4552         return NULL;
4553 }
4554
4555 __isl_give isl_set *isl_set_reset_space(__isl_take isl_set *set,
4556         __isl_take isl_space *dim)
4557 {
4558         return (struct isl_set *) isl_map_reset_space((struct isl_map *)set, dim);
4559 }
4560
4561 /* Compute the parameter domain of the given basic set.
4562  */
4563 __isl_give isl_basic_set *isl_basic_set_params(__isl_take isl_basic_set *bset)
4564 {
4565         isl_space *space;
4566         unsigned n;
4567
4568         if (isl_basic_set_is_params(bset))
4569                 return bset;
4570
4571         n = isl_basic_set_dim(bset, isl_dim_set);
4572         bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
4573         space = isl_basic_set_get_space(bset);
4574         space = isl_space_params(space);
4575         bset = isl_basic_set_reset_space(bset, space);
4576         return bset;
4577 }
4578
4579 /* Construct a zero-dimensional basic set with the given parameter domain.
4580  */
4581 __isl_give isl_basic_set *isl_basic_set_from_params(
4582         __isl_take isl_basic_set *bset)
4583 {
4584         isl_space *space;
4585         space = isl_basic_set_get_space(bset);
4586         space = isl_space_set_from_params(space);
4587         bset = isl_basic_set_reset_space(bset, space);
4588         return bset;
4589 }
4590
4591 /* Compute the parameter domain of the given set.
4592  */
4593 __isl_give isl_set *isl_set_params(__isl_take isl_set *set)
4594 {
4595         isl_space *space;
4596         unsigned n;
4597
4598         if (isl_set_is_params(set))
4599                 return set;
4600
4601         n = isl_set_dim(set, isl_dim_set);
4602         set = isl_set_project_out(set, isl_dim_set, 0, n);
4603         space = isl_set_get_space(set);
4604         space = isl_space_params(space);
4605         set = isl_set_reset_space(set, space);
4606         return set;
4607 }
4608
4609 /* Construct a zero-dimensional set with the given parameter domain.
4610  */
4611 __isl_give isl_set *isl_set_from_params(__isl_take isl_set *set)
4612 {
4613         isl_space *space;
4614         space = isl_set_get_space(set);
4615         space = isl_space_set_from_params(space);
4616         set = isl_set_reset_space(set, space);
4617         return set;
4618 }
4619
4620 /* Compute the parameter domain of the given map.
4621  */
4622 __isl_give isl_set *isl_map_params(__isl_take isl_map *map)
4623 {
4624         isl_space *space;
4625         unsigned n;
4626
4627         n = isl_map_dim(map, isl_dim_in);
4628         map = isl_map_project_out(map, isl_dim_in, 0, n);
4629         n = isl_map_dim(map, isl_dim_out);
4630         map = isl_map_project_out(map, isl_dim_out, 0, n);
4631         space = isl_map_get_space(map);
4632         space = isl_space_params(space);
4633         map = isl_map_reset_space(map, space);
4634         return map;
4635 }
4636
4637 struct isl_basic_set *isl_basic_map_domain(struct isl_basic_map *bmap)
4638 {
4639         isl_space *dim;
4640         struct isl_basic_set *domain;
4641         unsigned n_in;
4642         unsigned n_out;
4643
4644         if (!bmap)
4645                 return NULL;
4646         dim = isl_space_domain(isl_basic_map_get_space(bmap));
4647
4648         n_in = isl_basic_map_n_in(bmap);
4649         n_out = isl_basic_map_n_out(bmap);
4650         domain = isl_basic_set_from_basic_map(bmap);
4651         domain = isl_basic_set_project_out(domain, isl_dim_set, n_in, n_out);
4652
4653         domain = isl_basic_set_reset_space(domain, dim);
4654
4655         return domain;
4656 }
4657
4658 int isl_basic_map_may_be_set(__isl_keep isl_basic_map *bmap)
4659 {
4660         if (!bmap)
4661                 return -1;
4662         return isl_space_may_be_set(bmap->dim);
4663 }
4664
4665 /* Is this basic map actually a set?
4666  * Users should never call this function.  Outside of isl,
4667  * the type should indicate whether something is a set or a map.
4668  */
4669 int isl_basic_map_is_set(__isl_keep isl_basic_map *bmap)
4670 {
4671         if (!bmap)
4672                 return -1;
4673         return isl_space_is_set(bmap->dim);
4674 }
4675
4676 struct isl_basic_set *isl_basic_map_range(struct isl_basic_map *bmap)
4677 {
4678         if (!bmap)
4679                 return NULL;
4680         if (isl_basic_map_is_set(bmap))
4681                 return bmap;
4682         return isl_basic_map_domain(isl_basic_map_reverse(bmap));
4683 }
4684
4685 __isl_give isl_basic_map *isl_basic_map_domain_map(
4686         __isl_take isl_basic_map *bmap)
4687 {
4688         int i, k;
4689         isl_space *dim;
4690         isl_basic_map *domain;
4691         int nparam, n_in, n_out;
4692         unsigned total;
4693
4694         nparam = isl_basic_map_dim(bmap, isl_dim_param);
4695         n_in = isl_basic_map_dim(bmap, isl_dim_in);
4696         n_out = isl_basic_map_dim(bmap, isl_dim_out);
4697
4698         dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
4699         domain = isl_basic_map_universe(dim);
4700
4701         bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4702         bmap = isl_basic_map_apply_range(bmap, domain);
4703         bmap = isl_basic_map_extend_constraints(bmap, n_in, 0);
4704
4705         total = isl_basic_map_total_dim(bmap);
4706
4707         for (i = 0; i < n_in; ++i) {
4708                 k = isl_basic_map_alloc_equality(bmap);
4709                 if (k < 0)
4710                         goto error;
4711                 isl_seq_clr(bmap->eq[k], 1 + total);
4712                 isl_int_set_si(bmap->eq[k][1 + nparam + i], -1);
4713                 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4714         }
4715
4716         bmap = isl_basic_map_gauss(bmap, NULL);
4717         return isl_basic_map_finalize(bmap);
4718 error:
4719         isl_basic_map_free(bmap);
4720         return NULL;
4721 }
4722
4723 __isl_give isl_basic_map *isl_basic_map_range_map(
4724         __isl_take isl_basic_map *bmap)
4725 {
4726         int i, k;
4727         isl_space *dim;
4728         isl_basic_map *range;
4729         int nparam, n_in, n_out;
4730         unsigned total;
4731
4732         nparam = isl_basic_map_dim(bmap, isl_dim_param);
4733         n_in = isl_basic_map_dim(bmap, isl_dim_in);
4734         n_out = isl_basic_map_dim(bmap, isl_dim_out);
4735
4736         dim = isl_space_from_range(isl_space_range(isl_basic_map_get_space(bmap)));
4737         range = isl_basic_map_universe(dim);
4738
4739         bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
4740         bmap = isl_basic_map_apply_range(bmap, range);
4741         bmap = isl_basic_map_extend_constraints(bmap, n_out, 0);
4742
4743         total = isl_basic_map_total_dim(bmap);
4744
4745         for (i = 0; i < n_out; ++i) {
4746                 k = isl_basic_map_alloc_equality(bmap);
4747                 if (k < 0)
4748                         goto error;
4749                 isl_seq_clr(bmap->eq[k], 1 + total);
4750                 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + i], -1);
4751                 isl_int_set_si(bmap->eq[k][1 + nparam + n_in + n_out + i], 1);
4752         }
4753
4754         bmap = isl_basic_map_gauss(bmap, NULL);
4755         return isl_basic_map_finalize(bmap);
4756 error:
4757         isl_basic_map_free(bmap);
4758         return NULL;
4759 }
4760
4761 int isl_map_may_be_set(__isl_keep isl_map *map)
4762 {
4763         if (!map)
4764                 return -1;
4765         return isl_space_may_be_set(map->dim);
4766 }
4767
4768 /* Is this map actually a set?
4769  * Users should never call this function.  Outside of isl,
4770  * the type should indicate whether something is a set or a map.
4771  */
4772 int isl_map_is_set(__isl_keep isl_map *map)
4773 {
4774         if (!map)
4775                 return -1;
4776         return isl_space_is_set(map->dim);
4777 }
4778
4779 struct isl_set *isl_map_range(struct isl_map *map)
4780 {
4781         int i;
4782         struct isl_set *set;
4783
4784         if (!map)
4785                 goto error;
4786         if (isl_map_is_set(map))
4787                 return (isl_set *)map;
4788
4789         map = isl_map_cow(map);
4790         if (!map)
4791                 goto error;
4792
4793         set = (struct isl_set *) map;
4794         set->dim = isl_space_range(set->dim);
4795         if (!set->dim)
4796                 goto error;
4797         for (i = 0; i < map->n; ++i) {
4798                 set->p[i] = isl_basic_map_range(map->p[i]);
4799                 if (!set->p[i])
4800                         goto error;
4801         }
4802         ISL_F_CLR(set, ISL_MAP_DISJOINT);
4803         ISL_F_CLR(set, ISL_SET_NORMALIZED);
4804         return set;
4805 error:
4806         isl_map_free(map);
4807         return NULL;
4808 }
4809
4810 __isl_give isl_map *isl_map_domain_map(__isl_take isl_map *map)
4811 {
4812         int i;
4813         isl_space *domain_dim;
4814
4815         map = isl_map_cow(map);
4816         if (!map)
4817                 return NULL;
4818
4819         domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
4820         map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4821         map->dim = isl_space_join(map->dim, domain_dim);
4822         if (!map->dim)
4823                 goto error;
4824         for (i = 0; i < map->n; ++i) {
4825                 map->p[i] = isl_basic_map_domain_map(map->p[i]);
4826                 if (!map->p[i])
4827                         goto error;
4828         }
4829         ISL_F_CLR(map, ISL_MAP_DISJOINT);
4830         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4831         return map;
4832 error:
4833         isl_map_free(map);
4834         return NULL;
4835 }
4836
4837 __isl_give isl_map *isl_map_range_map(__isl_take isl_map *map)
4838 {
4839         int i;
4840         isl_space *range_dim;
4841
4842         map = isl_map_cow(map);
4843         if (!map)
4844                 return NULL;
4845
4846         range_dim = isl_space_range(isl_map_get_space(map));
4847         range_dim = isl_space_from_range(range_dim);
4848         map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
4849         map->dim = isl_space_join(map->dim, range_dim);
4850         if (!map->dim)
4851                 goto error;
4852         for (i = 0; i < map->n; ++i) {
4853                 map->p[i] = isl_basic_map_range_map(map->p[i]);
4854                 if (!map->p[i])
4855                         goto error;
4856         }
4857         ISL_F_CLR(map, ISL_MAP_DISJOINT);
4858         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
4859         return map;
4860 error:
4861         isl_map_free(map);
4862         return NULL;
4863 }
4864
4865 __isl_give isl_map *isl_map_from_set(__isl_take isl_set *set,
4866         __isl_take isl_space *dim)
4867 {
4868         int i;
4869         struct isl_map *map = NULL;
4870
4871         set = isl_set_cow(set);
4872         if (!set || !dim)
4873                 goto error;
4874         isl_assert(set->ctx, isl_space_compatible(set->dim, dim), goto error);
4875         map = (struct isl_map *)set;
4876         for (i = 0; i < set->n; ++i) {
4877                 map->p[i] = isl_basic_map_from_basic_set(
4878                                 set->p[i], isl_space_copy(dim));
4879                 if (!map->p[i])
4880                         goto error;
4881         }
4882         isl_space_free(map->dim);
4883         map->dim = dim;
4884         return map;
4885 error:
4886         isl_space_free(dim);
4887         isl_set_free(set);
4888         return NULL;
4889 }
4890
4891 __isl_give isl_basic_map *isl_basic_map_from_domain(
4892         __isl_take isl_basic_set *bset)
4893 {
4894         return isl_basic_map_reverse(isl_basic_map_from_range(bset));
4895 }
4896
4897 __isl_give isl_basic_map *isl_basic_map_from_range(
4898         __isl_take isl_basic_set *bset)
4899 {
4900         isl_space *space;
4901         space = isl_basic_set_get_space(bset);
4902         space = isl_space_from_range(space);
4903         bset = isl_basic_set_reset_space(bset, space);
4904         return (isl_basic_map *)bset;
4905 }
4906
4907 struct isl_map *isl_map_from_range(struct isl_set *set)
4908 {
4909         isl_space *space;
4910         space = isl_set_get_space(set);
4911         space = isl_space_from_range(space);
4912         set = isl_set_reset_space(set, space);
4913         return (struct isl_map *)set;
4914 }
4915
4916 __isl_give isl_map *isl_map_from_domain(__isl_take isl_set *set)
4917 {
4918         return isl_map_reverse(isl_map_from_range(set));
4919 }
4920
4921 __isl_give isl_basic_map *isl_basic_map_from_domain_and_range(
4922         __isl_take isl_basic_set *domain, __isl_take isl_basic_set *range)
4923 {
4924         return isl_basic_map_apply_range(isl_basic_map_reverse(domain), range);
4925 }
4926
4927 __isl_give isl_map *isl_map_from_domain_and_range(__isl_take isl_set *domain,
4928         __isl_take isl_set *range)
4929 {
4930         return isl_map_apply_range(isl_map_reverse(domain), range);
4931 }
4932
4933 struct isl_set *isl_set_from_map(struct isl_map *map)
4934 {
4935         int i;
4936         struct isl_set *set = NULL;
4937
4938         if (!map)
4939                 return NULL;
4940         map = isl_map_cow(map);
4941         if (!map)
4942                 return NULL;
4943         map->dim = isl_space_as_set_space(map->dim);
4944         if (!map->dim)
4945                 goto error;
4946         set = (struct isl_set *)map;
4947         for (i = 0; i < map->n; ++i) {
4948                 set->p[i] = isl_basic_set_from_basic_map(map->p[i]);
4949                 if (!set->p[i])
4950                         goto error;
4951         }
4952         return set;
4953 error:
4954         isl_map_free(map);
4955         return NULL;
4956 }
4957
4958 __isl_give isl_map *isl_map_alloc_space(__isl_take isl_space *dim, int n,
4959         unsigned flags)
4960 {
4961         struct isl_map *map;
4962
4963         if (!dim)
4964                 return NULL;
4965         if (n < 0)
4966                 isl_die(dim->ctx, isl_error_internal,
4967                         "negative number of basic maps", goto error);
4968         map = isl_alloc(dim->ctx, struct isl_map,
4969                         sizeof(struct isl_map) +
4970                         (n - 1) * sizeof(struct isl_basic_map *));
4971         if (!map)
4972                 goto error;
4973
4974         map->ctx = dim->ctx;
4975         isl_ctx_ref(map->ctx);
4976         map->ref = 1;
4977         map->size = n;
4978         map->n = 0;
4979         map->dim = dim;
4980         map->flags = flags;
4981         return map;
4982 error:
4983         isl_space_free(dim);
4984         return NULL;
4985 }
4986
4987 struct isl_map *isl_map_alloc(struct isl_ctx *ctx,
4988                 unsigned nparam, unsigned in, unsigned out, int n,
4989                 unsigned flags)
4990 {
4991         struct isl_map *map;
4992         isl_space *dims;
4993
4994         dims = isl_space_alloc(ctx, nparam, in, out);
4995         if (!dims)
4996                 return NULL;
4997
4998         map = isl_map_alloc_space(dims, n, flags);
4999         return map;
5000 }
5001
5002 __isl_give isl_basic_map *isl_basic_map_empty(__isl_take isl_space *dim)
5003 {
5004         struct isl_basic_map *bmap;
5005         bmap = isl_basic_map_alloc_space(dim, 0, 1, 0);
5006         bmap = isl_basic_map_set_to_empty(bmap);
5007         return bmap;
5008 }
5009
5010 __isl_give isl_basic_set *isl_basic_set_empty(__isl_take isl_space *dim)
5011 {
5012         struct isl_basic_set *bset;
5013         bset = isl_basic_set_alloc_space(dim, 0, 1, 0);
5014         bset = isl_basic_set_set_to_empty(bset);
5015         return bset;
5016 }
5017
5018 struct isl_basic_map *isl_basic_map_empty_like(struct isl_basic_map *model)
5019 {
5020         struct isl_basic_map *bmap;
5021         if (!model)
5022                 return NULL;
5023         bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5024         bmap = isl_basic_map_set_to_empty(bmap);
5025         return bmap;
5026 }
5027
5028 struct isl_basic_map *isl_basic_map_empty_like_map(struct isl_map *model)
5029 {
5030         struct isl_basic_map *bmap;
5031         if (!model)
5032                 return NULL;
5033         bmap = isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5034         bmap = isl_basic_map_set_to_empty(bmap);
5035         return bmap;
5036 }
5037
5038 struct isl_basic_set *isl_basic_set_empty_like(struct isl_basic_set *model)
5039 {
5040         struct isl_basic_set *bset;
5041         if (!model)
5042                 return NULL;
5043         bset = isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 1, 0);
5044         bset = isl_basic_set_set_to_empty(bset);
5045         return bset;
5046 }
5047
5048 __isl_give isl_basic_map *isl_basic_map_universe(__isl_take isl_space *dim)
5049 {
5050         struct isl_basic_map *bmap;
5051         bmap = isl_basic_map_alloc_space(dim, 0, 0, 0);
5052         bmap = isl_basic_map_finalize(bmap);
5053         return bmap;
5054 }
5055
5056 __isl_give isl_basic_set *isl_basic_set_universe(__isl_take isl_space *dim)
5057 {
5058         struct isl_basic_set *bset;
5059         bset = isl_basic_set_alloc_space(dim, 0, 0, 0);
5060         bset = isl_basic_set_finalize(bset);
5061         return bset;
5062 }
5063
5064 __isl_give isl_basic_map *isl_basic_map_nat_universe(__isl_take isl_space *dim)
5065 {
5066         int i;
5067         unsigned total = isl_space_dim(dim, isl_dim_all);
5068         isl_basic_map *bmap;
5069
5070         bmap= isl_basic_map_alloc_space(dim, 0, 0, total);
5071         for (i = 0; i < total; ++i) {
5072                 int k = isl_basic_map_alloc_inequality(bmap);
5073                 if (k < 0)
5074                         goto error;
5075                 isl_seq_clr(bmap->ineq[k], 1 + total);
5076                 isl_int_set_si(bmap->ineq[k][1 + i], 1);
5077         }
5078         return bmap;
5079 error:
5080         isl_basic_map_free(bmap);
5081         return NULL;
5082 }
5083
5084 __isl_give isl_basic_set *isl_basic_set_nat_universe(__isl_take isl_space *dim)
5085 {
5086         return isl_basic_map_nat_universe(dim);
5087 }
5088
5089 __isl_give isl_map *isl_map_nat_universe(__isl_take isl_space *dim)
5090 {
5091         return isl_map_from_basic_map(isl_basic_map_nat_universe(dim));
5092 }
5093
5094 __isl_give isl_set *isl_set_nat_universe(__isl_take isl_space *dim)
5095 {
5096         return isl_map_nat_universe(dim);
5097 }
5098
5099 __isl_give isl_basic_map *isl_basic_map_universe_like(
5100                 __isl_keep isl_basic_map *model)
5101 {
5102         if (!model)
5103                 return NULL;
5104         return isl_basic_map_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5105 }
5106
5107 struct isl_basic_set *isl_basic_set_universe_like(struct isl_basic_set *model)
5108 {
5109         if (!model)
5110                 return NULL;
5111         return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5112 }
5113
5114 __isl_give isl_basic_set *isl_basic_set_universe_like_set(
5115         __isl_keep isl_set *model)
5116 {
5117         if (!model)
5118                 return NULL;
5119         return isl_basic_set_alloc_space(isl_space_copy(model->dim), 0, 0, 0);
5120 }
5121
5122 __isl_give isl_map *isl_map_empty(__isl_take isl_space *dim)
5123 {
5124         return isl_map_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5125 }
5126
5127 struct isl_map *isl_map_empty_like(struct isl_map *model)
5128 {
5129         if (!model)
5130                 return NULL;
5131         return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5132 }
5133
5134 struct isl_map *isl_map_empty_like_basic_map(struct isl_basic_map *model)
5135 {
5136         if (!model)
5137                 return NULL;
5138         return isl_map_alloc_space(isl_space_copy(model->dim), 0, ISL_MAP_DISJOINT);
5139 }
5140
5141 __isl_give isl_set *isl_set_empty(__isl_take isl_space *dim)
5142 {
5143         return isl_set_alloc_space(dim, 0, ISL_MAP_DISJOINT);
5144 }
5145
5146 struct isl_set *isl_set_empty_like(struct isl_set *model)
5147 {
5148         if (!model)
5149                 return NULL;
5150         return isl_set_empty(isl_space_copy(model->dim));
5151 }
5152
5153 __isl_give isl_map *isl_map_universe(__isl_take isl_space *dim)
5154 {
5155         struct isl_map *map;
5156         if (!dim)
5157                 return NULL;
5158         map = isl_map_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5159         map = isl_map_add_basic_map(map, isl_basic_map_universe(dim));
5160         return map;
5161 }
5162
5163 __isl_give isl_set *isl_set_universe(__isl_take isl_space *dim)
5164 {
5165         struct isl_set *set;
5166         if (!dim)
5167                 return NULL;
5168         set = isl_set_alloc_space(isl_space_copy(dim), 1, ISL_MAP_DISJOINT);
5169         set = isl_set_add_basic_set(set, isl_basic_set_universe(dim));
5170         return set;
5171 }
5172
5173 __isl_give isl_set *isl_set_universe_like(__isl_keep isl_set *model)
5174 {
5175         if (!model)
5176                 return NULL;
5177         return isl_set_universe(isl_space_copy(model->dim));
5178 }
5179
5180 struct isl_map *isl_map_dup(struct isl_map *map)
5181 {
5182         int i;
5183         struct isl_map *dup;
5184
5185         if (!map)
5186                 return NULL;
5187         dup = isl_map_alloc_space(isl_space_copy(map->dim), map->n, map->flags);
5188         for (i = 0; i < map->n; ++i)
5189                 dup = isl_map_add_basic_map(dup, isl_basic_map_copy(map->p[i]));
5190         return dup;
5191 }
5192
5193 __isl_give isl_map *isl_map_add_basic_map(__isl_take isl_map *map,
5194                                                 __isl_take isl_basic_map *bmap)
5195 {
5196         if (!bmap || !map)
5197                 goto error;
5198         if (isl_basic_map_plain_is_empty(bmap)) {
5199                 isl_basic_map_free(bmap);
5200                 return map;
5201         }
5202         isl_assert(map->ctx, isl_space_is_equal(map->dim, bmap->dim), goto error);
5203         isl_assert(map->ctx, map->n < map->size, goto error);
5204         map->p[map->n] = bmap;
5205         map->n++;
5206         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5207         return map;
5208 error:
5209         if (map)
5210                 isl_map_free(map);
5211         if (bmap)
5212                 isl_basic_map_free(bmap);
5213         return NULL;
5214 }
5215
5216 void *isl_map_free(struct isl_map *map)
5217 {
5218         int i;
5219
5220         if (!map)
5221                 return NULL;
5222
5223         if (--map->ref > 0)
5224                 return NULL;
5225
5226         isl_ctx_deref(map->ctx);
5227         for (i = 0; i < map->n; ++i)
5228                 isl_basic_map_free(map->p[i]);
5229         isl_space_free(map->dim);
5230         free(map);
5231
5232         return NULL;
5233 }
5234
5235 struct isl_map *isl_map_extend(struct isl_map *base,
5236                 unsigned nparam, unsigned n_in, unsigned n_out)
5237 {
5238         int i;
5239
5240         base = isl_map_cow(base);
5241         if (!base)
5242                 return NULL;
5243
5244         base->dim = isl_space_extend(base->dim, nparam, n_in, n_out);
5245         if (!base->dim)
5246                 goto error;
5247         for (i = 0; i < base->n; ++i) {
5248                 base->p[i] = isl_basic_map_extend_space(base->p[i],
5249                                 isl_space_copy(base->dim), 0, 0, 0);
5250                 if (!base->p[i])
5251                         goto error;
5252         }
5253         return base;
5254 error:
5255         isl_map_free(base);
5256         return NULL;
5257 }
5258
5259 struct isl_set *isl_set_extend(struct isl_set *base,
5260                 unsigned nparam, unsigned dim)
5261 {
5262         return (struct isl_set *)isl_map_extend((struct isl_map *)base,
5263                                                         nparam, 0, dim);
5264 }
5265
5266 static struct isl_basic_map *isl_basic_map_fix_pos_si(
5267         struct isl_basic_map *bmap, unsigned pos, int value)
5268 {
5269         int j;
5270
5271         bmap = isl_basic_map_cow(bmap);
5272         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5273         j = isl_basic_map_alloc_equality(bmap);
5274         if (j < 0)
5275                 goto error;
5276         isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5277         isl_int_set_si(bmap->eq[j][pos], -1);
5278         isl_int_set_si(bmap->eq[j][0], value);
5279         bmap = isl_basic_map_simplify(bmap);
5280         return isl_basic_map_finalize(bmap);
5281 error:
5282         isl_basic_map_free(bmap);
5283         return NULL;
5284 }
5285
5286 static __isl_give isl_basic_map *isl_basic_map_fix_pos(
5287         __isl_take isl_basic_map *bmap, unsigned pos, isl_int value)
5288 {
5289         int j;
5290
5291         bmap = isl_basic_map_cow(bmap);
5292         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
5293         j = isl_basic_map_alloc_equality(bmap);
5294         if (j < 0)
5295                 goto error;
5296         isl_seq_clr(bmap->eq[j] + 1, isl_basic_map_total_dim(bmap));
5297         isl_int_set_si(bmap->eq[j][pos], -1);
5298         isl_int_set(bmap->eq[j][0], value);
5299         bmap = isl_basic_map_simplify(bmap);
5300         return isl_basic_map_finalize(bmap);
5301 error:
5302         isl_basic_map_free(bmap);
5303         return NULL;
5304 }
5305
5306 struct isl_basic_map *isl_basic_map_fix_si(struct isl_basic_map *bmap,
5307                 enum isl_dim_type type, unsigned pos, int value)
5308 {
5309         if (!bmap)
5310                 return NULL;
5311         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5312         return isl_basic_map_fix_pos_si(bmap,
5313                 isl_basic_map_offset(bmap, type) + pos, value);
5314 error:
5315         isl_basic_map_free(bmap);
5316         return NULL;
5317 }
5318
5319 __isl_give isl_basic_map *isl_basic_map_fix(__isl_take isl_basic_map *bmap,
5320                 enum isl_dim_type type, unsigned pos, isl_int value)
5321 {
5322         if (!bmap)
5323                 return NULL;
5324         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5325         return isl_basic_map_fix_pos(bmap,
5326                 isl_basic_map_offset(bmap, type) + pos, value);
5327 error:
5328         isl_basic_map_free(bmap);
5329         return NULL;
5330 }
5331
5332 /* Fix the value of the variable at position "pos" of type "type" of "bmap"
5333  * to be equal to "v".
5334  */
5335 __isl_give isl_basic_map *isl_basic_map_fix_val(__isl_take isl_basic_map *bmap,
5336         enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5337 {
5338         if (!bmap || !v)
5339                 goto error;
5340         if (!isl_val_is_int(v))
5341                 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5342                         "expecting integer value", goto error);
5343         if (pos >= isl_basic_map_dim(bmap, type))
5344                 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
5345                         "index out of bounds", goto error);
5346         pos += isl_basic_map_offset(bmap, type);
5347         bmap = isl_basic_map_fix_pos(bmap, pos, v->n);
5348         isl_val_free(v);
5349         return bmap;
5350 error:
5351         isl_basic_map_free(bmap);
5352         isl_val_free(v);
5353         return NULL;
5354 }
5355
5356 /* Fix the value of the variable at position "pos" of type "type" of "bset"
5357  * to be equal to "v".
5358  */
5359 __isl_give isl_basic_set *isl_basic_set_fix_val(__isl_take isl_basic_set *bset,
5360         enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5361 {
5362         return isl_basic_map_fix_val(bset, type, pos, v);
5363 }
5364
5365 struct isl_basic_set *isl_basic_set_fix_si(struct isl_basic_set *bset,
5366                 enum isl_dim_type type, unsigned pos, int value)
5367 {
5368         return (struct isl_basic_set *)
5369                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5370                                         type, pos, value);
5371 }
5372
5373 __isl_give isl_basic_set *isl_basic_set_fix(__isl_take isl_basic_set *bset,
5374                 enum isl_dim_type type, unsigned pos, isl_int value)
5375 {
5376         return (struct isl_basic_set *)
5377                 isl_basic_map_fix((struct isl_basic_map *)bset,
5378                                         type, pos, value);
5379 }
5380
5381 struct isl_basic_map *isl_basic_map_fix_input_si(struct isl_basic_map *bmap,
5382                 unsigned input, int value)
5383 {
5384         return isl_basic_map_fix_si(bmap, isl_dim_in, input, value);
5385 }
5386
5387 struct isl_basic_set *isl_basic_set_fix_dim_si(struct isl_basic_set *bset,
5388                 unsigned dim, int value)
5389 {
5390         return (struct isl_basic_set *)
5391                 isl_basic_map_fix_si((struct isl_basic_map *)bset,
5392                                         isl_dim_set, dim, value);
5393 }
5394
5395 static int remove_if_empty(__isl_keep isl_map *map, int i)
5396 {
5397         int empty = isl_basic_map_plain_is_empty(map->p[i]);
5398
5399         if (empty < 0)
5400                 return -1;
5401         if (!empty)
5402                 return 0;
5403
5404         isl_basic_map_free(map->p[i]);
5405         if (i != map->n - 1) {
5406                 ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5407                 map->p[i] = map->p[map->n - 1];
5408         }
5409         map->n--;
5410
5411         return 0;
5412 }
5413
5414 /* Perform "fn" on each basic map of "map", where we may not be holding
5415  * the only reference to "map".
5416  * In particular, "fn" should be a semantics preserving operation
5417  * that we want to apply to all copies of "map".  We therefore need
5418  * to be careful not to modify "map" in a way that breaks "map"
5419  * in case anything goes wrong.
5420  */
5421 __isl_give isl_map *isl_map_inline_foreach_basic_map(__isl_take isl_map *map,
5422         __isl_give isl_basic_map *(*fn)(__isl_take isl_basic_map *bmap))
5423 {
5424         struct isl_basic_map *bmap;
5425         int i;
5426
5427         if (!map)
5428                 return NULL;
5429
5430         for (i = map->n - 1; i >= 0; --i) {
5431                 bmap = isl_basic_map_copy(map->p[i]);
5432                 bmap = fn(bmap);
5433                 if (!bmap)
5434                         goto error;
5435                 isl_basic_map_free(map->p[i]);
5436                 map->p[i] = bmap;
5437                 if (remove_if_empty(map, i) < 0)
5438                         goto error;
5439         }
5440
5441         return map;
5442 error:
5443         isl_map_free(map);
5444         return NULL;
5445 }
5446
5447 struct isl_map *isl_map_fix_si(struct isl_map *map,
5448                 enum isl_dim_type type, unsigned pos, int value)
5449 {
5450         int i;
5451
5452         map = isl_map_cow(map);
5453         if (!map)
5454                 return NULL;
5455
5456         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5457         for (i = map->n - 1; i >= 0; --i) {
5458                 map->p[i] = isl_basic_map_fix_si(map->p[i], type, pos, value);
5459                 if (remove_if_empty(map, i) < 0)
5460                         goto error;
5461         }
5462         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5463         return map;
5464 error:
5465         isl_map_free(map);
5466         return NULL;
5467 }
5468
5469 __isl_give isl_set *isl_set_fix_si(__isl_take isl_set *set,
5470                 enum isl_dim_type type, unsigned pos, int value)
5471 {
5472         return (struct isl_set *)
5473                 isl_map_fix_si((struct isl_map *)set, type, pos, value);
5474 }
5475
5476 __isl_give isl_map *isl_map_fix(__isl_take isl_map *map,
5477                 enum isl_dim_type type, unsigned pos, isl_int value)
5478 {
5479         int i;
5480
5481         map = isl_map_cow(map);
5482         if (!map)
5483                 return NULL;
5484
5485         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5486         for (i = 0; i < map->n; ++i) {
5487                 map->p[i] = isl_basic_map_fix(map->p[i], type, pos, value);
5488                 if (!map->p[i])
5489                         goto error;
5490         }
5491         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5492         return map;
5493 error:
5494         isl_map_free(map);
5495         return NULL;
5496 }
5497
5498 __isl_give isl_set *isl_set_fix(__isl_take isl_set *set,
5499                 enum isl_dim_type type, unsigned pos, isl_int value)
5500 {
5501         return (struct isl_set *)isl_map_fix((isl_map *)set, type, pos, value);
5502 }
5503
5504 /* Fix the value of the variable at position "pos" of type "type" of "map"
5505  * to be equal to "v".
5506  */
5507 __isl_give isl_map *isl_map_fix_val(__isl_take isl_map *map,
5508         enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5509 {
5510         int i;
5511
5512         map = isl_map_cow(map);
5513         if (!map || !v)
5514                 goto error;
5515
5516         if (!isl_val_is_int(v))
5517                 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5518                         "expecting integer value", goto error);
5519         if (pos >= isl_map_dim(map, type))
5520                 isl_die(isl_map_get_ctx(map), isl_error_invalid,
5521                         "index out of bounds", goto error);
5522         for (i = map->n - 1; i >= 0; --i) {
5523                 map->p[i] = isl_basic_map_fix_val(map->p[i], type, pos,
5524                                                         isl_val_copy(v));
5525                 if (remove_if_empty(map, i) < 0)
5526                         goto error;
5527         }
5528         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5529         isl_val_free(v);
5530         return map;
5531 error:
5532         isl_map_free(map);
5533         isl_val_free(v);
5534         return NULL;
5535 }
5536
5537 /* Fix the value of the variable at position "pos" of type "type" of "set"
5538  * to be equal to "v".
5539  */
5540 __isl_give isl_set *isl_set_fix_val(__isl_take isl_set *set,
5541         enum isl_dim_type type, unsigned pos, __isl_take isl_val *v)
5542 {
5543         return isl_map_fix_val(set, type, pos, v);
5544 }
5545
5546 struct isl_map *isl_map_fix_input_si(struct isl_map *map,
5547                 unsigned input, int value)
5548 {
5549         return isl_map_fix_si(map, isl_dim_in, input, value);
5550 }
5551
5552 struct isl_set *isl_set_fix_dim_si(struct isl_set *set, unsigned dim, int value)
5553 {
5554         return (struct isl_set *)
5555                 isl_map_fix_si((struct isl_map *)set, isl_dim_set, dim, value);
5556 }
5557
5558 static __isl_give isl_basic_map *basic_map_bound_si(
5559         __isl_take isl_basic_map *bmap,
5560         enum isl_dim_type type, unsigned pos, int value, int upper)
5561 {
5562         int j;
5563
5564         if (!bmap)
5565                 return NULL;
5566         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), goto error);
5567         pos += isl_basic_map_offset(bmap, type);
5568         bmap = isl_basic_map_cow(bmap);
5569         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5570         j = isl_basic_map_alloc_inequality(bmap);
5571         if (j < 0)
5572                 goto error;
5573         isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5574         if (upper) {
5575                 isl_int_set_si(bmap->ineq[j][pos], -1);
5576                 isl_int_set_si(bmap->ineq[j][0], value);
5577         } else {
5578                 isl_int_set_si(bmap->ineq[j][pos], 1);
5579                 isl_int_set_si(bmap->ineq[j][0], -value);
5580         }
5581         bmap = isl_basic_map_simplify(bmap);
5582         return isl_basic_map_finalize(bmap);
5583 error:
5584         isl_basic_map_free(bmap);
5585         return NULL;
5586 }
5587
5588 __isl_give isl_basic_map *isl_basic_map_lower_bound_si(
5589         __isl_take isl_basic_map *bmap,
5590         enum isl_dim_type type, unsigned pos, int value)
5591 {
5592         return basic_map_bound_si(bmap, type, pos, value, 0);
5593 }
5594
5595 /* Constrain the values of the given dimension to be no greater than "value".
5596  */
5597 __isl_give isl_basic_map *isl_basic_map_upper_bound_si(
5598         __isl_take isl_basic_map *bmap,
5599         enum isl_dim_type type, unsigned pos, int value)
5600 {
5601         return basic_map_bound_si(bmap, type, pos, value, 1);
5602 }
5603
5604 struct isl_basic_set *isl_basic_set_lower_bound_dim(struct isl_basic_set *bset,
5605         unsigned dim, isl_int value)
5606 {
5607         int j;
5608
5609         bset = isl_basic_set_cow(bset);
5610         bset = isl_basic_set_extend_constraints(bset, 0, 1);
5611         j = isl_basic_set_alloc_inequality(bset);
5612         if (j < 0)
5613                 goto error;
5614         isl_seq_clr(bset->ineq[j], 1 + isl_basic_set_total_dim(bset));
5615         isl_int_set_si(bset->ineq[j][1 + isl_basic_set_n_param(bset) + dim], 1);
5616         isl_int_neg(bset->ineq[j][0], value);
5617         bset = isl_basic_set_simplify(bset);
5618         return isl_basic_set_finalize(bset);
5619 error:
5620         isl_basic_set_free(bset);
5621         return NULL;
5622 }
5623
5624 static __isl_give isl_map *map_bound_si(__isl_take isl_map *map,
5625         enum isl_dim_type type, unsigned pos, int value, int upper)
5626 {
5627         int i;
5628
5629         map = isl_map_cow(map);
5630         if (!map)
5631                 return NULL;
5632
5633         isl_assert(map->ctx, pos < isl_map_dim(map, type), goto error);
5634         for (i = 0; i < map->n; ++i) {
5635                 map->p[i] = basic_map_bound_si(map->p[i],
5636                                                  type, pos, value, upper);
5637                 if (!map->p[i])
5638                         goto error;
5639         }
5640         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5641         return map;
5642 error:
5643         isl_map_free(map);
5644         return NULL;
5645 }
5646
5647 __isl_give isl_map *isl_map_lower_bound_si(__isl_take isl_map *map,
5648         enum isl_dim_type type, unsigned pos, int value)
5649 {
5650         return map_bound_si(map, type, pos, value, 0);
5651 }
5652
5653 __isl_give isl_map *isl_map_upper_bound_si(__isl_take isl_map *map,
5654         enum isl_dim_type type, unsigned pos, int value)
5655 {
5656         return map_bound_si(map, type, pos, value, 1);
5657 }
5658
5659 __isl_give isl_set *isl_set_lower_bound_si(__isl_take isl_set *set,
5660                 enum isl_dim_type type, unsigned pos, int value)
5661 {
5662         return (struct isl_set *)
5663                 isl_map_lower_bound_si((struct isl_map *)set, type, pos, value);
5664 }
5665
5666 __isl_give isl_set *isl_set_upper_bound_si(__isl_take isl_set *set,
5667         enum isl_dim_type type, unsigned pos, int value)
5668 {
5669         return isl_map_upper_bound_si(set, type, pos, value);
5670 }
5671
5672 /* Bound the given variable of "bmap" from below (or above is "upper"
5673  * is set) to "value".
5674  */
5675 static __isl_give isl_basic_map *basic_map_bound(
5676         __isl_take isl_basic_map *bmap,
5677         enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5678 {
5679         int j;
5680
5681         if (!bmap)
5682                 return NULL;
5683         if (pos >= isl_basic_map_dim(bmap, type))
5684                 isl_die(bmap->ctx, isl_error_invalid,
5685                         "index out of bounds", goto error);
5686         pos += isl_basic_map_offset(bmap, type);
5687         bmap = isl_basic_map_cow(bmap);
5688         bmap = isl_basic_map_extend_constraints(bmap, 0, 1);
5689         j = isl_basic_map_alloc_inequality(bmap);
5690         if (j < 0)
5691                 goto error;
5692         isl_seq_clr(bmap->ineq[j], 1 + isl_basic_map_total_dim(bmap));
5693         if (upper) {
5694                 isl_int_set_si(bmap->ineq[j][pos], -1);
5695                 isl_int_set(bmap->ineq[j][0], value);
5696         } else {
5697                 isl_int_set_si(bmap->ineq[j][pos], 1);
5698                 isl_int_neg(bmap->ineq[j][0], value);
5699         }
5700         bmap = isl_basic_map_simplify(bmap);
5701         return isl_basic_map_finalize(bmap);
5702 error:
5703         isl_basic_map_free(bmap);
5704         return NULL;
5705 }
5706
5707 /* Bound the given variable of "map" from below (or above is "upper"
5708  * is set) to "value".
5709  */
5710 static __isl_give isl_map *map_bound(__isl_take isl_map *map,
5711         enum isl_dim_type type, unsigned pos, isl_int value, int upper)
5712 {
5713         int i;
5714
5715         map = isl_map_cow(map);
5716         if (!map)
5717                 return NULL;
5718
5719         if (pos >= isl_map_dim(map, type))
5720                 isl_die(map->ctx, isl_error_invalid,
5721                         "index out of bounds", goto error);
5722         for (i = map->n - 1; i >= 0; --i) {
5723                 map->p[i] = basic_map_bound(map->p[i], type, pos, value, upper);
5724                 if (remove_if_empty(map, i) < 0)
5725                         goto error;
5726         }
5727         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5728         return map;
5729 error:
5730         isl_map_free(map);
5731         return NULL;
5732 }
5733
5734 __isl_give isl_map *isl_map_lower_bound(__isl_take isl_map *map,
5735         enum isl_dim_type type, unsigned pos, isl_int value)
5736 {
5737         return map_bound(map, type, pos, value, 0);
5738 }
5739
5740 __isl_give isl_map *isl_map_upper_bound(__isl_take isl_map *map,
5741         enum isl_dim_type type, unsigned pos, isl_int value)
5742 {
5743         return map_bound(map, type, pos, value, 1);
5744 }
5745
5746 __isl_give isl_set *isl_set_lower_bound(__isl_take isl_set *set,
5747         enum isl_dim_type type, unsigned pos, isl_int value)
5748 {
5749         return isl_map_lower_bound(set, type, pos, value);
5750 }
5751
5752 __isl_give isl_set *isl_set_upper_bound(__isl_take isl_set *set,
5753         enum isl_dim_type type, unsigned pos, isl_int value)
5754 {
5755         return isl_map_upper_bound(set, type, pos, value);
5756 }
5757
5758 /* Force the values of the variable at position "pos" of type "type" of "set"
5759  * to be no smaller than "value".
5760  */
5761 __isl_give isl_set *isl_set_lower_bound_val(__isl_take isl_set *set,
5762         enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5763 {
5764         if (!value)
5765                 goto error;
5766         if (!isl_val_is_int(value))
5767                 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5768                         "expecting integer value", goto error);
5769         set = isl_set_lower_bound(set, type, pos, value->n);
5770         isl_val_free(value);
5771         return set;
5772 error:
5773         isl_val_free(value);
5774         isl_set_free(set);
5775         return NULL;
5776 }
5777
5778 /* Force the values of the variable at position "pos" of type "type" of "set"
5779  * to be no greater than "value".
5780  */
5781 __isl_give isl_set *isl_set_upper_bound_val(__isl_take isl_set *set,
5782         enum isl_dim_type type, unsigned pos, __isl_take isl_val *value)
5783 {
5784         if (!value)
5785                 goto error;
5786         if (!isl_val_is_int(value))
5787                 isl_die(isl_set_get_ctx(set), isl_error_invalid,
5788                         "expecting integer value", goto error);
5789         set = isl_set_upper_bound(set, type, pos, value->n);
5790         isl_val_free(value);
5791         return set;
5792 error:
5793         isl_val_free(value);
5794         isl_set_free(set);
5795         return NULL;
5796 }
5797
5798 struct isl_set *isl_set_lower_bound_dim(struct isl_set *set, unsigned dim,
5799                                         isl_int value)
5800 {
5801         int i;
5802
5803         set = isl_set_cow(set);
5804         if (!set)
5805                 return NULL;
5806
5807         isl_assert(set->ctx, dim < isl_set_n_dim(set), goto error);
5808         for (i = 0; i < set->n; ++i) {
5809                 set->p[i] = isl_basic_set_lower_bound_dim(set->p[i], dim, value);
5810                 if (!set->p[i])
5811                         goto error;
5812         }
5813         return set;
5814 error:
5815         isl_set_free(set);
5816         return NULL;
5817 }
5818
5819 struct isl_map *isl_map_reverse(struct isl_map *map)
5820 {
5821         int i;
5822
5823         map = isl_map_cow(map);
5824         if (!map)
5825                 return NULL;
5826
5827         map->dim = isl_space_reverse(map->dim);
5828         if (!map->dim)
5829                 goto error;
5830         for (i = 0; i < map->n; ++i) {
5831                 map->p[i] = isl_basic_map_reverse(map->p[i]);
5832                 if (!map->p[i])
5833                         goto error;
5834         }
5835         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
5836         return map;
5837 error:
5838         isl_map_free(map);
5839         return NULL;
5840 }
5841
5842 static struct isl_map *isl_basic_map_partial_lexopt(
5843                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5844                 struct isl_set **empty, int max)
5845 {
5846         if (!bmap)
5847                 goto error;
5848         if (bmap->ctx->opt->pip == ISL_PIP_PIP)
5849                 return isl_pip_basic_map_lexopt(bmap, dom, empty, max);
5850         else
5851                 return isl_tab_basic_map_partial_lexopt(bmap, dom, empty, max);
5852 error:
5853         isl_basic_map_free(bmap);
5854         isl_basic_set_free(dom);
5855         if (empty)
5856                 *empty = NULL;
5857         return NULL;
5858 }
5859
5860 struct isl_map *isl_basic_map_partial_lexmax(
5861                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5862                 struct isl_set **empty)
5863 {
5864         return isl_basic_map_partial_lexopt(bmap, dom, empty, 1);
5865 }
5866
5867 struct isl_map *isl_basic_map_partial_lexmin(
5868                 struct isl_basic_map *bmap, struct isl_basic_set *dom,
5869                 struct isl_set **empty)
5870 {
5871         return isl_basic_map_partial_lexopt(bmap, dom, empty, 0);
5872 }
5873
5874 struct isl_set *isl_basic_set_partial_lexmin(
5875                 struct isl_basic_set *bset, struct isl_basic_set *dom,
5876                 struct isl_set **empty)
5877 {
5878         return (struct isl_set *)
5879                 isl_basic_map_partial_lexmin((struct isl_basic_map *)bset,
5880                         dom, empty);
5881 }
5882
5883 struct isl_set *isl_basic_set_partial_lexmax(
5884                 struct isl_basic_set *bset, struct isl_basic_set *dom,
5885                 struct isl_set **empty)
5886 {
5887         return (struct isl_set *)
5888                 isl_basic_map_partial_lexmax((struct isl_basic_map *)bset,
5889                         dom, empty);
5890 }
5891
5892 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmin_pw_multi_aff(
5893         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5894         __isl_give isl_set **empty)
5895 {
5896         return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 0);
5897 }
5898
5899 __isl_give isl_pw_multi_aff *isl_basic_map_partial_lexmax_pw_multi_aff(
5900         __isl_take isl_basic_map *bmap, __isl_take isl_basic_set *dom,
5901         __isl_give isl_set **empty)
5902 {
5903         return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, empty, 1);
5904 }
5905
5906 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmin_pw_multi_aff(
5907         __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5908         __isl_give isl_set **empty)
5909 {
5910         return isl_basic_map_partial_lexmin_pw_multi_aff(bset, dom, empty);
5911 }
5912
5913 __isl_give isl_pw_multi_aff *isl_basic_set_partial_lexmax_pw_multi_aff(
5914         __isl_take isl_basic_set *bset, __isl_take isl_basic_set *dom,
5915         __isl_give isl_set **empty)
5916 {
5917         return isl_basic_map_partial_lexmax_pw_multi_aff(bset, dom, empty);
5918 }
5919
5920 __isl_give isl_pw_multi_aff *isl_basic_map_lexopt_pw_multi_aff(
5921         __isl_take isl_basic_map *bmap, int max)
5922 {
5923         isl_basic_set *dom = NULL;
5924         isl_space *dom_space;
5925
5926         if (!bmap)
5927                 goto error;
5928         dom_space = isl_space_domain(isl_space_copy(bmap->dim));
5929         dom = isl_basic_set_universe(dom_space);
5930         return isl_basic_map_partial_lexopt_pw_multi_aff(bmap, dom, NULL, max);
5931 error:
5932         isl_basic_map_free(bmap);
5933         return NULL;
5934 }
5935
5936 __isl_give isl_pw_multi_aff *isl_basic_map_lexmin_pw_multi_aff(
5937         __isl_take isl_basic_map *bmap)
5938 {
5939         return isl_basic_map_lexopt_pw_multi_aff(bmap, 0);
5940 }
5941
5942 #undef TYPE
5943 #define TYPE    isl_pw_multi_aff
5944 #undef SUFFIX
5945 #define SUFFIX  _pw_multi_aff
5946 #undef EMPTY
5947 #define EMPTY   isl_pw_multi_aff_empty
5948 #undef ADD
5949 #define ADD     isl_pw_multi_aff_union_add
5950 #include "isl_map_lexopt_templ.c"
5951
5952 /* Given a map "map", compute the lexicographically minimal
5953  * (or maximal) image element for each domain element in dom,
5954  * in the form of an isl_pw_multi_aff.
5955  * Set *empty to those elements in dom that do not have an image element.
5956  *
5957  * We first compute the lexicographically minimal or maximal element
5958  * in the first basic map.  This results in a partial solution "res"
5959  * and a subset "todo" of dom that still need to be handled.
5960  * We then consider each of the remaining maps in "map" and successively
5961  * update both "res" and "todo".
5962  */
5963 static __isl_give isl_pw_multi_aff *isl_map_partial_lexopt_aligned_pw_multi_aff(
5964         __isl_take isl_map *map, __isl_take isl_set *dom,
5965         __isl_give isl_set **empty, int max)
5966 {
5967         int i;
5968         isl_pw_multi_aff *res;
5969         isl_set *todo;
5970
5971         if (!map || !dom)
5972                 goto error;
5973
5974         if (isl_map_plain_is_empty(map)) {
5975                 if (empty)
5976                         *empty = dom;
5977                 else
5978                         isl_set_free(dom);
5979                 return isl_pw_multi_aff_from_map(map);
5980         }
5981
5982         res = basic_map_partial_lexopt_pw_multi_aff(
5983                                             isl_basic_map_copy(map->p[0]),
5984                                             isl_set_copy(dom), &todo, max);
5985
5986         for (i = 1; i < map->n; ++i) {
5987                 isl_pw_multi_aff *res_i;
5988                 isl_set *todo_i;
5989
5990                 res_i = basic_map_partial_lexopt_pw_multi_aff(
5991                                             isl_basic_map_copy(map->p[i]),
5992                                             isl_set_copy(dom), &todo_i, max);
5993
5994                 if (max)
5995                         res = isl_pw_multi_aff_union_lexmax(res, res_i);
5996                 else
5997                         res = isl_pw_multi_aff_union_lexmin(res, res_i);
5998
5999                 todo = isl_set_intersect(todo, todo_i);
6000         }
6001
6002         isl_set_free(dom);
6003         isl_map_free(map);
6004
6005         if (empty)
6006                 *empty = todo;
6007         else
6008                 isl_set_free(todo);
6009
6010         return res;
6011 error:
6012         if (empty)
6013                 *empty = NULL;
6014         isl_set_free(dom);
6015         isl_map_free(map);
6016         return NULL;
6017 }
6018
6019 #undef TYPE
6020 #define TYPE    isl_map
6021 #undef SUFFIX
6022 #define SUFFIX
6023 #undef EMPTY
6024 #define EMPTY   isl_map_empty
6025 #undef ADD
6026 #define ADD     isl_map_union_disjoint
6027 #include "isl_map_lexopt_templ.c"
6028
6029 /* Given a map "map", compute the lexicographically minimal
6030  * (or maximal) image element for each domain element in dom.
6031  * Set *empty to those elements in dom that do not have an image element.
6032  *
6033  * We first compute the lexicographically minimal or maximal element
6034  * in the first basic map.  This results in a partial solution "res"
6035  * and a subset "todo" of dom that still need to be handled.
6036  * We then consider each of the remaining maps in "map" and successively
6037  * update both "res" and "todo".
6038  *
6039  * Let res^k and todo^k be the results after k steps and let i = k + 1.
6040  * Assume we are computing the lexicographical maximum.
6041  * We first compute the lexicographically maximal element in basic map i.
6042  * This results in a partial solution res_i and a subset todo_i.
6043  * Then we combine these results with those obtain for the first k basic maps
6044  * to obtain a result that is valid for the first k+1 basic maps.
6045  * In particular, the set where there is no solution is the set where
6046  * there is no solution for the first k basic maps and also no solution
6047  * for the ith basic map, i.e.,
6048  *
6049  *      todo^i = todo^k * todo_i
6050  *
6051  * On dom(res^k) * dom(res_i), we need to pick the larger of the two
6052  * solutions, arbitrarily breaking ties in favor of res^k.
6053  * That is, when res^k(a) >= res_i(a), we pick res^k and
6054  * when res^k(a) < res_i(a), we pick res_i.  (Here, ">=" and "<" denote
6055  * the lexicographic order.)
6056  * In practice, we compute
6057  *
6058  *      res^k * (res_i . "<=")
6059  *
6060  * and
6061  *
6062  *      res_i * (res^k . "<")
6063  *
6064  * Finally, we consider the symmetric difference of dom(res^k) and dom(res_i),
6065  * where only one of res^k and res_i provides a solution and we simply pick
6066  * that one, i.e.,
6067  *
6068  *      res^k * todo_i
6069  * and
6070  *      res_i * todo^k
6071  *
6072  * Note that we only compute these intersections when dom(res^k) intersects
6073  * dom(res_i).  Otherwise, the only effect of these intersections is to
6074  * potentially break up res^k and res_i into smaller pieces.
6075  * We want to avoid such splintering as much as possible.
6076  * In fact, an earlier implementation of this function would look for
6077  * better results in the domain of res^k and for extra results in todo^k,
6078  * but this would always result in a splintering according to todo^k,
6079  * even when the domain of basic map i is disjoint from the domains of
6080  * the previous basic maps.
6081  */
6082 static __isl_give isl_map *isl_map_partial_lexopt_aligned(
6083                 __isl_take isl_map *map, __isl_take isl_set *dom,
6084                 __isl_give isl_set **empty, int max)
6085 {
6086         int i;
6087         struct isl_map *res;
6088         struct isl_set *todo;
6089
6090         if (!map || !dom)
6091                 goto error;
6092
6093         if (isl_map_plain_is_empty(map)) {
6094                 if (empty)
6095                         *empty = dom;
6096                 else
6097                         isl_set_free(dom);
6098                 return map;
6099         }
6100
6101         res = basic_map_partial_lexopt(isl_basic_map_copy(map->p[0]),
6102                                         isl_set_copy(dom), &todo, max);
6103
6104         for (i = 1; i < map->n; ++i) {
6105                 isl_map *lt, *le;
6106                 isl_map *res_i;
6107                 isl_set *todo_i;
6108                 isl_space *dim = isl_space_range(isl_map_get_space(res));
6109
6110                 res_i = basic_map_partial_lexopt(isl_basic_map_copy(map->p[i]),
6111                                         isl_set_copy(dom), &todo_i, max);
6112
6113                 if (max) {
6114                         lt = isl_map_lex_lt(isl_space_copy(dim));
6115                         le = isl_map_lex_le(dim);
6116                 } else {
6117                         lt = isl_map_lex_gt(isl_space_copy(dim));
6118                         le = isl_map_lex_ge(dim);
6119                 }
6120                 lt = isl_map_apply_range(isl_map_copy(res), lt);
6121                 lt = isl_map_intersect(lt, isl_map_copy(res_i));
6122                 le = isl_map_apply_range(isl_map_copy(res_i), le);
6123                 le = isl_map_intersect(le, isl_map_copy(res));
6124
6125                 if (!isl_map_is_empty(lt) || !isl_map_is_empty(le)) {
6126                         res = isl_map_intersect_domain(res,
6127                                                         isl_set_copy(todo_i));
6128                         res_i = isl_map_intersect_domain(res_i,
6129                                                         isl_set_copy(todo));
6130                 }
6131
6132                 res = isl_map_union_disjoint(res, res_i);
6133                 res = isl_map_union_disjoint(res, lt);
6134                 res = isl_map_union_disjoint(res, le);
6135
6136                 todo = isl_set_intersect(todo, todo_i);
6137         }
6138
6139         isl_set_free(dom);
6140         isl_map_free(map);
6141
6142         if (empty)
6143                 *empty = todo;
6144         else
6145                 isl_set_free(todo);
6146
6147         return res;
6148 error:
6149         if (empty)
6150                 *empty = NULL;
6151         isl_set_free(dom);
6152         isl_map_free(map);
6153         return NULL;
6154 }
6155
6156 __isl_give isl_map *isl_map_partial_lexmax(
6157                 __isl_take isl_map *map, __isl_take isl_set *dom,
6158                 __isl_give isl_set **empty)
6159 {
6160         return isl_map_partial_lexopt(map, dom, empty, 1);
6161 }
6162
6163 __isl_give isl_map *isl_map_partial_lexmin(
6164                 __isl_take isl_map *map, __isl_take isl_set *dom,
6165                 __isl_give isl_set **empty)
6166 {
6167         return isl_map_partial_lexopt(map, dom, empty, 0);
6168 }
6169
6170 __isl_give isl_set *isl_set_partial_lexmin(
6171                 __isl_take isl_set *set, __isl_take isl_set *dom,
6172                 __isl_give isl_set **empty)
6173 {
6174         return (struct isl_set *)
6175                 isl_map_partial_lexmin((struct isl_map *)set,
6176                         dom, empty);
6177 }
6178
6179 __isl_give isl_set *isl_set_partial_lexmax(
6180                 __isl_take isl_set *set, __isl_take isl_set *dom,
6181                 __isl_give isl_set **empty)
6182 {
6183         return (struct isl_set *)
6184                 isl_map_partial_lexmax((struct isl_map *)set,
6185                         dom, empty);
6186 }
6187
6188 /* Compute the lexicographic minimum (or maximum if "max" is set)
6189  * of "bmap" over its domain.
6190  *
6191  * Since we are not interested in the part of the domain space where
6192  * there is no solution, we initialize the domain to those constraints
6193  * of "bmap" that only involve the parameters and the input dimensions.
6194  * This relieves the parametric programming engine from detecting those
6195  * inequalities and transferring them to the context.  More importantly,
6196  * it ensures that those inequalities are transferred first and not
6197  * intermixed with inequalities that actually split the domain.
6198  */
6199 __isl_give isl_map *isl_basic_map_lexopt(__isl_take isl_basic_map *bmap, int max)
6200 {
6201         int n_div;
6202         int n_out;
6203         isl_basic_map *copy;
6204         isl_basic_set *dom;
6205
6206         n_div = isl_basic_map_dim(bmap, isl_dim_div);
6207         n_out = isl_basic_map_dim(bmap, isl_dim_out);
6208         copy = isl_basic_map_copy(bmap);
6209         copy = isl_basic_map_drop_constraints_involving_dims(copy,
6210                                                         isl_dim_div, 0, n_div);
6211         copy = isl_basic_map_drop_constraints_involving_dims(copy,
6212                                                         isl_dim_out, 0, n_out);
6213         dom = isl_basic_map_domain(copy);
6214         return isl_basic_map_partial_lexopt(bmap, dom, NULL, max);
6215 }
6216
6217 __isl_give isl_map *isl_basic_map_lexmin(__isl_take isl_basic_map *bmap)
6218 {
6219         return isl_basic_map_lexopt(bmap, 0);
6220 }
6221
6222 __isl_give isl_map *isl_basic_map_lexmax(__isl_take isl_basic_map *bmap)
6223 {
6224         return isl_basic_map_lexopt(bmap, 1);
6225 }
6226
6227 __isl_give isl_set *isl_basic_set_lexmin(__isl_take isl_basic_set *bset)
6228 {
6229         return (isl_set *)isl_basic_map_lexmin((isl_basic_map *)bset);
6230 }
6231
6232 __isl_give isl_set *isl_basic_set_lexmax(__isl_take isl_basic_set *bset)
6233 {
6234         return (isl_set *)isl_basic_map_lexmax((isl_basic_map *)bset);
6235 }
6236
6237 /* Extract the first and only affine expression from list
6238  * and then add it to *pwaff with the given dom.
6239  * This domain is known to be disjoint from other domains
6240  * because of the way isl_basic_map_foreach_lexmax works.
6241  */
6242 static int update_dim_opt(__isl_take isl_basic_set *dom,
6243         __isl_take isl_aff_list *list, void *user)
6244 {
6245         isl_ctx *ctx = isl_basic_set_get_ctx(dom);
6246         isl_aff *aff;
6247         isl_pw_aff **pwaff = user;
6248         isl_pw_aff *pwaff_i;
6249
6250         if (!list)
6251                 goto error;
6252         if (isl_aff_list_n_aff(list) != 1)
6253                 isl_die(ctx, isl_error_internal,
6254                         "expecting single element list", goto error);
6255
6256         aff = isl_aff_list_get_aff(list, 0);
6257         pwaff_i = isl_pw_aff_alloc(isl_set_from_basic_set(dom), aff);
6258
6259         *pwaff = isl_pw_aff_add_disjoint(*pwaff, pwaff_i);
6260
6261         isl_aff_list_free(list);
6262
6263         return 0;
6264 error:
6265         isl_basic_set_free(dom);
6266         isl_aff_list_free(list);
6267         return -1;
6268 }
6269
6270 /* Given a basic map with one output dimension, compute the minimum or
6271  * maximum of that dimension as an isl_pw_aff.
6272  *
6273  * The isl_pw_aff is constructed by having isl_basic_map_foreach_lexopt
6274  * call update_dim_opt on each leaf of the result.
6275  */
6276 static __isl_give isl_pw_aff *basic_map_dim_opt(__isl_keep isl_basic_map *bmap,
6277         int max)
6278 {
6279         isl_space *dim = isl_basic_map_get_space(bmap);
6280         isl_pw_aff *pwaff;
6281         int r;
6282
6283         dim = isl_space_from_domain(isl_space_domain(dim));
6284         dim = isl_space_add_dims(dim, isl_dim_out, 1);
6285         pwaff = isl_pw_aff_empty(dim);
6286
6287         r = isl_basic_map_foreach_lexopt(bmap, max, &update_dim_opt, &pwaff);
6288         if (r < 0)
6289                 return isl_pw_aff_free(pwaff);
6290
6291         return pwaff;
6292 }
6293
6294 /* Compute the minimum or maximum of the given output dimension
6295  * as a function of the parameters and the input dimensions,
6296  * but independently of the other output dimensions.
6297  *
6298  * We first project out the other output dimension and then compute
6299  * the "lexicographic" maximum in each basic map, combining the results
6300  * using isl_pw_aff_union_max.
6301  */
6302 static __isl_give isl_pw_aff *map_dim_opt(__isl_take isl_map *map, int pos,
6303         int max)
6304 {
6305         int i;
6306         isl_pw_aff *pwaff;
6307         unsigned n_out;
6308
6309         n_out = isl_map_dim(map, isl_dim_out);
6310         map = isl_map_project_out(map, isl_dim_out, pos + 1, n_out - (pos + 1));
6311         map = isl_map_project_out(map, isl_dim_out, 0, pos);
6312         if (!map)
6313                 return NULL;
6314
6315         if (map->n == 0) {
6316                 isl_space *dim = isl_map_get_space(map);
6317                 dim = isl_space_domain(isl_space_from_range(dim));
6318                 isl_map_free(map);
6319                 return isl_pw_aff_empty(dim);
6320         }
6321
6322         pwaff = basic_map_dim_opt(map->p[0], max);
6323         for (i = 1; i < map->n; ++i) {
6324                 isl_pw_aff *pwaff_i;
6325
6326                 pwaff_i = basic_map_dim_opt(map->p[i], max);
6327                 pwaff = isl_pw_aff_union_opt(pwaff, pwaff_i, max);
6328         }
6329
6330         isl_map_free(map);
6331
6332         return pwaff;
6333 }
6334
6335 /* Compute the maximum of the given output dimension as a function of the
6336  * parameters and input dimensions, but independently of
6337  * the other output dimensions.
6338  */
6339 __isl_give isl_pw_aff *isl_map_dim_max(__isl_take isl_map *map, int pos)
6340 {
6341         return map_dim_opt(map, pos, 1);
6342 }
6343
6344 /* Compute the minimum or maximum of the given set dimension
6345  * as a function of the parameters,
6346  * but independently of the other set dimensions.
6347  */
6348 static __isl_give isl_pw_aff *set_dim_opt(__isl_take isl_set *set, int pos,
6349         int max)
6350 {
6351         return map_dim_opt(set, pos, max);
6352 }
6353
6354 /* Compute the maximum of the given set dimension as a function of the
6355  * parameters, but independently of the other set dimensions.
6356  */
6357 __isl_give isl_pw_aff *isl_set_dim_max(__isl_take isl_set *set, int pos)
6358 {
6359         return set_dim_opt(set, pos, 1);
6360 }
6361
6362 /* Compute the minimum of the given set dimension as a function of the
6363  * parameters, but independently of the other set dimensions.
6364  */
6365 __isl_give isl_pw_aff *isl_set_dim_min(__isl_take isl_set *set, int pos)
6366 {
6367         return set_dim_opt(set, pos, 0);
6368 }
6369
6370 /* Apply a preimage specified by "mat" on the parameters of "bset".
6371  * bset is assumed to have only parameters and divs.
6372  */
6373 static struct isl_basic_set *basic_set_parameter_preimage(
6374         struct isl_basic_set *bset, struct isl_mat *mat)
6375 {
6376         unsigned nparam;
6377
6378         if (!bset || !mat)
6379                 goto error;
6380
6381         bset->dim = isl_space_cow(bset->dim);
6382         if (!bset->dim)
6383                 goto error;
6384
6385         nparam = isl_basic_set_dim(bset, isl_dim_param);
6386
6387         isl_assert(bset->ctx, mat->n_row == 1 + nparam, goto error);
6388
6389         bset->dim->nparam = 0;
6390         bset->dim->n_out = nparam;
6391         bset = isl_basic_set_preimage(bset, mat);
6392         if (bset) {
6393                 bset->dim->nparam = bset->dim->n_out;
6394                 bset->dim->n_out = 0;
6395         }
6396         return bset;
6397 error:
6398         isl_mat_free(mat);
6399         isl_basic_set_free(bset);
6400         return NULL;
6401 }
6402
6403 /* Apply a preimage specified by "mat" on the parameters of "set".
6404  * set is assumed to have only parameters and divs.
6405  */
6406 static struct isl_set *set_parameter_preimage(
6407         struct isl_set *set, struct isl_mat *mat)
6408 {
6409         isl_space *dim = NULL;
6410         unsigned nparam;
6411
6412         if (!set || !mat)
6413                 goto error;
6414
6415         dim = isl_space_copy(set->dim);
6416         dim = isl_space_cow(dim);
6417         if (!dim)
6418                 goto error;
6419
6420         nparam = isl_set_dim(set, isl_dim_param);
6421
6422         isl_assert(set->ctx, mat->n_row == 1 + nparam, goto error);
6423
6424         dim->nparam = 0;
6425         dim->n_out = nparam;
6426         isl_set_reset_space(set, dim);
6427         set = isl_set_preimage(set, mat);
6428         if (!set)
6429                 goto error2;
6430         dim = isl_space_copy(set->dim);
6431         dim = isl_space_cow(dim);
6432         if (!dim)
6433                 goto error2;
6434         dim->nparam = dim->n_out;
6435         dim->n_out = 0;
6436         isl_set_reset_space(set, dim);
6437         return set;
6438 error:
6439         isl_space_free(dim);
6440         isl_mat_free(mat);
6441 error2:
6442         isl_set_free(set);
6443         return NULL;
6444 }
6445
6446 /* Intersect the basic set "bset" with the affine space specified by the
6447  * equalities in "eq".
6448  */
6449 static struct isl_basic_set *basic_set_append_equalities(
6450         struct isl_basic_set *bset, struct isl_mat *eq)
6451 {
6452         int i, k;
6453         unsigned len;
6454
6455         if (!bset || !eq)
6456                 goto error;
6457
6458         bset = isl_basic_set_extend_space(bset, isl_space_copy(bset->dim), 0,
6459                                         eq->n_row, 0);
6460         if (!bset)
6461                 goto error;
6462
6463         len = 1 + isl_space_dim(bset->dim, isl_dim_all) + bset->extra;
6464         for (i = 0; i < eq->n_row; ++i) {
6465                 k = isl_basic_set_alloc_equality(bset);
6466                 if (k < 0)
6467                         goto error;
6468                 isl_seq_cpy(bset->eq[k], eq->row[i], eq->n_col);
6469                 isl_seq_clr(bset->eq[k] + eq->n_col, len - eq->n_col);
6470         }
6471         isl_mat_free(eq);
6472
6473         bset = isl_basic_set_gauss(bset, NULL);
6474         bset = isl_basic_set_finalize(bset);
6475
6476         return bset;
6477 error:
6478         isl_mat_free(eq);
6479         isl_basic_set_free(bset);
6480         return NULL;
6481 }
6482
6483 /* Intersect the set "set" with the affine space specified by the
6484  * equalities in "eq".
6485  */
6486 static struct isl_set *set_append_equalities(struct isl_set *set,
6487         struct isl_mat *eq)
6488 {
6489         int i;
6490
6491         if (!set || !eq)
6492                 goto error;
6493
6494         for (i = 0; i < set->n; ++i) {
6495                 set->p[i] = basic_set_append_equalities(set->p[i],
6496                                         isl_mat_copy(eq));
6497                 if (!set->p[i])
6498                         goto error;
6499         }
6500         isl_mat_free(eq);
6501         return set;
6502 error:
6503         isl_mat_free(eq);
6504         isl_set_free(set);
6505         return NULL;
6506 }
6507
6508 /* Given a basic set "bset" that only involves parameters and existentially
6509  * quantified variables, return the index of the first equality
6510  * that only involves parameters.  If there is no such equality then
6511  * return bset->n_eq.
6512  *
6513  * This function assumes that isl_basic_set_gauss has been called on "bset".
6514  */
6515 static int first_parameter_equality(__isl_keep isl_basic_set *bset)
6516 {
6517         int i, j;
6518         unsigned nparam, n_div;
6519
6520         if (!bset)
6521                 return -1;
6522
6523         nparam = isl_basic_set_dim(bset, isl_dim_param);
6524         n_div = isl_basic_set_dim(bset, isl_dim_div);
6525
6526         for (i = 0, j = n_div - 1; i < bset->n_eq && j >= 0; --j) {
6527                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + j]))
6528                         ++i;
6529         }
6530
6531         return i;
6532 }
6533
6534 /* Compute an explicit representation for the existentially quantified
6535  * variables in "bset" by computing the "minimal value" of the set
6536  * variables.  Since there are no set variables, the computation of
6537  * the minimal value essentially computes an explicit representation
6538  * of the non-empty part(s) of "bset".
6539  *
6540  * The input only involves parameters and existentially quantified variables.
6541  * All equalities among parameters have been removed.
6542  *
6543  * Since the existentially quantified variables in the result are in general
6544  * going to be different from those in the input, we first replace
6545  * them by the minimal number of variables based on their equalities.
6546  * This should simplify the parametric integer programming.
6547  */
6548 static __isl_give isl_set *base_compute_divs(__isl_take isl_basic_set *bset)
6549 {
6550         isl_morph *morph1, *morph2;
6551         isl_set *set;
6552         unsigned n;
6553
6554         if (!bset)
6555                 return NULL;
6556         if (bset->n_eq == 0)
6557                 return isl_basic_set_lexmin(bset);
6558
6559         morph1 = isl_basic_set_parameter_compression(bset);
6560         bset = isl_morph_basic_set(isl_morph_copy(morph1), bset);
6561         bset = isl_basic_set_lift(bset);
6562         morph2 = isl_basic_set_variable_compression(bset, isl_dim_set);
6563         bset = isl_morph_basic_set(morph2, bset);
6564         n = isl_basic_set_dim(bset, isl_dim_set);
6565         bset = isl_basic_set_project_out(bset, isl_dim_set, 0, n);
6566
6567         set = isl_basic_set_lexmin(bset);
6568
6569         set = isl_morph_set(isl_morph_inverse(morph1), set);
6570
6571         return set;
6572 }
6573
6574 /* Project the given basic set onto its parameter domain, possibly introducing
6575  * new, explicit, existential variables in the constraints.
6576  * The input has parameters and (possibly implicit) existential variables.
6577  * The output has the same parameters, but only
6578  * explicit existentially quantified variables.
6579  *
6580  * The actual projection is performed by pip, but pip doesn't seem
6581  * to like equalities very much, so we first remove the equalities
6582  * among the parameters by performing a variable compression on
6583  * the parameters.  Afterward, an inverse transformation is performed
6584  * and the equalities among the parameters are inserted back in.
6585  *
6586  * The variable compression on the parameters may uncover additional
6587  * equalities that were only implicit before.  We therefore check
6588  * if there are any new parameter equalities in the result and
6589  * if so recurse.  The removal of parameter equalities is required
6590  * for the parameter compression performed by base_compute_divs.
6591  */
6592 static struct isl_set *parameter_compute_divs(struct isl_basic_set *bset)
6593 {
6594         int i;
6595         struct isl_mat *eq;
6596         struct isl_mat *T, *T2;
6597         struct isl_set *set;
6598         unsigned nparam;
6599
6600         bset = isl_basic_set_cow(bset);
6601         if (!bset)
6602                 return NULL;
6603
6604         if (bset->n_eq == 0)
6605                 return base_compute_divs(bset);
6606
6607         bset = isl_basic_set_gauss(bset, NULL);
6608         if (!bset)
6609                 return NULL;
6610         if (isl_basic_set_plain_is_empty(bset))
6611                 return isl_set_from_basic_set(bset);
6612
6613         i = first_parameter_equality(bset);
6614         if (i == bset->n_eq)
6615                 return base_compute_divs(bset);
6616
6617         nparam = isl_basic_set_dim(bset, isl_dim_param);
6618         eq = isl_mat_sub_alloc6(bset->ctx, bset->eq, i, bset->n_eq - i,
6619                 0, 1 + nparam);
6620         eq = isl_mat_cow(eq);
6621         T = isl_mat_variable_compression(isl_mat_copy(eq), &T2);
6622         if (T && T->n_col == 0) {
6623                 isl_mat_free(T);
6624                 isl_mat_free(T2);
6625                 isl_mat_free(eq);
6626                 bset = isl_basic_set_set_to_empty(bset);
6627                 return isl_set_from_basic_set(bset);
6628         }
6629         bset = basic_set_parameter_preimage(bset, T);
6630
6631         i = first_parameter_equality(bset);
6632         if (!bset)
6633                 set = NULL;
6634         else if (i == bset->n_eq)
6635                 set = base_compute_divs(bset);
6636         else
6637                 set = parameter_compute_divs(bset);
6638         set = set_parameter_preimage(set, T2);
6639         set = set_append_equalities(set, eq);
6640         return set;
6641 }
6642
6643 /* Insert the divs from "ls" before those of "bmap".
6644  *
6645  * The number of columns is not changed, which means that the last
6646  * dimensions of "bmap" are being reintepreted as the divs from "ls".
6647  * The caller is responsible for removing the same number of dimensions
6648  * from the space of "bmap".
6649  */
6650 static __isl_give isl_basic_map *insert_divs_from_local_space(
6651         __isl_take isl_basic_map *bmap, __isl_keep isl_local_space *ls)
6652 {
6653         int i;
6654         int n_div;
6655         int old_n_div;
6656
6657         n_div = isl_local_space_dim(ls, isl_dim_div);
6658         if (n_div == 0)
6659                 return bmap;
6660
6661         old_n_div = bmap->n_div;
6662         bmap = insert_div_rows(bmap, n_div);
6663         if (!bmap)
6664                 return NULL;
6665
6666         for (i = 0; i < n_div; ++i) {
6667                 isl_seq_cpy(bmap->div[i], ls->div->row[i], ls->div->n_col);
6668                 isl_seq_clr(bmap->div[i] + ls->div->n_col, old_n_div);
6669         }
6670
6671         return bmap;
6672 }
6673
6674 /* Replace the space of "bmap" by the space and divs of "ls".
6675  *
6676  * If "ls" has any divs, then we simplify the result since we may
6677  * have discovered some additional equalities that could simplify
6678  * the div expressions.
6679  */
6680 static __isl_give isl_basic_map *basic_replace_space_by_local_space(
6681         __isl_take isl_basic_map *bmap, __isl_take isl_local_space *ls)
6682 {
6683         int n_div;
6684
6685         bmap = isl_basic_map_cow(bmap);
6686         if (!bmap || !ls)
6687                 goto error;
6688
6689         n_div = isl_local_space_dim(ls, isl_dim_div);
6690         bmap = insert_divs_from_local_space(bmap, ls);
6691         if (!bmap)
6692                 goto error;
6693
6694         isl_space_free(bmap->dim);
6695         bmap->dim = isl_local_space_get_space(ls);
6696         if (!bmap->dim)
6697                 goto error;
6698
6699         isl_local_space_free(ls);
6700         if (n_div > 0)
6701                 bmap = isl_basic_map_simplify(bmap);
6702         bmap = isl_basic_map_finalize(bmap);
6703         return bmap;
6704 error:
6705         isl_basic_map_free(bmap);
6706         isl_local_space_free(ls);
6707         return NULL;
6708 }
6709
6710 /* Replace the space of "map" by the space and divs of "ls".
6711  */
6712 static __isl_give isl_map *replace_space_by_local_space(__isl_take isl_map *map,
6713         __isl_take isl_local_space *ls)
6714 {
6715         int i;
6716
6717         map = isl_map_cow(map);
6718         if (!map || !ls)
6719                 goto error;
6720
6721         for (i = 0; i < map->n; ++i) {
6722                 map->p[i] = basic_replace_space_by_local_space(map->p[i],
6723                                                     isl_local_space_copy(ls));
6724                 if (!map->p[i])
6725                         goto error;
6726         }
6727         isl_space_free(map->dim);
6728         map->dim = isl_local_space_get_space(ls);
6729         if (!map->dim)
6730                 goto error;
6731
6732         isl_local_space_free(ls);
6733         return map;
6734 error:
6735         isl_local_space_free(ls);
6736         isl_map_free(map);
6737         return NULL;
6738 }
6739
6740 /* Compute an explicit representation for the existentially
6741  * quantified variables for which do not know any explicit representation yet.
6742  *
6743  * We first sort the existentially quantified variables so that the
6744  * existentially quantified variables for which we already have an explicit
6745  * representation are placed before those for which we do not.
6746  * The input dimensions, the output dimensions and the existentially
6747  * quantified variables for which we already have an explicit
6748  * representation are then turned into parameters.
6749  * compute_divs returns a map with the same parameters and
6750  * no input or output dimensions and the dimension specification
6751  * is reset to that of the input, including the existentially quantified
6752  * variables for which we already had an explicit representation.
6753  */
6754 static struct isl_map *compute_divs(struct isl_basic_map *bmap)
6755 {
6756         struct isl_basic_set *bset;
6757         struct isl_set *set;
6758         struct isl_map *map;
6759         isl_space *dim;
6760         isl_local_space *ls;
6761         unsigned         nparam;
6762         unsigned         n_in;
6763         unsigned         n_out;
6764         unsigned n_known;
6765         int i;
6766
6767         bmap = isl_basic_map_sort_divs(bmap);
6768         bmap = isl_basic_map_cow(bmap);
6769         if (!bmap)
6770                 return NULL;
6771
6772         for (n_known = 0; n_known < bmap->n_div; ++n_known)
6773                 if (isl_int_is_zero(bmap->div[n_known][0]))
6774                         break;
6775
6776         nparam = isl_basic_map_dim(bmap, isl_dim_param);
6777         n_in = isl_basic_map_dim(bmap, isl_dim_in);
6778         n_out = isl_basic_map_dim(bmap, isl_dim_out);
6779         dim = isl_space_set_alloc(bmap->ctx,
6780                                     nparam + n_in + n_out + n_known, 0);
6781         if (!dim)
6782                 goto error;
6783
6784         ls = isl_basic_map_get_local_space(bmap);
6785         ls = isl_local_space_drop_dims(ls, isl_dim_div,
6786                                         n_known, bmap->n_div - n_known);
6787         if (n_known > 0) {
6788                 for (i = n_known; i < bmap->n_div; ++i)
6789                         swap_div(bmap, i - n_known, i);
6790                 bmap->n_div -= n_known;
6791                 bmap->extra -= n_known;
6792         }
6793         bmap = isl_basic_map_reset_space(bmap, dim);
6794         bset = (struct isl_basic_set *)bmap;
6795
6796         set = parameter_compute_divs(bset);
6797         map = (struct isl_map *)set;
6798         map = replace_space_by_local_space(map, ls);
6799
6800         return map;
6801 error:
6802         isl_basic_map_free(bmap);
6803         return NULL;
6804 }
6805
6806 int isl_basic_map_divs_known(__isl_keep isl_basic_map *bmap)
6807 {
6808         int i;
6809         unsigned off;
6810
6811         if (!bmap)
6812                 return -1;
6813
6814         off = isl_space_dim(bmap->dim, isl_dim_all);
6815         for (i = 0; i < bmap->n_div; ++i) {
6816                 if (isl_int_is_zero(bmap->div[i][0]))
6817                         return 0;
6818                 isl_assert(bmap->ctx, isl_int_is_zero(bmap->div[i][1+1+off+i]),
6819                                 return -1);
6820         }
6821         return 1;
6822 }
6823
6824 static int map_divs_known(__isl_keep isl_map *map)
6825 {
6826         int i;
6827
6828         if (!map)
6829                 return -1;
6830
6831         for (i = 0; i < map->n; ++i) {
6832                 int known = isl_basic_map_divs_known(map->p[i]);
6833                 if (known <= 0)
6834                         return known;
6835         }
6836
6837         return 1;
6838 }
6839
6840 /* If bmap contains any unknown divs, then compute explicit
6841  * expressions for them.  However, this computation may be
6842  * quite expensive, so first try to remove divs that aren't
6843  * strictly needed.
6844  */
6845 struct isl_map *isl_basic_map_compute_divs(struct isl_basic_map *bmap)
6846 {
6847         int known;
6848         struct isl_map *map;
6849
6850         known = isl_basic_map_divs_known(bmap);
6851         if (known < 0)
6852                 goto error;
6853         if (known)
6854                 return isl_map_from_basic_map(bmap);
6855
6856         bmap = isl_basic_map_drop_redundant_divs(bmap);
6857
6858         known = isl_basic_map_divs_known(bmap);
6859         if (known < 0)
6860                 goto error;
6861         if (known)
6862                 return isl_map_from_basic_map(bmap);
6863
6864         map = compute_divs(bmap);
6865         return map;
6866 error:
6867         isl_basic_map_free(bmap);
6868         return NULL;
6869 }
6870
6871 struct isl_map *isl_map_compute_divs(struct isl_map *map)
6872 {
6873         int i;
6874         int known;
6875         struct isl_map *res;
6876
6877         if (!map)
6878                 return NULL;
6879         if (map->n == 0)
6880                 return map;
6881
6882         known = map_divs_known(map);
6883         if (known < 0) {
6884                 isl_map_free(map);
6885                 return NULL;
6886         }
6887         if (known)
6888                 return map;
6889
6890         res = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[0]));
6891         for (i = 1 ; i < map->n; ++i) {
6892                 struct isl_map *r2;
6893                 r2 = isl_basic_map_compute_divs(isl_basic_map_copy(map->p[i]));
6894                 if (ISL_F_ISSET(map, ISL_MAP_DISJOINT))
6895                         res = isl_map_union_disjoint(res, r2);
6896                 else
6897                         res = isl_map_union(res, r2);
6898         }
6899         isl_map_free(map);
6900
6901         return res;
6902 }
6903
6904 struct isl_set *isl_basic_set_compute_divs(struct isl_basic_set *bset)
6905 {
6906         return (struct isl_set *)
6907                 isl_basic_map_compute_divs((struct isl_basic_map *)bset);
6908 }
6909
6910 struct isl_set *isl_set_compute_divs(struct isl_set *set)
6911 {
6912         return (struct isl_set *)
6913                 isl_map_compute_divs((struct isl_map *)set);
6914 }
6915
6916 struct isl_set *isl_map_domain(struct isl_map *map)
6917 {
6918         int i;
6919         struct isl_set *set;
6920
6921         if (!map)
6922                 goto error;
6923
6924         map = isl_map_cow(map);
6925         if (!map)
6926                 return NULL;
6927
6928         set = (struct isl_set *)map;
6929         set->dim = isl_space_domain(set->dim);
6930         if (!set->dim)
6931                 goto error;
6932         for (i = 0; i < map->n; ++i) {
6933                 set->p[i] = isl_basic_map_domain(map->p[i]);
6934                 if (!set->p[i])
6935                         goto error;
6936         }
6937         ISL_F_CLR(set, ISL_MAP_DISJOINT);
6938         ISL_F_CLR(set, ISL_SET_NORMALIZED);
6939         return set;
6940 error:
6941         isl_map_free(map);
6942         return NULL;
6943 }
6944
6945 /* Return the union of "map1" and "map2", where we assume for now that
6946  * "map1" and "map2" are disjoint.  Note that the basic maps inside
6947  * "map1" or "map2" may not be disjoint from each other.
6948  * Also note that this function is also called from isl_map_union,
6949  * which takes care of handling the situation where "map1" and "map2"
6950  * may not be disjoint.
6951  *
6952  * If one of the inputs is empty, we can simply return the other input.
6953  * Similarly, if one of the inputs is universal, then it is equal to the union.
6954  */
6955 static __isl_give isl_map *map_union_disjoint(__isl_take isl_map *map1,
6956         __isl_take isl_map *map2)
6957 {
6958         int i;
6959         unsigned flags = 0;
6960         struct isl_map *map = NULL;
6961         int is_universe;
6962
6963         if (!map1 || !map2)
6964                 goto error;
6965
6966         if (map1->n == 0) {
6967                 isl_map_free(map1);
6968                 return map2;
6969         }
6970         if (map2->n == 0) {
6971                 isl_map_free(map2);
6972                 return map1;
6973         }
6974
6975         is_universe = isl_map_plain_is_universe(map1);
6976         if (is_universe < 0)
6977                 goto error;
6978         if (is_universe) {
6979                 isl_map_free(map2);
6980                 return map1;
6981         }
6982
6983         is_universe = isl_map_plain_is_universe(map2);
6984         if (is_universe < 0)
6985                 goto error;
6986         if (is_universe) {
6987                 isl_map_free(map1);
6988                 return map2;
6989         }
6990
6991         isl_assert(map1->ctx, isl_space_is_equal(map1->dim, map2->dim), goto error);
6992
6993         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
6994             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
6995                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
6996
6997         map = isl_map_alloc_space(isl_space_copy(map1->dim),
6998                                 map1->n + map2->n, flags);
6999         if (!map)
7000                 goto error;
7001         for (i = 0; i < map1->n; ++i) {
7002                 map = isl_map_add_basic_map(map,
7003                                   isl_basic_map_copy(map1->p[i]));
7004                 if (!map)
7005                         goto error;
7006         }
7007         for (i = 0; i < map2->n; ++i) {
7008                 map = isl_map_add_basic_map(map,
7009                                   isl_basic_map_copy(map2->p[i]));
7010                 if (!map)
7011                         goto error;
7012         }
7013         isl_map_free(map1);
7014         isl_map_free(map2);
7015         return map;
7016 error:
7017         isl_map_free(map);
7018         isl_map_free(map1);
7019         isl_map_free(map2);
7020         return NULL;
7021 }
7022
7023 __isl_give isl_map *isl_map_union_disjoint(__isl_take isl_map *map1,
7024         __isl_take isl_map *map2)
7025 {
7026         return isl_map_align_params_map_map_and(map1, map2, &map_union_disjoint);
7027 }
7028
7029 struct isl_map *isl_map_union(struct isl_map *map1, struct isl_map *map2)
7030 {
7031         map1 = isl_map_union_disjoint(map1, map2);
7032         if (!map1)
7033                 return NULL;
7034         if (map1->n > 1)
7035                 ISL_F_CLR(map1, ISL_MAP_DISJOINT);
7036         return map1;
7037 }
7038
7039 struct isl_set *isl_set_union_disjoint(
7040                         struct isl_set *set1, struct isl_set *set2)
7041 {
7042         return (struct isl_set *)
7043                 isl_map_union_disjoint(
7044                         (struct isl_map *)set1, (struct isl_map *)set2);
7045 }
7046
7047 struct isl_set *isl_set_union(struct isl_set *set1, struct isl_set *set2)
7048 {
7049         return (struct isl_set *)
7050                 isl_map_union((struct isl_map *)set1, (struct isl_map *)set2);
7051 }
7052
7053 /* Apply "fn" to pairs of elements from "map" and "set" and collect
7054  * the results.
7055  *
7056  * "map" and "set" are assumed to be compatible and non-NULL.
7057  */
7058 static __isl_give isl_map *map_intersect_set(__isl_take isl_map *map,
7059         __isl_take isl_set *set,
7060         __isl_give isl_basic_map *fn(__isl_take isl_basic_map *bmap,
7061                 __isl_take isl_basic_set *bset))
7062 {
7063         unsigned flags = 0;
7064         struct isl_map *result;
7065         int i, j;
7066
7067         if (isl_set_plain_is_universe(set)) {
7068                 isl_set_free(set);
7069                 return map;
7070         }
7071
7072         if (ISL_F_ISSET(map, ISL_MAP_DISJOINT) &&
7073             ISL_F_ISSET(set, ISL_MAP_DISJOINT))
7074                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
7075
7076         result = isl_map_alloc_space(isl_space_copy(map->dim),
7077                                         map->n * set->n, flags);
7078         for (i = 0; result && i < map->n; ++i)
7079                 for (j = 0; j < set->n; ++j) {
7080                         result = isl_map_add_basic_map(result,
7081                                         fn(isl_basic_map_copy(map->p[i]),
7082                                             isl_basic_set_copy(set->p[j])));
7083                         if (!result)
7084                                 break;
7085                 }
7086
7087         isl_map_free(map);
7088         isl_set_free(set);
7089         return result;
7090 }
7091
7092 static __isl_give isl_map *map_intersect_range(__isl_take isl_map *map,
7093         __isl_take isl_set *set)
7094 {
7095         if (!map || !set)
7096                 goto error;
7097
7098         if (!isl_map_compatible_range(map, set))
7099                 isl_die(set->ctx, isl_error_invalid,
7100                         "incompatible spaces", goto error);
7101
7102         return map_intersect_set(map, set, &isl_basic_map_intersect_range);
7103 error:
7104         isl_map_free(map);
7105         isl_set_free(set);
7106         return NULL;
7107 }
7108
7109 __isl_give isl_map *isl_map_intersect_range(__isl_take isl_map *map,
7110         __isl_take isl_set *set)
7111 {
7112         return isl_map_align_params_map_map_and(map, set, &map_intersect_range);
7113 }
7114
7115 static __isl_give isl_map *map_intersect_domain(__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_domain(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_domain);
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_domain(__isl_take isl_map *map,
7133         __isl_take isl_set *set)
7134 {
7135         return isl_map_align_params_map_map_and(map, set,
7136                                                 &map_intersect_domain);
7137 }
7138
7139 static __isl_give isl_map *map_apply_domain(__isl_take isl_map *map1,
7140         __isl_take isl_map *map2)
7141 {
7142         if (!map1 || !map2)
7143                 goto error;
7144         map1 = isl_map_reverse(map1);
7145         map1 = isl_map_apply_range(map1, map2);
7146         return isl_map_reverse(map1);
7147 error:
7148         isl_map_free(map1);
7149         isl_map_free(map2);
7150         return NULL;
7151 }
7152
7153 __isl_give isl_map *isl_map_apply_domain(__isl_take isl_map *map1,
7154         __isl_take isl_map *map2)
7155 {
7156         return isl_map_align_params_map_map_and(map1, map2, &map_apply_domain);
7157 }
7158
7159 static __isl_give isl_map *map_apply_range(__isl_take isl_map *map1,
7160         __isl_take isl_map *map2)
7161 {
7162         isl_space *dim_result;
7163         struct isl_map *result;
7164         int i, j;
7165
7166         if (!map1 || !map2)
7167                 goto error;
7168
7169         dim_result = isl_space_join(isl_space_copy(map1->dim),
7170                                   isl_space_copy(map2->dim));
7171
7172         result = isl_map_alloc_space(dim_result, map1->n * map2->n, 0);
7173         if (!result)
7174                 goto error;
7175         for (i = 0; i < map1->n; ++i)
7176                 for (j = 0; j < map2->n; ++j) {
7177                         result = isl_map_add_basic_map(result,
7178                             isl_basic_map_apply_range(
7179                                 isl_basic_map_copy(map1->p[i]),
7180                                 isl_basic_map_copy(map2->p[j])));
7181                         if (!result)
7182                                 goto error;
7183                 }
7184         isl_map_free(map1);
7185         isl_map_free(map2);
7186         if (result && result->n <= 1)
7187                 ISL_F_SET(result, ISL_MAP_DISJOINT);
7188         return result;
7189 error:
7190         isl_map_free(map1);
7191         isl_map_free(map2);
7192         return NULL;
7193 }
7194
7195 __isl_give isl_map *isl_map_apply_range(__isl_take isl_map *map1,
7196         __isl_take isl_map *map2)
7197 {
7198         return isl_map_align_params_map_map_and(map1, map2, &map_apply_range);
7199 }
7200
7201 /*
7202  * returns range - domain
7203  */
7204 struct isl_basic_set *isl_basic_map_deltas(struct isl_basic_map *bmap)
7205 {
7206         isl_space *dims, *target_dim;
7207         struct isl_basic_set *bset;
7208         unsigned dim;
7209         unsigned nparam;
7210         int i;
7211
7212         if (!bmap)
7213                 goto error;
7214         isl_assert(bmap->ctx, isl_space_tuple_match(bmap->dim, isl_dim_in,
7215                                                   bmap->dim, isl_dim_out),
7216                    goto error);
7217         target_dim = isl_space_domain(isl_basic_map_get_space(bmap));
7218         dim = isl_basic_map_n_in(bmap);
7219         nparam = isl_basic_map_n_param(bmap);
7220         bset = isl_basic_set_from_basic_map(bmap);
7221         bset = isl_basic_set_cow(bset);
7222         dims = isl_basic_set_get_space(bset);
7223         dims = isl_space_add_dims(dims, isl_dim_set, dim);
7224         bset = isl_basic_set_extend_space(bset, dims, 0, dim, 0);
7225         bset = isl_basic_set_swap_vars(bset, 2*dim);
7226         for (i = 0; i < dim; ++i) {
7227                 int j = isl_basic_map_alloc_equality(
7228                                             (struct isl_basic_map *)bset);
7229                 if (j < 0) {
7230                         bset = isl_basic_set_free(bset);
7231                         break;
7232                 }
7233                 isl_seq_clr(bset->eq[j], 1 + isl_basic_set_total_dim(bset));
7234                 isl_int_set_si(bset->eq[j][1+nparam+i], 1);
7235                 isl_int_set_si(bset->eq[j][1+nparam+dim+i], 1);
7236                 isl_int_set_si(bset->eq[j][1+nparam+2*dim+i], -1);
7237         }
7238         bset = isl_basic_set_project_out(bset, isl_dim_set, dim, 2*dim);
7239         bset = isl_basic_set_reset_space(bset, target_dim);
7240         return bset;
7241 error:
7242         isl_basic_map_free(bmap);
7243         return NULL;
7244 }
7245
7246 /*
7247  * returns range - domain
7248  */
7249 __isl_give isl_set *isl_map_deltas(__isl_take isl_map *map)
7250 {
7251         int i;
7252         isl_space *dim;
7253         struct isl_set *result;
7254
7255         if (!map)
7256                 return NULL;
7257
7258         isl_assert(map->ctx, isl_space_tuple_match(map->dim, isl_dim_in,
7259                                                  map->dim, isl_dim_out),
7260                    goto error);
7261         dim = isl_map_get_space(map);
7262         dim = isl_space_domain(dim);
7263         result = isl_set_alloc_space(dim, map->n, 0);
7264         if (!result)
7265                 goto error;
7266         for (i = 0; i < map->n; ++i)
7267                 result = isl_set_add_basic_set(result,
7268                           isl_basic_map_deltas(isl_basic_map_copy(map->p[i])));
7269         isl_map_free(map);
7270         return result;
7271 error:
7272         isl_map_free(map);
7273         return NULL;
7274 }
7275
7276 /*
7277  * returns [domain -> range] -> range - domain
7278  */
7279 __isl_give isl_basic_map *isl_basic_map_deltas_map(
7280         __isl_take isl_basic_map *bmap)
7281 {
7282         int i, k;
7283         isl_space *dim;
7284         isl_basic_map *domain;
7285         int nparam, n;
7286         unsigned total;
7287
7288         if (!isl_space_tuple_match(bmap->dim, isl_dim_in, bmap->dim, isl_dim_out))
7289                 isl_die(bmap->ctx, isl_error_invalid,
7290                         "domain and range don't match", goto error);
7291
7292         nparam = isl_basic_map_dim(bmap, isl_dim_param);
7293         n = isl_basic_map_dim(bmap, isl_dim_in);
7294
7295         dim = isl_space_from_range(isl_space_domain(isl_basic_map_get_space(bmap)));
7296         domain = isl_basic_map_universe(dim);
7297
7298         bmap = isl_basic_map_from_domain(isl_basic_map_wrap(bmap));
7299         bmap = isl_basic_map_apply_range(bmap, domain);
7300         bmap = isl_basic_map_extend_constraints(bmap, n, 0);
7301
7302         total = isl_basic_map_total_dim(bmap);
7303
7304         for (i = 0; i < n; ++i) {
7305                 k = isl_basic_map_alloc_equality(bmap);
7306                 if (k < 0)
7307                         goto error;
7308                 isl_seq_clr(bmap->eq[k], 1 + total);
7309                 isl_int_set_si(bmap->eq[k][1 + nparam + i], 1);
7310                 isl_int_set_si(bmap->eq[k][1 + nparam + n + i], -1);
7311                 isl_int_set_si(bmap->eq[k][1 + nparam + n + n + i], 1);
7312         }
7313
7314         bmap = isl_basic_map_gauss(bmap, NULL);
7315         return isl_basic_map_finalize(bmap);
7316 error:
7317         isl_basic_map_free(bmap);
7318         return NULL;
7319 }
7320
7321 /*
7322  * returns [domain -> range] -> range - domain
7323  */
7324 __isl_give isl_map *isl_map_deltas_map(__isl_take isl_map *map)
7325 {
7326         int i;
7327         isl_space *domain_dim;
7328
7329         if (!map)
7330                 return NULL;
7331
7332         if (!isl_space_tuple_match(map->dim, isl_dim_in, map->dim, isl_dim_out))
7333                 isl_die(map->ctx, isl_error_invalid,
7334                         "domain and range don't match", goto error);
7335
7336         map = isl_map_cow(map);
7337         if (!map)
7338                 return NULL;
7339
7340         domain_dim = isl_space_from_range(isl_space_domain(isl_map_get_space(map)));
7341         map->dim = isl_space_from_domain(isl_space_wrap(map->dim));
7342         map->dim = isl_space_join(map->dim, domain_dim);
7343         if (!map->dim)
7344                 goto error;
7345         for (i = 0; i < map->n; ++i) {
7346                 map->p[i] = isl_basic_map_deltas_map(map->p[i]);
7347                 if (!map->p[i])
7348                         goto error;
7349         }
7350         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
7351         return map;
7352 error:
7353         isl_map_free(map);
7354         return NULL;
7355 }
7356
7357 static __isl_give isl_basic_map *basic_map_identity(__isl_take isl_space *dims)
7358 {
7359         struct isl_basic_map *bmap;
7360         unsigned nparam;
7361         unsigned dim;
7362         int i;
7363
7364         if (!dims)
7365                 return NULL;
7366
7367         nparam = dims->nparam;
7368         dim = dims->n_out;
7369         bmap = isl_basic_map_alloc_space(dims, 0, dim, 0);
7370         if (!bmap)
7371                 goto error;
7372
7373         for (i = 0; i < dim; ++i) {
7374                 int j = isl_basic_map_alloc_equality(bmap);
7375                 if (j < 0)
7376                         goto error;
7377                 isl_seq_clr(bmap->eq[j], 1 + isl_basic_map_total_dim(bmap));
7378                 isl_int_set_si(bmap->eq[j][1+nparam+i], 1);
7379                 isl_int_set_si(bmap->eq[j][1+nparam+dim+i], -1);
7380         }
7381         return isl_basic_map_finalize(bmap);
7382 error:
7383         isl_basic_map_free(bmap);
7384         return NULL;
7385 }
7386
7387 __isl_give isl_basic_map *isl_basic_map_identity(__isl_take isl_space *dim)
7388 {
7389         if (!dim)
7390                 return NULL;
7391         if (dim->n_in != dim->n_out)
7392                 isl_die(dim->ctx, isl_error_invalid,
7393                         "number of input and output dimensions needs to be "
7394                         "the same", goto error);
7395         return basic_map_identity(dim);
7396 error:
7397         isl_space_free(dim);
7398         return NULL;
7399 }
7400
7401 struct isl_basic_map *isl_basic_map_identity_like(struct isl_basic_map *model)
7402 {
7403         if (!model || !model->dim)
7404                 return NULL;
7405         return isl_basic_map_identity(isl_space_copy(model->dim));
7406 }
7407
7408 __isl_give isl_map *isl_map_identity(__isl_take isl_space *dim)
7409 {
7410         return isl_map_from_basic_map(isl_basic_map_identity(dim));
7411 }
7412
7413 struct isl_map *isl_map_identity_like(struct isl_map *model)
7414 {
7415         if (!model || !model->dim)
7416                 return NULL;
7417         return isl_map_identity(isl_space_copy(model->dim));
7418 }
7419
7420 struct isl_map *isl_map_identity_like_basic_map(struct isl_basic_map *model)
7421 {
7422         if (!model || !model->dim)
7423                 return NULL;
7424         return isl_map_identity(isl_space_copy(model->dim));
7425 }
7426
7427 __isl_give isl_map *isl_set_identity(__isl_take isl_set *set)
7428 {
7429         isl_space *dim = isl_set_get_space(set);
7430         isl_map *id;
7431         id = isl_map_identity(isl_space_map_from_set(dim));
7432         return isl_map_intersect_range(id, set);
7433 }
7434
7435 /* Construct a basic set with all set dimensions having only non-negative
7436  * values.
7437  */
7438 __isl_give isl_basic_set *isl_basic_set_positive_orthant(
7439         __isl_take isl_space *space)
7440 {
7441         int i;
7442         unsigned nparam;
7443         unsigned dim;
7444         struct isl_basic_set *bset;
7445
7446         if (!space)
7447                 return NULL;
7448         nparam = space->nparam;
7449         dim = space->n_out;
7450         bset = isl_basic_set_alloc_space(space, 0, 0, dim);
7451         if (!bset)
7452                 return NULL;
7453         for (i = 0; i < dim; ++i) {
7454                 int k = isl_basic_set_alloc_inequality(bset);
7455                 if (k < 0)
7456                         goto error;
7457                 isl_seq_clr(bset->ineq[k], 1 + isl_basic_set_total_dim(bset));
7458                 isl_int_set_si(bset->ineq[k][1 + nparam + i], 1);
7459         }
7460         return bset;
7461 error:
7462         isl_basic_set_free(bset);
7463         return NULL;
7464 }
7465
7466 /* Construct the half-space x_pos >= 0.
7467  */
7468 static __isl_give isl_basic_set *nonneg_halfspace(__isl_take isl_space *dim,
7469         int pos)
7470 {
7471         int k;
7472         isl_basic_set *nonneg;
7473
7474         nonneg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7475         k = isl_basic_set_alloc_inequality(nonneg);
7476         if (k < 0)
7477                 goto error;
7478         isl_seq_clr(nonneg->ineq[k], 1 + isl_basic_set_total_dim(nonneg));
7479         isl_int_set_si(nonneg->ineq[k][pos], 1);
7480
7481         return isl_basic_set_finalize(nonneg);
7482 error:
7483         isl_basic_set_free(nonneg);
7484         return NULL;
7485 }
7486
7487 /* Construct the half-space x_pos <= -1.
7488  */
7489 static __isl_give isl_basic_set *neg_halfspace(__isl_take isl_space *dim, int pos)
7490 {
7491         int k;
7492         isl_basic_set *neg;
7493
7494         neg = isl_basic_set_alloc_space(dim, 0, 0, 1);
7495         k = isl_basic_set_alloc_inequality(neg);
7496         if (k < 0)
7497                 goto error;
7498         isl_seq_clr(neg->ineq[k], 1 + isl_basic_set_total_dim(neg));
7499         isl_int_set_si(neg->ineq[k][0], -1);
7500         isl_int_set_si(neg->ineq[k][pos], -1);
7501
7502         return isl_basic_set_finalize(neg);
7503 error:
7504         isl_basic_set_free(neg);
7505         return NULL;
7506 }
7507
7508 __isl_give isl_set *isl_set_split_dims(__isl_take isl_set *set,
7509         enum isl_dim_type type, unsigned first, unsigned n)
7510 {
7511         int i;
7512         isl_basic_set *nonneg;
7513         isl_basic_set *neg;
7514
7515         if (!set)
7516                 return NULL;
7517         if (n == 0)
7518                 return set;
7519
7520         isl_assert(set->ctx, first + n <= isl_set_dim(set, type), goto error);
7521
7522         for (i = 0; i < n; ++i) {
7523                 nonneg = nonneg_halfspace(isl_set_get_space(set),
7524                                           pos(set->dim, type) + first + i);
7525                 neg = neg_halfspace(isl_set_get_space(set),
7526                                           pos(set->dim, type) + first + i);
7527
7528                 set = isl_set_intersect(set, isl_basic_set_union(nonneg, neg));
7529         }
7530
7531         return set;
7532 error:
7533         isl_set_free(set);
7534         return NULL;
7535 }
7536
7537 static int foreach_orthant(__isl_take isl_set *set, int *signs, int first,
7538         int len, int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7539         void *user)
7540 {
7541         isl_set *half;
7542
7543         if (!set)
7544                 return -1;
7545         if (isl_set_plain_is_empty(set)) {
7546                 isl_set_free(set);
7547                 return 0;
7548         }
7549         if (first == len)
7550                 return fn(set, signs, user);
7551
7552         signs[first] = 1;
7553         half = isl_set_from_basic_set(nonneg_halfspace(isl_set_get_space(set),
7554                                                         1 + first));
7555         half = isl_set_intersect(half, isl_set_copy(set));
7556         if (foreach_orthant(half, signs, first + 1, len, fn, user) < 0)
7557                 goto error;
7558
7559         signs[first] = -1;
7560         half = isl_set_from_basic_set(neg_halfspace(isl_set_get_space(set),
7561                                                         1 + first));
7562         half = isl_set_intersect(half, set);
7563         return foreach_orthant(half, signs, first + 1, len, fn, user);
7564 error:
7565         isl_set_free(set);
7566         return -1;
7567 }
7568
7569 /* Call "fn" on the intersections of "set" with each of the orthants
7570  * (except for obviously empty intersections).  The orthant is identified
7571  * by the signs array, with each entry having value 1 or -1 according
7572  * to the sign of the corresponding variable.
7573  */
7574 int isl_set_foreach_orthant(__isl_keep isl_set *set,
7575         int (*fn)(__isl_take isl_set *orthant, int *signs, void *user),
7576         void *user)
7577 {
7578         unsigned nparam;
7579         unsigned nvar;
7580         int *signs;
7581         int r;
7582
7583         if (!set)
7584                 return -1;
7585         if (isl_set_plain_is_empty(set))
7586                 return 0;
7587
7588         nparam = isl_set_dim(set, isl_dim_param);
7589         nvar = isl_set_dim(set, isl_dim_set);
7590
7591         signs = isl_alloc_array(set->ctx, int, nparam + nvar);
7592
7593         r = foreach_orthant(isl_set_copy(set), signs, 0, nparam + nvar,
7594                             fn, user);
7595
7596         free(signs);
7597
7598         return r;
7599 }
7600
7601 int isl_set_is_equal(struct isl_set *set1, struct isl_set *set2)
7602 {
7603         return isl_map_is_equal((struct isl_map *)set1, (struct isl_map *)set2);
7604 }
7605
7606 int isl_basic_map_is_subset(
7607                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7608 {
7609         int is_subset;
7610         struct isl_map *map1;
7611         struct isl_map *map2;
7612
7613         if (!bmap1 || !bmap2)
7614                 return -1;
7615
7616         map1 = isl_map_from_basic_map(isl_basic_map_copy(bmap1));
7617         map2 = isl_map_from_basic_map(isl_basic_map_copy(bmap2));
7618
7619         is_subset = isl_map_is_subset(map1, map2);
7620
7621         isl_map_free(map1);
7622         isl_map_free(map2);
7623
7624         return is_subset;
7625 }
7626
7627 int isl_basic_set_is_subset(__isl_keep isl_basic_set *bset1,
7628         __isl_keep isl_basic_set *bset2)
7629 {
7630         return isl_basic_map_is_subset(bset1, bset2);
7631 }
7632
7633 int isl_basic_map_is_equal(
7634                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7635 {
7636         int is_subset;
7637
7638         if (!bmap1 || !bmap2)
7639                 return -1;
7640         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7641         if (is_subset != 1)
7642                 return is_subset;
7643         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7644         return is_subset;
7645 }
7646
7647 int isl_basic_set_is_equal(
7648                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7649 {
7650         return isl_basic_map_is_equal(
7651                 (struct isl_basic_map *)bset1, (struct isl_basic_map *)bset2);
7652 }
7653
7654 int isl_map_is_empty(struct isl_map *map)
7655 {
7656         int i;
7657         int is_empty;
7658
7659         if (!map)
7660                 return -1;
7661         for (i = 0; i < map->n; ++i) {
7662                 is_empty = isl_basic_map_is_empty(map->p[i]);
7663                 if (is_empty < 0)
7664                         return -1;
7665                 if (!is_empty)
7666                         return 0;
7667         }
7668         return 1;
7669 }
7670
7671 int isl_map_plain_is_empty(__isl_keep isl_map *map)
7672 {
7673         return map ? map->n == 0 : -1;
7674 }
7675
7676 int isl_map_fast_is_empty(__isl_keep isl_map *map)
7677 {
7678         return isl_map_plain_is_empty(map);
7679 }
7680
7681 int isl_set_plain_is_empty(struct isl_set *set)
7682 {
7683         return set ? set->n == 0 : -1;
7684 }
7685
7686 int isl_set_fast_is_empty(__isl_keep isl_set *set)
7687 {
7688         return isl_set_plain_is_empty(set);
7689 }
7690
7691 int isl_set_is_empty(struct isl_set *set)
7692 {
7693         return isl_map_is_empty((struct isl_map *)set);
7694 }
7695
7696 int isl_map_has_equal_space(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7697 {
7698         if (!map1 || !map2)
7699                 return -1;
7700
7701         return isl_space_is_equal(map1->dim, map2->dim);
7702 }
7703
7704 int isl_set_has_equal_space(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7705 {
7706         if (!set1 || !set2)
7707                 return -1;
7708
7709         return isl_space_is_equal(set1->dim, set2->dim);
7710 }
7711
7712 static int map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7713 {
7714         int is_subset;
7715
7716         if (!map1 || !map2)
7717                 return -1;
7718         is_subset = isl_map_is_subset(map1, map2);
7719         if (is_subset != 1)
7720                 return is_subset;
7721         is_subset = isl_map_is_subset(map2, map1);
7722         return is_subset;
7723 }
7724
7725 int isl_map_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
7726 {
7727         return isl_map_align_params_map_map_and_test(map1, map2, &map_is_equal);
7728 }
7729
7730 int isl_basic_map_is_strict_subset(
7731                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7732 {
7733         int is_subset;
7734
7735         if (!bmap1 || !bmap2)
7736                 return -1;
7737         is_subset = isl_basic_map_is_subset(bmap1, bmap2);
7738         if (is_subset != 1)
7739                 return is_subset;
7740         is_subset = isl_basic_map_is_subset(bmap2, bmap1);
7741         if (is_subset == -1)
7742                 return is_subset;
7743         return !is_subset;
7744 }
7745
7746 int isl_map_is_strict_subset(struct isl_map *map1, struct isl_map *map2)
7747 {
7748         int is_subset;
7749
7750         if (!map1 || !map2)
7751                 return -1;
7752         is_subset = isl_map_is_subset(map1, map2);
7753         if (is_subset != 1)
7754                 return is_subset;
7755         is_subset = isl_map_is_subset(map2, map1);
7756         if (is_subset == -1)
7757                 return is_subset;
7758         return !is_subset;
7759 }
7760
7761 int isl_set_is_strict_subset(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
7762 {
7763         return isl_map_is_strict_subset((isl_map *)set1, (isl_map *)set2);
7764 }
7765
7766 int isl_basic_map_is_universe(struct isl_basic_map *bmap)
7767 {
7768         if (!bmap)
7769                 return -1;
7770         return bmap->n_eq == 0 && bmap->n_ineq == 0;
7771 }
7772
7773 int isl_basic_set_is_universe(struct isl_basic_set *bset)
7774 {
7775         if (!bset)
7776                 return -1;
7777         return bset->n_eq == 0 && bset->n_ineq == 0;
7778 }
7779
7780 int isl_map_plain_is_universe(__isl_keep isl_map *map)
7781 {
7782         int i;
7783
7784         if (!map)
7785                 return -1;
7786
7787         for (i = 0; i < map->n; ++i) {
7788                 int r = isl_basic_map_is_universe(map->p[i]);
7789                 if (r < 0 || r)
7790                         return r;
7791         }
7792
7793         return 0;
7794 }
7795
7796 int isl_set_plain_is_universe(__isl_keep isl_set *set)
7797 {
7798         return isl_map_plain_is_universe((isl_map *) set);
7799 }
7800
7801 int isl_set_fast_is_universe(__isl_keep isl_set *set)
7802 {
7803         return isl_set_plain_is_universe(set);
7804 }
7805
7806 int isl_basic_map_is_empty(struct isl_basic_map *bmap)
7807 {
7808         struct isl_basic_set *bset = NULL;
7809         struct isl_vec *sample = NULL;
7810         int empty;
7811         unsigned total;
7812
7813         if (!bmap)
7814                 return -1;
7815
7816         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY))
7817                 return 1;
7818
7819         if (isl_basic_map_is_universe(bmap))
7820                 return 0;
7821
7822         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_RATIONAL)) {
7823                 struct isl_basic_map *copy = isl_basic_map_copy(bmap);
7824                 copy = isl_basic_map_remove_redundancies(copy);
7825                 empty = isl_basic_map_plain_is_empty(copy);
7826                 isl_basic_map_free(copy);
7827                 return empty;
7828         }
7829
7830         total = 1 + isl_basic_map_total_dim(bmap);
7831         if (bmap->sample && bmap->sample->size == total) {
7832                 int contains = isl_basic_map_contains(bmap, bmap->sample);
7833                 if (contains < 0)
7834                         return -1;
7835                 if (contains)
7836                         return 0;
7837         }
7838         isl_vec_free(bmap->sample);
7839         bmap->sample = NULL;
7840         bset = isl_basic_map_underlying_set(isl_basic_map_copy(bmap));
7841         if (!bset)
7842                 return -1;
7843         sample = isl_basic_set_sample_vec(bset);
7844         if (!sample)
7845                 return -1;
7846         empty = sample->size == 0;
7847         isl_vec_free(bmap->sample);
7848         bmap->sample = sample;
7849         if (empty)
7850                 ISL_F_SET(bmap, ISL_BASIC_MAP_EMPTY);
7851
7852         return empty;
7853 }
7854
7855 int isl_basic_map_plain_is_empty(__isl_keep isl_basic_map *bmap)
7856 {
7857         if (!bmap)
7858                 return -1;
7859         return ISL_F_ISSET(bmap, ISL_BASIC_MAP_EMPTY);
7860 }
7861
7862 int isl_basic_map_fast_is_empty(__isl_keep isl_basic_map *bmap)
7863 {
7864         return isl_basic_map_plain_is_empty(bmap);
7865 }
7866
7867 int isl_basic_set_plain_is_empty(__isl_keep isl_basic_set *bset)
7868 {
7869         if (!bset)
7870                 return -1;
7871         return ISL_F_ISSET(bset, ISL_BASIC_SET_EMPTY);
7872 }
7873
7874 int isl_basic_set_fast_is_empty(__isl_keep isl_basic_set *bset)
7875 {
7876         return isl_basic_set_plain_is_empty(bset);
7877 }
7878
7879 int isl_basic_set_is_empty(struct isl_basic_set *bset)
7880 {
7881         return isl_basic_map_is_empty((struct isl_basic_map *)bset);
7882 }
7883
7884 struct isl_map *isl_basic_map_union(
7885         struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
7886 {
7887         struct isl_map *map;
7888         if (!bmap1 || !bmap2)
7889                 goto error;
7890
7891         isl_assert(bmap1->ctx, isl_space_is_equal(bmap1->dim, bmap2->dim), goto error);
7892
7893         map = isl_map_alloc_space(isl_space_copy(bmap1->dim), 2, 0);
7894         if (!map)
7895                 goto error;
7896         map = isl_map_add_basic_map(map, bmap1);
7897         map = isl_map_add_basic_map(map, bmap2);
7898         return map;
7899 error:
7900         isl_basic_map_free(bmap1);
7901         isl_basic_map_free(bmap2);
7902         return NULL;
7903 }
7904
7905 struct isl_set *isl_basic_set_union(
7906                 struct isl_basic_set *bset1, struct isl_basic_set *bset2)
7907 {
7908         return (struct isl_set *)isl_basic_map_union(
7909                                             (struct isl_basic_map *)bset1,
7910                                             (struct isl_basic_map *)bset2);
7911 }
7912
7913 /* Order divs such that any div only depends on previous divs */
7914 struct isl_basic_map *isl_basic_map_order_divs(struct isl_basic_map *bmap)
7915 {
7916         int i;
7917         unsigned off;
7918
7919         if (!bmap)
7920                 return NULL;
7921
7922         off = isl_space_dim(bmap->dim, isl_dim_all);
7923
7924         for (i = 0; i < bmap->n_div; ++i) {
7925                 int pos;
7926                 if (isl_int_is_zero(bmap->div[i][0]))
7927                         continue;
7928                 pos = isl_seq_first_non_zero(bmap->div[i]+1+1+off+i,
7929                                                             bmap->n_div-i);
7930                 if (pos == -1)
7931                         continue;
7932                 isl_basic_map_swap_div(bmap, i, i + pos);
7933                 --i;
7934         }
7935         return bmap;
7936 }
7937
7938 struct isl_basic_set *isl_basic_set_order_divs(struct isl_basic_set *bset)
7939 {
7940         return (struct isl_basic_set *)
7941                 isl_basic_map_order_divs((struct isl_basic_map *)bset);
7942 }
7943
7944 __isl_give isl_map *isl_map_order_divs(__isl_take isl_map *map)
7945 {
7946         int i;
7947
7948         if (!map)
7949                 return 0;
7950
7951         for (i = 0; i < map->n; ++i) {
7952                 map->p[i] = isl_basic_map_order_divs(map->p[i]);
7953                 if (!map->p[i])
7954                         goto error;
7955         }
7956
7957         return map;
7958 error:
7959         isl_map_free(map);
7960         return NULL;
7961 }
7962
7963 /* Apply the expansion computed by isl_merge_divs.
7964  * The expansion itself is given by "exp" while the resulting
7965  * list of divs is given by "div".
7966  */
7967 __isl_give isl_basic_set *isl_basic_set_expand_divs(
7968         __isl_take isl_basic_set *bset, __isl_take isl_mat *div, int *exp)
7969 {
7970         int i, j;
7971         int n_div;
7972
7973         bset = isl_basic_set_cow(bset);
7974         if (!bset || !div)
7975                 goto error;
7976
7977         if (div->n_row < bset->n_div)
7978                 isl_die(isl_mat_get_ctx(div), isl_error_invalid,
7979                         "not an expansion", goto error);
7980
7981         n_div = bset->n_div;
7982         bset = isl_basic_map_extend_space(bset, isl_space_copy(bset->dim),
7983                                             div->n_row - n_div, 0,
7984                                             2 * (div->n_row - n_div));
7985
7986         for (i = n_div; i < div->n_row; ++i)
7987                 if (isl_basic_set_alloc_div(bset) < 0)
7988                         goto error;
7989
7990         j = n_div - 1;
7991         for (i = div->n_row - 1; i >= 0; --i) {
7992                 if (j >= 0 && exp[j] == i) {
7993                         if (i != j)
7994                                 isl_basic_map_swap_div(bset, i, j);
7995                         j--;
7996                 } else {
7997                         isl_seq_cpy(bset->div[i], div->row[i], div->n_col);
7998                         if (isl_basic_map_add_div_constraints(bset, i) < 0)
7999                                 goto error;
8000                 }
8001         }
8002
8003         isl_mat_free(div);
8004         return bset;
8005 error:
8006         isl_basic_set_free(bset);
8007         isl_mat_free(div);
8008         return NULL;
8009 }
8010
8011 /* Look for a div in dst that corresponds to the div "div" in src.
8012  * The divs before "div" in src and dst are assumed to be the same.
8013  * 
8014  * Returns -1 if no corresponding div was found and the position
8015  * of the corresponding div in dst otherwise.
8016  */
8017 static int find_div(struct isl_basic_map *dst,
8018                         struct isl_basic_map *src, unsigned div)
8019 {
8020         int i;
8021
8022         unsigned total = isl_space_dim(src->dim, isl_dim_all);
8023
8024         isl_assert(dst->ctx, div <= dst->n_div, return -1);
8025         for (i = div; i < dst->n_div; ++i)
8026                 if (isl_seq_eq(dst->div[i], src->div[div], 1+1+total+div) &&
8027                     isl_seq_first_non_zero(dst->div[i]+1+1+total+div,
8028                                                 dst->n_div - div) == -1)
8029                         return i;
8030         return -1;
8031 }
8032
8033 struct isl_basic_map *isl_basic_map_align_divs(
8034                 struct isl_basic_map *dst, struct isl_basic_map *src)
8035 {
8036         int i;
8037         unsigned total;
8038
8039         if (!dst || !src)
8040                 goto error;
8041
8042         if (src->n_div == 0)
8043                 return dst;
8044
8045         for (i = 0; i < src->n_div; ++i)
8046                 isl_assert(src->ctx, !isl_int_is_zero(src->div[i][0]), goto error);
8047
8048         src = isl_basic_map_order_divs(src);
8049         dst = isl_basic_map_cow(dst);
8050         dst = isl_basic_map_extend_space(dst, isl_space_copy(dst->dim),
8051                         src->n_div, 0, 2 * src->n_div);
8052         if (!dst)
8053                 return NULL;
8054         total = isl_space_dim(src->dim, isl_dim_all);
8055         for (i = 0; i < src->n_div; ++i) {
8056                 int j = find_div(dst, src, i);
8057                 if (j < 0) {
8058                         j = isl_basic_map_alloc_div(dst);
8059                         if (j < 0)
8060                                 goto error;
8061                         isl_seq_cpy(dst->div[j], src->div[i], 1+1+total+i);
8062                         isl_seq_clr(dst->div[j]+1+1+total+i, dst->n_div - i);
8063                         if (isl_basic_map_add_div_constraints(dst, j) < 0)
8064                                 goto error;
8065                 }
8066                 if (j != i)
8067                         isl_basic_map_swap_div(dst, i, j);
8068         }
8069         return dst;
8070 error:
8071         isl_basic_map_free(dst);
8072         return NULL;
8073 }
8074
8075 struct isl_basic_set *isl_basic_set_align_divs(
8076                 struct isl_basic_set *dst, struct isl_basic_set *src)
8077 {
8078         return (struct isl_basic_set *)isl_basic_map_align_divs(
8079                 (struct isl_basic_map *)dst, (struct isl_basic_map *)src);
8080 }
8081
8082 struct isl_map *isl_map_align_divs(struct isl_map *map)
8083 {
8084         int i;
8085
8086         if (!map)
8087                 return NULL;
8088         if (map->n == 0)
8089                 return map;
8090         map = isl_map_compute_divs(map);
8091         map = isl_map_cow(map);
8092         if (!map)
8093                 return NULL;
8094
8095         for (i = 1; i < map->n; ++i)
8096                 map->p[0] = isl_basic_map_align_divs(map->p[0], map->p[i]);
8097         for (i = 1; i < map->n; ++i)
8098                 map->p[i] = isl_basic_map_align_divs(map->p[i], map->p[0]);
8099
8100         ISL_F_CLR(map, ISL_MAP_NORMALIZED);
8101         return map;
8102 }
8103
8104 struct isl_set *isl_set_align_divs(struct isl_set *set)
8105 {
8106         return (struct isl_set *)isl_map_align_divs((struct isl_map *)set);
8107 }
8108
8109 static __isl_give isl_set *set_apply( __isl_take isl_set *set,
8110         __isl_take isl_map *map)
8111 {
8112         if (!set || !map)
8113                 goto error;
8114         isl_assert(set->ctx, isl_map_compatible_domain(map, set), goto error);
8115         map = isl_map_intersect_domain(map, set);
8116         set = isl_map_range(map);
8117         return set;
8118 error:
8119         isl_set_free(set);
8120         isl_map_free(map);
8121         return NULL;
8122 }
8123
8124 __isl_give isl_set *isl_set_apply( __isl_take isl_set *set,
8125         __isl_take isl_map *map)
8126 {
8127         return isl_map_align_params_map_map_and(set, map, &set_apply);
8128 }
8129
8130 /* There is no need to cow as removing empty parts doesn't change
8131  * the meaning of the set.
8132  */
8133 struct isl_map *isl_map_remove_empty_parts(struct isl_map *map)
8134 {
8135         int i;
8136
8137         if (!map)
8138                 return NULL;
8139
8140         for (i = map->n - 1; i >= 0; --i)
8141                 remove_if_empty(map, i);
8142
8143         return map;
8144 }
8145
8146 struct isl_set *isl_set_remove_empty_parts(struct isl_set *set)
8147 {
8148         return (struct isl_set *)
8149                 isl_map_remove_empty_parts((struct isl_map *)set);
8150 }
8151
8152 struct isl_basic_map *isl_map_copy_basic_map(struct isl_map *map)
8153 {
8154         struct isl_basic_map *bmap;
8155         if (!map || map->n == 0)
8156                 return NULL;
8157         bmap = map->p[map->n-1];
8158         isl_assert(map->ctx, ISL_F_ISSET(bmap, ISL_BASIC_SET_FINAL), return NULL);
8159         return isl_basic_map_copy(bmap);
8160 }
8161
8162 struct isl_basic_set *isl_set_copy_basic_set(struct isl_set *set)
8163 {
8164         return (struct isl_basic_set *)
8165                 isl_map_copy_basic_map((struct isl_map *)set);
8166 }
8167
8168 __isl_give isl_map *isl_map_drop_basic_map(__isl_take isl_map *map,
8169                                                 __isl_keep isl_basic_map *bmap)
8170 {
8171         int i;
8172
8173         if (!map || !bmap)
8174                 goto error;
8175         for (i = map->n-1; i >= 0; --i) {
8176                 if (map->p[i] != bmap)
8177                         continue;
8178                 map = isl_map_cow(map);
8179                 if (!map)
8180                         goto error;
8181                 isl_basic_map_free(map->p[i]);
8182                 if (i != map->n-1) {
8183                         ISL_F_CLR(map, ISL_SET_NORMALIZED);
8184                         map->p[i] = map->p[map->n-1];
8185                 }
8186                 map->n--;
8187                 return map;
8188         }
8189         return map;
8190 error:
8191         isl_map_free(map);
8192         return NULL;
8193 }
8194
8195 struct isl_set *isl_set_drop_basic_set(struct isl_set *set,
8196                                                 struct isl_basic_set *bset)
8197 {
8198         return (struct isl_set *)isl_map_drop_basic_map((struct isl_map *)set,
8199                                                 (struct isl_basic_map *)bset);
8200 }
8201
8202 /* Given two basic sets bset1 and bset2, compute the maximal difference
8203  * between the values of dimension pos in bset1 and those in bset2
8204  * for any common value of the parameters and dimensions preceding pos.
8205  */
8206 static enum isl_lp_result basic_set_maximal_difference_at(
8207         __isl_keep isl_basic_set *bset1, __isl_keep isl_basic_set *bset2,
8208         int pos, isl_int *opt)
8209 {
8210         isl_space *dims;
8211         struct isl_basic_map *bmap1 = NULL;
8212         struct isl_basic_map *bmap2 = NULL;
8213         struct isl_ctx *ctx;
8214         struct isl_vec *obj;
8215         unsigned total;
8216         unsigned nparam;
8217         unsigned dim1, dim2;
8218         enum isl_lp_result res;
8219
8220         if (!bset1 || !bset2)
8221                 return isl_lp_error;
8222
8223         nparam = isl_basic_set_n_param(bset1);
8224         dim1 = isl_basic_set_n_dim(bset1);
8225         dim2 = isl_basic_set_n_dim(bset2);
8226         dims = isl_space_alloc(bset1->ctx, nparam, pos, dim1 - pos);
8227         bmap1 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset1), dims);
8228         dims = isl_space_alloc(bset2->ctx, nparam, pos, dim2 - pos);
8229         bmap2 = isl_basic_map_from_basic_set(isl_basic_set_copy(bset2), dims);
8230         if (!bmap1 || !bmap2)
8231                 goto error;
8232         bmap1 = isl_basic_map_cow(bmap1);
8233         bmap1 = isl_basic_map_extend(bmap1, nparam,
8234                         pos, (dim1 - pos) + (dim2 - pos),
8235                         bmap2->n_div, bmap2->n_eq, bmap2->n_ineq);
8236         bmap1 = add_constraints(bmap1, bmap2, 0, dim1 - pos);
8237         if (!bmap1)
8238                 goto error;
8239         total = isl_basic_map_total_dim(bmap1);
8240         ctx = bmap1->ctx;
8241         obj = isl_vec_alloc(ctx, 1 + total);
8242         if (!obj)
8243                 goto error2;
8244         isl_seq_clr(obj->block.data, 1 + total);
8245         isl_int_set_si(obj->block.data[1+nparam+pos], 1);
8246         isl_int_set_si(obj->block.data[1+nparam+pos+(dim1-pos)], -1);
8247         res = isl_basic_map_solve_lp(bmap1, 1, obj->block.data, ctx->one,
8248                                         opt, NULL, NULL);
8249         isl_basic_map_free(bmap1);
8250         isl_vec_free(obj);
8251         return res;
8252 error:
8253         isl_basic_map_free(bmap2);
8254 error2:
8255         isl_basic_map_free(bmap1);
8256         return isl_lp_error;
8257 }
8258
8259 /* Given two _disjoint_ basic sets bset1 and bset2, check whether
8260  * for any common value of the parameters and dimensions preceding pos
8261  * in both basic sets, the values of dimension pos in bset1 are
8262  * smaller or larger than those in bset2.
8263  *
8264  * Returns
8265  *       1 if bset1 follows bset2
8266  *      -1 if bset1 precedes bset2
8267  *       0 if bset1 and bset2 are incomparable
8268  *      -2 if some error occurred.
8269  */
8270 int isl_basic_set_compare_at(struct isl_basic_set *bset1,
8271         struct isl_basic_set *bset2, int pos)
8272 {
8273         isl_int opt;
8274         enum isl_lp_result res;
8275         int cmp;
8276
8277         isl_int_init(opt);
8278
8279         res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8280
8281         if (res == isl_lp_empty)
8282                 cmp = 0;
8283         else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8284                   res == isl_lp_unbounded)
8285                 cmp = 1;
8286         else if (res == isl_lp_ok && isl_int_is_neg(opt))
8287                 cmp = -1;
8288         else
8289                 cmp = -2;
8290
8291         isl_int_clear(opt);
8292         return cmp;
8293 }
8294
8295 /* Given two basic sets bset1 and bset2, check whether
8296  * for any common value of the parameters and dimensions preceding pos
8297  * there is a value of dimension pos in bset1 that is larger
8298  * than a value of the same dimension in bset2.
8299  *
8300  * Return
8301  *       1 if there exists such a pair
8302  *       0 if there is no such pair, but there is a pair of equal values
8303  *      -1 otherwise
8304  *      -2 if some error occurred.
8305  */
8306 int isl_basic_set_follows_at(__isl_keep isl_basic_set *bset1,
8307         __isl_keep isl_basic_set *bset2, int pos)
8308 {
8309         isl_int opt;
8310         enum isl_lp_result res;
8311         int cmp;
8312
8313         isl_int_init(opt);
8314
8315         res = basic_set_maximal_difference_at(bset1, bset2, pos, &opt);
8316
8317         if (res == isl_lp_empty)
8318                 cmp = -1;
8319         else if ((res == isl_lp_ok && isl_int_is_pos(opt)) ||
8320                   res == isl_lp_unbounded)
8321                 cmp = 1;
8322         else if (res == isl_lp_ok && isl_int_is_neg(opt))
8323                 cmp = -1;
8324         else if (res == isl_lp_ok)
8325                 cmp = 0;
8326         else
8327                 cmp = -2;
8328
8329         isl_int_clear(opt);
8330         return cmp;
8331 }
8332
8333 /* Given two sets set1 and set2, check whether
8334  * for any common value of the parameters and dimensions preceding pos
8335  * there is a value of dimension pos in set1 that is larger
8336  * than a value of the same dimension in set2.
8337  *
8338  * Return
8339  *       1 if there exists such a pair
8340  *       0 if there is no such pair, but there is a pair of equal values
8341  *      -1 otherwise
8342  *      -2 if some error occurred.
8343  */
8344 int isl_set_follows_at(__isl_keep isl_set *set1,
8345         __isl_keep isl_set *set2, int pos)
8346 {
8347         int i, j;
8348         int follows = -1;
8349
8350         if (!set1 || !set2)
8351                 return -2;
8352
8353         for (i = 0; i < set1->n; ++i)
8354                 for (j = 0; j < set2->n; ++j) {
8355                         int f;
8356                         f = isl_basic_set_follows_at(set1->p[i], set2->p[j], pos);
8357                         if (f == 1 || f == -2)
8358                                 return f;
8359                         if (f > follows)
8360                                 follows = f;
8361                 }
8362
8363         return follows;
8364 }
8365
8366 static int isl_basic_map_plain_has_fixed_var(__isl_keep isl_basic_map *bmap,
8367         unsigned pos, isl_int *val)
8368 {
8369         int i;
8370         int d;
8371         unsigned total;
8372
8373         if (!bmap)
8374                 return -1;
8375         total = isl_basic_map_total_dim(bmap);
8376         for (i = 0, d = total-1; i < bmap->n_eq && d+1 > pos; ++i) {
8377                 for (; d+1 > pos; --d)
8378                         if (!isl_int_is_zero(bmap->eq[i][1+d]))
8379                                 break;
8380                 if (d != pos)
8381                         continue;
8382                 if (isl_seq_first_non_zero(bmap->eq[i]+1, d) != -1)
8383                         return 0;
8384                 if (isl_seq_first_non_zero(bmap->eq[i]+1+d+1, total-d-1) != -1)
8385                         return 0;
8386                 if (!isl_int_is_one(bmap->eq[i][1+d]))
8387                         return 0;
8388                 if (val)
8389                         isl_int_neg(*val, bmap->eq[i][0]);
8390                 return 1;
8391         }
8392         return 0;
8393 }
8394
8395 static int isl_map_plain_has_fixed_var(__isl_keep isl_map *map,
8396         unsigned pos, isl_int *val)
8397 {
8398         int i;
8399         isl_int v;
8400         isl_int tmp;
8401         int fixed;
8402
8403         if (!map)
8404                 return -1;
8405         if (map->n == 0)
8406                 return 0;
8407         if (map->n == 1)
8408                 return isl_basic_map_plain_has_fixed_var(map->p[0], pos, val); 
8409         isl_int_init(v);
8410         isl_int_init(tmp);
8411         fixed = isl_basic_map_plain_has_fixed_var(map->p[0], pos, &v); 
8412         for (i = 1; fixed == 1 && i < map->n; ++i) {
8413                 fixed = isl_basic_map_plain_has_fixed_var(map->p[i], pos, &tmp); 
8414                 if (fixed == 1 && isl_int_ne(tmp, v))
8415                         fixed = 0;
8416         }
8417         if (val)
8418                 isl_int_set(*val, v);
8419         isl_int_clear(tmp);
8420         isl_int_clear(v);
8421         return fixed;
8422 }
8423
8424 static int isl_basic_set_plain_has_fixed_var(__isl_keep isl_basic_set *bset,
8425         unsigned pos, isl_int *val)
8426 {
8427         return isl_basic_map_plain_has_fixed_var((struct isl_basic_map *)bset,
8428                                                 pos, val);
8429 }
8430
8431 static int isl_set_plain_has_fixed_var(__isl_keep isl_set *set, unsigned pos,
8432         isl_int *val)
8433 {
8434         return isl_map_plain_has_fixed_var((struct isl_map *)set, pos, val);
8435 }
8436
8437 int isl_basic_map_plain_is_fixed(__isl_keep isl_basic_map *bmap,
8438         enum isl_dim_type type, unsigned pos, isl_int *val)
8439 {
8440         if (pos >= isl_basic_map_dim(bmap, type))
8441                 return -1;
8442         return isl_basic_map_plain_has_fixed_var(bmap,
8443                 isl_basic_map_offset(bmap, type) - 1 + pos, val);
8444 }
8445
8446 int isl_map_plain_is_fixed(__isl_keep isl_map *map,
8447         enum isl_dim_type type, unsigned pos, isl_int *val)
8448 {
8449         if (pos >= isl_map_dim(map, type))
8450                 return -1;
8451         return isl_map_plain_has_fixed_var(map,
8452                 map_offset(map, type) - 1 + pos, val);
8453 }
8454
8455 /* If "map" obviously lies on a hyperplane where the given dimension
8456  * has a fixed value, then return that value.
8457  * Otherwise return NaN.
8458  */
8459 __isl_give isl_val *isl_map_plain_get_val_if_fixed(__isl_keep isl_map *map,
8460         enum isl_dim_type type, unsigned pos)
8461 {
8462         isl_ctx *ctx;
8463         isl_val *v;
8464         int fixed;
8465
8466         if (!map)
8467                 return NULL;
8468         ctx = isl_map_get_ctx(map);
8469         v = isl_val_alloc(ctx);
8470         if (!v)
8471                 return NULL;
8472         fixed = isl_map_plain_is_fixed(map, type, pos, &v->n);
8473         if (fixed < 0)
8474                 return isl_val_free(v);
8475         if (fixed) {
8476                 isl_int_set_si(v->d, 1);
8477                 return v;
8478         }
8479         isl_val_free(v);
8480         return isl_val_nan(ctx);
8481 }
8482
8483 int isl_set_plain_is_fixed(__isl_keep isl_set *set,
8484         enum isl_dim_type type, unsigned pos, isl_int *val)
8485 {
8486         return isl_map_plain_is_fixed(set, type, pos, val);
8487 }
8488
8489 int isl_map_fast_is_fixed(__isl_keep isl_map *map,
8490         enum isl_dim_type type, unsigned pos, isl_int *val)
8491 {
8492         return isl_map_plain_is_fixed(map, type, pos, val);
8493 }
8494
8495 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8496  * then return this fixed value in *val.
8497  */
8498 int isl_basic_set_plain_dim_is_fixed(__isl_keep isl_basic_set *bset,
8499         unsigned dim, isl_int *val)
8500 {
8501         return isl_basic_set_plain_has_fixed_var(bset,
8502                                         isl_basic_set_n_param(bset) + dim, val);
8503 }
8504
8505 /* Check if dimension dim has fixed value and if so and if val is not NULL,
8506  * then return this fixed value in *val.
8507  */
8508 int isl_set_plain_dim_is_fixed(__isl_keep isl_set *set,
8509         unsigned dim, isl_int *val)
8510 {
8511         return isl_set_plain_has_fixed_var(set, isl_set_n_param(set) + dim, val);
8512 }
8513
8514 int isl_set_fast_dim_is_fixed(__isl_keep isl_set *set,
8515         unsigned dim, isl_int *val)
8516 {
8517         return isl_set_plain_dim_is_fixed(set, dim, val);
8518 }
8519
8520 /* Check if input variable in has fixed value and if so and if val is not NULL,
8521  * then return this fixed value in *val.
8522  */
8523 int isl_map_plain_input_is_fixed(__isl_keep isl_map *map,
8524         unsigned in, isl_int *val)
8525 {
8526         return isl_map_plain_has_fixed_var(map, isl_map_n_param(map) + in, val);
8527 }
8528
8529 /* Check if dimension dim has an (obvious) fixed lower bound and if so
8530  * and if val is not NULL, then return this lower bound in *val.
8531  */
8532 int isl_basic_set_plain_dim_has_fixed_lower_bound(
8533         __isl_keep isl_basic_set *bset, unsigned dim, isl_int *val)
8534 {
8535         int i, i_eq = -1, i_ineq = -1;
8536         isl_int *c;
8537         unsigned total;
8538         unsigned nparam;
8539
8540         if (!bset)
8541                 return -1;
8542         total = isl_basic_set_total_dim(bset);
8543         nparam = isl_basic_set_n_param(bset);
8544         for (i = 0; i < bset->n_eq; ++i) {
8545                 if (isl_int_is_zero(bset->eq[i][1+nparam+dim]))
8546                         continue;
8547                 if (i_eq != -1)
8548                         return 0;
8549                 i_eq = i;
8550         }
8551         for (i = 0; i < bset->n_ineq; ++i) {
8552                 if (!isl_int_is_pos(bset->ineq[i][1+nparam+dim]))
8553                         continue;
8554                 if (i_eq != -1 || i_ineq != -1)
8555                         return 0;
8556                 i_ineq = i;
8557         }
8558         if (i_eq == -1 && i_ineq == -1)
8559                 return 0;
8560         c = i_eq != -1 ? bset->eq[i_eq] : bset->ineq[i_ineq];
8561         /* The coefficient should always be one due to normalization. */
8562         if (!isl_int_is_one(c[1+nparam+dim]))
8563                 return 0;
8564         if (isl_seq_first_non_zero(c+1, nparam+dim) != -1)
8565                 return 0;
8566         if (isl_seq_first_non_zero(c+1+nparam+dim+1,
8567                                         total - nparam - dim - 1) != -1)
8568                 return 0;
8569         if (val)
8570                 isl_int_neg(*val, c[0]);
8571         return 1;
8572 }
8573
8574 int isl_set_plain_dim_has_fixed_lower_bound(__isl_keep isl_set *set,
8575         unsigned dim, isl_int *val)
8576 {
8577         int i;
8578         isl_int v;
8579         isl_int tmp;
8580         int fixed;
8581
8582         if (!set)
8583                 return -1;
8584         if (set->n == 0)
8585                 return 0;
8586         if (set->n == 1)
8587                 return isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8588                                                                 dim, val);
8589         isl_int_init(v);
8590         isl_int_init(tmp);
8591         fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[0],
8592                                                                 dim, &v);
8593         for (i = 1; fixed == 1 && i < set->n; ++i) {
8594                 fixed = isl_basic_set_plain_dim_has_fixed_lower_bound(set->p[i],
8595                                                                 dim, &tmp);
8596                 if (fixed == 1 && isl_int_ne(tmp, v))
8597                         fixed = 0;
8598         }
8599         if (val)
8600                 isl_int_set(*val, v);
8601         isl_int_clear(tmp);
8602         isl_int_clear(v);
8603         return fixed;
8604 }
8605
8606 struct constraint {
8607         unsigned        size;
8608         isl_int         *c;
8609 };
8610
8611 /* uset_gist depends on constraints without existentially quantified
8612  * variables sorting first.
8613  */
8614 static int qsort_constraint_cmp(const void *p1, const void *p2)
8615 {
8616         const struct constraint *c1 = (const struct constraint *)p1;
8617         const struct constraint *c2 = (const struct constraint *)p2;
8618         int l1, l2;
8619         unsigned size = isl_min(c1->size, c2->size);
8620
8621         l1 = isl_seq_last_non_zero(c1->c + 1, size);
8622         l2 = isl_seq_last_non_zero(c2->c + 1, size);
8623
8624         if (l1 != l2)
8625                 return l1 - l2;
8626
8627         return isl_seq_cmp(c1->c + 1, c2->c + 1, size);
8628 }
8629
8630 static struct isl_basic_map *isl_basic_map_sort_constraints(
8631         struct isl_basic_map *bmap)
8632 {
8633         int i;
8634         struct constraint *c;
8635         unsigned total;
8636
8637         if (!bmap)
8638                 return NULL;
8639         total = isl_basic_map_total_dim(bmap);
8640         c = isl_alloc_array(bmap->ctx, struct constraint, bmap->n_ineq);
8641         if (!c)
8642                 goto error;
8643         for (i = 0; i < bmap->n_ineq; ++i) {
8644                 c[i].size = total;
8645                 c[i].c = bmap->ineq[i];
8646         }
8647         qsort(c, bmap->n_ineq, sizeof(struct constraint), qsort_constraint_cmp);
8648         for (i = 0; i < bmap->n_ineq; ++i)
8649                 bmap->ineq[i] = c[i].c;
8650         free(c);
8651         return bmap;
8652 error:
8653         isl_basic_map_free(bmap);
8654         return NULL;
8655 }
8656
8657 __isl_give isl_basic_set *isl_basic_set_sort_constraints(
8658         __isl_take isl_basic_set *bset)
8659 {
8660         return (struct isl_basic_set *)isl_basic_map_sort_constraints(
8661                                                 (struct isl_basic_map *)bset);
8662 }
8663
8664 struct isl_basic_map *isl_basic_map_normalize(struct isl_basic_map *bmap)
8665 {
8666         if (!bmap)
8667                 return NULL;
8668         if (ISL_F_ISSET(bmap, ISL_BASIC_MAP_NORMALIZED))
8669                 return bmap;
8670         bmap = isl_basic_map_remove_redundancies(bmap);
8671         bmap = isl_basic_map_sort_constraints(bmap);
8672         if (bmap)
8673                 ISL_F_SET(bmap, ISL_BASIC_MAP_NORMALIZED);
8674         return bmap;
8675 }
8676
8677 struct isl_basic_set *isl_basic_set_normalize(struct isl_basic_set *bset)
8678 {
8679         return (struct isl_basic_set *)isl_basic_map_normalize(
8680                                                 (struct isl_basic_map *)bset);
8681 }
8682
8683 int isl_basic_map_plain_cmp(const __isl_keep isl_basic_map *bmap1,
8684         const __isl_keep isl_basic_map *bmap2)
8685 {
8686         int i, cmp;
8687         unsigned total;
8688
8689         if (bmap1 == bmap2)
8690                 return 0;
8691         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) !=
8692             ISL_F_ISSET(bmap2, ISL_BASIC_MAP_RATIONAL))
8693                 return ISL_F_ISSET(bmap1, ISL_BASIC_MAP_RATIONAL) ? -1 : 1;
8694         if (isl_basic_map_n_param(bmap1) != isl_basic_map_n_param(bmap2))
8695                 return isl_basic_map_n_param(bmap1) - isl_basic_map_n_param(bmap2);
8696         if (isl_basic_map_n_in(bmap1) != isl_basic_map_n_in(bmap2))
8697                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8698         if (isl_basic_map_n_out(bmap1) != isl_basic_map_n_out(bmap2))
8699                 return isl_basic_map_n_out(bmap1) - isl_basic_map_n_out(bmap2);
8700         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY) &&
8701             ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8702                 return 0;
8703         if (ISL_F_ISSET(bmap1, ISL_BASIC_MAP_EMPTY))
8704                 return 1;
8705         if (ISL_F_ISSET(bmap2, ISL_BASIC_MAP_EMPTY))
8706                 return -1;
8707         if (bmap1->n_eq != bmap2->n_eq)
8708                 return bmap1->n_eq - bmap2->n_eq;
8709         if (bmap1->n_ineq != bmap2->n_ineq)
8710                 return bmap1->n_ineq - bmap2->n_ineq;
8711         if (bmap1->n_div != bmap2->n_div)
8712                 return bmap1->n_div - bmap2->n_div;
8713         total = isl_basic_map_total_dim(bmap1);
8714         for (i = 0; i < bmap1->n_eq; ++i) {
8715                 cmp = isl_seq_cmp(bmap1->eq[i], bmap2->eq[i], 1+total);
8716                 if (cmp)
8717                         return cmp;
8718         }
8719         for (i = 0; i < bmap1->n_ineq; ++i) {
8720                 cmp = isl_seq_cmp(bmap1->ineq[i], bmap2->ineq[i], 1+total);
8721                 if (cmp)
8722                         return cmp;
8723         }
8724         for (i = 0; i < bmap1->n_div; ++i) {
8725                 cmp = isl_seq_cmp(bmap1->div[i], bmap2->div[i], 1+1+total);
8726                 if (cmp)
8727                         return cmp;
8728         }
8729         return 0;
8730 }
8731
8732 int isl_basic_set_plain_cmp(const __isl_keep isl_basic_set *bset1,
8733         const __isl_keep isl_basic_set *bset2)
8734 {
8735         return isl_basic_map_plain_cmp(bset1, bset2);
8736 }
8737
8738 int isl_set_plain_cmp(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8739 {
8740         int i, cmp;
8741
8742         if (set1 == set2)
8743                 return 0;
8744         if (set1->n != set2->n)
8745                 return set1->n - set2->n;
8746
8747         for (i = 0; i < set1->n; ++i) {
8748                 cmp = isl_basic_set_plain_cmp(set1->p[i], set2->p[i]);
8749                 if (cmp)
8750                         return cmp;
8751         }
8752
8753         return 0;
8754 }
8755
8756 int isl_basic_map_plain_is_equal(__isl_keep isl_basic_map *bmap1,
8757         __isl_keep isl_basic_map *bmap2)
8758 {
8759         return isl_basic_map_plain_cmp(bmap1, bmap2) == 0;
8760 }
8761
8762 int isl_basic_set_plain_is_equal(__isl_keep isl_basic_set *bset1,
8763         __isl_keep isl_basic_set *bset2)
8764 {
8765         return isl_basic_map_plain_is_equal((isl_basic_map *)bset1,
8766                                             (isl_basic_map *)bset2);
8767 }
8768
8769 static int qsort_bmap_cmp(const void *p1, const void *p2)
8770 {
8771         const struct isl_basic_map *bmap1 = *(const struct isl_basic_map **)p1;
8772         const struct isl_basic_map *bmap2 = *(const struct isl_basic_map **)p2;
8773
8774         return isl_basic_map_plain_cmp(bmap1, bmap2);
8775 }
8776
8777 /* We normalize in place, but if anything goes wrong we need
8778  * to return NULL, so we need to make sure we don't change the
8779  * meaning of any possible other copies of map.
8780  */
8781 struct isl_map *isl_map_normalize(struct isl_map *map)
8782 {
8783         int i, j;
8784         struct isl_basic_map *bmap;
8785
8786         if (!map)
8787                 return NULL;
8788         if (ISL_F_ISSET(map, ISL_MAP_NORMALIZED))
8789                 return map;
8790         for (i = 0; i < map->n; ++i) {
8791                 bmap = isl_basic_map_normalize(isl_basic_map_copy(map->p[i]));
8792                 if (!bmap)
8793                         goto error;
8794                 isl_basic_map_free(map->p[i]);
8795                 map->p[i] = bmap;
8796         }
8797         qsort(map->p, map->n, sizeof(struct isl_basic_map *), qsort_bmap_cmp);
8798         ISL_F_SET(map, ISL_MAP_NORMALIZED);
8799         map = isl_map_remove_empty_parts(map);
8800         if (!map)
8801                 return NULL;
8802         for (i = map->n - 1; i >= 1; --i) {
8803                 if (!isl_basic_map_plain_is_equal(map->p[i-1], map->p[i]))
8804                         continue;
8805                 isl_basic_map_free(map->p[i-1]);
8806                 for (j = i; j < map->n; ++j)
8807                         map->p[j-1] = map->p[j];
8808                 map->n--;
8809         }
8810         return map;
8811 error:
8812         isl_map_free(map);
8813         return NULL;
8814
8815 }
8816
8817 struct isl_set *isl_set_normalize(struct isl_set *set)
8818 {
8819         return (struct isl_set *)isl_map_normalize((struct isl_map *)set);
8820 }
8821
8822 int isl_map_plain_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8823 {
8824         int i;
8825         int equal;
8826
8827         if (!map1 || !map2)
8828                 return -1;
8829
8830         if (map1 == map2)
8831                 return 1;
8832         if (!isl_space_is_equal(map1->dim, map2->dim))
8833                 return 0;
8834
8835         map1 = isl_map_copy(map1);
8836         map2 = isl_map_copy(map2);
8837         map1 = isl_map_normalize(map1);
8838         map2 = isl_map_normalize(map2);
8839         if (!map1 || !map2)
8840                 goto error;
8841         equal = map1->n == map2->n;
8842         for (i = 0; equal && i < map1->n; ++i) {
8843                 equal = isl_basic_map_plain_is_equal(map1->p[i], map2->p[i]);
8844                 if (equal < 0)
8845                         goto error;
8846         }
8847         isl_map_free(map1);
8848         isl_map_free(map2);
8849         return equal;
8850 error:
8851         isl_map_free(map1);
8852         isl_map_free(map2);
8853         return -1;
8854 }
8855
8856 int isl_map_fast_is_equal(__isl_keep isl_map *map1, __isl_keep isl_map *map2)
8857 {
8858         return isl_map_plain_is_equal(map1, map2);
8859 }
8860
8861 int isl_set_plain_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8862 {
8863         return isl_map_plain_is_equal((struct isl_map *)set1,
8864                                                 (struct isl_map *)set2);
8865 }
8866
8867 int isl_set_fast_is_equal(__isl_keep isl_set *set1, __isl_keep isl_set *set2)
8868 {
8869         return isl_set_plain_is_equal(set1, set2);
8870 }
8871
8872 /* Return an interval that ranges from min to max (inclusive)
8873  */
8874 struct isl_basic_set *isl_basic_set_interval(struct isl_ctx *ctx,
8875         isl_int min, isl_int max)
8876 {
8877         int k;
8878         struct isl_basic_set *bset = NULL;
8879
8880         bset = isl_basic_set_alloc(ctx, 0, 1, 0, 0, 2);
8881         if (!bset)
8882                 goto error;
8883
8884         k = isl_basic_set_alloc_inequality(bset);
8885         if (k < 0)
8886                 goto error;
8887         isl_int_set_si(bset->ineq[k][1], 1);
8888         isl_int_neg(bset->ineq[k][0], min);
8889
8890         k = isl_basic_set_alloc_inequality(bset);
8891         if (k < 0)
8892                 goto error;
8893         isl_int_set_si(bset->ineq[k][1], -1);
8894         isl_int_set(bset->ineq[k][0], max);
8895
8896         return bset;
8897 error:
8898         isl_basic_set_free(bset);
8899         return NULL;
8900 }
8901
8902 /* Return the Cartesian product of the basic sets in list (in the given order).
8903  */
8904 __isl_give isl_basic_set *isl_basic_set_list_product(
8905         __isl_take struct isl_basic_set_list *list)
8906 {
8907         int i;
8908         unsigned dim;
8909         unsigned nparam;
8910         unsigned extra;
8911         unsigned n_eq;
8912         unsigned n_ineq;
8913         struct isl_basic_set *product = NULL;
8914
8915         if (!list)
8916                 goto error;
8917         isl_assert(list->ctx, list->n > 0, goto error);
8918         isl_assert(list->ctx, list->p[0], goto error);
8919         nparam = isl_basic_set_n_param(list->p[0]);
8920         dim = isl_basic_set_n_dim(list->p[0]);
8921         extra = list->p[0]->n_div;
8922         n_eq = list->p[0]->n_eq;
8923         n_ineq = list->p[0]->n_ineq;
8924         for (i = 1; i < list->n; ++i) {
8925                 isl_assert(list->ctx, list->p[i], goto error);
8926                 isl_assert(list->ctx,
8927                     nparam == isl_basic_set_n_param(list->p[i]), goto error);
8928                 dim += isl_basic_set_n_dim(list->p[i]);
8929                 extra += list->p[i]->n_div;
8930                 n_eq += list->p[i]->n_eq;
8931                 n_ineq += list->p[i]->n_ineq;
8932         }
8933         product = isl_basic_set_alloc(list->ctx, nparam, dim, extra,
8934                                         n_eq, n_ineq);
8935         if (!product)
8936                 goto error;
8937         dim = 0;
8938         for (i = 0; i < list->n; ++i) {
8939                 isl_basic_set_add_constraints(product,
8940                                         isl_basic_set_copy(list->p[i]), dim);
8941                 dim += isl_basic_set_n_dim(list->p[i]);
8942         }
8943         isl_basic_set_list_free(list);
8944         return product;
8945 error:
8946         isl_basic_set_free(product);
8947         isl_basic_set_list_free(list);
8948         return NULL;
8949 }
8950
8951 struct isl_basic_map *isl_basic_map_product(
8952                 struct isl_basic_map *bmap1, struct isl_basic_map *bmap2)
8953 {
8954         isl_space *dim_result = NULL;
8955         struct isl_basic_map *bmap;
8956         unsigned in1, in2, out1, out2, nparam, total, pos;
8957         struct isl_dim_map *dim_map1, *dim_map2;
8958
8959         if (!bmap1 || !bmap2)
8960                 goto error;
8961
8962         isl_assert(bmap1->ctx, isl_space_match(bmap1->dim, isl_dim_param,
8963                                      bmap2->dim, isl_dim_param), goto error);
8964         dim_result = isl_space_product(isl_space_copy(bmap1->dim),
8965                                                    isl_space_copy(bmap2->dim));
8966
8967         in1 = isl_basic_map_n_in(bmap1);
8968         in2 = isl_basic_map_n_in(bmap2);
8969         out1 = isl_basic_map_n_out(bmap1);
8970         out2 = isl_basic_map_n_out(bmap2);
8971         nparam = isl_basic_map_n_param(bmap1);
8972
8973         total = nparam + in1 + in2 + out1 + out2 + bmap1->n_div + bmap2->n_div;
8974         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
8975         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
8976         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
8977         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
8978         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
8979         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
8980         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
8981         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
8982         isl_dim_map_div(dim_map1, bmap1, pos += out2);
8983         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
8984
8985         bmap = isl_basic_map_alloc_space(dim_result,
8986                         bmap1->n_div + bmap2->n_div,
8987                         bmap1->n_eq + bmap2->n_eq,
8988                         bmap1->n_ineq + bmap2->n_ineq);
8989         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
8990         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
8991         bmap = isl_basic_map_simplify(bmap);
8992         return isl_basic_map_finalize(bmap);
8993 error:
8994         isl_basic_map_free(bmap1);
8995         isl_basic_map_free(bmap2);
8996         return NULL;
8997 }
8998
8999 __isl_give isl_basic_map *isl_basic_map_flat_product(
9000         __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9001 {
9002         isl_basic_map *prod;
9003
9004         prod = isl_basic_map_product(bmap1, bmap2);
9005         prod = isl_basic_map_flatten(prod);
9006         return prod;
9007 }
9008
9009 __isl_give isl_basic_set *isl_basic_set_flat_product(
9010         __isl_take isl_basic_set *bset1, __isl_take isl_basic_set *bset2)
9011 {
9012         return isl_basic_map_flat_range_product(bset1, bset2);
9013 }
9014
9015 __isl_give isl_basic_map *isl_basic_map_domain_product(
9016         __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9017 {
9018         isl_space *space_result = NULL;
9019         isl_basic_map *bmap;
9020         unsigned in1, in2, out, nparam, total, pos;
9021         struct isl_dim_map *dim_map1, *dim_map2;
9022
9023         if (!bmap1 || !bmap2)
9024                 goto error;
9025
9026         space_result = isl_space_domain_product(isl_space_copy(bmap1->dim),
9027                                                 isl_space_copy(bmap2->dim));
9028
9029         in1 = isl_basic_map_dim(bmap1, isl_dim_in);
9030         in2 = isl_basic_map_dim(bmap2, isl_dim_in);
9031         out = isl_basic_map_dim(bmap1, isl_dim_out);
9032         nparam = isl_basic_map_dim(bmap1, isl_dim_param);
9033
9034         total = nparam + in1 + in2 + out + bmap1->n_div + bmap2->n_div;
9035         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9036         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9037         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9038         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9039         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9040         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos += in1);
9041         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in2);
9042         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos);
9043         isl_dim_map_div(dim_map1, bmap1, pos += out);
9044         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9045
9046         bmap = isl_basic_map_alloc_space(space_result,
9047                         bmap1->n_div + bmap2->n_div,
9048                         bmap1->n_eq + bmap2->n_eq,
9049                         bmap1->n_ineq + bmap2->n_ineq);
9050         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9051         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9052         bmap = isl_basic_map_simplify(bmap);
9053         return isl_basic_map_finalize(bmap);
9054 error:
9055         isl_basic_map_free(bmap1);
9056         isl_basic_map_free(bmap2);
9057         return NULL;
9058 }
9059
9060 __isl_give isl_basic_map *isl_basic_map_range_product(
9061         __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9062 {
9063         isl_space *dim_result = NULL;
9064         isl_basic_map *bmap;
9065         unsigned in, out1, out2, nparam, total, pos;
9066         struct isl_dim_map *dim_map1, *dim_map2;
9067
9068         if (!bmap1 || !bmap2)
9069                 goto error;
9070
9071         if (!isl_space_match(bmap1->dim, isl_dim_param,
9072                             bmap2->dim, isl_dim_param))
9073                 isl_die(isl_basic_map_get_ctx(bmap1), isl_error_invalid,
9074                         "parameters don't match", goto error);
9075
9076         dim_result = isl_space_range_product(isl_space_copy(bmap1->dim),
9077                                            isl_space_copy(bmap2->dim));
9078
9079         in = isl_basic_map_dim(bmap1, isl_dim_in);
9080         out1 = isl_basic_map_n_out(bmap1);
9081         out2 = isl_basic_map_n_out(bmap2);
9082         nparam = isl_basic_map_n_param(bmap1);
9083
9084         total = nparam + in + out1 + out2 + bmap1->n_div + bmap2->n_div;
9085         dim_map1 = isl_dim_map_alloc(bmap1->ctx, total);
9086         dim_map2 = isl_dim_map_alloc(bmap1->ctx, total);
9087         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_param, pos = 0);
9088         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_param, pos = 0);
9089         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_in, pos += nparam);
9090         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_in, pos);
9091         isl_dim_map_dim(dim_map1, bmap1->dim, isl_dim_out, pos += in);
9092         isl_dim_map_dim(dim_map2, bmap2->dim, isl_dim_out, pos += out1);
9093         isl_dim_map_div(dim_map1, bmap1, pos += out2);
9094         isl_dim_map_div(dim_map2, bmap2, pos += bmap1->n_div);
9095
9096         bmap = isl_basic_map_alloc_space(dim_result,
9097                         bmap1->n_div + bmap2->n_div,
9098                         bmap1->n_eq + bmap2->n_eq,
9099                         bmap1->n_ineq + bmap2->n_ineq);
9100         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap1, dim_map1);
9101         bmap = isl_basic_map_add_constraints_dim_map(bmap, bmap2, dim_map2);
9102         bmap = isl_basic_map_simplify(bmap);
9103         return isl_basic_map_finalize(bmap);
9104 error:
9105         isl_basic_map_free(bmap1);
9106         isl_basic_map_free(bmap2);
9107         return NULL;
9108 }
9109
9110 __isl_give isl_basic_map *isl_basic_map_flat_range_product(
9111         __isl_take isl_basic_map *bmap1, __isl_take isl_basic_map *bmap2)
9112 {
9113         isl_basic_map *prod;
9114
9115         prod = isl_basic_map_range_product(bmap1, bmap2);
9116         prod = isl_basic_map_flatten_range(prod);
9117         return prod;
9118 }
9119
9120 static __isl_give isl_map *map_product(__isl_take isl_map *map1,
9121         __isl_take isl_map *map2,
9122         __isl_give isl_space *(*dim_product)(__isl_take isl_space *left,
9123                                            __isl_take isl_space *right),
9124         __isl_give isl_basic_map *(*basic_map_product)(
9125                 __isl_take isl_basic_map *left, __isl_take isl_basic_map *right))
9126 {
9127         unsigned flags = 0;
9128         struct isl_map *result;
9129         int i, j;
9130
9131         if (!map1 || !map2)
9132                 goto error;
9133
9134         isl_assert(map1->ctx, isl_space_match(map1->dim, isl_dim_param,
9135                                          map2->dim, isl_dim_param), goto error);
9136
9137         if (ISL_F_ISSET(map1, ISL_MAP_DISJOINT) &&
9138             ISL_F_ISSET(map2, ISL_MAP_DISJOINT))
9139                 ISL_FL_SET(flags, ISL_MAP_DISJOINT);
9140
9141         result = isl_map_alloc_space(dim_product(isl_space_copy(map1->dim),
9142                                                isl_space_copy(map2->dim)),
9143                                 map1->n * map2->n, flags);
9144         if (!result)
9145                 goto error;
9146         for (i = 0; i < map1->n; ++i)
9147                 for (j = 0; j < map2->n; ++j) {
9148                         struct isl_basic_map *part;
9149                         part = basic_map_product(isl_basic_map_copy(map1->p[i]),
9150                                                  isl_basic_map_copy(map2->p[j]));
9151                         if (isl_basic_map_is_empty(part))
9152                                 isl_basic_map_free(part);
9153                         else
9154                                 result = isl_map_add_basic_map(result, part);
9155                         if (!result)
9156                                 goto error;
9157                 }
9158         isl_map_free(map1);
9159         isl_map_free(map2);
9160         return result;
9161 error:
9162         isl_map_free(map1);
9163         isl_map_free(map2);
9164         return NULL;
9165 }
9166
9167 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> [B -> D]
9168  */
9169 static __isl_give isl_map *map_product_aligned(__isl_take isl_map *map1,
9170         __isl_take isl_map *map2)
9171 {
9172         return map_product(map1, map2, &isl_space_product, &isl_basic_map_product);
9173 }
9174
9175 __isl_give isl_map *isl_map_product(__isl_take isl_map *map1,
9176         __isl_take isl_map *map2)
9177 {
9178         return isl_map_align_params_map_map_and(map1, map2, &map_product_aligned);
9179 }
9180
9181 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B, D)
9182  */
9183 __isl_give isl_map *isl_map_flat_product(__isl_take isl_map *map1,
9184         __isl_take isl_map *map2)
9185 {
9186         isl_map *prod;
9187
9188         prod = isl_map_product(map1, map2);
9189         prod = isl_map_flatten(prod);
9190         return prod;
9191 }
9192
9193 /* Given two set A and B, construct its Cartesian product A x B.
9194  */
9195 struct isl_set *isl_set_product(struct isl_set *set1, struct isl_set *set2)
9196 {
9197         return isl_map_range_product(set1, set2);
9198 }
9199
9200 __isl_give isl_set *isl_set_flat_product(__isl_take isl_set *set1,
9201         __isl_take isl_set *set2)
9202 {
9203         return isl_map_flat_range_product(set1, set2);
9204 }
9205
9206 /* Given two maps A -> B and C -> D, construct a map [A -> C] -> (B * D)
9207  */
9208 static __isl_give isl_map *map_domain_product_aligned(__isl_take isl_map *map1,
9209         __isl_take isl_map *map2)
9210 {
9211         return map_product(map1, map2, &isl_space_domain_product,
9212                                 &isl_basic_map_domain_product);
9213 }
9214
9215 /* Given two maps A -> B and C -> D, construct a map (A * C) -> [B -> D]
9216  */
9217 static __isl_give isl_map *map_range_product_aligned(__isl_take isl_map *map1,
9218         __isl_take isl_map *map2)
9219 {
9220         return map_product(map1, map2, &isl_space_range_product,
9221                                 &isl_basic_map_range_product);
9222 }
9223
9224 __isl_give isl_map *isl_map_domain_product(__isl_take isl_map *map1,
9225         __isl_take isl_map *map2)
9226 {
9227         return isl_map_align_params_map_map_and(map1, map2,
9228                                                 &map_domain_product_aligned);
9229 }
9230
9231 __isl_give isl_map *isl_map_range_product(__isl_take isl_map *map1,
9232         __isl_take isl_map *map2)
9233 {
9234         return isl_map_align_params_map_map_and(map1, map2,
9235                                                 &map_range_product_aligned);
9236 }
9237
9238 /* Given two maps A -> B and C -> D, construct a map (A, C) -> (B * D)
9239  */
9240 __isl_give isl_map *isl_map_flat_domain_product(__isl_take isl_map *map1,
9241         __isl_take isl_map *map2)
9242 {
9243         isl_map *prod;
9244
9245         prod = isl_map_domain_product(map1, map2);
9246         prod = isl_map_flatten_domain(prod);
9247         return prod;
9248 }
9249
9250 /* Given two maps A -> B and C -> D, construct a map (A * C) -> (B, D)
9251  */
9252 __isl_give isl_map *isl_map_flat_range_product(__isl_take isl_map *map1,
9253         __isl_take isl_map *map2)
9254 {
9255         isl_map *prod;
9256
9257         prod = isl_map_range_product(map1, map2);
9258         prod = isl_map_flatten_range(prod);
9259         return prod;
9260 }
9261
9262 uint32_t isl_basic_map_get_hash(__isl_keep isl_basic_map *bmap)
9263 {
9264         int i;
9265         uint32_t hash = isl_hash_init();
9266         unsigned total;
9267
9268         if (!bmap)
9269                 return 0;
9270         bmap = isl_basic_map_copy(bmap);
9271         bmap = isl_basic_map_normalize(bmap);
9272         if (!bmap)
9273                 return 0;
9274         total = isl_basic_map_total_dim(bmap);
9275         isl_hash_byte(hash, bmap->n_eq & 0xFF);
9276         for (i = 0; i < bmap->n_eq; ++i) {
9277                 uint32_t c_hash;
9278                 c_hash = isl_seq_get_hash(bmap->eq[i], 1 + total);
9279                 isl_hash_hash(hash, c_hash);
9280         }
9281         isl_hash_byte(hash, bmap->n_ineq & 0xFF);
9282         for (i = 0; i < bmap->n_ineq; ++i) {
9283                 uint32_t c_hash;
9284                 c_hash = isl_seq_get_hash(bmap->ineq[i], 1 + total);
9285                 isl_hash_hash(hash, c_hash);
9286         }
9287         isl_hash_byte(hash, bmap->n_div & 0xFF);
9288         for (i = 0; i < bmap->n_div; ++i) {
9289                 uint32_t c_hash;
9290                 if (isl_int_is_zero(bmap->div[i][0]))
9291                         continue;
9292                 isl_hash_byte(hash, i & 0xFF);
9293                 c_hash = isl_seq_get_hash(bmap->div[i], 1 + 1 + total);
9294                 isl_hash_hash(hash, c_hash);
9295         }
9296         isl_basic_map_free(bmap);
9297         return hash;
9298 }
9299
9300 uint32_t isl_basic_set_get_hash(__isl_keep isl_basic_set *bset)
9301 {
9302         return isl_basic_map_get_hash((isl_basic_map *)bset);
9303 }
9304
9305 uint32_t isl_map_get_hash(__isl_keep isl_map *map)
9306 {
9307         int i;
9308         uint32_t hash;
9309
9310         if (!map)
9311                 return 0;
9312         map = isl_map_copy(map);
9313         map = isl_map_normalize(map);
9314         if (!map)
9315                 return 0;
9316
9317         hash = isl_hash_init();
9318         for (i = 0; i < map->n; ++i) {
9319                 uint32_t bmap_hash;
9320                 bmap_hash = isl_basic_map_get_hash(map->p[i]);
9321                 isl_hash_hash(hash, bmap_hash);
9322         }
9323                 
9324         isl_map_free(map);
9325
9326         return hash;
9327 }
9328
9329 uint32_t isl_set_get_hash(__isl_keep isl_set *set)
9330 {
9331         return isl_map_get_hash((isl_map *)set);
9332 }
9333
9334 /* Check if the value for dimension dim is completely determined
9335  * by the values of the other parameters and variables.
9336  * That is, check if dimension dim is involved in an equality.
9337  */
9338 int isl_basic_set_dim_is_unique(struct isl_basic_set *bset, unsigned dim)
9339 {
9340         int i;
9341         unsigned nparam;
9342
9343         if (!bset)
9344                 return -1;
9345         nparam = isl_basic_set_n_param(bset);
9346         for (i = 0; i < bset->n_eq; ++i)
9347                 if (!isl_int_is_zero(bset->eq[i][1 + nparam + dim]))
9348                         return 1;
9349         return 0;
9350 }
9351
9352 /* Check if the value for dimension dim is completely determined
9353  * by the values of the other parameters and variables.
9354  * That is, check if dimension dim is involved in an equality
9355  * for each of the subsets.
9356  */
9357 int isl_set_dim_is_unique(struct isl_set *set, unsigned dim)
9358 {
9359         int i;
9360
9361         if (!set)
9362                 return -1;
9363         for (i = 0; i < set->n; ++i) {
9364                 int unique;
9365                 unique = isl_basic_set_dim_is_unique(set->p[i], dim);
9366                 if (unique != 1)
9367                         return unique;
9368         }
9369         return 1;
9370 }
9371
9372 int isl_set_n_basic_set(__isl_keep isl_set *set)
9373 {
9374         return set ? set->n : 0;
9375 }
9376
9377 int isl_map_foreach_basic_map(__isl_keep isl_map *map,
9378         int (*fn)(__isl_take isl_basic_map *bmap, void *user), void *user)
9379 {
9380         int i;
9381
9382         if (!map)
9383                 return -1;
9384
9385         for (i = 0; i < map->n; ++i)
9386                 if (fn(isl_basic_map_copy(map->p[i]), user) < 0)
9387                         return -1;
9388
9389         return 0;
9390 }
9391
9392 int isl_set_foreach_basic_set(__isl_keep isl_set *set,
9393         int (*fn)(__isl_take isl_basic_set *bset, void *user), void *user)
9394 {
9395         int i;
9396
9397         if (!set)
9398                 return -1;
9399
9400         for (i = 0; i < set->n; ++i)
9401                 if (fn(isl_basic_set_copy(set->p[i]), user) < 0)
9402                         return -1;
9403
9404         return 0;
9405 }
9406
9407 __isl_give isl_basic_set *isl_basic_set_lift(__isl_take isl_basic_set *bset)
9408 {
9409         isl_space *dim;
9410
9411         if (!bset)
9412                 return NULL;
9413
9414         bset = isl_basic_set_cow(bset);
9415         if (!bset)
9416                 return NULL;
9417
9418         dim = isl_basic_set_get_space(bset);
9419         dim = isl_space_lift(dim, bset->n_div);
9420         if (!dim)
9421                 goto error;
9422         isl_space_free(bset->dim);
9423         bset->dim = dim;
9424         bset->extra -= bset->n_div;
9425         bset->n_div = 0;
9426
9427         bset = isl_basic_set_finalize(bset);
9428
9429         return bset;
9430 error:
9431         isl_basic_set_free(bset);
9432         return NULL;
9433 }
9434
9435 __isl_give isl_set *isl_set_lift(__isl_take isl_set *set)
9436 {
9437         int i;
9438         isl_space *dim;
9439         unsigned n_div;
9440
9441         set = isl_set_align_divs(set);
9442
9443         if (!set)
9444                 return NULL;
9445
9446         set = isl_set_cow(set);
9447         if (!set)
9448                 return NULL;
9449
9450         n_div = set->p[0]->n_div;
9451         dim = isl_set_get_space(set);
9452         dim = isl_space_lift(dim, n_div);
9453         if (!dim)
9454                 goto error;
9455         isl_space_free(set->dim);
9456         set->dim = dim;
9457
9458         for (i = 0; i < set->n; ++i) {
9459                 set->p[i] = isl_basic_set_lift(set->p[i]);
9460                 if (!set->p[i])
9461                         goto error;
9462         }
9463
9464         return set;
9465 error:
9466         isl_set_free(set);
9467         return NULL;
9468 }
9469
9470 __isl_give isl_map *isl_set_lifting(__isl_take isl_set *set)
9471 {
9472         isl_space *dim;
9473         struct isl_basic_map *bmap;
9474         unsigned n_set;
9475         unsigned n_div;
9476         unsigned n_param;
9477         unsigned total;
9478         int i, k, l;
9479
9480         set = isl_set_align_divs(set);
9481
9482         if (!set)
9483                 return NULL;
9484
9485         dim = isl_set_get_space(set);
9486         if (set->n == 0 || set->p[0]->n_div == 0) {
9487                 isl_set_free(set);
9488                 return isl_map_identity(isl_space_map_from_set(dim));
9489         }
9490
9491         n_div = set->p[0]->n_div;
9492         dim = isl_space_map_from_set(dim);
9493         n_param = isl_space_dim(dim, isl_dim_param);
9494         n_set = isl_space_dim(dim, isl_dim_in);
9495         dim = isl_space_extend(dim, n_param, n_set, n_set + n_div);
9496         bmap = isl_basic_map_alloc_space(dim, 0, n_set, 2 * n_div);
9497         for (i = 0; i < n_set; ++i)
9498                 bmap = var_equal(bmap, i);
9499
9500         total = n_param + n_set + n_set + n_div;
9501         for (i = 0; i < n_div; ++i) {
9502                 k = isl_basic_map_alloc_inequality(bmap);
9503                 if (k < 0)
9504                         goto error;
9505                 isl_seq_cpy(bmap->ineq[k], set->p[0]->div[i]+1, 1+n_param);
9506                 isl_seq_clr(bmap->ineq[k]+1+n_param, n_set);
9507                 isl_seq_cpy(bmap->ineq[k]+1+n_param+n_set,
9508                             set->p[0]->div[i]+1+1+n_param, n_set + n_div);
9509                 isl_int_neg(bmap->ineq[k][1+n_param+n_set+n_set+i],
9510                             set->p[0]->div[i][0]);
9511
9512                 l = isl_basic_map_alloc_inequality(bmap);
9513                 if (l < 0)
9514                         goto error;
9515                 isl_seq_neg(bmap->ineq[l], bmap->ineq[k], 1 + total);
9516                 isl_int_add(bmap->ineq[l][0], bmap->ineq[l][0],
9517                             set->p[0]->div[i][0]);
9518                 isl_int_sub_ui(bmap->ineq[l][0], bmap->ineq[l][0], 1);
9519         }
9520
9521         isl_set_free(set);
9522         bmap = isl_basic_map_simplify(bmap);
9523         bmap = isl_basic_map_finalize(bmap);
9524         return isl_map_from_basic_map(bmap);
9525 error:
9526         isl_set_free(set);
9527         isl_basic_map_free(bmap);
9528         return NULL;
9529 }
9530
9531 int isl_basic_set_size(__isl_keep isl_basic_set *bset)
9532 {
9533         unsigned dim;
9534         int size = 0;
9535
9536         if (!bset)
9537                 return -1;
9538
9539         dim = isl_basic_set_total_dim(bset);
9540         size += bset->n_eq * (1 + dim);
9541         size += bset->n_ineq * (1 + dim);
9542         size += bset->n_div * (2 + dim);
9543
9544         return size;
9545 }
9546
9547 int isl_set_size(__isl_keep isl_set *set)
9548 {
9549         int i;
9550         int size = 0;
9551
9552         if (!set)
9553                 return -1;
9554
9555         for (i = 0; i < set->n; ++i)
9556                 size += isl_basic_set_size(set->p[i]);
9557
9558         return size;
9559 }
9560
9561 /* Check if there is any lower bound (if lower == 0) and/or upper
9562  * bound (if upper == 0) on the specified dim.
9563  */
9564 static int basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9565         enum isl_dim_type type, unsigned pos, int lower, int upper)
9566 {
9567         int i;
9568
9569         if (!bmap)
9570                 return -1;
9571
9572         isl_assert(bmap->ctx, pos < isl_basic_map_dim(bmap, type), return -1);
9573
9574         pos += isl_basic_map_offset(bmap, type);
9575
9576         for (i = 0; i < bmap->n_div; ++i) {
9577                 if (isl_int_is_zero(bmap->div[i][0]))
9578                         continue;
9579                 if (!isl_int_is_zero(bmap->div[i][1 + pos]))
9580                         return 1;
9581         }
9582
9583         for (i = 0; i < bmap->n_eq; ++i)
9584                 if (!isl_int_is_zero(bmap->eq[i][pos]))
9585                         return 1;
9586
9587         for (i = 0; i < bmap->n_ineq; ++i) {
9588                 int sgn = isl_int_sgn(bmap->ineq[i][pos]);
9589                 if (sgn > 0)
9590                         lower = 1;
9591                 if (sgn < 0)
9592                         upper = 1;
9593         }
9594
9595         return lower && upper;
9596 }
9597
9598 int isl_basic_map_dim_is_bounded(__isl_keep isl_basic_map *bmap,
9599         enum isl_dim_type type, unsigned pos)
9600 {
9601         return basic_map_dim_is_bounded(bmap, type, pos, 0, 0);
9602 }
9603
9604 int isl_basic_map_dim_has_lower_bound(__isl_keep isl_basic_map *bmap,
9605         enum isl_dim_type type, unsigned pos)
9606 {
9607         return basic_map_dim_is_bounded(bmap, type, pos, 0, 1);
9608 }
9609
9610 int isl_basic_map_dim_has_upper_bound(__isl_keep isl_basic_map *bmap,
9611         enum isl_dim_type type, unsigned pos)
9612 {
9613         return basic_map_dim_is_bounded(bmap, type, pos, 1, 0);
9614 }
9615
9616 int isl_map_dim_is_bounded(__isl_keep isl_map *map,
9617         enum isl_dim_type type, unsigned pos)
9618 {
9619         int i;
9620
9621         if (!map)
9622                 return -1;
9623
9624         for (i = 0; i < map->n; ++i) {
9625                 int bounded;
9626                 bounded = isl_basic_map_dim_is_bounded(map->p[i], type, pos);
9627                 if (bounded < 0 || !bounded)
9628                         return bounded;
9629         }
9630
9631         return 1;
9632 }
9633
9634 /* Return 1 if the specified dim is involved in both an upper bound
9635  * and a lower bound.
9636  */
9637 int isl_set_dim_is_bounded(__isl_keep isl_set *set,
9638         enum isl_dim_type type, unsigned pos)
9639 {
9640         return isl_map_dim_is_bounded((isl_map *)set, type, pos);
9641 }
9642
9643 /* Does "map" have a bound (according to "fn") for any of its basic maps?
9644  */
9645 static int has_any_bound(__isl_keep isl_map *map,
9646         enum isl_dim_type type, unsigned pos,
9647         int (*fn)(__isl_keep isl_basic_map *bmap,
9648                   enum isl_dim_type type, unsigned pos))
9649 {
9650         int i;
9651
9652         if (!map)
9653                 return -1;
9654
9655         for (i = 0; i < map->n; ++i) {
9656                 int bounded;
9657                 bounded = fn(map->p[i], type, pos);
9658                 if (bounded < 0 || bounded)
9659                         return bounded;
9660         }
9661
9662         return 0;
9663 }
9664
9665 /* Return 1 if the specified dim is involved in any lower bound.
9666  */
9667 int isl_set_dim_has_any_lower_bound(__isl_keep isl_set *set,
9668         enum isl_dim_type type, unsigned pos)
9669 {
9670         return has_any_bound(set, type, pos,
9671                                 &isl_basic_map_dim_has_lower_bound);
9672 }
9673
9674 /* Return 1 if the specified dim is involved in any upper bound.
9675  */
9676 int isl_set_dim_has_any_upper_bound(__isl_keep isl_set *set,
9677         enum isl_dim_type type, unsigned pos)
9678 {
9679         return has_any_bound(set, type, pos,
9680                                 &isl_basic_map_dim_has_upper_bound);
9681 }
9682
9683 /* Does "map" have a bound (according to "fn") for all of its basic maps?
9684  */
9685 static int has_bound(__isl_keep isl_map *map,
9686         enum isl_dim_type type, unsigned pos,
9687         int (*fn)(__isl_keep isl_basic_map *bmap,
9688                   enum isl_dim_type type, unsigned pos))
9689 {
9690         int i;
9691
9692         if (!map)
9693                 return -1;
9694
9695         for (i = 0; i < map->n; ++i) {
9696                 int bounded;
9697                 bounded = fn(map->p[i], type, pos);
9698                 if (bounded < 0 || !bounded)
9699                         return bounded;
9700         }
9701
9702         return 1;
9703 }
9704
9705 /* Return 1 if the specified dim has a lower bound (in each of its basic sets).
9706  */
9707 int isl_set_dim_has_lower_bound(__isl_keep isl_set *set,
9708         enum isl_dim_type type, unsigned pos)
9709 {
9710         return has_bound(set, type, pos, &isl_basic_map_dim_has_lower_bound);
9711 }
9712
9713 /* Return 1 if the specified dim has an upper bound (in each of its basic sets).
9714  */
9715 int isl_set_dim_has_upper_bound(__isl_keep isl_set *set,
9716         enum isl_dim_type type, unsigned pos)
9717 {
9718         return has_bound(set, type, pos, &isl_basic_map_dim_has_upper_bound);
9719 }
9720
9721 /* For each of the "n" variables starting at "first", determine
9722  * the sign of the variable and put the results in the first "n"
9723  * elements of the array "signs".
9724  * Sign
9725  *      1 means that the variable is non-negative
9726  *      -1 means that the variable is non-positive
9727  *      0 means the variable attains both positive and negative values.
9728  */
9729 int isl_basic_set_vars_get_sign(__isl_keep isl_basic_set *bset,
9730         unsigned first, unsigned n, int *signs)
9731 {
9732         isl_vec *bound = NULL;
9733         struct isl_tab *tab = NULL;
9734         struct isl_tab_undo *snap;
9735         int i;
9736
9737         if (!bset || !signs)
9738                 return -1;
9739
9740         bound = isl_vec_alloc(bset->ctx, 1 + isl_basic_set_total_dim(bset));
9741         tab = isl_tab_from_basic_set(bset, 0);
9742         if (!bound || !tab)
9743                 goto error;
9744
9745         isl_seq_clr(bound->el, bound->size);
9746         isl_int_set_si(bound->el[0], -1);
9747
9748         snap = isl_tab_snap(tab);
9749         for (i = 0; i < n; ++i) {
9750                 int empty;
9751
9752                 isl_int_set_si(bound->el[1 + first + i], -1);
9753                 if (isl_tab_add_ineq(tab, bound->el) < 0)
9754                         goto error;
9755                 empty = tab->empty;
9756                 isl_int_set_si(bound->el[1 + first + i], 0);
9757                 if (isl_tab_rollback(tab, snap) < 0)
9758                         goto error;
9759
9760                 if (empty) {
9761                         signs[i] = 1;
9762                         continue;
9763                 }
9764
9765                 isl_int_set_si(bound->el[1 + first + i], 1);
9766                 if (isl_tab_add_ineq(tab, bound->el) < 0)
9767                         goto error;
9768                 empty = tab->empty;
9769                 isl_int_set_si(bound->el[1 + first + i], 0);
9770                 if (isl_tab_rollback(tab, snap) < 0)
9771                         goto error;
9772
9773                 signs[i] = empty ? -1 : 0;
9774         }
9775
9776         isl_tab_free(tab);
9777         isl_vec_free(bound);
9778         return 0;
9779 error:
9780         isl_tab_free(tab);
9781         isl_vec_free(bound);
9782         return -1;
9783 }
9784
9785 int isl_basic_set_dims_get_sign(__isl_keep isl_basic_set *bset,
9786         enum isl_dim_type type, unsigned first, unsigned n, int *signs)
9787 {
9788         if (!bset || !signs)
9789                 return -1;
9790         isl_assert(bset->ctx, first + n <= isl_basic_set_dim(bset, type),
9791                 return -1);
9792
9793         first += pos(bset->dim, type) - 1;
9794         return isl_basic_set_vars_get_sign(bset, first, n, signs);
9795 }
9796
9797 /* Check if the given basic map is obviously single-valued.
9798  * In particular, for each output dimension, check that there is
9799  * an equality that defines the output dimension in terms of
9800  * earlier dimensions.
9801  */
9802 int isl_basic_map_plain_is_single_valued(__isl_keep isl_basic_map *bmap)
9803 {
9804         int i, j;
9805         unsigned total;
9806         unsigned n_out;
9807         unsigned o_out;
9808
9809         if (!bmap)
9810                 return -1;
9811
9812         total = 1 + isl_basic_map_total_dim(bmap);
9813         n_out = isl_basic_map_dim(bmap, isl_dim_out);
9814         o_out = isl_basic_map_offset(bmap, isl_dim_out);
9815
9816         for (i = 0; i < n_out; ++i) {
9817                 for (j = 0; j < bmap->n_eq; ++j) {
9818                         if (isl_int_is_zero(bmap->eq[j][o_out + i]))
9819                                 continue;
9820                         if (isl_seq_first_non_zero(bmap->eq[j] + o_out + i + 1,
9821                                                 total - (o_out + i + 1)) == -1)
9822                                 break;
9823                 }
9824                 if (j >= bmap->n_eq)
9825                         return 0;
9826         }
9827
9828         return 1;
9829 }
9830
9831 /* Check if the given basic map is single-valued.
9832  * We simply compute
9833  *
9834  *      M \circ M^-1
9835  *
9836  * and check if the result is a subset of the identity mapping.
9837  */
9838 int isl_basic_map_is_single_valued(__isl_keep isl_basic_map *bmap)
9839 {
9840         isl_space *space;
9841         isl_basic_map *test;
9842         isl_basic_map *id;
9843         int sv;
9844
9845         sv = isl_basic_map_plain_is_single_valued(bmap);
9846         if (sv < 0 || sv)
9847                 return sv;
9848
9849         test = isl_basic_map_reverse(isl_basic_map_copy(bmap));
9850         test = isl_basic_map_apply_range(test, isl_basic_map_copy(bmap));
9851
9852         space = isl_basic_map_get_space(bmap);
9853         space = isl_space_map_from_set(isl_space_range(space));
9854         id = isl_basic_map_identity(space);
9855
9856         sv = isl_basic_map_is_subset(test, id);
9857
9858         isl_basic_map_free(test);
9859         isl_basic_map_free(id);
9860
9861         return sv;
9862 }
9863
9864 /* Check if the given map is obviously single-valued.
9865  */
9866 int isl_map_plain_is_single_valued(__isl_keep isl_map *map)
9867 {
9868         if (!map)
9869                 return -1;
9870         if (map->n == 0)
9871                 return 1;
9872         if (map->n >= 2)
9873                 return 0;
9874
9875         return isl_basic_map_plain_is_single_valued(map->p[0]);
9876 }
9877
9878 /* Check if the given map is single-valued.
9879  * We simply compute
9880  *
9881  *      M \circ M^-1
9882  *
9883  * and check if the result is a subset of the identity mapping.
9884  */
9885 int isl_map_is_single_valued(__isl_keep isl_map *map)
9886 {
9887         isl_space *dim;
9888         isl_map *test;
9889         isl_map *id;
9890         int sv;
9891
9892         sv = isl_map_plain_is_single_valued(map);
9893         if (sv < 0 || sv)
9894                 return sv;
9895
9896         test = isl_map_reverse(isl_map_copy(map));
9897         test = isl_map_apply_range(test, isl_map_copy(map));
9898
9899         dim = isl_space_map_from_set(isl_space_range(isl_map_get_space(map)));
9900         id = isl_map_identity(dim);
9901
9902         sv = isl_map_is_subset(test, id);
9903
9904         isl_map_free(test);
9905         isl_map_free(id);
9906
9907         return sv;
9908 }
9909
9910 int isl_map_is_injective(__isl_keep isl_map *map)
9911 {
9912         int in;
9913
9914         map = isl_map_copy(map);
9915         map = isl_map_reverse(map);
9916         in = isl_map_is_single_valued(map);
9917         isl_map_free(map);
9918
9919         return in;
9920 }
9921
9922 /* Check if the given map is obviously injective.
9923  */
9924 int isl_map_plain_is_injective(__isl_keep isl_map *map)
9925 {
9926         int in;
9927
9928         map = isl_map_copy(map);
9929         map = isl_map_reverse(map);
9930         in = isl_map_plain_is_single_valued(map);
9931         isl_map_free(map);
9932
9933         return in;
9934 }
9935
9936 int isl_map_is_bijective(__isl_keep isl_map *map)
9937 {
9938         int sv;
9939
9940         sv = isl_map_is_single_valued(map);
9941         if (sv < 0 || !sv)
9942                 return sv;
9943
9944         return isl_map_is_injective(map);
9945 }
9946
9947 int isl_set_is_singleton(__isl_keep isl_set *set)
9948 {
9949         return isl_map_is_single_valued((isl_map *)set);
9950 }
9951
9952 int isl_map_is_translation(__isl_keep isl_map *map)
9953 {
9954         int ok;
9955         isl_set *delta;
9956
9957         delta = isl_map_deltas(isl_map_copy(map));
9958         ok = isl_set_is_singleton(delta);
9959         isl_set_free(delta);
9960
9961         return ok;
9962 }
9963
9964 static int unique(isl_int *p, unsigned pos, unsigned len)
9965 {
9966         if (isl_seq_first_non_zero(p, pos) != -1)
9967                 return 0;
9968         if (isl_seq_first_non_zero(p + pos + 1, len - pos - 1) != -1)
9969                 return 0;
9970         return 1;
9971 }
9972
9973 int isl_basic_set_is_box(__isl_keep isl_basic_set *bset)
9974 {
9975         int i, j;
9976         unsigned nvar;
9977         unsigned ovar;
9978
9979         if (!bset)
9980                 return -1;
9981
9982         if (isl_basic_set_dim(bset, isl_dim_div) != 0)
9983                 return 0;
9984
9985         nvar = isl_basic_set_dim(bset, isl_dim_set);
9986         ovar = isl_space_offset(bset->dim, isl_dim_set);
9987         for (j = 0; j < nvar; ++j) {
9988                 int lower = 0, upper = 0;
9989                 for (i = 0; i < bset->n_eq; ++i) {
9990                         if (isl_int_is_zero(bset->eq[i][1 + ovar + j]))
9991                                 continue;
9992                         if (!unique(bset->eq[i] + 1 + ovar, j, nvar))
9993                                 return 0;
9994                         break;
9995                 }
9996                 if (i < bset->n_eq)
9997                         continue;
9998                 for (i = 0; i < bset->n_ineq; ++i) {
9999                         if (isl_int_is_zero(bset->ineq[i][1 + ovar + j]))
10000                                 continue;
10001                         if (!unique(bset->ineq[i] + 1 + ovar, j, nvar))
10002                                 return 0;
10003                         if (isl_int_is_pos(bset->ineq[i][1 + ovar + j]))
10004                                 lower = 1;
10005                         else
10006                                 upper = 1;
10007                 }
10008                 if (!lower || !upper)
10009                         return 0;
10010         }
10011
10012         return 1;
10013 }
10014
10015 int isl_set_is_box(__isl_keep isl_set *set)
10016 {
10017         if (!set)
10018                 return -1;
10019         if (set->n != 1)
10020                 return 0;
10021
10022         return isl_basic_set_is_box(set->p[0]);
10023 }
10024
10025 int isl_basic_set_is_wrapping(__isl_keep isl_basic_set *bset)
10026 {
10027         if (!bset)
10028                 return -1;
10029         
10030         return isl_space_is_wrapping(bset->dim);
10031 }
10032
10033 int isl_set_is_wrapping(__isl_keep isl_set *set)
10034 {
10035         if (!set)
10036                 return -1;
10037         
10038         return isl_space_is_wrapping(set->dim);
10039 }
10040
10041 __isl_give isl_basic_set *isl_basic_map_wrap(__isl_take isl_basic_map *bmap)
10042 {
10043         bmap = isl_basic_map_cow(bmap);
10044         if (!bmap)
10045                 return NULL;
10046
10047         bmap->dim = isl_space_wrap(bmap->dim);
10048         if (!bmap->dim)
10049                 goto error;
10050
10051         bmap = isl_basic_map_finalize(bmap);
10052
10053         return (isl_basic_set *)bmap;
10054 error:
10055         isl_basic_map_free(bmap);
10056         return NULL;
10057 }
10058
10059 __isl_give isl_set *isl_map_wrap(__isl_take isl_map *map)
10060 {
10061         int i;
10062
10063         map = isl_map_cow(map);
10064         if (!map)
10065                 return NULL;
10066
10067         for (i = 0; i < map->n; ++i) {
10068                 map->p[i] = (isl_basic_map *)isl_basic_map_wrap(map->p[i]);
10069                 if (!map->p[i])
10070                         goto error;
10071         }
10072         map->dim = isl_space_wrap(map->dim);
10073         if (!map->dim)
10074                 goto error;
10075
10076         return (isl_set *)map;
10077 error:
10078         isl_map_free(map);
10079         return NULL;
10080 }
10081
10082 __isl_give isl_basic_map *isl_basic_set_unwrap(__isl_take isl_basic_set *bset)
10083 {
10084         bset = isl_basic_set_cow(bset);
10085         if (!bset)
10086                 return NULL;
10087
10088         bset->dim = isl_space_unwrap(bset->dim);
10089         if (!bset->dim)
10090                 goto error;
10091
10092         bset = isl_basic_set_finalize(bset);
10093
10094         return (isl_basic_map *)bset;
10095 error:
10096         isl_basic_set_free(bset);
10097         return NULL;
10098 }
10099
10100 __isl_give isl_map *isl_set_unwrap(__isl_take isl_set *set)
10101 {
10102         int i;
10103
10104         if (!set)
10105                 return NULL;
10106
10107         if (!isl_set_is_wrapping(set))
10108                 isl_die(set->ctx, isl_error_invalid, "not a wrapping set",
10109                         goto error);
10110
10111         set = isl_set_cow(set);
10112         if (!set)
10113                 return NULL;
10114
10115         for (i = 0; i < set->n; ++i) {
10116                 set->p[i] = (isl_basic_set *)isl_basic_set_unwrap(set->p[i]);
10117                 if (!set->p[i])
10118                         goto error;
10119         }
10120
10121         set->dim = isl_space_unwrap(set->dim);
10122         if (!set->dim)
10123                 goto error;
10124
10125         return (isl_map *)set;
10126 error:
10127         isl_set_free(set);
10128         return NULL;
10129 }
10130
10131 __isl_give isl_basic_map *isl_basic_map_reset(__isl_take isl_basic_map *bmap,
10132         enum isl_dim_type type)
10133 {
10134         if (!bmap)
10135                 return NULL;
10136
10137         if (!isl_space_is_named_or_nested(bmap->dim, type))
10138                 return bmap;
10139
10140         bmap = isl_basic_map_cow(bmap);
10141         if (!bmap)
10142                 return NULL;
10143
10144         bmap->dim = isl_space_reset(bmap->dim, type);
10145         if (!bmap->dim)
10146                 goto error;
10147
10148         bmap = isl_basic_map_finalize(bmap);
10149
10150         return bmap;
10151 error:
10152         isl_basic_map_free(bmap);
10153         return NULL;
10154 }
10155
10156 __isl_give isl_map *isl_map_reset(__isl_take isl_map *map,
10157         enum isl_dim_type type)
10158 {
10159         int i;
10160
10161         if (!map)
10162                 return NULL;
10163
10164         if (!isl_space_is_named_or_nested(map->dim, type))
10165                 return map;
10166
10167         map = isl_map_cow(map);
10168         if (!map)
10169                 return NULL;
10170
10171         for (i = 0; i < map->n; ++i) {
10172                 map->p[i] = isl_basic_map_reset(map->p[i], type);
10173                 if (!map->p[i])
10174                         goto error;
10175         }
10176         map->dim = isl_space_reset(map->dim, type);
10177         if (!map->dim)
10178                 goto error;
10179
10180         return map;
10181 error:
10182         isl_map_free(map);
10183         return NULL;
10184 }
10185
10186 __isl_give isl_basic_map *isl_basic_map_flatten(__isl_take isl_basic_map *bmap)
10187 {
10188         if (!bmap)
10189                 return NULL;
10190
10191         if (!bmap->dim->nested[0] && !bmap->dim->nested[1])
10192                 return bmap;
10193
10194         bmap = isl_basic_map_cow(bmap);
10195         if (!bmap)
10196                 return NULL;
10197
10198         bmap->dim = isl_space_flatten(bmap->dim);
10199         if (!bmap->dim)
10200                 goto error;
10201
10202         bmap = isl_basic_map_finalize(bmap);
10203
10204         return bmap;
10205 error:
10206         isl_basic_map_free(bmap);
10207         return NULL;
10208 }
10209
10210 __isl_give isl_basic_set *isl_basic_set_flatten(__isl_take isl_basic_set *bset)
10211 {
10212         return (isl_basic_set *)isl_basic_map_flatten((isl_basic_map *)bset);
10213 }
10214
10215 __isl_give isl_basic_map *isl_basic_map_flatten_domain(
10216         __isl_take isl_basic_map *bmap)
10217 {
10218         if (!bmap)
10219                 return NULL;
10220
10221         if (!bmap->dim->nested[0])
10222                 return bmap;
10223
10224         bmap = isl_basic_map_cow(bmap);
10225         if (!bmap)
10226                 return NULL;
10227
10228         bmap->dim = isl_space_flatten_domain(bmap->dim);
10229         if (!bmap->dim)
10230                 goto error;
10231
10232         bmap = isl_basic_map_finalize(bmap);
10233
10234         return bmap;
10235 error:
10236         isl_basic_map_free(bmap);
10237         return NULL;
10238 }
10239
10240 __isl_give isl_basic_map *isl_basic_map_flatten_range(
10241         __isl_take isl_basic_map *bmap)
10242 {
10243         if (!bmap)
10244                 return NULL;
10245
10246         if (!bmap->dim->nested[1])
10247                 return bmap;
10248
10249         bmap = isl_basic_map_cow(bmap);
10250         if (!bmap)
10251                 return NULL;
10252
10253         bmap->dim = isl_space_flatten_range(bmap->dim);
10254         if (!bmap->dim)
10255                 goto error;
10256
10257         bmap = isl_basic_map_finalize(bmap);
10258
10259         return bmap;
10260 error:
10261         isl_basic_map_free(bmap);
10262         return NULL;
10263 }
10264
10265 __isl_give isl_map *isl_map_flatten(__isl_take isl_map *map)
10266 {
10267         int i;
10268
10269         if (!map)
10270                 return NULL;
10271
10272         if (!map->dim->nested[0] && !map->dim->nested[1])
10273                 return map;
10274
10275         map = isl_map_cow(map);
10276         if (!map)
10277                 return NULL;
10278
10279         for (i = 0; i < map->n; ++i) {
10280                 map->p[i] = isl_basic_map_flatten(map->p[i]);
10281                 if (!map->p[i])
10282                         goto error;
10283         }
10284         map->dim = isl_space_flatten(map->dim);
10285         if (!map->dim)
10286                 goto error;
10287
10288         return map;
10289 error:
10290         isl_map_free(map);
10291         return NULL;
10292 }
10293
10294 __isl_give isl_set *isl_set_flatten(__isl_take isl_set *set)
10295 {
10296         return (isl_set *)isl_map_flatten((isl_map *)set);
10297 }
10298
10299 __isl_give isl_map *isl_set_flatten_map(__isl_take isl_set *set)
10300 {
10301         isl_space *dim, *flat_dim;
10302         isl_map *map;
10303
10304         dim = isl_set_get_space(set);
10305         flat_dim = isl_space_flatten(isl_space_copy(dim));
10306         map = isl_map_identity(isl_space_join(isl_space_reverse(dim), flat_dim));
10307         map = isl_map_intersect_domain(map, set);
10308
10309         return map;
10310 }
10311
10312 __isl_give isl_map *isl_map_flatten_domain(__isl_take isl_map *map)
10313 {
10314         int i;
10315
10316         if (!map)
10317                 return NULL;
10318
10319         if (!map->dim->nested[0])
10320                 return map;
10321
10322         map = isl_map_cow(map);
10323         if (!map)
10324                 return NULL;
10325
10326         for (i = 0; i < map->n; ++i) {
10327                 map->p[i] = isl_basic_map_flatten_domain(map->p[i]);
10328                 if (!map->p[i])
10329                         goto error;
10330         }
10331         map->dim = isl_space_flatten_domain(map->dim);
10332         if (!map->dim)
10333                 goto error;
10334
10335         return map;
10336 error:
10337         isl_map_free(map);
10338         return NULL;
10339 }
10340
10341 __isl_give isl_map *isl_map_flatten_range(__isl_take isl_map *map)
10342 {
10343         int i;
10344
10345         if (!map)
10346                 return NULL;
10347
10348         if (!map->dim->nested[1])
10349                 return map;
10350
10351         map = isl_map_cow(map);
10352         if (!map)
10353                 return NULL;
10354
10355         for (i = 0; i < map->n; ++i) {
10356                 map->p[i] = isl_basic_map_flatten_range(map->p[i]);
10357                 if (!map->p[i])
10358                         goto error;
10359         }
10360         map->dim = isl_space_flatten_range(map->dim);
10361         if (!map->dim)
10362                 goto error;
10363
10364         return map;
10365 error:
10366         isl_map_free(map);
10367         return NULL;
10368 }
10369
10370 /* Reorder the dimensions of "bmap" according to the given dim_map
10371  * and set the dimension specification to "dim".
10372  */
10373 __isl_give isl_basic_map *isl_basic_map_realign(__isl_take isl_basic_map *bmap,
10374         __isl_take isl_space *dim, __isl_take struct isl_dim_map *dim_map)
10375 {
10376         isl_basic_map *res;
10377         unsigned flags;
10378
10379         bmap = isl_basic_map_cow(bmap);
10380         if (!bmap || !dim || !dim_map)
10381                 goto error;
10382
10383         flags = bmap->flags;
10384         ISL_FL_CLR(flags, ISL_BASIC_MAP_FINAL);
10385         ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED);
10386         ISL_FL_CLR(flags, ISL_BASIC_MAP_NORMALIZED_DIVS);
10387         res = isl_basic_map_alloc_space(dim,
10388                         bmap->n_div, bmap->n_eq, bmap->n_ineq);
10389         res = isl_basic_map_add_constraints_dim_map(res, bmap, dim_map);
10390         if (res)
10391                 res->flags = flags;
10392         res = isl_basic_map_finalize(res);
10393         return res;
10394 error:
10395         free(dim_map);
10396         isl_basic_map_free(bmap);
10397         isl_space_free(dim);
10398         return NULL;
10399 }
10400
10401 /* Reorder the dimensions of "map" according to given reordering.
10402  */
10403 __isl_give isl_map *isl_map_realign(__isl_take isl_map *map,
10404         __isl_take isl_reordering *r)
10405 {
10406         int i;
10407         struct isl_dim_map *dim_map;
10408
10409         map = isl_map_cow(map);
10410         dim_map = isl_dim_map_from_reordering(r);
10411         if (!map || !r || !dim_map)
10412                 goto error;
10413
10414         for (i = 0; i < map->n; ++i) {
10415                 struct isl_dim_map *dim_map_i;
10416
10417                 dim_map_i = isl_dim_map_extend(dim_map, map->p[i]);
10418
10419                 map->p[i] = isl_basic_map_realign(map->p[i],
10420                                             isl_space_copy(r->dim), dim_map_i);
10421
10422                 if (!map->p[i])
10423                         goto error;
10424         }
10425
10426         map = isl_map_reset_space(map, isl_space_copy(r->dim));
10427
10428         isl_reordering_free(r);
10429         free(dim_map);
10430         return map;
10431 error:
10432         free(dim_map);
10433         isl_map_free(map);
10434         isl_reordering_free(r);
10435         return NULL;
10436 }
10437
10438 __isl_give isl_set *isl_set_realign(__isl_take isl_set *set,
10439         __isl_take isl_reordering *r)
10440 {
10441         return (isl_set *)isl_map_realign((isl_map *)set, r);
10442 }
10443
10444 __isl_give isl_map *isl_map_align_params(__isl_take isl_map *map,
10445         __isl_take isl_space *model)
10446 {
10447         isl_ctx *ctx;
10448
10449         if (!map || !model)
10450                 goto error;
10451
10452         ctx = isl_space_get_ctx(model);
10453         if (!isl_space_has_named_params(model))
10454                 isl_die(ctx, isl_error_invalid,
10455                         "model has unnamed parameters", goto error);
10456         if (!isl_space_has_named_params(map->dim))
10457                 isl_die(ctx, isl_error_invalid,
10458                         "relation has unnamed parameters", goto error);
10459         if (!isl_space_match(map->dim, isl_dim_param, model, isl_dim_param)) {
10460                 isl_reordering *exp;
10461
10462                 model = isl_space_drop_dims(model, isl_dim_in,
10463                                         0, isl_space_dim(model, isl_dim_in));
10464                 model = isl_space_drop_dims(model, isl_dim_out,
10465                                         0, isl_space_dim(model, isl_dim_out));
10466                 exp = isl_parameter_alignment_reordering(map->dim, model);
10467                 exp = isl_reordering_extend_space(exp, isl_map_get_space(map));
10468                 map = isl_map_realign(map, exp);
10469         }
10470
10471         isl_space_free(model);
10472         return map;
10473 error:
10474         isl_space_free(model);
10475         isl_map_free(map);
10476         return NULL;
10477 }
10478
10479 __isl_give isl_set *isl_set_align_params(__isl_take isl_set *set,
10480         __isl_take isl_space *model)
10481 {
10482         return isl_map_align_params(set, model);
10483 }
10484
10485 /* Align the parameters of "bmap" to those of "model", introducing
10486  * additional parameters if needed.
10487  */
10488 __isl_give isl_basic_map *isl_basic_map_align_params(
10489         __isl_take isl_basic_map *bmap, __isl_take isl_space *model)
10490 {
10491         isl_ctx *ctx;
10492
10493         if (!bmap || !model)
10494                 goto error;
10495
10496         ctx = isl_space_get_ctx(model);
10497         if (!isl_space_has_named_params(model))
10498                 isl_die(ctx, isl_error_invalid,
10499                         "model has unnamed parameters", goto error);
10500         if (!isl_space_has_named_params(bmap->dim))
10501                 isl_die(ctx, isl_error_invalid,
10502                         "relation has unnamed parameters", goto error);
10503         if (!isl_space_match(bmap->dim, isl_dim_param, model, isl_dim_param)) {
10504                 isl_reordering *exp;
10505                 struct isl_dim_map *dim_map;
10506
10507                 model = isl_space_drop_dims(model, isl_dim_in,
10508                                         0, isl_space_dim(model, isl_dim_in));
10509                 model = isl_space_drop_dims(model, isl_dim_out,
10510                                         0, isl_space_dim(model, isl_dim_out));
10511                 exp = isl_parameter_alignment_reordering(bmap->dim, model);
10512                 exp = isl_reordering_extend_space(exp,
10513                                         isl_basic_map_get_space(bmap));
10514                 dim_map = isl_dim_map_from_reordering(exp);
10515                 bmap = isl_basic_map_realign(bmap,
10516                                     exp ? isl_space_copy(exp->dim) : NULL,
10517                                     isl_dim_map_extend(dim_map, bmap));
10518                 isl_reordering_free(exp);
10519                 free(dim_map);
10520         }
10521
10522         isl_space_free(model);
10523         return bmap;
10524 error:
10525         isl_space_free(model);
10526         isl_basic_map_free(bmap);
10527         return NULL;
10528 }
10529
10530 /* Align the parameters of "bset" to those of "model", introducing
10531  * additional parameters if needed.
10532  */
10533 __isl_give isl_basic_set *isl_basic_set_align_params(
10534         __isl_take isl_basic_set *bset, __isl_take isl_space *model)
10535 {
10536         return isl_basic_map_align_params(bset, model);
10537 }
10538
10539 __isl_give isl_mat *isl_basic_map_equalities_matrix(
10540                 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10541                 enum isl_dim_type c2, enum isl_dim_type c3,
10542                 enum isl_dim_type c4, enum isl_dim_type c5)
10543 {
10544         enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10545         struct isl_mat *mat;
10546         int i, j, k;
10547         int pos;
10548
10549         if (!bmap)
10550                 return NULL;
10551         mat = isl_mat_alloc(bmap->ctx, bmap->n_eq,
10552                                 isl_basic_map_total_dim(bmap) + 1);
10553         if (!mat)
10554                 return NULL;
10555         for (i = 0; i < bmap->n_eq; ++i)
10556                 for (j = 0, pos = 0; j < 5; ++j) {
10557                         int off = isl_basic_map_offset(bmap, c[j]);
10558                         for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10559                                 isl_int_set(mat->row[i][pos],
10560                                             bmap->eq[i][off + k]);
10561                                 ++pos;
10562                         }
10563                 }
10564
10565         return mat;
10566 }
10567
10568 __isl_give isl_mat *isl_basic_map_inequalities_matrix(
10569                 __isl_keep isl_basic_map *bmap, enum isl_dim_type c1,
10570                 enum isl_dim_type c2, enum isl_dim_type c3,
10571                 enum isl_dim_type c4, enum isl_dim_type c5)
10572 {
10573         enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10574         struct isl_mat *mat;
10575         int i, j, k;
10576         int pos;
10577
10578         if (!bmap)
10579                 return NULL;
10580         mat = isl_mat_alloc(bmap->ctx, bmap->n_ineq,
10581                                 isl_basic_map_total_dim(bmap) + 1);
10582         if (!mat)
10583                 return NULL;
10584         for (i = 0; i < bmap->n_ineq; ++i)
10585                 for (j = 0, pos = 0; j < 5; ++j) {
10586                         int off = isl_basic_map_offset(bmap, c[j]);
10587                         for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10588                                 isl_int_set(mat->row[i][pos],
10589                                             bmap->ineq[i][off + k]);
10590                                 ++pos;
10591                         }
10592                 }
10593
10594         return mat;
10595 }
10596
10597 __isl_give isl_basic_map *isl_basic_map_from_constraint_matrices(
10598         __isl_take isl_space *dim,
10599         __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10600         enum isl_dim_type c2, enum isl_dim_type c3,
10601         enum isl_dim_type c4, enum isl_dim_type c5)
10602 {
10603         enum isl_dim_type c[5] = { c1, c2, c3, c4, c5 };
10604         isl_basic_map *bmap;
10605         unsigned total;
10606         unsigned extra;
10607         int i, j, k, l;
10608         int pos;
10609
10610         if (!dim || !eq || !ineq)
10611                 goto error;
10612
10613         if (eq->n_col != ineq->n_col)
10614                 isl_die(dim->ctx, isl_error_invalid,
10615                         "equalities and inequalities matrices should have "
10616                         "same number of columns", goto error);
10617
10618         total = 1 + isl_space_dim(dim, isl_dim_all);
10619
10620         if (eq->n_col < total)
10621                 isl_die(dim->ctx, isl_error_invalid,
10622                         "number of columns too small", goto error);
10623
10624         extra = eq->n_col - total;
10625
10626         bmap = isl_basic_map_alloc_space(isl_space_copy(dim), extra,
10627                                        eq->n_row, ineq->n_row);
10628         if (!bmap)
10629                 goto error;
10630         for (i = 0; i < extra; ++i) {
10631                 k = isl_basic_map_alloc_div(bmap);
10632                 if (k < 0)
10633                         goto error;
10634                 isl_int_set_si(bmap->div[k][0], 0);
10635         }
10636         for (i = 0; i < eq->n_row; ++i) {
10637                 l = isl_basic_map_alloc_equality(bmap);
10638                 if (l < 0)
10639                         goto error;
10640                 for (j = 0, pos = 0; j < 5; ++j) {
10641                         int off = isl_basic_map_offset(bmap, c[j]);
10642                         for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10643                                 isl_int_set(bmap->eq[l][off + k], 
10644                                             eq->row[i][pos]);
10645                                 ++pos;
10646                         }
10647                 }
10648         }
10649         for (i = 0; i < ineq->n_row; ++i) {
10650                 l = isl_basic_map_alloc_inequality(bmap);
10651                 if (l < 0)
10652                         goto error;
10653                 for (j = 0, pos = 0; j < 5; ++j) {
10654                         int off = isl_basic_map_offset(bmap, c[j]);
10655                         for (k = 0; k < isl_basic_map_dim(bmap, c[j]); ++k) {
10656                                 isl_int_set(bmap->ineq[l][off + k], 
10657                                             ineq->row[i][pos]);
10658                                 ++pos;
10659                         }
10660                 }
10661         }
10662
10663         isl_space_free(dim);
10664         isl_mat_free(eq);
10665         isl_mat_free(ineq);
10666
10667         bmap = isl_basic_map_simplify(bmap);
10668         return isl_basic_map_finalize(bmap);
10669 error:
10670         isl_space_free(dim);
10671         isl_mat_free(eq);
10672         isl_mat_free(ineq);
10673         return NULL;
10674 }
10675
10676 __isl_give isl_mat *isl_basic_set_equalities_matrix(
10677         __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10678         enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10679 {
10680         return isl_basic_map_equalities_matrix((isl_basic_map *)bset,
10681                                                 c1, c2, c3, c4, isl_dim_in);
10682 }
10683
10684 __isl_give isl_mat *isl_basic_set_inequalities_matrix(
10685         __isl_keep isl_basic_set *bset, enum isl_dim_type c1,
10686         enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10687 {
10688         return isl_basic_map_inequalities_matrix((isl_basic_map *)bset,
10689                                                  c1, c2, c3, c4, isl_dim_in);
10690 }
10691
10692 __isl_give isl_basic_set *isl_basic_set_from_constraint_matrices(
10693         __isl_take isl_space *dim,
10694         __isl_take isl_mat *eq, __isl_take isl_mat *ineq, enum isl_dim_type c1,
10695         enum isl_dim_type c2, enum isl_dim_type c3, enum isl_dim_type c4)
10696 {
10697         return (isl_basic_set*)
10698             isl_basic_map_from_constraint_matrices(dim, eq, ineq,
10699                                                    c1, c2, c3, c4, isl_dim_in);
10700 }
10701
10702 int isl_basic_map_can_zip(__isl_keep isl_basic_map *bmap)
10703 {
10704         if (!bmap)
10705                 return -1;
10706         
10707         return isl_space_can_zip(bmap->dim);
10708 }
10709
10710 int isl_map_can_zip(__isl_keep isl_map *map)
10711 {
10712         if (!map)
10713                 return -1;
10714         
10715         return isl_space_can_zip(map->dim);
10716 }
10717
10718 /* Given a basic map (A -> B) -> (C -> D), return the corresponding basic map
10719  * (A -> C) -> (B -> D).
10720  */
10721 __isl_give isl_basic_map *isl_basic_map_zip(__isl_take isl_basic_map *bmap)
10722 {
10723         unsigned pos;
10724         unsigned n1;
10725         unsigned n2;
10726
10727         if (!bmap)
10728                 return NULL;
10729
10730         if (!isl_basic_map_can_zip(bmap))
10731                 isl_die(bmap->ctx, isl_error_invalid,
10732                         "basic map cannot be zipped", goto error);
10733         pos = isl_basic_map_offset(bmap, isl_dim_in) +
10734                 isl_space_dim(bmap->dim->nested[0], isl_dim_in);
10735         n1 = isl_space_dim(bmap->dim->nested[0], isl_dim_out);
10736         n2 = isl_space_dim(bmap->dim->nested[1], isl_dim_in);
10737         bmap = isl_basic_map_cow(bmap);
10738         bmap = isl_basic_map_swap_vars(bmap, pos, n1, n2);
10739         if (!bmap)
10740                 return NULL;
10741         bmap->dim = isl_space_zip(bmap->dim);
10742         if (!bmap->dim)
10743                 goto error;
10744         return bmap;
10745 error:
10746         isl_basic_map_free(bmap);
10747         return NULL;
10748 }
10749
10750 /* Given a map (A -> B) -> (C -> D), return the corresponding map
10751  * (A -> C) -> (B -> D).
10752  */
10753 __isl_give isl_map *isl_map_zip(__isl_take isl_map *map)
10754 {
10755         int i;
10756
10757         if (!map)
10758                 return NULL;
10759
10760         if (!isl_map_can_zip(map))
10761                 isl_die(map->ctx, isl_error_invalid, "map cannot be zipped",
10762                         goto error);
10763
10764         map = isl_map_cow(map);
10765         if (!map)
10766                 return NULL;
10767
10768         for (i = 0; i < map->n; ++i) {
10769                 map->p[i] = isl_basic_map_zip(map->p[i]);
10770                 if (!map->p[i])
10771                         goto error;
10772         }
10773
10774         map->dim = isl_space_zip(map->dim);
10775         if (!map->dim)
10776                 goto error;
10777
10778         return map;
10779 error:
10780         isl_map_free(map);
10781         return NULL;
10782 }
10783
10784 /* Can we apply isl_basic_map_curry to "bmap"?
10785  * That is, does it have a nested relation in its domain?
10786  */
10787 int isl_basic_map_can_curry(__isl_keep isl_basic_map *bmap)
10788 {
10789         if (!bmap)
10790                 return -1;
10791
10792         return isl_space_can_curry(bmap->dim);
10793 }
10794
10795 /* Can we apply isl_map_curry to "map"?
10796  * That is, does it have a nested relation in its domain?
10797  */
10798 int isl_map_can_curry(__isl_keep isl_map *map)
10799 {
10800         if (!map)
10801                 return -1;
10802
10803         return isl_space_can_curry(map->dim);
10804 }
10805
10806 /* Given a basic map (A -> B) -> C, return the corresponding basic map
10807  * A -> (B -> C).
10808  */
10809 __isl_give isl_basic_map *isl_basic_map_curry(__isl_take isl_basic_map *bmap)
10810 {
10811
10812         if (!bmap)
10813                 return NULL;
10814
10815         if (!isl_basic_map_can_curry(bmap))
10816                 isl_die(bmap->ctx, isl_error_invalid,
10817                         "basic map cannot be curried", goto error);
10818         bmap = isl_basic_map_cow(bmap);
10819         if (!bmap)
10820                 return NULL;
10821         bmap->dim = isl_space_curry(bmap->dim);
10822         if (!bmap->dim)
10823                 goto error;
10824         return bmap;
10825 error:
10826         isl_basic_map_free(bmap);
10827         return NULL;
10828 }
10829
10830 /* Given a map (A -> B) -> C, return the corresponding map
10831  * A -> (B -> C).
10832  */
10833 __isl_give isl_map *isl_map_curry(__isl_take isl_map *map)
10834 {
10835         int i;
10836
10837         if (!map)
10838                 return NULL;
10839
10840         if (!isl_map_can_curry(map))
10841                 isl_die(map->ctx, isl_error_invalid, "map cannot be curried",
10842                         goto error);
10843
10844         map = isl_map_cow(map);
10845         if (!map)
10846                 return NULL;
10847
10848         for (i = 0; i < map->n; ++i) {
10849                 map->p[i] = isl_basic_map_curry(map->p[i]);
10850                 if (!map->p[i])
10851                         goto error;
10852         }
10853
10854         map->dim = isl_space_curry(map->dim);
10855         if (!map->dim)
10856                 goto error;
10857
10858         return map;
10859 error:
10860         isl_map_free(map);
10861         return NULL;
10862 }
10863
10864 /* Can we apply isl_basic_map_uncurry to "bmap"?
10865  * That is, does it have a nested relation in its domain?
10866  */
10867 int isl_basic_map_can_uncurry(__isl_keep isl_basic_map *bmap)
10868 {
10869         if (!bmap)
10870                 return -1;
10871
10872         return isl_space_can_uncurry(bmap->dim);
10873 }
10874
10875 /* Can we apply isl_map_uncurry to "map"?
10876  * That is, does it have a nested relation in its domain?
10877  */
10878 int isl_map_can_uncurry(__isl_keep isl_map *map)
10879 {
10880         if (!map)
10881                 return -1;
10882
10883         return isl_space_can_uncurry(map->dim);
10884 }
10885
10886 /* Given a basic map A -> (B -> C), return the corresponding basic map
10887  * (A -> B) -> C.
10888  */
10889 __isl_give isl_basic_map *isl_basic_map_uncurry(__isl_take isl_basic_map *bmap)
10890 {
10891
10892         if (!bmap)
10893                 return NULL;
10894
10895         if (!isl_basic_map_can_uncurry(bmap))
10896                 isl_die(bmap->ctx, isl_error_invalid,
10897                         "basic map cannot be uncurried",
10898                         return isl_basic_map_free(bmap));
10899         bmap = isl_basic_map_cow(bmap);
10900         if (!bmap)
10901                 return NULL;
10902         bmap->dim = isl_space_uncurry(bmap->dim);
10903         if (!bmap->dim)
10904                 return isl_basic_map_free(bmap);
10905         return bmap;
10906 }
10907
10908 /* Given a map A -> (B -> C), return the corresponding map
10909  * (A -> B) -> C.
10910  */
10911 __isl_give isl_map *isl_map_uncurry(__isl_take isl_map *map)
10912 {
10913         int i;
10914
10915         if (!map)
10916                 return NULL;
10917
10918         if (!isl_map_can_uncurry(map))
10919                 isl_die(map->ctx, isl_error_invalid, "map cannot be uncurried",
10920                         return isl_map_free(map));
10921
10922         map = isl_map_cow(map);
10923         if (!map)
10924                 return NULL;
10925
10926         for (i = 0; i < map->n; ++i) {
10927                 map->p[i] = isl_basic_map_uncurry(map->p[i]);
10928                 if (!map->p[i])
10929                         return isl_map_free(map);
10930         }
10931
10932         map->dim = isl_space_uncurry(map->dim);
10933         if (!map->dim)
10934                 return isl_map_free(map);
10935
10936         return map;
10937 }
10938
10939 /* Construct a basic map mapping the domain of the affine expression
10940  * to a one-dimensional range prescribed by the affine expression.
10941  */
10942 __isl_give isl_basic_map *isl_basic_map_from_aff(__isl_take isl_aff *aff)
10943 {
10944         int k;
10945         int pos;
10946         isl_local_space *ls;
10947         isl_basic_map *bmap;
10948
10949         if (!aff)
10950                 return NULL;
10951
10952         ls = isl_aff_get_local_space(aff);
10953         bmap = isl_basic_map_from_local_space(ls);
10954         bmap = isl_basic_map_extend_constraints(bmap, 1, 0);
10955         k = isl_basic_map_alloc_equality(bmap);
10956         if (k < 0)
10957                 goto error;
10958
10959         pos = isl_basic_map_offset(bmap, isl_dim_out);
10960         isl_seq_cpy(bmap->eq[k], aff->v->el + 1, pos);
10961         isl_int_neg(bmap->eq[k][pos], aff->v->el[0]);
10962         isl_seq_cpy(bmap->eq[k] + pos + 1, aff->v->el + 1 + pos,
10963                     aff->v->size - (pos + 1));
10964
10965         isl_aff_free(aff);
10966         bmap = isl_basic_map_finalize(bmap);
10967         return bmap;
10968 error:
10969         isl_aff_free(aff);
10970         isl_basic_map_free(bmap);
10971         return NULL;
10972 }
10973
10974 /* Construct a map mapping the domain of the affine expression
10975  * to a one-dimensional range prescribed by the affine expression.
10976  */
10977 __isl_give isl_map *isl_map_from_aff(__isl_take isl_aff *aff)
10978 {
10979         isl_basic_map *bmap;
10980
10981         bmap = isl_basic_map_from_aff(aff);
10982         return isl_map_from_basic_map(bmap);
10983 }
10984
10985 /* Construct a basic map mapping the domain the multi-affine expression
10986  * to its range, with each dimension in the range equated to the
10987  * corresponding affine expression.
10988  */
10989 __isl_give isl_basic_map *isl_basic_map_from_multi_aff(
10990         __isl_take isl_multi_aff *maff)
10991 {
10992         int i;
10993         isl_space *space;
10994         isl_basic_map *bmap;
10995
10996         if (!maff)
10997                 return NULL;
10998
10999         if (isl_space_dim(maff->space, isl_dim_out) != maff->n)
11000                 isl_die(isl_multi_aff_get_ctx(maff), isl_error_internal,
11001                         "invalid space", return isl_multi_aff_free(maff));
11002
11003         space = isl_space_domain(isl_multi_aff_get_space(maff));
11004         bmap = isl_basic_map_universe(isl_space_from_domain(space));
11005
11006         for (i = 0; i < maff->n; ++i) {
11007                 isl_aff *aff;
11008                 isl_basic_map *bmap_i;
11009
11010                 aff = isl_aff_copy(maff->p[i]);
11011                 bmap_i = isl_basic_map_from_aff(aff);
11012
11013                 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11014         }
11015
11016         bmap = isl_basic_map_reset_space(bmap, isl_multi_aff_get_space(maff));
11017
11018         isl_multi_aff_free(maff);
11019         return bmap;
11020 }
11021
11022 /* Construct a map mapping the domain the multi-affine expression
11023  * to its range, with each dimension in the range equated to the
11024  * corresponding affine expression.
11025  */
11026 __isl_give isl_map *isl_map_from_multi_aff(__isl_take isl_multi_aff *maff)
11027 {
11028         isl_basic_map *bmap;
11029
11030         bmap = isl_basic_map_from_multi_aff(maff);
11031         return isl_map_from_basic_map(bmap);
11032 }
11033
11034 /* Construct a basic map mapping a domain in the given space to
11035  * to an n-dimensional range, with n the number of elements in the list,
11036  * where each coordinate in the range is prescribed by the
11037  * corresponding affine expression.
11038  * The domains of all affine expressions in the list are assumed to match
11039  * domain_dim.
11040  */
11041 __isl_give isl_basic_map *isl_basic_map_from_aff_list(
11042         __isl_take isl_space *domain_dim, __isl_take isl_aff_list *list)
11043 {
11044         int i;
11045         isl_space *dim;
11046         isl_basic_map *bmap;
11047
11048         if (!list)
11049                 return NULL;
11050
11051         dim = isl_space_from_domain(domain_dim);
11052         bmap = isl_basic_map_universe(dim);
11053
11054         for (i = 0; i < list->n; ++i) {
11055                 isl_aff *aff;
11056                 isl_basic_map *bmap_i;
11057
11058                 aff = isl_aff_copy(list->p[i]);
11059                 bmap_i = isl_basic_map_from_aff(aff);
11060
11061                 bmap = isl_basic_map_flat_range_product(bmap, bmap_i);
11062         }
11063
11064         isl_aff_list_free(list);
11065         return bmap;
11066 }
11067
11068 __isl_give isl_set *isl_set_equate(__isl_take isl_set *set,
11069         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11070 {
11071         return isl_map_equate(set, type1, pos1, type2, pos2);
11072 }
11073
11074 /* Construct a basic map where the given dimensions are equal to each other.
11075  */
11076 static __isl_give isl_basic_map *equator(__isl_take isl_space *space,
11077         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11078 {
11079         isl_basic_map *bmap = NULL;
11080         int i;
11081
11082         if (!space)
11083                 return NULL;
11084
11085         if (pos1 >= isl_space_dim(space, type1))
11086                 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11087                         "index out of bounds", goto error);
11088         if (pos2 >= isl_space_dim(space, type2))
11089                 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11090                         "index out of bounds", goto error);
11091
11092         if (type1 == type2 && pos1 == pos2)
11093                 return isl_basic_map_universe(space);
11094
11095         bmap = isl_basic_map_alloc_space(isl_space_copy(space), 0, 1, 0);
11096         i = isl_basic_map_alloc_equality(bmap);
11097         if (i < 0)
11098                 goto error;
11099         isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11100         pos1 += isl_basic_map_offset(bmap, type1);
11101         pos2 += isl_basic_map_offset(bmap, type2);
11102         isl_int_set_si(bmap->eq[i][pos1], -1);
11103         isl_int_set_si(bmap->eq[i][pos2], 1);
11104         bmap = isl_basic_map_finalize(bmap);
11105         isl_space_free(space);
11106         return bmap;
11107 error:
11108         isl_space_free(space);
11109         isl_basic_map_free(bmap);
11110         return NULL;
11111 }
11112
11113 /* Add a constraint imposing that the given two dimensions are equal.
11114  */
11115 __isl_give isl_basic_map *isl_basic_map_equate(__isl_take isl_basic_map *bmap,
11116         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11117 {
11118         isl_basic_map *eq;
11119
11120         eq = equator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11121
11122         bmap = isl_basic_map_intersect(bmap, eq);
11123
11124         return bmap;
11125 }
11126
11127 /* Add a constraint imposing that the given two dimensions are equal.
11128  */
11129 __isl_give isl_map *isl_map_equate(__isl_take isl_map *map,
11130         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11131 {
11132         isl_basic_map *bmap;
11133
11134         bmap = equator(isl_map_get_space(map), type1, pos1, type2, pos2);
11135
11136         map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11137
11138         return map;
11139 }
11140
11141 /* Add a constraint imposing that the given two dimensions have opposite values.
11142  */
11143 __isl_give isl_map *isl_map_oppose(__isl_take isl_map *map,
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 (!map)
11150                 return NULL;
11151
11152         if (pos1 >= isl_map_dim(map, type1))
11153                 isl_die(map->ctx, isl_error_invalid,
11154                         "index out of bounds", goto error);
11155         if (pos2 >= isl_map_dim(map, type2))
11156                 isl_die(map->ctx, isl_error_invalid,
11157                         "index out of bounds", goto error);
11158
11159         bmap = isl_basic_map_alloc_space(isl_map_get_space(map), 0, 1, 0);
11160         i = isl_basic_map_alloc_equality(bmap);
11161         if (i < 0)
11162                 goto error;
11163         isl_seq_clr(bmap->eq[i], 1 + isl_basic_map_total_dim(bmap));
11164         pos1 += isl_basic_map_offset(bmap, type1);
11165         pos2 += isl_basic_map_offset(bmap, type2);
11166         isl_int_set_si(bmap->eq[i][pos1], 1);
11167         isl_int_set_si(bmap->eq[i][pos2], 1);
11168         bmap = isl_basic_map_finalize(bmap);
11169
11170         map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11171
11172         return map;
11173 error:
11174         isl_basic_map_free(bmap);
11175         isl_map_free(map);
11176         return NULL;
11177 }
11178
11179 /* Add a constraint imposing that the value of the first dimension is
11180  * greater than or equal to that of the second.
11181  */
11182 __isl_give isl_basic_map *isl_basic_map_order_ge(__isl_take isl_basic_map *bmap,
11183         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11184 {
11185         isl_constraint *c;
11186         isl_local_space *ls;
11187
11188         if (!bmap)
11189                 return NULL;
11190
11191         if (pos1 >= isl_basic_map_dim(bmap, type1))
11192                 isl_die(bmap->ctx, isl_error_invalid,
11193                         "index out of bounds", return isl_basic_map_free(bmap));
11194         if (pos2 >= isl_basic_map_dim(bmap, type2))
11195                 isl_die(bmap->ctx, isl_error_invalid,
11196                         "index out of bounds", return isl_basic_map_free(bmap));
11197
11198         if (type1 == type2 && pos1 == pos2)
11199                 return bmap;
11200
11201         ls = isl_local_space_from_space(isl_basic_map_get_space(bmap));
11202         c = isl_inequality_alloc(ls);
11203         c = isl_constraint_set_coefficient_si(c, type1, pos1, 1);
11204         c = isl_constraint_set_coefficient_si(c, type2, pos2, -1);
11205         bmap = isl_basic_map_add_constraint(bmap, c);
11206
11207         return bmap;
11208 }
11209
11210 /* Construct a basic map where the value of the first dimension is
11211  * greater than that of the second.
11212  */
11213 static __isl_give isl_basic_map *greator(__isl_take isl_space *space,
11214         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11215 {
11216         isl_basic_map *bmap = NULL;
11217         int i;
11218
11219         if (!space)
11220                 return NULL;
11221
11222         if (pos1 >= isl_space_dim(space, type1))
11223                 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11224                         "index out of bounds", goto error);
11225         if (pos2 >= isl_space_dim(space, type2))
11226                 isl_die(isl_space_get_ctx(space), isl_error_invalid,
11227                         "index out of bounds", goto error);
11228
11229         if (type1 == type2 && pos1 == pos2)
11230                 return isl_basic_map_empty(space);
11231
11232         bmap = isl_basic_map_alloc_space(space, 0, 0, 1);
11233         i = isl_basic_map_alloc_inequality(bmap);
11234         if (i < 0)
11235                 goto error;
11236         isl_seq_clr(bmap->ineq[i], 1 + isl_basic_map_total_dim(bmap));
11237         pos1 += isl_basic_map_offset(bmap, type1);
11238         pos2 += isl_basic_map_offset(bmap, type2);
11239         isl_int_set_si(bmap->ineq[i][pos1], 1);
11240         isl_int_set_si(bmap->ineq[i][pos2], -1);
11241         isl_int_set_si(bmap->ineq[i][0], -1);
11242         bmap = isl_basic_map_finalize(bmap);
11243
11244         return bmap;
11245 error:
11246         isl_space_free(space);
11247         isl_basic_map_free(bmap);
11248         return NULL;
11249 }
11250
11251 /* Add a constraint imposing that the value of the first dimension is
11252  * greater than that of the second.
11253  */
11254 __isl_give isl_basic_map *isl_basic_map_order_gt(__isl_take isl_basic_map *bmap,
11255         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11256 {
11257         isl_basic_map *gt;
11258
11259         gt = greator(isl_basic_map_get_space(bmap), type1, pos1, type2, pos2);
11260
11261         bmap = isl_basic_map_intersect(bmap, gt);
11262
11263         return bmap;
11264 }
11265
11266 /* Add a constraint imposing that the value of the first dimension is
11267  * greater than that of the second.
11268  */
11269 __isl_give isl_map *isl_map_order_gt(__isl_take isl_map *map,
11270         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11271 {
11272         isl_basic_map *bmap;
11273
11274         bmap = greator(isl_map_get_space(map), type1, pos1, type2, pos2);
11275
11276         map = isl_map_intersect(map, isl_map_from_basic_map(bmap));
11277
11278         return map;
11279 }
11280
11281 /* Add a constraint imposing that the value of the first dimension is
11282  * smaller than that of the second.
11283  */
11284 __isl_give isl_map *isl_map_order_lt(__isl_take isl_map *map,
11285         enum isl_dim_type type1, int pos1, enum isl_dim_type type2, int pos2)
11286 {
11287         return isl_map_order_gt(map, type2, pos2, type1, pos1);
11288 }
11289
11290 __isl_give isl_aff *isl_basic_map_get_div(__isl_keep isl_basic_map *bmap,
11291         int pos)
11292 {
11293         isl_aff *div;
11294         isl_local_space *ls;
11295
11296         if (!bmap)
11297                 return NULL;
11298
11299         if (!isl_basic_map_divs_known(bmap))
11300                 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11301                         "some divs are unknown", return NULL);
11302
11303         ls = isl_basic_map_get_local_space(bmap);
11304         div = isl_local_space_get_div(ls, pos);
11305         isl_local_space_free(ls);
11306
11307         return div;
11308 }
11309
11310 __isl_give isl_aff *isl_basic_set_get_div(__isl_keep isl_basic_set *bset,
11311         int pos)
11312 {
11313         return isl_basic_map_get_div(bset, pos);
11314 }
11315
11316 /* Plug in "subs" for dimension "type", "pos" of "bset".
11317  *
11318  * Let i be the dimension to replace and let "subs" be of the form
11319  *
11320  *      f/d
11321  *
11322  * Any integer division with a non-zero coefficient for i,
11323  *
11324  *      floor((a i + g)/m)
11325  *
11326  * is replaced by
11327  *
11328  *      floor((a f + d g)/(m d))
11329  *
11330  * Constraints of the form
11331  *
11332  *      a i + g
11333  *
11334  * are replaced by
11335  *
11336  *      a f + d g
11337  *
11338  * We currently require that "subs" is an integral expression.
11339  * Handling rational expressions may require us to add stride constraints
11340  * as we do in isl_basic_set_preimage_multi_aff.
11341  */
11342 __isl_give isl_basic_set *isl_basic_set_substitute(
11343         __isl_take isl_basic_set *bset,
11344         enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11345 {
11346         int i;
11347         isl_int v;
11348         isl_ctx *ctx;
11349
11350         if (bset && isl_basic_set_plain_is_empty(bset))
11351                 return bset;
11352
11353         bset = isl_basic_set_cow(bset);
11354         if (!bset || !subs)
11355                 goto error;
11356
11357         ctx = isl_basic_set_get_ctx(bset);
11358         if (!isl_space_is_equal(bset->dim, subs->ls->dim))
11359                 isl_die(ctx, isl_error_invalid,
11360                         "spaces don't match", goto error);
11361         if (isl_local_space_dim(subs->ls, isl_dim_div) != 0)
11362                 isl_die(ctx, isl_error_unsupported,
11363                         "cannot handle divs yet", goto error);
11364         if (!isl_int_is_one(subs->v->el[0]))
11365                 isl_die(ctx, isl_error_invalid,
11366                         "can only substitute integer expressions", goto error);
11367
11368         pos += isl_basic_set_offset(bset, type);
11369
11370         isl_int_init(v);
11371
11372         for (i = 0; i < bset->n_eq; ++i) {
11373                 if (isl_int_is_zero(bset->eq[i][pos]))
11374                         continue;
11375                 isl_int_set(v, bset->eq[i][pos]);
11376                 isl_int_set_si(bset->eq[i][pos], 0);
11377                 isl_seq_combine(bset->eq[i], subs->v->el[0], bset->eq[i],
11378                                 v, subs->v->el + 1, subs->v->size - 1);
11379         }
11380
11381         for (i = 0; i < bset->n_ineq; ++i) {
11382                 if (isl_int_is_zero(bset->ineq[i][pos]))
11383                         continue;
11384                 isl_int_set(v, bset->ineq[i][pos]);
11385                 isl_int_set_si(bset->ineq[i][pos], 0);
11386                 isl_seq_combine(bset->ineq[i], subs->v->el[0], bset->ineq[i],
11387                                 v, subs->v->el + 1, subs->v->size - 1);
11388         }
11389
11390         for (i = 0; i < bset->n_div; ++i) {
11391                 if (isl_int_is_zero(bset->div[i][1 + pos]))
11392                         continue;
11393                 isl_int_set(v, bset->div[i][1 + pos]);
11394                 isl_int_set_si(bset->div[i][1 + pos], 0);
11395                 isl_seq_combine(bset->div[i] + 1,
11396                                 subs->v->el[0], bset->div[i] + 1,
11397                                 v, subs->v->el + 1, subs->v->size - 1);
11398                 isl_int_mul(bset->div[i][0], bset->div[i][0], subs->v->el[0]);
11399         }
11400
11401         isl_int_clear(v);
11402
11403         bset = isl_basic_set_simplify(bset);
11404         return isl_basic_set_finalize(bset);
11405 error:
11406         isl_basic_set_free(bset);
11407         return NULL;
11408 }
11409
11410 /* Plug in "subs" for dimension "type", "pos" of "set".
11411  */
11412 __isl_give isl_set *isl_set_substitute(__isl_take isl_set *set,
11413         enum isl_dim_type type, unsigned pos, __isl_keep isl_aff *subs)
11414 {
11415         int i;
11416
11417         if (set && isl_set_plain_is_empty(set))
11418                 return set;
11419
11420         set = isl_set_cow(set);
11421         if (!set || !subs)
11422                 goto error;
11423
11424         for (i = set->n - 1; i >= 0; --i) {
11425                 set->p[i] = isl_basic_set_substitute(set->p[i], type, pos, subs);
11426                 if (remove_if_empty(set, i) < 0)
11427                         goto error;
11428         }
11429
11430         return set;
11431 error:
11432         isl_set_free(set);
11433         return NULL;
11434 }
11435
11436 /* Check if the range of "ma" is compatible with the domain or range
11437  * (depending on "type") of "bmap".
11438  * Return -1 if anything is wrong.
11439  */
11440 static int check_basic_map_compatible_range_multi_aff(
11441         __isl_keep isl_basic_map *bmap, enum isl_dim_type type,
11442         __isl_keep isl_multi_aff *ma)
11443 {
11444         int m;
11445         isl_space *ma_space;
11446
11447         ma_space = isl_multi_aff_get_space(ma);
11448         m = isl_space_tuple_match(bmap->dim, type, ma_space, isl_dim_out);
11449         isl_space_free(ma_space);
11450         if (m >= 0 && !m)
11451                 isl_die(isl_basic_map_get_ctx(bmap), isl_error_invalid,
11452                         "spaces don't match", return -1);
11453         return m;
11454 }
11455
11456 /* Copy the divs from "ma" to "bmap", adding zeros for the "n_before"
11457  * coefficients before the transformed range of dimensions,
11458  * the "n_after" coefficients after the transformed range of dimensions
11459  * and the coefficients of the other divs in "bmap".
11460  */
11461 static int set_ma_divs(__isl_keep isl_basic_map *bmap,
11462         __isl_keep isl_multi_aff *ma, int n_before, int n_after, int n_div)
11463 {
11464         int i;
11465         int n_param;
11466         int n_set;
11467         isl_local_space *ls;
11468
11469         if (n_div == 0)
11470                 return 0;
11471
11472         ls = isl_aff_get_domain_local_space(ma->p[0]);
11473         if (!ls)
11474                 return -1;
11475
11476         n_param = isl_local_space_dim(ls, isl_dim_param);
11477         n_set = isl_local_space_dim(ls, isl_dim_set);
11478         for (i = 0; i < n_div; ++i) {
11479                 int o_bmap = 0, o_ls = 0;
11480
11481                 isl_seq_cpy(bmap->div[i], ls->div->row[i], 1 + 1 + n_param);
11482                 o_bmap += 1 + 1 + n_param;
11483                 o_ls += 1 + 1 + n_param;
11484                 isl_seq_clr(bmap->div[i] + o_bmap, n_before);
11485                 o_bmap += n_before;
11486                 isl_seq_cpy(bmap->div[i] + o_bmap,
11487                             ls->div->row[i] + o_ls, n_set);
11488                 o_bmap += n_set;
11489                 o_ls += n_set;
11490                 isl_seq_clr(bmap->div[i] + o_bmap, n_after);
11491                 o_bmap += n_after;
11492                 isl_seq_cpy(bmap->div[i] + o_bmap,
11493                             ls->div->row[i] + o_ls, n_div);
11494                 o_bmap += n_div;
11495                 o_ls += n_div;
11496                 isl_seq_clr(bmap->div[i] + o_bmap, bmap->n_div - n_div);
11497                 if (isl_basic_set_add_div_constraints(bmap, i) < 0)
11498                         goto error;
11499         }
11500
11501         isl_local_space_free(ls);
11502         return 0;
11503 error:
11504         isl_local_space_free(ls);
11505         return -1;
11506 }
11507
11508 /* How many stride constraints does "ma" enforce?
11509  * That is, how many of the affine expressions have a denominator
11510  * different from one?
11511  */
11512 static int multi_aff_strides(__isl_keep isl_multi_aff *ma)
11513 {
11514         int i;
11515         int strides = 0;
11516
11517         for (i = 0; i < ma->n; ++i)
11518                 if (!isl_int_is_one(ma->p[i]->v->el[0]))
11519                         strides++;
11520
11521         return strides;
11522 }
11523
11524 /* For each affine expression in ma of the form
11525  *
11526  *      x_i = (f_i y + h_i)/m_i
11527  *
11528  * with m_i different from one, add a constraint to "bmap"
11529  * of the form
11530  *
11531  *      f_i y + h_i = m_i alpha_i
11532  *
11533  * with alpha_i an additional existentially quantified variable.
11534  */
11535 static __isl_give isl_basic_map *add_ma_strides(
11536         __isl_take isl_basic_map *bmap, __isl_keep isl_multi_aff *ma,
11537         int n_before, int n_after)
11538 {
11539         int i, k;
11540         int div;
11541         int total;
11542         int n_param;
11543         int n_in;
11544         int n_div;
11545
11546         total = isl_basic_map_total_dim(bmap);
11547         n_param = isl_multi_aff_dim(ma, isl_dim_param);
11548         n_in = isl_multi_aff_dim(ma, isl_dim_in);
11549         n_div = isl_multi_aff_dim(ma, isl_dim_div);
11550         for (i = 0; i < ma->n; ++i) {
11551                 int o_bmap = 0, o_ma = 1;
11552
11553                 if (isl_int_is_one(ma->p[i]->v->el[0]))
11554                         continue;
11555                 div = isl_basic_map_alloc_div(bmap);
11556                 k = isl_basic_map_alloc_equality(bmap);
11557                 if (div < 0 || k < 0)
11558                         goto error;
11559                 isl_int_set_si(bmap->div[div][0], 0);
11560                 isl_seq_cpy(bmap->eq[k] + o_bmap,
11561                             ma->p[i]->v->el + o_ma, 1 + n_param);
11562                 o_bmap += 1 + n_param;
11563                 o_ma += 1 + n_param;
11564                 isl_seq_clr(bmap->eq[k] + o_bmap, n_before);
11565                 o_bmap += n_before;
11566                 isl_seq_cpy(bmap->eq[k] + o_bmap,
11567                             ma->p[i]->v->el + o_ma, n_in);
11568                 o_bmap += n_in;
11569                 o_ma += n_in;
11570                 isl_seq_clr(bmap->eq[k] + o_bmap, n_after);
11571                 o_bmap += n_after;
11572                 isl_seq_cpy(bmap->eq[k] + o_bmap,
11573                             ma->p[i]->v->el + o_ma, n_div);
11574                 o_bmap += n_div;
11575                 o_ma += n_div;
11576                 isl_seq_clr(bmap->eq[k] + o_bmap, 1 + total - o_bmap);
11577                 isl_int_neg(bmap->eq[k][1 + total], ma->p[i]->v->el[0]);
11578                 total++;
11579         }
11580
11581         return bmap;
11582 error:
11583         isl_basic_map_free(bmap);
11584         return NULL;
11585 }
11586
11587 /* Replace the domain or range space (depending on "type) of "space" by "set".
11588  */
11589 static __isl_give isl_space *isl_space_set(__isl_take isl_space *space,
11590         enum isl_dim_type type, __isl_take isl_space *set)
11591 {
11592         if (type == isl_dim_in) {
11593                 space = isl_space_range(space);
11594                 space = isl_space_map_from_domain_and_range(set, space);
11595         } else {
11596                 space = isl_space_domain(space);
11597                 space = isl_space_map_from_domain_and_range(space, set);
11598         }
11599
11600         return space;
11601 }
11602
11603 /* Compute the preimage of the domain or range (depending on "type")
11604  * of "bmap" under the function represented by "ma".
11605  * In other words, plug in "ma" in the domain or range of "bmap".
11606  * The result is a basic map that lives in the same space as "bmap"
11607  * except that the domain or range has been replaced by
11608  * the domain space of "ma".
11609  *
11610  * If bmap is represented by
11611  *
11612  *      A(p) + S u + B x + T v + C(divs) >= 0,
11613  *
11614  * where u and x are input and output dimensions if type == isl_dim_out
11615  * while x and v are input and output dimensions if type == isl_dim_in,
11616  * and ma is represented by
11617  *
11618  *      x = D(p) + F(y) + G(divs')
11619  *
11620  * then the result is
11621  *
11622  *      A(p) + B D(p) + S u + B F(y) + T v + B G(divs') + C(divs) >= 0
11623  *
11624  * The divs in the input set are similarly adjusted.
11625  * In particular
11626  *
11627  *      floor((a_i(p) + s u + b_i x + t v + c_i(divs))/n_i)
11628  *
11629  * becomes
11630  *
11631  *      floor((a_i(p) + b_i D(p) + s u + b_i F(y) + t v +
11632  *              B_i G(divs') + c_i(divs))/n_i)
11633  *
11634  * If bmap is not a rational map and if F(y) involves any denominators
11635  *
11636  *      x_i = (f_i y + h_i)/m_i
11637  *
11638  * then additional constraints are added to ensure that we only
11639  * map back integer points.  That is we enforce
11640  *
11641  *      f_i y + h_i = m_i alpha_i
11642  *
11643  * with alpha_i an additional existentially quantified variable.
11644  *
11645  * We first copy over the divs from "ma".
11646  * Then we add the modified constraints and divs from "bmap".
11647  * Finally, we add the stride constraints, if needed.
11648  */
11649 __isl_give isl_basic_map *isl_basic_map_preimage_multi_aff(
11650         __isl_take isl_basic_map *bmap, enum isl_dim_type type,
11651         __isl_take isl_multi_aff *ma)
11652 {
11653         int i, k;
11654         isl_space *space;
11655         isl_basic_map *res = NULL;
11656         int n_before, n_after, n_div_bmap, n_div_ma;
11657         isl_int f, c1, c2, g;
11658         int rational, strides;
11659
11660         isl_int_init(f);
11661         isl_int_init(c1);
11662         isl_int_init(c2);
11663         isl_int_init(g);
11664
11665         ma = isl_multi_aff_align_divs(ma);
11666         if (!bmap || !ma)
11667                 goto error;
11668         if (check_basic_map_compatible_range_multi_aff(bmap, type, ma) < 0)
11669                 goto error;
11670
11671         if (type == isl_dim_in) {
11672                 n_before = 0;
11673                 n_after = isl_basic_map_dim(bmap, isl_dim_out);
11674         } else {
11675                 n_before = isl_basic_map_dim(bmap, isl_dim_in);
11676                 n_after = 0;
11677         }
11678         n_div_bmap = isl_basic_map_dim(bmap, isl_dim_div);
11679         n_div_ma = ma->n ? isl_aff_dim(ma->p[0], isl_dim_div) : 0;
11680
11681         space = isl_multi_aff_get_domain_space(ma);
11682         space = isl_space_set(isl_basic_map_get_space(bmap), type, space);
11683         rational = isl_basic_map_is_rational(bmap);
11684         strides = rational ? 0 : multi_aff_strides(ma);
11685         res = isl_basic_map_alloc_space(space, n_div_ma + n_div_bmap + strides,
11686                             bmap->n_eq + strides, bmap->n_ineq + 2 * n_div_ma);
11687         if (rational)
11688                 res = isl_basic_map_set_rational(res);
11689
11690         for (i = 0; i < n_div_ma + n_div_bmap; ++i)
11691                 if (isl_basic_map_alloc_div(res) < 0)
11692                         goto error;
11693
11694         if (set_ma_divs(res, ma, n_before, n_after, n_div_ma) < 0)
11695                 goto error;
11696
11697         for (i = 0; i < bmap->n_eq; ++i) {
11698                 k = isl_basic_map_alloc_equality(res);
11699                 if (k < 0)
11700                         goto error;
11701                 isl_seq_preimage(res->eq[k], bmap->eq[i], ma, n_before,
11702                                 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11703         }
11704
11705         for (i = 0; i < bmap->n_ineq; ++i) {
11706                 k = isl_basic_map_alloc_inequality(res);
11707                 if (k < 0)
11708                         goto error;
11709                 isl_seq_preimage(res->ineq[k], bmap->ineq[i], ma, n_before,
11710                                 n_after, n_div_ma, n_div_bmap, f, c1, c2, g, 0);
11711         }
11712
11713         for (i = 0; i < bmap->n_div; ++i) {
11714                 if (isl_int_is_zero(bmap->div[i][0])) {
11715                         isl_int_set_si(res->div[n_div_ma + i][0], 0);
11716                         continue;
11717                 }
11718                 isl_seq_preimage(res->div[n_div_ma + i], bmap->div[i], ma,
11719                                     n_before, n_after, n_div_ma, n_div_bmap,
11720                                     f, c1, c2, g, 1);
11721         }
11722
11723         if (strides)
11724                 res = add_ma_strides(res, ma, n_before, n_after);
11725
11726         isl_int_clear(f);
11727         isl_int_clear(c1);
11728         isl_int_clear(c2);
11729         isl_int_clear(g);
11730         isl_basic_map_free(bmap);
11731         isl_multi_aff_free(ma);
11732         res = isl_basic_set_simplify(res);
11733         return isl_basic_map_finalize(res);
11734 error:
11735         isl_int_clear(f);
11736         isl_int_clear(c1);
11737         isl_int_clear(c2);
11738         isl_int_clear(g);
11739         isl_basic_map_free(bmap);
11740         isl_multi_aff_free(ma);
11741         isl_basic_map_free(res);
11742         return NULL;
11743 }
11744
11745 /* Compute the preimage of "bset" under the function represented by "ma".
11746  * In other words, plug in "ma" in "bset".  The result is a basic set
11747  * that lives in the domain space of "ma".
11748  */
11749 __isl_give isl_basic_set *isl_basic_set_preimage_multi_aff(
11750         __isl_take isl_basic_set *bset, __isl_take isl_multi_aff *ma)
11751 {
11752         return isl_basic_map_preimage_multi_aff(bset, isl_dim_set, ma);
11753 }
11754
11755 /* Check if the range of "ma" is compatible with the domain or range
11756  * (depending on "type") of "map".
11757  * Return -1 if anything is wrong.
11758  */
11759 static int check_map_compatible_range_multi_aff(
11760         __isl_keep isl_map *map, enum isl_dim_type type,
11761         __isl_keep isl_multi_aff *ma)
11762 {
11763         int m;
11764         isl_space *ma_space;
11765
11766         ma_space = isl_multi_aff_get_space(ma);
11767         m = isl_space_tuple_match(map->dim, type, ma_space, isl_dim_out);
11768         isl_space_free(ma_space);
11769         if (m >= 0 && !m)
11770                 isl_die(isl_map_get_ctx(map), isl_error_invalid,
11771                         "spaces don't match", return -1);
11772         return m;
11773 }
11774
11775 /* Compute the preimage of the domain or range (depending on "type")
11776  * of "map" under the function represented by "ma".
11777  * In other words, plug in "ma" in the domain or range of "map".
11778  * The result is a map that lives in the same space as "map"
11779  * except that the domain or range has been replaced by
11780  * the domain space of "ma".
11781  *
11782  * The parameters are assumed to have been aligned.
11783  */
11784 static __isl_give isl_map *map_preimage_multi_aff(__isl_take isl_map *map,
11785         enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11786 {
11787         int i;
11788         isl_space *space;
11789
11790         map = isl_map_cow(map);
11791         ma = isl_multi_aff_align_divs(ma);
11792         if (!map || !ma)
11793                 goto error;
11794         if (check_map_compatible_range_multi_aff(map, type, ma) < 0)
11795                 goto error;
11796
11797         for (i = 0; i < map->n; ++i) {
11798                 map->p[i] = isl_basic_map_preimage_multi_aff(map->p[i], type,
11799                                                         isl_multi_aff_copy(ma));
11800                 if (!map->p[i])
11801                         goto error;
11802         }
11803
11804         space = isl_multi_aff_get_domain_space(ma);
11805         space = isl_space_set(isl_map_get_space(map), type, space);
11806
11807         isl_space_free(map->dim);
11808         map->dim = space;
11809         if (!map->dim)
11810                 goto error;
11811
11812         isl_multi_aff_free(ma);
11813         if (map->n > 1)
11814                 ISL_F_CLR(map, ISL_MAP_DISJOINT);
11815         ISL_F_CLR(map, ISL_SET_NORMALIZED);
11816         return map;
11817 error:
11818         isl_multi_aff_free(ma);
11819         isl_map_free(map);
11820         return NULL;
11821 }
11822
11823 /* Compute the preimage of the domain or range (depending on "type")
11824  * of "map" under the function represented by "ma".
11825  * In other words, plug in "ma" in the domain or range of "map".
11826  * The result is a map that lives in the same space as "map"
11827  * except that the domain or range has been replaced by
11828  * the domain space of "ma".
11829  */
11830 __isl_give isl_map *isl_map_preimage_multi_aff(__isl_take isl_map *map,
11831         enum isl_dim_type type, __isl_take isl_multi_aff *ma)
11832 {
11833         if (!map || !ma)
11834                 goto error;
11835
11836         if (isl_space_match(map->dim, isl_dim_param, ma->space, isl_dim_param))
11837                 return map_preimage_multi_aff(map, type, ma);
11838
11839         if (!isl_space_has_named_params(map->dim) ||
11840             !isl_space_has_named_params(ma->space))
11841                 isl_die(map->ctx, isl_error_invalid,
11842                         "unaligned unnamed parameters", goto error);
11843         map = isl_map_align_params(map, isl_multi_aff_get_space(ma));
11844         ma = isl_multi_aff_align_params(ma, isl_map_get_space(map));
11845
11846         return map_preimage_multi_aff(map, type, ma);
11847 error:
11848         isl_multi_aff_free(ma);
11849         return isl_map_free(map);
11850 }
11851
11852 /* Compute the preimage of "set" under the function represented by "ma".
11853  * In other words, plug in "ma" "set".  The result is a set
11854  * that lives in the domain space of "ma".
11855  */
11856 __isl_give isl_set *isl_set_preimage_multi_aff(__isl_take isl_set *set,
11857         __isl_take isl_multi_aff *ma)
11858 {
11859         return isl_map_preimage_multi_aff(set, isl_dim_set, ma);
11860 }
11861
11862 /* Compute the preimage of the domain of "map" under the function
11863  * represented by "ma".
11864  * In other words, plug in "ma" in the domain of "map".
11865  * The result is a map that lives in the same space as "map"
11866  * except that the domain has been replaced by the domain space of "ma".
11867  */
11868 __isl_give isl_map *isl_map_preimage_domain_multi_aff(__isl_take isl_map *map,
11869         __isl_take isl_multi_aff *ma)
11870 {
11871         return isl_map_preimage_multi_aff(map, isl_dim_in, ma);
11872 }
11873
11874 /* Compute the preimage of "set" under the function represented by "pma".
11875  * In other words, plug in "pma" in "set.  The result is a set
11876  * that lives in the domain space of "pma".
11877  */
11878 static __isl_give isl_set *set_preimage_pw_multi_aff(__isl_take isl_set *set,
11879         __isl_take isl_pw_multi_aff *pma)
11880 {
11881         int i;
11882         isl_set *res;
11883
11884         if (!pma)
11885                 goto error;
11886
11887         if (pma->n == 0) {
11888                 isl_pw_multi_aff_free(pma);
11889                 res = isl_set_empty(isl_set_get_space(set));
11890                 isl_set_free(set);
11891                 return res;
11892         }
11893
11894         res = isl_set_preimage_multi_aff(isl_set_copy(set),
11895                                         isl_multi_aff_copy(pma->p[0].maff));
11896         res = isl_set_intersect(res, isl_set_copy(pma->p[0].set));
11897
11898         for (i = 1; i < pma->n; ++i) {
11899                 isl_set *res_i;
11900
11901                 res_i = isl_set_preimage_multi_aff(isl_set_copy(set),
11902                                         isl_multi_aff_copy(pma->p[i].maff));
11903                 res_i = isl_set_intersect(res_i, isl_set_copy(pma->p[i].set));
11904                 res = isl_set_union(res, res_i);
11905         }
11906
11907         isl_pw_multi_aff_free(pma);
11908         isl_set_free(set);
11909         return res;
11910 error:
11911         isl_pw_multi_aff_free(pma);
11912         isl_set_free(set);
11913         return NULL;
11914 }
11915
11916 __isl_give isl_set *isl_set_preimage_pw_multi_aff(__isl_take isl_set *set,
11917         __isl_take isl_pw_multi_aff *pma)
11918 {
11919         if (!set || !pma)
11920                 goto error;
11921
11922         if (isl_space_match(set->dim, isl_dim_param, pma->dim, isl_dim_param))
11923                 return set_preimage_pw_multi_aff(set, pma);
11924
11925         if (!isl_space_has_named_params(set->dim) ||
11926             !isl_space_has_named_params(pma->dim))
11927                 isl_die(set->ctx, isl_error_invalid,
11928                         "unaligned unnamed parameters", goto error);
11929         set = isl_set_align_params(set, isl_pw_multi_aff_get_space(pma));
11930         pma = isl_pw_multi_aff_align_params(pma, isl_set_get_space(set));
11931
11932         return set_preimage_pw_multi_aff(set, pma);
11933 error:
11934         isl_pw_multi_aff_free(pma);
11935         return isl_set_free(set);
11936 }