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 int isl_space_has_dim_id(__isl_keep isl_space *dim,
485 enum isl_dim_type type, unsigned pos)
489 return get_id(dim, type, pos) != NULL;
492 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
493 enum isl_dim_type type, unsigned pos)
497 if (!get_id(dim, type, pos))
498 isl_die(dim->ctx, isl_error_invalid,
499 "dim has no id", return NULL);
500 return isl_id_copy(get_id(dim, type, pos));
503 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
504 enum isl_dim_type type, const char *s)
512 return isl_space_reset_tuple_id(dim, type);
514 if (!name_ok(dim->ctx, s))
517 id = isl_id_alloc(dim->ctx, s, NULL);
518 return isl_space_set_tuple_id(dim, type, id);
524 /* Does the tuple have a name?
526 int isl_space_has_tuple_name(__isl_keep isl_space *space,
527 enum isl_dim_type type)
531 if (!space_can_have_id(space, type))
533 id = space->tuple_id[type - isl_dim_in];
534 return id && id->name;
537 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
538 enum isl_dim_type type)
543 if (type != isl_dim_in && type != isl_dim_out)
545 id = dim->tuple_id[type - isl_dim_in];
546 return id ? id->name : NULL;
549 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
550 enum isl_dim_type type, unsigned pos,
557 if (!name_ok(dim->ctx, s))
559 id = isl_id_alloc(dim->ctx, s, NULL);
560 return isl_space_set_dim_id(dim, type, pos, id);
566 /* Does the given dimension have a name?
568 int isl_space_has_dim_name(__isl_keep isl_space *space,
569 enum isl_dim_type type, unsigned pos)
575 id = get_id(space, type, pos);
576 return id && id->name;
579 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
580 enum isl_dim_type type, unsigned pos)
582 isl_id *id = get_id(dim, type, pos);
583 return id ? id->name : NULL;
586 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
587 __isl_keep isl_id *id)
596 offset = isl_space_offset(dim, type);
597 n = isl_space_dim(dim, type);
598 for (i = 0; i < n && offset + i < dim->n_id; ++i)
599 if (dim->ids[offset + i] == id)
605 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
606 enum isl_dim_type type, const char *name)
615 offset = isl_space_offset(space, type);
616 n = isl_space_dim(space, type);
617 for (i = 0; i < n && offset + i < space->n_id; ++i)
618 if (space->ids[offset + i]->name &&
619 !strcmp(space->ids[offset + i]->name, name))
625 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
626 enum isl_dim_type type)
630 if (type == isl_dim_in)
631 return dim->tuple_id[0];
632 if (type == isl_dim_out)
633 return dim->tuple_id[1];
637 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
638 enum isl_dim_type type)
642 if (type == isl_dim_in)
643 return dim->nested[0];
644 if (type == isl_dim_out)
645 return dim->nested[1];
649 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
650 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
653 isl_space *nested1, *nested2;
658 if (dim1 == dim2 && dim1_type == dim2_type)
661 if (n(dim1, dim1_type) != n(dim2, dim2_type))
663 id1 = tuple_id(dim1, dim1_type);
664 id2 = tuple_id(dim2, dim2_type);
667 if (id1 && id1 != id2)
669 nested1 = nested(dim1, dim1_type);
670 nested2 = nested(dim2, dim2_type);
671 if (!nested1 ^ !nested2)
673 if (nested1 && !isl_space_is_equal(nested1, nested2))
678 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
679 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
683 if (dim1 == dim2 && dim1_type == dim2_type)
686 if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
689 if (!dim1->ids && !dim2->ids)
692 for (i = 0; i < n(dim1, dim1_type); ++i) {
693 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
699 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
700 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
705 return match(dim1, dim1_type, dim2, dim2_type);
708 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
709 unsigned first, unsigned n, __isl_keep isl_id **ids)
713 for (i = 0; i < n ; ++i)
714 ids[i] = get_id(dim, type, first + i);
717 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
718 unsigned nparam, unsigned n_in, unsigned n_out)
724 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
727 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
728 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
729 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
731 dim = isl_space_cow(dim);
734 ids = isl_calloc_array(dim->ctx, isl_id *,
735 nparam + n_in + n_out);
738 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
739 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
740 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
743 dim->n_id = nparam + n_in + n_out;
745 dim->nparam = nparam;
756 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
757 enum isl_dim_type type, unsigned n)
761 dim = isl_space_reset(dim, type);
764 dim = isl_space_extend(dim,
765 dim->nparam + n, dim->n_in, dim->n_out);
766 if (dim && dim->nested[0] &&
767 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
770 if (dim && dim->nested[1] &&
771 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
776 return isl_space_extend(dim,
777 dim->nparam, dim->n_in + n, dim->n_out);
779 return isl_space_extend(dim,
780 dim->nparam, dim->n_in, dim->n_out + n);
782 isl_die(dim->ctx, isl_error_invalid,
783 "cannot add dimensions of specified type", goto error);
790 static int valid_dim_type(enum isl_dim_type type)
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);
865 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
866 enum isl_dim_type dst_type, unsigned dst_pos,
867 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
876 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
879 if (dst_type == src_type && dst_pos == src_pos)
882 isl_assert(dim->ctx, dst_type != src_type, goto error);
884 dim = isl_space_reset(dim, src_type);
885 dim = isl_space_reset(dim, dst_type);
887 dim = isl_space_cow(dim);
896 int *size = s - isl_dim_param;
897 ids = isl_calloc_array(dim->ctx, isl_id *,
898 dim->nparam + dim->n_in + dim->n_out);
902 size[isl_dim_param] = dim->nparam;
903 size[isl_dim_in] = dim->n_in;
904 size[isl_dim_out] = dim->n_out;
905 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
907 get_ids(dim, t, 0, dst_pos, ids + off);
909 get_ids(dim, src_type, src_pos, n, ids + off);
911 get_ids(dim, t, dst_pos, size[t] - dst_pos,
913 off += size[t] - dst_pos;
914 } else if (t == src_type) {
915 get_ids(dim, t, 0, src_pos, ids + off);
917 get_ids(dim, t, src_pos + n,
918 size[t] - src_pos - n, ids + off);
919 off += size[t] - src_pos - n;
921 get_ids(dim, t, 0, size[t], ids + off);
927 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
931 case isl_dim_param: dim->nparam += n; break;
932 case isl_dim_in: dim->n_in += n; break;
933 case isl_dim_out: dim->n_out += n; break;
938 case isl_dim_param: dim->nparam -= n; break;
939 case isl_dim_in: dim->n_in -= n; break;
940 case isl_dim_out: dim->n_out -= n; break;
944 if (dst_type != isl_dim_param && src_type != isl_dim_param)
947 for (i = 0; i < 2; ++i) {
950 dim->nested[i] = isl_space_replace(dim->nested[i],
962 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
963 __isl_take isl_space *right)
970 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
972 isl_assert(left->ctx,
973 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
976 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
980 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
981 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
982 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
984 if (dim && left->tuple_id[0] &&
985 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
987 if (dim && right->tuple_id[1] &&
988 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
990 if (dim && left->nested[0] &&
991 !(dim->nested[0] = isl_space_copy(left->nested[0])))
993 if (dim && right->nested[1] &&
994 !(dim->nested[1] = isl_space_copy(right->nested[1])))
997 isl_space_free(left);
998 isl_space_free(right);
1002 isl_space_free(left);
1003 isl_space_free(right);
1007 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1008 __isl_take isl_space *right)
1010 isl_space *dom1, *dom2, *nest1, *nest2;
1012 if (!left || !right)
1015 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1018 dom1 = isl_space_domain(isl_space_copy(left));
1019 dom2 = isl_space_domain(isl_space_copy(right));
1020 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1022 dom1 = isl_space_range(left);
1023 dom2 = isl_space_range(right);
1024 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1026 return isl_space_join(isl_space_reverse(nest1), nest2);
1028 isl_space_free(left);
1029 isl_space_free(right);
1033 /* Given two spaces { A -> C } and { B -> C }, construct the space
1036 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1037 __isl_take isl_space *right)
1039 isl_space *ran, *dom1, *dom2, *nest;
1041 if (!left || !right)
1044 if (!match(left, isl_dim_param, right, isl_dim_param))
1045 isl_die(left->ctx, isl_error_invalid,
1046 "parameters need to match", goto error);
1047 if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1048 isl_die(left->ctx, isl_error_invalid,
1049 "ranges need to match", goto error);
1051 ran = isl_space_range(isl_space_copy(left));
1053 dom1 = isl_space_domain(left);
1054 dom2 = isl_space_domain(right);
1055 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1057 return isl_space_join(isl_space_reverse(nest), ran);
1059 isl_space_free(left);
1060 isl_space_free(right);
1064 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1065 __isl_take isl_space *right)
1067 isl_space *dom, *ran1, *ran2, *nest;
1069 if (!left || !right)
1072 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1074 if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1075 isl_die(left->ctx, isl_error_invalid,
1076 "domains need to match", goto error);
1078 dom = isl_space_domain(isl_space_copy(left));
1080 ran1 = isl_space_range(left);
1081 ran2 = isl_space_range(right);
1082 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1084 return isl_space_join(isl_space_reverse(dom), nest);
1086 isl_space_free(left);
1087 isl_space_free(right);
1091 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1094 isl_id **ids = NULL;
1098 ctx = isl_space_get_ctx(dim);
1099 if (!isl_space_is_set(dim))
1100 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1101 dim = isl_space_cow(dim);
1105 ids = isl_calloc_array(dim->ctx, isl_id *,
1106 dim->nparam + dim->n_out + dim->n_out);
1109 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1110 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1112 dim->n_in = dim->n_out;
1116 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1117 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1119 isl_id_free(dim->tuple_id[0]);
1120 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1121 isl_space_free(dim->nested[0]);
1122 dim->nested[0] = isl_space_copy(dim->nested[1]);
1125 isl_space_free(dim);
1129 __isl_give isl_space *isl_space_map_from_domain_and_range(
1130 __isl_take isl_space *domain, __isl_take isl_space *range)
1132 if (!domain || !range)
1134 if (!isl_space_is_set(domain))
1135 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1136 "domain is not a set space", goto error);
1137 if (!isl_space_is_set(range))
1138 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1139 "range is not a set space", goto error);
1140 return isl_space_join(isl_space_reverse(domain), range);
1142 isl_space_free(domain);
1143 isl_space_free(range);
1147 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1148 enum isl_dim_type type,
1149 unsigned first, unsigned n, __isl_take isl_id **ids)
1153 for (i = 0; i < n ; ++i)
1154 dim = set_id(dim, type, first + i, ids[i]);
1159 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1163 isl_id **ids = NULL;
1168 if (match(dim, isl_dim_in, dim, isl_dim_out))
1171 dim = isl_space_cow(dim);
1175 id = dim->tuple_id[0];
1176 dim->tuple_id[0] = dim->tuple_id[1];
1177 dim->tuple_id[1] = id;
1179 nested = dim->nested[0];
1180 dim->nested[0] = dim->nested[1];
1181 dim->nested[1] = nested;
1184 ids = isl_alloc_array(dim->ctx, isl_id *,
1185 dim->n_in + dim->n_out);
1188 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1189 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1193 dim->n_in = dim->n_out;
1197 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1198 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1205 isl_space_free(dim);
1209 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1210 enum isl_dim_type type, unsigned first, unsigned num)
1218 return isl_space_reset(dim, type);
1220 if (!valid_dim_type(type))
1221 isl_die(dim->ctx, isl_error_invalid,
1222 "cannot drop dimensions of specified type", goto error);
1224 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1225 dim = isl_space_cow(dim);
1229 dim = extend_ids(dim);
1232 for (i = 0; i < num; ++i)
1233 isl_id_free(get_id(dim, type, first + i));
1234 for (i = first+num; i < n(dim, type); ++i)
1235 set_id(dim, type, i - num, get_id(dim, type, i));
1238 get_ids(dim, isl_dim_in, 0, dim->n_in,
1239 dim->ids + offset(dim, isl_dim_in) - num);
1241 get_ids(dim, isl_dim_out, 0, dim->n_out,
1242 dim->ids + offset(dim, isl_dim_out) - num);
1249 case isl_dim_param: dim->nparam -= num; break;
1250 case isl_dim_in: dim->n_in -= num; break;
1251 case isl_dim_out: dim->n_out -= num; break;
1254 dim = isl_space_reset(dim, type);
1255 if (type == isl_dim_param) {
1256 if (dim && dim->nested[0] &&
1257 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1258 isl_dim_param, first, num)))
1260 if (dim && dim->nested[1] &&
1261 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1262 isl_dim_param, first, num)))
1267 isl_space_free(dim);
1271 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1272 unsigned first, unsigned n)
1276 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1279 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1280 unsigned first, unsigned n)
1284 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1287 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1291 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1292 dim = isl_space_reverse(dim);
1293 dim = mark_as_set(dim);
1297 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1301 if (!isl_space_is_set(dim))
1302 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1303 "not a set space", goto error);
1304 dim = isl_space_reverse(dim);
1305 dim = isl_space_reset(dim, isl_dim_out);
1308 isl_space_free(dim);
1312 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1316 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1317 dim = mark_as_set(dim);
1321 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1325 if (!isl_space_is_set(dim))
1326 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1327 "not a set space", goto error);
1328 return isl_space_reset(dim, isl_dim_in);
1330 isl_space_free(dim);
1334 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1336 if (isl_space_is_params(space))
1338 space = isl_space_drop_dims(space,
1339 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1340 space = isl_space_drop_dims(space,
1341 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1342 space = mark_as_params(space);
1346 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1350 if (!isl_space_is_params(space))
1351 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1352 "not a parameter space", goto error);
1353 return isl_space_reset(space, isl_dim_set);
1355 isl_space_free(space);
1359 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1361 dim = isl_space_cow(dim);
1365 dim->n_out += dim->n_in;
1367 dim = isl_space_reset(dim, isl_dim_in);
1368 dim = isl_space_reset(dim, isl_dim_out);
1373 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1381 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1382 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1383 dim = isl_space_cow(dim);
1386 dim->n_out += dim->nparam + dim->n_in + n_div;
1390 for (i = 0; i < dim->n_id; ++i)
1391 isl_id_free(get_id(dim, isl_dim_out, i));
1393 dim = isl_space_reset(dim, isl_dim_in);
1394 dim = isl_space_reset(dim, isl_dim_out);
1399 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1405 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1406 isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1407 isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1410 /* Is space1 equal to the domain of space2?
1412 int isl_space_is_domain(__isl_keep isl_space *space1,
1413 __isl_keep isl_space *space2)
1415 if (!space1 || !space2)
1417 if (!isl_space_is_set(space1))
1419 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1420 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1423 int isl_space_compatible(__isl_keep isl_space *dim1,
1424 __isl_keep isl_space *dim2)
1426 return dim1->nparam == dim2->nparam &&
1427 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1430 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1438 hash = isl_hash_builtin(hash, dim->nparam);
1439 hash = isl_hash_builtin(hash, dim->n_in);
1440 hash = isl_hash_builtin(hash, dim->n_out);
1442 for (i = 0; i < dim->nparam; ++i) {
1443 id = get_id(dim, isl_dim_param, i);
1444 hash = isl_hash_id(hash, id);
1447 id = tuple_id(dim, isl_dim_in);
1448 hash = isl_hash_id(hash, id);
1449 id = tuple_id(dim, isl_dim_out);
1450 hash = isl_hash_id(hash, id);
1452 hash = isl_hash_dim(hash, dim->nested[0]);
1453 hash = isl_hash_dim(hash, dim->nested[1]);
1458 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1465 hash = isl_hash_init();
1466 hash = isl_hash_dim(hash, dim);
1471 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1476 if (!isl_space_is_set(dim))
1479 return dim->nested[1] != NULL;
1482 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1489 wrap = isl_space_set_alloc(dim->ctx,
1490 dim->nparam, dim->n_in + dim->n_out);
1492 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1493 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1494 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1499 wrap->nested[1] = dim;
1503 isl_space_free(dim);
1507 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1514 if (!isl_space_is_wrapping(dim))
1515 isl_die(dim->ctx, isl_error_invalid, "not a wrapping dim",
1518 unwrap = isl_space_copy(dim->nested[1]);
1519 isl_space_free(dim);
1523 isl_space_free(dim);
1527 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1529 if (type != isl_dim_in && type != isl_dim_out)
1533 if (dim->tuple_id[type - isl_dim_in])
1535 if (dim->nested[type - isl_dim_in])
1540 int isl_space_may_be_set(__isl_keep isl_space *dim)
1544 if (isl_space_is_set(dim))
1546 if (isl_space_dim(dim, isl_dim_in) != 0)
1548 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1553 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1554 enum isl_dim_type type)
1556 if (!isl_space_is_named_or_nested(dim, type))
1559 dim = isl_space_cow(dim);
1563 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1564 dim->tuple_id[type - isl_dim_in] = NULL;
1565 isl_space_free(dim->nested[type - isl_dim_in]);
1566 dim->nested[type - isl_dim_in] = NULL;
1571 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1575 if (!dim->nested[0] && !dim->nested[1])
1579 dim = isl_space_reset(dim, isl_dim_in);
1580 if (dim && dim->nested[1])
1581 dim = isl_space_reset(dim, isl_dim_out);
1586 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1590 if (!dim->nested[0])
1593 return isl_space_reset(dim, isl_dim_in);
1596 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1600 if (!dim->nested[1])
1603 return isl_space_reset(dim, isl_dim_out);
1606 /* Replace the dimensions of the given type of dst by those of src.
1608 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1609 enum isl_dim_type type, __isl_keep isl_space *src)
1611 dst = isl_space_cow(dst);
1616 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1617 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1618 dst = copy_ids(dst, type, 0, src, type);
1620 if (dst && type == isl_dim_param) {
1622 for (i = 0; i <= 1; ++i) {
1623 if (!dst->nested[i])
1625 dst->nested[i] = isl_space_replace(dst->nested[i],
1627 if (!dst->nested[i])
1634 isl_space_free(dst);
1638 /* Given a dimension specification "dim" of a set, create a dimension
1639 * specification for the lift of the set. In particular, the result
1640 * is of the form [dim -> local[..]], with n_local variables in the
1641 * range of the wrapped map.
1643 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1645 isl_space *local_dim;
1650 local_dim = isl_space_dup(dim);
1651 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1652 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1653 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1654 dim = isl_space_join(isl_space_from_domain(dim),
1655 isl_space_from_range(local_dim));
1656 dim = isl_space_wrap(dim);
1657 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1662 int isl_space_can_zip(__isl_keep isl_space *dim)
1667 return dim->nested[0] && dim->nested[1];
1670 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1672 isl_space *dom, *ran;
1673 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1675 if (!isl_space_can_zip(dim))
1676 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1681 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1682 ran = isl_space_unwrap(isl_space_range(dim));
1683 dom_dom = isl_space_domain(isl_space_copy(dom));
1684 dom_ran = isl_space_range(dom);
1685 ran_dom = isl_space_domain(isl_space_copy(ran));
1686 ran_ran = isl_space_range(ran);
1687 dom = isl_space_join(isl_space_from_domain(dom_dom),
1688 isl_space_from_range(ran_dom));
1689 ran = isl_space_join(isl_space_from_domain(dom_ran),
1690 isl_space_from_range(ran_ran));
1691 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1692 isl_space_from_range(isl_space_wrap(ran)));
1694 isl_space_free(dim);
1698 /* Can we apply isl_space_curry to "space"?
1699 * That is, does it have a nested relation in its domain?
1701 int isl_space_can_curry(__isl_keep isl_space *space)
1706 return !!space->nested[0];
1709 /* Given a space (A -> B) -> C, return the corresponding space
1712 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
1714 isl_space *dom, *ran;
1715 isl_space *dom_dom, *dom_ran;
1720 if (!isl_space_can_curry(space))
1721 isl_die(space->ctx, isl_error_invalid,
1722 "space cannot be curried", goto error);
1724 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
1725 ran = isl_space_range(space);
1726 dom_dom = isl_space_domain(isl_space_copy(dom));
1727 dom_ran = isl_space_range(dom);
1728 ran = isl_space_join(isl_space_from_domain(dom_ran),
1729 isl_space_from_range(ran));
1730 return isl_space_join(isl_space_from_domain(dom_dom),
1731 isl_space_from_range(isl_space_wrap(ran)));
1733 isl_space_free(space);
1737 int isl_space_has_named_params(__isl_keep isl_space *dim)
1744 if (dim->nparam == 0)
1746 off = isl_space_offset(dim, isl_dim_param);
1747 if (off + dim->nparam > dim->n_id)
1749 for (i = 0; i < dim->nparam; ++i)
1750 if (!dim->ids[off + i])
1755 /* Align the initial parameters of dim1 to match the order in dim2.
1757 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1758 __isl_take isl_space *dim2)
1760 isl_reordering *exp;
1762 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1763 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1764 "parameter alignment requires named parameters",
1767 dim2 = isl_space_params(dim2);
1768 exp = isl_parameter_alignment_reordering(dim1, dim2);
1769 exp = isl_reordering_extend_space(exp, dim1);
1770 isl_space_free(dim2);
1773 dim1 = isl_space_copy(exp->dim);
1774 isl_reordering_free(exp);
1777 isl_space_free(dim1);
1778 isl_space_free(dim2);
1782 /* Given the space of set (domain), construct a space for a map
1783 * with as domain the given space and as range the range of "model".
1785 __isl_give isl_space *isl_space_extend_domain_with_range(
1786 __isl_take isl_space *domain, __isl_take isl_space *model)
1790 space = isl_space_from_domain(domain);
1791 space = isl_space_add_dims(space, isl_dim_out,
1792 isl_space_dim(model, isl_dim_out));
1793 if (isl_space_has_tuple_id(model, isl_dim_out))
1794 space = isl_space_set_tuple_id(space, isl_dim_out,
1795 isl_space_get_tuple_id(model, isl_dim_out));
1796 isl_space_free(model);