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 /* Is it possible for the given dimension type to have a tuple id?
365 static int space_can_have_id(__isl_keep isl_space *space,
366 enum isl_dim_type type)
370 if (isl_space_is_params(space))
371 isl_die(space->ctx, isl_error_invalid,
372 "parameter spaces don't have tuple ids", return 0);
373 if (isl_space_is_set(space) && type != isl_dim_set)
374 isl_die(space->ctx, isl_error_invalid,
375 "set spaces can only have a set id", return 0);
376 if (type != isl_dim_in && type != isl_dim_out)
377 isl_die(space->ctx, isl_error_invalid,
378 "only input, output and set tuples can have ids",
384 /* Does the tuple have an id?
386 int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
388 if (!space_can_have_id(dim, type))
390 return dim->tuple_id[type - isl_dim_in] != NULL;
393 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
394 enum isl_dim_type type)
400 has_id = isl_space_has_tuple_id(dim, type);
404 isl_die(dim->ctx, isl_error_invalid,
405 "tuple has no id", return NULL);
406 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
409 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
410 enum isl_dim_type type, __isl_take isl_id *id)
412 dim = isl_space_cow(dim);
415 if (type != isl_dim_in && type != isl_dim_out)
416 isl_die(dim->ctx, isl_error_invalid,
417 "only input, output and set tuples can have names",
420 isl_id_free(dim->tuple_id[type - isl_dim_in]);
421 dim->tuple_id[type - isl_dim_in] = id;
430 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
431 enum isl_dim_type type)
433 dim = isl_space_cow(dim);
436 if (type != isl_dim_in && type != isl_dim_out)
437 isl_die(dim->ctx, isl_error_invalid,
438 "only input, output and set tuples can have names",
441 isl_id_free(dim->tuple_id[type - isl_dim_in]);
442 dim->tuple_id[type - isl_dim_in] = NULL;
450 /* Set the id of the given dimension of "space" to "id".
451 * If the dimension already has an id, then it is replaced.
452 * If the dimension is a parameter, then we need to change it
453 * in the nested spaces (if any) as well.
455 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
456 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
458 space = isl_space_cow(space);
462 if (type == isl_dim_param) {
465 for (i = 0; i < 2; ++i) {
466 if (!space->nested[i])
469 isl_space_set_dim_id(space->nested[i],
470 type, pos, isl_id_copy(id));
471 if (!space->nested[i])
476 isl_id_free(get_id(space, type, pos));
477 return set_id(space, type, pos, id);
480 isl_space_free(space);
484 /* Reset the id of the given dimension of "space".
485 * If the dimension already has an id, then it is removed.
486 * If the dimension is a parameter, then we need to reset it
487 * in the nested spaces (if any) as well.
489 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
490 enum isl_dim_type type, unsigned pos)
492 space = isl_space_cow(space);
496 if (type == isl_dim_param) {
499 for (i = 0; i < 2; ++i) {
500 if (!space->nested[i])
503 isl_space_reset_dim_id(space->nested[i],
505 if (!space->nested[i])
510 isl_id_free(get_id(space, type, pos));
511 return set_id(space, type, pos, NULL);
513 isl_space_free(space);
517 int isl_space_has_dim_id(__isl_keep isl_space *dim,
518 enum isl_dim_type type, unsigned pos)
522 return get_id(dim, type, pos) != NULL;
525 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
526 enum isl_dim_type type, unsigned pos)
530 if (!get_id(dim, type, pos))
531 isl_die(dim->ctx, isl_error_invalid,
532 "dim has no id", return NULL);
533 return isl_id_copy(get_id(dim, type, pos));
536 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
537 enum isl_dim_type type, const char *s)
545 return isl_space_reset_tuple_id(dim, type);
547 if (!name_ok(dim->ctx, s))
550 id = isl_id_alloc(dim->ctx, s, NULL);
551 return isl_space_set_tuple_id(dim, type, id);
557 /* Does the tuple have a name?
559 int isl_space_has_tuple_name(__isl_keep isl_space *space,
560 enum isl_dim_type type)
564 if (!space_can_have_id(space, type))
566 id = space->tuple_id[type - isl_dim_in];
567 return id && id->name;
570 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
571 enum isl_dim_type type)
576 if (type != isl_dim_in && type != isl_dim_out)
578 id = dim->tuple_id[type - isl_dim_in];
579 return id ? id->name : NULL;
582 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
583 enum isl_dim_type type, unsigned pos,
591 return isl_space_reset_dim_id(dim, type, pos);
592 if (!name_ok(dim->ctx, s))
594 id = isl_id_alloc(dim->ctx, s, NULL);
595 return isl_space_set_dim_id(dim, type, pos, id);
601 /* Does the given dimension have a name?
603 int isl_space_has_dim_name(__isl_keep isl_space *space,
604 enum isl_dim_type type, unsigned pos)
610 id = get_id(space, type, pos);
611 return id && id->name;
614 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
615 enum isl_dim_type type, unsigned pos)
617 isl_id *id = get_id(dim, type, pos);
618 return id ? id->name : NULL;
621 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
622 __isl_keep isl_id *id)
631 offset = isl_space_offset(dim, type);
632 n = isl_space_dim(dim, type);
633 for (i = 0; i < n && offset + i < dim->n_id; ++i)
634 if (dim->ids[offset + i] == id)
640 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
641 enum isl_dim_type type, const char *name)
650 offset = isl_space_offset(space, type);
651 n = isl_space_dim(space, type);
652 for (i = 0; i < n && offset + i < space->n_id; ++i)
653 if (space->ids[offset + i]->name &&
654 !strcmp(space->ids[offset + i]->name, name))
660 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
661 enum isl_dim_type type)
665 if (type == isl_dim_in)
666 return dim->tuple_id[0];
667 if (type == isl_dim_out)
668 return dim->tuple_id[1];
672 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
673 enum isl_dim_type type)
677 if (type == isl_dim_in)
678 return dim->nested[0];
679 if (type == isl_dim_out)
680 return dim->nested[1];
684 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
685 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
688 isl_space *nested1, *nested2;
693 if (dim1 == dim2 && dim1_type == dim2_type)
696 if (n(dim1, dim1_type) != n(dim2, dim2_type))
698 id1 = tuple_id(dim1, dim1_type);
699 id2 = tuple_id(dim2, dim2_type);
702 if (id1 && id1 != id2)
704 nested1 = nested(dim1, dim1_type);
705 nested2 = nested(dim2, dim2_type);
706 if (!nested1 ^ !nested2)
708 if (nested1 && !isl_space_is_equal(nested1, nested2))
713 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
714 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
718 if (dim1 == dim2 && dim1_type == dim2_type)
721 if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
724 if (!dim1->ids && !dim2->ids)
727 for (i = 0; i < n(dim1, dim1_type); ++i) {
728 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
734 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
735 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
740 return match(dim1, dim1_type, dim2, dim2_type);
743 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
744 unsigned first, unsigned n, __isl_keep isl_id **ids)
748 for (i = 0; i < n ; ++i)
749 ids[i] = get_id(dim, type, first + i);
752 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
753 unsigned nparam, unsigned n_in, unsigned n_out)
759 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
762 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
763 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
764 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
766 dim = isl_space_cow(dim);
769 ids = isl_calloc_array(dim->ctx, isl_id *,
770 nparam + n_in + n_out);
773 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
774 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
775 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
778 dim->n_id = nparam + n_in + n_out;
780 dim->nparam = nparam;
791 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
792 enum isl_dim_type type, unsigned n)
796 dim = isl_space_reset(dim, type);
799 dim = isl_space_extend(dim,
800 dim->nparam + n, dim->n_in, dim->n_out);
801 if (dim && dim->nested[0] &&
802 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
805 if (dim && dim->nested[1] &&
806 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
811 return isl_space_extend(dim,
812 dim->nparam, dim->n_in + n, dim->n_out);
814 return isl_space_extend(dim,
815 dim->nparam, dim->n_in, dim->n_out + n);
817 isl_die(dim->ctx, isl_error_invalid,
818 "cannot add dimensions of specified type", goto error);
825 static int valid_dim_type(enum isl_dim_type type)
837 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
838 enum isl_dim_type type, unsigned pos, unsigned n)
845 return isl_space_reset(dim, type);
847 if (!valid_dim_type(type))
848 isl_die(dim->ctx, isl_error_invalid,
849 "cannot insert dimensions of specified type",
852 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
854 dim = isl_space_cow(dim);
862 int *size = s - isl_dim_param;
863 ids = isl_calloc_array(dim->ctx, isl_id *,
864 dim->nparam + dim->n_in + dim->n_out + n);
868 size[isl_dim_param] = dim->nparam;
869 size[isl_dim_in] = dim->n_in;
870 size[isl_dim_out] = dim->n_out;
871 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
873 get_ids(dim, t, 0, size[t], ids + off);
876 get_ids(dim, t, 0, pos, ids + off);
878 get_ids(dim, t, pos, size[t] - pos, ids + off);
879 off += size[t] - pos;
884 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
887 case isl_dim_param: dim->nparam += n; break;
888 case isl_dim_in: dim->n_in += n; break;
889 case isl_dim_out: dim->n_out += n; break;
892 dim = isl_space_reset(dim, type);
900 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
901 enum isl_dim_type dst_type, unsigned dst_pos,
902 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
911 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
914 if (dst_type == src_type && dst_pos == src_pos)
917 isl_assert(dim->ctx, dst_type != src_type, goto error);
919 dim = isl_space_reset(dim, src_type);
920 dim = isl_space_reset(dim, dst_type);
922 dim = isl_space_cow(dim);
931 int *size = s - isl_dim_param;
932 ids = isl_calloc_array(dim->ctx, isl_id *,
933 dim->nparam + dim->n_in + dim->n_out);
937 size[isl_dim_param] = dim->nparam;
938 size[isl_dim_in] = dim->n_in;
939 size[isl_dim_out] = dim->n_out;
940 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
942 get_ids(dim, t, 0, dst_pos, ids + off);
944 get_ids(dim, src_type, src_pos, n, ids + off);
946 get_ids(dim, t, dst_pos, size[t] - dst_pos,
948 off += size[t] - dst_pos;
949 } else if (t == src_type) {
950 get_ids(dim, t, 0, src_pos, ids + off);
952 get_ids(dim, t, src_pos + n,
953 size[t] - src_pos - n, ids + off);
954 off += size[t] - src_pos - n;
956 get_ids(dim, t, 0, size[t], ids + off);
962 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
966 case isl_dim_param: dim->nparam += n; break;
967 case isl_dim_in: dim->n_in += n; break;
968 case isl_dim_out: dim->n_out += n; break;
973 case isl_dim_param: dim->nparam -= n; break;
974 case isl_dim_in: dim->n_in -= n; break;
975 case isl_dim_out: dim->n_out -= n; break;
979 if (dst_type != isl_dim_param && src_type != isl_dim_param)
982 for (i = 0; i < 2; ++i) {
985 dim->nested[i] = isl_space_replace(dim->nested[i],
997 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
998 __isl_take isl_space *right)
1002 if (!left || !right)
1005 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1007 isl_assert(left->ctx,
1008 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
1011 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1015 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1016 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1017 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1019 if (dim && left->tuple_id[0] &&
1020 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1022 if (dim && right->tuple_id[1] &&
1023 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1025 if (dim && left->nested[0] &&
1026 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1028 if (dim && right->nested[1] &&
1029 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1032 isl_space_free(left);
1033 isl_space_free(right);
1037 isl_space_free(left);
1038 isl_space_free(right);
1042 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1043 __isl_take isl_space *right)
1045 isl_space *dom1, *dom2, *nest1, *nest2;
1047 if (!left || !right)
1050 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1053 dom1 = isl_space_domain(isl_space_copy(left));
1054 dom2 = isl_space_domain(isl_space_copy(right));
1055 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1057 dom1 = isl_space_range(left);
1058 dom2 = isl_space_range(right);
1059 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1061 return isl_space_join(isl_space_reverse(nest1), nest2);
1063 isl_space_free(left);
1064 isl_space_free(right);
1068 /* Given two spaces { A -> C } and { B -> C }, construct the space
1071 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1072 __isl_take isl_space *right)
1074 isl_space *ran, *dom1, *dom2, *nest;
1076 if (!left || !right)
1079 if (!match(left, isl_dim_param, right, isl_dim_param))
1080 isl_die(left->ctx, isl_error_invalid,
1081 "parameters need to match", goto error);
1082 if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1083 isl_die(left->ctx, isl_error_invalid,
1084 "ranges need to match", goto error);
1086 ran = isl_space_range(isl_space_copy(left));
1088 dom1 = isl_space_domain(left);
1089 dom2 = isl_space_domain(right);
1090 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1092 return isl_space_join(isl_space_reverse(nest), ran);
1094 isl_space_free(left);
1095 isl_space_free(right);
1099 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1100 __isl_take isl_space *right)
1102 isl_space *dom, *ran1, *ran2, *nest;
1104 if (!left || !right)
1107 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1109 if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1110 isl_die(left->ctx, isl_error_invalid,
1111 "domains need to match", goto error);
1113 dom = isl_space_domain(isl_space_copy(left));
1115 ran1 = isl_space_range(left);
1116 ran2 = isl_space_range(right);
1117 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1119 return isl_space_join(isl_space_reverse(dom), nest);
1121 isl_space_free(left);
1122 isl_space_free(right);
1126 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1129 isl_id **ids = NULL;
1133 ctx = isl_space_get_ctx(dim);
1134 if (!isl_space_is_set(dim))
1135 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1136 dim = isl_space_cow(dim);
1140 ids = isl_calloc_array(dim->ctx, isl_id *,
1141 dim->nparam + dim->n_out + dim->n_out);
1144 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1145 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1147 dim->n_in = dim->n_out;
1151 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1152 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1154 isl_id_free(dim->tuple_id[0]);
1155 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1156 isl_space_free(dim->nested[0]);
1157 dim->nested[0] = isl_space_copy(dim->nested[1]);
1160 isl_space_free(dim);
1164 __isl_give isl_space *isl_space_map_from_domain_and_range(
1165 __isl_take isl_space *domain, __isl_take isl_space *range)
1167 if (!domain || !range)
1169 if (!isl_space_is_set(domain))
1170 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1171 "domain is not a set space", goto error);
1172 if (!isl_space_is_set(range))
1173 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1174 "range is not a set space", goto error);
1175 return isl_space_join(isl_space_reverse(domain), range);
1177 isl_space_free(domain);
1178 isl_space_free(range);
1182 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1183 enum isl_dim_type type,
1184 unsigned first, unsigned n, __isl_take isl_id **ids)
1188 for (i = 0; i < n ; ++i)
1189 dim = set_id(dim, type, first + i, ids[i]);
1194 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1198 isl_id **ids = NULL;
1203 if (match(dim, isl_dim_in, dim, isl_dim_out))
1206 dim = isl_space_cow(dim);
1210 id = dim->tuple_id[0];
1211 dim->tuple_id[0] = dim->tuple_id[1];
1212 dim->tuple_id[1] = id;
1214 nested = dim->nested[0];
1215 dim->nested[0] = dim->nested[1];
1216 dim->nested[1] = nested;
1219 ids = isl_alloc_array(dim->ctx, isl_id *,
1220 dim->n_in + dim->n_out);
1223 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1224 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1228 dim->n_in = dim->n_out;
1232 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1233 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1240 isl_space_free(dim);
1244 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1245 enum isl_dim_type type, unsigned first, unsigned num)
1253 return isl_space_reset(dim, type);
1255 if (!valid_dim_type(type))
1256 isl_die(dim->ctx, isl_error_invalid,
1257 "cannot drop dimensions of specified type", goto error);
1259 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1260 dim = isl_space_cow(dim);
1264 dim = extend_ids(dim);
1267 for (i = 0; i < num; ++i)
1268 isl_id_free(get_id(dim, type, first + i));
1269 for (i = first+num; i < n(dim, type); ++i)
1270 set_id(dim, type, i - num, get_id(dim, type, i));
1273 get_ids(dim, isl_dim_in, 0, dim->n_in,
1274 dim->ids + offset(dim, isl_dim_in) - num);
1276 get_ids(dim, isl_dim_out, 0, dim->n_out,
1277 dim->ids + offset(dim, isl_dim_out) - num);
1284 case isl_dim_param: dim->nparam -= num; break;
1285 case isl_dim_in: dim->n_in -= num; break;
1286 case isl_dim_out: dim->n_out -= num; break;
1289 dim = isl_space_reset(dim, type);
1290 if (type == isl_dim_param) {
1291 if (dim && dim->nested[0] &&
1292 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1293 isl_dim_param, first, num)))
1295 if (dim && dim->nested[1] &&
1296 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1297 isl_dim_param, first, num)))
1302 isl_space_free(dim);
1306 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1307 unsigned first, unsigned n)
1311 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1314 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1315 unsigned first, unsigned n)
1319 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1322 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1326 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1327 dim = isl_space_reverse(dim);
1328 dim = mark_as_set(dim);
1332 __isl_give isl_space *isl_space_from_domain(__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 dim = isl_space_reverse(dim);
1340 dim = isl_space_reset(dim, isl_dim_out);
1343 isl_space_free(dim);
1347 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1351 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1352 dim = mark_as_set(dim);
1356 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1360 if (!isl_space_is_set(dim))
1361 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1362 "not a set space", goto error);
1363 return isl_space_reset(dim, isl_dim_in);
1365 isl_space_free(dim);
1369 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1371 if (isl_space_is_params(space))
1373 space = isl_space_drop_dims(space,
1374 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1375 space = isl_space_drop_dims(space,
1376 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1377 space = mark_as_params(space);
1381 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1385 if (!isl_space_is_params(space))
1386 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1387 "not a parameter space", goto error);
1388 return isl_space_reset(space, isl_dim_set);
1390 isl_space_free(space);
1394 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1396 dim = isl_space_cow(dim);
1400 dim->n_out += dim->n_in;
1402 dim = isl_space_reset(dim, isl_dim_in);
1403 dim = isl_space_reset(dim, isl_dim_out);
1408 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1416 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1417 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1418 dim = isl_space_cow(dim);
1421 dim->n_out += dim->nparam + dim->n_in + n_div;
1425 for (i = 0; i < dim->n_id; ++i)
1426 isl_id_free(get_id(dim, isl_dim_out, i));
1428 dim = isl_space_reset(dim, isl_dim_in);
1429 dim = isl_space_reset(dim, isl_dim_out);
1434 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1440 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1441 isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1442 isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1445 /* Is space1 equal to the domain of space2?
1447 int isl_space_is_domain(__isl_keep isl_space *space1,
1448 __isl_keep isl_space *space2)
1450 if (!space1 || !space2)
1452 if (!isl_space_is_set(space1))
1454 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1455 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1458 int isl_space_compatible(__isl_keep isl_space *dim1,
1459 __isl_keep isl_space *dim2)
1461 return dim1->nparam == dim2->nparam &&
1462 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1465 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1473 hash = isl_hash_builtin(hash, dim->nparam);
1474 hash = isl_hash_builtin(hash, dim->n_in);
1475 hash = isl_hash_builtin(hash, dim->n_out);
1477 for (i = 0; i < dim->nparam; ++i) {
1478 id = get_id(dim, isl_dim_param, i);
1479 hash = isl_hash_id(hash, id);
1482 id = tuple_id(dim, isl_dim_in);
1483 hash = isl_hash_id(hash, id);
1484 id = tuple_id(dim, isl_dim_out);
1485 hash = isl_hash_id(hash, id);
1487 hash = isl_hash_dim(hash, dim->nested[0]);
1488 hash = isl_hash_dim(hash, dim->nested[1]);
1493 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1500 hash = isl_hash_init();
1501 hash = isl_hash_dim(hash, dim);
1506 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1511 if (!isl_space_is_set(dim))
1514 return dim->nested[1] != NULL;
1517 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1524 wrap = isl_space_set_alloc(dim->ctx,
1525 dim->nparam, dim->n_in + dim->n_out);
1527 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1528 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1529 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1534 wrap->nested[1] = dim;
1538 isl_space_free(dim);
1542 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1549 if (!isl_space_is_wrapping(dim))
1550 isl_die(dim->ctx, isl_error_invalid, "not a wrapping dim",
1553 unwrap = isl_space_copy(dim->nested[1]);
1554 isl_space_free(dim);
1558 isl_space_free(dim);
1562 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1564 if (type != isl_dim_in && type != isl_dim_out)
1568 if (dim->tuple_id[type - isl_dim_in])
1570 if (dim->nested[type - isl_dim_in])
1575 int isl_space_may_be_set(__isl_keep isl_space *dim)
1579 if (isl_space_is_set(dim))
1581 if (isl_space_dim(dim, isl_dim_in) != 0)
1583 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1588 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1589 enum isl_dim_type type)
1591 if (!isl_space_is_named_or_nested(dim, type))
1594 dim = isl_space_cow(dim);
1598 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1599 dim->tuple_id[type - isl_dim_in] = NULL;
1600 isl_space_free(dim->nested[type - isl_dim_in]);
1601 dim->nested[type - isl_dim_in] = NULL;
1606 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1610 if (!dim->nested[0] && !dim->nested[1])
1614 dim = isl_space_reset(dim, isl_dim_in);
1615 if (dim && dim->nested[1])
1616 dim = isl_space_reset(dim, isl_dim_out);
1621 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1625 if (!dim->nested[0])
1628 return isl_space_reset(dim, isl_dim_in);
1631 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1635 if (!dim->nested[1])
1638 return isl_space_reset(dim, isl_dim_out);
1641 /* Replace the dimensions of the given type of dst by those of src.
1643 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1644 enum isl_dim_type type, __isl_keep isl_space *src)
1646 dst = isl_space_cow(dst);
1651 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1652 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1653 dst = copy_ids(dst, type, 0, src, type);
1655 if (dst && type == isl_dim_param) {
1657 for (i = 0; i <= 1; ++i) {
1658 if (!dst->nested[i])
1660 dst->nested[i] = isl_space_replace(dst->nested[i],
1662 if (!dst->nested[i])
1669 isl_space_free(dst);
1673 /* Given a dimension specification "dim" of a set, create a dimension
1674 * specification for the lift of the set. In particular, the result
1675 * is of the form [dim -> local[..]], with n_local variables in the
1676 * range of the wrapped map.
1678 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1680 isl_space *local_dim;
1685 local_dim = isl_space_dup(dim);
1686 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1687 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1688 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1689 dim = isl_space_join(isl_space_from_domain(dim),
1690 isl_space_from_range(local_dim));
1691 dim = isl_space_wrap(dim);
1692 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1697 int isl_space_can_zip(__isl_keep isl_space *dim)
1702 return dim->nested[0] && dim->nested[1];
1705 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1707 isl_space *dom, *ran;
1708 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1710 if (!isl_space_can_zip(dim))
1711 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1716 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1717 ran = isl_space_unwrap(isl_space_range(dim));
1718 dom_dom = isl_space_domain(isl_space_copy(dom));
1719 dom_ran = isl_space_range(dom);
1720 ran_dom = isl_space_domain(isl_space_copy(ran));
1721 ran_ran = isl_space_range(ran);
1722 dom = isl_space_join(isl_space_from_domain(dom_dom),
1723 isl_space_from_range(ran_dom));
1724 ran = isl_space_join(isl_space_from_domain(dom_ran),
1725 isl_space_from_range(ran_ran));
1726 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1727 isl_space_from_range(isl_space_wrap(ran)));
1729 isl_space_free(dim);
1733 /* Can we apply isl_space_curry to "space"?
1734 * That is, does it have a nested relation in its domain?
1736 int isl_space_can_curry(__isl_keep isl_space *space)
1741 return !!space->nested[0];
1744 /* Given a space (A -> B) -> C, return the corresponding space
1747 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
1749 isl_space *dom, *ran;
1750 isl_space *dom_dom, *dom_ran;
1755 if (!isl_space_can_curry(space))
1756 isl_die(space->ctx, isl_error_invalid,
1757 "space cannot be curried", goto error);
1759 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
1760 ran = isl_space_range(space);
1761 dom_dom = isl_space_domain(isl_space_copy(dom));
1762 dom_ran = isl_space_range(dom);
1763 ran = isl_space_join(isl_space_from_domain(dom_ran),
1764 isl_space_from_range(ran));
1765 return isl_space_join(isl_space_from_domain(dom_dom),
1766 isl_space_from_range(isl_space_wrap(ran)));
1768 isl_space_free(space);
1772 int isl_space_has_named_params(__isl_keep isl_space *dim)
1779 if (dim->nparam == 0)
1781 off = isl_space_offset(dim, isl_dim_param);
1782 if (off + dim->nparam > dim->n_id)
1784 for (i = 0; i < dim->nparam; ++i)
1785 if (!dim->ids[off + i])
1790 /* Align the initial parameters of dim1 to match the order in dim2.
1792 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1793 __isl_take isl_space *dim2)
1795 isl_reordering *exp;
1797 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1798 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1799 "parameter alignment requires named parameters",
1802 dim2 = isl_space_params(dim2);
1803 exp = isl_parameter_alignment_reordering(dim1, dim2);
1804 exp = isl_reordering_extend_space(exp, dim1);
1805 isl_space_free(dim2);
1808 dim1 = isl_space_copy(exp->dim);
1809 isl_reordering_free(exp);
1812 isl_space_free(dim1);
1813 isl_space_free(dim2);
1817 /* Given the space of set (domain), construct a space for a map
1818 * with as domain the given space and as range the range of "model".
1820 __isl_give isl_space *isl_space_extend_domain_with_range(
1821 __isl_take isl_space *domain, __isl_take isl_space *model)
1825 space = isl_space_from_domain(domain);
1826 space = isl_space_add_dims(space, isl_dim_out,
1827 isl_space_dim(model, isl_dim_out));
1828 if (isl_space_has_tuple_id(model, isl_dim_out))
1829 space = isl_space_set_tuple_id(space, isl_dim_out,
1830 isl_space_get_tuple_id(model, isl_dim_out));
1831 isl_space_free(model);