isl_space_wrap: properly create set space
[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 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
52                         unsigned nparam, unsigned dim)
53 {
54         return isl_space_alloc(ctx, nparam, 0, dim);
55 }
56
57 static unsigned global_pos(__isl_keep isl_space *dim,
58                                  enum isl_dim_type type, unsigned pos)
59 {
60         struct isl_ctx *ctx = dim->ctx;
61
62         switch (type) {
63         case isl_dim_param:
64                 isl_assert(ctx, pos < dim->nparam,
65                             return isl_space_dim(dim, isl_dim_all));
66                 return pos;
67         case isl_dim_in:
68                 isl_assert(ctx, pos < dim->n_in,
69                             return isl_space_dim(dim, isl_dim_all));
70                 return pos + dim->nparam;
71         case isl_dim_out:
72                 isl_assert(ctx, pos < dim->n_out,
73                             return isl_space_dim(dim, isl_dim_all));
74                 return pos + dim->nparam + dim->n_in;
75         default:
76                 isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
77         }
78         return isl_space_dim(dim, isl_dim_all);
79 }
80
81 /* Extend length of ids array to the total number of dimensions.
82  */
83 static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
84 {
85         isl_id **ids;
86         int i;
87
88         if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
89                 return dim;
90
91         if (!dim->ids) {
92                 dim->ids = isl_calloc_array(dim->ctx,
93                                 isl_id *, isl_space_dim(dim, isl_dim_all));
94                 if (!dim->ids)
95                         goto error;
96         } else {
97                 ids = isl_realloc_array(dim->ctx, dim->ids,
98                                 isl_id *, isl_space_dim(dim, isl_dim_all));
99                 if (!ids)
100                         goto error;
101                 dim->ids = ids;
102                 for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); ++i)
103                         dim->ids[i] = NULL;
104         }
105
106         dim->n_id = isl_space_dim(dim, isl_dim_all);
107
108         return dim;
109 error:
110         isl_space_free(dim);
111         return NULL;
112 }
113
114 static __isl_give isl_space *set_id(__isl_take isl_space *dim,
115         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
116 {
117         dim = isl_space_cow(dim);
118
119         if (!dim)
120                 goto error;
121
122         pos = global_pos(dim, type, pos);
123         if (pos == isl_space_dim(dim, isl_dim_all))
124                 goto error;
125
126         if (pos >= dim->n_id) {
127                 if (!id)
128                         return dim;
129                 dim = extend_ids(dim);
130                 if (!dim)
131                         goto error;
132         }
133
134         dim->ids[pos] = id;
135
136         return dim;
137 error:
138         isl_id_free(id);
139         isl_space_free(dim);
140         return NULL;
141 }
142
143 static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
144                                  enum isl_dim_type type, unsigned pos)
145 {
146         if (!dim)
147                 return NULL;
148
149         pos = global_pos(dim, type, pos);
150         if (pos == isl_space_dim(dim, isl_dim_all))
151                 return NULL;
152         if (pos >= dim->n_id)
153                 return NULL;
154         return dim->ids[pos];
155 }
156
157 static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
158 {
159         switch (type) {
160         case isl_dim_param:     return 0;
161         case isl_dim_in:        return dim->nparam;
162         case isl_dim_out:       return dim->nparam + dim->n_in;
163         default:                return 0;
164         }
165 }
166
167 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
168 {
169         switch (type) {
170         case isl_dim_param:     return dim->nparam;
171         case isl_dim_in:        return dim->n_in;
172         case isl_dim_out:       return dim->n_out;
173         case isl_dim_all:       return dim->nparam + dim->n_in + dim->n_out;
174         default:                return 0;
175         }
176 }
177
178 unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
179 {
180         if (!dim)
181                 return 0;
182         return n(dim, type);
183 }
184
185 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
186 {
187         if (!dim)
188                 return 0;
189         return offset(dim, type);
190 }
191
192 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
193         enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
194         enum isl_dim_type src_type)
195 {
196         int i;
197         isl_id *id;
198
199         if (!dst)
200                 return NULL;
201
202         for (i = 0; i < n(src, src_type); ++i) {
203                 id = get_id(src, src_type, i);
204                 if (!id)
205                         continue;
206                 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
207                 if (!dst)
208                         return NULL;
209         }
210         return dst;
211 }
212
213 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
214 {
215         isl_space *dup;
216         if (!dim)
217                 return NULL;
218         dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
219         if (dim->tuple_id[0] &&
220             !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
221                 goto error;
222         if (dim->tuple_id[1] &&
223             !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
224                 goto error;
225         if (dim->nested[0] && !(dup->nested[0] = isl_space_copy(dim->nested[0])))
226                 goto error;
227         if (dim->nested[1] && !(dup->nested[1] = isl_space_copy(dim->nested[1])))
228                 goto error;
229         if (!dim->ids)
230                 return dup;
231         dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
232         dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
233         dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
234         return dup;
235 error:
236         isl_space_free(dup);
237         return NULL;
238 }
239
240 __isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
241 {
242         if (!dim)
243                 return NULL;
244
245         if (dim->ref == 1)
246                 return dim;
247         dim->ref--;
248         return isl_space_dup(dim);
249 }
250
251 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
252 {
253         if (!dim)
254                 return NULL;
255
256         dim->ref++;
257         return dim;
258 }
259
260 void isl_space_free(__isl_take isl_space *dim)
261 {
262         int i;
263
264         if (!dim)
265                 return;
266
267         if (--dim->ref > 0)
268                 return;
269
270         isl_id_free(dim->tuple_id[0]);
271         isl_id_free(dim->tuple_id[1]);
272
273         isl_space_free(dim->nested[0]);
274         isl_space_free(dim->nested[1]);
275
276         for (i = 0; i < dim->n_id; ++i)
277                 isl_id_free(dim->ids[i]);
278         free(dim->ids);
279         isl_ctx_deref(dim->ctx);
280         
281         free(dim);
282 }
283
284 static int name_ok(isl_ctx *ctx, const char *s)
285 {
286         char *p;
287         long dummy;
288
289         dummy = strtol(s, &p, 0);
290         if (p != s)
291                 isl_die(ctx, isl_error_invalid, "name looks like a number",
292                         return 0);
293
294         return 1;
295 }
296
297 int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
298 {
299         if (!dim)
300                 return -1;
301         if (type != isl_dim_in && type != isl_dim_out)
302                 isl_die(dim->ctx, isl_error_invalid,
303                         "only input, output and set tuples can have ids",
304                         return -1);
305         return dim->tuple_id[type - isl_dim_in] != NULL;
306 }
307
308 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
309         enum isl_dim_type type)
310 {
311         if (!dim)
312                 return NULL;
313         if (type != isl_dim_in && type != isl_dim_out)
314                 isl_die(dim->ctx, isl_error_invalid,
315                         "only input, output and set tuples can have ids",
316                         return NULL);
317         if (!dim->tuple_id[type - isl_dim_in])
318                 isl_die(dim->ctx, isl_error_invalid,
319                         "tuple has no id", return NULL);
320         return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
321 }
322
323 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
324         enum isl_dim_type type, __isl_take isl_id *id)
325 {
326         dim = isl_space_cow(dim);
327         if (!dim || !id)
328                 goto error;
329         if (type != isl_dim_in && type != isl_dim_out)
330                 isl_die(dim->ctx, isl_error_invalid,
331                         "only input, output and set tuples can have names",
332                         goto error);
333
334         isl_id_free(dim->tuple_id[type - isl_dim_in]);
335         dim->tuple_id[type - isl_dim_in] = id;
336
337         return dim;
338 error:
339         isl_id_free(id);
340         isl_space_free(dim);
341         return NULL;
342 }
343
344 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
345         enum isl_dim_type type)
346 {
347         dim = isl_space_cow(dim);
348         if (!dim)
349                 return NULL;
350         if (type != isl_dim_in && type != isl_dim_out)
351                 isl_die(dim->ctx, isl_error_invalid,
352                         "only input, output and set tuples can have names",
353                         goto error);
354
355         isl_id_free(dim->tuple_id[type - isl_dim_in]);
356         dim->tuple_id[type - isl_dim_in] = NULL;
357
358         return dim;
359 error:
360         isl_space_free(dim);
361         return NULL;
362 }
363
364 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *dim,
365         enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
366 {
367         dim = isl_space_cow(dim);
368         if (!dim || !id)
369                 goto error;
370         isl_id_free(get_id(dim, type, pos));
371         return set_id(dim, type, pos, id);
372 error:
373         isl_id_free(id);
374         isl_space_free(dim);
375         return NULL;
376 }
377
378 int isl_space_has_dim_id(__isl_keep isl_space *dim,
379         enum isl_dim_type type, unsigned pos)
380 {
381         if (!dim)
382                 return -1;
383         return get_id(dim, type, pos) != NULL;
384 }
385
386 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
387         enum isl_dim_type type, unsigned pos)
388 {
389         if (!dim)
390                 return NULL;
391         if (!get_id(dim, type, pos))
392                 isl_die(dim->ctx, isl_error_invalid,
393                         "dim has no id", return NULL);
394         return isl_id_copy(get_id(dim, type, pos));
395 }
396
397 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
398         enum isl_dim_type type, const char *s)
399 {
400         isl_id *id;
401
402         if (!dim)
403                 return NULL;
404
405         if (!s)
406                 return isl_space_reset_tuple_id(dim, type);
407
408         if (!name_ok(dim->ctx, s))
409                 goto error;
410
411         id = isl_id_alloc(dim->ctx, s, NULL);
412         return isl_space_set_tuple_id(dim, type, id);
413 error:
414         isl_space_free(dim);
415         return NULL;
416 }
417
418 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
419          enum isl_dim_type type)
420 {
421         isl_id *id;
422         if (!dim)
423                 return NULL;
424         if (type != isl_dim_in && type != isl_dim_out)
425                 return NULL;
426         id = dim->tuple_id[type - isl_dim_in];
427         return id ? id->name : NULL;
428 }
429
430 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
431                                  enum isl_dim_type type, unsigned pos,
432                                  const char *s)
433 {
434         isl_id *id;
435
436         if (!dim)
437                 return NULL;
438         if (!name_ok(dim->ctx, s))
439                 goto error;
440         id = isl_id_alloc(dim->ctx, s, NULL);
441         return isl_space_set_dim_id(dim, type, pos, id);
442 error:
443         isl_space_free(dim);
444         return NULL;
445 }
446
447 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
448                                  enum isl_dim_type type, unsigned pos)
449 {
450         isl_id *id = get_id(dim, type, pos);
451         return id ? id->name : NULL;
452 }
453
454 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
455         __isl_keep isl_id *id)
456 {
457         int i;
458         int offset;
459         int n;
460
461         if (!dim || !id)
462                 return -1;
463
464         offset = isl_space_offset(dim, type);
465         n = isl_space_dim(dim, type);
466         for (i = 0; i < n && offset + i < dim->n_id; ++i)
467                 if (dim->ids[offset + i] == id)
468                         return i;
469
470         return -1;
471 }
472
473 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
474         enum isl_dim_type type)
475 {
476         if (!dim)
477                 return NULL;
478         if (type == isl_dim_in)
479                 return dim->tuple_id[0];
480         if (type == isl_dim_out)
481                 return dim->tuple_id[1];
482         return NULL;
483 }
484
485 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
486         enum isl_dim_type type)
487 {
488         if (!dim)
489                 return NULL;
490         if (type == isl_dim_in)
491                 return dim->nested[0];
492         if (type == isl_dim_out)
493                 return dim->nested[1];
494         return NULL;
495 }
496
497 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
498                         __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
499 {
500         isl_id *id1, *id2;
501         isl_space *nested1, *nested2;
502
503         if (!dim1 || !dim2)
504                 return -1;
505
506         if (dim1 == dim2 && dim1_type == dim2_type)
507                 return 1;
508
509         if (n(dim1, dim1_type) != n(dim2, dim2_type))
510                 return 0;
511         id1 = tuple_id(dim1, dim1_type);
512         id2 = tuple_id(dim2, dim2_type);
513         if (!id1 ^ !id2)
514                 return 0;
515         if (id1 && id1 != id2)
516                 return 0;
517         nested1 = nested(dim1, dim1_type);
518         nested2 = nested(dim2, dim2_type);
519         if (!nested1 ^ !nested2)
520                 return 0;
521         if (nested1 && !isl_space_is_equal(nested1, nested2))
522                 return 0;
523         return 1;
524 }
525
526 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
527         __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
528 {
529         int i;
530
531         if (dim1 == dim2 && dim1_type == dim2_type)
532                 return 1;
533
534         if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
535                 return 0;
536
537         if (!dim1->ids && !dim2->ids)
538                 return 1;
539
540         for (i = 0; i < n(dim1, dim1_type); ++i) {
541                 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
542                         return 0;
543         }
544         return 1;
545 }
546
547 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
548         __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
549 {
550         if (!dim1 || !dim2)
551                 return -1;
552
553         return match(dim1, dim1_type, dim2, dim2_type);
554 }
555
556 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
557         unsigned first, unsigned n, __isl_keep isl_id **ids)
558 {
559         int i;
560
561         for (i = 0; i < n ; ++i)
562                 ids[i] = get_id(dim, type, first + i);
563 }
564
565 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
566                         unsigned nparam, unsigned n_in, unsigned n_out)
567 {
568         isl_id **ids = NULL;
569
570         if (!dim)
571                 return NULL;
572         if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
573                 return dim;
574
575         isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
576         isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
577         isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
578
579         dim = isl_space_cow(dim);
580
581         if (dim->ids) {
582                 ids = isl_calloc_array(dim->ctx, isl_id *,
583                                          nparam + n_in + n_out);
584                 if (!ids)
585                         goto error;
586                 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
587                 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
588                 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
589                 free(dim->ids);
590                 dim->ids = ids;
591                 dim->n_id = nparam + n_in + n_out;
592         }
593         dim->nparam = nparam;
594         dim->n_in = n_in;
595         dim->n_out = n_out;
596
597         return dim;
598 error:
599         free(ids);
600         isl_space_free(dim);
601         return NULL;
602 }
603
604 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
605         enum isl_dim_type type, unsigned n)
606 {
607         if (!dim)
608                 return NULL;
609         dim = isl_space_reset(dim, type);
610         switch (type) {
611         case isl_dim_param:
612                 dim = isl_space_extend(dim,
613                                         dim->nparam + n, dim->n_in, dim->n_out);
614                 if (dim && dim->nested[0] &&
615                     !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
616                                                     isl_dim_param, n)))
617                         goto error;
618                 if (dim && dim->nested[1] &&
619                     !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
620                                                     isl_dim_param, n)))
621                         goto error;
622                 return dim;
623         case isl_dim_in:
624                 return isl_space_extend(dim,
625                                         dim->nparam, dim->n_in + n, dim->n_out);
626         case isl_dim_out:
627                 return isl_space_extend(dim,
628                                         dim->nparam, dim->n_in, dim->n_out + n);
629         default:
630                 isl_die(dim->ctx, isl_error_invalid,
631                         "cannot add dimensions of specified type", goto error);
632         }
633 error:
634         isl_space_free(dim);
635         return NULL;
636 }
637
638 static int valid_dim_type(enum isl_dim_type type)
639 {
640         switch (type) {
641         case isl_dim_param:
642         case isl_dim_in:
643         case isl_dim_out:
644                 return 1;
645         default:
646                 return 0;
647         }
648 }
649
650 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
651         enum isl_dim_type type, unsigned pos, unsigned n)
652 {
653         isl_id **ids = NULL;
654
655         if (!dim)
656                 return NULL;
657         if (n == 0)
658                 return isl_space_reset(dim, type);
659
660         if (!valid_dim_type(type))
661                 isl_die(dim->ctx, isl_error_invalid,
662                         "cannot insert dimensions of specified type",
663                         goto error);
664
665         isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
666
667         dim = isl_space_cow(dim);
668         if (!dim)
669                 return NULL;
670
671         if (dim->ids) {
672                 enum isl_dim_type t;
673                 int off;
674                 int s[3];
675                 int *size = s - isl_dim_param;
676                 ids = isl_calloc_array(dim->ctx, isl_id *,
677                                      dim->nparam + dim->n_in + dim->n_out + n);
678                 if (!ids)
679                         goto error;
680                 off = 0;
681                 size[isl_dim_param] = dim->nparam;
682                 size[isl_dim_in] = dim->n_in;
683                 size[isl_dim_out] = dim->n_out;
684                 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
685                         if (t != type) {
686                                 get_ids(dim, t, 0, size[t], ids + off);
687                                 off += size[t];
688                         } else {
689                                 get_ids(dim, t, 0, pos, ids + off);
690                                 off += pos + n;
691                                 get_ids(dim, t, pos, size[t] - pos, ids + off);
692                                 off += size[t] - pos;
693                         }
694                 }
695                 free(dim->ids);
696                 dim->ids = ids;
697                 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
698         }
699         switch (type) {
700         case isl_dim_param:     dim->nparam += n; break;
701         case isl_dim_in:        dim->n_in += n; break;
702         case isl_dim_out:       dim->n_out += n; break;
703         default:                ;
704         }
705         dim = isl_space_reset(dim, type);
706
707         return dim;
708 error:
709         isl_space_free(dim);
710         return NULL;
711 }
712
713 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
714         enum isl_dim_type dst_type, unsigned dst_pos,
715         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
716 {
717         int i;
718
719         if (!dim)
720                 return NULL;
721         if (n == 0)
722                 return dim;
723
724         isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
725                 goto error);
726
727         if (dst_type == src_type && dst_pos == src_pos)
728                 return dim;
729
730         isl_assert(dim->ctx, dst_type != src_type, goto error);
731
732         dim = isl_space_reset(dim, src_type);
733         dim = isl_space_reset(dim, dst_type);
734
735         dim = isl_space_cow(dim);
736         if (!dim)
737                 return NULL;
738
739         if (dim->ids) {
740                 isl_id **ids;
741                 enum isl_dim_type t;
742                 int off;
743                 int s[3];
744                 int *size = s - isl_dim_param;
745                 ids = isl_calloc_array(dim->ctx, isl_id *,
746                                          dim->nparam + dim->n_in + dim->n_out);
747                 if (!ids)
748                         goto error;
749                 off = 0;
750                 size[isl_dim_param] = dim->nparam;
751                 size[isl_dim_in] = dim->n_in;
752                 size[isl_dim_out] = dim->n_out;
753                 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
754                         if (t == dst_type) {
755                                 get_ids(dim, t, 0, dst_pos, ids + off);
756                                 off += dst_pos;
757                                 get_ids(dim, src_type, src_pos, n, ids + off);
758                                 off += n;
759                                 get_ids(dim, t, dst_pos, size[t] - dst_pos,
760                                                 ids + off);
761                                 off += size[t] - dst_pos;
762                         } else if (t == src_type) {
763                                 get_ids(dim, t, 0, src_pos, ids + off);
764                                 off += src_pos;
765                                 get_ids(dim, t, src_pos + n,
766                                             size[t] - src_pos - n, ids + off);
767                                 off += size[t] - src_pos - n;
768                         } else {
769                                 get_ids(dim, t, 0, size[t], ids + off);
770                                 off += size[t];
771                         }
772                 }
773                 free(dim->ids);
774                 dim->ids = ids;
775                 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
776         }
777
778         switch (dst_type) {
779         case isl_dim_param:     dim->nparam += n; break;
780         case isl_dim_in:        dim->n_in += n; break;
781         case isl_dim_out:       dim->n_out += n; break;
782         default:                ;
783         }
784
785         switch (src_type) {
786         case isl_dim_param:     dim->nparam -= n; break;
787         case isl_dim_in:        dim->n_in -= n; break;
788         case isl_dim_out:       dim->n_out -= n; break;
789         default:                ;
790         }
791
792         if (dst_type != isl_dim_param && src_type != isl_dim_param)
793                 return dim;
794
795         for (i = 0; i < 2; ++i) {
796                 if (!dim->nested[i])
797                         continue;
798                 dim->nested[i] = isl_space_replace(dim->nested[i],
799                                                  isl_dim_param, dim);
800                 if (!dim->nested[i])
801                         goto error;
802         }
803
804         return dim;
805 error:
806         isl_space_free(dim);
807         return NULL;
808 }
809
810 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
811         __isl_take isl_space *right)
812 {
813         isl_space *dim;
814
815         if (!left || !right)
816                 goto error;
817
818         isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
819                         goto error);
820         isl_assert(left->ctx,
821                 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
822                 goto error);
823
824         dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
825         if (!dim)
826                 goto error;
827
828         dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
829         dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
830         dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
831
832         if (dim && left->tuple_id[0] &&
833             !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
834                 goto error;
835         if (dim && right->tuple_id[1] &&
836             !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
837                 goto error;
838         if (dim && left->nested[0] &&
839             !(dim->nested[0] = isl_space_copy(left->nested[0])))
840                 goto error;
841         if (dim && right->nested[1] &&
842             !(dim->nested[1] = isl_space_copy(right->nested[1])))
843                 goto error;
844
845         isl_space_free(left);
846         isl_space_free(right);
847
848         return dim;
849 error:
850         isl_space_free(left);
851         isl_space_free(right);
852         return NULL;
853 }
854
855 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
856         __isl_take isl_space *right)
857 {
858         isl_space *dom1, *dom2, *nest1, *nest2;
859
860         if (!left || !right)
861                 goto error;
862
863         isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
864                         goto error);
865
866         dom1 = isl_space_domain(isl_space_copy(left));
867         dom2 = isl_space_domain(isl_space_copy(right));
868         nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
869
870         dom1 = isl_space_range(left);
871         dom2 = isl_space_range(right);
872         nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
873
874         return isl_space_join(isl_space_reverse(nest1), nest2);
875 error:
876         isl_space_free(left);
877         isl_space_free(right);
878         return NULL;
879 }
880
881 /* Given two spaces { A -> C } and { B -> C }, construct the space
882  * { [A -> B] -> C }
883  */
884 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
885         __isl_take isl_space *right)
886 {
887         isl_space *ran, *dom1, *dom2, *nest;
888
889         if (!left || !right)
890                 goto error;
891
892         if (!match(left, isl_dim_param, right, isl_dim_param))
893                 isl_die(left->ctx, isl_error_invalid,
894                         "parameters need to match", goto error);
895         if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
896                 isl_die(left->ctx, isl_error_invalid,
897                         "ranges need to match", goto error);
898
899         ran = isl_space_range(isl_space_copy(left));
900
901         dom1 = isl_space_domain(left);
902         dom2 = isl_space_domain(right);
903         nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
904
905         return isl_space_join(isl_space_reverse(nest), ran);
906 error:
907         isl_space_free(left);
908         isl_space_free(right);
909         return NULL;
910 }
911
912 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
913         __isl_take isl_space *right)
914 {
915         isl_space *dom, *ran1, *ran2, *nest;
916
917         if (!left || !right)
918                 goto error;
919
920         isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
921                         goto error);
922         if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
923                 isl_die(left->ctx, isl_error_invalid,
924                         "domains need to match", goto error);
925
926         dom = isl_space_domain(isl_space_copy(left));
927
928         ran1 = isl_space_range(left);
929         ran2 = isl_space_range(right);
930         nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
931
932         return isl_space_join(isl_space_reverse(dom), nest);
933 error:
934         isl_space_free(left);
935         isl_space_free(right);
936         return NULL;
937 }
938
939 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
940 {
941         isl_id **ids = NULL;
942
943         if (!dim)
944                 return NULL;
945         isl_assert(dim->ctx, dim->n_in == 0, goto error);
946         if (dim->n_out == 0 && !isl_space_is_named_or_nested(dim, isl_dim_out))
947                 return dim;
948         dim = isl_space_cow(dim);
949         if (!dim)
950                 return NULL;
951         if (dim->ids) {
952                 ids = isl_calloc_array(dim->ctx, isl_id *,
953                                         dim->nparam + dim->n_out + dim->n_out);
954                 if (!ids)
955                         goto error;
956                 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
957                 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
958         }
959         dim->n_in = dim->n_out;
960         if (ids) {
961                 free(dim->ids);
962                 dim->ids = ids;
963                 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
964                 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
965         }
966         isl_id_free(dim->tuple_id[0]);
967         dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
968         isl_space_free(dim->nested[0]);
969         dim->nested[0] = isl_space_copy(dim->nested[1]);
970         return dim;
971 error:
972         isl_space_free(dim);
973         return NULL;
974 }
975
976 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
977         enum isl_dim_type type,
978         unsigned first, unsigned n, __isl_take isl_id **ids)
979 {
980         int i;
981
982         for (i = 0; i < n ; ++i)
983                 dim = set_id(dim, type, first + i, ids[i]);
984
985         return dim;
986 }
987
988 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
989 {
990         unsigned t;
991         isl_space *nested;
992         isl_id **ids = NULL;
993         isl_id *id;
994
995         if (!dim)
996                 return NULL;
997         if (match(dim, isl_dim_in, dim, isl_dim_out))
998                 return dim;
999
1000         dim = isl_space_cow(dim);
1001         if (!dim)
1002                 return NULL;
1003
1004         id = dim->tuple_id[0];
1005         dim->tuple_id[0] = dim->tuple_id[1];
1006         dim->tuple_id[1] = id;
1007
1008         nested = dim->nested[0];
1009         dim->nested[0] = dim->nested[1];
1010         dim->nested[1] = nested;
1011
1012         if (dim->ids) {
1013                 ids = isl_alloc_array(dim->ctx, isl_id *,
1014                                         dim->n_in + dim->n_out);
1015                 if (!ids)
1016                         goto error;
1017                 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1018                 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1019         }
1020
1021         t = dim->n_in;
1022         dim->n_in = dim->n_out;
1023         dim->n_out = t;
1024
1025         if (dim->ids) {
1026                 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1027                 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1028                 free(ids);
1029         }
1030
1031         return dim;
1032 error:
1033         free(ids);
1034         isl_space_free(dim);
1035         return NULL;
1036 }
1037
1038 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1039         enum isl_dim_type type, unsigned first, unsigned num)
1040 {
1041         int i;
1042
1043         if (!dim)
1044                 return NULL;
1045
1046         if (num == 0)
1047                 return isl_space_reset(dim, type);
1048
1049         if (!valid_dim_type(type))
1050                 isl_die(dim->ctx, isl_error_invalid,
1051                         "cannot drop dimensions of specified type", goto error);
1052
1053         isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1054         dim = isl_space_cow(dim);
1055         if (!dim)
1056                 goto error;
1057         if (dim->ids) {
1058                 dim = extend_ids(dim);
1059                 if (!dim)
1060                         goto error;
1061                 for (i = 0; i < num; ++i)
1062                         isl_id_free(get_id(dim, type, first + i));
1063                 for (i = first+num; i < n(dim, type); ++i)
1064                         set_id(dim, type, i - num, get_id(dim, type, i));
1065                 switch (type) {
1066                 case isl_dim_param:
1067                         get_ids(dim, isl_dim_in, 0, dim->n_in,
1068                                 dim->ids + offset(dim, isl_dim_in) - num);
1069                 case isl_dim_in:
1070                         get_ids(dim, isl_dim_out, 0, dim->n_out,
1071                                 dim->ids + offset(dim, isl_dim_out) - num);
1072                 default:
1073                         ;
1074                 }
1075                 dim->n_id -= num;
1076         }
1077         switch (type) {
1078         case isl_dim_param:     dim->nparam -= num; break;
1079         case isl_dim_in:        dim->n_in -= num; break;
1080         case isl_dim_out:       dim->n_out -= num; break;
1081         default:                ;
1082         }
1083         dim = isl_space_reset(dim, type);
1084         if (type == isl_dim_param) {
1085                 if (dim && dim->nested[0] &&
1086                     !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1087                                                     isl_dim_param, first, num)))
1088                         goto error;
1089                 if (dim && dim->nested[1] &&
1090                     !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1091                                                     isl_dim_param, first, num)))
1092                         goto error;
1093         }
1094         return dim;
1095 error:
1096         isl_space_free(dim);
1097         return NULL;
1098 }
1099
1100 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1101                 unsigned first, unsigned n)
1102 {
1103         if (!dim)
1104                 return NULL;
1105         return isl_space_drop_dims(dim, isl_dim_in, first, n);
1106 }
1107
1108 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1109                 unsigned first, unsigned n)
1110 {
1111         if (!dim)
1112                 return NULL;
1113         return isl_space_drop_dims(dim, isl_dim_out, first, n);
1114 }
1115
1116 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1117 {
1118         if (!dim)
1119                 return NULL;
1120         dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1121         return isl_space_reverse(dim);
1122 }
1123
1124 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1125 {
1126         return isl_space_reverse(dim);
1127 }
1128
1129 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1130 {
1131         if (!dim)
1132                 return NULL;
1133         return isl_space_drop_inputs(dim, 0, dim->n_in);
1134 }
1135
1136 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1137 {
1138         return dim;
1139 }
1140
1141 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1142 {
1143         space = isl_space_drop_dims(space,
1144                             isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1145         space = isl_space_drop_dims(space,
1146                             isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1147         return space;
1148 }
1149
1150 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1151 {
1152         dim = isl_space_cow(dim);
1153         if (!dim)
1154                 return NULL;
1155
1156         dim->n_out += dim->n_in;
1157         dim->n_in = 0;
1158         dim = isl_space_reset(dim, isl_dim_in);
1159         dim = isl_space_reset(dim, isl_dim_out);
1160
1161         return dim;
1162 }
1163
1164 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1165         unsigned n_div)
1166 {
1167         int i;
1168
1169         if (!dim)
1170                 return NULL;
1171         if (n_div == 0 &&
1172             dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1173                 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1174         dim = isl_space_cow(dim);
1175         if (!dim)
1176                 return NULL;
1177         dim->n_out += dim->nparam + dim->n_in + n_div;
1178         dim->nparam = 0;
1179         dim->n_in = 0;
1180
1181         for (i = 0; i < dim->n_id; ++i)
1182                 isl_id_free(get_id(dim, isl_dim_out, i));
1183         dim->n_id = 0;
1184         dim = isl_space_reset(dim, isl_dim_in);
1185         dim = isl_space_reset(dim, isl_dim_out);
1186
1187         return dim;
1188 }
1189
1190 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1191 {
1192         if (!dim1 || !dim2)
1193                 return -1;
1194         if (dim1 == dim2)
1195                 return 1;
1196         return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1197                isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1198                isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1199 }
1200
1201 int isl_space_compatible(__isl_keep isl_space *dim1,
1202         __isl_keep isl_space *dim2)
1203 {
1204         return dim1->nparam == dim2->nparam &&
1205                dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1206 }
1207
1208 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1209 {
1210         int i;
1211         isl_id *id;
1212
1213         if (!dim)
1214                 return hash;
1215
1216         hash = isl_hash_builtin(hash, dim->nparam);
1217         hash = isl_hash_builtin(hash, dim->n_in);
1218         hash = isl_hash_builtin(hash, dim->n_out);
1219
1220         for (i = 0; i < dim->nparam; ++i) {
1221                 id = get_id(dim, isl_dim_param, i);
1222                 hash = isl_hash_id(hash, id);
1223         }
1224
1225         id = tuple_id(dim, isl_dim_in);
1226         hash = isl_hash_id(hash, id);
1227         id = tuple_id(dim, isl_dim_out);
1228         hash = isl_hash_id(hash, id);
1229
1230         hash = isl_hash_dim(hash, dim->nested[0]);
1231         hash = isl_hash_dim(hash, dim->nested[1]);
1232
1233         return hash;
1234 }
1235
1236 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1237 {
1238         uint32_t hash;
1239
1240         if (!dim)
1241                 return 0;
1242
1243         hash = isl_hash_init();
1244         hash = isl_hash_dim(hash, dim);
1245
1246         return hash;
1247 }
1248
1249 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1250 {
1251         if (!dim)
1252                 return -1;
1253
1254         if (dim->n_in != 0 || dim->tuple_id[0] || dim->nested[0])
1255                 return 0;
1256
1257         return dim->nested[1] != NULL;
1258 }
1259
1260 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1261 {
1262         isl_space *wrap;
1263
1264         if (!dim)
1265                 return NULL;
1266
1267         wrap = isl_space_set_alloc(dim->ctx,
1268                                     dim->nparam, dim->n_in + dim->n_out);
1269
1270         wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1271         wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1272         wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1273
1274         if (!wrap)
1275                 goto error;
1276
1277         wrap->nested[1] = dim;
1278
1279         return wrap;
1280 error:
1281         isl_space_free(dim);
1282         return NULL;
1283 }
1284
1285 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1286 {
1287         isl_space *unwrap;
1288
1289         if (!dim)
1290                 return NULL;
1291
1292         if (!isl_space_is_wrapping(dim))
1293                 isl_die(dim->ctx, isl_error_invalid, "not a wrapping dim",
1294                         goto error);
1295
1296         unwrap = isl_space_copy(dim->nested[1]);
1297         isl_space_free(dim);
1298
1299         return unwrap;
1300 error:
1301         isl_space_free(dim);
1302         return NULL;
1303 }
1304
1305 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1306 {
1307         if (type != isl_dim_in && type != isl_dim_out)
1308                 return 0;
1309         if (!dim)
1310                 return -1;
1311         if (dim->tuple_id[type - isl_dim_in])
1312                 return 1;
1313         if (dim->nested[type - isl_dim_in])
1314                 return 1;
1315         return 0;
1316 }
1317
1318 int isl_space_may_be_set(__isl_keep isl_space *dim)
1319 {
1320         if (!dim)
1321                 return -1;
1322         if (isl_space_dim(dim, isl_dim_in) != 0)
1323                 return 0;
1324         if (isl_space_is_named_or_nested(dim, isl_dim_in))
1325                 return 0;
1326         return 1;
1327 }
1328
1329 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1330         enum isl_dim_type type)
1331 {
1332         if (!isl_space_is_named_or_nested(dim, type))
1333                 return dim;
1334
1335         dim = isl_space_cow(dim);
1336         if (!dim)
1337                 return NULL;
1338
1339         isl_id_free(dim->tuple_id[type - isl_dim_in]);
1340         dim->tuple_id[type - isl_dim_in] = NULL;
1341         isl_space_free(dim->nested[type - isl_dim_in]);
1342         dim->nested[type - isl_dim_in] = NULL;
1343
1344         return dim;
1345 }
1346
1347 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1348 {
1349         if (!dim)
1350                 return NULL;
1351         if (!dim->nested[0] && !dim->nested[1])
1352                 return dim;
1353
1354         if (dim->nested[0])
1355                 dim = isl_space_reset(dim, isl_dim_in);
1356         if (dim && dim->nested[1])
1357                 dim = isl_space_reset(dim, isl_dim_out);
1358
1359         return dim;
1360 }
1361
1362 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1363 {
1364         if (!dim)
1365                 return NULL;
1366         if (!dim->nested[0])
1367                 return dim;
1368
1369         return isl_space_reset(dim, isl_dim_in);
1370 }
1371
1372 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1373 {
1374         if (!dim)
1375                 return NULL;
1376         if (!dim->nested[1])
1377                 return dim;
1378
1379         return isl_space_reset(dim, isl_dim_out);
1380 }
1381
1382 /* Replace the dimensions of the given type of dst by those of src.
1383  */
1384 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1385         enum isl_dim_type type, __isl_keep isl_space *src)
1386 {
1387         dst = isl_space_cow(dst);
1388
1389         if (!dst || !src)
1390                 goto error;
1391
1392         dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1393         dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1394         dst = copy_ids(dst, type, 0, src, type);
1395
1396         if (dst && type == isl_dim_param) {
1397                 int i;
1398                 for (i = 0; i <= 1; ++i) {
1399                         if (!dst->nested[i])
1400                                 continue;
1401                         dst->nested[i] = isl_space_replace(dst->nested[i],
1402                                                          type, src);
1403                         if (!dst->nested[i])
1404                                 goto error;
1405                 }
1406         }
1407
1408         return dst;
1409 error:
1410         isl_space_free(dst);
1411         return NULL;
1412 }
1413
1414 /* Given a dimension specification "dim" of a set, create a dimension
1415  * specification for the lift of the set.  In particular, the result
1416  * is of the form [dim -> local[..]], with n_local variables in the
1417  * range of the wrapped map.
1418  */
1419 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1420 {
1421         isl_space *local_dim;
1422
1423         if (!dim)
1424                 return NULL;
1425
1426         local_dim = isl_space_dup(dim);
1427         local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1428         local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1429         local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1430         dim = isl_space_join(isl_space_from_domain(dim),
1431                             isl_space_from_range(local_dim));
1432         dim = isl_space_wrap(dim);
1433         dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1434
1435         return dim;
1436 }
1437
1438 int isl_space_can_zip(__isl_keep isl_space *dim)
1439 {
1440         if (!dim)
1441                 return -1;
1442
1443         return dim->nested[0] && dim->nested[1];
1444 }
1445
1446 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1447 {
1448         isl_space *dom, *ran;
1449         isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1450
1451         if (!isl_space_can_zip(dim))
1452                 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1453                         goto error);
1454
1455         if (!dim)
1456                 return 0;
1457         dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1458         ran = isl_space_unwrap(isl_space_range(dim));
1459         dom_dom = isl_space_domain(isl_space_copy(dom));
1460         dom_ran = isl_space_range(dom);
1461         ran_dom = isl_space_domain(isl_space_copy(ran));
1462         ran_ran = isl_space_range(ran);
1463         dom = isl_space_join(isl_space_from_domain(dom_dom),
1464                            isl_space_from_range(ran_dom));
1465         ran = isl_space_join(isl_space_from_domain(dom_ran),
1466                            isl_space_from_range(ran_ran));
1467         return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1468                             isl_space_from_range(isl_space_wrap(ran)));
1469 error:
1470         isl_space_free(dim);
1471         return NULL;
1472 }
1473
1474 int isl_space_has_named_params(__isl_keep isl_space *dim)
1475 {
1476         int i;
1477         unsigned off;
1478
1479         if (!dim)
1480                 return -1;
1481         if (dim->nparam == 0)
1482                 return 1;
1483         off = isl_space_offset(dim, isl_dim_param);
1484         if (off + dim->nparam > dim->n_id)
1485                 return 0;
1486         for (i = 0; i < dim->nparam; ++i)
1487                 if (!dim->ids[off + i])
1488                         return 0;
1489         return 1;
1490 }
1491
1492 /* Align the initial parameters of dim1 to match the order in dim2.
1493  */
1494 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1495         __isl_take isl_space *dim2)
1496 {
1497         isl_reordering *exp;
1498
1499         if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1500                 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1501                         "parameter alignment requires named parameters",
1502                         goto error);
1503
1504         dim2 = isl_space_params(dim2);
1505         exp = isl_parameter_alignment_reordering(dim1, dim2);
1506         exp = isl_reordering_extend_space(exp, dim1);
1507         isl_space_free(dim2);
1508         if (!exp)
1509                 return NULL;
1510         dim1 = isl_space_copy(exp->dim);
1511         isl_reordering_free(exp);
1512         return dim1;
1513 error:
1514         isl_space_free(dim1);
1515         isl_space_free(dim2);
1516         return NULL;
1517 }