a7d7d53a48dcd85cad157485b285c1404a2a9ea7
[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 (n(dim1, dim1_type) != n(dim2, dim2_type))
507                 return 0;
508         id1 = tuple_id(dim1, dim1_type);
509         id2 = tuple_id(dim2, dim2_type);
510         if (!id1 ^ !id2)
511                 return 0;
512         if (id1 && id1 != id2)
513                 return 0;
514         nested1 = nested(dim1, dim1_type);
515         nested2 = nested(dim2, dim2_type);
516         if (!nested1 ^ !nested2)
517                 return 0;
518         if (nested1 && !isl_space_is_equal(nested1, nested2))
519                 return 0;
520         return 1;
521 }
522
523 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
524         __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
525 {
526         int i;
527
528         if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
529                 return 0;
530
531         if (!dim1->ids && !dim2->ids)
532                 return 1;
533
534         for (i = 0; i < n(dim1, dim1_type); ++i) {
535                 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
536                         return 0;
537         }
538         return 1;
539 }
540
541 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
542         __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
543 {
544         return match(dim1, dim1_type, dim2, dim2_type);
545 }
546
547 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
548         unsigned first, unsigned n, __isl_keep isl_id **ids)
549 {
550         int i;
551
552         for (i = 0; i < n ; ++i)
553                 ids[i] = get_id(dim, type, first + i);
554 }
555
556 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
557                         unsigned nparam, unsigned n_in, unsigned n_out)
558 {
559         isl_id **ids = NULL;
560
561         if (!dim)
562                 return NULL;
563         if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
564                 return dim;
565
566         isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
567         isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
568         isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
569
570         dim = isl_space_cow(dim);
571
572         if (dim->ids) {
573                 ids = isl_calloc_array(dim->ctx, isl_id *,
574                                          nparam + n_in + n_out);
575                 if (!ids)
576                         goto error;
577                 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
578                 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
579                 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
580                 free(dim->ids);
581                 dim->ids = ids;
582                 dim->n_id = nparam + n_in + n_out;
583         }
584         dim->nparam = nparam;
585         dim->n_in = n_in;
586         dim->n_out = n_out;
587
588         return dim;
589 error:
590         free(ids);
591         isl_space_free(dim);
592         return NULL;
593 }
594
595 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
596         enum isl_dim_type type, unsigned n)
597 {
598         if (!dim)
599                 return NULL;
600         dim = isl_space_reset(dim, type);
601         switch (type) {
602         case isl_dim_param:
603                 dim = isl_space_extend(dim,
604                                         dim->nparam + n, dim->n_in, dim->n_out);
605                 if (dim && dim->nested[0] &&
606                     !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
607                                                     isl_dim_param, n)))
608                         goto error;
609                 if (dim && dim->nested[1] &&
610                     !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
611                                                     isl_dim_param, n)))
612                         goto error;
613                 return dim;
614         case isl_dim_in:
615                 return isl_space_extend(dim,
616                                         dim->nparam, dim->n_in + n, dim->n_out);
617         case isl_dim_out:
618                 return isl_space_extend(dim,
619                                         dim->nparam, dim->n_in, dim->n_out + n);
620         default:
621                 isl_die(dim->ctx, isl_error_invalid,
622                         "cannot add dimensions of specified type", goto error);
623         }
624 error:
625         isl_space_free(dim);
626         return NULL;
627 }
628
629 static int valid_dim_type(enum isl_dim_type type)
630 {
631         switch (type) {
632         case isl_dim_param:
633         case isl_dim_in:
634         case isl_dim_out:
635                 return 1;
636         default:
637                 return 0;
638         }
639 }
640
641 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
642         enum isl_dim_type type, unsigned pos, unsigned n)
643 {
644         isl_id **ids = NULL;
645
646         if (!dim)
647                 return NULL;
648         if (n == 0)
649                 return isl_space_reset(dim, type);
650
651         if (!valid_dim_type(type))
652                 isl_die(dim->ctx, isl_error_invalid,
653                         "cannot insert dimensions of specified type",
654                         goto error);
655
656         isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
657
658         dim = isl_space_cow(dim);
659         if (!dim)
660                 return NULL;
661
662         if (dim->ids) {
663                 enum isl_dim_type t;
664                 int off;
665                 int s[3];
666                 int *size = s - isl_dim_param;
667                 ids = isl_calloc_array(dim->ctx, isl_id *,
668                                      dim->nparam + dim->n_in + dim->n_out + n);
669                 if (!ids)
670                         goto error;
671                 off = 0;
672                 size[isl_dim_param] = dim->nparam;
673                 size[isl_dim_in] = dim->n_in;
674                 size[isl_dim_out] = dim->n_out;
675                 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
676                         if (t != type) {
677                                 get_ids(dim, t, 0, size[t], ids + off);
678                                 off += size[t];
679                         } else {
680                                 get_ids(dim, t, 0, pos, ids + off);
681                                 off += pos + n;
682                                 get_ids(dim, t, pos, size[t] - pos, ids + off);
683                                 off += size[t] - pos;
684                         }
685                 }
686                 free(dim->ids);
687                 dim->ids = ids;
688                 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
689         }
690         switch (type) {
691         case isl_dim_param:     dim->nparam += n; break;
692         case isl_dim_in:        dim->n_in += n; break;
693         case isl_dim_out:       dim->n_out += n; break;
694         default:                ;
695         }
696         dim = isl_space_reset(dim, type);
697
698         return dim;
699 error:
700         isl_space_free(dim);
701         return NULL;
702 }
703
704 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
705         enum isl_dim_type dst_type, unsigned dst_pos,
706         enum isl_dim_type src_type, unsigned src_pos, unsigned n)
707 {
708         int i;
709
710         if (!dim)
711                 return NULL;
712         if (n == 0)
713                 return dim;
714
715         isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
716                 goto error);
717
718         if (dst_type == src_type && dst_pos == src_pos)
719                 return dim;
720
721         isl_assert(dim->ctx, dst_type != src_type, goto error);
722
723         dim = isl_space_reset(dim, src_type);
724         dim = isl_space_reset(dim, dst_type);
725
726         dim = isl_space_cow(dim);
727         if (!dim)
728                 return NULL;
729
730         if (dim->ids) {
731                 isl_id **ids;
732                 enum isl_dim_type t;
733                 int off;
734                 int s[3];
735                 int *size = s - isl_dim_param;
736                 ids = isl_calloc_array(dim->ctx, isl_id *,
737                                          dim->nparam + dim->n_in + dim->n_out);
738                 if (!ids)
739                         goto error;
740                 off = 0;
741                 size[isl_dim_param] = dim->nparam;
742                 size[isl_dim_in] = dim->n_in;
743                 size[isl_dim_out] = dim->n_out;
744                 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
745                         if (t == dst_type) {
746                                 get_ids(dim, t, 0, dst_pos, ids + off);
747                                 off += dst_pos;
748                                 get_ids(dim, src_type, src_pos, n, ids + off);
749                                 off += n;
750                                 get_ids(dim, t, dst_pos, size[t] - dst_pos,
751                                                 ids + off);
752                                 off += size[t] - dst_pos;
753                         } else if (t == src_type) {
754                                 get_ids(dim, t, 0, src_pos, ids + off);
755                                 off += src_pos;
756                                 get_ids(dim, t, src_pos + n,
757                                             size[t] - src_pos - n, ids + off);
758                                 off += size[t] - src_pos - n;
759                         } else {
760                                 get_ids(dim, t, 0, size[t], ids + off);
761                                 off += size[t];
762                         }
763                 }
764                 free(dim->ids);
765                 dim->ids = ids;
766                 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
767         }
768
769         switch (dst_type) {
770         case isl_dim_param:     dim->nparam += n; break;
771         case isl_dim_in:        dim->n_in += n; break;
772         case isl_dim_out:       dim->n_out += n; break;
773         default:                ;
774         }
775
776         switch (src_type) {
777         case isl_dim_param:     dim->nparam -= n; break;
778         case isl_dim_in:        dim->n_in -= n; break;
779         case isl_dim_out:       dim->n_out -= n; break;
780         default:                ;
781         }
782
783         if (dst_type != isl_dim_param && src_type != isl_dim_param)
784                 return dim;
785
786         for (i = 0; i < 2; ++i) {
787                 if (!dim->nested[i])
788                         continue;
789                 dim->nested[i] = isl_space_replace(dim->nested[i],
790                                                  isl_dim_param, dim);
791                 if (!dim->nested[i])
792                         goto error;
793         }
794
795         return dim;
796 error:
797         isl_space_free(dim);
798         return NULL;
799 }
800
801 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
802         __isl_take isl_space *right)
803 {
804         isl_space *dim;
805
806         if (!left || !right)
807                 goto error;
808
809         isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
810                         goto error);
811         isl_assert(left->ctx,
812                 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
813                 goto error);
814
815         dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
816         if (!dim)
817                 goto error;
818
819         dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
820         dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
821         dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
822
823         if (dim && left->tuple_id[0] &&
824             !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
825                 goto error;
826         if (dim && right->tuple_id[1] &&
827             !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
828                 goto error;
829         if (dim && left->nested[0] &&
830             !(dim->nested[0] = isl_space_copy(left->nested[0])))
831                 goto error;
832         if (dim && right->nested[1] &&
833             !(dim->nested[1] = isl_space_copy(right->nested[1])))
834                 goto error;
835
836         isl_space_free(left);
837         isl_space_free(right);
838
839         return dim;
840 error:
841         isl_space_free(left);
842         isl_space_free(right);
843         return NULL;
844 }
845
846 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
847         __isl_take isl_space *right)
848 {
849         isl_space *dom1, *dom2, *nest1, *nest2;
850
851         if (!left || !right)
852                 goto error;
853
854         isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
855                         goto error);
856
857         dom1 = isl_space_domain(isl_space_copy(left));
858         dom2 = isl_space_domain(isl_space_copy(right));
859         nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
860
861         dom1 = isl_space_range(left);
862         dom2 = isl_space_range(right);
863         nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
864
865         return isl_space_join(isl_space_reverse(nest1), nest2);
866 error:
867         isl_space_free(left);
868         isl_space_free(right);
869         return NULL;
870 }
871
872 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
873         __isl_take isl_space *right)
874 {
875         isl_space *dom, *ran1, *ran2, *nest;
876
877         if (!left || !right)
878                 goto error;
879
880         isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
881                         goto error);
882         if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
883                 isl_die(left->ctx, isl_error_invalid,
884                         "domains need to match", goto error);
885
886         dom = isl_space_domain(isl_space_copy(left));
887
888         ran1 = isl_space_range(left);
889         ran2 = isl_space_range(right);
890         nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
891
892         return isl_space_join(isl_space_reverse(dom), nest);
893 error:
894         isl_space_free(left);
895         isl_space_free(right);
896         return NULL;
897 }
898
899 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
900 {
901         isl_id **ids = NULL;
902
903         if (!dim)
904                 return NULL;
905         isl_assert(dim->ctx, dim->n_in == 0, goto error);
906         if (dim->n_out == 0 && !isl_space_is_named_or_nested(dim, isl_dim_out))
907                 return dim;
908         dim = isl_space_cow(dim);
909         if (!dim)
910                 return NULL;
911         if (dim->ids) {
912                 ids = isl_calloc_array(dim->ctx, isl_id *,
913                                         dim->nparam + dim->n_out + dim->n_out);
914                 if (!ids)
915                         goto error;
916                 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
917                 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
918         }
919         dim->n_in = dim->n_out;
920         if (ids) {
921                 free(dim->ids);
922                 dim->ids = ids;
923                 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
924                 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
925         }
926         isl_id_free(dim->tuple_id[0]);
927         dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
928         isl_space_free(dim->nested[0]);
929         dim->nested[0] = isl_space_copy(dim->nested[1]);
930         return dim;
931 error:
932         isl_space_free(dim);
933         return NULL;
934 }
935
936 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
937         enum isl_dim_type type,
938         unsigned first, unsigned n, __isl_take isl_id **ids)
939 {
940         int i;
941
942         for (i = 0; i < n ; ++i)
943                 dim = set_id(dim, type, first + i, ids[i]);
944
945         return dim;
946 }
947
948 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
949 {
950         unsigned t;
951         isl_space *nested;
952         isl_id **ids = NULL;
953         isl_id *id;
954
955         if (!dim)
956                 return NULL;
957         if (match(dim, isl_dim_in, dim, isl_dim_out))
958                 return dim;
959
960         dim = isl_space_cow(dim);
961         if (!dim)
962                 return NULL;
963
964         id = dim->tuple_id[0];
965         dim->tuple_id[0] = dim->tuple_id[1];
966         dim->tuple_id[1] = id;
967
968         nested = dim->nested[0];
969         dim->nested[0] = dim->nested[1];
970         dim->nested[1] = nested;
971
972         if (dim->ids) {
973                 ids = isl_alloc_array(dim->ctx, isl_id *,
974                                         dim->n_in + dim->n_out);
975                 if (!ids)
976                         goto error;
977                 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
978                 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
979         }
980
981         t = dim->n_in;
982         dim->n_in = dim->n_out;
983         dim->n_out = t;
984
985         if (dim->ids) {
986                 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
987                 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
988                 free(ids);
989         }
990
991         return dim;
992 error:
993         free(ids);
994         isl_space_free(dim);
995         return NULL;
996 }
997
998 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
999         enum isl_dim_type type, unsigned first, unsigned num)
1000 {
1001         int i;
1002
1003         if (!dim)
1004                 return NULL;
1005
1006         if (num == 0)
1007                 return isl_space_reset(dim, type);
1008
1009         if (!valid_dim_type(type))
1010                 isl_die(dim->ctx, isl_error_invalid,
1011                         "cannot drop dimensions of specified type", goto error);
1012
1013         isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1014         dim = isl_space_cow(dim);
1015         if (!dim)
1016                 goto error;
1017         if (dim->ids) {
1018                 dim = extend_ids(dim);
1019                 if (!dim)
1020                         goto error;
1021                 for (i = 0; i < num; ++i)
1022                         isl_id_free(get_id(dim, type, first + i));
1023                 for (i = first+num; i < n(dim, type); ++i)
1024                         set_id(dim, type, i - num, get_id(dim, type, i));
1025                 switch (type) {
1026                 case isl_dim_param:
1027                         get_ids(dim, isl_dim_in, 0, dim->n_in,
1028                                 dim->ids + offset(dim, isl_dim_in) - num);
1029                 case isl_dim_in:
1030                         get_ids(dim, isl_dim_out, 0, dim->n_out,
1031                                 dim->ids + offset(dim, isl_dim_out) - num);
1032                 default:
1033                         ;
1034                 }
1035                 dim->n_id -= num;
1036         }
1037         switch (type) {
1038         case isl_dim_param:     dim->nparam -= num; break;
1039         case isl_dim_in:        dim->n_in -= num; break;
1040         case isl_dim_out:       dim->n_out -= num; break;
1041         default:                ;
1042         }
1043         dim = isl_space_reset(dim, type);
1044         if (type == isl_dim_param) {
1045                 if (dim && dim->nested[0] &&
1046                     !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1047                                                     isl_dim_param, first, num)))
1048                         goto error;
1049                 if (dim && dim->nested[1] &&
1050                     !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1051                                                     isl_dim_param, first, num)))
1052                         goto error;
1053         }
1054         return dim;
1055 error:
1056         isl_space_free(dim);
1057         return NULL;
1058 }
1059
1060 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1061                 unsigned first, unsigned n)
1062 {
1063         if (!dim)
1064                 return NULL;
1065         return isl_space_drop_dims(dim, isl_dim_in, first, n);
1066 }
1067
1068 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1069                 unsigned first, unsigned n)
1070 {
1071         if (!dim)
1072                 return NULL;
1073         return isl_space_drop_dims(dim, isl_dim_out, first, n);
1074 }
1075
1076 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1077 {
1078         if (!dim)
1079                 return NULL;
1080         dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1081         return isl_space_reverse(dim);
1082 }
1083
1084 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1085 {
1086         return isl_space_reverse(dim);
1087 }
1088
1089 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1090 {
1091         if (!dim)
1092                 return NULL;
1093         return isl_space_drop_inputs(dim, 0, dim->n_in);
1094 }
1095
1096 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1097 {
1098         return dim;
1099 }
1100
1101 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1102 {
1103         dim = isl_space_cow(dim);
1104         if (!dim)
1105                 return NULL;
1106
1107         dim->n_out += dim->n_in;
1108         dim->n_in = 0;
1109         dim = isl_space_reset(dim, isl_dim_in);
1110         dim = isl_space_reset(dim, isl_dim_out);
1111
1112         return dim;
1113 }
1114
1115 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1116         unsigned n_div)
1117 {
1118         int i;
1119
1120         if (!dim)
1121                 return NULL;
1122         if (n_div == 0 &&
1123             dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1124                 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1125         dim = isl_space_cow(dim);
1126         if (!dim)
1127                 return NULL;
1128         dim->n_out += dim->nparam + dim->n_in + n_div;
1129         dim->nparam = 0;
1130         dim->n_in = 0;
1131
1132         for (i = 0; i < dim->n_id; ++i)
1133                 isl_id_free(get_id(dim, isl_dim_out, i));
1134         dim->n_id = 0;
1135         dim = isl_space_reset(dim, isl_dim_in);
1136         dim = isl_space_reset(dim, isl_dim_out);
1137
1138         return dim;
1139 }
1140
1141 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1142 {
1143         if (!dim1 || !dim2)
1144                 return -1;
1145         return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1146                isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1147                isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1148 }
1149
1150 int isl_space_compatible(__isl_keep isl_space *dim1,
1151         __isl_keep isl_space *dim2)
1152 {
1153         return dim1->nparam == dim2->nparam &&
1154                dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1155 }
1156
1157 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1158 {
1159         int i;
1160         isl_id *id;
1161
1162         if (!dim)
1163                 return hash;
1164
1165         hash = isl_hash_builtin(hash, dim->nparam);
1166         hash = isl_hash_builtin(hash, dim->n_in);
1167         hash = isl_hash_builtin(hash, dim->n_out);
1168
1169         for (i = 0; i < dim->nparam; ++i) {
1170                 id = get_id(dim, isl_dim_param, i);
1171                 hash = isl_hash_id(hash, id);
1172         }
1173
1174         id = tuple_id(dim, isl_dim_in);
1175         hash = isl_hash_id(hash, id);
1176         id = tuple_id(dim, isl_dim_out);
1177         hash = isl_hash_id(hash, id);
1178
1179         hash = isl_hash_dim(hash, dim->nested[0]);
1180         hash = isl_hash_dim(hash, dim->nested[1]);
1181
1182         return hash;
1183 }
1184
1185 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1186 {
1187         uint32_t hash;
1188
1189         if (!dim)
1190                 return 0;
1191
1192         hash = isl_hash_init();
1193         hash = isl_hash_dim(hash, dim);
1194
1195         return hash;
1196 }
1197
1198 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1199 {
1200         if (!dim)
1201                 return -1;
1202
1203         if (dim->n_in != 0 || dim->tuple_id[0] || dim->nested[0])
1204                 return 0;
1205
1206         return dim->nested[1] != NULL;
1207 }
1208
1209 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1210 {
1211         isl_space *wrap;
1212
1213         if (!dim)
1214                 return NULL;
1215
1216         wrap = isl_space_alloc(dim->ctx, dim->nparam, 0, dim->n_in + dim->n_out);
1217
1218         wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1219         wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1220         wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1221
1222         if (!wrap)
1223                 goto error;
1224
1225         wrap->nested[1] = dim;
1226
1227         return wrap;
1228 error:
1229         isl_space_free(dim);
1230         return NULL;
1231 }
1232
1233 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1234 {
1235         isl_space *unwrap;
1236
1237         if (!dim)
1238                 return NULL;
1239
1240         if (!isl_space_is_wrapping(dim))
1241                 isl_die(dim->ctx, isl_error_invalid, "not a wrapping dim",
1242                         goto error);
1243
1244         unwrap = isl_space_copy(dim->nested[1]);
1245         isl_space_free(dim);
1246
1247         return unwrap;
1248 error:
1249         isl_space_free(dim);
1250         return NULL;
1251 }
1252
1253 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1254 {
1255         if (type != isl_dim_in && type != isl_dim_out)
1256                 return 0;
1257         if (!dim)
1258                 return -1;
1259         if (dim->tuple_id[type - isl_dim_in])
1260                 return 1;
1261         if (dim->nested[type - isl_dim_in])
1262                 return 1;
1263         return 0;
1264 }
1265
1266 int isl_space_may_be_set(__isl_keep isl_space *dim)
1267 {
1268         if (!dim)
1269                 return -1;
1270         if (isl_space_dim(dim, isl_dim_in) != 0)
1271                 return 0;
1272         if (isl_space_is_named_or_nested(dim, isl_dim_in))
1273                 return 0;
1274         return 1;
1275 }
1276
1277 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1278         enum isl_dim_type type)
1279 {
1280         if (!isl_space_is_named_or_nested(dim, type))
1281                 return dim;
1282
1283         dim = isl_space_cow(dim);
1284         if (!dim)
1285                 return NULL;
1286
1287         isl_id_free(dim->tuple_id[type - isl_dim_in]);
1288         dim->tuple_id[type - isl_dim_in] = NULL;
1289         isl_space_free(dim->nested[type - isl_dim_in]);
1290         dim->nested[type - isl_dim_in] = NULL;
1291
1292         return dim;
1293 }
1294
1295 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1296 {
1297         if (!dim)
1298                 return NULL;
1299         if (!dim->nested[0] && !dim->nested[1])
1300                 return dim;
1301
1302         if (dim->nested[0])
1303                 dim = isl_space_reset(dim, isl_dim_in);
1304         if (dim && dim->nested[1])
1305                 dim = isl_space_reset(dim, isl_dim_out);
1306
1307         return dim;
1308 }
1309
1310 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1311 {
1312         if (!dim)
1313                 return NULL;
1314         if (!dim->nested[1])
1315                 return dim;
1316
1317         return isl_space_reset(dim, isl_dim_out);
1318 }
1319
1320 /* Replace the dimensions of the given type of dst by those of src.
1321  */
1322 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1323         enum isl_dim_type type, __isl_keep isl_space *src)
1324 {
1325         dst = isl_space_cow(dst);
1326
1327         if (!dst || !src)
1328                 goto error;
1329
1330         dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1331         dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1332         dst = copy_ids(dst, type, 0, src, type);
1333
1334         if (dst && type == isl_dim_param) {
1335                 int i;
1336                 for (i = 0; i <= 1; ++i) {
1337                         if (!dst->nested[i])
1338                                 continue;
1339                         dst->nested[i] = isl_space_replace(dst->nested[i],
1340                                                          type, src);
1341                         if (!dst->nested[i])
1342                                 goto error;
1343                 }
1344         }
1345
1346         return dst;
1347 error:
1348         isl_space_free(dst);
1349         return NULL;
1350 }
1351
1352 /* Given a dimension specification "dim" of a set, create a dimension
1353  * specification for the lift of the set.  In particular, the result
1354  * is of the form [dim -> local[..]], with n_local variables in the
1355  * range of the wrapped map.
1356  */
1357 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1358 {
1359         isl_space *local_dim;
1360
1361         if (!dim)
1362                 return NULL;
1363
1364         local_dim = isl_space_dup(dim);
1365         local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1366         local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1367         local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1368         dim = isl_space_join(isl_space_from_domain(dim),
1369                             isl_space_from_range(local_dim));
1370         dim = isl_space_wrap(dim);
1371         dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1372
1373         return dim;
1374 }
1375
1376 int isl_space_can_zip(__isl_keep isl_space *dim)
1377 {
1378         if (!dim)
1379                 return -1;
1380
1381         return dim->nested[0] && dim->nested[1];
1382 }
1383
1384 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1385 {
1386         isl_space *dom, *ran;
1387         isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1388
1389         if (!isl_space_can_zip(dim))
1390                 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1391                         goto error);
1392
1393         if (!dim)
1394                 return 0;
1395         dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1396         ran = isl_space_unwrap(isl_space_range(dim));
1397         dom_dom = isl_space_domain(isl_space_copy(dom));
1398         dom_ran = isl_space_range(dom);
1399         ran_dom = isl_space_domain(isl_space_copy(ran));
1400         ran_ran = isl_space_range(ran);
1401         dom = isl_space_join(isl_space_from_domain(dom_dom),
1402                            isl_space_from_range(ran_dom));
1403         ran = isl_space_join(isl_space_from_domain(dom_ran),
1404                            isl_space_from_range(ran_ran));
1405         return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1406                             isl_space_from_range(isl_space_wrap(ran)));
1407 error:
1408         isl_space_free(dim);
1409         return NULL;
1410 }
1411
1412 int isl_space_has_named_params(__isl_keep isl_space *dim)
1413 {
1414         int i;
1415         unsigned off;
1416
1417         if (!dim)
1418                 return -1;
1419         if (dim->nparam == 0)
1420                 return 1;
1421         off = isl_space_offset(dim, isl_dim_param);
1422         if (off + dim->nparam > dim->n_id)
1423                 return 0;
1424         for (i = 0; i < dim->nparam; ++i)
1425                 if (!dim->ids[off + i])
1426                         return 0;
1427         return 1;
1428 }
1429
1430 /* Align the initial parameters of dim1 to match the order in dim2.
1431  */
1432 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1433         __isl_take isl_space *dim2)
1434 {
1435         isl_reordering *exp;
1436
1437         if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1438                 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1439                         "parameter alignment requires named parameters",
1440                         goto error);
1441
1442         exp = isl_parameter_alignment_reordering(dim1, dim2);
1443         isl_space_free(dim1);
1444         isl_space_free(dim2);
1445         if (!exp)
1446                 return NULL;
1447         dim1 = isl_space_copy(exp->dim);
1448         isl_reordering_free(exp);
1449         return dim1;
1450 error:
1451         isl_space_free(dim1);
1452         isl_space_free(dim2);
1453         return NULL;
1454 }