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