2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the MIT license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_space_private.h>
15 #include <isl_id_private.h>
16 #include <isl_reordering.h>
18 isl_ctx *isl_space_get_ctx(__isl_keep isl_space *dim)
20 return dim ? dim->ctx : NULL;
23 __isl_give isl_space *isl_space_alloc(isl_ctx *ctx,
24 unsigned nparam, unsigned n_in, unsigned n_out)
28 dim = isl_alloc_type(ctx, struct isl_space);
39 dim->tuple_id[0] = NULL;
40 dim->tuple_id[1] = NULL;
42 dim->nested[0] = NULL;
43 dim->nested[1] = NULL;
51 /* Mark the space as being that of a set, by setting the domain tuple
54 static __isl_give isl_space *mark_as_set(__isl_take isl_space *space)
56 space = isl_space_cow(space);
59 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
63 /* Is the space that of a set?
65 int isl_space_is_set(__isl_keep isl_space *space)
69 if (space->n_in != 0 || space->nested[0])
71 if (space->tuple_id[0] != &isl_id_none)
76 /* Is the given space that of a map?
78 int isl_space_is_map(__isl_keep isl_space *space)
82 return space->tuple_id[0] != &isl_id_none &&
83 space->tuple_id[1] != &isl_id_none;
86 __isl_give isl_space *isl_space_set_alloc(isl_ctx *ctx,
87 unsigned nparam, unsigned dim)
90 space = isl_space_alloc(ctx, nparam, 0, dim);
91 space = mark_as_set(space);
95 /* Mark the space as being that of a parameter domain, by setting
96 * both tuples to isl_id_none.
98 static __isl_give isl_space *mark_as_params(isl_space *space)
102 space = isl_space_set_tuple_id(space, isl_dim_in, &isl_id_none);
103 space = isl_space_set_tuple_id(space, isl_dim_out, &isl_id_none);
107 /* Is the space that of a parameter domain?
109 int isl_space_is_params(__isl_keep isl_space *space)
113 if (space->n_in != 0 || space->nested[0] ||
114 space->n_out != 0 || space->nested[1])
116 if (space->tuple_id[0] != &isl_id_none)
118 if (space->tuple_id[1] != &isl_id_none)
123 /* Create a space for a parameter domain.
125 __isl_give isl_space *isl_space_params_alloc(isl_ctx *ctx, unsigned nparam)
128 space = isl_space_alloc(ctx, nparam, 0, 0);
129 space = mark_as_params(space);
133 static unsigned global_pos(__isl_keep isl_space *dim,
134 enum isl_dim_type type, unsigned pos)
136 struct isl_ctx *ctx = dim->ctx;
140 isl_assert(ctx, pos < dim->nparam,
141 return isl_space_dim(dim, isl_dim_all));
144 isl_assert(ctx, pos < dim->n_in,
145 return isl_space_dim(dim, isl_dim_all));
146 return pos + dim->nparam;
148 isl_assert(ctx, pos < dim->n_out,
149 return isl_space_dim(dim, isl_dim_all));
150 return pos + dim->nparam + dim->n_in;
152 isl_assert(ctx, 0, return isl_space_dim(dim, isl_dim_all));
154 return isl_space_dim(dim, isl_dim_all);
157 /* Extend length of ids array to the total number of dimensions.
159 static __isl_give isl_space *extend_ids(__isl_take isl_space *dim)
164 if (isl_space_dim(dim, isl_dim_all) <= dim->n_id)
168 dim->ids = isl_calloc_array(dim->ctx,
169 isl_id *, isl_space_dim(dim, isl_dim_all));
173 ids = isl_realloc_array(dim->ctx, dim->ids,
174 isl_id *, isl_space_dim(dim, isl_dim_all));
178 for (i = dim->n_id; i < isl_space_dim(dim, isl_dim_all); ++i)
182 dim->n_id = isl_space_dim(dim, isl_dim_all);
190 static __isl_give isl_space *set_id(__isl_take isl_space *dim,
191 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
193 dim = isl_space_cow(dim);
198 pos = global_pos(dim, type, pos);
199 if (pos == isl_space_dim(dim, isl_dim_all))
202 if (pos >= dim->n_id) {
205 dim = extend_ids(dim);
219 static __isl_keep isl_id *get_id(__isl_keep isl_space *dim,
220 enum isl_dim_type type, unsigned pos)
225 pos = global_pos(dim, type, pos);
226 if (pos == isl_space_dim(dim, isl_dim_all))
228 if (pos >= dim->n_id)
230 return dim->ids[pos];
233 static unsigned offset(__isl_keep isl_space *dim, enum isl_dim_type type)
236 case isl_dim_param: return 0;
237 case isl_dim_in: return dim->nparam;
238 case isl_dim_out: return dim->nparam + dim->n_in;
243 static unsigned n(__isl_keep isl_space *dim, enum isl_dim_type type)
246 case isl_dim_param: return dim->nparam;
247 case isl_dim_in: return dim->n_in;
248 case isl_dim_out: return dim->n_out;
249 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
254 unsigned isl_space_dim(__isl_keep isl_space *dim, enum isl_dim_type type)
261 unsigned isl_space_offset(__isl_keep isl_space *dim, enum isl_dim_type type)
265 return offset(dim, type);
268 static __isl_give isl_space *copy_ids(__isl_take isl_space *dst,
269 enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_space *src,
270 enum isl_dim_type src_type)
278 for (i = 0; i < n(src, src_type); ++i) {
279 id = get_id(src, src_type, i);
282 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
289 __isl_take isl_space *isl_space_dup(__isl_keep isl_space *dim)
294 dup = isl_space_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
297 if (dim->tuple_id[0] &&
298 !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
300 if (dim->tuple_id[1] &&
301 !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
303 if (dim->nested[0] && !(dup->nested[0] = isl_space_copy(dim->nested[0])))
305 if (dim->nested[1] && !(dup->nested[1] = isl_space_copy(dim->nested[1])))
309 dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
310 dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
311 dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
318 __isl_give isl_space *isl_space_cow(__isl_take isl_space *dim)
326 return isl_space_dup(dim);
329 __isl_give isl_space *isl_space_copy(__isl_keep isl_space *dim)
338 void *isl_space_free(__isl_take isl_space *dim)
348 isl_id_free(dim->tuple_id[0]);
349 isl_id_free(dim->tuple_id[1]);
351 isl_space_free(dim->nested[0]);
352 isl_space_free(dim->nested[1]);
354 for (i = 0; i < dim->n_id; ++i)
355 isl_id_free(dim->ids[i]);
357 isl_ctx_deref(dim->ctx);
364 /* Check if "s" is a valid dimension or tuple name.
365 * We currently only forbid names that look like a number.
367 * s is assumed to be non-NULL.
369 static int name_ok(isl_ctx *ctx, const char *s)
374 dummy = strtol(s, &p, 0);
376 isl_die(ctx, isl_error_invalid, "name looks like a number",
382 /* Is it possible for the given dimension type to have a tuple id?
384 static int space_can_have_id(__isl_keep isl_space *space,
385 enum isl_dim_type type)
389 if (isl_space_is_params(space))
390 isl_die(space->ctx, isl_error_invalid,
391 "parameter spaces don't have tuple ids", return 0);
392 if (isl_space_is_set(space) && type != isl_dim_set)
393 isl_die(space->ctx, isl_error_invalid,
394 "set spaces can only have a set id", return 0);
395 if (type != isl_dim_in && type != isl_dim_out)
396 isl_die(space->ctx, isl_error_invalid,
397 "only input, output and set tuples can have ids",
403 /* Does the tuple have an id?
405 int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
407 if (!space_can_have_id(dim, type))
409 return dim->tuple_id[type - isl_dim_in] != NULL;
412 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
413 enum isl_dim_type type)
419 has_id = isl_space_has_tuple_id(dim, type);
423 isl_die(dim->ctx, isl_error_invalid,
424 "tuple has no id", return NULL);
425 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
428 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
429 enum isl_dim_type type, __isl_take isl_id *id)
431 dim = isl_space_cow(dim);
434 if (type != isl_dim_in && type != isl_dim_out)
435 isl_die(dim->ctx, isl_error_invalid,
436 "only input, output and set tuples can have names",
439 isl_id_free(dim->tuple_id[type - isl_dim_in]);
440 dim->tuple_id[type - isl_dim_in] = id;
449 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
450 enum isl_dim_type type)
452 dim = isl_space_cow(dim);
455 if (type != isl_dim_in && type != isl_dim_out)
456 isl_die(dim->ctx, isl_error_invalid,
457 "only input, output and set tuples can have names",
460 isl_id_free(dim->tuple_id[type - isl_dim_in]);
461 dim->tuple_id[type - isl_dim_in] = NULL;
469 /* Set the id of the given dimension of "space" to "id".
470 * If the dimension already has an id, then it is replaced.
471 * If the dimension is a parameter, then we need to change it
472 * in the nested spaces (if any) as well.
474 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
475 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
477 space = isl_space_cow(space);
481 if (type == isl_dim_param) {
484 for (i = 0; i < 2; ++i) {
485 if (!space->nested[i])
488 isl_space_set_dim_id(space->nested[i],
489 type, pos, isl_id_copy(id));
490 if (!space->nested[i])
495 isl_id_free(get_id(space, type, pos));
496 return set_id(space, type, pos, id);
499 isl_space_free(space);
503 /* Reset the id of the given dimension of "space".
504 * If the dimension already has an id, then it is removed.
505 * If the dimension is a parameter, then we need to reset it
506 * in the nested spaces (if any) as well.
508 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
509 enum isl_dim_type type, unsigned pos)
511 space = isl_space_cow(space);
515 if (type == isl_dim_param) {
518 for (i = 0; i < 2; ++i) {
519 if (!space->nested[i])
522 isl_space_reset_dim_id(space->nested[i],
524 if (!space->nested[i])
529 isl_id_free(get_id(space, type, pos));
530 return set_id(space, type, pos, NULL);
532 isl_space_free(space);
536 int isl_space_has_dim_id(__isl_keep isl_space *dim,
537 enum isl_dim_type type, unsigned pos)
541 return get_id(dim, type, pos) != NULL;
544 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
545 enum isl_dim_type type, unsigned pos)
549 if (!get_id(dim, type, pos))
550 isl_die(dim->ctx, isl_error_invalid,
551 "dim has no id", return NULL);
552 return isl_id_copy(get_id(dim, type, pos));
555 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
556 enum isl_dim_type type, const char *s)
564 return isl_space_reset_tuple_id(dim, type);
566 if (!name_ok(dim->ctx, s))
569 id = isl_id_alloc(dim->ctx, s, NULL);
570 return isl_space_set_tuple_id(dim, type, id);
576 /* Does the tuple have a name?
578 int isl_space_has_tuple_name(__isl_keep isl_space *space,
579 enum isl_dim_type type)
583 if (!space_can_have_id(space, type))
585 id = space->tuple_id[type - isl_dim_in];
586 return id && id->name;
589 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
590 enum isl_dim_type type)
595 if (type != isl_dim_in && type != isl_dim_out)
597 id = dim->tuple_id[type - isl_dim_in];
598 return id ? id->name : NULL;
601 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
602 enum isl_dim_type type, unsigned pos,
610 return isl_space_reset_dim_id(dim, type, pos);
611 if (!name_ok(dim->ctx, s))
613 id = isl_id_alloc(dim->ctx, s, NULL);
614 return isl_space_set_dim_id(dim, type, pos, id);
620 /* Does the given dimension have a name?
622 int isl_space_has_dim_name(__isl_keep isl_space *space,
623 enum isl_dim_type type, unsigned pos)
629 id = get_id(space, type, pos);
630 return id && id->name;
633 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
634 enum isl_dim_type type, unsigned pos)
636 isl_id *id = get_id(dim, type, pos);
637 return id ? id->name : NULL;
640 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
641 __isl_keep isl_id *id)
650 offset = isl_space_offset(dim, type);
651 n = isl_space_dim(dim, type);
652 for (i = 0; i < n && offset + i < dim->n_id; ++i)
653 if (dim->ids[offset + i] == id)
659 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
660 enum isl_dim_type type, const char *name)
669 offset = isl_space_offset(space, type);
670 n = isl_space_dim(space, type);
671 for (i = 0; i < n && offset + i < space->n_id; ++i)
672 if (space->ids[offset + i]->name &&
673 !strcmp(space->ids[offset + i]->name, name))
679 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
680 enum isl_dim_type type)
684 if (type == isl_dim_in)
685 return dim->tuple_id[0];
686 if (type == isl_dim_out)
687 return dim->tuple_id[1];
691 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
692 enum isl_dim_type type)
696 if (type == isl_dim_in)
697 return dim->nested[0];
698 if (type == isl_dim_out)
699 return dim->nested[1];
703 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
704 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
707 isl_space *nested1, *nested2;
712 if (dim1 == dim2 && dim1_type == dim2_type)
715 if (n(dim1, dim1_type) != n(dim2, dim2_type))
717 id1 = tuple_id(dim1, dim1_type);
718 id2 = tuple_id(dim2, dim2_type);
721 if (id1 && id1 != id2)
723 nested1 = nested(dim1, dim1_type);
724 nested2 = nested(dim2, dim2_type);
725 if (!nested1 ^ !nested2)
727 if (nested1 && !isl_space_is_equal(nested1, nested2))
732 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
733 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
737 if (dim1 == dim2 && dim1_type == dim2_type)
740 if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
743 if (!dim1->ids && !dim2->ids)
746 for (i = 0; i < n(dim1, dim1_type); ++i) {
747 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
753 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
754 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
759 return match(dim1, dim1_type, dim2, dim2_type);
762 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
763 unsigned first, unsigned n, __isl_keep isl_id **ids)
767 for (i = 0; i < n ; ++i)
768 ids[i] = get_id(dim, type, first + i);
771 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
772 unsigned nparam, unsigned n_in, unsigned n_out)
778 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
781 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
782 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
783 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
785 dim = isl_space_cow(dim);
790 ids = isl_calloc_array(dim->ctx, isl_id *,
791 nparam + n_in + n_out);
794 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
795 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
796 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
799 dim->n_id = nparam + n_in + n_out;
801 dim->nparam = nparam;
812 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
813 enum isl_dim_type type, unsigned n)
817 dim = isl_space_reset(dim, type);
820 dim = isl_space_extend(dim,
821 dim->nparam + n, dim->n_in, dim->n_out);
822 if (dim && dim->nested[0] &&
823 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
826 if (dim && dim->nested[1] &&
827 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
832 return isl_space_extend(dim,
833 dim->nparam, dim->n_in + n, dim->n_out);
835 return isl_space_extend(dim,
836 dim->nparam, dim->n_in, dim->n_out + n);
838 isl_die(dim->ctx, isl_error_invalid,
839 "cannot add dimensions of specified type", goto error);
846 static int valid_dim_type(enum isl_dim_type type)
858 /* Insert "n" dimensions of type "type" at position "pos".
859 * If we are inserting parameters, then they are also inserted in
862 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
863 enum isl_dim_type type, unsigned pos, unsigned n)
870 return isl_space_reset(dim, type);
872 if (!valid_dim_type(type))
873 isl_die(dim->ctx, isl_error_invalid,
874 "cannot insert dimensions of specified type",
877 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
879 dim = isl_space_cow(dim);
884 enum isl_dim_type t, o = isl_dim_param;
887 ids = isl_calloc_array(dim->ctx, isl_id *,
888 dim->nparam + dim->n_in + dim->n_out + n);
892 s[isl_dim_param - o] = dim->nparam;
893 s[isl_dim_in - o] = dim->n_in;
894 s[isl_dim_out - o] = dim->n_out;
895 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
897 get_ids(dim, t, 0, s[t - o], ids + off);
900 get_ids(dim, t, 0, pos, ids + off);
902 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
903 off += s[t - o] - pos;
908 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
911 case isl_dim_param: dim->nparam += n; break;
912 case isl_dim_in: dim->n_in += n; break;
913 case isl_dim_out: dim->n_out += n; break;
916 dim = isl_space_reset(dim, type);
918 if (type == isl_dim_param) {
919 if (dim && dim->nested[0] &&
920 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
921 isl_dim_param, pos, n)))
923 if (dim && dim->nested[1] &&
924 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
925 isl_dim_param, pos, n)))
935 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
936 enum isl_dim_type dst_type, unsigned dst_pos,
937 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
946 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
949 if (dst_type == src_type && dst_pos == src_pos)
952 isl_assert(dim->ctx, dst_type != src_type, goto error);
954 dim = isl_space_reset(dim, src_type);
955 dim = isl_space_reset(dim, dst_type);
957 dim = isl_space_cow(dim);
963 enum isl_dim_type t, o = isl_dim_param;
966 ids = isl_calloc_array(dim->ctx, isl_id *,
967 dim->nparam + dim->n_in + dim->n_out);
971 s[isl_dim_param - o] = dim->nparam;
972 s[isl_dim_in - o] = dim->n_in;
973 s[isl_dim_out - o] = dim->n_out;
974 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
976 get_ids(dim, t, 0, dst_pos, ids + off);
978 get_ids(dim, src_type, src_pos, n, ids + off);
980 get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
982 off += s[t - o] - dst_pos;
983 } else if (t == src_type) {
984 get_ids(dim, t, 0, src_pos, ids + off);
986 get_ids(dim, t, src_pos + n,
987 s[t - o] - src_pos - n, ids + off);
988 off += s[t - o] - src_pos - n;
990 get_ids(dim, t, 0, s[t - o], ids + off);
996 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
1000 case isl_dim_param: dim->nparam += n; break;
1001 case isl_dim_in: dim->n_in += n; break;
1002 case isl_dim_out: dim->n_out += n; break;
1007 case isl_dim_param: dim->nparam -= n; break;
1008 case isl_dim_in: dim->n_in -= n; break;
1009 case isl_dim_out: dim->n_out -= n; break;
1013 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1016 for (i = 0; i < 2; ++i) {
1017 if (!dim->nested[i])
1019 dim->nested[i] = isl_space_replace(dim->nested[i],
1020 isl_dim_param, dim);
1021 if (!dim->nested[i])
1027 isl_space_free(dim);
1031 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1032 __isl_take isl_space *right)
1036 if (!left || !right)
1039 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1041 isl_assert(left->ctx,
1042 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
1045 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1049 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1050 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1051 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1053 if (dim && left->tuple_id[0] &&
1054 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1056 if (dim && right->tuple_id[1] &&
1057 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1059 if (dim && left->nested[0] &&
1060 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1062 if (dim && right->nested[1] &&
1063 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1066 isl_space_free(left);
1067 isl_space_free(right);
1071 isl_space_free(left);
1072 isl_space_free(right);
1076 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1077 __isl_take isl_space *right)
1079 isl_space *dom1, *dom2, *nest1, *nest2;
1081 if (!left || !right)
1084 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1087 dom1 = isl_space_domain(isl_space_copy(left));
1088 dom2 = isl_space_domain(isl_space_copy(right));
1089 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1091 dom1 = isl_space_range(left);
1092 dom2 = isl_space_range(right);
1093 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1095 return isl_space_join(isl_space_reverse(nest1), nest2);
1097 isl_space_free(left);
1098 isl_space_free(right);
1102 /* Given two spaces { A -> C } and { B -> C }, construct the space
1105 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1106 __isl_take isl_space *right)
1108 isl_space *ran, *dom1, *dom2, *nest;
1110 if (!left || !right)
1113 if (!match(left, isl_dim_param, right, isl_dim_param))
1114 isl_die(left->ctx, isl_error_invalid,
1115 "parameters need to match", goto error);
1116 if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1117 isl_die(left->ctx, isl_error_invalid,
1118 "ranges need to match", goto error);
1120 ran = isl_space_range(isl_space_copy(left));
1122 dom1 = isl_space_domain(left);
1123 dom2 = isl_space_domain(right);
1124 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1126 return isl_space_join(isl_space_reverse(nest), ran);
1128 isl_space_free(left);
1129 isl_space_free(right);
1133 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1134 __isl_take isl_space *right)
1136 isl_space *dom, *ran1, *ran2, *nest;
1138 if (!left || !right)
1141 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1143 if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1144 isl_die(left->ctx, isl_error_invalid,
1145 "domains need to match", goto error);
1147 dom = isl_space_domain(isl_space_copy(left));
1149 ran1 = isl_space_range(left);
1150 ran2 = isl_space_range(right);
1151 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1153 return isl_space_join(isl_space_reverse(dom), nest);
1155 isl_space_free(left);
1156 isl_space_free(right);
1160 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1163 isl_id **ids = NULL;
1167 ctx = isl_space_get_ctx(dim);
1168 if (!isl_space_is_set(dim))
1169 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1170 dim = isl_space_cow(dim);
1174 ids = isl_calloc_array(dim->ctx, isl_id *,
1175 dim->nparam + dim->n_out + dim->n_out);
1178 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1179 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1181 dim->n_in = dim->n_out;
1185 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1186 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1188 isl_id_free(dim->tuple_id[0]);
1189 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1190 isl_space_free(dim->nested[0]);
1191 dim->nested[0] = isl_space_copy(dim->nested[1]);
1194 isl_space_free(dim);
1198 __isl_give isl_space *isl_space_map_from_domain_and_range(
1199 __isl_take isl_space *domain, __isl_take isl_space *range)
1201 if (!domain || !range)
1203 if (!isl_space_is_set(domain))
1204 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1205 "domain is not a set space", goto error);
1206 if (!isl_space_is_set(range))
1207 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1208 "range is not a set space", goto error);
1209 return isl_space_join(isl_space_reverse(domain), range);
1211 isl_space_free(domain);
1212 isl_space_free(range);
1216 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1217 enum isl_dim_type type,
1218 unsigned first, unsigned n, __isl_take isl_id **ids)
1222 for (i = 0; i < n ; ++i)
1223 dim = set_id(dim, type, first + i, ids[i]);
1228 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1232 isl_id **ids = NULL;
1237 if (match(dim, isl_dim_in, dim, isl_dim_out))
1240 dim = isl_space_cow(dim);
1244 id = dim->tuple_id[0];
1245 dim->tuple_id[0] = dim->tuple_id[1];
1246 dim->tuple_id[1] = id;
1248 nested = dim->nested[0];
1249 dim->nested[0] = dim->nested[1];
1250 dim->nested[1] = nested;
1253 ids = isl_alloc_array(dim->ctx, isl_id *,
1254 dim->n_in + dim->n_out);
1257 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1258 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1262 dim->n_in = dim->n_out;
1266 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1267 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1274 isl_space_free(dim);
1278 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1279 enum isl_dim_type type, unsigned first, unsigned num)
1287 return isl_space_reset(dim, type);
1289 if (!valid_dim_type(type))
1290 isl_die(dim->ctx, isl_error_invalid,
1291 "cannot drop dimensions of specified type", goto error);
1293 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1294 dim = isl_space_cow(dim);
1298 dim = extend_ids(dim);
1301 for (i = 0; i < num; ++i)
1302 isl_id_free(get_id(dim, type, first + i));
1303 for (i = first+num; i < n(dim, type); ++i)
1304 set_id(dim, type, i - num, get_id(dim, type, i));
1307 get_ids(dim, isl_dim_in, 0, dim->n_in,
1308 dim->ids + offset(dim, isl_dim_in) - num);
1310 get_ids(dim, isl_dim_out, 0, dim->n_out,
1311 dim->ids + offset(dim, isl_dim_out) - num);
1318 case isl_dim_param: dim->nparam -= num; break;
1319 case isl_dim_in: dim->n_in -= num; break;
1320 case isl_dim_out: dim->n_out -= num; break;
1323 dim = isl_space_reset(dim, type);
1324 if (type == isl_dim_param) {
1325 if (dim && dim->nested[0] &&
1326 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1327 isl_dim_param, first, num)))
1329 if (dim && dim->nested[1] &&
1330 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1331 isl_dim_param, first, num)))
1336 isl_space_free(dim);
1340 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1341 unsigned first, unsigned n)
1345 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1348 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1349 unsigned first, unsigned n)
1353 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1356 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1360 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1361 dim = isl_space_reverse(dim);
1362 dim = mark_as_set(dim);
1366 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1370 if (!isl_space_is_set(dim))
1371 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1372 "not a set space", goto error);
1373 dim = isl_space_reverse(dim);
1374 dim = isl_space_reset(dim, isl_dim_out);
1377 isl_space_free(dim);
1381 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1385 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1386 dim = mark_as_set(dim);
1390 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1394 if (!isl_space_is_set(dim))
1395 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1396 "not a set space", goto error);
1397 return isl_space_reset(dim, isl_dim_in);
1399 isl_space_free(dim);
1403 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1405 if (isl_space_is_params(space))
1407 space = isl_space_drop_dims(space,
1408 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1409 space = isl_space_drop_dims(space,
1410 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1411 space = mark_as_params(space);
1415 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1419 if (!isl_space_is_params(space))
1420 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1421 "not a parameter space", goto error);
1422 return isl_space_reset(space, isl_dim_set);
1424 isl_space_free(space);
1428 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1430 dim = isl_space_cow(dim);
1434 dim->n_out += dim->n_in;
1436 dim = isl_space_reset(dim, isl_dim_in);
1437 dim = isl_space_reset(dim, isl_dim_out);
1442 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1450 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1451 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1452 dim = isl_space_cow(dim);
1455 dim->n_out += dim->nparam + dim->n_in + n_div;
1459 for (i = 0; i < dim->n_id; ++i)
1460 isl_id_free(get_id(dim, isl_dim_out, i));
1462 dim = isl_space_reset(dim, isl_dim_in);
1463 dim = isl_space_reset(dim, isl_dim_out);
1468 /* Are the two spaces the same, including positions and names of parameters?
1470 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1476 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1477 isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1478 isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1481 /* Is space1 equal to the domain of space2?
1483 * In the internal version we also allow space2 to be the space of a set,
1484 * provided space1 is a parameter space.
1486 int isl_space_is_domain_internal(__isl_keep isl_space *space1,
1487 __isl_keep isl_space *space2)
1489 if (!space1 || !space2)
1491 if (!isl_space_is_set(space1))
1493 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1494 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1497 /* Is space1 equal to the domain of space2?
1499 int isl_space_is_domain(__isl_keep isl_space *space1,
1500 __isl_keep isl_space *space2)
1504 if (!isl_space_is_map(space2))
1506 return isl_space_is_domain_internal(space1, space2);
1509 /* Is space1 equal to the range of space2?
1511 * In the internal version, space2 is allowed to be the space of a set,
1512 * in which case it should be equal to space1.
1514 int isl_space_is_range_internal(__isl_keep isl_space *space1,
1515 __isl_keep isl_space *space2)
1517 if (!space1 || !space2)
1519 if (!isl_space_is_set(space1))
1521 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1522 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_out);
1525 /* Is space1 equal to the range of space2?
1527 int isl_space_is_range(__isl_keep isl_space *space1,
1528 __isl_keep isl_space *space2)
1532 if (!isl_space_is_map(space2))
1534 return isl_space_is_range_internal(space1, space2);
1537 int isl_space_compatible(__isl_keep isl_space *dim1,
1538 __isl_keep isl_space *dim2)
1540 return dim1->nparam == dim2->nparam &&
1541 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1544 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1552 hash = isl_hash_builtin(hash, dim->nparam);
1553 hash = isl_hash_builtin(hash, dim->n_in);
1554 hash = isl_hash_builtin(hash, dim->n_out);
1556 for (i = 0; i < dim->nparam; ++i) {
1557 id = get_id(dim, isl_dim_param, i);
1558 hash = isl_hash_id(hash, id);
1561 id = tuple_id(dim, isl_dim_in);
1562 hash = isl_hash_id(hash, id);
1563 id = tuple_id(dim, isl_dim_out);
1564 hash = isl_hash_id(hash, id);
1566 hash = isl_hash_dim(hash, dim->nested[0]);
1567 hash = isl_hash_dim(hash, dim->nested[1]);
1572 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1579 hash = isl_hash_init();
1580 hash = isl_hash_dim(hash, dim);
1585 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1590 if (!isl_space_is_set(dim))
1593 return dim->nested[1] != NULL;
1596 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1603 wrap = isl_space_set_alloc(dim->ctx,
1604 dim->nparam, dim->n_in + dim->n_out);
1606 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1607 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1608 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1613 wrap->nested[1] = dim;
1617 isl_space_free(dim);
1621 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1628 if (!isl_space_is_wrapping(dim))
1629 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
1632 unwrap = isl_space_copy(dim->nested[1]);
1633 isl_space_free(dim);
1637 isl_space_free(dim);
1641 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1643 if (type != isl_dim_in && type != isl_dim_out)
1647 if (dim->tuple_id[type - isl_dim_in])
1649 if (dim->nested[type - isl_dim_in])
1654 int isl_space_may_be_set(__isl_keep isl_space *dim)
1658 if (isl_space_is_set(dim))
1660 if (isl_space_dim(dim, isl_dim_in) != 0)
1662 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1667 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1668 enum isl_dim_type type)
1670 if (!isl_space_is_named_or_nested(dim, type))
1673 dim = isl_space_cow(dim);
1677 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1678 dim->tuple_id[type - isl_dim_in] = NULL;
1679 isl_space_free(dim->nested[type - isl_dim_in]);
1680 dim->nested[type - isl_dim_in] = NULL;
1685 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1689 if (!dim->nested[0] && !dim->nested[1])
1693 dim = isl_space_reset(dim, isl_dim_in);
1694 if (dim && dim->nested[1])
1695 dim = isl_space_reset(dim, isl_dim_out);
1700 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1704 if (!dim->nested[0])
1707 return isl_space_reset(dim, isl_dim_in);
1710 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1714 if (!dim->nested[1])
1717 return isl_space_reset(dim, isl_dim_out);
1720 /* Replace the dimensions of the given type of dst by those of src.
1722 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1723 enum isl_dim_type type, __isl_keep isl_space *src)
1725 dst = isl_space_cow(dst);
1730 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1731 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1732 dst = copy_ids(dst, type, 0, src, type);
1734 if (dst && type == isl_dim_param) {
1736 for (i = 0; i <= 1; ++i) {
1737 if (!dst->nested[i])
1739 dst->nested[i] = isl_space_replace(dst->nested[i],
1741 if (!dst->nested[i])
1748 isl_space_free(dst);
1752 /* Given a dimension specification "dim" of a set, create a dimension
1753 * specification for the lift of the set. In particular, the result
1754 * is of the form [dim -> local[..]], with n_local variables in the
1755 * range of the wrapped map.
1757 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1759 isl_space *local_dim;
1764 local_dim = isl_space_dup(dim);
1765 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1766 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1767 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1768 dim = isl_space_join(isl_space_from_domain(dim),
1769 isl_space_from_range(local_dim));
1770 dim = isl_space_wrap(dim);
1771 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1776 int isl_space_can_zip(__isl_keep isl_space *dim)
1781 return dim->nested[0] && dim->nested[1];
1784 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1786 isl_space *dom, *ran;
1787 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1789 if (!isl_space_can_zip(dim))
1790 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1795 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1796 ran = isl_space_unwrap(isl_space_range(dim));
1797 dom_dom = isl_space_domain(isl_space_copy(dom));
1798 dom_ran = isl_space_range(dom);
1799 ran_dom = isl_space_domain(isl_space_copy(ran));
1800 ran_ran = isl_space_range(ran);
1801 dom = isl_space_join(isl_space_from_domain(dom_dom),
1802 isl_space_from_range(ran_dom));
1803 ran = isl_space_join(isl_space_from_domain(dom_ran),
1804 isl_space_from_range(ran_ran));
1805 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1806 isl_space_from_range(isl_space_wrap(ran)));
1808 isl_space_free(dim);
1812 /* Can we apply isl_space_curry to "space"?
1813 * That is, does it have a nested relation in its domain?
1815 int isl_space_can_curry(__isl_keep isl_space *space)
1820 return !!space->nested[0];
1823 /* Given a space (A -> B) -> C, return the corresponding space
1826 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
1828 isl_space *dom, *ran;
1829 isl_space *dom_dom, *dom_ran;
1834 if (!isl_space_can_curry(space))
1835 isl_die(space->ctx, isl_error_invalid,
1836 "space cannot be curried", goto error);
1838 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
1839 ran = isl_space_range(space);
1840 dom_dom = isl_space_domain(isl_space_copy(dom));
1841 dom_ran = isl_space_range(dom);
1842 ran = isl_space_join(isl_space_from_domain(dom_ran),
1843 isl_space_from_range(ran));
1844 return isl_space_join(isl_space_from_domain(dom_dom),
1845 isl_space_from_range(isl_space_wrap(ran)));
1847 isl_space_free(space);
1851 /* Can we apply isl_space_uncurry to "space"?
1852 * That is, does it have a nested relation in its range?
1854 int isl_space_can_uncurry(__isl_keep isl_space *space)
1859 return !!space->nested[1];
1862 /* Given a space A -> (B -> C), return the corresponding space
1865 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
1867 isl_space *dom, *ran;
1868 isl_space *ran_dom, *ran_ran;
1873 if (!isl_space_can_uncurry(space))
1874 isl_die(space->ctx, isl_error_invalid,
1875 "space cannot be uncurried",
1876 return isl_space_free(space));
1878 dom = isl_space_domain(isl_space_copy(space));
1879 ran = isl_space_unwrap(isl_space_range(space));
1880 ran_dom = isl_space_domain(isl_space_copy(ran));
1881 ran_ran = isl_space_range(ran);
1882 dom = isl_space_join(isl_space_from_domain(dom),
1883 isl_space_from_range(ran_dom));
1884 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1885 isl_space_from_range(ran_ran));
1888 int isl_space_has_named_params(__isl_keep isl_space *dim)
1895 if (dim->nparam == 0)
1897 off = isl_space_offset(dim, isl_dim_param);
1898 if (off + dim->nparam > dim->n_id)
1900 for (i = 0; i < dim->nparam; ++i)
1901 if (!dim->ids[off + i])
1906 /* Align the initial parameters of dim1 to match the order in dim2.
1908 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1909 __isl_take isl_space *dim2)
1911 isl_reordering *exp;
1913 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1914 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1915 "parameter alignment requires named parameters",
1918 dim2 = isl_space_params(dim2);
1919 exp = isl_parameter_alignment_reordering(dim1, dim2);
1920 exp = isl_reordering_extend_space(exp, dim1);
1921 isl_space_free(dim2);
1924 dim1 = isl_space_copy(exp->dim);
1925 isl_reordering_free(exp);
1928 isl_space_free(dim1);
1929 isl_space_free(dim2);
1933 /* Given the space of set (domain), construct a space for a map
1934 * with as domain the given space and as range the range of "model".
1936 __isl_give isl_space *isl_space_extend_domain_with_range(
1937 __isl_take isl_space *space, __isl_take isl_space *model)
1942 space = isl_space_from_domain(space);
1943 space = isl_space_add_dims(space, isl_dim_out,
1944 isl_space_dim(model, isl_dim_out));
1945 if (isl_space_has_tuple_id(model, isl_dim_out))
1946 space = isl_space_set_tuple_id(space, isl_dim_out,
1947 isl_space_get_tuple_id(model, isl_dim_out));
1950 if (model->nested[1]) {
1951 isl_space *nested = isl_space_copy(model->nested[1]);
1952 int n_nested, n_space;
1953 nested = isl_space_align_params(nested, isl_space_copy(space));
1954 n_nested = isl_space_dim(nested, isl_dim_param);
1955 n_space = isl_space_dim(space, isl_dim_param);
1956 if (n_nested > n_space)
1957 nested = isl_space_drop_dims(nested, isl_dim_param,
1958 n_space, n_nested - n_space);
1961 space->nested[1] = nested;
1963 isl_space_free(model);
1966 isl_space_free(model);
1967 isl_space_free(space);