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
15 #include <isl_space_private.h>
16 #include <isl_id_private.h>
17 #include <isl_reordering.h>
19 isl_ctx *isl_space_get_ctx(__isl_keep isl_space *dim)
21 return dim ? dim->ctx : NULL;
24 __isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
25 unsigned nparam, unsigned n_in, unsigned n_out)
29 dim = isl_alloc_type(ctx, struct isl_space);
40 dim->tuple_id[0] = NULL;
41 dim->tuple_id[1] = NULL;
43 dim->nested[0] = NULL;
44 dim->nested[1] = NULL;
52 /* Mark the space as being that of a set, by setting the domain tuple
55 static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
57 space = isl_space_cow(space);
60 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
64 /* Is the space that of a set?
66 int isl_space_is_set(__isl_keep isl_space *space)
70 if (space->n_in != 0 || space->nested[0])
72 if (space->tuple_id[0] != &isl_id_none)
77 /* Is the given space that of a map?
79 int isl_space_is_map(__isl_keep isl_space *space)
83 return space->tuple_id[0] != &isl_id_none &&
84 space->tuple_id[1] != &isl_id_none;
87 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
88 unsigned nparam, unsigned dim)
91 space = isl_space_alloc(ctx, nparam, 0, dim);
92 space = mark_as_set(space);
96 /* Mark the space as being that of a parameter domain, by setting
97 * both tuples to isl_id_none.
99 static __isl_give isl_space *mark_as_params(isl_space *space)
103 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
104 space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
108 /* Is the space that of a parameter domain?
110 int isl_space_is_params(__isl_keep isl_space *space)
114 if (space->n_in != 0 || space->nested[0] ||
115 space->n_out != 0 || space->nested[1])
117 if (space->tuple_id[0] != &isl_id_none)
119 if (space->tuple_id[1] != &isl_id_none)
124 /* Create a space for a parameter domain.
126 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
129 space = isl_space_alloc(ctx, nparam, 0, 0);
130 space = mark_as_params(space);
134 static unsigned global_pos(__isl_keep isl_space *dim,
135 enum isl_dim_type type, unsigned pos)
137 struct isl_ctx *ctx = dim->ctx;
141 isl_assert(ctx, pos < dim->nparam,
142 return isl_space_dim(dim, isl_dim_all));
145 isl_assert(ctx, pos < dim->n_in,
146 return isl_space_dim(dim, isl_dim_all));
147 return pos + dim->nparam;
149 isl_assert(ctx, pos < dim->n_out,
150 return isl_space_dim(dim, isl_dim_all));
151 return pos + dim->nparam + dim->n_in;
153 isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
155 return isl_space_dim(dim, isl_dim_all);
158 /* Extend length of ids array to the total number of dimensions.
160 static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
165 if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
169 dim->ids = isl_calloc_array(dim->ctx,
170 isl_id *, isl_space_dim(dim, isl_dim_all));
174 ids = isl_realloc_array(dim->ctx, dim->ids,
175 isl_id *, isl_space_dim(dim, isl_dim_all));
179 for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); ++i)
183 dim->n_id = isl_space_dim(dim, isl_dim_all);
191 static __isl_give isl_space *set_id(__isl_take isl_space *dim,
192 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
194 dim = isl_space_cow(dim);
199 pos = global_pos(dim, type, pos);
200 if (pos == isl_space_dim(dim, isl_dim_all))
203 if (pos >= dim->n_id) {
206 dim = extend_ids(dim);
220 static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
221 enum isl_dim_type type, unsigned pos)
226 pos = global_pos(dim, type, pos);
227 if (pos == isl_space_dim(dim, isl_dim_all))
229 if (pos >= dim->n_id)
231 return dim->ids[pos];
234 static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
237 case isl_dim_param: return 0;
238 case isl_dim_in: return dim->nparam;
239 case isl_dim_out: return dim->nparam + dim->n_in;
244 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
247 case isl_dim_param: return dim->nparam;
248 case isl_dim_in: return dim->n_in;
249 case isl_dim_out: return dim->n_out;
250 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
255 unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
262 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
266 return offset(dim, type);
269 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
270 enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
271 enum isl_dim_type src_type)
279 for (i = 0; i < n(src, src_type); ++i) {
280 id = get_id(src, src_type, i);
283 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
290 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
295 dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
298 if (dim->tuple_id[0] &&
299 !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
301 if (dim->tuple_id[1] &&
302 !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
304 if (dim->nested[0] && !(dup->nested[0] = isl_space_copy(dim->nested[0])))
306 if (dim->nested[1] && !(dup->nested[1] = isl_space_copy(dim->nested[1])))
310 dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
311 dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
312 dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
319 __isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
327 return isl_space_dup(dim);
330 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
339 void *isl_space_free(__isl_take isl_space *dim)
349 isl_id_free(dim->tuple_id[0]);
350 isl_id_free(dim->tuple_id[1]);
352 isl_space_free(dim->nested[0]);
353 isl_space_free(dim->nested[1]);
355 for (i = 0; i < dim->n_id; ++i)
356 isl_id_free(dim->ids[i]);
358 isl_ctx_deref(dim->ctx);
365 /* Check if "s" is a valid dimension or tuple name.
366 * We currently only forbid names that look like a number.
368 * s is assumed to be non-NULL.
370 static int name_ok(isl_ctx *ctx, const char *s)
375 dummy = strtol(s, &p, 0);
377 isl_die(ctx, isl_error_invalid, "name looks like a number",
383 /* Is it possible for the given dimension type to have a tuple id?
385 static int space_can_have_id(__isl_keep isl_space *space,
386 enum isl_dim_type type)
390 if (isl_space_is_params(space))
391 isl_die(space->ctx, isl_error_invalid,
392 "parameter spaces don't have tuple ids", return 0);
393 if (isl_space_is_set(space) && type != isl_dim_set)
394 isl_die(space->ctx, isl_error_invalid,
395 "set spaces can only have a set id", return 0);
396 if (type != isl_dim_in && type != isl_dim_out)
397 isl_die(space->ctx, isl_error_invalid,
398 "only input, output and set tuples can have ids",
404 /* Does the tuple have an id?
406 int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
408 if (!space_can_have_id(dim, type))
410 return dim->tuple_id[type - isl_dim_in] != NULL;
413 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
414 enum isl_dim_type type)
420 has_id = isl_space_has_tuple_id(dim, type);
424 isl_die(dim->ctx, isl_error_invalid,
425 "tuple has no id", return NULL);
426 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
429 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
430 enum isl_dim_type type, __isl_take isl_id *id)
432 dim = isl_space_cow(dim);
435 if (type != isl_dim_in && type != isl_dim_out)
436 isl_die(dim->ctx, isl_error_invalid,
437 "only input, output and set tuples can have names",
440 isl_id_free(dim->tuple_id[type - isl_dim_in]);
441 dim->tuple_id[type - isl_dim_in] = id;
450 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
451 enum isl_dim_type type)
453 dim = isl_space_cow(dim);
456 if (type != isl_dim_in && type != isl_dim_out)
457 isl_die(dim->ctx, isl_error_invalid,
458 "only input, output and set tuples can have names",
461 isl_id_free(dim->tuple_id[type - isl_dim_in]);
462 dim->tuple_id[type - isl_dim_in] = NULL;
470 /* Set the id of the given dimension of "space" to "id".
471 * If the dimension already has an id, then it is replaced.
472 * If the dimension is a parameter, then we need to change it
473 * in the nested spaces (if any) as well.
475 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
476 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
478 space = isl_space_cow(space);
482 if (type == isl_dim_param) {
485 for (i = 0; i < 2; ++i) {
486 if (!space->nested[i])
489 isl_space_set_dim_id(space->nested[i],
490 type, pos, isl_id_copy(id));
491 if (!space->nested[i])
496 isl_id_free(get_id(space, type, pos));
497 return set_id(space, type, pos, id);
500 isl_space_free(space);
504 /* Reset the id of the given dimension of "space".
505 * If the dimension already has an id, then it is removed.
506 * If the dimension is a parameter, then we need to reset it
507 * in the nested spaces (if any) as well.
509 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
510 enum isl_dim_type type, unsigned pos)
512 space = isl_space_cow(space);
516 if (type == isl_dim_param) {
519 for (i = 0; i < 2; ++i) {
520 if (!space->nested[i])
523 isl_space_reset_dim_id(space->nested[i],
525 if (!space->nested[i])
530 isl_id_free(get_id(space, type, pos));
531 return set_id(space, type, pos, NULL);
533 isl_space_free(space);
537 int isl_space_has_dim_id(__isl_keep isl_space *dim,
538 enum isl_dim_type type, unsigned pos)
542 return get_id(dim, type, pos) != NULL;
545 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
546 enum isl_dim_type type, unsigned pos)
550 if (!get_id(dim, type, pos))
551 isl_die(dim->ctx, isl_error_invalid,
552 "dim has no id", return NULL);
553 return isl_id_copy(get_id(dim, type, pos));
556 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
557 enum isl_dim_type type, const char *s)
565 return isl_space_reset_tuple_id(dim, type);
567 if (!name_ok(dim->ctx, s))
570 id = isl_id_alloc(dim->ctx, s, NULL);
571 return isl_space_set_tuple_id(dim, type, id);
577 /* Does the tuple have a name?
579 int isl_space_has_tuple_name(__isl_keep isl_space *space,
580 enum isl_dim_type type)
584 if (!space_can_have_id(space, type))
586 id = space->tuple_id[type - isl_dim_in];
587 return id && id->name;
590 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
591 enum isl_dim_type type)
596 if (type != isl_dim_in && type != isl_dim_out)
598 id = dim->tuple_id[type - isl_dim_in];
599 return id ? id->name : NULL;
602 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
603 enum isl_dim_type type, unsigned pos,
611 return isl_space_reset_dim_id(dim, type, pos);
612 if (!name_ok(dim->ctx, s))
614 id = isl_id_alloc(dim->ctx, s, NULL);
615 return isl_space_set_dim_id(dim, type, pos, id);
621 /* Does the given dimension have a name?
623 int isl_space_has_dim_name(__isl_keep isl_space *space,
624 enum isl_dim_type type, unsigned pos)
630 id = get_id(space, type, pos);
631 return id && id->name;
634 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
635 enum isl_dim_type type, unsigned pos)
637 isl_id *id = get_id(dim, type, pos);
638 return id ? id->name : NULL;
641 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
642 __isl_keep isl_id *id)
651 offset = isl_space_offset(dim, type);
652 n = isl_space_dim(dim, type);
653 for (i = 0; i < n && offset + i < dim->n_id; ++i)
654 if (dim->ids[offset + i] == id)
660 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
661 enum isl_dim_type type, const char *name)
670 offset = isl_space_offset(space, type);
671 n = isl_space_dim(space, type);
672 for (i = 0; i < n && offset + i < space->n_id; ++i)
673 if (space->ids[offset + i]->name &&
674 !strcmp(space->ids[offset + i]->name, name))
680 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
681 enum isl_dim_type type)
685 if (type == isl_dim_in)
686 return dim->tuple_id[0];
687 if (type == isl_dim_out)
688 return dim->tuple_id[1];
692 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
693 enum isl_dim_type type)
697 if (type == isl_dim_in)
698 return dim->nested[0];
699 if (type == isl_dim_out)
700 return dim->nested[1];
704 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
705 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
708 isl_space *nested1, *nested2;
713 if (dim1 == dim2 && dim1_type == dim2_type)
716 if (n(dim1, dim1_type) != n(dim2, dim2_type))
718 id1 = tuple_id(dim1, dim1_type);
719 id2 = tuple_id(dim2, dim2_type);
722 if (id1 && id1 != id2)
724 nested1 = nested(dim1, dim1_type);
725 nested2 = nested(dim2, dim2_type);
726 if (!nested1 ^ !nested2)
728 if (nested1 && !isl_space_is_equal(nested1, nested2))
733 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
734 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
738 if (dim1 == dim2 && dim1_type == dim2_type)
741 if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
744 if (!dim1->ids && !dim2->ids)
747 for (i = 0; i < n(dim1, dim1_type); ++i) {
748 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
754 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
755 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
760 return match(dim1, dim1_type, dim2, dim2_type);
763 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
764 unsigned first, unsigned n, __isl_keep isl_id **ids)
768 for (i = 0; i < n ; ++i)
769 ids[i] = get_id(dim, type, first + i);
772 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
773 unsigned nparam, unsigned n_in, unsigned n_out)
779 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
782 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
783 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
784 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
786 dim = isl_space_cow(dim);
791 ids = isl_calloc_array(dim->ctx, isl_id *,
792 nparam + n_in + n_out);
795 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
796 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
797 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
800 dim->n_id = nparam + n_in + n_out;
802 dim->nparam = nparam;
813 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
814 enum isl_dim_type type, unsigned n)
818 dim = isl_space_reset(dim, type);
821 dim = isl_space_extend(dim,
822 dim->nparam + n, dim->n_in, dim->n_out);
823 if (dim && dim->nested[0] &&
824 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
827 if (dim && dim->nested[1] &&
828 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
833 return isl_space_extend(dim,
834 dim->nparam, dim->n_in + n, dim->n_out);
836 return isl_space_extend(dim,
837 dim->nparam, dim->n_in, dim->n_out + n);
839 isl_die(dim->ctx, isl_error_invalid,
840 "cannot add dimensions of specified type", goto error);
847 static int valid_dim_type(enum isl_dim_type type)
859 /* Insert "n" dimensions of type "type" at position "pos".
860 * If we are inserting parameters, then they are also inserted in
863 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
864 enum isl_dim_type type, unsigned pos, unsigned n)
871 return isl_space_reset(dim, type);
873 if (!valid_dim_type(type))
874 isl_die(dim->ctx, isl_error_invalid,
875 "cannot insert dimensions of specified type",
878 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
880 dim = isl_space_cow(dim);
885 enum isl_dim_type t, o = isl_dim_param;
888 ids = isl_calloc_array(dim->ctx, isl_id *,
889 dim->nparam + dim->n_in + dim->n_out + n);
893 s[isl_dim_param - o] = dim->nparam;
894 s[isl_dim_in - o] = dim->n_in;
895 s[isl_dim_out - o] = dim->n_out;
896 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
898 get_ids(dim, t, 0, s[t - o], ids + off);
901 get_ids(dim, t, 0, pos, ids + off);
903 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
904 off += s[t - o] - pos;
909 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
912 case isl_dim_param: dim->nparam += n; break;
913 case isl_dim_in: dim->n_in += n; break;
914 case isl_dim_out: dim->n_out += n; break;
917 dim = isl_space_reset(dim, type);
919 if (type == isl_dim_param) {
920 if (dim && dim->nested[0] &&
921 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
922 isl_dim_param, pos, n)))
924 if (dim && dim->nested[1] &&
925 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
926 isl_dim_param, pos, n)))
936 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
937 enum isl_dim_type dst_type, unsigned dst_pos,
938 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
947 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
950 if (dst_type == src_type && dst_pos == src_pos)
953 isl_assert(dim->ctx, dst_type != src_type, goto error);
955 dim = isl_space_reset(dim, src_type);
956 dim = isl_space_reset(dim, dst_type);
958 dim = isl_space_cow(dim);
964 enum isl_dim_type t, o = isl_dim_param;
967 ids = isl_calloc_array(dim->ctx, isl_id *,
968 dim->nparam + dim->n_in + dim->n_out);
972 s[isl_dim_param - o] = dim->nparam;
973 s[isl_dim_in - o] = dim->n_in;
974 s[isl_dim_out - o] = dim->n_out;
975 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
977 get_ids(dim, t, 0, dst_pos, ids + off);
979 get_ids(dim, src_type, src_pos, n, ids + off);
981 get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
983 off += s[t - o] - dst_pos;
984 } else if (t == src_type) {
985 get_ids(dim, t, 0, src_pos, ids + off);
987 get_ids(dim, t, src_pos + n,
988 s[t - o] - src_pos - n, ids + off);
989 off += s[t - o] - src_pos - n;
991 get_ids(dim, t, 0, s[t - o], ids + off);
997 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
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;
1008 case isl_dim_param: dim->nparam -= n; break;
1009 case isl_dim_in: dim->n_in -= n; break;
1010 case isl_dim_out: dim->n_out -= n; break;
1014 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1017 for (i = 0; i < 2; ++i) {
1018 if (!dim->nested[i])
1020 dim->nested[i] = isl_space_replace(dim->nested[i],
1021 isl_dim_param, dim);
1022 if (!dim->nested[i])
1028 isl_space_free(dim);
1032 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1033 __isl_take isl_space *right)
1037 if (!left || !right)
1040 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1042 isl_assert(left->ctx,
1043 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
1046 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1050 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1051 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1052 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1054 if (dim && left->tuple_id[0] &&
1055 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1057 if (dim && right->tuple_id[1] &&
1058 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1060 if (dim && left->nested[0] &&
1061 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1063 if (dim && right->nested[1] &&
1064 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1067 isl_space_free(left);
1068 isl_space_free(right);
1072 isl_space_free(left);
1073 isl_space_free(right);
1077 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1078 __isl_take isl_space *right)
1080 isl_space *dom1, *dom2, *nest1, *nest2;
1082 if (!left || !right)
1085 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1088 dom1 = isl_space_domain(isl_space_copy(left));
1089 dom2 = isl_space_domain(isl_space_copy(right));
1090 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1092 dom1 = isl_space_range(left);
1093 dom2 = isl_space_range(right);
1094 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1096 return isl_space_join(isl_space_reverse(nest1), nest2);
1098 isl_space_free(left);
1099 isl_space_free(right);
1103 /* Given two spaces { A -> C } and { B -> C }, construct the space
1106 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1107 __isl_take isl_space *right)
1109 isl_space *ran, *dom1, *dom2, *nest;
1111 if (!left || !right)
1114 if (!match(left, isl_dim_param, right, isl_dim_param))
1115 isl_die(left->ctx, isl_error_invalid,
1116 "parameters need to match", goto error);
1117 if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1118 isl_die(left->ctx, isl_error_invalid,
1119 "ranges need to match", goto error);
1121 ran = isl_space_range(isl_space_copy(left));
1123 dom1 = isl_space_domain(left);
1124 dom2 = isl_space_domain(right);
1125 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1127 return isl_space_join(isl_space_reverse(nest), ran);
1129 isl_space_free(left);
1130 isl_space_free(right);
1134 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1135 __isl_take isl_space *right)
1137 isl_space *dom, *ran1, *ran2, *nest;
1139 if (!left || !right)
1142 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1144 if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1145 isl_die(left->ctx, isl_error_invalid,
1146 "domains need to match", goto error);
1148 dom = isl_space_domain(isl_space_copy(left));
1150 ran1 = isl_space_range(left);
1151 ran2 = isl_space_range(right);
1152 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1154 return isl_space_join(isl_space_reverse(dom), nest);
1156 isl_space_free(left);
1157 isl_space_free(right);
1161 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1164 isl_id **ids = NULL;
1168 ctx = isl_space_get_ctx(dim);
1169 if (!isl_space_is_set(dim))
1170 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1171 dim = isl_space_cow(dim);
1175 ids = isl_calloc_array(dim->ctx, isl_id *,
1176 dim->nparam + dim->n_out + dim->n_out);
1179 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1180 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1182 dim->n_in = dim->n_out;
1186 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1187 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1189 isl_id_free(dim->tuple_id[0]);
1190 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1191 isl_space_free(dim->nested[0]);
1192 dim->nested[0] = isl_space_copy(dim->nested[1]);
1195 isl_space_free(dim);
1199 __isl_give isl_space *isl_space_map_from_domain_and_range(
1200 __isl_take isl_space *domain, __isl_take isl_space *range)
1202 if (!domain || !range)
1204 if (!isl_space_is_set(domain))
1205 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1206 "domain is not a set space", goto error);
1207 if (!isl_space_is_set(range))
1208 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1209 "range is not a set space", goto error);
1210 return isl_space_join(isl_space_reverse(domain), range);
1212 isl_space_free(domain);
1213 isl_space_free(range);
1217 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1218 enum isl_dim_type type,
1219 unsigned first, unsigned n, __isl_take isl_id **ids)
1223 for (i = 0; i < n ; ++i)
1224 dim = set_id(dim, type, first + i, ids[i]);
1229 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1233 isl_id **ids = NULL;
1238 if (match(dim, isl_dim_in, dim, isl_dim_out))
1241 dim = isl_space_cow(dim);
1245 id = dim->tuple_id[0];
1246 dim->tuple_id[0] = dim->tuple_id[1];
1247 dim->tuple_id[1] = id;
1249 nested = dim->nested[0];
1250 dim->nested[0] = dim->nested[1];
1251 dim->nested[1] = nested;
1254 ids = isl_alloc_array(dim->ctx, isl_id *,
1255 dim->n_in + dim->n_out);
1258 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1259 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1263 dim->n_in = dim->n_out;
1267 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1268 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1275 isl_space_free(dim);
1279 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1280 enum isl_dim_type type, unsigned first, unsigned num)
1288 return isl_space_reset(dim, type);
1290 if (!valid_dim_type(type))
1291 isl_die(dim->ctx, isl_error_invalid,
1292 "cannot drop dimensions of specified type", goto error);
1294 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1295 dim = isl_space_cow(dim);
1299 dim = extend_ids(dim);
1302 for (i = 0; i < num; ++i)
1303 isl_id_free(get_id(dim, type, first + i));
1304 for (i = first+num; i < n(dim, type); ++i)
1305 set_id(dim, type, i - num, get_id(dim, type, i));
1308 get_ids(dim, isl_dim_in, 0, dim->n_in,
1309 dim->ids + offset(dim, isl_dim_in) - num);
1311 get_ids(dim, isl_dim_out, 0, dim->n_out,
1312 dim->ids + offset(dim, isl_dim_out) - num);
1319 case isl_dim_param: dim->nparam -= num; break;
1320 case isl_dim_in: dim->n_in -= num; break;
1321 case isl_dim_out: dim->n_out -= num; break;
1324 dim = isl_space_reset(dim, type);
1325 if (type == isl_dim_param) {
1326 if (dim && dim->nested[0] &&
1327 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1328 isl_dim_param, first, num)))
1330 if (dim && dim->nested[1] &&
1331 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1332 isl_dim_param, first, num)))
1337 isl_space_free(dim);
1341 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1342 unsigned first, unsigned n)
1346 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1349 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1350 unsigned first, unsigned n)
1354 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1357 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1361 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1362 dim = isl_space_reverse(dim);
1363 dim = mark_as_set(dim);
1367 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1371 if (!isl_space_is_set(dim))
1372 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1373 "not a set space", goto error);
1374 dim = isl_space_reverse(dim);
1375 dim = isl_space_reset(dim, isl_dim_out);
1378 isl_space_free(dim);
1382 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1386 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1387 dim = mark_as_set(dim);
1391 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1395 if (!isl_space_is_set(dim))
1396 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1397 "not a set space", goto error);
1398 return isl_space_reset(dim, isl_dim_in);
1400 isl_space_free(dim);
1404 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1406 if (isl_space_is_params(space))
1408 space = isl_space_drop_dims(space,
1409 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1410 space = isl_space_drop_dims(space,
1411 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1412 space = mark_as_params(space);
1416 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1420 if (!isl_space_is_params(space))
1421 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1422 "not a parameter space", goto error);
1423 return isl_space_reset(space, isl_dim_set);
1425 isl_space_free(space);
1429 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1431 dim = isl_space_cow(dim);
1435 dim->n_out += dim->n_in;
1437 dim = isl_space_reset(dim, isl_dim_in);
1438 dim = isl_space_reset(dim, isl_dim_out);
1443 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1451 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1452 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1453 dim = isl_space_cow(dim);
1456 dim->n_out += dim->nparam + dim->n_in + n_div;
1460 for (i = 0; i < dim->n_id; ++i)
1461 isl_id_free(get_id(dim, isl_dim_out, i));
1463 dim = isl_space_reset(dim, isl_dim_in);
1464 dim = isl_space_reset(dim, isl_dim_out);
1469 /* Are the two spaces the same, including positions and names of parameters?
1471 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1477 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1478 isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1479 isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1482 /* Is space1 equal to the domain of space2?
1484 * In the internal version we also allow space2 to be the space of a set,
1485 * provided space1 is a parameter space.
1487 int isl_space_is_domain_internal(__isl_keep isl_space *space1,
1488 __isl_keep isl_space *space2)
1490 if (!space1 || !space2)
1492 if (!isl_space_is_set(space1))
1494 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1495 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1498 /* Is space1 equal to the domain of space2?
1500 int isl_space_is_domain(__isl_keep isl_space *space1,
1501 __isl_keep isl_space *space2)
1505 if (!isl_space_is_map(space2))
1507 return isl_space_is_domain_internal(space1, space2);
1510 /* Is space1 equal to the range of space2?
1512 * In the internal version, space2 is allowed to be the space of a set,
1513 * in which case it should be equal to space1.
1515 int isl_space_is_range_internal(__isl_keep isl_space *space1,
1516 __isl_keep isl_space *space2)
1518 if (!space1 || !space2)
1520 if (!isl_space_is_set(space1))
1522 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1523 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_out);
1526 /* Is space1 equal to the range of space2?
1528 int isl_space_is_range(__isl_keep isl_space *space1,
1529 __isl_keep isl_space *space2)
1533 if (!isl_space_is_map(space2))
1535 return isl_space_is_range_internal(space1, space2);
1538 int isl_space_compatible(__isl_keep isl_space *dim1,
1539 __isl_keep isl_space *dim2)
1541 return dim1->nparam == dim2->nparam &&
1542 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1545 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1553 isl_hash_byte(hash, dim->nparam % 256);
1554 isl_hash_byte(hash, dim->n_in % 256);
1555 isl_hash_byte(hash, dim->n_out % 256);
1557 for (i = 0; i < dim->nparam; ++i) {
1558 id = get_id(dim, isl_dim_param, i);
1559 hash = isl_hash_id(hash, id);
1562 id = tuple_id(dim, isl_dim_in);
1563 hash = isl_hash_id(hash, id);
1564 id = tuple_id(dim, isl_dim_out);
1565 hash = isl_hash_id(hash, id);
1567 hash = isl_hash_dim(hash, dim->nested[0]);
1568 hash = isl_hash_dim(hash, dim->nested[1]);
1573 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1580 hash = isl_hash_init();
1581 hash = isl_hash_dim(hash, dim);
1586 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1591 if (!isl_space_is_set(dim))
1594 return dim->nested[1] != NULL;
1597 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1604 wrap = isl_space_set_alloc(dim->ctx,
1605 dim->nparam, dim->n_in + dim->n_out);
1607 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1608 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1609 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1614 wrap->nested[1] = dim;
1618 isl_space_free(dim);
1622 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1629 if (!isl_space_is_wrapping(dim))
1630 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
1633 unwrap = isl_space_copy(dim->nested[1]);
1634 isl_space_free(dim);
1638 isl_space_free(dim);
1642 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1644 if (type != isl_dim_in && type != isl_dim_out)
1648 if (dim->tuple_id[type - isl_dim_in])
1650 if (dim->nested[type - isl_dim_in])
1655 int isl_space_may_be_set(__isl_keep isl_space *dim)
1659 if (isl_space_is_set(dim))
1661 if (isl_space_dim(dim, isl_dim_in) != 0)
1663 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1668 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1669 enum isl_dim_type type)
1671 if (!isl_space_is_named_or_nested(dim, type))
1674 dim = isl_space_cow(dim);
1678 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1679 dim->tuple_id[type - isl_dim_in] = NULL;
1680 isl_space_free(dim->nested[type - isl_dim_in]);
1681 dim->nested[type - isl_dim_in] = NULL;
1686 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1690 if (!dim->nested[0] && !dim->nested[1])
1694 dim = isl_space_reset(dim, isl_dim_in);
1695 if (dim && dim->nested[1])
1696 dim = isl_space_reset(dim, isl_dim_out);
1701 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1705 if (!dim->nested[0])
1708 return isl_space_reset(dim, isl_dim_in);
1711 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1715 if (!dim->nested[1])
1718 return isl_space_reset(dim, isl_dim_out);
1721 /* Replace the dimensions of the given type of dst by those of src.
1723 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1724 enum isl_dim_type type, __isl_keep isl_space *src)
1726 dst = isl_space_cow(dst);
1731 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1732 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1733 dst = copy_ids(dst, type, 0, src, type);
1735 if (dst && type == isl_dim_param) {
1737 for (i = 0; i <= 1; ++i) {
1738 if (!dst->nested[i])
1740 dst->nested[i] = isl_space_replace(dst->nested[i],
1742 if (!dst->nested[i])
1749 isl_space_free(dst);
1753 /* Given a dimension specification "dim" of a set, create a dimension
1754 * specification for the lift of the set. In particular, the result
1755 * is of the form [dim -> local[..]], with n_local variables in the
1756 * range of the wrapped map.
1758 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1760 isl_space *local_dim;
1765 local_dim = isl_space_dup(dim);
1766 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1767 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1768 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1769 dim = isl_space_join(isl_space_from_domain(dim),
1770 isl_space_from_range(local_dim));
1771 dim = isl_space_wrap(dim);
1772 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1777 int isl_space_can_zip(__isl_keep isl_space *dim)
1782 return dim->nested[0] && dim->nested[1];
1785 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1787 isl_space *dom, *ran;
1788 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1790 if (!isl_space_can_zip(dim))
1791 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1796 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1797 ran = isl_space_unwrap(isl_space_range(dim));
1798 dom_dom = isl_space_domain(isl_space_copy(dom));
1799 dom_ran = isl_space_range(dom);
1800 ran_dom = isl_space_domain(isl_space_copy(ran));
1801 ran_ran = isl_space_range(ran);
1802 dom = isl_space_join(isl_space_from_domain(dom_dom),
1803 isl_space_from_range(ran_dom));
1804 ran = isl_space_join(isl_space_from_domain(dom_ran),
1805 isl_space_from_range(ran_ran));
1806 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1807 isl_space_from_range(isl_space_wrap(ran)));
1809 isl_space_free(dim);
1813 /* Can we apply isl_space_curry to "space"?
1814 * That is, does it have a nested relation in its domain?
1816 int isl_space_can_curry(__isl_keep isl_space *space)
1821 return !!space->nested[0];
1824 /* Given a space (A -> B) -> C, return the corresponding space
1827 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
1829 isl_space *dom, *ran;
1830 isl_space *dom_dom, *dom_ran;
1835 if (!isl_space_can_curry(space))
1836 isl_die(space->ctx, isl_error_invalid,
1837 "space cannot be curried", goto error);
1839 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
1840 ran = isl_space_range(space);
1841 dom_dom = isl_space_domain(isl_space_copy(dom));
1842 dom_ran = isl_space_range(dom);
1843 ran = isl_space_join(isl_space_from_domain(dom_ran),
1844 isl_space_from_range(ran));
1845 return isl_space_join(isl_space_from_domain(dom_dom),
1846 isl_space_from_range(isl_space_wrap(ran)));
1848 isl_space_free(space);
1852 /* Can we apply isl_space_uncurry to "space"?
1853 * That is, does it have a nested relation in its range?
1855 int isl_space_can_uncurry(__isl_keep isl_space *space)
1860 return !!space->nested[1];
1863 /* Given a space A -> (B -> C), return the corresponding space
1866 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
1868 isl_space *dom, *ran;
1869 isl_space *ran_dom, *ran_ran;
1874 if (!isl_space_can_uncurry(space))
1875 isl_die(space->ctx, isl_error_invalid,
1876 "space cannot be uncurried",
1877 return isl_space_free(space));
1879 dom = isl_space_domain(isl_space_copy(space));
1880 ran = isl_space_unwrap(isl_space_range(space));
1881 ran_dom = isl_space_domain(isl_space_copy(ran));
1882 ran_ran = isl_space_range(ran);
1883 dom = isl_space_join(isl_space_from_domain(dom),
1884 isl_space_from_range(ran_dom));
1885 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1886 isl_space_from_range(ran_ran));
1889 int isl_space_has_named_params(__isl_keep isl_space *dim)
1896 if (dim->nparam == 0)
1898 off = isl_space_offset(dim, isl_dim_param);
1899 if (off + dim->nparam > dim->n_id)
1901 for (i = 0; i < dim->nparam; ++i)
1902 if (!dim->ids[off + i])
1907 /* Align the initial parameters of dim1 to match the order in dim2.
1909 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1910 __isl_take isl_space *dim2)
1912 isl_reordering *exp;
1914 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1915 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1916 "parameter alignment requires named parameters",
1919 dim2 = isl_space_params(dim2);
1920 exp = isl_parameter_alignment_reordering(dim1, dim2);
1921 exp = isl_reordering_extend_space(exp, dim1);
1922 isl_space_free(dim2);
1925 dim1 = isl_space_copy(exp->dim);
1926 isl_reordering_free(exp);
1929 isl_space_free(dim1);
1930 isl_space_free(dim2);
1934 /* Given the space of set (domain), construct a space for a map
1935 * with as domain the given space and as range the range of "model".
1937 __isl_give isl_space *isl_space_extend_domain_with_range(
1938 __isl_take isl_space *space, __isl_take isl_space *model)
1943 space = isl_space_from_domain(space);
1944 space = isl_space_add_dims(space, isl_dim_out,
1945 isl_space_dim(model, isl_dim_out));
1946 if (isl_space_has_tuple_id(model, isl_dim_out))
1947 space = isl_space_set_tuple_id(space, isl_dim_out,
1948 isl_space_get_tuple_id(model, isl_dim_out));
1951 if (model->nested[1]) {
1952 isl_space *nested = isl_space_copy(model->nested[1]);
1953 int n_nested, n_space;
1954 nested = isl_space_align_params(nested, isl_space_copy(space));
1955 n_nested = isl_space_dim(nested, isl_dim_param);
1956 n_space = isl_space_dim(space, isl_dim_param);
1957 if (n_nested > n_space)
1958 nested = isl_space_drop_dims(nested, isl_dim_param,
1959 n_space, n_nested - n_space);
1962 space->nested[1] = nested;
1964 isl_space_free(model);
1967 isl_space_free(model);
1968 isl_space_free(space);