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);
362 /* Check if "s" is a valid dimension or tuple name.
363 * We currently only forbid names that look like a number.
365 * s is assumed to be non-NULL.
367 static int name_ok(isl_ctx *ctx, const char *s)
372 dummy = strtol(s, &p, 0);
374 isl_die(ctx, isl_error_invalid, "name looks like a number",
380 /* Is it possible for the given dimension type to have a tuple id?
382 static int space_can_have_id(__isl_keep isl_space *space,
383 enum isl_dim_type type)
387 if (isl_space_is_params(space))
388 isl_die(space->ctx, isl_error_invalid,
389 "parameter spaces don't have tuple ids", return 0);
390 if (isl_space_is_set(space) && type != isl_dim_set)
391 isl_die(space->ctx, isl_error_invalid,
392 "set spaces can only have a set id", return 0);
393 if (type != isl_dim_in && type != isl_dim_out)
394 isl_die(space->ctx, isl_error_invalid,
395 "only input, output and set tuples can have ids",
401 /* Does the tuple have an id?
403 int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
405 if (!space_can_have_id(dim, type))
407 return dim->tuple_id[type - isl_dim_in] != NULL;
410 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
411 enum isl_dim_type type)
417 has_id = isl_space_has_tuple_id(dim, type);
421 isl_die(dim->ctx, isl_error_invalid,
422 "tuple has no id", return NULL);
423 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
426 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
427 enum isl_dim_type type, __isl_take isl_id *id)
429 dim = isl_space_cow(dim);
432 if (type != isl_dim_in && type != isl_dim_out)
433 isl_die(dim->ctx, isl_error_invalid,
434 "only input, output and set tuples can have names",
437 isl_id_free(dim->tuple_id[type - isl_dim_in]);
438 dim->tuple_id[type - isl_dim_in] = id;
447 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
448 enum isl_dim_type type)
450 dim = isl_space_cow(dim);
453 if (type != isl_dim_in && type != isl_dim_out)
454 isl_die(dim->ctx, isl_error_invalid,
455 "only input, output and set tuples can have names",
458 isl_id_free(dim->tuple_id[type - isl_dim_in]);
459 dim->tuple_id[type - isl_dim_in] = NULL;
467 /* Set the id of the given dimension of "space" to "id".
468 * If the dimension already has an id, then it is replaced.
469 * If the dimension is a parameter, then we need to change it
470 * in the nested spaces (if any) as well.
472 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
473 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
475 space = isl_space_cow(space);
479 if (type == isl_dim_param) {
482 for (i = 0; i < 2; ++i) {
483 if (!space->nested[i])
486 isl_space_set_dim_id(space->nested[i],
487 type, pos, isl_id_copy(id));
488 if (!space->nested[i])
493 isl_id_free(get_id(space, type, pos));
494 return set_id(space, type, pos, id);
497 isl_space_free(space);
501 /* Reset the id of the given dimension of "space".
502 * If the dimension already has an id, then it is removed.
503 * If the dimension is a parameter, then we need to reset it
504 * in the nested spaces (if any) as well.
506 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
507 enum isl_dim_type type, unsigned pos)
509 space = isl_space_cow(space);
513 if (type == isl_dim_param) {
516 for (i = 0; i < 2; ++i) {
517 if (!space->nested[i])
520 isl_space_reset_dim_id(space->nested[i],
522 if (!space->nested[i])
527 isl_id_free(get_id(space, type, pos));
528 return set_id(space, type, pos, NULL);
530 isl_space_free(space);
534 int isl_space_has_dim_id(__isl_keep isl_space *dim,
535 enum isl_dim_type type, unsigned pos)
539 return get_id(dim, type, pos) != NULL;
542 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
543 enum isl_dim_type type, unsigned pos)
547 if (!get_id(dim, type, pos))
548 isl_die(dim->ctx, isl_error_invalid,
549 "dim has no id", return NULL);
550 return isl_id_copy(get_id(dim, type, pos));
553 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
554 enum isl_dim_type type, const char *s)
562 return isl_space_reset_tuple_id(dim, type);
564 if (!name_ok(dim->ctx, s))
567 id = isl_id_alloc(dim->ctx, s, NULL);
568 return isl_space_set_tuple_id(dim, type, id);
574 /* Does the tuple have a name?
576 int isl_space_has_tuple_name(__isl_keep isl_space *space,
577 enum isl_dim_type type)
581 if (!space_can_have_id(space, type))
583 id = space->tuple_id[type - isl_dim_in];
584 return id && id->name;
587 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
588 enum isl_dim_type type)
593 if (type != isl_dim_in && type != isl_dim_out)
595 id = dim->tuple_id[type - isl_dim_in];
596 return id ? id->name : NULL;
599 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
600 enum isl_dim_type type, unsigned pos,
608 return isl_space_reset_dim_id(dim, type, pos);
609 if (!name_ok(dim->ctx, s))
611 id = isl_id_alloc(dim->ctx, s, NULL);
612 return isl_space_set_dim_id(dim, type, pos, id);
618 /* Does the given dimension have a name?
620 int isl_space_has_dim_name(__isl_keep isl_space *space,
621 enum isl_dim_type type, unsigned pos)
627 id = get_id(space, type, pos);
628 return id && id->name;
631 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
632 enum isl_dim_type type, unsigned pos)
634 isl_id *id = get_id(dim, type, pos);
635 return id ? id->name : NULL;
638 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
639 __isl_keep isl_id *id)
648 offset = isl_space_offset(dim, type);
649 n = isl_space_dim(dim, type);
650 for (i = 0; i < n && offset + i < dim->n_id; ++i)
651 if (dim->ids[offset + i] == id)
657 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
658 enum isl_dim_type type, const char *name)
667 offset = isl_space_offset(space, type);
668 n = isl_space_dim(space, type);
669 for (i = 0; i < n && offset + i < space->n_id; ++i)
670 if (space->ids[offset + i]->name &&
671 !strcmp(space->ids[offset + i]->name, name))
677 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
678 enum isl_dim_type type)
682 if (type == isl_dim_in)
683 return dim->tuple_id[0];
684 if (type == isl_dim_out)
685 return dim->tuple_id[1];
689 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
690 enum isl_dim_type type)
694 if (type == isl_dim_in)
695 return dim->nested[0];
696 if (type == isl_dim_out)
697 return dim->nested[1];
701 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
702 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
705 isl_space *nested1, *nested2;
710 if (dim1 == dim2 && dim1_type == dim2_type)
713 if (n(dim1, dim1_type) != n(dim2, dim2_type))
715 id1 = tuple_id(dim1, dim1_type);
716 id2 = tuple_id(dim2, dim2_type);
719 if (id1 && id1 != id2)
721 nested1 = nested(dim1, dim1_type);
722 nested2 = nested(dim2, dim2_type);
723 if (!nested1 ^ !nested2)
725 if (nested1 && !isl_space_is_equal(nested1, nested2))
730 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
731 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
735 if (dim1 == dim2 && dim1_type == dim2_type)
738 if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
741 if (!dim1->ids && !dim2->ids)
744 for (i = 0; i < n(dim1, dim1_type); ++i) {
745 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
751 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
752 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
757 return match(dim1, dim1_type, dim2, dim2_type);
760 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
761 unsigned first, unsigned n, __isl_keep isl_id **ids)
765 for (i = 0; i < n ; ++i)
766 ids[i] = get_id(dim, type, first + i);
769 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
770 unsigned nparam, unsigned n_in, unsigned n_out)
776 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
779 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
780 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
781 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
783 dim = isl_space_cow(dim);
786 ids = isl_calloc_array(dim->ctx, isl_id *,
787 nparam + n_in + n_out);
790 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
791 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
792 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
795 dim->n_id = nparam + n_in + n_out;
797 dim->nparam = nparam;
808 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
809 enum isl_dim_type type, unsigned n)
813 dim = isl_space_reset(dim, type);
816 dim = isl_space_extend(dim,
817 dim->nparam + n, dim->n_in, dim->n_out);
818 if (dim && dim->nested[0] &&
819 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
822 if (dim && dim->nested[1] &&
823 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
828 return isl_space_extend(dim,
829 dim->nparam, dim->n_in + n, dim->n_out);
831 return isl_space_extend(dim,
832 dim->nparam, dim->n_in, dim->n_out + n);
834 isl_die(dim->ctx, isl_error_invalid,
835 "cannot add dimensions of specified type", goto error);
842 static int valid_dim_type(enum isl_dim_type type)
854 /* Insert "n" dimensions of type "type" at position "pos".
855 * If we are inserting parameters, then they are also inserted in
858 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
859 enum isl_dim_type type, unsigned pos, unsigned n)
866 return isl_space_reset(dim, type);
868 if (!valid_dim_type(type))
869 isl_die(dim->ctx, isl_error_invalid,
870 "cannot insert dimensions of specified type",
873 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
875 dim = isl_space_cow(dim);
880 enum isl_dim_type t, o = isl_dim_param;
883 ids = isl_calloc_array(dim->ctx, isl_id *,
884 dim->nparam + dim->n_in + dim->n_out + n);
888 s[isl_dim_param - o] = dim->nparam;
889 s[isl_dim_in - o] = dim->n_in;
890 s[isl_dim_out - o] = dim->n_out;
891 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
893 get_ids(dim, t, 0, s[t - o], ids + off);
896 get_ids(dim, t, 0, pos, ids + off);
898 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
899 off += s[t - o] - pos;
904 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
907 case isl_dim_param: dim->nparam += n; break;
908 case isl_dim_in: dim->n_in += n; break;
909 case isl_dim_out: dim->n_out += n; break;
912 dim = isl_space_reset(dim, type);
914 if (type == isl_dim_param) {
915 if (dim && dim->nested[0] &&
916 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
917 isl_dim_param, pos, n)))
919 if (dim && dim->nested[1] &&
920 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
921 isl_dim_param, pos, n)))
931 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
932 enum isl_dim_type dst_type, unsigned dst_pos,
933 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
942 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
945 if (dst_type == src_type && dst_pos == src_pos)
948 isl_assert(dim->ctx, dst_type != src_type, goto error);
950 dim = isl_space_reset(dim, src_type);
951 dim = isl_space_reset(dim, dst_type);
953 dim = isl_space_cow(dim);
959 enum isl_dim_type t, o = isl_dim_param;
962 ids = isl_calloc_array(dim->ctx, isl_id *,
963 dim->nparam + dim->n_in + dim->n_out);
967 s[isl_dim_param - o] = dim->nparam;
968 s[isl_dim_in - o] = dim->n_in;
969 s[isl_dim_out - o] = dim->n_out;
970 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
972 get_ids(dim, t, 0, dst_pos, ids + off);
974 get_ids(dim, src_type, src_pos, n, ids + off);
976 get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
978 off += s[t - o] - dst_pos;
979 } else if (t == src_type) {
980 get_ids(dim, t, 0, src_pos, ids + off);
982 get_ids(dim, t, src_pos + n,
983 s[t - o] - src_pos - n, ids + off);
984 off += s[t - o] - src_pos - n;
986 get_ids(dim, t, 0, s[t - o], ids + off);
992 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
996 case isl_dim_param: dim->nparam += n; break;
997 case isl_dim_in: dim->n_in += n; break;
998 case isl_dim_out: dim->n_out += n; break;
1003 case isl_dim_param: dim->nparam -= n; break;
1004 case isl_dim_in: dim->n_in -= n; break;
1005 case isl_dim_out: dim->n_out -= n; break;
1009 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1012 for (i = 0; i < 2; ++i) {
1013 if (!dim->nested[i])
1015 dim->nested[i] = isl_space_replace(dim->nested[i],
1016 isl_dim_param, dim);
1017 if (!dim->nested[i])
1023 isl_space_free(dim);
1027 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1028 __isl_take isl_space *right)
1032 if (!left || !right)
1035 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1037 isl_assert(left->ctx,
1038 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
1041 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1045 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1046 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1047 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1049 if (dim && left->tuple_id[0] &&
1050 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1052 if (dim && right->tuple_id[1] &&
1053 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1055 if (dim && left->nested[0] &&
1056 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1058 if (dim && right->nested[1] &&
1059 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1062 isl_space_free(left);
1063 isl_space_free(right);
1067 isl_space_free(left);
1068 isl_space_free(right);
1072 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1073 __isl_take isl_space *right)
1075 isl_space *dom1, *dom2, *nest1, *nest2;
1077 if (!left || !right)
1080 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1083 dom1 = isl_space_domain(isl_space_copy(left));
1084 dom2 = isl_space_domain(isl_space_copy(right));
1085 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1087 dom1 = isl_space_range(left);
1088 dom2 = isl_space_range(right);
1089 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1091 return isl_space_join(isl_space_reverse(nest1), nest2);
1093 isl_space_free(left);
1094 isl_space_free(right);
1098 /* Given two spaces { A -> C } and { B -> C }, construct the space
1101 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1102 __isl_take isl_space *right)
1104 isl_space *ran, *dom1, *dom2, *nest;
1106 if (!left || !right)
1109 if (!match(left, isl_dim_param, right, isl_dim_param))
1110 isl_die(left->ctx, isl_error_invalid,
1111 "parameters need to match", goto error);
1112 if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1113 isl_die(left->ctx, isl_error_invalid,
1114 "ranges need to match", goto error);
1116 ran = isl_space_range(isl_space_copy(left));
1118 dom1 = isl_space_domain(left);
1119 dom2 = isl_space_domain(right);
1120 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1122 return isl_space_join(isl_space_reverse(nest), ran);
1124 isl_space_free(left);
1125 isl_space_free(right);
1129 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1130 __isl_take isl_space *right)
1132 isl_space *dom, *ran1, *ran2, *nest;
1134 if (!left || !right)
1137 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1139 if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1140 isl_die(left->ctx, isl_error_invalid,
1141 "domains need to match", goto error);
1143 dom = isl_space_domain(isl_space_copy(left));
1145 ran1 = isl_space_range(left);
1146 ran2 = isl_space_range(right);
1147 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1149 return isl_space_join(isl_space_reverse(dom), nest);
1151 isl_space_free(left);
1152 isl_space_free(right);
1156 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1159 isl_id **ids = NULL;
1163 ctx = isl_space_get_ctx(dim);
1164 if (!isl_space_is_set(dim))
1165 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1166 dim = isl_space_cow(dim);
1170 ids = isl_calloc_array(dim->ctx, isl_id *,
1171 dim->nparam + dim->n_out + dim->n_out);
1174 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1175 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1177 dim->n_in = dim->n_out;
1181 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1182 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1184 isl_id_free(dim->tuple_id[0]);
1185 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1186 isl_space_free(dim->nested[0]);
1187 dim->nested[0] = isl_space_copy(dim->nested[1]);
1190 isl_space_free(dim);
1194 __isl_give isl_space *isl_space_map_from_domain_and_range(
1195 __isl_take isl_space *domain, __isl_take isl_space *range)
1197 if (!domain || !range)
1199 if (!isl_space_is_set(domain))
1200 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1201 "domain is not a set space", goto error);
1202 if (!isl_space_is_set(range))
1203 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1204 "range is not a set space", goto error);
1205 return isl_space_join(isl_space_reverse(domain), range);
1207 isl_space_free(domain);
1208 isl_space_free(range);
1212 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1213 enum isl_dim_type type,
1214 unsigned first, unsigned n, __isl_take isl_id **ids)
1218 for (i = 0; i < n ; ++i)
1219 dim = set_id(dim, type, first + i, ids[i]);
1224 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1228 isl_id **ids = NULL;
1233 if (match(dim, isl_dim_in, dim, isl_dim_out))
1236 dim = isl_space_cow(dim);
1240 id = dim->tuple_id[0];
1241 dim->tuple_id[0] = dim->tuple_id[1];
1242 dim->tuple_id[1] = id;
1244 nested = dim->nested[0];
1245 dim->nested[0] = dim->nested[1];
1246 dim->nested[1] = nested;
1249 ids = isl_alloc_array(dim->ctx, isl_id *,
1250 dim->n_in + dim->n_out);
1253 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1254 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1258 dim->n_in = dim->n_out;
1262 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1263 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1270 isl_space_free(dim);
1274 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1275 enum isl_dim_type type, unsigned first, unsigned num)
1283 return isl_space_reset(dim, type);
1285 if (!valid_dim_type(type))
1286 isl_die(dim->ctx, isl_error_invalid,
1287 "cannot drop dimensions of specified type", goto error);
1289 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1290 dim = isl_space_cow(dim);
1294 dim = extend_ids(dim);
1297 for (i = 0; i < num; ++i)
1298 isl_id_free(get_id(dim, type, first + i));
1299 for (i = first+num; i < n(dim, type); ++i)
1300 set_id(dim, type, i - num, get_id(dim, type, i));
1303 get_ids(dim, isl_dim_in, 0, dim->n_in,
1304 dim->ids + offset(dim, isl_dim_in) - num);
1306 get_ids(dim, isl_dim_out, 0, dim->n_out,
1307 dim->ids + offset(dim, isl_dim_out) - num);
1314 case isl_dim_param: dim->nparam -= num; break;
1315 case isl_dim_in: dim->n_in -= num; break;
1316 case isl_dim_out: dim->n_out -= num; break;
1319 dim = isl_space_reset(dim, type);
1320 if (type == isl_dim_param) {
1321 if (dim && dim->nested[0] &&
1322 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1323 isl_dim_param, first, num)))
1325 if (dim && dim->nested[1] &&
1326 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1327 isl_dim_param, first, num)))
1332 isl_space_free(dim);
1336 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1337 unsigned first, unsigned n)
1341 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1344 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1345 unsigned first, unsigned n)
1349 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1352 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1356 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1357 dim = isl_space_reverse(dim);
1358 dim = mark_as_set(dim);
1362 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1366 if (!isl_space_is_set(dim))
1367 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1368 "not a set space", goto error);
1369 dim = isl_space_reverse(dim);
1370 dim = isl_space_reset(dim, isl_dim_out);
1373 isl_space_free(dim);
1377 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1381 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1382 dim = mark_as_set(dim);
1386 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1390 if (!isl_space_is_set(dim))
1391 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1392 "not a set space", goto error);
1393 return isl_space_reset(dim, isl_dim_in);
1395 isl_space_free(dim);
1399 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1401 if (isl_space_is_params(space))
1403 space = isl_space_drop_dims(space,
1404 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1405 space = isl_space_drop_dims(space,
1406 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1407 space = mark_as_params(space);
1411 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1415 if (!isl_space_is_params(space))
1416 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1417 "not a parameter space", goto error);
1418 return isl_space_reset(space, isl_dim_set);
1420 isl_space_free(space);
1424 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1426 dim = isl_space_cow(dim);
1430 dim->n_out += dim->n_in;
1432 dim = isl_space_reset(dim, isl_dim_in);
1433 dim = isl_space_reset(dim, isl_dim_out);
1438 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1446 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1447 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1448 dim = isl_space_cow(dim);
1451 dim->n_out += dim->nparam + dim->n_in + n_div;
1455 for (i = 0; i < dim->n_id; ++i)
1456 isl_id_free(get_id(dim, isl_dim_out, i));
1458 dim = isl_space_reset(dim, isl_dim_in);
1459 dim = isl_space_reset(dim, isl_dim_out);
1464 /* Are the two spaces the same, including positions and names of parameters?
1466 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1472 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1473 isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1474 isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1477 /* Is space1 equal to the domain of space2?
1479 * In the internal version we also allow space2 to be the space of a set,
1480 * provided space1 is a parameter space.
1482 int isl_space_is_domain_internal(__isl_keep isl_space *space1,
1483 __isl_keep isl_space *space2)
1485 if (!space1 || !space2)
1487 if (!isl_space_is_set(space1))
1489 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1490 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1493 /* Is space1 equal to the domain of space2?
1495 int isl_space_is_domain(__isl_keep isl_space *space1,
1496 __isl_keep isl_space *space2)
1500 if (!isl_space_is_map(space2))
1502 return isl_space_is_domain_internal(space1, space2);
1505 /* Is space1 equal to the range of space2?
1507 * In the internal version, space2 is allowed to be the space of a set,
1508 * in which case it should be equal to space1.
1510 int isl_space_is_range_internal(__isl_keep isl_space *space1,
1511 __isl_keep isl_space *space2)
1513 if (!space1 || !space2)
1515 if (!isl_space_is_set(space1))
1517 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1518 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_out);
1521 /* Is space1 equal to the range of space2?
1523 int isl_space_is_range(__isl_keep isl_space *space1,
1524 __isl_keep isl_space *space2)
1528 if (!isl_space_is_map(space2))
1530 return isl_space_is_range_internal(space1, space2);
1533 int isl_space_compatible(__isl_keep isl_space *dim1,
1534 __isl_keep isl_space *dim2)
1536 return dim1->nparam == dim2->nparam &&
1537 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1540 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1548 hash = isl_hash_builtin(hash, dim->nparam);
1549 hash = isl_hash_builtin(hash, dim->n_in);
1550 hash = isl_hash_builtin(hash, dim->n_out);
1552 for (i = 0; i < dim->nparam; ++i) {
1553 id = get_id(dim, isl_dim_param, i);
1554 hash = isl_hash_id(hash, id);
1557 id = tuple_id(dim, isl_dim_in);
1558 hash = isl_hash_id(hash, id);
1559 id = tuple_id(dim, isl_dim_out);
1560 hash = isl_hash_id(hash, id);
1562 hash = isl_hash_dim(hash, dim->nested[0]);
1563 hash = isl_hash_dim(hash, dim->nested[1]);
1568 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1575 hash = isl_hash_init();
1576 hash = isl_hash_dim(hash, dim);
1581 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1586 if (!isl_space_is_set(dim))
1589 return dim->nested[1] != NULL;
1592 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1599 wrap = isl_space_set_alloc(dim->ctx,
1600 dim->nparam, dim->n_in + dim->n_out);
1602 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1603 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1604 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1609 wrap->nested[1] = dim;
1613 isl_space_free(dim);
1617 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1624 if (!isl_space_is_wrapping(dim))
1625 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
1628 unwrap = isl_space_copy(dim->nested[1]);
1629 isl_space_free(dim);
1633 isl_space_free(dim);
1637 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1639 if (type != isl_dim_in && type != isl_dim_out)
1643 if (dim->tuple_id[type - isl_dim_in])
1645 if (dim->nested[type - isl_dim_in])
1650 int isl_space_may_be_set(__isl_keep isl_space *dim)
1654 if (isl_space_is_set(dim))
1656 if (isl_space_dim(dim, isl_dim_in) != 0)
1658 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1663 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1664 enum isl_dim_type type)
1666 if (!isl_space_is_named_or_nested(dim, type))
1669 dim = isl_space_cow(dim);
1673 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1674 dim->tuple_id[type - isl_dim_in] = NULL;
1675 isl_space_free(dim->nested[type - isl_dim_in]);
1676 dim->nested[type - isl_dim_in] = NULL;
1681 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1685 if (!dim->nested[0] && !dim->nested[1])
1689 dim = isl_space_reset(dim, isl_dim_in);
1690 if (dim && dim->nested[1])
1691 dim = isl_space_reset(dim, isl_dim_out);
1696 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1700 if (!dim->nested[0])
1703 return isl_space_reset(dim, isl_dim_in);
1706 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1710 if (!dim->nested[1])
1713 return isl_space_reset(dim, isl_dim_out);
1716 /* Replace the dimensions of the given type of dst by those of src.
1718 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1719 enum isl_dim_type type, __isl_keep isl_space *src)
1721 dst = isl_space_cow(dst);
1726 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1727 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1728 dst = copy_ids(dst, type, 0, src, type);
1730 if (dst && type == isl_dim_param) {
1732 for (i = 0; i <= 1; ++i) {
1733 if (!dst->nested[i])
1735 dst->nested[i] = isl_space_replace(dst->nested[i],
1737 if (!dst->nested[i])
1744 isl_space_free(dst);
1748 /* Given a dimension specification "dim" of a set, create a dimension
1749 * specification for the lift of the set. In particular, the result
1750 * is of the form [dim -> local[..]], with n_local variables in the
1751 * range of the wrapped map.
1753 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1755 isl_space *local_dim;
1760 local_dim = isl_space_dup(dim);
1761 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1762 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1763 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1764 dim = isl_space_join(isl_space_from_domain(dim),
1765 isl_space_from_range(local_dim));
1766 dim = isl_space_wrap(dim);
1767 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1772 int isl_space_can_zip(__isl_keep isl_space *dim)
1777 return dim->nested[0] && dim->nested[1];
1780 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1782 isl_space *dom, *ran;
1783 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1785 if (!isl_space_can_zip(dim))
1786 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1791 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1792 ran = isl_space_unwrap(isl_space_range(dim));
1793 dom_dom = isl_space_domain(isl_space_copy(dom));
1794 dom_ran = isl_space_range(dom);
1795 ran_dom = isl_space_domain(isl_space_copy(ran));
1796 ran_ran = isl_space_range(ran);
1797 dom = isl_space_join(isl_space_from_domain(dom_dom),
1798 isl_space_from_range(ran_dom));
1799 ran = isl_space_join(isl_space_from_domain(dom_ran),
1800 isl_space_from_range(ran_ran));
1801 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1802 isl_space_from_range(isl_space_wrap(ran)));
1804 isl_space_free(dim);
1808 /* Can we apply isl_space_curry to "space"?
1809 * That is, does it have a nested relation in its domain?
1811 int isl_space_can_curry(__isl_keep isl_space *space)
1816 return !!space->nested[0];
1819 /* Given a space (A -> B) -> C, return the corresponding space
1822 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
1824 isl_space *dom, *ran;
1825 isl_space *dom_dom, *dom_ran;
1830 if (!isl_space_can_curry(space))
1831 isl_die(space->ctx, isl_error_invalid,
1832 "space cannot be curried", goto error);
1834 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
1835 ran = isl_space_range(space);
1836 dom_dom = isl_space_domain(isl_space_copy(dom));
1837 dom_ran = isl_space_range(dom);
1838 ran = isl_space_join(isl_space_from_domain(dom_ran),
1839 isl_space_from_range(ran));
1840 return isl_space_join(isl_space_from_domain(dom_dom),
1841 isl_space_from_range(isl_space_wrap(ran)));
1843 isl_space_free(space);
1847 /* Can we apply isl_space_uncurry to "space"?
1848 * That is, does it have a nested relation in its range?
1850 int isl_space_can_uncurry(__isl_keep isl_space *space)
1855 return !!space->nested[1];
1858 /* Given a space A -> (B -> C), return the corresponding space
1861 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
1863 isl_space *dom, *ran;
1864 isl_space *ran_dom, *ran_ran;
1869 if (!isl_space_can_uncurry(space))
1870 isl_die(space->ctx, isl_error_invalid,
1871 "space cannot be uncurried",
1872 return isl_space_free(space));
1874 dom = isl_space_domain(isl_space_copy(space));
1875 ran = isl_space_unwrap(isl_space_range(space));
1876 ran_dom = isl_space_domain(isl_space_copy(ran));
1877 ran_ran = isl_space_range(ran);
1878 dom = isl_space_join(isl_space_from_domain(dom),
1879 isl_space_from_range(ran_dom));
1880 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1881 isl_space_from_range(ran_ran));
1884 int isl_space_has_named_params(__isl_keep isl_space *dim)
1891 if (dim->nparam == 0)
1893 off = isl_space_offset(dim, isl_dim_param);
1894 if (off + dim->nparam > dim->n_id)
1896 for (i = 0; i < dim->nparam; ++i)
1897 if (!dim->ids[off + i])
1902 /* Align the initial parameters of dim1 to match the order in dim2.
1904 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1905 __isl_take isl_space *dim2)
1907 isl_reordering *exp;
1909 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1910 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1911 "parameter alignment requires named parameters",
1914 dim2 = isl_space_params(dim2);
1915 exp = isl_parameter_alignment_reordering(dim1, dim2);
1916 exp = isl_reordering_extend_space(exp, dim1);
1917 isl_space_free(dim2);
1920 dim1 = isl_space_copy(exp->dim);
1921 isl_reordering_free(exp);
1924 isl_space_free(dim1);
1925 isl_space_free(dim2);
1929 /* Given the space of set (domain), construct a space for a map
1930 * with as domain the given space and as range the range of "model".
1932 __isl_give isl_space *isl_space_extend_domain_with_range(
1933 __isl_take isl_space *space, __isl_take isl_space *model)
1938 space = isl_space_from_domain(space);
1939 space = isl_space_add_dims(space, isl_dim_out,
1940 isl_space_dim(model, isl_dim_out));
1941 if (isl_space_has_tuple_id(model, isl_dim_out))
1942 space = isl_space_set_tuple_id(space, isl_dim_out,
1943 isl_space_get_tuple_id(model, isl_dim_out));
1946 if (model->nested[1]) {
1947 isl_space *nested = isl_space_copy(model->nested[1]);
1948 int n_nested, n_space;
1949 nested = isl_space_align_params(nested, isl_space_copy(space));
1950 n_nested = isl_space_dim(nested, isl_dim_param);
1951 n_space = isl_space_dim(space, isl_dim_param);
1952 if (n_nested > n_space)
1953 nested = isl_space_drop_dims(nested, isl_dim_param,
1954 n_space, n_nested - n_space);
1957 space->nested[1] = nested;
1959 isl_space_free(model);
1962 isl_space_free(model);
1963 isl_space_free(space);