isl_space_insert_dims: fix handling of nested spaces
[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 /* Insert "n" dimensions of type "type" at position "pos".
799  * If we are inserting parameters, then they are also inserted in
800  * any nested spaces.
801  */
802 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
803         enum isl_dim_type type, unsigned pos, unsigned n)
804 {
805         isl_id **ids = NULL;
806
807         if (!dim)
808                 return NULL;
809         if (n == 0)
810                 return isl_space_reset(dim, type);
811
812         if (!valid_dim_type(type))
813                 isl_die(dim->ctx, isl_error_invalid,
814                         "cannot insert dimensions of specified type",
815                         goto error);
816
817         isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
818
819         dim = isl_space_cow(dim);
820         if (!dim)
821                 return NULL;
822
823         if (dim->ids) {
824                 enum isl_dim_type t;
825                 int off;
826                 int s[3];
827                 int *size = s - isl_dim_param;
828                 ids = isl_calloc_array(dim->ctx, isl_id *,
829                                      dim->nparam + dim->n_in + dim->n_out + n);
830                 if (!ids)
831                         goto error;
832                 off = 0;
833                 size[isl_dim_param] = dim->nparam;
834                 size[isl_dim_in] = dim->n_in;
835                 size[isl_dim_out] = dim->n_out;
836                 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
837                         if (t != type) {
838                                 get_ids(dim, t, 0, size[t], ids + off);
839                                 off += size[t];
840                         } else {
841                                 get_ids(dim, t, 0, pos, ids + off);
842                                 off += pos + n;
843                                 get_ids(dim, t, pos, size[t] - pos, ids + off);
844                                 off += size[t] - pos;
845                         }
846                 }
847                 free(dim->ids);
848                 dim->ids = ids;
849                 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
850         }
851         switch (type) {
852         case isl_dim_param:     dim->nparam += n; break;
853         case isl_dim_in:        dim->n_in += n; break;
854         case isl_dim_out:       dim->n_out += n; break;
855         default:                ;
856         }
857         dim = isl_space_reset(dim, type);
858
859         if (type == isl_dim_param) {
860                 if (dim && dim->nested[0] &&
861                     !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
862                                                     isl_dim_param, pos, n)))
863                         goto error;
864                 if (dim && dim->nested[1] &&
865                     !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
866                                                     isl_dim_param, pos, n)))
867                         goto error;
868         }
869
870         return dim;
871 error:
872         isl_space_free(dim);
873         return NULL;
874 }
875
876 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
877         enum isl_dim_type dst_type, unsigned dst_pos,
878         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
879 {
880         int i;
881
882         if (!dim)
883                 return NULL;
884         if (n == 0)
885                 return dim;
886
887         isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
888                 goto error);
889
890         if (dst_type == src_type && dst_pos == src_pos)
891                 return dim;
892
893         isl_assert(dim->ctx, dst_type != src_type, goto error);
894
895         dim = isl_space_reset(dim, src_type);
896         dim = isl_space_reset(dim, dst_type);
897
898         dim = isl_space_cow(dim);
899         if (!dim)
900                 return NULL;
901
902         if (dim->ids) {
903                 isl_id **ids;
904                 enum isl_dim_type t;
905                 int off;
906                 int s[3];
907                 int *size = s - isl_dim_param;
908                 ids = isl_calloc_array(dim->ctx, isl_id *,
909                                          dim->nparam + dim->n_in + dim->n_out);
910                 if (!ids)
911                         goto error;
912                 off = 0;
913                 size[isl_dim_param] = dim->nparam;
914                 size[isl_dim_in] = dim->n_in;
915                 size[isl_dim_out] = dim->n_out;
916                 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
917                         if (t == dst_type) {
918                                 get_ids(dim, t, 0, dst_pos, ids + off);
919                                 off += dst_pos;
920                                 get_ids(dim, src_type, src_pos, n, ids + off);
921                                 off += n;
922                                 get_ids(dim, t, dst_pos, size[t] - dst_pos,
923                                                 ids + off);
924                                 off += size[t] - dst_pos;
925                         } else if (t == src_type) {
926                                 get_ids(dim, t, 0, src_pos, ids + off);
927                                 off += src_pos;
928                                 get_ids(dim, t, src_pos + n,
929                                             size[t] - src_pos - n, ids + off);
930                                 off += size[t] - src_pos - n;
931                         } else {
932                                 get_ids(dim, t, 0, size[t], ids + off);
933                                 off += size[t];
934                         }
935                 }
936                 free(dim->ids);
937                 dim->ids = ids;
938                 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
939         }
940
941         switch (dst_type) {
942         case isl_dim_param:     dim->nparam += n; break;
943         case isl_dim_in:        dim->n_in += n; break;
944         case isl_dim_out:       dim->n_out += n; break;
945         default:                ;
946         }
947
948         switch (src_type) {
949         case isl_dim_param:     dim->nparam -= n; break;
950         case isl_dim_in:        dim->n_in -= n; break;
951         case isl_dim_out:       dim->n_out -= n; break;
952         default:                ;
953         }
954
955         if (dst_type != isl_dim_param && src_type != isl_dim_param)
956                 return dim;
957
958         for (i = 0; i < 2; ++i) {
959                 if (!dim->nested[i])
960                         continue;
961                 dim->nested[i] = isl_space_replace(dim->nested[i],
962                                                  isl_dim_param, dim);
963                 if (!dim->nested[i])
964                         goto error;
965         }
966
967         return dim;
968 error:
969         isl_space_free(dim);
970         return NULL;
971 }
972
973 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
974         __isl_take isl_space *right)
975 {
976         isl_space *dim;
977
978         if (!left || !right)
979                 goto error;
980
981         isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
982                         goto error);
983         isl_assert(left->ctx,
984                 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
985                 goto error);
986
987         dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
988         if (!dim)
989                 goto error;
990
991         dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
992         dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
993         dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
994
995         if (dim && left->tuple_id[0] &&
996             !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
997                 goto error;
998         if (dim && right->tuple_id[1] &&
999             !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1000                 goto error;
1001         if (dim && left->nested[0] &&
1002             !(dim->nested[0] = isl_space_copy(left->nested[0])))
1003                 goto error;
1004         if (dim && right->nested[1] &&
1005             !(dim->nested[1] = isl_space_copy(right->nested[1])))
1006                 goto error;
1007
1008         isl_space_free(left);
1009         isl_space_free(right);
1010
1011         return dim;
1012 error:
1013         isl_space_free(left);
1014         isl_space_free(right);
1015         return NULL;
1016 }
1017
1018 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1019         __isl_take isl_space *right)
1020 {
1021         isl_space *dom1, *dom2, *nest1, *nest2;
1022
1023         if (!left || !right)
1024                 goto error;
1025
1026         isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1027                         goto error);
1028
1029         dom1 = isl_space_domain(isl_space_copy(left));
1030         dom2 = isl_space_domain(isl_space_copy(right));
1031         nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1032
1033         dom1 = isl_space_range(left);
1034         dom2 = isl_space_range(right);
1035         nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1036
1037         return isl_space_join(isl_space_reverse(nest1), nest2);
1038 error:
1039         isl_space_free(left);
1040         isl_space_free(right);
1041         return NULL;
1042 }
1043
1044 /* Given two spaces { A -> C } and { B -> C }, construct the space
1045  * { [A -> B] -> C }
1046  */
1047 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1048         __isl_take isl_space *right)
1049 {
1050         isl_space *ran, *dom1, *dom2, *nest;
1051
1052         if (!left || !right)
1053                 goto error;
1054
1055         if (!match(left, isl_dim_param, right, isl_dim_param))
1056                 isl_die(left->ctx, isl_error_invalid,
1057                         "parameters need to match", goto error);
1058         if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1059                 isl_die(left->ctx, isl_error_invalid,
1060                         "ranges need to match", goto error);
1061
1062         ran = isl_space_range(isl_space_copy(left));
1063
1064         dom1 = isl_space_domain(left);
1065         dom2 = isl_space_domain(right);
1066         nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1067
1068         return isl_space_join(isl_space_reverse(nest), ran);
1069 error:
1070         isl_space_free(left);
1071         isl_space_free(right);
1072         return NULL;
1073 }
1074
1075 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1076         __isl_take isl_space *right)
1077 {
1078         isl_space *dom, *ran1, *ran2, *nest;
1079
1080         if (!left || !right)
1081                 goto error;
1082
1083         isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1084                         goto error);
1085         if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1086                 isl_die(left->ctx, isl_error_invalid,
1087                         "domains need to match", goto error);
1088
1089         dom = isl_space_domain(isl_space_copy(left));
1090
1091         ran1 = isl_space_range(left);
1092         ran2 = isl_space_range(right);
1093         nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1094
1095         return isl_space_join(isl_space_reverse(dom), nest);
1096 error:
1097         isl_space_free(left);
1098         isl_space_free(right);
1099         return NULL;
1100 }
1101
1102 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1103 {
1104         isl_ctx *ctx;
1105         isl_id **ids = NULL;
1106
1107         if (!dim)
1108                 return NULL;
1109         ctx = isl_space_get_ctx(dim);
1110         if (!isl_space_is_set(dim))
1111                 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1112         dim = isl_space_cow(dim);
1113         if (!dim)
1114                 return NULL;
1115         if (dim->ids) {
1116                 ids = isl_calloc_array(dim->ctx, isl_id *,
1117                                         dim->nparam + dim->n_out + dim->n_out);
1118                 if (!ids)
1119                         goto error;
1120                 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1121                 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1122         }
1123         dim->n_in = dim->n_out;
1124         if (ids) {
1125                 free(dim->ids);
1126                 dim->ids = ids;
1127                 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1128                 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1129         }
1130         isl_id_free(dim->tuple_id[0]);
1131         dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1132         isl_space_free(dim->nested[0]);
1133         dim->nested[0] = isl_space_copy(dim->nested[1]);
1134         return dim;
1135 error:
1136         isl_space_free(dim);
1137         return NULL;
1138 }
1139
1140 __isl_give isl_space *isl_space_map_from_domain_and_range(
1141         __isl_take isl_space *domain, __isl_take isl_space *range)
1142 {
1143         if (!domain || !range)
1144                 goto error;
1145         if (!isl_space_is_set(domain))
1146                 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1147                         "domain is not a set space", goto error);
1148         if (!isl_space_is_set(range))
1149                 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1150                         "range is not a set space", goto error);
1151         return isl_space_join(isl_space_reverse(domain), range);
1152 error:
1153         isl_space_free(domain);
1154         isl_space_free(range);
1155         return NULL;
1156 }
1157
1158 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1159         enum isl_dim_type type,
1160         unsigned first, unsigned n, __isl_take isl_id **ids)
1161 {
1162         int i;
1163
1164         for (i = 0; i < n ; ++i)
1165                 dim = set_id(dim, type, first + i, ids[i]);
1166
1167         return dim;
1168 }
1169
1170 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1171 {
1172         unsigned t;
1173         isl_space *nested;
1174         isl_id **ids = NULL;
1175         isl_id *id;
1176
1177         if (!dim)
1178                 return NULL;
1179         if (match(dim, isl_dim_in, dim, isl_dim_out))
1180                 return dim;
1181
1182         dim = isl_space_cow(dim);
1183         if (!dim)
1184                 return NULL;
1185
1186         id = dim->tuple_id[0];
1187         dim->tuple_id[0] = dim->tuple_id[1];
1188         dim->tuple_id[1] = id;
1189
1190         nested = dim->nested[0];
1191         dim->nested[0] = dim->nested[1];
1192         dim->nested[1] = nested;
1193
1194         if (dim->ids) {
1195                 ids = isl_alloc_array(dim->ctx, isl_id *,
1196                                         dim->n_in + dim->n_out);
1197                 if (!ids)
1198                         goto error;
1199                 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1200                 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1201         }
1202
1203         t = dim->n_in;
1204         dim->n_in = dim->n_out;
1205         dim->n_out = t;
1206
1207         if (dim->ids) {
1208                 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1209                 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1210                 free(ids);
1211         }
1212
1213         return dim;
1214 error:
1215         free(ids);
1216         isl_space_free(dim);
1217         return NULL;
1218 }
1219
1220 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1221         enum isl_dim_type type, unsigned first, unsigned num)
1222 {
1223         int i;
1224
1225         if (!dim)
1226                 return NULL;
1227
1228         if (num == 0)
1229                 return isl_space_reset(dim, type);
1230
1231         if (!valid_dim_type(type))
1232                 isl_die(dim->ctx, isl_error_invalid,
1233                         "cannot drop dimensions of specified type", goto error);
1234
1235         isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1236         dim = isl_space_cow(dim);
1237         if (!dim)
1238                 goto error;
1239         if (dim->ids) {
1240                 dim = extend_ids(dim);
1241                 if (!dim)
1242                         goto error;
1243                 for (i = 0; i < num; ++i)
1244                         isl_id_free(get_id(dim, type, first + i));
1245                 for (i = first+num; i < n(dim, type); ++i)
1246                         set_id(dim, type, i - num, get_id(dim, type, i));
1247                 switch (type) {
1248                 case isl_dim_param:
1249                         get_ids(dim, isl_dim_in, 0, dim->n_in,
1250                                 dim->ids + offset(dim, isl_dim_in) - num);
1251                 case isl_dim_in:
1252                         get_ids(dim, isl_dim_out, 0, dim->n_out,
1253                                 dim->ids + offset(dim, isl_dim_out) - num);
1254                 default:
1255                         ;
1256                 }
1257                 dim->n_id -= num;
1258         }
1259         switch (type) {
1260         case isl_dim_param:     dim->nparam -= num; break;
1261         case isl_dim_in:        dim->n_in -= num; break;
1262         case isl_dim_out:       dim->n_out -= num; break;
1263         default:                ;
1264         }
1265         dim = isl_space_reset(dim, type);
1266         if (type == isl_dim_param) {
1267                 if (dim && dim->nested[0] &&
1268                     !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1269                                                     isl_dim_param, first, num)))
1270                         goto error;
1271                 if (dim && dim->nested[1] &&
1272                     !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1273                                                     isl_dim_param, first, num)))
1274                         goto error;
1275         }
1276         return dim;
1277 error:
1278         isl_space_free(dim);
1279         return NULL;
1280 }
1281
1282 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1283                 unsigned first, unsigned n)
1284 {
1285         if (!dim)
1286                 return NULL;
1287         return isl_space_drop_dims(dim, isl_dim_in, first, n);
1288 }
1289
1290 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1291                 unsigned first, unsigned n)
1292 {
1293         if (!dim)
1294                 return NULL;
1295         return isl_space_drop_dims(dim, isl_dim_out, first, n);
1296 }
1297
1298 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1299 {
1300         if (!dim)
1301                 return NULL;
1302         dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1303         dim = isl_space_reverse(dim);
1304         dim = mark_as_set(dim);
1305         return dim;
1306 }
1307
1308 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1309 {
1310         if (!dim)
1311                 return NULL;
1312         if (!isl_space_is_set(dim))
1313                 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1314                         "not a set space", goto error);
1315         dim = isl_space_reverse(dim);
1316         dim = isl_space_reset(dim, isl_dim_out);
1317         return dim;
1318 error:
1319         isl_space_free(dim);
1320         return NULL;
1321 }
1322
1323 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1324 {
1325         if (!dim)
1326                 return NULL;
1327         dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1328         dim = mark_as_set(dim);
1329         return dim;
1330 }
1331
1332 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1333 {
1334         if (!dim)
1335                 return NULL;
1336         if (!isl_space_is_set(dim))
1337                 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1338                         "not a set space", goto error);
1339         return isl_space_reset(dim, isl_dim_in);
1340 error:
1341         isl_space_free(dim);
1342         return NULL;
1343 }
1344
1345 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1346 {
1347         if (isl_space_is_params(space))
1348                 return space;
1349         space = isl_space_drop_dims(space,
1350                             isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1351         space = isl_space_drop_dims(space,
1352                             isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1353         space = mark_as_params(space);
1354         return space;
1355 }
1356
1357 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1358 {
1359         if (!space)
1360                 return NULL;
1361         if (!isl_space_is_params(space))
1362                 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1363                         "not a parameter space", goto error);
1364         return isl_space_reset(space, isl_dim_set);
1365 error:
1366         isl_space_free(space);
1367         return NULL;
1368 }
1369
1370 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1371 {
1372         dim = isl_space_cow(dim);
1373         if (!dim)
1374                 return NULL;
1375
1376         dim->n_out += dim->n_in;
1377         dim->n_in = 0;
1378         dim = isl_space_reset(dim, isl_dim_in);
1379         dim = isl_space_reset(dim, isl_dim_out);
1380
1381         return dim;
1382 }
1383
1384 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1385         unsigned n_div)
1386 {
1387         int i;
1388
1389         if (!dim)
1390                 return NULL;
1391         if (n_div == 0 &&
1392             dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1393                 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1394         dim = isl_space_cow(dim);
1395         if (!dim)
1396                 return NULL;
1397         dim->n_out += dim->nparam + dim->n_in + n_div;
1398         dim->nparam = 0;
1399         dim->n_in = 0;
1400
1401         for (i = 0; i < dim->n_id; ++i)
1402                 isl_id_free(get_id(dim, isl_dim_out, i));
1403         dim->n_id = 0;
1404         dim = isl_space_reset(dim, isl_dim_in);
1405         dim = isl_space_reset(dim, isl_dim_out);
1406
1407         return dim;
1408 }
1409
1410 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1411 {
1412         if (!dim1 || !dim2)
1413                 return -1;
1414         if (dim1 == dim2)
1415                 return 1;
1416         return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1417                isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1418                isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1419 }
1420
1421 /* Is space1 equal to the domain of space2?
1422  */
1423 int isl_space_is_domain(__isl_keep isl_space *space1,
1424         __isl_keep isl_space *space2)
1425 {
1426         if (!space1 || !space2)
1427                 return -1;
1428         if (!isl_space_is_set(space1))
1429                 return 0;
1430         return match(space1, isl_dim_param, space2, isl_dim_param) &&
1431                isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1432 }
1433
1434 int isl_space_compatible(__isl_keep isl_space *dim1,
1435         __isl_keep isl_space *dim2)
1436 {
1437         return dim1->nparam == dim2->nparam &&
1438                dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1439 }
1440
1441 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1442 {
1443         int i;
1444         isl_id *id;
1445
1446         if (!dim)
1447                 return hash;
1448
1449         hash = isl_hash_builtin(hash, dim->nparam);
1450         hash = isl_hash_builtin(hash, dim->n_in);
1451         hash = isl_hash_builtin(hash, dim->n_out);
1452
1453         for (i = 0; i < dim->nparam; ++i) {
1454                 id = get_id(dim, isl_dim_param, i);
1455                 hash = isl_hash_id(hash, id);
1456         }
1457
1458         id = tuple_id(dim, isl_dim_in);
1459         hash = isl_hash_id(hash, id);
1460         id = tuple_id(dim, isl_dim_out);
1461         hash = isl_hash_id(hash, id);
1462
1463         hash = isl_hash_dim(hash, dim->nested[0]);
1464         hash = isl_hash_dim(hash, dim->nested[1]);
1465
1466         return hash;
1467 }
1468
1469 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1470 {
1471         uint32_t hash;
1472
1473         if (!dim)
1474                 return 0;
1475
1476         hash = isl_hash_init();
1477         hash = isl_hash_dim(hash, dim);
1478
1479         return hash;
1480 }
1481
1482 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1483 {
1484         if (!dim)
1485                 return -1;
1486
1487         if (!isl_space_is_set(dim))
1488                 return 0;
1489
1490         return dim->nested[1] != NULL;
1491 }
1492
1493 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1494 {
1495         isl_space *wrap;
1496
1497         if (!dim)
1498                 return NULL;
1499
1500         wrap = isl_space_set_alloc(dim->ctx,
1501                                     dim->nparam, dim->n_in + dim->n_out);
1502
1503         wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1504         wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1505         wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1506
1507         if (!wrap)
1508                 goto error;
1509
1510         wrap->nested[1] = dim;
1511
1512         return wrap;
1513 error:
1514         isl_space_free(dim);
1515         return NULL;
1516 }
1517
1518 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1519 {
1520         isl_space *unwrap;
1521
1522         if (!dim)
1523                 return NULL;
1524
1525         if (!isl_space_is_wrapping(dim))
1526                 isl_die(dim->ctx, isl_error_invalid, "not a wrapping dim",
1527                         goto error);
1528
1529         unwrap = isl_space_copy(dim->nested[1]);
1530         isl_space_free(dim);
1531
1532         return unwrap;
1533 error:
1534         isl_space_free(dim);
1535         return NULL;
1536 }
1537
1538 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1539 {
1540         if (type != isl_dim_in && type != isl_dim_out)
1541                 return 0;
1542         if (!dim)
1543                 return -1;
1544         if (dim->tuple_id[type - isl_dim_in])
1545                 return 1;
1546         if (dim->nested[type - isl_dim_in])
1547                 return 1;
1548         return 0;
1549 }
1550
1551 int isl_space_may_be_set(__isl_keep isl_space *dim)
1552 {
1553         if (!dim)
1554                 return -1;
1555         if (isl_space_is_set(dim))
1556                 return 1;
1557         if (isl_space_dim(dim, isl_dim_in) != 0)
1558                 return 0;
1559         if (isl_space_is_named_or_nested(dim, isl_dim_in))
1560                 return 0;
1561         return 1;
1562 }
1563
1564 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1565         enum isl_dim_type type)
1566 {
1567         if (!isl_space_is_named_or_nested(dim, type))
1568                 return dim;
1569
1570         dim = isl_space_cow(dim);
1571         if (!dim)
1572                 return NULL;
1573
1574         isl_id_free(dim->tuple_id[type - isl_dim_in]);
1575         dim->tuple_id[type - isl_dim_in] = NULL;
1576         isl_space_free(dim->nested[type - isl_dim_in]);
1577         dim->nested[type - isl_dim_in] = NULL;
1578
1579         return dim;
1580 }
1581
1582 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1583 {
1584         if (!dim)
1585                 return NULL;
1586         if (!dim->nested[0] && !dim->nested[1])
1587                 return dim;
1588
1589         if (dim->nested[0])
1590                 dim = isl_space_reset(dim, isl_dim_in);
1591         if (dim && dim->nested[1])
1592                 dim = isl_space_reset(dim, isl_dim_out);
1593
1594         return dim;
1595 }
1596
1597 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1598 {
1599         if (!dim)
1600                 return NULL;
1601         if (!dim->nested[0])
1602                 return dim;
1603
1604         return isl_space_reset(dim, isl_dim_in);
1605 }
1606
1607 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1608 {
1609         if (!dim)
1610                 return NULL;
1611         if (!dim->nested[1])
1612                 return dim;
1613
1614         return isl_space_reset(dim, isl_dim_out);
1615 }
1616
1617 /* Replace the dimensions of the given type of dst by those of src.
1618  */
1619 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1620         enum isl_dim_type type, __isl_keep isl_space *src)
1621 {
1622         dst = isl_space_cow(dst);
1623
1624         if (!dst || !src)
1625                 goto error;
1626
1627         dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1628         dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1629         dst = copy_ids(dst, type, 0, src, type);
1630
1631         if (dst && type == isl_dim_param) {
1632                 int i;
1633                 for (i = 0; i <= 1; ++i) {
1634                         if (!dst->nested[i])
1635                                 continue;
1636                         dst->nested[i] = isl_space_replace(dst->nested[i],
1637                                                          type, src);
1638                         if (!dst->nested[i])
1639                                 goto error;
1640                 }
1641         }
1642
1643         return dst;
1644 error:
1645         isl_space_free(dst);
1646         return NULL;
1647 }
1648
1649 /* Given a dimension specification "dim" of a set, create a dimension
1650  * specification for the lift of the set.  In particular, the result
1651  * is of the form [dim -> local[..]], with n_local variables in the
1652  * range of the wrapped map.
1653  */
1654 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1655 {
1656         isl_space *local_dim;
1657
1658         if (!dim)
1659                 return NULL;
1660
1661         local_dim = isl_space_dup(dim);
1662         local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1663         local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1664         local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1665         dim = isl_space_join(isl_space_from_domain(dim),
1666                             isl_space_from_range(local_dim));
1667         dim = isl_space_wrap(dim);
1668         dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1669
1670         return dim;
1671 }
1672
1673 int isl_space_can_zip(__isl_keep isl_space *dim)
1674 {
1675         if (!dim)
1676                 return -1;
1677
1678         return dim->nested[0] && dim->nested[1];
1679 }
1680
1681 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1682 {
1683         isl_space *dom, *ran;
1684         isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1685
1686         if (!isl_space_can_zip(dim))
1687                 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1688                         goto error);
1689
1690         if (!dim)
1691                 return 0;
1692         dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1693         ran = isl_space_unwrap(isl_space_range(dim));
1694         dom_dom = isl_space_domain(isl_space_copy(dom));
1695         dom_ran = isl_space_range(dom);
1696         ran_dom = isl_space_domain(isl_space_copy(ran));
1697         ran_ran = isl_space_range(ran);
1698         dom = isl_space_join(isl_space_from_domain(dom_dom),
1699                            isl_space_from_range(ran_dom));
1700         ran = isl_space_join(isl_space_from_domain(dom_ran),
1701                            isl_space_from_range(ran_ran));
1702         return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1703                             isl_space_from_range(isl_space_wrap(ran)));
1704 error:
1705         isl_space_free(dim);
1706         return NULL;
1707 }
1708
1709 int isl_space_has_named_params(__isl_keep isl_space *dim)
1710 {
1711         int i;
1712         unsigned off;
1713
1714         if (!dim)
1715                 return -1;
1716         if (dim->nparam == 0)
1717                 return 1;
1718         off = isl_space_offset(dim, isl_dim_param);
1719         if (off + dim->nparam > dim->n_id)
1720                 return 0;
1721         for (i = 0; i < dim->nparam; ++i)
1722                 if (!dim->ids[off + i])
1723                         return 0;
1724         return 1;
1725 }
1726
1727 /* Align the initial parameters of dim1 to match the order in dim2.
1728  */
1729 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1730         __isl_take isl_space *dim2)
1731 {
1732         isl_reordering *exp;
1733
1734         if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1735                 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1736                         "parameter alignment requires named parameters",
1737                         goto error);
1738
1739         dim2 = isl_space_params(dim2);
1740         exp = isl_parameter_alignment_reordering(dim1, dim2);
1741         exp = isl_reordering_extend_space(exp, dim1);
1742         isl_space_free(dim2);
1743         if (!exp)
1744                 return NULL;
1745         dim1 = isl_space_copy(exp->dim);
1746         isl_reordering_free(exp);
1747         return dim1;
1748 error:
1749         isl_space_free(dim1);
1750         isl_space_free(dim2);
1751         return NULL;
1752 }
1753
1754 /* Given the space of set (domain), construct a space for a map
1755  * with as domain the given space and as range the range of "model".
1756  */
1757 __isl_give isl_space *isl_space_extend_domain_with_range(
1758         __isl_take isl_space *domain, __isl_take isl_space *model)
1759 {
1760         isl_space *space;
1761
1762         space = isl_space_from_domain(domain);
1763         space = isl_space_add_dims(space, isl_dim_out,
1764                                     isl_space_dim(model, isl_dim_out));
1765         if (isl_space_has_tuple_id(model, isl_dim_out))
1766                 space = isl_space_set_tuple_id(space, isl_dim_out,
1767                                 isl_space_get_tuple_id(model, isl_dim_out));
1768         isl_space_free(model);
1769         return space;
1770 }