Add isl_restriction_get_ctx
[isl.git] / isl_space.c
blob48b099b11210ad5e8e2cbf4ef06b642671f46e3c
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
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
13 #include <stdlib.h>
14 #include <isl_space_private.h>
15 #include <isl_id_private.h>
16 #include <isl_reordering.h>
18 isl_ctx *isl_space_get_ctx(__isl_keep isl_space *dim)
20 return dim ? dim->ctx : NULL;
23 __isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
24 unsigned nparam, unsigned n_in, unsigned n_out)
26 isl_space *dim;
28 dim = isl_alloc_type(ctx, struct isl_space);
29 if (!dim)
30 return NULL;
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;
39 dim->tuple_id[0] = NULL;
40 dim->tuple_id[1] = NULL;
42 dim->nested[0] = NULL;
43 dim->nested[1] = NULL;
45 dim->n_id = 0;
46 dim->ids = NULL;
48 return dim;
51 /* Mark the space as being that of a set, by setting the domain tuple
52 * to isl_id_none.
54 static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
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;
63 /* Is the space that of a set?
65 int isl_space_is_set(__isl_keep isl_space *space)
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;
76 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
77 unsigned nparam, unsigned dim)
79 isl_space *space;
80 space = isl_space_alloc(ctx, nparam, 0, dim);
81 space = mark_as_set(space);
82 return space;
85 /* Mark the space as being that of a parameter domain, by setting
86 * both tuples to isl_id_none.
88 static __isl_give isl_space *mark_as_params(isl_space *space)
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;
97 /* Is the space that of a parameter domain?
99 int isl_space_is_params(__isl_keep isl_space *space)
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;
113 /* Create a space for a parameter domain.
115 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
117 isl_space *space;
118 space = isl_space_alloc(ctx, nparam, 0, 0);
119 space = mark_as_params(space);
120 return space;
123 static unsigned global_pos(__isl_keep isl_space *dim,
124 enum isl_dim_type type, unsigned pos)
126 struct isl_ctx *ctx = dim->ctx;
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));
144 return isl_space_dim(dim, isl_dim_all);
147 /* Extend length of ids array to the total number of dimensions.
149 static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
151 isl_id **ids;
152 int i;
154 if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
155 return dim;
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;
172 dim->n_id = isl_space_dim(dim, isl_dim_all);
174 return dim;
175 error:
176 isl_space_free(dim);
177 return NULL;
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)
183 dim = isl_space_cow(dim);
185 if (!dim)
186 goto error;
188 pos = global_pos(dim, type, pos);
189 if (pos == isl_space_dim(dim, isl_dim_all))
190 goto error;
192 if (pos >= dim->n_id) {
193 if (!id)
194 return dim;
195 dim = extend_ids(dim);
196 if (!dim)
197 goto error;
200 dim->ids[pos] = id;
202 return dim;
203 error:
204 isl_id_free(id);
205 isl_space_free(dim);
206 return NULL;
209 static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
210 enum isl_dim_type type, unsigned pos)
212 if (!dim)
213 return NULL;
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];
223 static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
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;
233 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
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;
244 unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
246 if (!dim)
247 return 0;
248 return n(dim, type);
251 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
253 if (!dim)
254 return 0;
255 return offset(dim, type);
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)
262 int i;
263 isl_id *id;
265 if (!dst)
266 return NULL;
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;
276 return dst;
279 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
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;
306 __isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
308 if (!dim)
309 return NULL;
311 if (dim->ref == 1)
312 return dim;
313 dim->ref--;
314 return isl_space_dup(dim);
317 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
319 if (!dim)
320 return NULL;
322 dim->ref++;
323 return dim;
326 void isl_space_free(__isl_take isl_space *dim)
328 int i;
330 if (!dim)
331 return;
333 if (--dim->ref > 0)
334 return;
336 isl_id_free(dim->tuple_id[0]);
337 isl_id_free(dim->tuple_id[1]);
339 isl_space_free(dim->nested[0]);
340 isl_space_free(dim->nested[1]);
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);
347 free(dim);
350 /* Check if "s" is a valid dimension or tuple name.
351 * We currently only forbid names that look like a number.
353 * s is assumed to be non-NULL.
355 static int name_ok(isl_ctx *ctx, const char *s)
357 char *p;
358 long dummy;
360 dummy = strtol(s, &p, 0);
361 if (p != s)
362 isl_die(ctx, isl_error_invalid, "name looks like a number",
363 return 0);
365 return 1;
368 /* Is it possible for the given dimension type to have a tuple id?
370 static int space_can_have_id(__isl_keep isl_space *space,
371 enum isl_dim_type type)
373 if (!space)
374 return 0;
375 if (isl_space_is_params(space))
376 isl_die(space->ctx, isl_error_invalid,
377 "parameter spaces don't have tuple ids", return 0);
378 if (isl_space_is_set(space) && type != isl_dim_set)
379 isl_die(space->ctx, isl_error_invalid,
380 "set spaces can only have a set id", return 0);
381 if (type != isl_dim_in && type != isl_dim_out)
382 isl_die(space->ctx, isl_error_invalid,
383 "only input, output and set tuples can have ids",
384 return 0);
386 return 1;
389 /* Does the tuple have an id?
391 int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
393 if (!space_can_have_id(dim, type))
394 return -1;
395 return dim->tuple_id[type - isl_dim_in] != NULL;
398 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
399 enum isl_dim_type type)
401 int has_id;
403 if (!dim)
404 return NULL;
405 has_id = isl_space_has_tuple_id(dim, type);
406 if (has_id < 0)
407 return NULL;
408 if (!has_id)
409 isl_die(dim->ctx, isl_error_invalid,
410 "tuple has no id", return NULL);
411 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
414 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
415 enum isl_dim_type type, __isl_take isl_id *id)
417 dim = isl_space_cow(dim);
418 if (!dim || !id)
419 goto error;
420 if (type != isl_dim_in && type != isl_dim_out)
421 isl_die(dim->ctx, isl_error_invalid,
422 "only input, output and set tuples can have names",
423 goto error);
425 isl_id_free(dim->tuple_id[type - isl_dim_in]);
426 dim->tuple_id[type - isl_dim_in] = id;
428 return dim;
429 error:
430 isl_id_free(id);
431 isl_space_free(dim);
432 return NULL;
435 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
436 enum isl_dim_type type)
438 dim = isl_space_cow(dim);
439 if (!dim)
440 return NULL;
441 if (type != isl_dim_in && type != isl_dim_out)
442 isl_die(dim->ctx, isl_error_invalid,
443 "only input, output and set tuples can have names",
444 goto error);
446 isl_id_free(dim->tuple_id[type - isl_dim_in]);
447 dim->tuple_id[type - isl_dim_in] = NULL;
449 return dim;
450 error:
451 isl_space_free(dim);
452 return NULL;
455 /* Set the id of the given dimension of "space" to "id".
456 * If the dimension already has an id, then it is replaced.
457 * If the dimension is a parameter, then we need to change it
458 * in the nested spaces (if any) as well.
460 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
461 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
463 space = isl_space_cow(space);
464 if (!space || !id)
465 goto error;
467 if (type == isl_dim_param) {
468 int i;
470 for (i = 0; i < 2; ++i) {
471 if (!space->nested[i])
472 continue;
473 space->nested[i] =
474 isl_space_set_dim_id(space->nested[i],
475 type, pos, isl_id_copy(id));
476 if (!space->nested[i])
477 goto error;
481 isl_id_free(get_id(space, type, pos));
482 return set_id(space, type, pos, id);
483 error:
484 isl_id_free(id);
485 isl_space_free(space);
486 return NULL;
489 /* Reset the id of the given dimension of "space".
490 * If the dimension already has an id, then it is removed.
491 * If the dimension is a parameter, then we need to reset it
492 * in the nested spaces (if any) as well.
494 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
495 enum isl_dim_type type, unsigned pos)
497 space = isl_space_cow(space);
498 if (!space)
499 goto error;
501 if (type == isl_dim_param) {
502 int i;
504 for (i = 0; i < 2; ++i) {
505 if (!space->nested[i])
506 continue;
507 space->nested[i] =
508 isl_space_reset_dim_id(space->nested[i],
509 type, pos);
510 if (!space->nested[i])
511 goto error;
515 isl_id_free(get_id(space, type, pos));
516 return set_id(space, type, pos, NULL);
517 error:
518 isl_space_free(space);
519 return NULL;
522 int isl_space_has_dim_id(__isl_keep isl_space *dim,
523 enum isl_dim_type type, unsigned pos)
525 if (!dim)
526 return -1;
527 return get_id(dim, type, pos) != NULL;
530 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
531 enum isl_dim_type type, unsigned pos)
533 if (!dim)
534 return NULL;
535 if (!get_id(dim, type, pos))
536 isl_die(dim->ctx, isl_error_invalid,
537 "dim has no id", return NULL);
538 return isl_id_copy(get_id(dim, type, pos));
541 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
542 enum isl_dim_type type, const char *s)
544 isl_id *id;
546 if (!dim)
547 return NULL;
549 if (!s)
550 return isl_space_reset_tuple_id(dim, type);
552 if (!name_ok(dim->ctx, s))
553 goto error;
555 id = isl_id_alloc(dim->ctx, s, NULL);
556 return isl_space_set_tuple_id(dim, type, id);
557 error:
558 isl_space_free(dim);
559 return NULL;
562 /* Does the tuple have a name?
564 int isl_space_has_tuple_name(__isl_keep isl_space *space,
565 enum isl_dim_type type)
567 isl_id *id;
569 if (!space_can_have_id(space, type))
570 return -1;
571 id = space->tuple_id[type - isl_dim_in];
572 return id && id->name;
575 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
576 enum isl_dim_type type)
578 isl_id *id;
579 if (!dim)
580 return NULL;
581 if (type != isl_dim_in && type != isl_dim_out)
582 return NULL;
583 id = dim->tuple_id[type - isl_dim_in];
584 return id ? id->name : NULL;
587 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
588 enum isl_dim_type type, unsigned pos,
589 const char *s)
591 isl_id *id;
593 if (!dim)
594 return NULL;
595 if (!s)
596 return isl_space_reset_dim_id(dim, type, pos);
597 if (!name_ok(dim->ctx, s))
598 goto error;
599 id = isl_id_alloc(dim->ctx, s, NULL);
600 return isl_space_set_dim_id(dim, type, pos, id);
601 error:
602 isl_space_free(dim);
603 return NULL;
606 /* Does the given dimension have a name?
608 int isl_space_has_dim_name(__isl_keep isl_space *space,
609 enum isl_dim_type type, unsigned pos)
611 isl_id *id;
613 if (!space)
614 return -1;
615 id = get_id(space, type, pos);
616 return id && id->name;
619 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
620 enum isl_dim_type type, unsigned pos)
622 isl_id *id = get_id(dim, type, pos);
623 return id ? id->name : NULL;
626 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
627 __isl_keep isl_id *id)
629 int i;
630 int offset;
631 int n;
633 if (!dim || !id)
634 return -1;
636 offset = isl_space_offset(dim, type);
637 n = isl_space_dim(dim, type);
638 for (i = 0; i < n && offset + i < dim->n_id; ++i)
639 if (dim->ids[offset + i] == id)
640 return i;
642 return -1;
645 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
646 enum isl_dim_type type, const char *name)
648 int i;
649 int offset;
650 int n;
652 if (!space || !name)
653 return -1;
655 offset = isl_space_offset(space, type);
656 n = isl_space_dim(space, type);
657 for (i = 0; i < n && offset + i < space->n_id; ++i)
658 if (space->ids[offset + i]->name &&
659 !strcmp(space->ids[offset + i]->name, name))
660 return i;
662 return -1;
665 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
666 enum isl_dim_type type)
668 if (!dim)
669 return NULL;
670 if (type == isl_dim_in)
671 return dim->tuple_id[0];
672 if (type == isl_dim_out)
673 return dim->tuple_id[1];
674 return NULL;
677 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
678 enum isl_dim_type type)
680 if (!dim)
681 return NULL;
682 if (type == isl_dim_in)
683 return dim->nested[0];
684 if (type == isl_dim_out)
685 return dim->nested[1];
686 return NULL;
689 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
690 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
692 isl_id *id1, *id2;
693 isl_space *nested1, *nested2;
695 if (!dim1 || !dim2)
696 return -1;
698 if (dim1 == dim2 && dim1_type == dim2_type)
699 return 1;
701 if (n(dim1, dim1_type) != n(dim2, dim2_type))
702 return 0;
703 id1 = tuple_id(dim1, dim1_type);
704 id2 = tuple_id(dim2, dim2_type);
705 if (!id1 ^ !id2)
706 return 0;
707 if (id1 && id1 != id2)
708 return 0;
709 nested1 = nested(dim1, dim1_type);
710 nested2 = nested(dim2, dim2_type);
711 if (!nested1 ^ !nested2)
712 return 0;
713 if (nested1 && !isl_space_is_equal(nested1, nested2))
714 return 0;
715 return 1;
718 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
719 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
721 int i;
723 if (dim1 == dim2 && dim1_type == dim2_type)
724 return 1;
726 if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
727 return 0;
729 if (!dim1->ids && !dim2->ids)
730 return 1;
732 for (i = 0; i < n(dim1, dim1_type); ++i) {
733 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
734 return 0;
736 return 1;
739 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
740 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
742 if (!dim1 || !dim2)
743 return -1;
745 return match(dim1, dim1_type, dim2, dim2_type);
748 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
749 unsigned first, unsigned n, __isl_keep isl_id **ids)
751 int i;
753 for (i = 0; i < n ; ++i)
754 ids[i] = get_id(dim, type, first + i);
757 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
758 unsigned nparam, unsigned n_in, unsigned n_out)
760 isl_id **ids = NULL;
762 if (!dim)
763 return NULL;
764 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
765 return dim;
767 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
768 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
769 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
771 dim = isl_space_cow(dim);
773 if (dim->ids) {
774 ids = isl_calloc_array(dim->ctx, isl_id *,
775 nparam + n_in + n_out);
776 if (!ids)
777 goto error;
778 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
779 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
780 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
781 free(dim->ids);
782 dim->ids = ids;
783 dim->n_id = nparam + n_in + n_out;
785 dim->nparam = nparam;
786 dim->n_in = n_in;
787 dim->n_out = n_out;
789 return dim;
790 error:
791 free(ids);
792 isl_space_free(dim);
793 return NULL;
796 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
797 enum isl_dim_type type, unsigned n)
799 if (!dim)
800 return NULL;
801 dim = isl_space_reset(dim, type);
802 switch (type) {
803 case isl_dim_param:
804 dim = isl_space_extend(dim,
805 dim->nparam + n, dim->n_in, dim->n_out);
806 if (dim && dim->nested[0] &&
807 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
808 isl_dim_param, n)))
809 goto error;
810 if (dim && dim->nested[1] &&
811 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
812 isl_dim_param, n)))
813 goto error;
814 return dim;
815 case isl_dim_in:
816 return isl_space_extend(dim,
817 dim->nparam, dim->n_in + n, dim->n_out);
818 case isl_dim_out:
819 return isl_space_extend(dim,
820 dim->nparam, dim->n_in, dim->n_out + n);
821 default:
822 isl_die(dim->ctx, isl_error_invalid,
823 "cannot add dimensions of specified type", goto error);
825 error:
826 isl_space_free(dim);
827 return NULL;
830 static int valid_dim_type(enum isl_dim_type type)
832 switch (type) {
833 case isl_dim_param:
834 case isl_dim_in:
835 case isl_dim_out:
836 return 1;
837 default:
838 return 0;
842 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
843 enum isl_dim_type type, unsigned pos, unsigned n)
845 isl_id **ids = NULL;
847 if (!dim)
848 return NULL;
849 if (n == 0)
850 return isl_space_reset(dim, type);
852 if (!valid_dim_type(type))
853 isl_die(dim->ctx, isl_error_invalid,
854 "cannot insert dimensions of specified type",
855 goto error);
857 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
859 dim = isl_space_cow(dim);
860 if (!dim)
861 return NULL;
863 if (dim->ids) {
864 enum isl_dim_type t;
865 int off;
866 int s[3];
867 int *size = s - isl_dim_param;
868 ids = isl_calloc_array(dim->ctx, isl_id *,
869 dim->nparam + dim->n_in + dim->n_out + n);
870 if (!ids)
871 goto error;
872 off = 0;
873 size[isl_dim_param] = dim->nparam;
874 size[isl_dim_in] = dim->n_in;
875 size[isl_dim_out] = dim->n_out;
876 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
877 if (t != type) {
878 get_ids(dim, t, 0, size[t], ids + off);
879 off += size[t];
880 } else {
881 get_ids(dim, t, 0, pos, ids + off);
882 off += pos + n;
883 get_ids(dim, t, pos, size[t] - pos, ids + off);
884 off += size[t] - pos;
887 free(dim->ids);
888 dim->ids = ids;
889 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
891 switch (type) {
892 case isl_dim_param: dim->nparam += n; break;
893 case isl_dim_in: dim->n_in += n; break;
894 case isl_dim_out: dim->n_out += n; break;
895 default: ;
897 dim = isl_space_reset(dim, type);
899 return dim;
900 error:
901 isl_space_free(dim);
902 return NULL;
905 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
906 enum isl_dim_type dst_type, unsigned dst_pos,
907 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
909 int i;
911 if (!dim)
912 return NULL;
913 if (n == 0)
914 return dim;
916 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
917 goto error);
919 if (dst_type == src_type && dst_pos == src_pos)
920 return dim;
922 isl_assert(dim->ctx, dst_type != src_type, goto error);
924 dim = isl_space_reset(dim, src_type);
925 dim = isl_space_reset(dim, dst_type);
927 dim = isl_space_cow(dim);
928 if (!dim)
929 return NULL;
931 if (dim->ids) {
932 isl_id **ids;
933 enum isl_dim_type t;
934 int off;
935 int s[3];
936 int *size = s - isl_dim_param;
937 ids = isl_calloc_array(dim->ctx, isl_id *,
938 dim->nparam + dim->n_in + dim->n_out);
939 if (!ids)
940 goto error;
941 off = 0;
942 size[isl_dim_param] = dim->nparam;
943 size[isl_dim_in] = dim->n_in;
944 size[isl_dim_out] = dim->n_out;
945 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
946 if (t == dst_type) {
947 get_ids(dim, t, 0, dst_pos, ids + off);
948 off += dst_pos;
949 get_ids(dim, src_type, src_pos, n, ids + off);
950 off += n;
951 get_ids(dim, t, dst_pos, size[t] - dst_pos,
952 ids + off);
953 off += size[t] - dst_pos;
954 } else if (t == src_type) {
955 get_ids(dim, t, 0, src_pos, ids + off);
956 off += src_pos;
957 get_ids(dim, t, src_pos + n,
958 size[t] - src_pos - n, ids + off);
959 off += size[t] - src_pos - n;
960 } else {
961 get_ids(dim, t, 0, size[t], ids + off);
962 off += size[t];
965 free(dim->ids);
966 dim->ids = ids;
967 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
970 switch (dst_type) {
971 case isl_dim_param: dim->nparam += n; break;
972 case isl_dim_in: dim->n_in += n; break;
973 case isl_dim_out: dim->n_out += n; break;
974 default: ;
977 switch (src_type) {
978 case isl_dim_param: dim->nparam -= n; break;
979 case isl_dim_in: dim->n_in -= n; break;
980 case isl_dim_out: dim->n_out -= n; break;
981 default: ;
984 if (dst_type != isl_dim_param && src_type != isl_dim_param)
985 return dim;
987 for (i = 0; i < 2; ++i) {
988 if (!dim->nested[i])
989 continue;
990 dim->nested[i] = isl_space_replace(dim->nested[i],
991 isl_dim_param, dim);
992 if (!dim->nested[i])
993 goto error;
996 return dim;
997 error:
998 isl_space_free(dim);
999 return NULL;
1002 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1003 __isl_take isl_space *right)
1005 isl_space *dim;
1007 if (!left || !right)
1008 goto error;
1010 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1011 goto error);
1012 isl_assert(left->ctx,
1013 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
1014 goto error);
1016 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1017 if (!dim)
1018 goto error;
1020 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1021 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1022 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1024 if (dim && left->tuple_id[0] &&
1025 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1026 goto error;
1027 if (dim && right->tuple_id[1] &&
1028 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1029 goto error;
1030 if (dim && left->nested[0] &&
1031 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1032 goto error;
1033 if (dim && right->nested[1] &&
1034 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1035 goto error;
1037 isl_space_free(left);
1038 isl_space_free(right);
1040 return dim;
1041 error:
1042 isl_space_free(left);
1043 isl_space_free(right);
1044 return NULL;
1047 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1048 __isl_take isl_space *right)
1050 isl_space *dom1, *dom2, *nest1, *nest2;
1052 if (!left || !right)
1053 goto error;
1055 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1056 goto error);
1058 dom1 = isl_space_domain(isl_space_copy(left));
1059 dom2 = isl_space_domain(isl_space_copy(right));
1060 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1062 dom1 = isl_space_range(left);
1063 dom2 = isl_space_range(right);
1064 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1066 return isl_space_join(isl_space_reverse(nest1), nest2);
1067 error:
1068 isl_space_free(left);
1069 isl_space_free(right);
1070 return NULL;
1073 /* Given two spaces { A -> C } and { B -> C }, construct the space
1074 * { [A -> B] -> C }
1076 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1077 __isl_take isl_space *right)
1079 isl_space *ran, *dom1, *dom2, *nest;
1081 if (!left || !right)
1082 goto error;
1084 if (!match(left, isl_dim_param, right, isl_dim_param))
1085 isl_die(left->ctx, isl_error_invalid,
1086 "parameters need to match", goto error);
1087 if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1088 isl_die(left->ctx, isl_error_invalid,
1089 "ranges need to match", goto error);
1091 ran = isl_space_range(isl_space_copy(left));
1093 dom1 = isl_space_domain(left);
1094 dom2 = isl_space_domain(right);
1095 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1097 return isl_space_join(isl_space_reverse(nest), ran);
1098 error:
1099 isl_space_free(left);
1100 isl_space_free(right);
1101 return NULL;
1104 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1105 __isl_take isl_space *right)
1107 isl_space *dom, *ran1, *ran2, *nest;
1109 if (!left || !right)
1110 goto error;
1112 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1113 goto error);
1114 if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1115 isl_die(left->ctx, isl_error_invalid,
1116 "domains need to match", goto error);
1118 dom = isl_space_domain(isl_space_copy(left));
1120 ran1 = isl_space_range(left);
1121 ran2 = isl_space_range(right);
1122 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1124 return isl_space_join(isl_space_reverse(dom), nest);
1125 error:
1126 isl_space_free(left);
1127 isl_space_free(right);
1128 return NULL;
1131 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1133 isl_ctx *ctx;
1134 isl_id **ids = NULL;
1136 if (!dim)
1137 return NULL;
1138 ctx = isl_space_get_ctx(dim);
1139 if (!isl_space_is_set(dim))
1140 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1141 dim = isl_space_cow(dim);
1142 if (!dim)
1143 return NULL;
1144 if (dim->ids) {
1145 ids = isl_calloc_array(dim->ctx, isl_id *,
1146 dim->nparam + dim->n_out + dim->n_out);
1147 if (!ids)
1148 goto error;
1149 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1150 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1152 dim->n_in = dim->n_out;
1153 if (ids) {
1154 free(dim->ids);
1155 dim->ids = ids;
1156 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1157 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1159 isl_id_free(dim->tuple_id[0]);
1160 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1161 isl_space_free(dim->nested[0]);
1162 dim->nested[0] = isl_space_copy(dim->nested[1]);
1163 return dim;
1164 error:
1165 isl_space_free(dim);
1166 return NULL;
1169 __isl_give isl_space *isl_space_map_from_domain_and_range(
1170 __isl_take isl_space *domain, __isl_take isl_space *range)
1172 if (!domain || !range)
1173 goto error;
1174 if (!isl_space_is_set(domain))
1175 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1176 "domain is not a set space", goto error);
1177 if (!isl_space_is_set(range))
1178 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1179 "range is not a set space", goto error);
1180 return isl_space_join(isl_space_reverse(domain), range);
1181 error:
1182 isl_space_free(domain);
1183 isl_space_free(range);
1184 return NULL;
1187 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1188 enum isl_dim_type type,
1189 unsigned first, unsigned n, __isl_take isl_id **ids)
1191 int i;
1193 for (i = 0; i < n ; ++i)
1194 dim = set_id(dim, type, first + i, ids[i]);
1196 return dim;
1199 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1201 unsigned t;
1202 isl_space *nested;
1203 isl_id **ids = NULL;
1204 isl_id *id;
1206 if (!dim)
1207 return NULL;
1208 if (match(dim, isl_dim_in, dim, isl_dim_out))
1209 return dim;
1211 dim = isl_space_cow(dim);
1212 if (!dim)
1213 return NULL;
1215 id = dim->tuple_id[0];
1216 dim->tuple_id[0] = dim->tuple_id[1];
1217 dim->tuple_id[1] = id;
1219 nested = dim->nested[0];
1220 dim->nested[0] = dim->nested[1];
1221 dim->nested[1] = nested;
1223 if (dim->ids) {
1224 ids = isl_alloc_array(dim->ctx, isl_id *,
1225 dim->n_in + dim->n_out);
1226 if (!ids)
1227 goto error;
1228 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1229 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1232 t = dim->n_in;
1233 dim->n_in = dim->n_out;
1234 dim->n_out = t;
1236 if (dim->ids) {
1237 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1238 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1239 free(ids);
1242 return dim;
1243 error:
1244 free(ids);
1245 isl_space_free(dim);
1246 return NULL;
1249 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1250 enum isl_dim_type type, unsigned first, unsigned num)
1252 int i;
1254 if (!dim)
1255 return NULL;
1257 if (num == 0)
1258 return isl_space_reset(dim, type);
1260 if (!valid_dim_type(type))
1261 isl_die(dim->ctx, isl_error_invalid,
1262 "cannot drop dimensions of specified type", goto error);
1264 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1265 dim = isl_space_cow(dim);
1266 if (!dim)
1267 goto error;
1268 if (dim->ids) {
1269 dim = extend_ids(dim);
1270 if (!dim)
1271 goto error;
1272 for (i = 0; i < num; ++i)
1273 isl_id_free(get_id(dim, type, first + i));
1274 for (i = first+num; i < n(dim, type); ++i)
1275 set_id(dim, type, i - num, get_id(dim, type, i));
1276 switch (type) {
1277 case isl_dim_param:
1278 get_ids(dim, isl_dim_in, 0, dim->n_in,
1279 dim->ids + offset(dim, isl_dim_in) - num);
1280 case isl_dim_in:
1281 get_ids(dim, isl_dim_out, 0, dim->n_out,
1282 dim->ids + offset(dim, isl_dim_out) - num);
1283 default:
1286 dim->n_id -= num;
1288 switch (type) {
1289 case isl_dim_param: dim->nparam -= num; break;
1290 case isl_dim_in: dim->n_in -= num; break;
1291 case isl_dim_out: dim->n_out -= num; break;
1292 default: ;
1294 dim = isl_space_reset(dim, type);
1295 if (type == isl_dim_param) {
1296 if (dim && dim->nested[0] &&
1297 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1298 isl_dim_param, first, num)))
1299 goto error;
1300 if (dim && dim->nested[1] &&
1301 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1302 isl_dim_param, first, num)))
1303 goto error;
1305 return dim;
1306 error:
1307 isl_space_free(dim);
1308 return NULL;
1311 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1312 unsigned first, unsigned n)
1314 if (!dim)
1315 return NULL;
1316 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1319 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1320 unsigned first, unsigned n)
1322 if (!dim)
1323 return NULL;
1324 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1327 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1329 if (!dim)
1330 return NULL;
1331 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1332 dim = isl_space_reverse(dim);
1333 dim = mark_as_set(dim);
1334 return dim;
1337 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1339 if (!dim)
1340 return NULL;
1341 if (!isl_space_is_set(dim))
1342 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1343 "not a set space", goto error);
1344 dim = isl_space_reverse(dim);
1345 dim = isl_space_reset(dim, isl_dim_out);
1346 return dim;
1347 error:
1348 isl_space_free(dim);
1349 return NULL;
1352 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1354 if (!dim)
1355 return NULL;
1356 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1357 dim = mark_as_set(dim);
1358 return dim;
1361 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1363 if (!dim)
1364 return NULL;
1365 if (!isl_space_is_set(dim))
1366 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1367 "not a set space", goto error);
1368 return isl_space_reset(dim, isl_dim_in);
1369 error:
1370 isl_space_free(dim);
1371 return NULL;
1374 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1376 if (isl_space_is_params(space))
1377 return space;
1378 space = isl_space_drop_dims(space,
1379 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1380 space = isl_space_drop_dims(space,
1381 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1382 space = mark_as_params(space);
1383 return space;
1386 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1388 if (!space)
1389 return NULL;
1390 if (!isl_space_is_params(space))
1391 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1392 "not a parameter space", goto error);
1393 return isl_space_reset(space, isl_dim_set);
1394 error:
1395 isl_space_free(space);
1396 return NULL;
1399 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1401 dim = isl_space_cow(dim);
1402 if (!dim)
1403 return NULL;
1405 dim->n_out += dim->n_in;
1406 dim->n_in = 0;
1407 dim = isl_space_reset(dim, isl_dim_in);
1408 dim = isl_space_reset(dim, isl_dim_out);
1410 return dim;
1413 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1414 unsigned n_div)
1416 int i;
1418 if (!dim)
1419 return NULL;
1420 if (n_div == 0 &&
1421 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1422 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1423 dim = isl_space_cow(dim);
1424 if (!dim)
1425 return NULL;
1426 dim->n_out += dim->nparam + dim->n_in + n_div;
1427 dim->nparam = 0;
1428 dim->n_in = 0;
1430 for (i = 0; i < dim->n_id; ++i)
1431 isl_id_free(get_id(dim, isl_dim_out, i));
1432 dim->n_id = 0;
1433 dim = isl_space_reset(dim, isl_dim_in);
1434 dim = isl_space_reset(dim, isl_dim_out);
1436 return dim;
1439 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1441 if (!dim1 || !dim2)
1442 return -1;
1443 if (dim1 == dim2)
1444 return 1;
1445 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1446 isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1447 isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1450 /* Is space1 equal to the domain of space2?
1452 int isl_space_is_domain(__isl_keep isl_space *space1,
1453 __isl_keep isl_space *space2)
1455 if (!space1 || !space2)
1456 return -1;
1457 if (!isl_space_is_set(space1))
1458 return 0;
1459 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1460 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1463 int isl_space_compatible(__isl_keep isl_space *dim1,
1464 __isl_keep isl_space *dim2)
1466 return dim1->nparam == dim2->nparam &&
1467 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1470 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1472 int i;
1473 isl_id *id;
1475 if (!dim)
1476 return hash;
1478 hash = isl_hash_builtin(hash, dim->nparam);
1479 hash = isl_hash_builtin(hash, dim->n_in);
1480 hash = isl_hash_builtin(hash, dim->n_out);
1482 for (i = 0; i < dim->nparam; ++i) {
1483 id = get_id(dim, isl_dim_param, i);
1484 hash = isl_hash_id(hash, id);
1487 id = tuple_id(dim, isl_dim_in);
1488 hash = isl_hash_id(hash, id);
1489 id = tuple_id(dim, isl_dim_out);
1490 hash = isl_hash_id(hash, id);
1492 hash = isl_hash_dim(hash, dim->nested[0]);
1493 hash = isl_hash_dim(hash, dim->nested[1]);
1495 return hash;
1498 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1500 uint32_t hash;
1502 if (!dim)
1503 return 0;
1505 hash = isl_hash_init();
1506 hash = isl_hash_dim(hash, dim);
1508 return hash;
1511 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1513 if (!dim)
1514 return -1;
1516 if (!isl_space_is_set(dim))
1517 return 0;
1519 return dim->nested[1] != NULL;
1522 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1524 isl_space *wrap;
1526 if (!dim)
1527 return NULL;
1529 wrap = isl_space_set_alloc(dim->ctx,
1530 dim->nparam, dim->n_in + dim->n_out);
1532 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1533 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1534 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1536 if (!wrap)
1537 goto error;
1539 wrap->nested[1] = dim;
1541 return wrap;
1542 error:
1543 isl_space_free(dim);
1544 return NULL;
1547 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1549 isl_space *unwrap;
1551 if (!dim)
1552 return NULL;
1554 if (!isl_space_is_wrapping(dim))
1555 isl_die(dim->ctx, isl_error_invalid, "not a wrapping dim",
1556 goto error);
1558 unwrap = isl_space_copy(dim->nested[1]);
1559 isl_space_free(dim);
1561 return unwrap;
1562 error:
1563 isl_space_free(dim);
1564 return NULL;
1567 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1569 if (type != isl_dim_in && type != isl_dim_out)
1570 return 0;
1571 if (!dim)
1572 return -1;
1573 if (dim->tuple_id[type - isl_dim_in])
1574 return 1;
1575 if (dim->nested[type - isl_dim_in])
1576 return 1;
1577 return 0;
1580 int isl_space_may_be_set(__isl_keep isl_space *dim)
1582 if (!dim)
1583 return -1;
1584 if (isl_space_is_set(dim))
1585 return 1;
1586 if (isl_space_dim(dim, isl_dim_in) != 0)
1587 return 0;
1588 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1589 return 0;
1590 return 1;
1593 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1594 enum isl_dim_type type)
1596 if (!isl_space_is_named_or_nested(dim, type))
1597 return dim;
1599 dim = isl_space_cow(dim);
1600 if (!dim)
1601 return NULL;
1603 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1604 dim->tuple_id[type - isl_dim_in] = NULL;
1605 isl_space_free(dim->nested[type - isl_dim_in]);
1606 dim->nested[type - isl_dim_in] = NULL;
1608 return dim;
1611 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1613 if (!dim)
1614 return NULL;
1615 if (!dim->nested[0] && !dim->nested[1])
1616 return dim;
1618 if (dim->nested[0])
1619 dim = isl_space_reset(dim, isl_dim_in);
1620 if (dim && dim->nested[1])
1621 dim = isl_space_reset(dim, isl_dim_out);
1623 return dim;
1626 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1628 if (!dim)
1629 return NULL;
1630 if (!dim->nested[0])
1631 return dim;
1633 return isl_space_reset(dim, isl_dim_in);
1636 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1638 if (!dim)
1639 return NULL;
1640 if (!dim->nested[1])
1641 return dim;
1643 return isl_space_reset(dim, isl_dim_out);
1646 /* Replace the dimensions of the given type of dst by those of src.
1648 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1649 enum isl_dim_type type, __isl_keep isl_space *src)
1651 dst = isl_space_cow(dst);
1653 if (!dst || !src)
1654 goto error;
1656 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1657 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1658 dst = copy_ids(dst, type, 0, src, type);
1660 if (dst && type == isl_dim_param) {
1661 int i;
1662 for (i = 0; i <= 1; ++i) {
1663 if (!dst->nested[i])
1664 continue;
1665 dst->nested[i] = isl_space_replace(dst->nested[i],
1666 type, src);
1667 if (!dst->nested[i])
1668 goto error;
1672 return dst;
1673 error:
1674 isl_space_free(dst);
1675 return NULL;
1678 /* Given a dimension specification "dim" of a set, create a dimension
1679 * specification for the lift of the set. In particular, the result
1680 * is of the form [dim -> local[..]], with n_local variables in the
1681 * range of the wrapped map.
1683 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1685 isl_space *local_dim;
1687 if (!dim)
1688 return NULL;
1690 local_dim = isl_space_dup(dim);
1691 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1692 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1693 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1694 dim = isl_space_join(isl_space_from_domain(dim),
1695 isl_space_from_range(local_dim));
1696 dim = isl_space_wrap(dim);
1697 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1699 return dim;
1702 int isl_space_can_zip(__isl_keep isl_space *dim)
1704 if (!dim)
1705 return -1;
1707 return dim->nested[0] && dim->nested[1];
1710 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1712 isl_space *dom, *ran;
1713 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1715 if (!isl_space_can_zip(dim))
1716 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1717 goto error);
1719 if (!dim)
1720 return NULL;
1721 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1722 ran = isl_space_unwrap(isl_space_range(dim));
1723 dom_dom = isl_space_domain(isl_space_copy(dom));
1724 dom_ran = isl_space_range(dom);
1725 ran_dom = isl_space_domain(isl_space_copy(ran));
1726 ran_ran = isl_space_range(ran);
1727 dom = isl_space_join(isl_space_from_domain(dom_dom),
1728 isl_space_from_range(ran_dom));
1729 ran = isl_space_join(isl_space_from_domain(dom_ran),
1730 isl_space_from_range(ran_ran));
1731 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1732 isl_space_from_range(isl_space_wrap(ran)));
1733 error:
1734 isl_space_free(dim);
1735 return NULL;
1738 /* Can we apply isl_space_curry to "space"?
1739 * That is, does it have a nested relation in its domain?
1741 int isl_space_can_curry(__isl_keep isl_space *space)
1743 if (!space)
1744 return -1;
1746 return !!space->nested[0];
1749 /* Given a space (A -> B) -> C, return the corresponding space
1750 * A -> (B -> C).
1752 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
1754 isl_space *dom, *ran;
1755 isl_space *dom_dom, *dom_ran;
1757 if (!space)
1758 return NULL;
1760 if (!isl_space_can_curry(space))
1761 isl_die(space->ctx, isl_error_invalid,
1762 "space cannot be curried", goto error);
1764 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
1765 ran = isl_space_range(space);
1766 dom_dom = isl_space_domain(isl_space_copy(dom));
1767 dom_ran = isl_space_range(dom);
1768 ran = isl_space_join(isl_space_from_domain(dom_ran),
1769 isl_space_from_range(ran));
1770 return isl_space_join(isl_space_from_domain(dom_dom),
1771 isl_space_from_range(isl_space_wrap(ran)));
1772 error:
1773 isl_space_free(space);
1774 return NULL;
1777 int isl_space_has_named_params(__isl_keep isl_space *dim)
1779 int i;
1780 unsigned off;
1782 if (!dim)
1783 return -1;
1784 if (dim->nparam == 0)
1785 return 1;
1786 off = isl_space_offset(dim, isl_dim_param);
1787 if (off + dim->nparam > dim->n_id)
1788 return 0;
1789 for (i = 0; i < dim->nparam; ++i)
1790 if (!dim->ids[off + i])
1791 return 0;
1792 return 1;
1795 /* Align the initial parameters of dim1 to match the order in dim2.
1797 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1798 __isl_take isl_space *dim2)
1800 isl_reordering *exp;
1802 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1803 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1804 "parameter alignment requires named parameters",
1805 goto error);
1807 dim2 = isl_space_params(dim2);
1808 exp = isl_parameter_alignment_reordering(dim1, dim2);
1809 exp = isl_reordering_extend_space(exp, dim1);
1810 isl_space_free(dim2);
1811 if (!exp)
1812 return NULL;
1813 dim1 = isl_space_copy(exp->dim);
1814 isl_reordering_free(exp);
1815 return dim1;
1816 error:
1817 isl_space_free(dim1);
1818 isl_space_free(dim2);
1819 return NULL;
1822 /* Given the space of set (domain), construct a space for a map
1823 * with as domain the given space and as range the range of "model".
1825 __isl_give isl_space *isl_space_extend_domain_with_range(
1826 __isl_take isl_space *domain, __isl_take isl_space *model)
1828 isl_space *space;
1830 space = isl_space_from_domain(domain);
1831 space = isl_space_add_dims(space, isl_dim_out,
1832 isl_space_dim(model, isl_dim_out));
1833 if (isl_space_has_tuple_id(model, isl_dim_out))
1834 space = isl_space_set_tuple_id(space, isl_dim_out,
1835 isl_space_get_tuple_id(model, isl_dim_out));
1836 isl_space_free(model);
1837 return space;