2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
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 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
77 unsigned nparam, unsigned dim)
80 space = isl_space_alloc(ctx, nparam, 0, dim);
81 space = mark_as_set(space);
85 /* Mark the space as being that of a parameter domain, by setting
86 * both tuples to isl_id_none.
88 static __isl_give isl_space *mark_as_params(isl_space *space)
92 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
93 space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
97 /* Is the space that of a parameter domain?
99 int isl_space_is_params(__isl_keep isl_space *space)
103 if (space->n_in != 0 || space->nested[0] ||
104 space->n_out != 0 || space->nested[1])
106 if (space->tuple_id[0] != &isl_id_none)
108 if (space->tuple_id[1] != &isl_id_none)
113 /* Create a space for a parameter domain.
115 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
118 space = isl_space_alloc(ctx, nparam, 0, 0);
119 space = mark_as_params(space);
123 static unsigned global_pos(__isl_keep isl_space *dim,
124 enum isl_dim_type type, unsigned pos)
126 struct isl_ctx *ctx = dim->ctx;
130 isl_assert(ctx, pos < dim->nparam,
131 return isl_space_dim(dim, isl_dim_all));
134 isl_assert(ctx, pos < dim->n_in,
135 return isl_space_dim(dim, isl_dim_all));
136 return pos + dim->nparam;
138 isl_assert(ctx, pos < dim->n_out,
139 return isl_space_dim(dim, isl_dim_all));
140 return pos + dim->nparam + dim->n_in;
142 isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
144 return isl_space_dim(dim, isl_dim_all);
147 /* Extend length of ids array to the total number of dimensions.
149 static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
154 if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
158 dim->ids = isl_calloc_array(dim->ctx,
159 isl_id *, isl_space_dim(dim, isl_dim_all));
163 ids = isl_realloc_array(dim->ctx, dim->ids,
164 isl_id *, isl_space_dim(dim, isl_dim_all));
168 for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); ++i)
172 dim->n_id = isl_space_dim(dim, isl_dim_all);
180 static __isl_give isl_space *set_id(__isl_take isl_space *dim,
181 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
183 dim = isl_space_cow(dim);
188 pos = global_pos(dim, type, pos);
189 if (pos == isl_space_dim(dim, isl_dim_all))
192 if (pos >= dim->n_id) {
195 dim = extend_ids(dim);
209 static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
210 enum isl_dim_type type, unsigned pos)
215 pos = global_pos(dim, type, pos);
216 if (pos == isl_space_dim(dim, isl_dim_all))
218 if (pos >= dim->n_id)
220 return dim->ids[pos];
223 static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
226 case isl_dim_param: return 0;
227 case isl_dim_in: return dim->nparam;
228 case isl_dim_out: return dim->nparam + dim->n_in;
233 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
236 case isl_dim_param: return dim->nparam;
237 case isl_dim_in: return dim->n_in;
238 case isl_dim_out: return dim->n_out;
239 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
244 unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
251 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
255 return offset(dim, type);
258 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
259 enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
260 enum isl_dim_type src_type)
268 for (i = 0; i < n(src, src_type); ++i) {
269 id = get_id(src, src_type, i);
272 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
279 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
284 dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
285 if (dim->tuple_id[0] &&
286 !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
288 if (dim->tuple_id[1] &&
289 !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
291 if (dim->nested[0] && !(dup->nested[0] = isl_space_copy(dim->nested[0])))
293 if (dim->nested[1] && !(dup->nested[1] = isl_space_copy(dim->nested[1])))
297 dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
298 dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
299 dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
306 __isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
314 return isl_space_dup(dim);
317 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
326 void isl_space_free(__isl_take isl_space *dim)
336 isl_id_free(dim->tuple_id[0]);
337 isl_id_free(dim->tuple_id[1]);
339 isl_space_free(dim->nested[0]);
340 isl_space_free(dim->nested[1]);
342 for (i = 0; i < dim->n_id; ++i)
343 isl_id_free(dim->ids[i]);
345 isl_ctx_deref(dim->ctx);
350 static int name_ok(isl_ctx *ctx, const char *s)
355 dummy = strtol(s, &p, 0);
357 isl_die(ctx, isl_error_invalid, "name looks like a number",
363 int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
367 if (isl_space_is_params(dim))
368 isl_die(dim->ctx, isl_error_invalid,
369 "parameter spaces don't have tuple ids", return -1);
370 if (isl_space_is_set(dim) && type != isl_dim_set)
371 isl_die(dim->ctx, isl_error_invalid,
372 "set spaces can only have a set id", return -1);
373 if (type != isl_dim_in && type != isl_dim_out)
374 isl_die(dim->ctx, isl_error_invalid,
375 "only input, output and set tuples can have ids",
377 return dim->tuple_id[type - isl_dim_in] != NULL;
380 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
381 enum isl_dim_type type)
387 has_id = isl_space_has_tuple_id(dim, type);
391 isl_die(dim->ctx, isl_error_invalid,
392 "tuple has no id", return NULL);
393 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
396 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
397 enum isl_dim_type type, __isl_take isl_id *id)
399 dim = isl_space_cow(dim);
402 if (type != isl_dim_in && type != isl_dim_out)
403 isl_die(dim->ctx, isl_error_invalid,
404 "only input, output and set tuples can have names",
407 isl_id_free(dim->tuple_id[type - isl_dim_in]);
408 dim->tuple_id[type - isl_dim_in] = id;
417 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
418 enum isl_dim_type type)
420 dim = isl_space_cow(dim);
423 if (type != isl_dim_in && type != isl_dim_out)
424 isl_die(dim->ctx, isl_error_invalid,
425 "only input, output and set tuples can have names",
428 isl_id_free(dim->tuple_id[type - isl_dim_in]);
429 dim->tuple_id[type - isl_dim_in] = NULL;
437 /* Set the id of the given dimension of "space" to "id".
438 * If the dimension already has an id, then it is replaced.
439 * If the dimension is a parameter, then we need to change it
440 * in the nested spaces (if any) as well.
442 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
443 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
445 space = isl_space_cow(space);
449 if (type == isl_dim_param) {
452 for (i = 0; i < 2; ++i) {
453 if (!space->nested[i])
456 isl_space_set_dim_id(space->nested[i],
457 type, pos, isl_id_copy(id));
458 if (!space->nested[i])
463 isl_id_free(get_id(space, type, pos));
464 return set_id(space, type, pos, id);
467 isl_space_free(space);
471 int isl_space_has_dim_id(__isl_keep isl_space *dim,
472 enum isl_dim_type type, unsigned pos)
476 return get_id(dim, type, pos) != NULL;
479 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
480 enum isl_dim_type type, unsigned pos)
484 if (!get_id(dim, type, pos))
485 isl_die(dim->ctx, isl_error_invalid,
486 "dim has no id", return NULL);
487 return isl_id_copy(get_id(dim, type, pos));
490 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
491 enum isl_dim_type type, const char *s)
499 return isl_space_reset_tuple_id(dim, type);
501 if (!name_ok(dim->ctx, s))
504 id = isl_id_alloc(dim->ctx, s, NULL);
505 return isl_space_set_tuple_id(dim, type, id);
511 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
512 enum isl_dim_type type)
517 if (type != isl_dim_in && type != isl_dim_out)
519 id = dim->tuple_id[type - isl_dim_in];
520 return id ? id->name : NULL;
523 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
524 enum isl_dim_type type, unsigned pos,
531 if (!name_ok(dim->ctx, s))
533 id = isl_id_alloc(dim->ctx, s, NULL);
534 return isl_space_set_dim_id(dim, type, pos, id);
540 /* Does the given dimension have a name?
542 int isl_space_has_dim_name(__isl_keep isl_space *space,
543 enum isl_dim_type type, unsigned pos)
549 id = get_id(space, type, pos);
550 return id && id->name;
553 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
554 enum isl_dim_type type, unsigned pos)
556 isl_id *id = get_id(dim, type, pos);
557 return id ? id->name : NULL;
560 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
561 __isl_keep isl_id *id)
570 offset = isl_space_offset(dim, type);
571 n = isl_space_dim(dim, type);
572 for (i = 0; i < n && offset + i < dim->n_id; ++i)
573 if (dim->ids[offset + i] == id)
579 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
580 enum isl_dim_type type, const char *name)
589 offset = isl_space_offset(space, type);
590 n = isl_space_dim(space, type);
591 for (i = 0; i < n && offset + i < space->n_id; ++i)
592 if (space->ids[offset + i]->name &&
593 !strcmp(space->ids[offset + i]->name, name))
599 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
600 enum isl_dim_type type)
604 if (type == isl_dim_in)
605 return dim->tuple_id[0];
606 if (type == isl_dim_out)
607 return dim->tuple_id[1];
611 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
612 enum isl_dim_type type)
616 if (type == isl_dim_in)
617 return dim->nested[0];
618 if (type == isl_dim_out)
619 return dim->nested[1];
623 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
624 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
627 isl_space *nested1, *nested2;
632 if (dim1 == dim2 && dim1_type == dim2_type)
635 if (n(dim1, dim1_type) != n(dim2, dim2_type))
637 id1 = tuple_id(dim1, dim1_type);
638 id2 = tuple_id(dim2, dim2_type);
641 if (id1 && id1 != id2)
643 nested1 = nested(dim1, dim1_type);
644 nested2 = nested(dim2, dim2_type);
645 if (!nested1 ^ !nested2)
647 if (nested1 && !isl_space_is_equal(nested1, nested2))
652 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
653 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
657 if (dim1 == dim2 && dim1_type == dim2_type)
660 if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
663 if (!dim1->ids && !dim2->ids)
666 for (i = 0; i < n(dim1, dim1_type); ++i) {
667 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
673 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
674 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
679 return match(dim1, dim1_type, dim2, dim2_type);
682 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
683 unsigned first, unsigned n, __isl_keep isl_id **ids)
687 for (i = 0; i < n ; ++i)
688 ids[i] = get_id(dim, type, first + i);
691 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
692 unsigned nparam, unsigned n_in, unsigned n_out)
698 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
701 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
702 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
703 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
705 dim = isl_space_cow(dim);
708 ids = isl_calloc_array(dim->ctx, isl_id *,
709 nparam + n_in + n_out);
712 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
713 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
714 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
717 dim->n_id = nparam + n_in + n_out;
719 dim->nparam = nparam;
730 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
731 enum isl_dim_type type, unsigned n)
735 dim = isl_space_reset(dim, type);
738 dim = isl_space_extend(dim,
739 dim->nparam + n, dim->n_in, dim->n_out);
740 if (dim && dim->nested[0] &&
741 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
744 if (dim && dim->nested[1] &&
745 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
750 return isl_space_extend(dim,
751 dim->nparam, dim->n_in + n, dim->n_out);
753 return isl_space_extend(dim,
754 dim->nparam, dim->n_in, dim->n_out + n);
756 isl_die(dim->ctx, isl_error_invalid,
757 "cannot add dimensions of specified type", goto error);
764 static int valid_dim_type(enum isl_dim_type type)
776 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
777 enum isl_dim_type type, unsigned pos, unsigned n)
784 return isl_space_reset(dim, type);
786 if (!valid_dim_type(type))
787 isl_die(dim->ctx, isl_error_invalid,
788 "cannot insert dimensions of specified type",
791 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
793 dim = isl_space_cow(dim);
801 int *size = s - isl_dim_param;
802 ids = isl_calloc_array(dim->ctx, isl_id *,
803 dim->nparam + dim->n_in + dim->n_out + n);
807 size[isl_dim_param] = dim->nparam;
808 size[isl_dim_in] = dim->n_in;
809 size[isl_dim_out] = dim->n_out;
810 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
812 get_ids(dim, t, 0, size[t], ids + off);
815 get_ids(dim, t, 0, pos, ids + off);
817 get_ids(dim, t, pos, size[t] - pos, ids + off);
818 off += size[t] - pos;
823 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
826 case isl_dim_param: dim->nparam += n; break;
827 case isl_dim_in: dim->n_in += n; break;
828 case isl_dim_out: dim->n_out += n; break;
831 dim = isl_space_reset(dim, type);
839 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
840 enum isl_dim_type dst_type, unsigned dst_pos,
841 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
850 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
853 if (dst_type == src_type && dst_pos == src_pos)
856 isl_assert(dim->ctx, dst_type != src_type, goto error);
858 dim = isl_space_reset(dim, src_type);
859 dim = isl_space_reset(dim, dst_type);
861 dim = isl_space_cow(dim);
870 int *size = s - isl_dim_param;
871 ids = isl_calloc_array(dim->ctx, isl_id *,
872 dim->nparam + dim->n_in + dim->n_out);
876 size[isl_dim_param] = dim->nparam;
877 size[isl_dim_in] = dim->n_in;
878 size[isl_dim_out] = dim->n_out;
879 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
881 get_ids(dim, t, 0, dst_pos, ids + off);
883 get_ids(dim, src_type, src_pos, n, ids + off);
885 get_ids(dim, t, dst_pos, size[t] - dst_pos,
887 off += size[t] - dst_pos;
888 } else if (t == src_type) {
889 get_ids(dim, t, 0, src_pos, ids + off);
891 get_ids(dim, t, src_pos + n,
892 size[t] - src_pos - n, ids + off);
893 off += size[t] - src_pos - n;
895 get_ids(dim, t, 0, size[t], ids + off);
901 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
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;
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;
918 if (dst_type != isl_dim_param && src_type != isl_dim_param)
921 for (i = 0; i < 2; ++i) {
924 dim->nested[i] = isl_space_replace(dim->nested[i],
936 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
937 __isl_take isl_space *right)
944 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
946 isl_assert(left->ctx,
947 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
950 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
954 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
955 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
956 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
958 if (dim && left->tuple_id[0] &&
959 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
961 if (dim && right->tuple_id[1] &&
962 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
964 if (dim && left->nested[0] &&
965 !(dim->nested[0] = isl_space_copy(left->nested[0])))
967 if (dim && right->nested[1] &&
968 !(dim->nested[1] = isl_space_copy(right->nested[1])))
971 isl_space_free(left);
972 isl_space_free(right);
976 isl_space_free(left);
977 isl_space_free(right);
981 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
982 __isl_take isl_space *right)
984 isl_space *dom1, *dom2, *nest1, *nest2;
989 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
992 dom1 = isl_space_domain(isl_space_copy(left));
993 dom2 = isl_space_domain(isl_space_copy(right));
994 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
996 dom1 = isl_space_range(left);
997 dom2 = isl_space_range(right);
998 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1000 return isl_space_join(isl_space_reverse(nest1), nest2);
1002 isl_space_free(left);
1003 isl_space_free(right);
1007 /* Given two spaces { A -> C } and { B -> C }, construct the space
1010 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1011 __isl_take isl_space *right)
1013 isl_space *ran, *dom1, *dom2, *nest;
1015 if (!left || !right)
1018 if (!match(left, isl_dim_param, right, isl_dim_param))
1019 isl_die(left->ctx, isl_error_invalid,
1020 "parameters need to match", goto error);
1021 if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1022 isl_die(left->ctx, isl_error_invalid,
1023 "ranges need to match", goto error);
1025 ran = isl_space_range(isl_space_copy(left));
1027 dom1 = isl_space_domain(left);
1028 dom2 = isl_space_domain(right);
1029 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1031 return isl_space_join(isl_space_reverse(nest), ran);
1033 isl_space_free(left);
1034 isl_space_free(right);
1038 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1039 __isl_take isl_space *right)
1041 isl_space *dom, *ran1, *ran2, *nest;
1043 if (!left || !right)
1046 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1048 if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1049 isl_die(left->ctx, isl_error_invalid,
1050 "domains need to match", goto error);
1052 dom = isl_space_domain(isl_space_copy(left));
1054 ran1 = isl_space_range(left);
1055 ran2 = isl_space_range(right);
1056 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1058 return isl_space_join(isl_space_reverse(dom), nest);
1060 isl_space_free(left);
1061 isl_space_free(right);
1065 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1068 isl_id **ids = NULL;
1072 ctx = isl_space_get_ctx(dim);
1073 if (!isl_space_is_set(dim))
1074 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1075 dim = isl_space_cow(dim);
1079 ids = isl_calloc_array(dim->ctx, isl_id *,
1080 dim->nparam + dim->n_out + dim->n_out);
1083 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1084 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1086 dim->n_in = dim->n_out;
1090 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1091 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1093 isl_id_free(dim->tuple_id[0]);
1094 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1095 isl_space_free(dim->nested[0]);
1096 dim->nested[0] = isl_space_copy(dim->nested[1]);
1099 isl_space_free(dim);
1103 __isl_give isl_space *isl_space_map_from_domain_and_range(
1104 __isl_take isl_space *domain, __isl_take isl_space *range)
1106 if (!domain || !range)
1108 if (!isl_space_is_set(domain))
1109 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1110 "domain is not a set space", goto error);
1111 if (!isl_space_is_set(range))
1112 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1113 "range is not a set space", goto error);
1114 return isl_space_join(isl_space_reverse(domain), range);
1116 isl_space_free(domain);
1117 isl_space_free(range);
1121 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1122 enum isl_dim_type type,
1123 unsigned first, unsigned n, __isl_take isl_id **ids)
1127 for (i = 0; i < n ; ++i)
1128 dim = set_id(dim, type, first + i, ids[i]);
1133 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1137 isl_id **ids = NULL;
1142 if (match(dim, isl_dim_in, dim, isl_dim_out))
1145 dim = isl_space_cow(dim);
1149 id = dim->tuple_id[0];
1150 dim->tuple_id[0] = dim->tuple_id[1];
1151 dim->tuple_id[1] = id;
1153 nested = dim->nested[0];
1154 dim->nested[0] = dim->nested[1];
1155 dim->nested[1] = nested;
1158 ids = isl_alloc_array(dim->ctx, isl_id *,
1159 dim->n_in + dim->n_out);
1162 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1163 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1167 dim->n_in = dim->n_out;
1171 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1172 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1179 isl_space_free(dim);
1183 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1184 enum isl_dim_type type, unsigned first, unsigned num)
1192 return isl_space_reset(dim, type);
1194 if (!valid_dim_type(type))
1195 isl_die(dim->ctx, isl_error_invalid,
1196 "cannot drop dimensions of specified type", goto error);
1198 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1199 dim = isl_space_cow(dim);
1203 dim = extend_ids(dim);
1206 for (i = 0; i < num; ++i)
1207 isl_id_free(get_id(dim, type, first + i));
1208 for (i = first+num; i < n(dim, type); ++i)
1209 set_id(dim, type, i - num, get_id(dim, type, i));
1212 get_ids(dim, isl_dim_in, 0, dim->n_in,
1213 dim->ids + offset(dim, isl_dim_in) - num);
1215 get_ids(dim, isl_dim_out, 0, dim->n_out,
1216 dim->ids + offset(dim, isl_dim_out) - num);
1223 case isl_dim_param: dim->nparam -= num; break;
1224 case isl_dim_in: dim->n_in -= num; break;
1225 case isl_dim_out: dim->n_out -= num; break;
1228 dim = isl_space_reset(dim, type);
1229 if (type == isl_dim_param) {
1230 if (dim && dim->nested[0] &&
1231 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1232 isl_dim_param, first, num)))
1234 if (dim && dim->nested[1] &&
1235 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1236 isl_dim_param, first, num)))
1241 isl_space_free(dim);
1245 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1246 unsigned first, unsigned n)
1250 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1253 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1254 unsigned first, unsigned n)
1258 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1261 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1265 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1266 dim = isl_space_reverse(dim);
1267 dim = mark_as_set(dim);
1271 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1275 if (!isl_space_is_set(dim))
1276 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1277 "not a set space", goto error);
1278 dim = isl_space_reverse(dim);
1279 dim = isl_space_reset(dim, isl_dim_out);
1282 isl_space_free(dim);
1286 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1290 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1291 dim = mark_as_set(dim);
1295 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1299 if (!isl_space_is_set(dim))
1300 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1301 "not a set space", goto error);
1302 return isl_space_reset(dim, isl_dim_in);
1304 isl_space_free(dim);
1308 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1310 if (isl_space_is_params(space))
1312 space = isl_space_drop_dims(space,
1313 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1314 space = isl_space_drop_dims(space,
1315 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1316 space = mark_as_params(space);
1320 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1324 if (!isl_space_is_params(space))
1325 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1326 "not a parameter space", goto error);
1327 return isl_space_reset(space, isl_dim_set);
1329 isl_space_free(space);
1333 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1335 dim = isl_space_cow(dim);
1339 dim->n_out += dim->n_in;
1341 dim = isl_space_reset(dim, isl_dim_in);
1342 dim = isl_space_reset(dim, isl_dim_out);
1347 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1355 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1356 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1357 dim = isl_space_cow(dim);
1360 dim->n_out += dim->nparam + dim->n_in + n_div;
1364 for (i = 0; i < dim->n_id; ++i)
1365 isl_id_free(get_id(dim, isl_dim_out, i));
1367 dim = isl_space_reset(dim, isl_dim_in);
1368 dim = isl_space_reset(dim, isl_dim_out);
1373 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1379 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1380 isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1381 isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1384 /* Is space1 equal to the domain of space2?
1386 int isl_space_is_domain(__isl_keep isl_space *space1,
1387 __isl_keep isl_space *space2)
1389 if (!space1 || !space2)
1391 if (!isl_space_is_set(space1))
1393 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1394 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1397 int isl_space_compatible(__isl_keep isl_space *dim1,
1398 __isl_keep isl_space *dim2)
1400 return dim1->nparam == dim2->nparam &&
1401 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1404 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1412 hash = isl_hash_builtin(hash, dim->nparam);
1413 hash = isl_hash_builtin(hash, dim->n_in);
1414 hash = isl_hash_builtin(hash, dim->n_out);
1416 for (i = 0; i < dim->nparam; ++i) {
1417 id = get_id(dim, isl_dim_param, i);
1418 hash = isl_hash_id(hash, id);
1421 id = tuple_id(dim, isl_dim_in);
1422 hash = isl_hash_id(hash, id);
1423 id = tuple_id(dim, isl_dim_out);
1424 hash = isl_hash_id(hash, id);
1426 hash = isl_hash_dim(hash, dim->nested[0]);
1427 hash = isl_hash_dim(hash, dim->nested[1]);
1432 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1439 hash = isl_hash_init();
1440 hash = isl_hash_dim(hash, dim);
1445 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1450 if (!isl_space_is_set(dim))
1453 return dim->nested[1] != NULL;
1456 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1463 wrap = isl_space_set_alloc(dim->ctx,
1464 dim->nparam, dim->n_in + dim->n_out);
1466 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1467 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1468 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1473 wrap->nested[1] = dim;
1477 isl_space_free(dim);
1481 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1488 if (!isl_space_is_wrapping(dim))
1489 isl_die(dim->ctx, isl_error_invalid, "not a wrapping dim",
1492 unwrap = isl_space_copy(dim->nested[1]);
1493 isl_space_free(dim);
1497 isl_space_free(dim);
1501 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1503 if (type != isl_dim_in && type != isl_dim_out)
1507 if (dim->tuple_id[type - isl_dim_in])
1509 if (dim->nested[type - isl_dim_in])
1514 int isl_space_may_be_set(__isl_keep isl_space *dim)
1518 if (isl_space_is_set(dim))
1520 if (isl_space_dim(dim, isl_dim_in) != 0)
1522 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1527 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1528 enum isl_dim_type type)
1530 if (!isl_space_is_named_or_nested(dim, type))
1533 dim = isl_space_cow(dim);
1537 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1538 dim->tuple_id[type - isl_dim_in] = NULL;
1539 isl_space_free(dim->nested[type - isl_dim_in]);
1540 dim->nested[type - isl_dim_in] = NULL;
1545 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1549 if (!dim->nested[0] && !dim->nested[1])
1553 dim = isl_space_reset(dim, isl_dim_in);
1554 if (dim && dim->nested[1])
1555 dim = isl_space_reset(dim, isl_dim_out);
1560 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1564 if (!dim->nested[0])
1567 return isl_space_reset(dim, isl_dim_in);
1570 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1574 if (!dim->nested[1])
1577 return isl_space_reset(dim, isl_dim_out);
1580 /* Replace the dimensions of the given type of dst by those of src.
1582 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1583 enum isl_dim_type type, __isl_keep isl_space *src)
1585 dst = isl_space_cow(dst);
1590 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1591 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1592 dst = copy_ids(dst, type, 0, src, type);
1594 if (dst && type == isl_dim_param) {
1596 for (i = 0; i <= 1; ++i) {
1597 if (!dst->nested[i])
1599 dst->nested[i] = isl_space_replace(dst->nested[i],
1601 if (!dst->nested[i])
1608 isl_space_free(dst);
1612 /* Given a dimension specification "dim" of a set, create a dimension
1613 * specification for the lift of the set. In particular, the result
1614 * is of the form [dim -> local[..]], with n_local variables in the
1615 * range of the wrapped map.
1617 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1619 isl_space *local_dim;
1624 local_dim = isl_space_dup(dim);
1625 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1626 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1627 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1628 dim = isl_space_join(isl_space_from_domain(dim),
1629 isl_space_from_range(local_dim));
1630 dim = isl_space_wrap(dim);
1631 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1636 int isl_space_can_zip(__isl_keep isl_space *dim)
1641 return dim->nested[0] && dim->nested[1];
1644 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1646 isl_space *dom, *ran;
1647 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1649 if (!isl_space_can_zip(dim))
1650 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1655 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1656 ran = isl_space_unwrap(isl_space_range(dim));
1657 dom_dom = isl_space_domain(isl_space_copy(dom));
1658 dom_ran = isl_space_range(dom);
1659 ran_dom = isl_space_domain(isl_space_copy(ran));
1660 ran_ran = isl_space_range(ran);
1661 dom = isl_space_join(isl_space_from_domain(dom_dom),
1662 isl_space_from_range(ran_dom));
1663 ran = isl_space_join(isl_space_from_domain(dom_ran),
1664 isl_space_from_range(ran_ran));
1665 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1666 isl_space_from_range(isl_space_wrap(ran)));
1668 isl_space_free(dim);
1672 int isl_space_has_named_params(__isl_keep isl_space *dim)
1679 if (dim->nparam == 0)
1681 off = isl_space_offset(dim, isl_dim_param);
1682 if (off + dim->nparam > dim->n_id)
1684 for (i = 0; i < dim->nparam; ++i)
1685 if (!dim->ids[off + i])
1690 /* Align the initial parameters of dim1 to match the order in dim2.
1692 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1693 __isl_take isl_space *dim2)
1695 isl_reordering *exp;
1697 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1698 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1699 "parameter alignment requires named parameters",
1702 dim2 = isl_space_params(dim2);
1703 exp = isl_parameter_alignment_reordering(dim1, dim2);
1704 exp = isl_reordering_extend_space(exp, dim1);
1705 isl_space_free(dim2);
1708 dim1 = isl_space_copy(exp->dim);
1709 isl_reordering_free(exp);
1712 isl_space_free(dim1);
1713 isl_space_free(dim2);
1717 /* Given the space of set (domain), construct a space for a map
1718 * with as domain the given space and as range the range of "model".
1720 __isl_give isl_space *isl_space_extend_domain_with_range(
1721 __isl_take isl_space *domain, __isl_take isl_space *model)
1725 space = isl_space_from_domain(domain);
1726 space = isl_space_add_dims(space, isl_dim_out,
1727 isl_space_dim(model, isl_dim_out));
1728 if (isl_space_has_tuple_id(model, isl_dim_out))
1729 space = isl_space_set_tuple_id(space, isl_dim_out,
1730 isl_space_get_tuple_id(model, isl_dim_out));
1731 isl_space_free(model);