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 /* Reset the id of the given dimension of "space".
472 * If the dimension already has an id, then it is removed.
473 * If the dimension is a parameter, then we need to reset it
474 * in the nested spaces (if any) as well.
476 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
477 enum isl_dim_type type, unsigned pos)
479 space = isl_space_cow(space);
483 if (type == isl_dim_param) {
486 for (i = 0; i < 2; ++i) {
487 if (!space->nested[i])
490 isl_space_reset_dim_id(space->nested[i],
492 if (!space->nested[i])
497 isl_id_free(get_id(space, type, pos));
498 return set_id(space, type, pos, NULL);
500 isl_space_free(space);
504 int isl_space_has_dim_id(__isl_keep isl_space *dim,
505 enum isl_dim_type type, unsigned pos)
509 return get_id(dim, type, pos) != NULL;
512 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
513 enum isl_dim_type type, unsigned pos)
517 if (!get_id(dim, type, pos))
518 isl_die(dim->ctx, isl_error_invalid,
519 "dim has no id", return NULL);
520 return isl_id_copy(get_id(dim, type, pos));
523 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
524 enum isl_dim_type type, const char *s)
532 return isl_space_reset_tuple_id(dim, type);
534 if (!name_ok(dim->ctx, s))
537 id = isl_id_alloc(dim->ctx, s, NULL);
538 return isl_space_set_tuple_id(dim, type, id);
544 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
545 enum isl_dim_type type)
550 if (type != isl_dim_in && type != isl_dim_out)
552 id = dim->tuple_id[type - isl_dim_in];
553 return id ? id->name : NULL;
556 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
557 enum isl_dim_type type, unsigned pos,
565 return isl_space_reset_dim_id(dim, type, pos);
566 if (!name_ok(dim->ctx, s))
568 id = isl_id_alloc(dim->ctx, s, NULL);
569 return isl_space_set_dim_id(dim, type, pos, id);
575 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
576 enum isl_dim_type type, unsigned pos)
578 isl_id *id = get_id(dim, type, pos);
579 return id ? id->name : NULL;
582 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
583 __isl_keep isl_id *id)
592 offset = isl_space_offset(dim, type);
593 n = isl_space_dim(dim, type);
594 for (i = 0; i < n && offset + i < dim->n_id; ++i)
595 if (dim->ids[offset + i] == id)
601 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
602 enum isl_dim_type type, const char *name)
611 offset = isl_space_offset(space, type);
612 n = isl_space_dim(space, type);
613 for (i = 0; i < n && offset + i < space->n_id; ++i)
614 if (space->ids[offset + i]->name &&
615 !strcmp(space->ids[offset + i]->name, name))
621 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
622 enum isl_dim_type type)
626 if (type == isl_dim_in)
627 return dim->tuple_id[0];
628 if (type == isl_dim_out)
629 return dim->tuple_id[1];
633 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
634 enum isl_dim_type type)
638 if (type == isl_dim_in)
639 return dim->nested[0];
640 if (type == isl_dim_out)
641 return dim->nested[1];
645 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
646 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
649 isl_space *nested1, *nested2;
654 if (dim1 == dim2 && dim1_type == dim2_type)
657 if (n(dim1, dim1_type) != n(dim2, dim2_type))
659 id1 = tuple_id(dim1, dim1_type);
660 id2 = tuple_id(dim2, dim2_type);
663 if (id1 && id1 != id2)
665 nested1 = nested(dim1, dim1_type);
666 nested2 = nested(dim2, dim2_type);
667 if (!nested1 ^ !nested2)
669 if (nested1 && !isl_space_is_equal(nested1, nested2))
674 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
675 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
679 if (dim1 == dim2 && dim1_type == dim2_type)
682 if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
685 if (!dim1->ids && !dim2->ids)
688 for (i = 0; i < n(dim1, dim1_type); ++i) {
689 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
695 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
696 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
701 return match(dim1, dim1_type, dim2, dim2_type);
704 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
705 unsigned first, unsigned n, __isl_keep isl_id **ids)
709 for (i = 0; i < n ; ++i)
710 ids[i] = get_id(dim, type, first + i);
713 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
714 unsigned nparam, unsigned n_in, unsigned n_out)
720 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
723 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
724 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
725 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
727 dim = isl_space_cow(dim);
730 ids = isl_calloc_array(dim->ctx, isl_id *,
731 nparam + n_in + n_out);
734 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
735 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
736 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
739 dim->n_id = nparam + n_in + n_out;
741 dim->nparam = nparam;
752 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
753 enum isl_dim_type type, unsigned n)
757 dim = isl_space_reset(dim, type);
760 dim = isl_space_extend(dim,
761 dim->nparam + n, dim->n_in, dim->n_out);
762 if (dim && dim->nested[0] &&
763 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
766 if (dim && dim->nested[1] &&
767 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
772 return isl_space_extend(dim,
773 dim->nparam, dim->n_in + n, dim->n_out);
775 return isl_space_extend(dim,
776 dim->nparam, dim->n_in, dim->n_out + n);
778 isl_die(dim->ctx, isl_error_invalid,
779 "cannot add dimensions of specified type", goto error);
786 static int valid_dim_type(enum isl_dim_type type)
798 /* Insert "n" dimensions of type "type" at position "pos".
799 * If we are inserting parameters, then they are also inserted in
802 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
803 enum isl_dim_type type, unsigned pos, unsigned n)
810 return isl_space_reset(dim, type);
812 if (!valid_dim_type(type))
813 isl_die(dim->ctx, isl_error_invalid,
814 "cannot insert dimensions of specified type",
817 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
819 dim = isl_space_cow(dim);
827 int *size = s - isl_dim_param;
828 ids = isl_calloc_array(dim->ctx, isl_id *,
829 dim->nparam + dim->n_in + dim->n_out + n);
833 size[isl_dim_param] = dim->nparam;
834 size[isl_dim_in] = dim->n_in;
835 size[isl_dim_out] = dim->n_out;
836 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
838 get_ids(dim, t, 0, size[t], ids + off);
841 get_ids(dim, t, 0, pos, ids + off);
843 get_ids(dim, t, pos, size[t] - pos, ids + off);
844 off += size[t] - pos;
849 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
852 case isl_dim_param: dim->nparam += n; break;
853 case isl_dim_in: dim->n_in += n; break;
854 case isl_dim_out: dim->n_out += n; break;
857 dim = isl_space_reset(dim, type);
859 if (type == isl_dim_param) {
860 if (dim && dim->nested[0] &&
861 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
862 isl_dim_param, pos, n)))
864 if (dim && dim->nested[1] &&
865 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
866 isl_dim_param, pos, n)))
876 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
877 enum isl_dim_type dst_type, unsigned dst_pos,
878 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
887 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
890 if (dst_type == src_type && dst_pos == src_pos)
893 isl_assert(dim->ctx, dst_type != src_type, goto error);
895 dim = isl_space_reset(dim, src_type);
896 dim = isl_space_reset(dim, dst_type);
898 dim = isl_space_cow(dim);
907 int *size = s - isl_dim_param;
908 ids = isl_calloc_array(dim->ctx, isl_id *,
909 dim->nparam + dim->n_in + dim->n_out);
913 size[isl_dim_param] = dim->nparam;
914 size[isl_dim_in] = dim->n_in;
915 size[isl_dim_out] = dim->n_out;
916 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
918 get_ids(dim, t, 0, dst_pos, ids + off);
920 get_ids(dim, src_type, src_pos, n, ids + off);
922 get_ids(dim, t, dst_pos, size[t] - dst_pos,
924 off += size[t] - dst_pos;
925 } else if (t == src_type) {
926 get_ids(dim, t, 0, src_pos, ids + off);
928 get_ids(dim, t, src_pos + n,
929 size[t] - src_pos - n, ids + off);
930 off += size[t] - src_pos - n;
932 get_ids(dim, t, 0, size[t], ids + off);
938 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
942 case isl_dim_param: dim->nparam += n; break;
943 case isl_dim_in: dim->n_in += n; break;
944 case isl_dim_out: dim->n_out += n; break;
949 case isl_dim_param: dim->nparam -= n; break;
950 case isl_dim_in: dim->n_in -= n; break;
951 case isl_dim_out: dim->n_out -= n; break;
955 if (dst_type != isl_dim_param && src_type != isl_dim_param)
958 for (i = 0; i < 2; ++i) {
961 dim->nested[i] = isl_space_replace(dim->nested[i],
973 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
974 __isl_take isl_space *right)
981 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
983 isl_assert(left->ctx,
984 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
987 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
991 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
992 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
993 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
995 if (dim && left->tuple_id[0] &&
996 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
998 if (dim && right->tuple_id[1] &&
999 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1001 if (dim && left->nested[0] &&
1002 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1004 if (dim && right->nested[1] &&
1005 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1008 isl_space_free(left);
1009 isl_space_free(right);
1013 isl_space_free(left);
1014 isl_space_free(right);
1018 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1019 __isl_take isl_space *right)
1021 isl_space *dom1, *dom2, *nest1, *nest2;
1023 if (!left || !right)
1026 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1029 dom1 = isl_space_domain(isl_space_copy(left));
1030 dom2 = isl_space_domain(isl_space_copy(right));
1031 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1033 dom1 = isl_space_range(left);
1034 dom2 = isl_space_range(right);
1035 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1037 return isl_space_join(isl_space_reverse(nest1), nest2);
1039 isl_space_free(left);
1040 isl_space_free(right);
1044 /* Given two spaces { A -> C } and { B -> C }, construct the space
1047 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1048 __isl_take isl_space *right)
1050 isl_space *ran, *dom1, *dom2, *nest;
1052 if (!left || !right)
1055 if (!match(left, isl_dim_param, right, isl_dim_param))
1056 isl_die(left->ctx, isl_error_invalid,
1057 "parameters need to match", goto error);
1058 if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1059 isl_die(left->ctx, isl_error_invalid,
1060 "ranges need to match", goto error);
1062 ran = isl_space_range(isl_space_copy(left));
1064 dom1 = isl_space_domain(left);
1065 dom2 = isl_space_domain(right);
1066 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1068 return isl_space_join(isl_space_reverse(nest), ran);
1070 isl_space_free(left);
1071 isl_space_free(right);
1075 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1076 __isl_take isl_space *right)
1078 isl_space *dom, *ran1, *ran2, *nest;
1080 if (!left || !right)
1083 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1085 if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1086 isl_die(left->ctx, isl_error_invalid,
1087 "domains need to match", goto error);
1089 dom = isl_space_domain(isl_space_copy(left));
1091 ran1 = isl_space_range(left);
1092 ran2 = isl_space_range(right);
1093 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1095 return isl_space_join(isl_space_reverse(dom), nest);
1097 isl_space_free(left);
1098 isl_space_free(right);
1102 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1105 isl_id **ids = NULL;
1109 ctx = isl_space_get_ctx(dim);
1110 if (!isl_space_is_set(dim))
1111 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1112 dim = isl_space_cow(dim);
1116 ids = isl_calloc_array(dim->ctx, isl_id *,
1117 dim->nparam + dim->n_out + dim->n_out);
1120 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1121 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1123 dim->n_in = dim->n_out;
1127 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1128 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1130 isl_id_free(dim->tuple_id[0]);
1131 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1132 isl_space_free(dim->nested[0]);
1133 dim->nested[0] = isl_space_copy(dim->nested[1]);
1136 isl_space_free(dim);
1140 __isl_give isl_space *isl_space_map_from_domain_and_range(
1141 __isl_take isl_space *domain, __isl_take isl_space *range)
1143 if (!domain || !range)
1145 if (!isl_space_is_set(domain))
1146 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1147 "domain is not a set space", goto error);
1148 if (!isl_space_is_set(range))
1149 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1150 "range is not a set space", goto error);
1151 return isl_space_join(isl_space_reverse(domain), range);
1153 isl_space_free(domain);
1154 isl_space_free(range);
1158 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1159 enum isl_dim_type type,
1160 unsigned first, unsigned n, __isl_take isl_id **ids)
1164 for (i = 0; i < n ; ++i)
1165 dim = set_id(dim, type, first + i, ids[i]);
1170 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1174 isl_id **ids = NULL;
1179 if (match(dim, isl_dim_in, dim, isl_dim_out))
1182 dim = isl_space_cow(dim);
1186 id = dim->tuple_id[0];
1187 dim->tuple_id[0] = dim->tuple_id[1];
1188 dim->tuple_id[1] = id;
1190 nested = dim->nested[0];
1191 dim->nested[0] = dim->nested[1];
1192 dim->nested[1] = nested;
1195 ids = isl_alloc_array(dim->ctx, isl_id *,
1196 dim->n_in + dim->n_out);
1199 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1200 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1204 dim->n_in = dim->n_out;
1208 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1209 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1216 isl_space_free(dim);
1220 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1221 enum isl_dim_type type, unsigned first, unsigned num)
1229 return isl_space_reset(dim, type);
1231 if (!valid_dim_type(type))
1232 isl_die(dim->ctx, isl_error_invalid,
1233 "cannot drop dimensions of specified type", goto error);
1235 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1236 dim = isl_space_cow(dim);
1240 dim = extend_ids(dim);
1243 for (i = 0; i < num; ++i)
1244 isl_id_free(get_id(dim, type, first + i));
1245 for (i = first+num; i < n(dim, type); ++i)
1246 set_id(dim, type, i - num, get_id(dim, type, i));
1249 get_ids(dim, isl_dim_in, 0, dim->n_in,
1250 dim->ids + offset(dim, isl_dim_in) - num);
1252 get_ids(dim, isl_dim_out, 0, dim->n_out,
1253 dim->ids + offset(dim, isl_dim_out) - num);
1260 case isl_dim_param: dim->nparam -= num; break;
1261 case isl_dim_in: dim->n_in -= num; break;
1262 case isl_dim_out: dim->n_out -= num; break;
1265 dim = isl_space_reset(dim, type);
1266 if (type == isl_dim_param) {
1267 if (dim && dim->nested[0] &&
1268 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1269 isl_dim_param, first, num)))
1271 if (dim && dim->nested[1] &&
1272 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1273 isl_dim_param, first, num)))
1278 isl_space_free(dim);
1282 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1283 unsigned first, unsigned n)
1287 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1290 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1291 unsigned first, unsigned n)
1295 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1298 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1302 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1303 dim = isl_space_reverse(dim);
1304 dim = mark_as_set(dim);
1308 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1312 if (!isl_space_is_set(dim))
1313 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1314 "not a set space", goto error);
1315 dim = isl_space_reverse(dim);
1316 dim = isl_space_reset(dim, isl_dim_out);
1319 isl_space_free(dim);
1323 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1327 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1328 dim = mark_as_set(dim);
1332 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1336 if (!isl_space_is_set(dim))
1337 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1338 "not a set space", goto error);
1339 return isl_space_reset(dim, isl_dim_in);
1341 isl_space_free(dim);
1345 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1347 if (isl_space_is_params(space))
1349 space = isl_space_drop_dims(space,
1350 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1351 space = isl_space_drop_dims(space,
1352 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1353 space = mark_as_params(space);
1357 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1361 if (!isl_space_is_params(space))
1362 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1363 "not a parameter space", goto error);
1364 return isl_space_reset(space, isl_dim_set);
1366 isl_space_free(space);
1370 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1372 dim = isl_space_cow(dim);
1376 dim->n_out += dim->n_in;
1378 dim = isl_space_reset(dim, isl_dim_in);
1379 dim = isl_space_reset(dim, isl_dim_out);
1384 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1392 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1393 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1394 dim = isl_space_cow(dim);
1397 dim->n_out += dim->nparam + dim->n_in + n_div;
1401 for (i = 0; i < dim->n_id; ++i)
1402 isl_id_free(get_id(dim, isl_dim_out, i));
1404 dim = isl_space_reset(dim, isl_dim_in);
1405 dim = isl_space_reset(dim, isl_dim_out);
1410 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1416 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1417 isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1418 isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1421 /* Is space1 equal to the domain of space2?
1423 int isl_space_is_domain(__isl_keep isl_space *space1,
1424 __isl_keep isl_space *space2)
1426 if (!space1 || !space2)
1428 if (!isl_space_is_set(space1))
1430 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1431 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1434 int isl_space_compatible(__isl_keep isl_space *dim1,
1435 __isl_keep isl_space *dim2)
1437 return dim1->nparam == dim2->nparam &&
1438 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1441 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1449 hash = isl_hash_builtin(hash, dim->nparam);
1450 hash = isl_hash_builtin(hash, dim->n_in);
1451 hash = isl_hash_builtin(hash, dim->n_out);
1453 for (i = 0; i < dim->nparam; ++i) {
1454 id = get_id(dim, isl_dim_param, i);
1455 hash = isl_hash_id(hash, id);
1458 id = tuple_id(dim, isl_dim_in);
1459 hash = isl_hash_id(hash, id);
1460 id = tuple_id(dim, isl_dim_out);
1461 hash = isl_hash_id(hash, id);
1463 hash = isl_hash_dim(hash, dim->nested[0]);
1464 hash = isl_hash_dim(hash, dim->nested[1]);
1469 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1476 hash = isl_hash_init();
1477 hash = isl_hash_dim(hash, dim);
1482 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1487 if (!isl_space_is_set(dim))
1490 return dim->nested[1] != NULL;
1493 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1500 wrap = isl_space_set_alloc(dim->ctx,
1501 dim->nparam, dim->n_in + dim->n_out);
1503 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1504 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1505 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1510 wrap->nested[1] = dim;
1514 isl_space_free(dim);
1518 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1525 if (!isl_space_is_wrapping(dim))
1526 isl_die(dim->ctx, isl_error_invalid, "not a wrapping dim",
1529 unwrap = isl_space_copy(dim->nested[1]);
1530 isl_space_free(dim);
1534 isl_space_free(dim);
1538 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1540 if (type != isl_dim_in && type != isl_dim_out)
1544 if (dim->tuple_id[type - isl_dim_in])
1546 if (dim->nested[type - isl_dim_in])
1551 int isl_space_may_be_set(__isl_keep isl_space *dim)
1555 if (isl_space_is_set(dim))
1557 if (isl_space_dim(dim, isl_dim_in) != 0)
1559 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1564 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1565 enum isl_dim_type type)
1567 if (!isl_space_is_named_or_nested(dim, type))
1570 dim = isl_space_cow(dim);
1574 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1575 dim->tuple_id[type - isl_dim_in] = NULL;
1576 isl_space_free(dim->nested[type - isl_dim_in]);
1577 dim->nested[type - isl_dim_in] = NULL;
1582 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1586 if (!dim->nested[0] && !dim->nested[1])
1590 dim = isl_space_reset(dim, isl_dim_in);
1591 if (dim && dim->nested[1])
1592 dim = isl_space_reset(dim, isl_dim_out);
1597 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1601 if (!dim->nested[0])
1604 return isl_space_reset(dim, isl_dim_in);
1607 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1611 if (!dim->nested[1])
1614 return isl_space_reset(dim, isl_dim_out);
1617 /* Replace the dimensions of the given type of dst by those of src.
1619 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1620 enum isl_dim_type type, __isl_keep isl_space *src)
1622 dst = isl_space_cow(dst);
1627 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1628 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1629 dst = copy_ids(dst, type, 0, src, type);
1631 if (dst && type == isl_dim_param) {
1633 for (i = 0; i <= 1; ++i) {
1634 if (!dst->nested[i])
1636 dst->nested[i] = isl_space_replace(dst->nested[i],
1638 if (!dst->nested[i])
1645 isl_space_free(dst);
1649 /* Given a dimension specification "dim" of a set, create a dimension
1650 * specification for the lift of the set. In particular, the result
1651 * is of the form [dim -> local[..]], with n_local variables in the
1652 * range of the wrapped map.
1654 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1656 isl_space *local_dim;
1661 local_dim = isl_space_dup(dim);
1662 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1663 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1664 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1665 dim = isl_space_join(isl_space_from_domain(dim),
1666 isl_space_from_range(local_dim));
1667 dim = isl_space_wrap(dim);
1668 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1673 int isl_space_can_zip(__isl_keep isl_space *dim)
1678 return dim->nested[0] && dim->nested[1];
1681 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1683 isl_space *dom, *ran;
1684 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1686 if (!isl_space_can_zip(dim))
1687 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1692 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1693 ran = isl_space_unwrap(isl_space_range(dim));
1694 dom_dom = isl_space_domain(isl_space_copy(dom));
1695 dom_ran = isl_space_range(dom);
1696 ran_dom = isl_space_domain(isl_space_copy(ran));
1697 ran_ran = isl_space_range(ran);
1698 dom = isl_space_join(isl_space_from_domain(dom_dom),
1699 isl_space_from_range(ran_dom));
1700 ran = isl_space_join(isl_space_from_domain(dom_ran),
1701 isl_space_from_range(ran_ran));
1702 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1703 isl_space_from_range(isl_space_wrap(ran)));
1705 isl_space_free(dim);
1709 int isl_space_has_named_params(__isl_keep isl_space *dim)
1716 if (dim->nparam == 0)
1718 off = isl_space_offset(dim, isl_dim_param);
1719 if (off + dim->nparam > dim->n_id)
1721 for (i = 0; i < dim->nparam; ++i)
1722 if (!dim->ids[off + i])
1727 /* Align the initial parameters of dim1 to match the order in dim2.
1729 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1730 __isl_take isl_space *dim2)
1732 isl_reordering *exp;
1734 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1735 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1736 "parameter alignment requires named parameters",
1739 dim2 = isl_space_params(dim2);
1740 exp = isl_parameter_alignment_reordering(dim1, dim2);
1741 exp = isl_reordering_extend_space(exp, dim1);
1742 isl_space_free(dim2);
1745 dim1 = isl_space_copy(exp->dim);
1746 isl_reordering_free(exp);
1749 isl_space_free(dim1);
1750 isl_space_free(dim2);
1754 /* Given the space of set (domain), construct a space for a map
1755 * with as domain the given space and as range the range of "model".
1757 __isl_give isl_space *isl_space_extend_domain_with_range(
1758 __isl_take isl_space *domain, __isl_take isl_space *model)
1762 space = isl_space_from_domain(domain);
1763 space = isl_space_add_dims(space, isl_dim_out,
1764 isl_space_dim(model, isl_dim_out));
1765 if (isl_space_has_tuple_id(model, isl_dim_out))
1766 space = isl_space_set_tuple_id(space, isl_dim_out,
1767 isl_space_get_tuple_id(model, isl_dim_out));
1768 isl_space_free(model);