isl_space_set_dim_name: handle NULL name
[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 static int name_ok(isl_ctx *ctx, const char *s)
351 {
352         char *p;
353         long dummy;
354
355         dummy = strtol(s, &p, 0);
356         if (p != s)
357                 isl_die(ctx, isl_error_invalid, "name looks like a number",
358                         return 0);
359
360         return 1;
361 }
362
363 int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
364 {
365         if (!dim)
366                 return -1;
367         if (isl_space_is_params(dim))
368                 isl_die(dim->ctx, isl_error_invalid,
369                         "parameter spaces don't have tuple ids", return -1);
370         if (isl_space_is_set(dim) && type != isl_dim_set)
371                 isl_die(dim->ctx, isl_error_invalid,
372                         "set spaces can only have a set id", return -1);
373         if (type != isl_dim_in && type != isl_dim_out)
374                 isl_die(dim->ctx, isl_error_invalid,
375                         "only input, output and set tuples can have ids",
376                         return -1);
377         return dim->tuple_id[type - isl_dim_in] != NULL;
378 }
379
380 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
381         enum isl_dim_type type)
382 {
383         int has_id;
384
385         if (!dim)
386                 return NULL;
387         has_id = isl_space_has_tuple_id(dim, type);
388         if (has_id < 0)
389                 return NULL;
390         if (!has_id)
391                 isl_die(dim->ctx, isl_error_invalid,
392                         "tuple has no id", return NULL);
393         return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
394 }
395
396 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
397         enum isl_dim_type type, __isl_take isl_id *id)
398 {
399         dim = isl_space_cow(dim);
400         if (!dim || !id)
401                 goto error;
402         if (type != isl_dim_in && type != isl_dim_out)
403                 isl_die(dim->ctx, isl_error_invalid,
404                         "only input, output and set tuples can have names",
405                         goto error);
406
407         isl_id_free(dim->tuple_id[type - isl_dim_in]);
408         dim->tuple_id[type - isl_dim_in] = id;
409
410         return dim;
411 error:
412         isl_id_free(id);
413         isl_space_free(dim);
414         return NULL;
415 }
416
417 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
418         enum isl_dim_type type)
419 {
420         dim = isl_space_cow(dim);
421         if (!dim)
422                 return NULL;
423         if (type != isl_dim_in && type != isl_dim_out)
424                 isl_die(dim->ctx, isl_error_invalid,
425                         "only input, output and set tuples can have names",
426                         goto error);
427
428         isl_id_free(dim->tuple_id[type - isl_dim_in]);
429         dim->tuple_id[type - isl_dim_in] = NULL;
430
431         return dim;
432 error:
433         isl_space_free(dim);
434         return NULL;
435 }
436
437 /* Set the id of the given dimension of "space" to "id".
438  * If the dimension already has an id, then it is replaced.
439  * If the dimension is a parameter, then we need to change it
440  * in the nested spaces (if any) as well.
441  */
442 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
443         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
444 {
445         space = isl_space_cow(space);
446         if (!space || !id)
447                 goto error;
448
449         if (type == isl_dim_param) {
450                 int i;
451
452                 for (i = 0; i < 2; ++i) {
453                         if (!space->nested[i])
454                                 continue;
455                         space->nested[i] =
456                                 isl_space_set_dim_id(space->nested[i],
457                                                 type, pos, isl_id_copy(id));
458                         if (!space->nested[i])
459                                 goto error;
460                 }
461         }
462
463         isl_id_free(get_id(space, type, pos));
464         return set_id(space, type, pos, id);
465 error:
466         isl_id_free(id);
467         isl_space_free(space);
468         return NULL;
469 }
470
471 /* Reset the id of the given dimension of "space".
472  * If the dimension already has an id, then it is removed.
473  * If the dimension is a parameter, then we need to reset it
474  * in the nested spaces (if any) as well.
475  */
476 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
477         enum isl_dim_type type, unsigned pos)
478 {
479         space = isl_space_cow(space);
480         if (!space)
481                 goto error;
482
483         if (type == isl_dim_param) {
484                 int i;
485
486                 for (i = 0; i < 2; ++i) {
487                         if (!space->nested[i])
488                                 continue;
489                         space->nested[i] =
490                                 isl_space_reset_dim_id(space->nested[i],
491                                                         type, pos);
492                         if (!space->nested[i])
493                                 goto error;
494                 }
495         }
496
497         isl_id_free(get_id(space, type, pos));
498         return set_id(space, type, pos, NULL);
499 error:
500         isl_space_free(space);
501         return NULL;
502 }
503
504 int isl_space_has_dim_id(__isl_keep isl_space *dim,
505         enum isl_dim_type type, unsigned pos)
506 {
507         if (!dim)
508                 return -1;
509         return get_id(dim, type, pos) != NULL;
510 }
511
512 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
513         enum isl_dim_type type, unsigned pos)
514 {
515         if (!dim)
516                 return NULL;
517         if (!get_id(dim, type, pos))
518                 isl_die(dim->ctx, isl_error_invalid,
519                         "dim has no id", return NULL);
520         return isl_id_copy(get_id(dim, type, pos));
521 }
522
523 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
524         enum isl_dim_type type, const char *s)
525 {
526         isl_id *id;
527
528         if (!dim)
529                 return NULL;
530
531         if (!s)
532                 return isl_space_reset_tuple_id(dim, type);
533
534         if (!name_ok(dim->ctx, s))
535                 goto error;
536
537         id = isl_id_alloc(dim->ctx, s, NULL);
538         return isl_space_set_tuple_id(dim, type, id);
539 error:
540         isl_space_free(dim);
541         return NULL;
542 }
543
544 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
545          enum isl_dim_type type)
546 {
547         isl_id *id;
548         if (!dim)
549                 return NULL;
550         if (type != isl_dim_in && type != isl_dim_out)
551                 return NULL;
552         id = dim->tuple_id[type - isl_dim_in];
553         return id ? id->name : NULL;
554 }
555
556 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
557                                  enum isl_dim_type type, unsigned pos,
558                                  const char *s)
559 {
560         isl_id *id;
561
562         if (!dim)
563                 return NULL;
564         if (!s)
565                 return isl_space_reset_dim_id(dim, type, pos);
566         if (!name_ok(dim->ctx, s))
567                 goto error;
568         id = isl_id_alloc(dim->ctx, s, NULL);
569         return isl_space_set_dim_id(dim, type, pos, id);
570 error:
571         isl_space_free(dim);
572         return NULL;
573 }
574
575 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
576                                  enum isl_dim_type type, unsigned pos)
577 {
578         isl_id *id = get_id(dim, type, pos);
579         return id ? id->name : NULL;
580 }
581
582 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
583         __isl_keep isl_id *id)
584 {
585         int i;
586         int offset;
587         int n;
588
589         if (!dim || !id)
590                 return -1;
591
592         offset = isl_space_offset(dim, type);
593         n = isl_space_dim(dim, type);
594         for (i = 0; i < n && offset + i < dim->n_id; ++i)
595                 if (dim->ids[offset + i] == id)
596                         return i;
597
598         return -1;
599 }
600
601 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
602         enum isl_dim_type type, const char *name)
603 {
604         int i;
605         int offset;
606         int n;
607
608         if (!space || !name)
609                 return -1;
610
611         offset = isl_space_offset(space, type);
612         n = isl_space_dim(space, type);
613         for (i = 0; i < n && offset + i < space->n_id; ++i)
614                 if (space->ids[offset + i]->name &&
615                     !strcmp(space->ids[offset + i]->name, name))
616                         return i;
617
618         return -1;
619 }
620
621 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
622         enum isl_dim_type type)
623 {
624         if (!dim)
625                 return NULL;
626         if (type == isl_dim_in)
627                 return dim->tuple_id[0];
628         if (type == isl_dim_out)
629                 return dim->tuple_id[1];
630         return NULL;
631 }
632
633 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
634         enum isl_dim_type type)
635 {
636         if (!dim)
637                 return NULL;
638         if (type == isl_dim_in)
639                 return dim->nested[0];
640         if (type == isl_dim_out)
641                 return dim->nested[1];
642         return NULL;
643 }
644
645 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
646                         __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
647 {
648         isl_id *id1, *id2;
649         isl_space *nested1, *nested2;
650
651         if (!dim1 || !dim2)
652                 return -1;
653
654         if (dim1 == dim2 && dim1_type == dim2_type)
655                 return 1;
656
657         if (n(dim1, dim1_type) != n(dim2, dim2_type))
658                 return 0;
659         id1 = tuple_id(dim1, dim1_type);
660         id2 = tuple_id(dim2, dim2_type);
661         if (!id1 ^ !id2)
662                 return 0;
663         if (id1 && id1 != id2)
664                 return 0;
665         nested1 = nested(dim1, dim1_type);
666         nested2 = nested(dim2, dim2_type);
667         if (!nested1 ^ !nested2)
668                 return 0;
669         if (nested1 && !isl_space_is_equal(nested1, nested2))
670                 return 0;
671         return 1;
672 }
673
674 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
675         __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
676 {
677         int i;
678
679         if (dim1 == dim2 && dim1_type == dim2_type)
680                 return 1;
681
682         if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
683                 return 0;
684
685         if (!dim1->ids && !dim2->ids)
686                 return 1;
687
688         for (i = 0; i < n(dim1, dim1_type); ++i) {
689                 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
690                         return 0;
691         }
692         return 1;
693 }
694
695 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
696         __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
697 {
698         if (!dim1 || !dim2)
699                 return -1;
700
701         return match(dim1, dim1_type, dim2, dim2_type);
702 }
703
704 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
705         unsigned first, unsigned n, __isl_keep isl_id **ids)
706 {
707         int i;
708
709         for (i = 0; i < n ; ++i)
710                 ids[i] = get_id(dim, type, first + i);
711 }
712
713 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
714                         unsigned nparam, unsigned n_in, unsigned n_out)
715 {
716         isl_id **ids = NULL;
717
718         if (!dim)
719                 return NULL;
720         if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
721                 return dim;
722
723         isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
724         isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
725         isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
726
727         dim = isl_space_cow(dim);
728
729         if (dim->ids) {
730                 ids = isl_calloc_array(dim->ctx, isl_id *,
731                                          nparam + n_in + n_out);
732                 if (!ids)
733                         goto error;
734                 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
735                 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
736                 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
737                 free(dim->ids);
738                 dim->ids = ids;
739                 dim->n_id = nparam + n_in + n_out;
740         }
741         dim->nparam = nparam;
742         dim->n_in = n_in;
743         dim->n_out = n_out;
744
745         return dim;
746 error:
747         free(ids);
748         isl_space_free(dim);
749         return NULL;
750 }
751
752 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
753         enum isl_dim_type type, unsigned n)
754 {
755         if (!dim)
756                 return NULL;
757         dim = isl_space_reset(dim, type);
758         switch (type) {
759         case isl_dim_param:
760                 dim = isl_space_extend(dim,
761                                         dim->nparam + n, dim->n_in, dim->n_out);
762                 if (dim && dim->nested[0] &&
763                     !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
764                                                     isl_dim_param, n)))
765                         goto error;
766                 if (dim && dim->nested[1] &&
767                     !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
768                                                     isl_dim_param, n)))
769                         goto error;
770                 return dim;
771         case isl_dim_in:
772                 return isl_space_extend(dim,
773                                         dim->nparam, dim->n_in + n, dim->n_out);
774         case isl_dim_out:
775                 return isl_space_extend(dim,
776                                         dim->nparam, dim->n_in, dim->n_out + n);
777         default:
778                 isl_die(dim->ctx, isl_error_invalid,
779                         "cannot add dimensions of specified type", goto error);
780         }
781 error:
782         isl_space_free(dim);
783         return NULL;
784 }
785
786 static int valid_dim_type(enum isl_dim_type type)
787 {
788         switch (type) {
789         case isl_dim_param:
790         case isl_dim_in:
791         case isl_dim_out:
792                 return 1;
793         default:
794                 return 0;
795         }
796 }
797
798 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
799         enum isl_dim_type type, unsigned pos, unsigned n)
800 {
801         isl_id **ids = NULL;
802
803         if (!dim)
804                 return NULL;
805         if (n == 0)
806                 return isl_space_reset(dim, type);
807
808         if (!valid_dim_type(type))
809                 isl_die(dim->ctx, isl_error_invalid,
810                         "cannot insert dimensions of specified type",
811                         goto error);
812
813         isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
814
815         dim = isl_space_cow(dim);
816         if (!dim)
817                 return NULL;
818
819         if (dim->ids) {
820                 enum isl_dim_type t;
821                 int off;
822                 int s[3];
823                 int *size = s - isl_dim_param;
824                 ids = isl_calloc_array(dim->ctx, isl_id *,
825                                      dim->nparam + dim->n_in + dim->n_out + n);
826                 if (!ids)
827                         goto error;
828                 off = 0;
829                 size[isl_dim_param] = dim->nparam;
830                 size[isl_dim_in] = dim->n_in;
831                 size[isl_dim_out] = dim->n_out;
832                 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
833                         if (t != type) {
834                                 get_ids(dim, t, 0, size[t], ids + off);
835                                 off += size[t];
836                         } else {
837                                 get_ids(dim, t, 0, pos, ids + off);
838                                 off += pos + n;
839                                 get_ids(dim, t, pos, size[t] - pos, ids + off);
840                                 off += size[t] - pos;
841                         }
842                 }
843                 free(dim->ids);
844                 dim->ids = ids;
845                 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
846         }
847         switch (type) {
848         case isl_dim_param:     dim->nparam += n; break;
849         case isl_dim_in:        dim->n_in += n; break;
850         case isl_dim_out:       dim->n_out += n; break;
851         default:                ;
852         }
853         dim = isl_space_reset(dim, type);
854
855         return dim;
856 error:
857         isl_space_free(dim);
858         return NULL;
859 }
860
861 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
862         enum isl_dim_type dst_type, unsigned dst_pos,
863         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
864 {
865         int i;
866
867         if (!dim)
868                 return NULL;
869         if (n == 0)
870                 return dim;
871
872         isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
873                 goto error);
874
875         if (dst_type == src_type && dst_pos == src_pos)
876                 return dim;
877
878         isl_assert(dim->ctx, dst_type != src_type, goto error);
879
880         dim = isl_space_reset(dim, src_type);
881         dim = isl_space_reset(dim, dst_type);
882
883         dim = isl_space_cow(dim);
884         if (!dim)
885                 return NULL;
886
887         if (dim->ids) {
888                 isl_id **ids;
889                 enum isl_dim_type t;
890                 int off;
891                 int s[3];
892                 int *size = s - isl_dim_param;
893                 ids = isl_calloc_array(dim->ctx, isl_id *,
894                                          dim->nparam + dim->n_in + dim->n_out);
895                 if (!ids)
896                         goto error;
897                 off = 0;
898                 size[isl_dim_param] = dim->nparam;
899                 size[isl_dim_in] = dim->n_in;
900                 size[isl_dim_out] = dim->n_out;
901                 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
902                         if (t == dst_type) {
903                                 get_ids(dim, t, 0, dst_pos, ids + off);
904                                 off += dst_pos;
905                                 get_ids(dim, src_type, src_pos, n, ids + off);
906                                 off += n;
907                                 get_ids(dim, t, dst_pos, size[t] - dst_pos,
908                                                 ids + off);
909                                 off += size[t] - dst_pos;
910                         } else if (t == src_type) {
911                                 get_ids(dim, t, 0, src_pos, ids + off);
912                                 off += src_pos;
913                                 get_ids(dim, t, src_pos + n,
914                                             size[t] - src_pos - n, ids + off);
915                                 off += size[t] - src_pos - n;
916                         } else {
917                                 get_ids(dim, t, 0, size[t], ids + off);
918                                 off += size[t];
919                         }
920                 }
921                 free(dim->ids);
922                 dim->ids = ids;
923                 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
924         }
925
926         switch (dst_type) {
927         case isl_dim_param:     dim->nparam += n; break;
928         case isl_dim_in:        dim->n_in += n; break;
929         case isl_dim_out:       dim->n_out += n; break;
930         default:                ;
931         }
932
933         switch (src_type) {
934         case isl_dim_param:     dim->nparam -= n; break;
935         case isl_dim_in:        dim->n_in -= n; break;
936         case isl_dim_out:       dim->n_out -= n; break;
937         default:                ;
938         }
939
940         if (dst_type != isl_dim_param && src_type != isl_dim_param)
941                 return dim;
942
943         for (i = 0; i < 2; ++i) {
944                 if (!dim->nested[i])
945                         continue;
946                 dim->nested[i] = isl_space_replace(dim->nested[i],
947                                                  isl_dim_param, dim);
948                 if (!dim->nested[i])
949                         goto error;
950         }
951
952         return dim;
953 error:
954         isl_space_free(dim);
955         return NULL;
956 }
957
958 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
959         __isl_take isl_space *right)
960 {
961         isl_space *dim;
962
963         if (!left || !right)
964                 goto error;
965
966         isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
967                         goto error);
968         isl_assert(left->ctx,
969                 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
970                 goto error);
971
972         dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
973         if (!dim)
974                 goto error;
975
976         dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
977         dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
978         dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
979
980         if (dim && left->tuple_id[0] &&
981             !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
982                 goto error;
983         if (dim && right->tuple_id[1] &&
984             !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
985                 goto error;
986         if (dim && left->nested[0] &&
987             !(dim->nested[0] = isl_space_copy(left->nested[0])))
988                 goto error;
989         if (dim && right->nested[1] &&
990             !(dim->nested[1] = isl_space_copy(right->nested[1])))
991                 goto error;
992
993         isl_space_free(left);
994         isl_space_free(right);
995
996         return dim;
997 error:
998         isl_space_free(left);
999         isl_space_free(right);
1000         return NULL;
1001 }
1002
1003 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1004         __isl_take isl_space *right)
1005 {
1006         isl_space *dom1, *dom2, *nest1, *nest2;
1007
1008         if (!left || !right)
1009                 goto error;
1010
1011         isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1012                         goto error);
1013
1014         dom1 = isl_space_domain(isl_space_copy(left));
1015         dom2 = isl_space_domain(isl_space_copy(right));
1016         nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1017
1018         dom1 = isl_space_range(left);
1019         dom2 = isl_space_range(right);
1020         nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1021
1022         return isl_space_join(isl_space_reverse(nest1), nest2);
1023 error:
1024         isl_space_free(left);
1025         isl_space_free(right);
1026         return NULL;
1027 }
1028
1029 /* Given two spaces { A -> C } and { B -> C }, construct the space
1030  * { [A -> B] -> C }
1031  */
1032 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1033         __isl_take isl_space *right)
1034 {
1035         isl_space *ran, *dom1, *dom2, *nest;
1036
1037         if (!left || !right)
1038                 goto error;
1039
1040         if (!match(left, isl_dim_param, right, isl_dim_param))
1041                 isl_die(left->ctx, isl_error_invalid,
1042                         "parameters need to match", goto error);
1043         if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1044                 isl_die(left->ctx, isl_error_invalid,
1045                         "ranges need to match", goto error);
1046
1047         ran = isl_space_range(isl_space_copy(left));
1048
1049         dom1 = isl_space_domain(left);
1050         dom2 = isl_space_domain(right);
1051         nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1052
1053         return isl_space_join(isl_space_reverse(nest), ran);
1054 error:
1055         isl_space_free(left);
1056         isl_space_free(right);
1057         return NULL;
1058 }
1059
1060 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1061         __isl_take isl_space *right)
1062 {
1063         isl_space *dom, *ran1, *ran2, *nest;
1064
1065         if (!left || !right)
1066                 goto error;
1067
1068         isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1069                         goto error);
1070         if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1071                 isl_die(left->ctx, isl_error_invalid,
1072                         "domains need to match", goto error);
1073
1074         dom = isl_space_domain(isl_space_copy(left));
1075
1076         ran1 = isl_space_range(left);
1077         ran2 = isl_space_range(right);
1078         nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1079
1080         return isl_space_join(isl_space_reverse(dom), nest);
1081 error:
1082         isl_space_free(left);
1083         isl_space_free(right);
1084         return NULL;
1085 }
1086
1087 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1088 {
1089         isl_ctx *ctx;
1090         isl_id **ids = NULL;
1091
1092         if (!dim)
1093                 return NULL;
1094         ctx = isl_space_get_ctx(dim);
1095         if (!isl_space_is_set(dim))
1096                 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1097         dim = isl_space_cow(dim);
1098         if (!dim)
1099                 return NULL;
1100         if (dim->ids) {
1101                 ids = isl_calloc_array(dim->ctx, isl_id *,
1102                                         dim->nparam + dim->n_out + dim->n_out);
1103                 if (!ids)
1104                         goto error;
1105                 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1106                 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1107         }
1108         dim->n_in = dim->n_out;
1109         if (ids) {
1110                 free(dim->ids);
1111                 dim->ids = ids;
1112                 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1113                 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1114         }
1115         isl_id_free(dim->tuple_id[0]);
1116         dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1117         isl_space_free(dim->nested[0]);
1118         dim->nested[0] = isl_space_copy(dim->nested[1]);
1119         return dim;
1120 error:
1121         isl_space_free(dim);
1122         return NULL;
1123 }
1124
1125 __isl_give isl_space *isl_space_map_from_domain_and_range(
1126         __isl_take isl_space *domain, __isl_take isl_space *range)
1127 {
1128         if (!domain || !range)
1129                 goto error;
1130         if (!isl_space_is_set(domain))
1131                 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1132                         "domain is not a set space", goto error);
1133         if (!isl_space_is_set(range))
1134                 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1135                         "range is not a set space", goto error);
1136         return isl_space_join(isl_space_reverse(domain), range);
1137 error:
1138         isl_space_free(domain);
1139         isl_space_free(range);
1140         return NULL;
1141 }
1142
1143 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1144         enum isl_dim_type type,
1145         unsigned first, unsigned n, __isl_take isl_id **ids)
1146 {
1147         int i;
1148
1149         for (i = 0; i < n ; ++i)
1150                 dim = set_id(dim, type, first + i, ids[i]);
1151
1152         return dim;
1153 }
1154
1155 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1156 {
1157         unsigned t;
1158         isl_space *nested;
1159         isl_id **ids = NULL;
1160         isl_id *id;
1161
1162         if (!dim)
1163                 return NULL;
1164         if (match(dim, isl_dim_in, dim, isl_dim_out))
1165                 return dim;
1166
1167         dim = isl_space_cow(dim);
1168         if (!dim)
1169                 return NULL;
1170
1171         id = dim->tuple_id[0];
1172         dim->tuple_id[0] = dim->tuple_id[1];
1173         dim->tuple_id[1] = id;
1174
1175         nested = dim->nested[0];
1176         dim->nested[0] = dim->nested[1];
1177         dim->nested[1] = nested;
1178
1179         if (dim->ids) {
1180                 ids = isl_alloc_array(dim->ctx, isl_id *,
1181                                         dim->n_in + dim->n_out);
1182                 if (!ids)
1183                         goto error;
1184                 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1185                 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1186         }
1187
1188         t = dim->n_in;
1189         dim->n_in = dim->n_out;
1190         dim->n_out = t;
1191
1192         if (dim->ids) {
1193                 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1194                 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1195                 free(ids);
1196         }
1197
1198         return dim;
1199 error:
1200         free(ids);
1201         isl_space_free(dim);
1202         return NULL;
1203 }
1204
1205 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1206         enum isl_dim_type type, unsigned first, unsigned num)
1207 {
1208         int i;
1209
1210         if (!dim)
1211                 return NULL;
1212
1213         if (num == 0)
1214                 return isl_space_reset(dim, type);
1215
1216         if (!valid_dim_type(type))
1217                 isl_die(dim->ctx, isl_error_invalid,
1218                         "cannot drop dimensions of specified type", goto error);
1219
1220         isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1221         dim = isl_space_cow(dim);
1222         if (!dim)
1223                 goto error;
1224         if (dim->ids) {
1225                 dim = extend_ids(dim);
1226                 if (!dim)
1227                         goto error;
1228                 for (i = 0; i < num; ++i)
1229                         isl_id_free(get_id(dim, type, first + i));
1230                 for (i = first+num; i < n(dim, type); ++i)
1231                         set_id(dim, type, i - num, get_id(dim, type, i));
1232                 switch (type) {
1233                 case isl_dim_param:
1234                         get_ids(dim, isl_dim_in, 0, dim->n_in,
1235                                 dim->ids + offset(dim, isl_dim_in) - num);
1236                 case isl_dim_in:
1237                         get_ids(dim, isl_dim_out, 0, dim->n_out,
1238                                 dim->ids + offset(dim, isl_dim_out) - num);
1239                 default:
1240                         ;
1241                 }
1242                 dim->n_id -= num;
1243         }
1244         switch (type) {
1245         case isl_dim_param:     dim->nparam -= num; break;
1246         case isl_dim_in:        dim->n_in -= num; break;
1247         case isl_dim_out:       dim->n_out -= num; break;
1248         default:                ;
1249         }
1250         dim = isl_space_reset(dim, type);
1251         if (type == isl_dim_param) {
1252                 if (dim && dim->nested[0] &&
1253                     !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1254                                                     isl_dim_param, first, num)))
1255                         goto error;
1256                 if (dim && dim->nested[1] &&
1257                     !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1258                                                     isl_dim_param, first, num)))
1259                         goto error;
1260         }
1261         return dim;
1262 error:
1263         isl_space_free(dim);
1264         return NULL;
1265 }
1266
1267 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1268                 unsigned first, unsigned n)
1269 {
1270         if (!dim)
1271                 return NULL;
1272         return isl_space_drop_dims(dim, isl_dim_in, first, n);
1273 }
1274
1275 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1276                 unsigned first, unsigned n)
1277 {
1278         if (!dim)
1279                 return NULL;
1280         return isl_space_drop_dims(dim, isl_dim_out, first, n);
1281 }
1282
1283 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1284 {
1285         if (!dim)
1286                 return NULL;
1287         dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1288         dim = isl_space_reverse(dim);
1289         dim = mark_as_set(dim);
1290         return dim;
1291 }
1292
1293 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1294 {
1295         if (!dim)
1296                 return NULL;
1297         if (!isl_space_is_set(dim))
1298                 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1299                         "not a set space", goto error);
1300         dim = isl_space_reverse(dim);
1301         dim = isl_space_reset(dim, isl_dim_out);
1302         return dim;
1303 error:
1304         isl_space_free(dim);
1305         return NULL;
1306 }
1307
1308 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1309 {
1310         if (!dim)
1311                 return NULL;
1312         dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1313         dim = mark_as_set(dim);
1314         return dim;
1315 }
1316
1317 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1318 {
1319         if (!dim)
1320                 return NULL;
1321         if (!isl_space_is_set(dim))
1322                 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1323                         "not a set space", goto error);
1324         return isl_space_reset(dim, isl_dim_in);
1325 error:
1326         isl_space_free(dim);
1327         return NULL;
1328 }
1329
1330 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1331 {
1332         if (isl_space_is_params(space))
1333                 return space;
1334         space = isl_space_drop_dims(space,
1335                             isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1336         space = isl_space_drop_dims(space,
1337                             isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1338         space = mark_as_params(space);
1339         return space;
1340 }
1341
1342 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1343 {
1344         if (!space)
1345                 return NULL;
1346         if (!isl_space_is_params(space))
1347                 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1348                         "not a parameter space", goto error);
1349         return isl_space_reset(space, isl_dim_set);
1350 error:
1351         isl_space_free(space);
1352         return NULL;
1353 }
1354
1355 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1356 {
1357         dim = isl_space_cow(dim);
1358         if (!dim)
1359                 return NULL;
1360
1361         dim->n_out += dim->n_in;
1362         dim->n_in = 0;
1363         dim = isl_space_reset(dim, isl_dim_in);
1364         dim = isl_space_reset(dim, isl_dim_out);
1365
1366         return dim;
1367 }
1368
1369 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1370         unsigned n_div)
1371 {
1372         int i;
1373
1374         if (!dim)
1375                 return NULL;
1376         if (n_div == 0 &&
1377             dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1378                 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1379         dim = isl_space_cow(dim);
1380         if (!dim)
1381                 return NULL;
1382         dim->n_out += dim->nparam + dim->n_in + n_div;
1383         dim->nparam = 0;
1384         dim->n_in = 0;
1385
1386         for (i = 0; i < dim->n_id; ++i)
1387                 isl_id_free(get_id(dim, isl_dim_out, i));
1388         dim->n_id = 0;
1389         dim = isl_space_reset(dim, isl_dim_in);
1390         dim = isl_space_reset(dim, isl_dim_out);
1391
1392         return dim;
1393 }
1394
1395 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1396 {
1397         if (!dim1 || !dim2)
1398                 return -1;
1399         if (dim1 == dim2)
1400                 return 1;
1401         return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1402                isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1403                isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1404 }
1405
1406 /* Is space1 equal to the domain of space2?
1407  */
1408 int isl_space_is_domain(__isl_keep isl_space *space1,
1409         __isl_keep isl_space *space2)
1410 {
1411         if (!space1 || !space2)
1412                 return -1;
1413         if (!isl_space_is_set(space1))
1414                 return 0;
1415         return match(space1, isl_dim_param, space2, isl_dim_param) &&
1416                isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1417 }
1418
1419 int isl_space_compatible(__isl_keep isl_space *dim1,
1420         __isl_keep isl_space *dim2)
1421 {
1422         return dim1->nparam == dim2->nparam &&
1423                dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1424 }
1425
1426 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1427 {
1428         int i;
1429         isl_id *id;
1430
1431         if (!dim)
1432                 return hash;
1433
1434         hash = isl_hash_builtin(hash, dim->nparam);
1435         hash = isl_hash_builtin(hash, dim->n_in);
1436         hash = isl_hash_builtin(hash, dim->n_out);
1437
1438         for (i = 0; i < dim->nparam; ++i) {
1439                 id = get_id(dim, isl_dim_param, i);
1440                 hash = isl_hash_id(hash, id);
1441         }
1442
1443         id = tuple_id(dim, isl_dim_in);
1444         hash = isl_hash_id(hash, id);
1445         id = tuple_id(dim, isl_dim_out);
1446         hash = isl_hash_id(hash, id);
1447
1448         hash = isl_hash_dim(hash, dim->nested[0]);
1449         hash = isl_hash_dim(hash, dim->nested[1]);
1450
1451         return hash;
1452 }
1453
1454 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1455 {
1456         uint32_t hash;
1457
1458         if (!dim)
1459                 return 0;
1460
1461         hash = isl_hash_init();
1462         hash = isl_hash_dim(hash, dim);
1463
1464         return hash;
1465 }
1466
1467 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1468 {
1469         if (!dim)
1470                 return -1;
1471
1472         if (!isl_space_is_set(dim))
1473                 return 0;
1474
1475         return dim->nested[1] != NULL;
1476 }
1477
1478 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1479 {
1480         isl_space *wrap;
1481
1482         if (!dim)
1483                 return NULL;
1484
1485         wrap = isl_space_set_alloc(dim->ctx,
1486                                     dim->nparam, dim->n_in + dim->n_out);
1487
1488         wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1489         wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1490         wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1491
1492         if (!wrap)
1493                 goto error;
1494
1495         wrap->nested[1] = dim;
1496
1497         return wrap;
1498 error:
1499         isl_space_free(dim);
1500         return NULL;
1501 }
1502
1503 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1504 {
1505         isl_space *unwrap;
1506
1507         if (!dim)
1508                 return NULL;
1509
1510         if (!isl_space_is_wrapping(dim))
1511                 isl_die(dim->ctx, isl_error_invalid, "not a wrapping dim",
1512                         goto error);
1513
1514         unwrap = isl_space_copy(dim->nested[1]);
1515         isl_space_free(dim);
1516
1517         return unwrap;
1518 error:
1519         isl_space_free(dim);
1520         return NULL;
1521 }
1522
1523 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1524 {
1525         if (type != isl_dim_in && type != isl_dim_out)
1526                 return 0;
1527         if (!dim)
1528                 return -1;
1529         if (dim->tuple_id[type - isl_dim_in])
1530                 return 1;
1531         if (dim->nested[type - isl_dim_in])
1532                 return 1;
1533         return 0;
1534 }
1535
1536 int isl_space_may_be_set(__isl_keep isl_space *dim)
1537 {
1538         if (!dim)
1539                 return -1;
1540         if (isl_space_is_set(dim))
1541                 return 1;
1542         if (isl_space_dim(dim, isl_dim_in) != 0)
1543                 return 0;
1544         if (isl_space_is_named_or_nested(dim, isl_dim_in))
1545                 return 0;
1546         return 1;
1547 }
1548
1549 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1550         enum isl_dim_type type)
1551 {
1552         if (!isl_space_is_named_or_nested(dim, type))
1553                 return dim;
1554
1555         dim = isl_space_cow(dim);
1556         if (!dim)
1557                 return NULL;
1558
1559         isl_id_free(dim->tuple_id[type - isl_dim_in]);
1560         dim->tuple_id[type - isl_dim_in] = NULL;
1561         isl_space_free(dim->nested[type - isl_dim_in]);
1562         dim->nested[type - isl_dim_in] = NULL;
1563
1564         return dim;
1565 }
1566
1567 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1568 {
1569         if (!dim)
1570                 return NULL;
1571         if (!dim->nested[0] && !dim->nested[1])
1572                 return dim;
1573
1574         if (dim->nested[0])
1575                 dim = isl_space_reset(dim, isl_dim_in);
1576         if (dim && dim->nested[1])
1577                 dim = isl_space_reset(dim, isl_dim_out);
1578
1579         return dim;
1580 }
1581
1582 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1583 {
1584         if (!dim)
1585                 return NULL;
1586         if (!dim->nested[0])
1587                 return dim;
1588
1589         return isl_space_reset(dim, isl_dim_in);
1590 }
1591
1592 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1593 {
1594         if (!dim)
1595                 return NULL;
1596         if (!dim->nested[1])
1597                 return dim;
1598
1599         return isl_space_reset(dim, isl_dim_out);
1600 }
1601
1602 /* Replace the dimensions of the given type of dst by those of src.
1603  */
1604 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1605         enum isl_dim_type type, __isl_keep isl_space *src)
1606 {
1607         dst = isl_space_cow(dst);
1608
1609         if (!dst || !src)
1610                 goto error;
1611
1612         dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1613         dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1614         dst = copy_ids(dst, type, 0, src, type);
1615
1616         if (dst && type == isl_dim_param) {
1617                 int i;
1618                 for (i = 0; i <= 1; ++i) {
1619                         if (!dst->nested[i])
1620                                 continue;
1621                         dst->nested[i] = isl_space_replace(dst->nested[i],
1622                                                          type, src);
1623                         if (!dst->nested[i])
1624                                 goto error;
1625                 }
1626         }
1627
1628         return dst;
1629 error:
1630         isl_space_free(dst);
1631         return NULL;
1632 }
1633
1634 /* Given a dimension specification "dim" of a set, create a dimension
1635  * specification for the lift of the set.  In particular, the result
1636  * is of the form [dim -> local[..]], with n_local variables in the
1637  * range of the wrapped map.
1638  */
1639 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1640 {
1641         isl_space *local_dim;
1642
1643         if (!dim)
1644                 return NULL;
1645
1646         local_dim = isl_space_dup(dim);
1647         local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1648         local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1649         local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1650         dim = isl_space_join(isl_space_from_domain(dim),
1651                             isl_space_from_range(local_dim));
1652         dim = isl_space_wrap(dim);
1653         dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1654
1655         return dim;
1656 }
1657
1658 int isl_space_can_zip(__isl_keep isl_space *dim)
1659 {
1660         if (!dim)
1661                 return -1;
1662
1663         return dim->nested[0] && dim->nested[1];
1664 }
1665
1666 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1667 {
1668         isl_space *dom, *ran;
1669         isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1670
1671         if (!isl_space_can_zip(dim))
1672                 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1673                         goto error);
1674
1675         if (!dim)
1676                 return 0;
1677         dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1678         ran = isl_space_unwrap(isl_space_range(dim));
1679         dom_dom = isl_space_domain(isl_space_copy(dom));
1680         dom_ran = isl_space_range(dom);
1681         ran_dom = isl_space_domain(isl_space_copy(ran));
1682         ran_ran = isl_space_range(ran);
1683         dom = isl_space_join(isl_space_from_domain(dom_dom),
1684                            isl_space_from_range(ran_dom));
1685         ran = isl_space_join(isl_space_from_domain(dom_ran),
1686                            isl_space_from_range(ran_ran));
1687         return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1688                             isl_space_from_range(isl_space_wrap(ran)));
1689 error:
1690         isl_space_free(dim);
1691         return NULL;
1692 }
1693
1694 int isl_space_has_named_params(__isl_keep isl_space *dim)
1695 {
1696         int i;
1697         unsigned off;
1698
1699         if (!dim)
1700                 return -1;
1701         if (dim->nparam == 0)
1702                 return 1;
1703         off = isl_space_offset(dim, isl_dim_param);
1704         if (off + dim->nparam > dim->n_id)
1705                 return 0;
1706         for (i = 0; i < dim->nparam; ++i)
1707                 if (!dim->ids[off + i])
1708                         return 0;
1709         return 1;
1710 }
1711
1712 /* Align the initial parameters of dim1 to match the order in dim2.
1713  */
1714 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1715         __isl_take isl_space *dim2)
1716 {
1717         isl_reordering *exp;
1718
1719         if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1720                 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1721                         "parameter alignment requires named parameters",
1722                         goto error);
1723
1724         dim2 = isl_space_params(dim2);
1725         exp = isl_parameter_alignment_reordering(dim1, dim2);
1726         exp = isl_reordering_extend_space(exp, dim1);
1727         isl_space_free(dim2);
1728         if (!exp)
1729                 return NULL;
1730         dim1 = isl_space_copy(exp->dim);
1731         isl_reordering_free(exp);
1732         return dim1;
1733 error:
1734         isl_space_free(dim1);
1735         isl_space_free(dim2);
1736         return NULL;
1737 }
1738
1739 /* Given the space of set (domain), construct a space for a map
1740  * with as domain the given space and as range the range of "model".
1741  */
1742 __isl_give isl_space *isl_space_extend_domain_with_range(
1743         __isl_take isl_space *domain, __isl_take isl_space *model)
1744 {
1745         isl_space *space;
1746
1747         space = isl_space_from_domain(domain);
1748         space = isl_space_add_dims(space, isl_dim_out,
1749                                     isl_space_dim(model, isl_dim_out));
1750         if (isl_space_has_tuple_id(model, isl_dim_out))
1751                 space = isl_space_set_tuple_id(space, isl_dim_out,
1752                                 isl_space_get_tuple_id(model, isl_dim_out));
1753         isl_space_free(model);
1754         return space;
1755 }