isl_space_unwrap: fix error message
[platform/upstream/isl.git] / isl_space.c
1 /*
2  * Copyright 2008-2009 Katholieke Universiteit Leuven
3  * Copyright 2010      INRIA Saclay
4  *
5  * Use of this software is governed by the GNU LGPLv2.1 license
6  *
7  * Written by Sven Verdoolaege, K.U.Leuven, Departement
8  * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9  * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10  * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France 
11  */
12
13 #include <stdlib.h>
14 #include <isl_space_private.h>
15 #include <isl_id_private.h>
16 #include <isl_reordering.h>
17
18 isl_ctx *isl_space_get_ctx(__isl_keep isl_space *dim)
19 {
20         return dim ? dim->ctx : NULL;
21 }
22
23 __isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
24                         unsigned nparam, unsigned n_in, unsigned n_out)
25 {
26         isl_space *dim;
27
28         dim = isl_alloc_type(ctx, struct isl_space);
29         if (!dim)
30                 return NULL;
31
32         dim->ctx = ctx;
33         isl_ctx_ref(ctx);
34         dim->ref = 1;
35         dim->nparam = nparam;
36         dim->n_in = n_in;
37         dim->n_out = n_out;
38
39         dim->tuple_id[0] = NULL;
40         dim->tuple_id[1] = NULL;
41
42         dim->nested[0] = NULL;
43         dim->nested[1] = NULL;
44
45         dim->n_id = 0;
46         dim->ids = NULL;
47
48         return dim;
49 }
50
51 /* Mark the space as being that of a set, by setting the domain tuple
52  * to isl_id_none.
53  */
54 static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
55 {
56         space = isl_space_cow(space);
57         if (!space)
58                 return NULL;
59         space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
60         return space;
61 }
62
63 /* Is the space that of a set?
64  */
65 int isl_space_is_set(__isl_keep isl_space *space)
66 {
67         if (!space)
68                 return -1;
69         if (space->n_in != 0 || space->nested[0])
70                 return 0;
71         if (space->tuple_id[0] != &isl_id_none)
72                 return 0;
73         return 1;
74 }
75
76 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
77                         unsigned nparam, unsigned dim)
78 {
79         isl_space *space;
80         space = isl_space_alloc(ctx, nparam, 0, dim);
81         space = mark_as_set(space);
82         return space;
83 }
84
85 /* Mark the space as being that of a parameter domain, by setting
86  * both tuples to isl_id_none.
87  */
88 static __isl_give isl_space *mark_as_params(isl_space *space)
89 {
90         if (!space)
91                 return NULL;
92         space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
93         space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
94         return space;
95 }
96
97 /* Is the space that of a parameter domain?
98  */
99 int isl_space_is_params(__isl_keep isl_space *space)
100 {
101         if (!space)
102                 return -1;
103         if (space->n_in != 0 || space->nested[0] ||
104             space->n_out != 0 || space->nested[1])
105                 return 0;
106         if (space->tuple_id[0] != &isl_id_none)
107                 return 0;
108         if (space->tuple_id[1] != &isl_id_none)
109                 return 0;
110         return 1;
111 }
112
113 /* Create a space for a parameter domain.
114  */
115 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
116 {
117         isl_space *space;
118         space = isl_space_alloc(ctx, nparam, 0, 0);
119         space = mark_as_params(space);
120         return space;
121 }
122
123 static unsigned global_pos(__isl_keep isl_space *dim,
124                                  enum isl_dim_type type, unsigned pos)
125 {
126         struct isl_ctx *ctx = dim->ctx;
127
128         switch (type) {
129         case isl_dim_param:
130                 isl_assert(ctx, pos < dim->nparam,
131                             return isl_space_dim(dim, isl_dim_all));
132                 return pos;
133         case isl_dim_in:
134                 isl_assert(ctx, pos < dim->n_in,
135                             return isl_space_dim(dim, isl_dim_all));
136                 return pos + dim->nparam;
137         case isl_dim_out:
138                 isl_assert(ctx, pos < dim->n_out,
139                             return isl_space_dim(dim, isl_dim_all));
140                 return pos + dim->nparam + dim->n_in;
141         default:
142                 isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
143         }
144         return isl_space_dim(dim, isl_dim_all);
145 }
146
147 /* Extend length of ids array to the total number of dimensions.
148  */
149 static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
150 {
151         isl_id **ids;
152         int i;
153
154         if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
155                 return dim;
156
157         if (!dim->ids) {
158                 dim->ids = isl_calloc_array(dim->ctx,
159                                 isl_id *, isl_space_dim(dim, isl_dim_all));
160                 if (!dim->ids)
161                         goto error;
162         } else {
163                 ids = isl_realloc_array(dim->ctx, dim->ids,
164                                 isl_id *, isl_space_dim(dim, isl_dim_all));
165                 if (!ids)
166                         goto error;
167                 dim->ids = ids;
168                 for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); ++i)
169                         dim->ids[i] = NULL;
170         }
171
172         dim->n_id = isl_space_dim(dim, isl_dim_all);
173
174         return dim;
175 error:
176         isl_space_free(dim);
177         return NULL;
178 }
179
180 static __isl_give isl_space *set_id(__isl_take isl_space *dim,
181         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
182 {
183         dim = isl_space_cow(dim);
184
185         if (!dim)
186                 goto error;
187
188         pos = global_pos(dim, type, pos);
189         if (pos == isl_space_dim(dim, isl_dim_all))
190                 goto error;
191
192         if (pos >= dim->n_id) {
193                 if (!id)
194                         return dim;
195                 dim = extend_ids(dim);
196                 if (!dim)
197                         goto error;
198         }
199
200         dim->ids[pos] = id;
201
202         return dim;
203 error:
204         isl_id_free(id);
205         isl_space_free(dim);
206         return NULL;
207 }
208
209 static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
210                                  enum isl_dim_type type, unsigned pos)
211 {
212         if (!dim)
213                 return NULL;
214
215         pos = global_pos(dim, type, pos);
216         if (pos == isl_space_dim(dim, isl_dim_all))
217                 return NULL;
218         if (pos >= dim->n_id)
219                 return NULL;
220         return dim->ids[pos];
221 }
222
223 static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
224 {
225         switch (type) {
226         case isl_dim_param:     return 0;
227         case isl_dim_in:        return dim->nparam;
228         case isl_dim_out:       return dim->nparam + dim->n_in;
229         default:                return 0;
230         }
231 }
232
233 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
234 {
235         switch (type) {
236         case isl_dim_param:     return dim->nparam;
237         case isl_dim_in:        return dim->n_in;
238         case isl_dim_out:       return dim->n_out;
239         case isl_dim_all:       return dim->nparam + dim->n_in + dim->n_out;
240         default:                return 0;
241         }
242 }
243
244 unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
245 {
246         if (!dim)
247                 return 0;
248         return n(dim, type);
249 }
250
251 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
252 {
253         if (!dim)
254                 return 0;
255         return offset(dim, type);
256 }
257
258 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
259         enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
260         enum isl_dim_type src_type)
261 {
262         int i;
263         isl_id *id;
264
265         if (!dst)
266                 return NULL;
267
268         for (i = 0; i < n(src, src_type); ++i) {
269                 id = get_id(src, src_type, i);
270                 if (!id)
271                         continue;
272                 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
273                 if (!dst)
274                         return NULL;
275         }
276         return dst;
277 }
278
279 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
280 {
281         isl_space *dup;
282         if (!dim)
283                 return NULL;
284         dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
285         if (dim->tuple_id[0] &&
286             !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
287                 goto error;
288         if (dim->tuple_id[1] &&
289             !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
290                 goto error;
291         if (dim->nested[0] && !(dup->nested[0] = isl_space_copy(dim->nested[0])))
292                 goto error;
293         if (dim->nested[1] && !(dup->nested[1] = isl_space_copy(dim->nested[1])))
294                 goto error;
295         if (!dim->ids)
296                 return dup;
297         dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
298         dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
299         dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
300         return dup;
301 error:
302         isl_space_free(dup);
303         return NULL;
304 }
305
306 __isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
307 {
308         if (!dim)
309                 return NULL;
310
311         if (dim->ref == 1)
312                 return dim;
313         dim->ref--;
314         return isl_space_dup(dim);
315 }
316
317 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
318 {
319         if (!dim)
320                 return NULL;
321
322         dim->ref++;
323         return dim;
324 }
325
326 void isl_space_free(__isl_take isl_space *dim)
327 {
328         int i;
329
330         if (!dim)
331                 return;
332
333         if (--dim->ref > 0)
334                 return;
335
336         isl_id_free(dim->tuple_id[0]);
337         isl_id_free(dim->tuple_id[1]);
338
339         isl_space_free(dim->nested[0]);
340         isl_space_free(dim->nested[1]);
341
342         for (i = 0; i < dim->n_id; ++i)
343                 isl_id_free(dim->ids[i]);
344         free(dim->ids);
345         isl_ctx_deref(dim->ctx);
346         
347         free(dim);
348 }
349
350 /* Check if "s" is a valid dimension or tuple name.
351  * We currently only forbid names that look like a number.
352  *
353  * s is assumed to be non-NULL.
354  */
355 static int name_ok(isl_ctx *ctx, const char *s)
356 {
357         char *p;
358         long dummy;
359
360         dummy = strtol(s, &p, 0);
361         if (p != s)
362                 isl_die(ctx, isl_error_invalid, "name looks like a number",
363                         return 0);
364
365         return 1;
366 }
367
368 /* Is it possible for the given dimension type to have a tuple id?
369  */
370 static int space_can_have_id(__isl_keep isl_space *space,
371         enum isl_dim_type type)
372 {
373         if (!space)
374                 return 0;
375         if (isl_space_is_params(space))
376                 isl_die(space->ctx, isl_error_invalid,
377                         "parameter spaces don't have tuple ids", return 0);
378         if (isl_space_is_set(space) && type != isl_dim_set)
379                 isl_die(space->ctx, isl_error_invalid,
380                         "set spaces can only have a set id", return 0);
381         if (type != isl_dim_in && type != isl_dim_out)
382                 isl_die(space->ctx, isl_error_invalid,
383                         "only input, output and set tuples can have ids",
384                         return 0);
385
386         return 1;
387 }
388
389 /* Does the tuple have an id?
390  */
391 int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
392 {
393         if (!space_can_have_id(dim, type))
394                 return -1;
395         return dim->tuple_id[type - isl_dim_in] != NULL;
396 }
397
398 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
399         enum isl_dim_type type)
400 {
401         int has_id;
402
403         if (!dim)
404                 return NULL;
405         has_id = isl_space_has_tuple_id(dim, type);
406         if (has_id < 0)
407                 return NULL;
408         if (!has_id)
409                 isl_die(dim->ctx, isl_error_invalid,
410                         "tuple has no id", return NULL);
411         return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
412 }
413
414 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
415         enum isl_dim_type type, __isl_take isl_id *id)
416 {
417         dim = isl_space_cow(dim);
418         if (!dim || !id)
419                 goto error;
420         if (type != isl_dim_in && type != isl_dim_out)
421                 isl_die(dim->ctx, isl_error_invalid,
422                         "only input, output and set tuples can have names",
423                         goto error);
424
425         isl_id_free(dim->tuple_id[type - isl_dim_in]);
426         dim->tuple_id[type - isl_dim_in] = id;
427
428         return dim;
429 error:
430         isl_id_free(id);
431         isl_space_free(dim);
432         return NULL;
433 }
434
435 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
436         enum isl_dim_type type)
437 {
438         dim = isl_space_cow(dim);
439         if (!dim)
440                 return NULL;
441         if (type != isl_dim_in && type != isl_dim_out)
442                 isl_die(dim->ctx, isl_error_invalid,
443                         "only input, output and set tuples can have names",
444                         goto error);
445
446         isl_id_free(dim->tuple_id[type - isl_dim_in]);
447         dim->tuple_id[type - isl_dim_in] = NULL;
448
449         return dim;
450 error:
451         isl_space_free(dim);
452         return NULL;
453 }
454
455 /* Set the id of the given dimension of "space" to "id".
456  * If the dimension already has an id, then it is replaced.
457  * If the dimension is a parameter, then we need to change it
458  * in the nested spaces (if any) as well.
459  */
460 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
461         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
462 {
463         space = isl_space_cow(space);
464         if (!space || !id)
465                 goto error;
466
467         if (type == isl_dim_param) {
468                 int i;
469
470                 for (i = 0; i < 2; ++i) {
471                         if (!space->nested[i])
472                                 continue;
473                         space->nested[i] =
474                                 isl_space_set_dim_id(space->nested[i],
475                                                 type, pos, isl_id_copy(id));
476                         if (!space->nested[i])
477                                 goto error;
478                 }
479         }
480
481         isl_id_free(get_id(space, type, pos));
482         return set_id(space, type, pos, id);
483 error:
484         isl_id_free(id);
485         isl_space_free(space);
486         return NULL;
487 }
488
489 /* Reset the id of the given dimension of "space".
490  * If the dimension already has an id, then it is removed.
491  * If the dimension is a parameter, then we need to reset it
492  * in the nested spaces (if any) as well.
493  */
494 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
495         enum isl_dim_type type, unsigned pos)
496 {
497         space = isl_space_cow(space);
498         if (!space)
499                 goto error;
500
501         if (type == isl_dim_param) {
502                 int i;
503
504                 for (i = 0; i < 2; ++i) {
505                         if (!space->nested[i])
506                                 continue;
507                         space->nested[i] =
508                                 isl_space_reset_dim_id(space->nested[i],
509                                                         type, pos);
510                         if (!space->nested[i])
511                                 goto error;
512                 }
513         }
514
515         isl_id_free(get_id(space, type, pos));
516         return set_id(space, type, pos, NULL);
517 error:
518         isl_space_free(space);
519         return NULL;
520 }
521
522 int isl_space_has_dim_id(__isl_keep isl_space *dim,
523         enum isl_dim_type type, unsigned pos)
524 {
525         if (!dim)
526                 return -1;
527         return get_id(dim, type, pos) != NULL;
528 }
529
530 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
531         enum isl_dim_type type, unsigned pos)
532 {
533         if (!dim)
534                 return NULL;
535         if (!get_id(dim, type, pos))
536                 isl_die(dim->ctx, isl_error_invalid,
537                         "dim has no id", return NULL);
538         return isl_id_copy(get_id(dim, type, pos));
539 }
540
541 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
542         enum isl_dim_type type, const char *s)
543 {
544         isl_id *id;
545
546         if (!dim)
547                 return NULL;
548
549         if (!s)
550                 return isl_space_reset_tuple_id(dim, type);
551
552         if (!name_ok(dim->ctx, s))
553                 goto error;
554
555         id = isl_id_alloc(dim->ctx, s, NULL);
556         return isl_space_set_tuple_id(dim, type, id);
557 error:
558         isl_space_free(dim);
559         return NULL;
560 }
561
562 /* Does the tuple have a name?
563  */
564 int isl_space_has_tuple_name(__isl_keep isl_space *space,
565         enum isl_dim_type type)
566 {
567         isl_id *id;
568
569         if (!space_can_have_id(space, type))
570                 return -1;
571         id = space->tuple_id[type - isl_dim_in];
572         return id && id->name;
573 }
574
575 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
576          enum isl_dim_type type)
577 {
578         isl_id *id;
579         if (!dim)
580                 return NULL;
581         if (type != isl_dim_in && type != isl_dim_out)
582                 return NULL;
583         id = dim->tuple_id[type - isl_dim_in];
584         return id ? id->name : NULL;
585 }
586
587 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
588                                  enum isl_dim_type type, unsigned pos,
589                                  const char *s)
590 {
591         isl_id *id;
592
593         if (!dim)
594                 return NULL;
595         if (!s)
596                 return isl_space_reset_dim_id(dim, type, pos);
597         if (!name_ok(dim->ctx, s))
598                 goto error;
599         id = isl_id_alloc(dim->ctx, s, NULL);
600         return isl_space_set_dim_id(dim, type, pos, id);
601 error:
602         isl_space_free(dim);
603         return NULL;
604 }
605
606 /* Does the given dimension have a name?
607  */
608 int isl_space_has_dim_name(__isl_keep isl_space *space,
609         enum isl_dim_type type, unsigned pos)
610 {
611         isl_id *id;
612
613         if (!space)
614                 return -1;
615         id = get_id(space, type, pos);
616         return id && id->name;
617 }
618
619 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
620                                  enum isl_dim_type type, unsigned pos)
621 {
622         isl_id *id = get_id(dim, type, pos);
623         return id ? id->name : NULL;
624 }
625
626 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
627         __isl_keep isl_id *id)
628 {
629         int i;
630         int offset;
631         int n;
632
633         if (!dim || !id)
634                 return -1;
635
636         offset = isl_space_offset(dim, type);
637         n = isl_space_dim(dim, type);
638         for (i = 0; i < n && offset + i < dim->n_id; ++i)
639                 if (dim->ids[offset + i] == id)
640                         return i;
641
642         return -1;
643 }
644
645 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
646         enum isl_dim_type type, const char *name)
647 {
648         int i;
649         int offset;
650         int n;
651
652         if (!space || !name)
653                 return -1;
654
655         offset = isl_space_offset(space, type);
656         n = isl_space_dim(space, type);
657         for (i = 0; i < n && offset + i < space->n_id; ++i)
658                 if (space->ids[offset + i]->name &&
659                     !strcmp(space->ids[offset + i]->name, name))
660                         return i;
661
662         return -1;
663 }
664
665 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
666         enum isl_dim_type type)
667 {
668         if (!dim)
669                 return NULL;
670         if (type == isl_dim_in)
671                 return dim->tuple_id[0];
672         if (type == isl_dim_out)
673                 return dim->tuple_id[1];
674         return NULL;
675 }
676
677 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
678         enum isl_dim_type type)
679 {
680         if (!dim)
681                 return NULL;
682         if (type == isl_dim_in)
683                 return dim->nested[0];
684         if (type == isl_dim_out)
685                 return dim->nested[1];
686         return NULL;
687 }
688
689 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
690                         __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
691 {
692         isl_id *id1, *id2;
693         isl_space *nested1, *nested2;
694
695         if (!dim1 || !dim2)
696                 return -1;
697
698         if (dim1 == dim2 && dim1_type == dim2_type)
699                 return 1;
700
701         if (n(dim1, dim1_type) != n(dim2, dim2_type))
702                 return 0;
703         id1 = tuple_id(dim1, dim1_type);
704         id2 = tuple_id(dim2, dim2_type);
705         if (!id1 ^ !id2)
706                 return 0;
707         if (id1 && id1 != id2)
708                 return 0;
709         nested1 = nested(dim1, dim1_type);
710         nested2 = nested(dim2, dim2_type);
711         if (!nested1 ^ !nested2)
712                 return 0;
713         if (nested1 && !isl_space_is_equal(nested1, nested2))
714                 return 0;
715         return 1;
716 }
717
718 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
719         __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
720 {
721         int i;
722
723         if (dim1 == dim2 && dim1_type == dim2_type)
724                 return 1;
725
726         if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
727                 return 0;
728
729         if (!dim1->ids && !dim2->ids)
730                 return 1;
731
732         for (i = 0; i < n(dim1, dim1_type); ++i) {
733                 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
734                         return 0;
735         }
736         return 1;
737 }
738
739 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
740         __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
741 {
742         if (!dim1 || !dim2)
743                 return -1;
744
745         return match(dim1, dim1_type, dim2, dim2_type);
746 }
747
748 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
749         unsigned first, unsigned n, __isl_keep isl_id **ids)
750 {
751         int i;
752
753         for (i = 0; i < n ; ++i)
754                 ids[i] = get_id(dim, type, first + i);
755 }
756
757 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
758                         unsigned nparam, unsigned n_in, unsigned n_out)
759 {
760         isl_id **ids = NULL;
761
762         if (!dim)
763                 return NULL;
764         if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
765                 return dim;
766
767         isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
768         isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
769         isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
770
771         dim = isl_space_cow(dim);
772
773         if (dim->ids) {
774                 ids = isl_calloc_array(dim->ctx, isl_id *,
775                                          nparam + n_in + n_out);
776                 if (!ids)
777                         goto error;
778                 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
779                 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
780                 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
781                 free(dim->ids);
782                 dim->ids = ids;
783                 dim->n_id = nparam + n_in + n_out;
784         }
785         dim->nparam = nparam;
786         dim->n_in = n_in;
787         dim->n_out = n_out;
788
789         return dim;
790 error:
791         free(ids);
792         isl_space_free(dim);
793         return NULL;
794 }
795
796 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
797         enum isl_dim_type type, unsigned n)
798 {
799         if (!dim)
800                 return NULL;
801         dim = isl_space_reset(dim, type);
802         switch (type) {
803         case isl_dim_param:
804                 dim = isl_space_extend(dim,
805                                         dim->nparam + n, dim->n_in, dim->n_out);
806                 if (dim && dim->nested[0] &&
807                     !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
808                                                     isl_dim_param, n)))
809                         goto error;
810                 if (dim && dim->nested[1] &&
811                     !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
812                                                     isl_dim_param, n)))
813                         goto error;
814                 return dim;
815         case isl_dim_in:
816                 return isl_space_extend(dim,
817                                         dim->nparam, dim->n_in + n, dim->n_out);
818         case isl_dim_out:
819                 return isl_space_extend(dim,
820                                         dim->nparam, dim->n_in, dim->n_out + n);
821         default:
822                 isl_die(dim->ctx, isl_error_invalid,
823                         "cannot add dimensions of specified type", goto error);
824         }
825 error:
826         isl_space_free(dim);
827         return NULL;
828 }
829
830 static int valid_dim_type(enum isl_dim_type type)
831 {
832         switch (type) {
833         case isl_dim_param:
834         case isl_dim_in:
835         case isl_dim_out:
836                 return 1;
837         default:
838                 return 0;
839         }
840 }
841
842 /* Insert "n" dimensions of type "type" at position "pos".
843  * If we are inserting parameters, then they are also inserted in
844  * any nested spaces.
845  */
846 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
847         enum isl_dim_type type, unsigned pos, unsigned n)
848 {
849         isl_id **ids = NULL;
850
851         if (!dim)
852                 return NULL;
853         if (n == 0)
854                 return isl_space_reset(dim, type);
855
856         if (!valid_dim_type(type))
857                 isl_die(dim->ctx, isl_error_invalid,
858                         "cannot insert dimensions of specified type",
859                         goto error);
860
861         isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
862
863         dim = isl_space_cow(dim);
864         if (!dim)
865                 return NULL;
866
867         if (dim->ids) {
868                 enum isl_dim_type t;
869                 int off;
870                 int s[3];
871                 int *size = s - isl_dim_param;
872                 ids = isl_calloc_array(dim->ctx, isl_id *,
873                                      dim->nparam + dim->n_in + dim->n_out + n);
874                 if (!ids)
875                         goto error;
876                 off = 0;
877                 size[isl_dim_param] = dim->nparam;
878                 size[isl_dim_in] = dim->n_in;
879                 size[isl_dim_out] = dim->n_out;
880                 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
881                         if (t != type) {
882                                 get_ids(dim, t, 0, size[t], ids + off);
883                                 off += size[t];
884                         } else {
885                                 get_ids(dim, t, 0, pos, ids + off);
886                                 off += pos + n;
887                                 get_ids(dim, t, pos, size[t] - pos, ids + off);
888                                 off += size[t] - pos;
889                         }
890                 }
891                 free(dim->ids);
892                 dim->ids = ids;
893                 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
894         }
895         switch (type) {
896         case isl_dim_param:     dim->nparam += n; break;
897         case isl_dim_in:        dim->n_in += n; break;
898         case isl_dim_out:       dim->n_out += n; break;
899         default:                ;
900         }
901         dim = isl_space_reset(dim, type);
902
903         if (type == isl_dim_param) {
904                 if (dim && dim->nested[0] &&
905                     !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
906                                                     isl_dim_param, pos, n)))
907                         goto error;
908                 if (dim && dim->nested[1] &&
909                     !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
910                                                     isl_dim_param, pos, n)))
911                         goto error;
912         }
913
914         return dim;
915 error:
916         isl_space_free(dim);
917         return NULL;
918 }
919
920 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
921         enum isl_dim_type dst_type, unsigned dst_pos,
922         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
923 {
924         int i;
925
926         if (!dim)
927                 return NULL;
928         if (n == 0)
929                 return dim;
930
931         isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
932                 goto error);
933
934         if (dst_type == src_type && dst_pos == src_pos)
935                 return dim;
936
937         isl_assert(dim->ctx, dst_type != src_type, goto error);
938
939         dim = isl_space_reset(dim, src_type);
940         dim = isl_space_reset(dim, dst_type);
941
942         dim = isl_space_cow(dim);
943         if (!dim)
944                 return NULL;
945
946         if (dim->ids) {
947                 isl_id **ids;
948                 enum isl_dim_type t;
949                 int off;
950                 int s[3];
951                 int *size = s - isl_dim_param;
952                 ids = isl_calloc_array(dim->ctx, isl_id *,
953                                          dim->nparam + dim->n_in + dim->n_out);
954                 if (!ids)
955                         goto error;
956                 off = 0;
957                 size[isl_dim_param] = dim->nparam;
958                 size[isl_dim_in] = dim->n_in;
959                 size[isl_dim_out] = dim->n_out;
960                 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
961                         if (t == dst_type) {
962                                 get_ids(dim, t, 0, dst_pos, ids + off);
963                                 off += dst_pos;
964                                 get_ids(dim, src_type, src_pos, n, ids + off);
965                                 off += n;
966                                 get_ids(dim, t, dst_pos, size[t] - dst_pos,
967                                                 ids + off);
968                                 off += size[t] - dst_pos;
969                         } else if (t == src_type) {
970                                 get_ids(dim, t, 0, src_pos, ids + off);
971                                 off += src_pos;
972                                 get_ids(dim, t, src_pos + n,
973                                             size[t] - src_pos - n, ids + off);
974                                 off += size[t] - src_pos - n;
975                         } else {
976                                 get_ids(dim, t, 0, size[t], ids + off);
977                                 off += size[t];
978                         }
979                 }
980                 free(dim->ids);
981                 dim->ids = ids;
982                 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
983         }
984
985         switch (dst_type) {
986         case isl_dim_param:     dim->nparam += n; break;
987         case isl_dim_in:        dim->n_in += n; break;
988         case isl_dim_out:       dim->n_out += n; break;
989         default:                ;
990         }
991
992         switch (src_type) {
993         case isl_dim_param:     dim->nparam -= n; break;
994         case isl_dim_in:        dim->n_in -= n; break;
995         case isl_dim_out:       dim->n_out -= n; break;
996         default:                ;
997         }
998
999         if (dst_type != isl_dim_param && src_type != isl_dim_param)
1000                 return dim;
1001
1002         for (i = 0; i < 2; ++i) {
1003                 if (!dim->nested[i])
1004                         continue;
1005                 dim->nested[i] = isl_space_replace(dim->nested[i],
1006                                                  isl_dim_param, dim);
1007                 if (!dim->nested[i])
1008                         goto error;
1009         }
1010
1011         return dim;
1012 error:
1013         isl_space_free(dim);
1014         return NULL;
1015 }
1016
1017 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1018         __isl_take isl_space *right)
1019 {
1020         isl_space *dim;
1021
1022         if (!left || !right)
1023                 goto error;
1024
1025         isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1026                         goto error);
1027         isl_assert(left->ctx,
1028                 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
1029                 goto error);
1030
1031         dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1032         if (!dim)
1033                 goto error;
1034
1035         dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1036         dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1037         dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1038
1039         if (dim && left->tuple_id[0] &&
1040             !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1041                 goto error;
1042         if (dim && right->tuple_id[1] &&
1043             !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1044                 goto error;
1045         if (dim && left->nested[0] &&
1046             !(dim->nested[0] = isl_space_copy(left->nested[0])))
1047                 goto error;
1048         if (dim && right->nested[1] &&
1049             !(dim->nested[1] = isl_space_copy(right->nested[1])))
1050                 goto error;
1051
1052         isl_space_free(left);
1053         isl_space_free(right);
1054
1055         return dim;
1056 error:
1057         isl_space_free(left);
1058         isl_space_free(right);
1059         return NULL;
1060 }
1061
1062 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1063         __isl_take isl_space *right)
1064 {
1065         isl_space *dom1, *dom2, *nest1, *nest2;
1066
1067         if (!left || !right)
1068                 goto error;
1069
1070         isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1071                         goto error);
1072
1073         dom1 = isl_space_domain(isl_space_copy(left));
1074         dom2 = isl_space_domain(isl_space_copy(right));
1075         nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1076
1077         dom1 = isl_space_range(left);
1078         dom2 = isl_space_range(right);
1079         nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1080
1081         return isl_space_join(isl_space_reverse(nest1), nest2);
1082 error:
1083         isl_space_free(left);
1084         isl_space_free(right);
1085         return NULL;
1086 }
1087
1088 /* Given two spaces { A -> C } and { B -> C }, construct the space
1089  * { [A -> B] -> C }
1090  */
1091 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1092         __isl_take isl_space *right)
1093 {
1094         isl_space *ran, *dom1, *dom2, *nest;
1095
1096         if (!left || !right)
1097                 goto error;
1098
1099         if (!match(left, isl_dim_param, right, isl_dim_param))
1100                 isl_die(left->ctx, isl_error_invalid,
1101                         "parameters need to match", goto error);
1102         if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1103                 isl_die(left->ctx, isl_error_invalid,
1104                         "ranges need to match", goto error);
1105
1106         ran = isl_space_range(isl_space_copy(left));
1107
1108         dom1 = isl_space_domain(left);
1109         dom2 = isl_space_domain(right);
1110         nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1111
1112         return isl_space_join(isl_space_reverse(nest), ran);
1113 error:
1114         isl_space_free(left);
1115         isl_space_free(right);
1116         return NULL;
1117 }
1118
1119 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1120         __isl_take isl_space *right)
1121 {
1122         isl_space *dom, *ran1, *ran2, *nest;
1123
1124         if (!left || !right)
1125                 goto error;
1126
1127         isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1128                         goto error);
1129         if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1130                 isl_die(left->ctx, isl_error_invalid,
1131                         "domains need to match", goto error);
1132
1133         dom = isl_space_domain(isl_space_copy(left));
1134
1135         ran1 = isl_space_range(left);
1136         ran2 = isl_space_range(right);
1137         nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1138
1139         return isl_space_join(isl_space_reverse(dom), nest);
1140 error:
1141         isl_space_free(left);
1142         isl_space_free(right);
1143         return NULL;
1144 }
1145
1146 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1147 {
1148         isl_ctx *ctx;
1149         isl_id **ids = NULL;
1150
1151         if (!dim)
1152                 return NULL;
1153         ctx = isl_space_get_ctx(dim);
1154         if (!isl_space_is_set(dim))
1155                 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1156         dim = isl_space_cow(dim);
1157         if (!dim)
1158                 return NULL;
1159         if (dim->ids) {
1160                 ids = isl_calloc_array(dim->ctx, isl_id *,
1161                                         dim->nparam + dim->n_out + dim->n_out);
1162                 if (!ids)
1163                         goto error;
1164                 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1165                 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1166         }
1167         dim->n_in = dim->n_out;
1168         if (ids) {
1169                 free(dim->ids);
1170                 dim->ids = ids;
1171                 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1172                 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1173         }
1174         isl_id_free(dim->tuple_id[0]);
1175         dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1176         isl_space_free(dim->nested[0]);
1177         dim->nested[0] = isl_space_copy(dim->nested[1]);
1178         return dim;
1179 error:
1180         isl_space_free(dim);
1181         return NULL;
1182 }
1183
1184 __isl_give isl_space *isl_space_map_from_domain_and_range(
1185         __isl_take isl_space *domain, __isl_take isl_space *range)
1186 {
1187         if (!domain || !range)
1188                 goto error;
1189         if (!isl_space_is_set(domain))
1190                 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1191                         "domain is not a set space", goto error);
1192         if (!isl_space_is_set(range))
1193                 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1194                         "range is not a set space", goto error);
1195         return isl_space_join(isl_space_reverse(domain), range);
1196 error:
1197         isl_space_free(domain);
1198         isl_space_free(range);
1199         return NULL;
1200 }
1201
1202 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1203         enum isl_dim_type type,
1204         unsigned first, unsigned n, __isl_take isl_id **ids)
1205 {
1206         int i;
1207
1208         for (i = 0; i < n ; ++i)
1209                 dim = set_id(dim, type, first + i, ids[i]);
1210
1211         return dim;
1212 }
1213
1214 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1215 {
1216         unsigned t;
1217         isl_space *nested;
1218         isl_id **ids = NULL;
1219         isl_id *id;
1220
1221         if (!dim)
1222                 return NULL;
1223         if (match(dim, isl_dim_in, dim, isl_dim_out))
1224                 return dim;
1225
1226         dim = isl_space_cow(dim);
1227         if (!dim)
1228                 return NULL;
1229
1230         id = dim->tuple_id[0];
1231         dim->tuple_id[0] = dim->tuple_id[1];
1232         dim->tuple_id[1] = id;
1233
1234         nested = dim->nested[0];
1235         dim->nested[0] = dim->nested[1];
1236         dim->nested[1] = nested;
1237
1238         if (dim->ids) {
1239                 ids = isl_alloc_array(dim->ctx, isl_id *,
1240                                         dim->n_in + dim->n_out);
1241                 if (!ids)
1242                         goto error;
1243                 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1244                 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1245         }
1246
1247         t = dim->n_in;
1248         dim->n_in = dim->n_out;
1249         dim->n_out = t;
1250
1251         if (dim->ids) {
1252                 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1253                 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1254                 free(ids);
1255         }
1256
1257         return dim;
1258 error:
1259         free(ids);
1260         isl_space_free(dim);
1261         return NULL;
1262 }
1263
1264 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1265         enum isl_dim_type type, unsigned first, unsigned num)
1266 {
1267         int i;
1268
1269         if (!dim)
1270                 return NULL;
1271
1272         if (num == 0)
1273                 return isl_space_reset(dim, type);
1274
1275         if (!valid_dim_type(type))
1276                 isl_die(dim->ctx, isl_error_invalid,
1277                         "cannot drop dimensions of specified type", goto error);
1278
1279         isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1280         dim = isl_space_cow(dim);
1281         if (!dim)
1282                 goto error;
1283         if (dim->ids) {
1284                 dim = extend_ids(dim);
1285                 if (!dim)
1286                         goto error;
1287                 for (i = 0; i < num; ++i)
1288                         isl_id_free(get_id(dim, type, first + i));
1289                 for (i = first+num; i < n(dim, type); ++i)
1290                         set_id(dim, type, i - num, get_id(dim, type, i));
1291                 switch (type) {
1292                 case isl_dim_param:
1293                         get_ids(dim, isl_dim_in, 0, dim->n_in,
1294                                 dim->ids + offset(dim, isl_dim_in) - num);
1295                 case isl_dim_in:
1296                         get_ids(dim, isl_dim_out, 0, dim->n_out,
1297                                 dim->ids + offset(dim, isl_dim_out) - num);
1298                 default:
1299                         ;
1300                 }
1301                 dim->n_id -= num;
1302         }
1303         switch (type) {
1304         case isl_dim_param:     dim->nparam -= num; break;
1305         case isl_dim_in:        dim->n_in -= num; break;
1306         case isl_dim_out:       dim->n_out -= num; break;
1307         default:                ;
1308         }
1309         dim = isl_space_reset(dim, type);
1310         if (type == isl_dim_param) {
1311                 if (dim && dim->nested[0] &&
1312                     !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1313                                                     isl_dim_param, first, num)))
1314                         goto error;
1315                 if (dim && dim->nested[1] &&
1316                     !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1317                                                     isl_dim_param, first, num)))
1318                         goto error;
1319         }
1320         return dim;
1321 error:
1322         isl_space_free(dim);
1323         return NULL;
1324 }
1325
1326 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1327                 unsigned first, unsigned n)
1328 {
1329         if (!dim)
1330                 return NULL;
1331         return isl_space_drop_dims(dim, isl_dim_in, first, n);
1332 }
1333
1334 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1335                 unsigned first, unsigned n)
1336 {
1337         if (!dim)
1338                 return NULL;
1339         return isl_space_drop_dims(dim, isl_dim_out, first, n);
1340 }
1341
1342 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1343 {
1344         if (!dim)
1345                 return NULL;
1346         dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1347         dim = isl_space_reverse(dim);
1348         dim = mark_as_set(dim);
1349         return dim;
1350 }
1351
1352 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1353 {
1354         if (!dim)
1355                 return NULL;
1356         if (!isl_space_is_set(dim))
1357                 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1358                         "not a set space", goto error);
1359         dim = isl_space_reverse(dim);
1360         dim = isl_space_reset(dim, isl_dim_out);
1361         return dim;
1362 error:
1363         isl_space_free(dim);
1364         return NULL;
1365 }
1366
1367 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1368 {
1369         if (!dim)
1370                 return NULL;
1371         dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1372         dim = mark_as_set(dim);
1373         return dim;
1374 }
1375
1376 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1377 {
1378         if (!dim)
1379                 return NULL;
1380         if (!isl_space_is_set(dim))
1381                 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1382                         "not a set space", goto error);
1383         return isl_space_reset(dim, isl_dim_in);
1384 error:
1385         isl_space_free(dim);
1386         return NULL;
1387 }
1388
1389 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1390 {
1391         if (isl_space_is_params(space))
1392                 return space;
1393         space = isl_space_drop_dims(space,
1394                             isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1395         space = isl_space_drop_dims(space,
1396                             isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1397         space = mark_as_params(space);
1398         return space;
1399 }
1400
1401 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1402 {
1403         if (!space)
1404                 return NULL;
1405         if (!isl_space_is_params(space))
1406                 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1407                         "not a parameter space", goto error);
1408         return isl_space_reset(space, isl_dim_set);
1409 error:
1410         isl_space_free(space);
1411         return NULL;
1412 }
1413
1414 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1415 {
1416         dim = isl_space_cow(dim);
1417         if (!dim)
1418                 return NULL;
1419
1420         dim->n_out += dim->n_in;
1421         dim->n_in = 0;
1422         dim = isl_space_reset(dim, isl_dim_in);
1423         dim = isl_space_reset(dim, isl_dim_out);
1424
1425         return dim;
1426 }
1427
1428 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1429         unsigned n_div)
1430 {
1431         int i;
1432
1433         if (!dim)
1434                 return NULL;
1435         if (n_div == 0 &&
1436             dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1437                 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1438         dim = isl_space_cow(dim);
1439         if (!dim)
1440                 return NULL;
1441         dim->n_out += dim->nparam + dim->n_in + n_div;
1442         dim->nparam = 0;
1443         dim->n_in = 0;
1444
1445         for (i = 0; i < dim->n_id; ++i)
1446                 isl_id_free(get_id(dim, isl_dim_out, i));
1447         dim->n_id = 0;
1448         dim = isl_space_reset(dim, isl_dim_in);
1449         dim = isl_space_reset(dim, isl_dim_out);
1450
1451         return dim;
1452 }
1453
1454 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1455 {
1456         if (!dim1 || !dim2)
1457                 return -1;
1458         if (dim1 == dim2)
1459                 return 1;
1460         return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1461                isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1462                isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1463 }
1464
1465 /* Is space1 equal to the domain of space2?
1466  */
1467 int isl_space_is_domain(__isl_keep isl_space *space1,
1468         __isl_keep isl_space *space2)
1469 {
1470         if (!space1 || !space2)
1471                 return -1;
1472         if (!isl_space_is_set(space1))
1473                 return 0;
1474         return match(space1, isl_dim_param, space2, isl_dim_param) &&
1475                isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1476 }
1477
1478 int isl_space_compatible(__isl_keep isl_space *dim1,
1479         __isl_keep isl_space *dim2)
1480 {
1481         return dim1->nparam == dim2->nparam &&
1482                dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1483 }
1484
1485 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1486 {
1487         int i;
1488         isl_id *id;
1489
1490         if (!dim)
1491                 return hash;
1492
1493         hash = isl_hash_builtin(hash, dim->nparam);
1494         hash = isl_hash_builtin(hash, dim->n_in);
1495         hash = isl_hash_builtin(hash, dim->n_out);
1496
1497         for (i = 0; i < dim->nparam; ++i) {
1498                 id = get_id(dim, isl_dim_param, i);
1499                 hash = isl_hash_id(hash, id);
1500         }
1501
1502         id = tuple_id(dim, isl_dim_in);
1503         hash = isl_hash_id(hash, id);
1504         id = tuple_id(dim, isl_dim_out);
1505         hash = isl_hash_id(hash, id);
1506
1507         hash = isl_hash_dim(hash, dim->nested[0]);
1508         hash = isl_hash_dim(hash, dim->nested[1]);
1509
1510         return hash;
1511 }
1512
1513 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1514 {
1515         uint32_t hash;
1516
1517         if (!dim)
1518                 return 0;
1519
1520         hash = isl_hash_init();
1521         hash = isl_hash_dim(hash, dim);
1522
1523         return hash;
1524 }
1525
1526 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1527 {
1528         if (!dim)
1529                 return -1;
1530
1531         if (!isl_space_is_set(dim))
1532                 return 0;
1533
1534         return dim->nested[1] != NULL;
1535 }
1536
1537 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1538 {
1539         isl_space *wrap;
1540
1541         if (!dim)
1542                 return NULL;
1543
1544         wrap = isl_space_set_alloc(dim->ctx,
1545                                     dim->nparam, dim->n_in + dim->n_out);
1546
1547         wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1548         wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1549         wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1550
1551         if (!wrap)
1552                 goto error;
1553
1554         wrap->nested[1] = dim;
1555
1556         return wrap;
1557 error:
1558         isl_space_free(dim);
1559         return NULL;
1560 }
1561
1562 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1563 {
1564         isl_space *unwrap;
1565
1566         if (!dim)
1567                 return NULL;
1568
1569         if (!isl_space_is_wrapping(dim))
1570                 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
1571                         goto error);
1572
1573         unwrap = isl_space_copy(dim->nested[1]);
1574         isl_space_free(dim);
1575
1576         return unwrap;
1577 error:
1578         isl_space_free(dim);
1579         return NULL;
1580 }
1581
1582 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1583 {
1584         if (type != isl_dim_in && type != isl_dim_out)
1585                 return 0;
1586         if (!dim)
1587                 return -1;
1588         if (dim->tuple_id[type - isl_dim_in])
1589                 return 1;
1590         if (dim->nested[type - isl_dim_in])
1591                 return 1;
1592         return 0;
1593 }
1594
1595 int isl_space_may_be_set(__isl_keep isl_space *dim)
1596 {
1597         if (!dim)
1598                 return -1;
1599         if (isl_space_is_set(dim))
1600                 return 1;
1601         if (isl_space_dim(dim, isl_dim_in) != 0)
1602                 return 0;
1603         if (isl_space_is_named_or_nested(dim, isl_dim_in))
1604                 return 0;
1605         return 1;
1606 }
1607
1608 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1609         enum isl_dim_type type)
1610 {
1611         if (!isl_space_is_named_or_nested(dim, type))
1612                 return dim;
1613
1614         dim = isl_space_cow(dim);
1615         if (!dim)
1616                 return NULL;
1617
1618         isl_id_free(dim->tuple_id[type - isl_dim_in]);
1619         dim->tuple_id[type - isl_dim_in] = NULL;
1620         isl_space_free(dim->nested[type - isl_dim_in]);
1621         dim->nested[type - isl_dim_in] = NULL;
1622
1623         return dim;
1624 }
1625
1626 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1627 {
1628         if (!dim)
1629                 return NULL;
1630         if (!dim->nested[0] && !dim->nested[1])
1631                 return dim;
1632
1633         if (dim->nested[0])
1634                 dim = isl_space_reset(dim, isl_dim_in);
1635         if (dim && dim->nested[1])
1636                 dim = isl_space_reset(dim, isl_dim_out);
1637
1638         return dim;
1639 }
1640
1641 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1642 {
1643         if (!dim)
1644                 return NULL;
1645         if (!dim->nested[0])
1646                 return dim;
1647
1648         return isl_space_reset(dim, isl_dim_in);
1649 }
1650
1651 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1652 {
1653         if (!dim)
1654                 return NULL;
1655         if (!dim->nested[1])
1656                 return dim;
1657
1658         return isl_space_reset(dim, isl_dim_out);
1659 }
1660
1661 /* Replace the dimensions of the given type of dst by those of src.
1662  */
1663 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1664         enum isl_dim_type type, __isl_keep isl_space *src)
1665 {
1666         dst = isl_space_cow(dst);
1667
1668         if (!dst || !src)
1669                 goto error;
1670
1671         dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1672         dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1673         dst = copy_ids(dst, type, 0, src, type);
1674
1675         if (dst && type == isl_dim_param) {
1676                 int i;
1677                 for (i = 0; i <= 1; ++i) {
1678                         if (!dst->nested[i])
1679                                 continue;
1680                         dst->nested[i] = isl_space_replace(dst->nested[i],
1681                                                          type, src);
1682                         if (!dst->nested[i])
1683                                 goto error;
1684                 }
1685         }
1686
1687         return dst;
1688 error:
1689         isl_space_free(dst);
1690         return NULL;
1691 }
1692
1693 /* Given a dimension specification "dim" of a set, create a dimension
1694  * specification for the lift of the set.  In particular, the result
1695  * is of the form [dim -> local[..]], with n_local variables in the
1696  * range of the wrapped map.
1697  */
1698 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1699 {
1700         isl_space *local_dim;
1701
1702         if (!dim)
1703                 return NULL;
1704
1705         local_dim = isl_space_dup(dim);
1706         local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1707         local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1708         local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1709         dim = isl_space_join(isl_space_from_domain(dim),
1710                             isl_space_from_range(local_dim));
1711         dim = isl_space_wrap(dim);
1712         dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1713
1714         return dim;
1715 }
1716
1717 int isl_space_can_zip(__isl_keep isl_space *dim)
1718 {
1719         if (!dim)
1720                 return -1;
1721
1722         return dim->nested[0] && dim->nested[1];
1723 }
1724
1725 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1726 {
1727         isl_space *dom, *ran;
1728         isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1729
1730         if (!isl_space_can_zip(dim))
1731                 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1732                         goto error);
1733
1734         if (!dim)
1735                 return NULL;
1736         dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1737         ran = isl_space_unwrap(isl_space_range(dim));
1738         dom_dom = isl_space_domain(isl_space_copy(dom));
1739         dom_ran = isl_space_range(dom);
1740         ran_dom = isl_space_domain(isl_space_copy(ran));
1741         ran_ran = isl_space_range(ran);
1742         dom = isl_space_join(isl_space_from_domain(dom_dom),
1743                            isl_space_from_range(ran_dom));
1744         ran = isl_space_join(isl_space_from_domain(dom_ran),
1745                            isl_space_from_range(ran_ran));
1746         return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1747                             isl_space_from_range(isl_space_wrap(ran)));
1748 error:
1749         isl_space_free(dim);
1750         return NULL;
1751 }
1752
1753 /* Can we apply isl_space_curry to "space"?
1754  * That is, does it have a nested relation in its domain?
1755  */
1756 int isl_space_can_curry(__isl_keep isl_space *space)
1757 {
1758         if (!space)
1759                 return -1;
1760
1761         return !!space->nested[0];
1762 }
1763
1764 /* Given a space (A -> B) -> C, return the corresponding space
1765  * A -> (B -> C).
1766  */
1767 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
1768 {
1769         isl_space *dom, *ran;
1770         isl_space *dom_dom, *dom_ran;
1771
1772         if (!space)
1773                 return NULL;
1774
1775         if (!isl_space_can_curry(space))
1776                 isl_die(space->ctx, isl_error_invalid,
1777                         "space cannot be curried", goto error);
1778
1779         dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
1780         ran = isl_space_range(space);
1781         dom_dom = isl_space_domain(isl_space_copy(dom));
1782         dom_ran = isl_space_range(dom);
1783         ran = isl_space_join(isl_space_from_domain(dom_ran),
1784                            isl_space_from_range(ran));
1785         return isl_space_join(isl_space_from_domain(dom_dom),
1786                             isl_space_from_range(isl_space_wrap(ran)));
1787 error:
1788         isl_space_free(space);
1789         return NULL;
1790 }
1791
1792 int isl_space_has_named_params(__isl_keep isl_space *dim)
1793 {
1794         int i;
1795         unsigned off;
1796
1797         if (!dim)
1798                 return -1;
1799         if (dim->nparam == 0)
1800                 return 1;
1801         off = isl_space_offset(dim, isl_dim_param);
1802         if (off + dim->nparam > dim->n_id)
1803                 return 0;
1804         for (i = 0; i < dim->nparam; ++i)
1805                 if (!dim->ids[off + i])
1806                         return 0;
1807         return 1;
1808 }
1809
1810 /* Align the initial parameters of dim1 to match the order in dim2.
1811  */
1812 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1813         __isl_take isl_space *dim2)
1814 {
1815         isl_reordering *exp;
1816
1817         if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1818                 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1819                         "parameter alignment requires named parameters",
1820                         goto error);
1821
1822         dim2 = isl_space_params(dim2);
1823         exp = isl_parameter_alignment_reordering(dim1, dim2);
1824         exp = isl_reordering_extend_space(exp, dim1);
1825         isl_space_free(dim2);
1826         if (!exp)
1827                 return NULL;
1828         dim1 = isl_space_copy(exp->dim);
1829         isl_reordering_free(exp);
1830         return dim1;
1831 error:
1832         isl_space_free(dim1);
1833         isl_space_free(dim2);
1834         return NULL;
1835 }
1836
1837 /* Given the space of set (domain), construct a space for a map
1838  * with as domain the given space and as range the range of "model".
1839  */
1840 __isl_give isl_space *isl_space_extend_domain_with_range(
1841         __isl_take isl_space *domain, __isl_take isl_space *model)
1842 {
1843         isl_space *space;
1844
1845         space = isl_space_from_domain(domain);
1846         space = isl_space_add_dims(space, isl_dim_out,
1847                                     isl_space_dim(model, isl_dim_out));
1848         if (isl_space_has_tuple_id(model, isl_dim_out))
1849                 space = isl_space_set_tuple_id(space, isl_dim_out,
1850                                 isl_space_get_tuple_id(model, isl_dim_out));
1851         isl_space_free(model);
1852         return space;
1853 }