isl_multi_*_align_params: check if parameters match first
[isl.git] / isl_space.c
blob28781985699929cdec04bc047390831286206156
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2013-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
15 #include <stdlib.h>
16 #include <string.h>
17 #include <isl_space_private.h>
18 #include <isl_id_private.h>
19 #include <isl_reordering.h>
21 isl_ctx *isl_space_get_ctx(__isl_keep isl_space *dim)
23 return dim ? dim->ctx : NULL;
26 __isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
27 unsigned nparam, unsigned n_in, unsigned n_out)
29 isl_space *dim;
31 dim = isl_alloc_type(ctx, struct isl_space);
32 if (!dim)
33 return NULL;
35 dim->ctx = ctx;
36 isl_ctx_ref(ctx);
37 dim->ref = 1;
38 dim->nparam = nparam;
39 dim->n_in = n_in;
40 dim->n_out = n_out;
42 dim->tuple_id[0] = NULL;
43 dim->tuple_id[1] = NULL;
45 dim->nested[0] = NULL;
46 dim->nested[1] = NULL;
48 dim->n_id = 0;
49 dim->ids = NULL;
51 return dim;
54 /* Mark the space as being that of a set, by setting the domain tuple
55 * to isl_id_none.
57 static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
59 space = isl_space_cow(space);
60 if (!space)
61 return NULL;
62 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
63 return space;
66 /* Is the space that of a set?
68 int isl_space_is_set(__isl_keep isl_space *space)
70 if (!space)
71 return -1;
72 if (space->n_in != 0 || space->nested[0])
73 return 0;
74 if (space->tuple_id[0] != &isl_id_none)
75 return 0;
76 return 1;
79 /* Is the given space that of a map?
81 int isl_space_is_map(__isl_keep isl_space *space)
83 if (!space)
84 return -1;
85 return space->tuple_id[0] != &isl_id_none &&
86 space->tuple_id[1] != &isl_id_none;
89 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
90 unsigned nparam, unsigned dim)
92 isl_space *space;
93 space = isl_space_alloc(ctx, nparam, 0, dim);
94 space = mark_as_set(space);
95 return space;
98 /* Mark the space as being that of a parameter domain, by setting
99 * both tuples to isl_id_none.
101 static __isl_give isl_space *mark_as_params(isl_space *space)
103 if (!space)
104 return NULL;
105 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
106 space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
107 return space;
110 /* Is the space that of a parameter domain?
112 int isl_space_is_params(__isl_keep isl_space *space)
114 if (!space)
115 return -1;
116 if (space->n_in != 0 || space->nested[0] ||
117 space->n_out != 0 || space->nested[1])
118 return 0;
119 if (space->tuple_id[0] != &isl_id_none)
120 return 0;
121 if (space->tuple_id[1] != &isl_id_none)
122 return 0;
123 return 1;
126 /* Create a space for a parameter domain.
128 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
130 isl_space *space;
131 space = isl_space_alloc(ctx, nparam, 0, 0);
132 space = mark_as_params(space);
133 return space;
136 static unsigned global_pos(__isl_keep isl_space *dim,
137 enum isl_dim_type type, unsigned pos)
139 struct isl_ctx *ctx = dim->ctx;
141 switch (type) {
142 case isl_dim_param:
143 isl_assert(ctx, pos < dim->nparam,
144 return isl_space_dim(dim, isl_dim_all));
145 return pos;
146 case isl_dim_in:
147 isl_assert(ctx, pos < dim->n_in,
148 return isl_space_dim(dim, isl_dim_all));
149 return pos + dim->nparam;
150 case isl_dim_out:
151 isl_assert(ctx, pos < dim->n_out,
152 return isl_space_dim(dim, isl_dim_all));
153 return pos + dim->nparam + dim->n_in;
154 default:
155 isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
157 return isl_space_dim(dim, isl_dim_all);
160 /* Extend length of ids array to the total number of dimensions.
162 static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
164 isl_id **ids;
165 int i;
167 if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
168 return dim;
170 if (!dim->ids) {
171 dim->ids = isl_calloc_array(dim->ctx,
172 isl_id *, isl_space_dim(dim, isl_dim_all));
173 if (!dim->ids)
174 goto error;
175 } else {
176 ids = isl_realloc_array(dim->ctx, dim->ids,
177 isl_id *, isl_space_dim(dim, isl_dim_all));
178 if (!ids)
179 goto error;
180 dim->ids = ids;
181 for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); ++i)
182 dim->ids[i] = NULL;
185 dim->n_id = isl_space_dim(dim, isl_dim_all);
187 return dim;
188 error:
189 isl_space_free(dim);
190 return NULL;
193 static __isl_give isl_space *set_id(__isl_take isl_space *dim,
194 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
196 dim = isl_space_cow(dim);
198 if (!dim)
199 goto error;
201 pos = global_pos(dim, type, pos);
202 if (pos == isl_space_dim(dim, isl_dim_all))
203 goto error;
205 if (pos >= dim->n_id) {
206 if (!id)
207 return dim;
208 dim = extend_ids(dim);
209 if (!dim)
210 goto error;
213 dim->ids[pos] = id;
215 return dim;
216 error:
217 isl_id_free(id);
218 isl_space_free(dim);
219 return NULL;
222 static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
223 enum isl_dim_type type, unsigned pos)
225 if (!dim)
226 return NULL;
228 pos = global_pos(dim, type, pos);
229 if (pos == isl_space_dim(dim, isl_dim_all))
230 return NULL;
231 if (pos >= dim->n_id)
232 return NULL;
233 return dim->ids[pos];
236 static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
238 switch (type) {
239 case isl_dim_param: return 0;
240 case isl_dim_in: return dim->nparam;
241 case isl_dim_out: return dim->nparam + dim->n_in;
242 default: return 0;
246 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
248 switch (type) {
249 case isl_dim_param: return dim->nparam;
250 case isl_dim_in: return dim->n_in;
251 case isl_dim_out: return dim->n_out;
252 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
253 default: return 0;
257 unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
259 if (!dim)
260 return 0;
261 return n(dim, type);
264 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
266 if (!dim)
267 return 0;
268 return offset(dim, type);
271 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
272 enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
273 enum isl_dim_type src_type)
275 int i;
276 isl_id *id;
278 if (!dst)
279 return NULL;
281 for (i = 0; i < n(src, src_type); ++i) {
282 id = get_id(src, src_type, i);
283 if (!id)
284 continue;
285 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
286 if (!dst)
287 return NULL;
289 return dst;
292 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
294 isl_space *dup;
295 if (!dim)
296 return NULL;
297 dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
298 if (!dup)
299 return NULL;
300 if (dim->tuple_id[0] &&
301 !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
302 goto error;
303 if (dim->tuple_id[1] &&
304 !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
305 goto error;
306 if (dim->nested[0] && !(dup->nested[0] = isl_space_copy(dim->nested[0])))
307 goto error;
308 if (dim->nested[1] && !(dup->nested[1] = isl_space_copy(dim->nested[1])))
309 goto error;
310 if (!dim->ids)
311 return dup;
312 dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
313 dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
314 dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
315 return dup;
316 error:
317 isl_space_free(dup);
318 return NULL;
321 __isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
323 if (!dim)
324 return NULL;
326 if (dim->ref == 1)
327 return dim;
328 dim->ref--;
329 return isl_space_dup(dim);
332 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
334 if (!dim)
335 return NULL;
337 dim->ref++;
338 return dim;
341 void *isl_space_free(__isl_take isl_space *dim)
343 int i;
345 if (!dim)
346 return NULL;
348 if (--dim->ref > 0)
349 return NULL;
351 isl_id_free(dim->tuple_id[0]);
352 isl_id_free(dim->tuple_id[1]);
354 isl_space_free(dim->nested[0]);
355 isl_space_free(dim->nested[1]);
357 for (i = 0; i < dim->n_id; ++i)
358 isl_id_free(dim->ids[i]);
359 free(dim->ids);
360 isl_ctx_deref(dim->ctx);
362 free(dim);
364 return NULL;
367 /* Check if "s" is a valid dimension or tuple name.
368 * We currently only forbid names that look like a number.
370 * s is assumed to be non-NULL.
372 static int name_ok(isl_ctx *ctx, const char *s)
374 char *p;
375 long dummy;
377 dummy = strtol(s, &p, 0);
378 if (p != s)
379 isl_die(ctx, isl_error_invalid, "name looks like a number",
380 return 0);
382 return 1;
385 /* Is it possible for the given dimension type to have a tuple id?
387 static int space_can_have_id(__isl_keep isl_space *space,
388 enum isl_dim_type type)
390 if (!space)
391 return 0;
392 if (isl_space_is_params(space))
393 isl_die(space->ctx, isl_error_invalid,
394 "parameter spaces don't have tuple ids", return 0);
395 if (isl_space_is_set(space) && type != isl_dim_set)
396 isl_die(space->ctx, isl_error_invalid,
397 "set spaces can only have a set id", return 0);
398 if (type != isl_dim_in && type != isl_dim_out)
399 isl_die(space->ctx, isl_error_invalid,
400 "only input, output and set tuples can have ids",
401 return 0);
403 return 1;
406 /* Does the tuple have an id?
408 int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
410 if (!space_can_have_id(dim, type))
411 return -1;
412 return dim->tuple_id[type - isl_dim_in] != NULL;
415 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
416 enum isl_dim_type type)
418 int has_id;
420 if (!dim)
421 return NULL;
422 has_id = isl_space_has_tuple_id(dim, type);
423 if (has_id < 0)
424 return NULL;
425 if (!has_id)
426 isl_die(dim->ctx, isl_error_invalid,
427 "tuple has no id", return NULL);
428 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
431 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
432 enum isl_dim_type type, __isl_take isl_id *id)
434 dim = isl_space_cow(dim);
435 if (!dim || !id)
436 goto error;
437 if (type != isl_dim_in && type != isl_dim_out)
438 isl_die(dim->ctx, isl_error_invalid,
439 "only input, output and set tuples can have names",
440 goto error);
442 isl_id_free(dim->tuple_id[type - isl_dim_in]);
443 dim->tuple_id[type - isl_dim_in] = id;
445 return dim;
446 error:
447 isl_id_free(id);
448 isl_space_free(dim);
449 return NULL;
452 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
453 enum isl_dim_type type)
455 dim = isl_space_cow(dim);
456 if (!dim)
457 return NULL;
458 if (type != isl_dim_in && type != isl_dim_out)
459 isl_die(dim->ctx, isl_error_invalid,
460 "only input, output and set tuples can have names",
461 goto error);
463 isl_id_free(dim->tuple_id[type - isl_dim_in]);
464 dim->tuple_id[type - isl_dim_in] = NULL;
466 return dim;
467 error:
468 isl_space_free(dim);
469 return NULL;
472 /* Set the id of the given dimension of "space" to "id".
473 * If the dimension already has an id, then it is replaced.
474 * If the dimension is a parameter, then we need to change it
475 * in the nested spaces (if any) as well.
477 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
478 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
480 space = isl_space_cow(space);
481 if (!space || !id)
482 goto error;
484 if (type == isl_dim_param) {
485 int i;
487 for (i = 0; i < 2; ++i) {
488 if (!space->nested[i])
489 continue;
490 space->nested[i] =
491 isl_space_set_dim_id(space->nested[i],
492 type, pos, isl_id_copy(id));
493 if (!space->nested[i])
494 goto error;
498 isl_id_free(get_id(space, type, pos));
499 return set_id(space, type, pos, id);
500 error:
501 isl_id_free(id);
502 isl_space_free(space);
503 return NULL;
506 /* Reset the id of the given dimension of "space".
507 * If the dimension already has an id, then it is removed.
508 * If the dimension is a parameter, then we need to reset it
509 * in the nested spaces (if any) as well.
511 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
512 enum isl_dim_type type, unsigned pos)
514 space = isl_space_cow(space);
515 if (!space)
516 goto error;
518 if (type == isl_dim_param) {
519 int i;
521 for (i = 0; i < 2; ++i) {
522 if (!space->nested[i])
523 continue;
524 space->nested[i] =
525 isl_space_reset_dim_id(space->nested[i],
526 type, pos);
527 if (!space->nested[i])
528 goto error;
532 isl_id_free(get_id(space, type, pos));
533 return set_id(space, type, pos, NULL);
534 error:
535 isl_space_free(space);
536 return NULL;
539 int isl_space_has_dim_id(__isl_keep isl_space *dim,
540 enum isl_dim_type type, unsigned pos)
542 if (!dim)
543 return -1;
544 return get_id(dim, type, pos) != NULL;
547 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
548 enum isl_dim_type type, unsigned pos)
550 if (!dim)
551 return NULL;
552 if (!get_id(dim, type, pos))
553 isl_die(dim->ctx, isl_error_invalid,
554 "dim has no id", return NULL);
555 return isl_id_copy(get_id(dim, type, pos));
558 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
559 enum isl_dim_type type, const char *s)
561 isl_id *id;
563 if (!dim)
564 return NULL;
566 if (!s)
567 return isl_space_reset_tuple_id(dim, type);
569 if (!name_ok(dim->ctx, s))
570 goto error;
572 id = isl_id_alloc(dim->ctx, s, NULL);
573 return isl_space_set_tuple_id(dim, type, id);
574 error:
575 isl_space_free(dim);
576 return NULL;
579 /* Does the tuple have a name?
581 int isl_space_has_tuple_name(__isl_keep isl_space *space,
582 enum isl_dim_type type)
584 isl_id *id;
586 if (!space_can_have_id(space, type))
587 return -1;
588 id = space->tuple_id[type - isl_dim_in];
589 return id && id->name;
592 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
593 enum isl_dim_type type)
595 isl_id *id;
596 if (!dim)
597 return NULL;
598 if (type != isl_dim_in && type != isl_dim_out)
599 return NULL;
600 id = dim->tuple_id[type - isl_dim_in];
601 return id ? id->name : NULL;
604 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
605 enum isl_dim_type type, unsigned pos,
606 const char *s)
608 isl_id *id;
610 if (!dim)
611 return NULL;
612 if (!s)
613 return isl_space_reset_dim_id(dim, type, pos);
614 if (!name_ok(dim->ctx, s))
615 goto error;
616 id = isl_id_alloc(dim->ctx, s, NULL);
617 return isl_space_set_dim_id(dim, type, pos, id);
618 error:
619 isl_space_free(dim);
620 return NULL;
623 /* Does the given dimension have a name?
625 int isl_space_has_dim_name(__isl_keep isl_space *space,
626 enum isl_dim_type type, unsigned pos)
628 isl_id *id;
630 if (!space)
631 return -1;
632 id = get_id(space, type, pos);
633 return id && id->name;
636 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
637 enum isl_dim_type type, unsigned pos)
639 isl_id *id = get_id(dim, type, pos);
640 return id ? id->name : NULL;
643 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
644 __isl_keep isl_id *id)
646 int i;
647 int offset;
648 int n;
650 if (!dim || !id)
651 return -1;
653 offset = isl_space_offset(dim, type);
654 n = isl_space_dim(dim, type);
655 for (i = 0; i < n && offset + i < dim->n_id; ++i)
656 if (dim->ids[offset + i] == id)
657 return i;
659 return -1;
662 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
663 enum isl_dim_type type, const char *name)
665 int i;
666 int offset;
667 int n;
669 if (!space || !name)
670 return -1;
672 offset = isl_space_offset(space, type);
673 n = isl_space_dim(space, type);
674 for (i = 0; i < n && offset + i < space->n_id; ++i)
675 if (space->ids[offset + i]->name &&
676 !strcmp(space->ids[offset + i]->name, name))
677 return i;
679 return -1;
682 /* Reset the user pointer on all identifiers of parameters and tuples
683 * of "space".
685 __isl_give isl_space *isl_space_reset_user(__isl_take isl_space *space)
687 int i;
688 isl_ctx *ctx;
689 isl_id *id;
690 const char *name;
692 if (!space)
693 return NULL;
695 ctx = isl_space_get_ctx(space);
697 for (i = 0; i < space->nparam && i < space->n_id; ++i) {
698 if (!isl_id_get_user(space->ids[i]))
699 continue;
700 space = isl_space_cow(space);
701 if (!space)
702 return NULL;
703 name = isl_id_get_name(space->ids[i]);
704 id = isl_id_alloc(ctx, name, NULL);
705 isl_id_free(space->ids[i]);
706 space->ids[i] = id;
707 if (!id)
708 return isl_space_free(space);
711 for (i = 0; i < 2; ++i) {
712 if (!space->tuple_id[i])
713 continue;
714 if (!isl_id_get_user(space->tuple_id[i]))
715 continue;
716 space = isl_space_cow(space);
717 if (!space)
718 return NULL;
719 name = isl_id_get_name(space->tuple_id[i]);
720 id = isl_id_alloc(ctx, name, NULL);
721 isl_id_free(space->tuple_id[i]);
722 space->tuple_id[i] = id;
723 if (!id)
724 return isl_space_free(space);
727 for (i = 0; i < 2; ++i) {
728 if (!space->nested[i])
729 continue;
730 space = isl_space_cow(space);
731 if (!space)
732 return NULL;
733 space->nested[i] = isl_space_reset_user(space->nested[i]);
734 if (!space->nested[i])
735 return isl_space_free(space);
738 return space;
741 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
742 enum isl_dim_type type)
744 if (!dim)
745 return NULL;
746 if (type == isl_dim_in)
747 return dim->tuple_id[0];
748 if (type == isl_dim_out)
749 return dim->tuple_id[1];
750 return NULL;
753 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
754 enum isl_dim_type type)
756 if (!dim)
757 return NULL;
758 if (type == isl_dim_in)
759 return dim->nested[0];
760 if (type == isl_dim_out)
761 return dim->nested[1];
762 return NULL;
765 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
766 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
768 isl_id *id1, *id2;
769 isl_space *nested1, *nested2;
771 if (!dim1 || !dim2)
772 return -1;
774 if (dim1 == dim2 && dim1_type == dim2_type)
775 return 1;
777 if (n(dim1, dim1_type) != n(dim2, dim2_type))
778 return 0;
779 id1 = tuple_id(dim1, dim1_type);
780 id2 = tuple_id(dim2, dim2_type);
781 if (!id1 ^ !id2)
782 return 0;
783 if (id1 && id1 != id2)
784 return 0;
785 nested1 = nested(dim1, dim1_type);
786 nested2 = nested(dim2, dim2_type);
787 if (!nested1 ^ !nested2)
788 return 0;
789 if (nested1 && !isl_space_is_equal(nested1, nested2))
790 return 0;
791 return 1;
794 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
795 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
797 int i;
799 if (dim1 == dim2 && dim1_type == dim2_type)
800 return 1;
802 if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
803 return 0;
805 if (!dim1->ids && !dim2->ids)
806 return 1;
808 for (i = 0; i < n(dim1, dim1_type); ++i) {
809 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
810 return 0;
812 return 1;
815 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
816 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
818 if (!dim1 || !dim2)
819 return -1;
821 return match(dim1, dim1_type, dim2, dim2_type);
824 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
825 unsigned first, unsigned n, __isl_keep isl_id **ids)
827 int i;
829 for (i = 0; i < n ; ++i)
830 ids[i] = get_id(dim, type, first + i);
833 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
834 unsigned nparam, unsigned n_in, unsigned n_out)
836 isl_id **ids = NULL;
838 if (!dim)
839 return NULL;
840 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
841 return dim;
843 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
844 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
845 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
847 dim = isl_space_cow(dim);
848 if (!dim)
849 goto error;
851 if (dim->ids) {
852 ids = isl_calloc_array(dim->ctx, isl_id *,
853 nparam + n_in + n_out);
854 if (!ids)
855 goto error;
856 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
857 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
858 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
859 free(dim->ids);
860 dim->ids = ids;
861 dim->n_id = nparam + n_in + n_out;
863 dim->nparam = nparam;
864 dim->n_in = n_in;
865 dim->n_out = n_out;
867 return dim;
868 error:
869 free(ids);
870 isl_space_free(dim);
871 return NULL;
874 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
875 enum isl_dim_type type, unsigned n)
877 if (!dim)
878 return NULL;
879 dim = isl_space_reset(dim, type);
880 switch (type) {
881 case isl_dim_param:
882 dim = isl_space_extend(dim,
883 dim->nparam + n, dim->n_in, dim->n_out);
884 if (dim && dim->nested[0] &&
885 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
886 isl_dim_param, n)))
887 goto error;
888 if (dim && dim->nested[1] &&
889 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
890 isl_dim_param, n)))
891 goto error;
892 return dim;
893 case isl_dim_in:
894 return isl_space_extend(dim,
895 dim->nparam, dim->n_in + n, dim->n_out);
896 case isl_dim_out:
897 return isl_space_extend(dim,
898 dim->nparam, dim->n_in, dim->n_out + n);
899 default:
900 isl_die(dim->ctx, isl_error_invalid,
901 "cannot add dimensions of specified type", goto error);
903 error:
904 isl_space_free(dim);
905 return NULL;
908 static int valid_dim_type(enum isl_dim_type type)
910 switch (type) {
911 case isl_dim_param:
912 case isl_dim_in:
913 case isl_dim_out:
914 return 1;
915 default:
916 return 0;
920 /* Insert "n" dimensions of type "type" at position "pos".
921 * If we are inserting parameters, then they are also inserted in
922 * any nested spaces.
924 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
925 enum isl_dim_type type, unsigned pos, unsigned n)
927 isl_id **ids = NULL;
929 if (!dim)
930 return NULL;
931 if (n == 0)
932 return isl_space_reset(dim, type);
934 if (!valid_dim_type(type))
935 isl_die(dim->ctx, isl_error_invalid,
936 "cannot insert dimensions of specified type",
937 goto error);
939 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
941 dim = isl_space_cow(dim);
942 if (!dim)
943 return NULL;
945 if (dim->ids) {
946 enum isl_dim_type t, o = isl_dim_param;
947 int off;
948 int s[3];
949 ids = isl_calloc_array(dim->ctx, isl_id *,
950 dim->nparam + dim->n_in + dim->n_out + n);
951 if (!ids)
952 goto error;
953 off = 0;
954 s[isl_dim_param - o] = dim->nparam;
955 s[isl_dim_in - o] = dim->n_in;
956 s[isl_dim_out - o] = dim->n_out;
957 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
958 if (t != type) {
959 get_ids(dim, t, 0, s[t - o], ids + off);
960 off += s[t - o];
961 } else {
962 get_ids(dim, t, 0, pos, ids + off);
963 off += pos + n;
964 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
965 off += s[t - o] - pos;
968 free(dim->ids);
969 dim->ids = ids;
970 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
972 switch (type) {
973 case isl_dim_param: dim->nparam += n; break;
974 case isl_dim_in: dim->n_in += n; break;
975 case isl_dim_out: dim->n_out += n; break;
976 default: ;
978 dim = isl_space_reset(dim, type);
980 if (type == isl_dim_param) {
981 if (dim && dim->nested[0] &&
982 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
983 isl_dim_param, pos, n)))
984 goto error;
985 if (dim && dim->nested[1] &&
986 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
987 isl_dim_param, pos, n)))
988 goto error;
991 return dim;
992 error:
993 isl_space_free(dim);
994 return NULL;
997 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
998 enum isl_dim_type dst_type, unsigned dst_pos,
999 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
1001 int i;
1003 if (!dim)
1004 return NULL;
1005 if (n == 0) {
1006 dim = isl_space_reset(dim, src_type);
1007 return isl_space_reset(dim, dst_type);
1010 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
1011 goto error);
1013 if (dst_type == src_type && dst_pos == src_pos)
1014 return dim;
1016 isl_assert(dim->ctx, dst_type != src_type, goto error);
1018 dim = isl_space_reset(dim, src_type);
1019 dim = isl_space_reset(dim, dst_type);
1021 dim = isl_space_cow(dim);
1022 if (!dim)
1023 return NULL;
1025 if (dim->ids) {
1026 isl_id **ids;
1027 enum isl_dim_type t, o = isl_dim_param;
1028 int off;
1029 int s[3];
1030 ids = isl_calloc_array(dim->ctx, isl_id *,
1031 dim->nparam + dim->n_in + dim->n_out);
1032 if (!ids)
1033 goto error;
1034 off = 0;
1035 s[isl_dim_param - o] = dim->nparam;
1036 s[isl_dim_in - o] = dim->n_in;
1037 s[isl_dim_out - o] = dim->n_out;
1038 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
1039 if (t == dst_type) {
1040 get_ids(dim, t, 0, dst_pos, ids + off);
1041 off += dst_pos;
1042 get_ids(dim, src_type, src_pos, n, ids + off);
1043 off += n;
1044 get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
1045 ids + off);
1046 off += s[t - o] - dst_pos;
1047 } else if (t == src_type) {
1048 get_ids(dim, t, 0, src_pos, ids + off);
1049 off += src_pos;
1050 get_ids(dim, t, src_pos + n,
1051 s[t - o] - src_pos - n, ids + off);
1052 off += s[t - o] - src_pos - n;
1053 } else {
1054 get_ids(dim, t, 0, s[t - o], ids + off);
1055 off += s[t - o];
1058 free(dim->ids);
1059 dim->ids = ids;
1060 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
1063 switch (dst_type) {
1064 case isl_dim_param: dim->nparam += n; break;
1065 case isl_dim_in: dim->n_in += n; break;
1066 case isl_dim_out: dim->n_out += n; break;
1067 default: ;
1070 switch (src_type) {
1071 case isl_dim_param: dim->nparam -= n; break;
1072 case isl_dim_in: dim->n_in -= n; break;
1073 case isl_dim_out: dim->n_out -= n; break;
1074 default: ;
1077 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1078 return dim;
1080 for (i = 0; i < 2; ++i) {
1081 if (!dim->nested[i])
1082 continue;
1083 dim->nested[i] = isl_space_replace(dim->nested[i],
1084 isl_dim_param, dim);
1085 if (!dim->nested[i])
1086 goto error;
1089 return dim;
1090 error:
1091 isl_space_free(dim);
1092 return NULL;
1095 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1096 __isl_take isl_space *right)
1098 isl_space *dim;
1100 if (!left || !right)
1101 goto error;
1103 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1104 goto error);
1105 isl_assert(left->ctx,
1106 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
1107 goto error);
1109 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1110 if (!dim)
1111 goto error;
1113 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1114 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1115 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1117 if (dim && left->tuple_id[0] &&
1118 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1119 goto error;
1120 if (dim && right->tuple_id[1] &&
1121 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1122 goto error;
1123 if (dim && left->nested[0] &&
1124 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1125 goto error;
1126 if (dim && right->nested[1] &&
1127 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1128 goto error;
1130 isl_space_free(left);
1131 isl_space_free(right);
1133 return dim;
1134 error:
1135 isl_space_free(left);
1136 isl_space_free(right);
1137 return NULL;
1140 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1141 __isl_take isl_space *right)
1143 isl_space *dom1, *dom2, *nest1, *nest2;
1145 if (!left || !right)
1146 goto error;
1148 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1149 goto error);
1151 dom1 = isl_space_domain(isl_space_copy(left));
1152 dom2 = isl_space_domain(isl_space_copy(right));
1153 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1155 dom1 = isl_space_range(left);
1156 dom2 = isl_space_range(right);
1157 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1159 return isl_space_join(isl_space_reverse(nest1), nest2);
1160 error:
1161 isl_space_free(left);
1162 isl_space_free(right);
1163 return NULL;
1166 /* Given two spaces { A -> C } and { B -> C }, construct the space
1167 * { [A -> B] -> C }
1169 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1170 __isl_take isl_space *right)
1172 isl_space *ran, *dom1, *dom2, *nest;
1174 if (!left || !right)
1175 goto error;
1177 if (!match(left, isl_dim_param, right, isl_dim_param))
1178 isl_die(left->ctx, isl_error_invalid,
1179 "parameters need to match", goto error);
1180 if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1181 isl_die(left->ctx, isl_error_invalid,
1182 "ranges need to match", goto error);
1184 ran = isl_space_range(isl_space_copy(left));
1186 dom1 = isl_space_domain(left);
1187 dom2 = isl_space_domain(right);
1188 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1190 return isl_space_join(isl_space_reverse(nest), ran);
1191 error:
1192 isl_space_free(left);
1193 isl_space_free(right);
1194 return NULL;
1197 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1198 __isl_take isl_space *right)
1200 isl_space *dom, *ran1, *ran2, *nest;
1202 if (!left || !right)
1203 goto error;
1205 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1206 goto error);
1207 if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1208 isl_die(left->ctx, isl_error_invalid,
1209 "domains need to match", goto error);
1211 dom = isl_space_domain(isl_space_copy(left));
1213 ran1 = isl_space_range(left);
1214 ran2 = isl_space_range(right);
1215 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1217 return isl_space_join(isl_space_reverse(dom), nest);
1218 error:
1219 isl_space_free(left);
1220 isl_space_free(right);
1221 return NULL;
1224 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1226 __isl_give isl_space *isl_space_domain_factor_domain(
1227 __isl_take isl_space *space)
1229 isl_space *nested;
1230 isl_space *domain;
1232 if (!space)
1233 return NULL;
1234 if (!isl_space_domain_is_wrapping(space))
1235 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1236 "domain not a product", return isl_space_free(space));
1238 nested = space->nested[0];
1239 domain = isl_space_copy(space);
1240 domain = isl_space_drop_dims(space, isl_dim_in,
1241 nested->n_in, nested->n_out);
1242 if (!domain)
1243 return isl_space_free(space);
1244 if (nested->tuple_id[0]) {
1245 domain->tuple_id[0] = isl_id_copy(nested->tuple_id[0]);
1246 if (!domain->tuple_id[0])
1247 goto error;
1249 if (nested->nested[0]) {
1250 domain->nested[0] = isl_space_copy(nested->nested[0]);
1251 if (!domain->nested[0])
1252 goto error;
1255 isl_space_free(space);
1256 return domain;
1257 error:
1258 isl_space_free(space);
1259 isl_space_free(domain);
1260 return NULL;
1263 /* Given a space of the form A -> [B -> C], return the space A -> B.
1265 __isl_give isl_space *isl_space_range_factor_domain(
1266 __isl_take isl_space *space)
1268 isl_space *nested;
1269 isl_space *domain;
1271 if (!space)
1272 return NULL;
1273 if (!isl_space_range_is_wrapping(space))
1274 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1275 "range not a product", return isl_space_free(space));
1277 nested = space->nested[1];
1278 domain = isl_space_copy(space);
1279 domain = isl_space_drop_dims(space, isl_dim_out,
1280 nested->n_in, nested->n_out);
1281 if (!domain)
1282 return isl_space_free(space);
1283 if (nested->tuple_id[0]) {
1284 domain->tuple_id[1] = isl_id_copy(nested->tuple_id[0]);
1285 if (!domain->tuple_id[1])
1286 goto error;
1288 if (nested->nested[0]) {
1289 domain->nested[1] = isl_space_copy(nested->nested[0]);
1290 if (!domain->nested[1])
1291 goto error;
1294 isl_space_free(space);
1295 return domain;
1296 error:
1297 isl_space_free(space);
1298 isl_space_free(domain);
1299 return NULL;
1302 /* Given a space of the form A -> [B -> C], return the space A -> C.
1304 __isl_give isl_space *isl_space_range_factor_range(
1305 __isl_take isl_space *space)
1307 isl_space *nested;
1308 isl_space *range;
1310 if (!space)
1311 return NULL;
1312 if (!isl_space_range_is_wrapping(space))
1313 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1314 "range not a product", return isl_space_free(space));
1316 nested = space->nested[1];
1317 range = isl_space_copy(space);
1318 range = isl_space_drop_dims(space, isl_dim_out, 0, nested->n_in);
1319 if (!range)
1320 return isl_space_free(space);
1321 if (nested->tuple_id[1]) {
1322 range->tuple_id[1] = isl_id_copy(nested->tuple_id[1]);
1323 if (!range->tuple_id[1])
1324 goto error;
1326 if (nested->nested[1]) {
1327 range->nested[1] = isl_space_copy(nested->nested[1]);
1328 if (!range->nested[1])
1329 goto error;
1332 isl_space_free(space);
1333 return range;
1334 error:
1335 isl_space_free(space);
1336 isl_space_free(range);
1337 return NULL;
1340 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1342 isl_ctx *ctx;
1343 isl_id **ids = NULL;
1345 if (!dim)
1346 return NULL;
1347 ctx = isl_space_get_ctx(dim);
1348 if (!isl_space_is_set(dim))
1349 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1350 dim = isl_space_cow(dim);
1351 if (!dim)
1352 return NULL;
1353 if (dim->ids) {
1354 ids = isl_calloc_array(dim->ctx, isl_id *,
1355 dim->nparam + dim->n_out + dim->n_out);
1356 if (!ids)
1357 goto error;
1358 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1359 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1361 dim->n_in = dim->n_out;
1362 if (ids) {
1363 free(dim->ids);
1364 dim->ids = ids;
1365 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1366 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1368 isl_id_free(dim->tuple_id[0]);
1369 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1370 isl_space_free(dim->nested[0]);
1371 dim->nested[0] = isl_space_copy(dim->nested[1]);
1372 return dim;
1373 error:
1374 isl_space_free(dim);
1375 return NULL;
1378 __isl_give isl_space *isl_space_map_from_domain_and_range(
1379 __isl_take isl_space *domain, __isl_take isl_space *range)
1381 if (!domain || !range)
1382 goto error;
1383 if (!isl_space_is_set(domain))
1384 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1385 "domain is not a set space", goto error);
1386 if (!isl_space_is_set(range))
1387 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1388 "range is not a set space", goto error);
1389 return isl_space_join(isl_space_reverse(domain), range);
1390 error:
1391 isl_space_free(domain);
1392 isl_space_free(range);
1393 return NULL;
1396 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1397 enum isl_dim_type type,
1398 unsigned first, unsigned n, __isl_take isl_id **ids)
1400 int i;
1402 for (i = 0; i < n ; ++i)
1403 dim = set_id(dim, type, first + i, ids[i]);
1405 return dim;
1408 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1410 unsigned t;
1411 isl_space *nested;
1412 isl_id **ids = NULL;
1413 isl_id *id;
1415 if (!dim)
1416 return NULL;
1417 if (match(dim, isl_dim_in, dim, isl_dim_out))
1418 return dim;
1420 dim = isl_space_cow(dim);
1421 if (!dim)
1422 return NULL;
1424 id = dim->tuple_id[0];
1425 dim->tuple_id[0] = dim->tuple_id[1];
1426 dim->tuple_id[1] = id;
1428 nested = dim->nested[0];
1429 dim->nested[0] = dim->nested[1];
1430 dim->nested[1] = nested;
1432 if (dim->ids) {
1433 int n_id = dim->n_in + dim->n_out;
1434 ids = isl_alloc_array(dim->ctx, isl_id *, n_id);
1435 if (n_id && !ids)
1436 goto error;
1437 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1438 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1441 t = dim->n_in;
1442 dim->n_in = dim->n_out;
1443 dim->n_out = t;
1445 if (dim->ids) {
1446 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1447 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1448 free(ids);
1451 return dim;
1452 error:
1453 free(ids);
1454 isl_space_free(dim);
1455 return NULL;
1458 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1459 enum isl_dim_type type, unsigned first, unsigned num)
1461 int i;
1463 if (!dim)
1464 return NULL;
1466 if (num == 0)
1467 return isl_space_reset(dim, type);
1469 if (!valid_dim_type(type))
1470 isl_die(dim->ctx, isl_error_invalid,
1471 "cannot drop dimensions of specified type", goto error);
1473 if (first + num > n(dim, type) || first + num < first)
1474 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1475 "index out of bounds", return isl_space_free(dim));
1476 dim = isl_space_cow(dim);
1477 if (!dim)
1478 goto error;
1479 if (dim->ids) {
1480 dim = extend_ids(dim);
1481 if (!dim)
1482 goto error;
1483 for (i = 0; i < num; ++i)
1484 isl_id_free(get_id(dim, type, first + i));
1485 for (i = first+num; i < n(dim, type); ++i)
1486 set_id(dim, type, i - num, get_id(dim, type, i));
1487 switch (type) {
1488 case isl_dim_param:
1489 get_ids(dim, isl_dim_in, 0, dim->n_in,
1490 dim->ids + offset(dim, isl_dim_in) - num);
1491 case isl_dim_in:
1492 get_ids(dim, isl_dim_out, 0, dim->n_out,
1493 dim->ids + offset(dim, isl_dim_out) - num);
1494 default:
1497 dim->n_id -= num;
1499 switch (type) {
1500 case isl_dim_param: dim->nparam -= num; break;
1501 case isl_dim_in: dim->n_in -= num; break;
1502 case isl_dim_out: dim->n_out -= num; break;
1503 default: ;
1505 dim = isl_space_reset(dim, type);
1506 if (type == isl_dim_param) {
1507 if (dim && dim->nested[0] &&
1508 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1509 isl_dim_param, first, num)))
1510 goto error;
1511 if (dim && dim->nested[1] &&
1512 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1513 isl_dim_param, first, num)))
1514 goto error;
1516 return dim;
1517 error:
1518 isl_space_free(dim);
1519 return NULL;
1522 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1523 unsigned first, unsigned n)
1525 if (!dim)
1526 return NULL;
1527 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1530 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1531 unsigned first, unsigned n)
1533 if (!dim)
1534 return NULL;
1535 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1538 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1540 if (!dim)
1541 return NULL;
1542 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1543 dim = isl_space_reverse(dim);
1544 dim = mark_as_set(dim);
1545 return dim;
1548 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1550 if (!dim)
1551 return NULL;
1552 if (!isl_space_is_set(dim))
1553 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1554 "not a set space", goto error);
1555 dim = isl_space_reverse(dim);
1556 dim = isl_space_reset(dim, isl_dim_out);
1557 return dim;
1558 error:
1559 isl_space_free(dim);
1560 return NULL;
1563 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1565 if (!dim)
1566 return NULL;
1567 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1568 dim = mark_as_set(dim);
1569 return dim;
1572 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1574 if (!dim)
1575 return NULL;
1576 if (!isl_space_is_set(dim))
1577 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1578 "not a set space", goto error);
1579 return isl_space_reset(dim, isl_dim_in);
1580 error:
1581 isl_space_free(dim);
1582 return NULL;
1585 /* Given a map space A -> B, return the map space [A -> B] -> A.
1587 __isl_give isl_space *isl_space_domain_map(__isl_take isl_space *space)
1589 isl_space *domain;
1591 domain = isl_space_from_range(isl_space_domain(isl_space_copy(space)));
1592 space = isl_space_from_domain(isl_space_wrap(space));
1593 space = isl_space_join(space, domain);
1595 return space;
1598 /* Given a map space A -> B, return the map space [A -> B] -> B.
1600 __isl_give isl_space *isl_space_range_map(__isl_take isl_space *space)
1602 isl_space *range;
1604 range = isl_space_from_range(isl_space_range(isl_space_copy(space)));
1605 space = isl_space_from_domain(isl_space_wrap(space));
1606 space = isl_space_join(space, range);
1608 return space;
1611 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1613 if (isl_space_is_params(space))
1614 return space;
1615 space = isl_space_drop_dims(space,
1616 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1617 space = isl_space_drop_dims(space,
1618 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1619 space = mark_as_params(space);
1620 return space;
1623 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1625 if (!space)
1626 return NULL;
1627 if (!isl_space_is_params(space))
1628 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1629 "not a parameter space", goto error);
1630 return isl_space_reset(space, isl_dim_set);
1631 error:
1632 isl_space_free(space);
1633 return NULL;
1636 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1638 dim = isl_space_cow(dim);
1639 if (!dim)
1640 return NULL;
1642 dim->n_out += dim->n_in;
1643 dim->n_in = 0;
1644 dim = isl_space_reset(dim, isl_dim_in);
1645 dim = isl_space_reset(dim, isl_dim_out);
1647 return dim;
1650 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1651 unsigned n_div)
1653 int i;
1655 if (!dim)
1656 return NULL;
1657 if (n_div == 0 &&
1658 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1659 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1660 dim = isl_space_cow(dim);
1661 if (!dim)
1662 return NULL;
1663 dim->n_out += dim->nparam + dim->n_in + n_div;
1664 dim->nparam = 0;
1665 dim->n_in = 0;
1667 for (i = 0; i < dim->n_id; ++i)
1668 isl_id_free(get_id(dim, isl_dim_out, i));
1669 dim->n_id = 0;
1670 dim = isl_space_reset(dim, isl_dim_in);
1671 dim = isl_space_reset(dim, isl_dim_out);
1673 return dim;
1676 /* Are the two spaces the same, including positions and names of parameters?
1678 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1680 if (!dim1 || !dim2)
1681 return -1;
1682 if (dim1 == dim2)
1683 return 1;
1684 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1685 isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1686 isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1689 /* Is space1 equal to the domain of space2?
1691 * In the internal version we also allow space2 to be the space of a set,
1692 * provided space1 is a parameter space.
1694 int isl_space_is_domain_internal(__isl_keep isl_space *space1,
1695 __isl_keep isl_space *space2)
1697 if (!space1 || !space2)
1698 return -1;
1699 if (!isl_space_is_set(space1))
1700 return 0;
1701 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1702 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1705 /* Is space1 equal to the domain of space2?
1707 int isl_space_is_domain(__isl_keep isl_space *space1,
1708 __isl_keep isl_space *space2)
1710 if (!space2)
1711 return -1;
1712 if (!isl_space_is_map(space2))
1713 return 0;
1714 return isl_space_is_domain_internal(space1, space2);
1717 /* Is space1 equal to the range of space2?
1719 * In the internal version, space2 is allowed to be the space of a set,
1720 * in which case it should be equal to space1.
1722 int isl_space_is_range_internal(__isl_keep isl_space *space1,
1723 __isl_keep isl_space *space2)
1725 if (!space1 || !space2)
1726 return -1;
1727 if (!isl_space_is_set(space1))
1728 return 0;
1729 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1730 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_out);
1733 /* Is space1 equal to the range of space2?
1735 int isl_space_is_range(__isl_keep isl_space *space1,
1736 __isl_keep isl_space *space2)
1738 if (!space2)
1739 return -1;
1740 if (!isl_space_is_map(space2))
1741 return 0;
1742 return isl_space_is_range_internal(space1, space2);
1745 int isl_space_compatible(__isl_keep isl_space *dim1,
1746 __isl_keep isl_space *dim2)
1748 return dim1->nparam == dim2->nparam &&
1749 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1752 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1754 int i;
1755 isl_id *id;
1757 if (!dim)
1758 return hash;
1760 isl_hash_byte(hash, dim->nparam % 256);
1761 isl_hash_byte(hash, dim->n_in % 256);
1762 isl_hash_byte(hash, dim->n_out % 256);
1764 for (i = 0; i < dim->nparam; ++i) {
1765 id = get_id(dim, isl_dim_param, i);
1766 hash = isl_hash_id(hash, id);
1769 id = tuple_id(dim, isl_dim_in);
1770 hash = isl_hash_id(hash, id);
1771 id = tuple_id(dim, isl_dim_out);
1772 hash = isl_hash_id(hash, id);
1774 hash = isl_hash_dim(hash, dim->nested[0]);
1775 hash = isl_hash_dim(hash, dim->nested[1]);
1777 return hash;
1780 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1782 uint32_t hash;
1784 if (!dim)
1785 return 0;
1787 hash = isl_hash_init();
1788 hash = isl_hash_dim(hash, dim);
1790 return hash;
1793 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1795 if (!dim)
1796 return -1;
1798 if (!isl_space_is_set(dim))
1799 return 0;
1801 return dim->nested[1] != NULL;
1804 /* Is "space" the space of a map where the domain is a wrapped map space?
1806 int isl_space_domain_is_wrapping(__isl_keep isl_space *space)
1808 if (!space)
1809 return -1;
1811 if (isl_space_is_set(space))
1812 return 0;
1814 return space->nested[0] != NULL;
1817 /* Is "space" the space of a map where the range is a wrapped map space?
1819 int isl_space_range_is_wrapping(__isl_keep isl_space *space)
1821 if (!space)
1822 return -1;
1824 if (isl_space_is_set(space))
1825 return 0;
1827 return space->nested[1] != NULL;
1830 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1832 isl_space *wrap;
1834 if (!dim)
1835 return NULL;
1837 wrap = isl_space_set_alloc(dim->ctx,
1838 dim->nparam, dim->n_in + dim->n_out);
1840 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1841 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1842 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1844 if (!wrap)
1845 goto error;
1847 wrap->nested[1] = dim;
1849 return wrap;
1850 error:
1851 isl_space_free(dim);
1852 return NULL;
1855 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1857 isl_space *unwrap;
1859 if (!dim)
1860 return NULL;
1862 if (!isl_space_is_wrapping(dim))
1863 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
1864 goto error);
1866 unwrap = isl_space_copy(dim->nested[1]);
1867 isl_space_free(dim);
1869 return unwrap;
1870 error:
1871 isl_space_free(dim);
1872 return NULL;
1875 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1877 if (type != isl_dim_in && type != isl_dim_out)
1878 return 0;
1879 if (!dim)
1880 return -1;
1881 if (dim->tuple_id[type - isl_dim_in])
1882 return 1;
1883 if (dim->nested[type - isl_dim_in])
1884 return 1;
1885 return 0;
1888 int isl_space_may_be_set(__isl_keep isl_space *dim)
1890 if (!dim)
1891 return -1;
1892 if (isl_space_is_set(dim))
1893 return 1;
1894 if (isl_space_dim(dim, isl_dim_in) != 0)
1895 return 0;
1896 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1897 return 0;
1898 return 1;
1901 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1902 enum isl_dim_type type)
1904 if (!isl_space_is_named_or_nested(dim, type))
1905 return dim;
1907 dim = isl_space_cow(dim);
1908 if (!dim)
1909 return NULL;
1911 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1912 dim->tuple_id[type - isl_dim_in] = NULL;
1913 isl_space_free(dim->nested[type - isl_dim_in]);
1914 dim->nested[type - isl_dim_in] = NULL;
1916 return dim;
1919 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1921 if (!dim)
1922 return NULL;
1923 if (!dim->nested[0] && !dim->nested[1])
1924 return dim;
1926 if (dim->nested[0])
1927 dim = isl_space_reset(dim, isl_dim_in);
1928 if (dim && dim->nested[1])
1929 dim = isl_space_reset(dim, isl_dim_out);
1931 return dim;
1934 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1936 if (!dim)
1937 return NULL;
1938 if (!dim->nested[0])
1939 return dim;
1941 return isl_space_reset(dim, isl_dim_in);
1944 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1946 if (!dim)
1947 return NULL;
1948 if (!dim->nested[1])
1949 return dim;
1951 return isl_space_reset(dim, isl_dim_out);
1954 /* Replace the dimensions of the given type of dst by those of src.
1956 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1957 enum isl_dim_type type, __isl_keep isl_space *src)
1959 dst = isl_space_cow(dst);
1961 if (!dst || !src)
1962 goto error;
1964 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1965 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1966 dst = copy_ids(dst, type, 0, src, type);
1968 if (dst && type == isl_dim_param) {
1969 int i;
1970 for (i = 0; i <= 1; ++i) {
1971 if (!dst->nested[i])
1972 continue;
1973 dst->nested[i] = isl_space_replace(dst->nested[i],
1974 type, src);
1975 if (!dst->nested[i])
1976 goto error;
1980 return dst;
1981 error:
1982 isl_space_free(dst);
1983 return NULL;
1986 /* Given a dimension specification "dim" of a set, create a dimension
1987 * specification for the lift of the set. In particular, the result
1988 * is of the form [dim -> local[..]], with n_local variables in the
1989 * range of the wrapped map.
1991 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1993 isl_space *local_dim;
1995 if (!dim)
1996 return NULL;
1998 local_dim = isl_space_dup(dim);
1999 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
2000 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
2001 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
2002 dim = isl_space_join(isl_space_from_domain(dim),
2003 isl_space_from_range(local_dim));
2004 dim = isl_space_wrap(dim);
2005 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
2007 return dim;
2010 int isl_space_can_zip(__isl_keep isl_space *dim)
2012 if (!dim)
2013 return -1;
2015 return dim->nested[0] && dim->nested[1];
2018 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
2020 isl_space *dom, *ran;
2021 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
2023 if (!isl_space_can_zip(dim))
2024 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
2025 goto error);
2027 if (!dim)
2028 return NULL;
2029 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
2030 ran = isl_space_unwrap(isl_space_range(dim));
2031 dom_dom = isl_space_domain(isl_space_copy(dom));
2032 dom_ran = isl_space_range(dom);
2033 ran_dom = isl_space_domain(isl_space_copy(ran));
2034 ran_ran = isl_space_range(ran);
2035 dom = isl_space_join(isl_space_from_domain(dom_dom),
2036 isl_space_from_range(ran_dom));
2037 ran = isl_space_join(isl_space_from_domain(dom_ran),
2038 isl_space_from_range(ran_ran));
2039 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2040 isl_space_from_range(isl_space_wrap(ran)));
2041 error:
2042 isl_space_free(dim);
2043 return NULL;
2046 /* Can we apply isl_space_curry to "space"?
2047 * That is, does it have a nested relation in its domain?
2049 int isl_space_can_curry(__isl_keep isl_space *space)
2051 if (!space)
2052 return -1;
2054 return !!space->nested[0];
2057 /* Given a space (A -> B) -> C, return the corresponding space
2058 * A -> (B -> C).
2060 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
2062 isl_space *dom, *ran;
2063 isl_space *dom_dom, *dom_ran;
2065 if (!space)
2066 return NULL;
2068 if (!isl_space_can_curry(space))
2069 isl_die(space->ctx, isl_error_invalid,
2070 "space cannot be curried", goto error);
2072 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
2073 ran = isl_space_range(space);
2074 dom_dom = isl_space_domain(isl_space_copy(dom));
2075 dom_ran = isl_space_range(dom);
2076 ran = isl_space_join(isl_space_from_domain(dom_ran),
2077 isl_space_from_range(ran));
2078 return isl_space_join(isl_space_from_domain(dom_dom),
2079 isl_space_from_range(isl_space_wrap(ran)));
2080 error:
2081 isl_space_free(space);
2082 return NULL;
2085 /* Can we apply isl_space_uncurry to "space"?
2086 * That is, does it have a nested relation in its range?
2088 int isl_space_can_uncurry(__isl_keep isl_space *space)
2090 if (!space)
2091 return -1;
2093 return !!space->nested[1];
2096 /* Given a space A -> (B -> C), return the corresponding space
2097 * (A -> B) -> C.
2099 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
2101 isl_space *dom, *ran;
2102 isl_space *ran_dom, *ran_ran;
2104 if (!space)
2105 return NULL;
2107 if (!isl_space_can_uncurry(space))
2108 isl_die(space->ctx, isl_error_invalid,
2109 "space cannot be uncurried",
2110 return isl_space_free(space));
2112 dom = isl_space_domain(isl_space_copy(space));
2113 ran = isl_space_unwrap(isl_space_range(space));
2114 ran_dom = isl_space_domain(isl_space_copy(ran));
2115 ran_ran = isl_space_range(ran);
2116 dom = isl_space_join(isl_space_from_domain(dom),
2117 isl_space_from_range(ran_dom));
2118 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
2119 isl_space_from_range(ran_ran));
2122 int isl_space_has_named_params(__isl_keep isl_space *dim)
2124 int i;
2125 unsigned off;
2127 if (!dim)
2128 return -1;
2129 if (dim->nparam == 0)
2130 return 1;
2131 off = isl_space_offset(dim, isl_dim_param);
2132 if (off + dim->nparam > dim->n_id)
2133 return 0;
2134 for (i = 0; i < dim->nparam; ++i)
2135 if (!dim->ids[off + i])
2136 return 0;
2137 return 1;
2140 /* Align the initial parameters of dim1 to match the order in dim2.
2142 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
2143 __isl_take isl_space *dim2)
2145 isl_reordering *exp;
2147 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
2148 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
2149 "parameter alignment requires named parameters",
2150 goto error);
2152 dim2 = isl_space_params(dim2);
2153 exp = isl_parameter_alignment_reordering(dim1, dim2);
2154 exp = isl_reordering_extend_space(exp, dim1);
2155 isl_space_free(dim2);
2156 if (!exp)
2157 return NULL;
2158 dim1 = isl_space_copy(exp->dim);
2159 isl_reordering_free(exp);
2160 return dim1;
2161 error:
2162 isl_space_free(dim1);
2163 isl_space_free(dim2);
2164 return NULL;
2167 /* Given the space of set (domain), construct a space for a map
2168 * with as domain the given space and as range the range of "model".
2170 __isl_give isl_space *isl_space_extend_domain_with_range(
2171 __isl_take isl_space *space, __isl_take isl_space *model)
2173 if (!model)
2174 goto error;
2176 space = isl_space_from_domain(space);
2177 space = isl_space_add_dims(space, isl_dim_out,
2178 isl_space_dim(model, isl_dim_out));
2179 if (isl_space_has_tuple_id(model, isl_dim_out))
2180 space = isl_space_set_tuple_id(space, isl_dim_out,
2181 isl_space_get_tuple_id(model, isl_dim_out));
2182 if (!space)
2183 goto error;
2184 if (model->nested[1]) {
2185 isl_space *nested = isl_space_copy(model->nested[1]);
2186 int n_nested, n_space;
2187 nested = isl_space_align_params(nested, isl_space_copy(space));
2188 n_nested = isl_space_dim(nested, isl_dim_param);
2189 n_space = isl_space_dim(space, isl_dim_param);
2190 if (n_nested > n_space)
2191 nested = isl_space_drop_dims(nested, isl_dim_param,
2192 n_space, n_nested - n_space);
2193 if (!nested)
2194 goto error;
2195 space->nested[1] = nested;
2197 isl_space_free(model);
2198 return space;
2199 error:
2200 isl_space_free(model);
2201 isl_space_free(space);
2202 return NULL;