2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the MIT 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
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)
28 dim = isl_alloc_type(ctx, struct isl_space);
39 dim->tuple_id[0] = NULL;
40 dim->tuple_id[1] = NULL;
42 dim->nested[0] = NULL;
43 dim->nested[1] = NULL;
51 /* Mark the space as being that of a set, by setting the domain tuple
54 static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
56 space = isl_space_cow(space);
59 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
63 /* Is the space that of a set?
65 int isl_space_is_set(__isl_keep isl_space *space)
69 if (space->n_in != 0 || space->nested[0])
71 if (space->tuple_id[0] != &isl_id_none)
76 /* Is the given space that of a map?
78 int isl_space_is_map(__isl_keep isl_space *space)
82 return space->tuple_id[0] != &isl_id_none &&
83 space->tuple_id[1] != &isl_id_none;
86 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
87 unsigned nparam, unsigned dim)
90 space = isl_space_alloc(ctx, nparam, 0, dim);
91 space = mark_as_set(space);
95 /* Mark the space as being that of a parameter domain, by setting
96 * both tuples to isl_id_none.
98 static __isl_give isl_space *mark_as_params(isl_space *space)
102 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
103 space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
107 /* Is the space that of a parameter domain?
109 int isl_space_is_params(__isl_keep isl_space *space)
113 if (space->n_in != 0 || space->nested[0] ||
114 space->n_out != 0 || space->nested[1])
116 if (space->tuple_id[0] != &isl_id_none)
118 if (space->tuple_id[1] != &isl_id_none)
123 /* Create a space for a parameter domain.
125 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
128 space = isl_space_alloc(ctx, nparam, 0, 0);
129 space = mark_as_params(space);
133 static unsigned global_pos(__isl_keep isl_space *dim,
134 enum isl_dim_type type, unsigned pos)
136 struct isl_ctx *ctx = dim->ctx;
140 isl_assert(ctx, pos < dim->nparam,
141 return isl_space_dim(dim, isl_dim_all));
144 isl_assert(ctx, pos < dim->n_in,
145 return isl_space_dim(dim, isl_dim_all));
146 return pos + dim->nparam;
148 isl_assert(ctx, pos < dim->n_out,
149 return isl_space_dim(dim, isl_dim_all));
150 return pos + dim->nparam + dim->n_in;
152 isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
154 return isl_space_dim(dim, isl_dim_all);
157 /* Extend length of ids array to the total number of dimensions.
159 static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
164 if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
168 dim->ids = isl_calloc_array(dim->ctx,
169 isl_id *, isl_space_dim(dim, isl_dim_all));
173 ids = isl_realloc_array(dim->ctx, dim->ids,
174 isl_id *, isl_space_dim(dim, isl_dim_all));
178 for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); ++i)
182 dim->n_id = isl_space_dim(dim, isl_dim_all);
190 static __isl_give isl_space *set_id(__isl_take isl_space *dim,
191 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
193 dim = isl_space_cow(dim);
198 pos = global_pos(dim, type, pos);
199 if (pos == isl_space_dim(dim, isl_dim_all))
202 if (pos >= dim->n_id) {
205 dim = extend_ids(dim);
219 static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
220 enum isl_dim_type type, unsigned pos)
225 pos = global_pos(dim, type, pos);
226 if (pos == isl_space_dim(dim, isl_dim_all))
228 if (pos >= dim->n_id)
230 return dim->ids[pos];
233 static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
236 case isl_dim_param: return 0;
237 case isl_dim_in: return dim->nparam;
238 case isl_dim_out: return dim->nparam + dim->n_in;
243 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
246 case isl_dim_param: return dim->nparam;
247 case isl_dim_in: return dim->n_in;
248 case isl_dim_out: return dim->n_out;
249 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
254 unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
261 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
265 return offset(dim, type);
268 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
269 enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
270 enum isl_dim_type src_type)
278 for (i = 0; i < n(src, src_type); ++i) {
279 id = get_id(src, src_type, i);
282 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
289 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
294 dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
295 if (dim->tuple_id[0] &&
296 !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
298 if (dim->tuple_id[1] &&
299 !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
301 if (dim->nested[0] && !(dup->nested[0] = isl_space_copy(dim->nested[0])))
303 if (dim->nested[1] && !(dup->nested[1] = isl_space_copy(dim->nested[1])))
307 dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
308 dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
309 dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
316 __isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
324 return isl_space_dup(dim);
327 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
336 void isl_space_free(__isl_take isl_space *dim)
346 isl_id_free(dim->tuple_id[0]);
347 isl_id_free(dim->tuple_id[1]);
349 isl_space_free(dim->nested[0]);
350 isl_space_free(dim->nested[1]);
352 for (i = 0; i < dim->n_id; ++i)
353 isl_id_free(dim->ids[i]);
355 isl_ctx_deref(dim->ctx);
360 /* Check if "s" is a valid dimension or tuple name.
361 * We currently only forbid names that look like a number.
363 * s is assumed to be non-NULL.
365 static int name_ok(isl_ctx *ctx, const char *s)
370 dummy = strtol(s, &p, 0);
372 isl_die(ctx, isl_error_invalid, "name looks like a number",
378 /* Is it possible for the given dimension type to have a tuple id?
380 static int space_can_have_id(__isl_keep isl_space *space,
381 enum isl_dim_type type)
385 if (isl_space_is_params(space))
386 isl_die(space->ctx, isl_error_invalid,
387 "parameter spaces don't have tuple ids", return 0);
388 if (isl_space_is_set(space) && type != isl_dim_set)
389 isl_die(space->ctx, isl_error_invalid,
390 "set spaces can only have a set id", return 0);
391 if (type != isl_dim_in && type != isl_dim_out)
392 isl_die(space->ctx, isl_error_invalid,
393 "only input, output and set tuples can have ids",
399 /* Does the tuple have an id?
401 int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
403 if (!space_can_have_id(dim, type))
405 return dim->tuple_id[type - isl_dim_in] != NULL;
408 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
409 enum isl_dim_type type)
415 has_id = isl_space_has_tuple_id(dim, type);
419 isl_die(dim->ctx, isl_error_invalid,
420 "tuple has no id", return NULL);
421 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
424 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
425 enum isl_dim_type type, __isl_take isl_id *id)
427 dim = isl_space_cow(dim);
430 if (type != isl_dim_in && type != isl_dim_out)
431 isl_die(dim->ctx, isl_error_invalid,
432 "only input, output and set tuples can have names",
435 isl_id_free(dim->tuple_id[type - isl_dim_in]);
436 dim->tuple_id[type - isl_dim_in] = id;
445 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
446 enum isl_dim_type type)
448 dim = isl_space_cow(dim);
451 if (type != isl_dim_in && type != isl_dim_out)
452 isl_die(dim->ctx, isl_error_invalid,
453 "only input, output and set tuples can have names",
456 isl_id_free(dim->tuple_id[type - isl_dim_in]);
457 dim->tuple_id[type - isl_dim_in] = NULL;
465 /* Set the id of the given dimension of "space" to "id".
466 * If the dimension already has an id, then it is replaced.
467 * If the dimension is a parameter, then we need to change it
468 * in the nested spaces (if any) as well.
470 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
471 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
473 space = isl_space_cow(space);
477 if (type == isl_dim_param) {
480 for (i = 0; i < 2; ++i) {
481 if (!space->nested[i])
484 isl_space_set_dim_id(space->nested[i],
485 type, pos, isl_id_copy(id));
486 if (!space->nested[i])
491 isl_id_free(get_id(space, type, pos));
492 return set_id(space, type, pos, id);
495 isl_space_free(space);
499 /* Reset the id of the given dimension of "space".
500 * If the dimension already has an id, then it is removed.
501 * If the dimension is a parameter, then we need to reset it
502 * in the nested spaces (if any) as well.
504 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
505 enum isl_dim_type type, unsigned pos)
507 space = isl_space_cow(space);
511 if (type == isl_dim_param) {
514 for (i = 0; i < 2; ++i) {
515 if (!space->nested[i])
518 isl_space_reset_dim_id(space->nested[i],
520 if (!space->nested[i])
525 isl_id_free(get_id(space, type, pos));
526 return set_id(space, type, pos, NULL);
528 isl_space_free(space);
532 int isl_space_has_dim_id(__isl_keep isl_space *dim,
533 enum isl_dim_type type, unsigned pos)
537 return get_id(dim, type, pos) != NULL;
540 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
541 enum isl_dim_type type, unsigned pos)
545 if (!get_id(dim, type, pos))
546 isl_die(dim->ctx, isl_error_invalid,
547 "dim has no id", return NULL);
548 return isl_id_copy(get_id(dim, type, pos));
551 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
552 enum isl_dim_type type, const char *s)
560 return isl_space_reset_tuple_id(dim, type);
562 if (!name_ok(dim->ctx, s))
565 id = isl_id_alloc(dim->ctx, s, NULL);
566 return isl_space_set_tuple_id(dim, type, id);
572 /* Does the tuple have a name?
574 int isl_space_has_tuple_name(__isl_keep isl_space *space,
575 enum isl_dim_type type)
579 if (!space_can_have_id(space, type))
581 id = space->tuple_id[type - isl_dim_in];
582 return id && id->name;
585 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
586 enum isl_dim_type type)
591 if (type != isl_dim_in && type != isl_dim_out)
593 id = dim->tuple_id[type - isl_dim_in];
594 return id ? id->name : NULL;
597 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
598 enum isl_dim_type type, unsigned pos,
606 return isl_space_reset_dim_id(dim, type, pos);
607 if (!name_ok(dim->ctx, s))
609 id = isl_id_alloc(dim->ctx, s, NULL);
610 return isl_space_set_dim_id(dim, type, pos, id);
616 /* Does the given dimension have a name?
618 int isl_space_has_dim_name(__isl_keep isl_space *space,
619 enum isl_dim_type type, unsigned pos)
625 id = get_id(space, type, pos);
626 return id && id->name;
629 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
630 enum isl_dim_type type, unsigned pos)
632 isl_id *id = get_id(dim, type, pos);
633 return id ? id->name : NULL;
636 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
637 __isl_keep isl_id *id)
646 offset = isl_space_offset(dim, type);
647 n = isl_space_dim(dim, type);
648 for (i = 0; i < n && offset + i < dim->n_id; ++i)
649 if (dim->ids[offset + i] == id)
655 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
656 enum isl_dim_type type, const char *name)
665 offset = isl_space_offset(space, type);
666 n = isl_space_dim(space, type);
667 for (i = 0; i < n && offset + i < space->n_id; ++i)
668 if (space->ids[offset + i]->name &&
669 !strcmp(space->ids[offset + i]->name, name))
675 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
676 enum isl_dim_type type)
680 if (type == isl_dim_in)
681 return dim->tuple_id[0];
682 if (type == isl_dim_out)
683 return dim->tuple_id[1];
687 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
688 enum isl_dim_type type)
692 if (type == isl_dim_in)
693 return dim->nested[0];
694 if (type == isl_dim_out)
695 return dim->nested[1];
699 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
700 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
703 isl_space *nested1, *nested2;
708 if (dim1 == dim2 && dim1_type == dim2_type)
711 if (n(dim1, dim1_type) != n(dim2, dim2_type))
713 id1 = tuple_id(dim1, dim1_type);
714 id2 = tuple_id(dim2, dim2_type);
717 if (id1 && id1 != id2)
719 nested1 = nested(dim1, dim1_type);
720 nested2 = nested(dim2, dim2_type);
721 if (!nested1 ^ !nested2)
723 if (nested1 && !isl_space_is_equal(nested1, nested2))
728 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
729 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
733 if (dim1 == dim2 && dim1_type == dim2_type)
736 if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
739 if (!dim1->ids && !dim2->ids)
742 for (i = 0; i < n(dim1, dim1_type); ++i) {
743 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
749 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
750 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
755 return match(dim1, dim1_type, dim2, dim2_type);
758 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
759 unsigned first, unsigned n, __isl_keep isl_id **ids)
763 for (i = 0; i < n ; ++i)
764 ids[i] = get_id(dim, type, first + i);
767 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
768 unsigned nparam, unsigned n_in, unsigned n_out)
774 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
777 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
778 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
779 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
781 dim = isl_space_cow(dim);
784 ids = isl_calloc_array(dim->ctx, isl_id *,
785 nparam + n_in + n_out);
788 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
789 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
790 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
793 dim->n_id = nparam + n_in + n_out;
795 dim->nparam = nparam;
806 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
807 enum isl_dim_type type, unsigned n)
811 dim = isl_space_reset(dim, type);
814 dim = isl_space_extend(dim,
815 dim->nparam + n, dim->n_in, dim->n_out);
816 if (dim && dim->nested[0] &&
817 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
820 if (dim && dim->nested[1] &&
821 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
826 return isl_space_extend(dim,
827 dim->nparam, dim->n_in + n, dim->n_out);
829 return isl_space_extend(dim,
830 dim->nparam, dim->n_in, dim->n_out + n);
832 isl_die(dim->ctx, isl_error_invalid,
833 "cannot add dimensions of specified type", goto error);
840 static int valid_dim_type(enum isl_dim_type type)
852 /* Insert "n" dimensions of type "type" at position "pos".
853 * If we are inserting parameters, then they are also inserted in
856 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
857 enum isl_dim_type type, unsigned pos, unsigned n)
864 return isl_space_reset(dim, type);
866 if (!valid_dim_type(type))
867 isl_die(dim->ctx, isl_error_invalid,
868 "cannot insert dimensions of specified type",
871 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
873 dim = isl_space_cow(dim);
878 enum isl_dim_type t, o = isl_dim_param;
881 ids = isl_calloc_array(dim->ctx, isl_id *,
882 dim->nparam + dim->n_in + dim->n_out + n);
886 s[isl_dim_param - o] = dim->nparam;
887 s[isl_dim_in - o] = dim->n_in;
888 s[isl_dim_out - o] = dim->n_out;
889 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
891 get_ids(dim, t, 0, s[t - o], ids + off);
894 get_ids(dim, t, 0, pos, ids + off);
896 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
897 off += s[t - o] - pos;
902 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
905 case isl_dim_param: dim->nparam += n; break;
906 case isl_dim_in: dim->n_in += n; break;
907 case isl_dim_out: dim->n_out += n; break;
910 dim = isl_space_reset(dim, type);
912 if (type == isl_dim_param) {
913 if (dim && dim->nested[0] &&
914 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
915 isl_dim_param, pos, n)))
917 if (dim && dim->nested[1] &&
918 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
919 isl_dim_param, pos, n)))
929 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
930 enum isl_dim_type dst_type, unsigned dst_pos,
931 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
940 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
943 if (dst_type == src_type && dst_pos == src_pos)
946 isl_assert(dim->ctx, dst_type != src_type, goto error);
948 dim = isl_space_reset(dim, src_type);
949 dim = isl_space_reset(dim, dst_type);
951 dim = isl_space_cow(dim);
957 enum isl_dim_type t, o = isl_dim_param;
960 ids = isl_calloc_array(dim->ctx, isl_id *,
961 dim->nparam + dim->n_in + dim->n_out);
965 s[isl_dim_param - o] = dim->nparam;
966 s[isl_dim_in - o] = dim->n_in;
967 s[isl_dim_out - o] = dim->n_out;
968 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
970 get_ids(dim, t, 0, dst_pos, ids + off);
972 get_ids(dim, src_type, src_pos, n, ids + off);
974 get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
976 off += s[t - o] - dst_pos;
977 } else if (t == src_type) {
978 get_ids(dim, t, 0, src_pos, ids + off);
980 get_ids(dim, t, src_pos + n,
981 s[t - o] - src_pos - n, ids + off);
982 off += s[t - o] - src_pos - n;
984 get_ids(dim, t, 0, s[t - o], ids + off);
990 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
994 case isl_dim_param: dim->nparam += n; break;
995 case isl_dim_in: dim->n_in += n; break;
996 case isl_dim_out: dim->n_out += n; break;
1001 case isl_dim_param: dim->nparam -= n; break;
1002 case isl_dim_in: dim->n_in -= n; break;
1003 case isl_dim_out: dim->n_out -= n; break;
1007 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1010 for (i = 0; i < 2; ++i) {
1011 if (!dim->nested[i])
1013 dim->nested[i] = isl_space_replace(dim->nested[i],
1014 isl_dim_param, dim);
1015 if (!dim->nested[i])
1021 isl_space_free(dim);
1025 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1026 __isl_take isl_space *right)
1030 if (!left || !right)
1033 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1035 isl_assert(left->ctx,
1036 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
1039 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1043 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1044 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1045 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1047 if (dim && left->tuple_id[0] &&
1048 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1050 if (dim && right->tuple_id[1] &&
1051 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1053 if (dim && left->nested[0] &&
1054 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1056 if (dim && right->nested[1] &&
1057 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1060 isl_space_free(left);
1061 isl_space_free(right);
1065 isl_space_free(left);
1066 isl_space_free(right);
1070 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1071 __isl_take isl_space *right)
1073 isl_space *dom1, *dom2, *nest1, *nest2;
1075 if (!left || !right)
1078 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1081 dom1 = isl_space_domain(isl_space_copy(left));
1082 dom2 = isl_space_domain(isl_space_copy(right));
1083 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1085 dom1 = isl_space_range(left);
1086 dom2 = isl_space_range(right);
1087 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1089 return isl_space_join(isl_space_reverse(nest1), nest2);
1091 isl_space_free(left);
1092 isl_space_free(right);
1096 /* Given two spaces { A -> C } and { B -> C }, construct the space
1099 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1100 __isl_take isl_space *right)
1102 isl_space *ran, *dom1, *dom2, *nest;
1104 if (!left || !right)
1107 if (!match(left, isl_dim_param, right, isl_dim_param))
1108 isl_die(left->ctx, isl_error_invalid,
1109 "parameters need to match", goto error);
1110 if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1111 isl_die(left->ctx, isl_error_invalid,
1112 "ranges need to match", goto error);
1114 ran = isl_space_range(isl_space_copy(left));
1116 dom1 = isl_space_domain(left);
1117 dom2 = isl_space_domain(right);
1118 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1120 return isl_space_join(isl_space_reverse(nest), ran);
1122 isl_space_free(left);
1123 isl_space_free(right);
1127 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1128 __isl_take isl_space *right)
1130 isl_space *dom, *ran1, *ran2, *nest;
1132 if (!left || !right)
1135 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1137 if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1138 isl_die(left->ctx, isl_error_invalid,
1139 "domains need to match", goto error);
1141 dom = isl_space_domain(isl_space_copy(left));
1143 ran1 = isl_space_range(left);
1144 ran2 = isl_space_range(right);
1145 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1147 return isl_space_join(isl_space_reverse(dom), nest);
1149 isl_space_free(left);
1150 isl_space_free(right);
1154 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1157 isl_id **ids = NULL;
1161 ctx = isl_space_get_ctx(dim);
1162 if (!isl_space_is_set(dim))
1163 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1164 dim = isl_space_cow(dim);
1168 ids = isl_calloc_array(dim->ctx, isl_id *,
1169 dim->nparam + dim->n_out + dim->n_out);
1172 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1173 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1175 dim->n_in = dim->n_out;
1179 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1180 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1182 isl_id_free(dim->tuple_id[0]);
1183 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1184 isl_space_free(dim->nested[0]);
1185 dim->nested[0] = isl_space_copy(dim->nested[1]);
1188 isl_space_free(dim);
1192 __isl_give isl_space *isl_space_map_from_domain_and_range(
1193 __isl_take isl_space *domain, __isl_take isl_space *range)
1195 if (!domain || !range)
1197 if (!isl_space_is_set(domain))
1198 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1199 "domain is not a set space", goto error);
1200 if (!isl_space_is_set(range))
1201 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1202 "range is not a set space", goto error);
1203 return isl_space_join(isl_space_reverse(domain), range);
1205 isl_space_free(domain);
1206 isl_space_free(range);
1210 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1211 enum isl_dim_type type,
1212 unsigned first, unsigned n, __isl_take isl_id **ids)
1216 for (i = 0; i < n ; ++i)
1217 dim = set_id(dim, type, first + i, ids[i]);
1222 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1226 isl_id **ids = NULL;
1231 if (match(dim, isl_dim_in, dim, isl_dim_out))
1234 dim = isl_space_cow(dim);
1238 id = dim->tuple_id[0];
1239 dim->tuple_id[0] = dim->tuple_id[1];
1240 dim->tuple_id[1] = id;
1242 nested = dim->nested[0];
1243 dim->nested[0] = dim->nested[1];
1244 dim->nested[1] = nested;
1247 ids = isl_alloc_array(dim->ctx, isl_id *,
1248 dim->n_in + dim->n_out);
1251 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1252 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1256 dim->n_in = dim->n_out;
1260 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1261 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1268 isl_space_free(dim);
1272 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1273 enum isl_dim_type type, unsigned first, unsigned num)
1281 return isl_space_reset(dim, type);
1283 if (!valid_dim_type(type))
1284 isl_die(dim->ctx, isl_error_invalid,
1285 "cannot drop dimensions of specified type", goto error);
1287 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1288 dim = isl_space_cow(dim);
1292 dim = extend_ids(dim);
1295 for (i = 0; i < num; ++i)
1296 isl_id_free(get_id(dim, type, first + i));
1297 for (i = first+num; i < n(dim, type); ++i)
1298 set_id(dim, type, i - num, get_id(dim, type, i));
1301 get_ids(dim, isl_dim_in, 0, dim->n_in,
1302 dim->ids + offset(dim, isl_dim_in) - num);
1304 get_ids(dim, isl_dim_out, 0, dim->n_out,
1305 dim->ids + offset(dim, isl_dim_out) - num);
1312 case isl_dim_param: dim->nparam -= num; break;
1313 case isl_dim_in: dim->n_in -= num; break;
1314 case isl_dim_out: dim->n_out -= num; break;
1317 dim = isl_space_reset(dim, type);
1318 if (type == isl_dim_param) {
1319 if (dim && dim->nested[0] &&
1320 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1321 isl_dim_param, first, num)))
1323 if (dim && dim->nested[1] &&
1324 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1325 isl_dim_param, first, num)))
1330 isl_space_free(dim);
1334 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1335 unsigned first, unsigned n)
1339 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1342 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1343 unsigned first, unsigned n)
1347 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1350 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1354 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1355 dim = isl_space_reverse(dim);
1356 dim = mark_as_set(dim);
1360 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1364 if (!isl_space_is_set(dim))
1365 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1366 "not a set space", goto error);
1367 dim = isl_space_reverse(dim);
1368 dim = isl_space_reset(dim, isl_dim_out);
1371 isl_space_free(dim);
1375 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1379 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1380 dim = mark_as_set(dim);
1384 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1388 if (!isl_space_is_set(dim))
1389 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1390 "not a set space", goto error);
1391 return isl_space_reset(dim, isl_dim_in);
1393 isl_space_free(dim);
1397 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1399 if (isl_space_is_params(space))
1401 space = isl_space_drop_dims(space,
1402 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1403 space = isl_space_drop_dims(space,
1404 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1405 space = mark_as_params(space);
1409 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1413 if (!isl_space_is_params(space))
1414 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1415 "not a parameter space", goto error);
1416 return isl_space_reset(space, isl_dim_set);
1418 isl_space_free(space);
1422 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1424 dim = isl_space_cow(dim);
1428 dim->n_out += dim->n_in;
1430 dim = isl_space_reset(dim, isl_dim_in);
1431 dim = isl_space_reset(dim, isl_dim_out);
1436 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1444 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1445 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1446 dim = isl_space_cow(dim);
1449 dim->n_out += dim->nparam + dim->n_in + n_div;
1453 for (i = 0; i < dim->n_id; ++i)
1454 isl_id_free(get_id(dim, isl_dim_out, i));
1456 dim = isl_space_reset(dim, isl_dim_in);
1457 dim = isl_space_reset(dim, isl_dim_out);
1462 /* Are the two spaces the same, including positions and names of parameters?
1464 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1470 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1471 isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1472 isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1475 /* Is space1 equal to the domain of space2?
1477 * In the internal version we also allow space2 to be the space of a set,
1478 * provided space1 is a parameter space.
1480 int isl_space_is_domain_internal(__isl_keep isl_space *space1,
1481 __isl_keep isl_space *space2)
1483 if (!space1 || !space2)
1485 if (!isl_space_is_set(space1))
1487 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1488 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1491 /* Is space1 equal to the domain of space2?
1493 int isl_space_is_domain(__isl_keep isl_space *space1,
1494 __isl_keep isl_space *space2)
1498 if (!isl_space_is_map(space2))
1500 return isl_space_is_domain_internal(space1, space2);
1503 /* Is space1 equal to the range of space2?
1505 * In the internal version, space2 is allowed to be the space of a set,
1506 * in which case it should be equal to space1.
1508 int isl_space_is_range_internal(__isl_keep isl_space *space1,
1509 __isl_keep isl_space *space2)
1511 if (!space1 || !space2)
1513 if (!isl_space_is_set(space1))
1515 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1516 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_out);
1519 /* Is space1 equal to the range of space2?
1521 int isl_space_is_range(__isl_keep isl_space *space1,
1522 __isl_keep isl_space *space2)
1526 if (!isl_space_is_map(space2))
1528 return isl_space_is_range_internal(space1, space2);
1531 int isl_space_compatible(__isl_keep isl_space *dim1,
1532 __isl_keep isl_space *dim2)
1534 return dim1->nparam == dim2->nparam &&
1535 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1538 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1546 hash = isl_hash_builtin(hash, dim->nparam);
1547 hash = isl_hash_builtin(hash, dim->n_in);
1548 hash = isl_hash_builtin(hash, dim->n_out);
1550 for (i = 0; i < dim->nparam; ++i) {
1551 id = get_id(dim, isl_dim_param, i);
1552 hash = isl_hash_id(hash, id);
1555 id = tuple_id(dim, isl_dim_in);
1556 hash = isl_hash_id(hash, id);
1557 id = tuple_id(dim, isl_dim_out);
1558 hash = isl_hash_id(hash, id);
1560 hash = isl_hash_dim(hash, dim->nested[0]);
1561 hash = isl_hash_dim(hash, dim->nested[1]);
1566 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1573 hash = isl_hash_init();
1574 hash = isl_hash_dim(hash, dim);
1579 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1584 if (!isl_space_is_set(dim))
1587 return dim->nested[1] != NULL;
1590 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1597 wrap = isl_space_set_alloc(dim->ctx,
1598 dim->nparam, dim->n_in + dim->n_out);
1600 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1601 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1602 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1607 wrap->nested[1] = dim;
1611 isl_space_free(dim);
1615 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1622 if (!isl_space_is_wrapping(dim))
1623 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
1626 unwrap = isl_space_copy(dim->nested[1]);
1627 isl_space_free(dim);
1631 isl_space_free(dim);
1635 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1637 if (type != isl_dim_in && type != isl_dim_out)
1641 if (dim->tuple_id[type - isl_dim_in])
1643 if (dim->nested[type - isl_dim_in])
1648 int isl_space_may_be_set(__isl_keep isl_space *dim)
1652 if (isl_space_is_set(dim))
1654 if (isl_space_dim(dim, isl_dim_in) != 0)
1656 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1661 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1662 enum isl_dim_type type)
1664 if (!isl_space_is_named_or_nested(dim, type))
1667 dim = isl_space_cow(dim);
1671 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1672 dim->tuple_id[type - isl_dim_in] = NULL;
1673 isl_space_free(dim->nested[type - isl_dim_in]);
1674 dim->nested[type - isl_dim_in] = NULL;
1679 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1683 if (!dim->nested[0] && !dim->nested[1])
1687 dim = isl_space_reset(dim, isl_dim_in);
1688 if (dim && dim->nested[1])
1689 dim = isl_space_reset(dim, isl_dim_out);
1694 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1698 if (!dim->nested[0])
1701 return isl_space_reset(dim, isl_dim_in);
1704 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1708 if (!dim->nested[1])
1711 return isl_space_reset(dim, isl_dim_out);
1714 /* Replace the dimensions of the given type of dst by those of src.
1716 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1717 enum isl_dim_type type, __isl_keep isl_space *src)
1719 dst = isl_space_cow(dst);
1724 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1725 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1726 dst = copy_ids(dst, type, 0, src, type);
1728 if (dst && type == isl_dim_param) {
1730 for (i = 0; i <= 1; ++i) {
1731 if (!dst->nested[i])
1733 dst->nested[i] = isl_space_replace(dst->nested[i],
1735 if (!dst->nested[i])
1742 isl_space_free(dst);
1746 /* Given a dimension specification "dim" of a set, create a dimension
1747 * specification for the lift of the set. In particular, the result
1748 * is of the form [dim -> local[..]], with n_local variables in the
1749 * range of the wrapped map.
1751 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1753 isl_space *local_dim;
1758 local_dim = isl_space_dup(dim);
1759 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1760 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1761 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1762 dim = isl_space_join(isl_space_from_domain(dim),
1763 isl_space_from_range(local_dim));
1764 dim = isl_space_wrap(dim);
1765 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1770 int isl_space_can_zip(__isl_keep isl_space *dim)
1775 return dim->nested[0] && dim->nested[1];
1778 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1780 isl_space *dom, *ran;
1781 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1783 if (!isl_space_can_zip(dim))
1784 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1789 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1790 ran = isl_space_unwrap(isl_space_range(dim));
1791 dom_dom = isl_space_domain(isl_space_copy(dom));
1792 dom_ran = isl_space_range(dom);
1793 ran_dom = isl_space_domain(isl_space_copy(ran));
1794 ran_ran = isl_space_range(ran);
1795 dom = isl_space_join(isl_space_from_domain(dom_dom),
1796 isl_space_from_range(ran_dom));
1797 ran = isl_space_join(isl_space_from_domain(dom_ran),
1798 isl_space_from_range(ran_ran));
1799 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1800 isl_space_from_range(isl_space_wrap(ran)));
1802 isl_space_free(dim);
1806 /* Can we apply isl_space_curry to "space"?
1807 * That is, does it have a nested relation in its domain?
1809 int isl_space_can_curry(__isl_keep isl_space *space)
1814 return !!space->nested[0];
1817 /* Given a space (A -> B) -> C, return the corresponding space
1820 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
1822 isl_space *dom, *ran;
1823 isl_space *dom_dom, *dom_ran;
1828 if (!isl_space_can_curry(space))
1829 isl_die(space->ctx, isl_error_invalid,
1830 "space cannot be curried", goto error);
1832 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
1833 ran = isl_space_range(space);
1834 dom_dom = isl_space_domain(isl_space_copy(dom));
1835 dom_ran = isl_space_range(dom);
1836 ran = isl_space_join(isl_space_from_domain(dom_ran),
1837 isl_space_from_range(ran));
1838 return isl_space_join(isl_space_from_domain(dom_dom),
1839 isl_space_from_range(isl_space_wrap(ran)));
1841 isl_space_free(space);
1845 int isl_space_has_named_params(__isl_keep isl_space *dim)
1852 if (dim->nparam == 0)
1854 off = isl_space_offset(dim, isl_dim_param);
1855 if (off + dim->nparam > dim->n_id)
1857 for (i = 0; i < dim->nparam; ++i)
1858 if (!dim->ids[off + i])
1863 /* Align the initial parameters of dim1 to match the order in dim2.
1865 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1866 __isl_take isl_space *dim2)
1868 isl_reordering *exp;
1870 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1871 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1872 "parameter alignment requires named parameters",
1875 dim2 = isl_space_params(dim2);
1876 exp = isl_parameter_alignment_reordering(dim1, dim2);
1877 exp = isl_reordering_extend_space(exp, dim1);
1878 isl_space_free(dim2);
1881 dim1 = isl_space_copy(exp->dim);
1882 isl_reordering_free(exp);
1885 isl_space_free(dim1);
1886 isl_space_free(dim2);
1890 /* Given the space of set (domain), construct a space for a map
1891 * with as domain the given space and as range the range of "model".
1893 __isl_give isl_space *isl_space_extend_domain_with_range(
1894 __isl_take isl_space *space, __isl_take isl_space *model)
1899 space = isl_space_from_domain(space);
1900 space = isl_space_add_dims(space, isl_dim_out,
1901 isl_space_dim(model, isl_dim_out));
1902 if (isl_space_has_tuple_id(model, isl_dim_out))
1903 space = isl_space_set_tuple_id(space, isl_dim_out,
1904 isl_space_get_tuple_id(model, isl_dim_out));
1907 if (model->nested[1]) {
1908 isl_space *nested = isl_space_copy(model->nested[1]);
1909 int n_nested, n_space;
1910 nested = isl_space_align_params(nested, isl_space_copy(space));
1911 n_nested = isl_space_dim(nested, isl_dim_param);
1912 n_space = isl_space_dim(space, isl_dim_param);
1913 if (n_nested > n_space)
1914 nested = isl_space_drop_dims(nested, isl_dim_param,
1915 n_space, n_nested - n_space);
1918 space->nested[1] = nested;
1920 isl_space_free(model);
1923 isl_space_free(model);
1924 isl_space_free(space);