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);
788 ids = isl_calloc_array(dim->ctx, isl_id *,
789 nparam + n_in + n_out);
792 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
793 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
794 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
797 dim->n_id = nparam + n_in + n_out;
799 dim->nparam = nparam;
810 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
811 enum isl_dim_type type, unsigned n)
815 dim = isl_space_reset(dim, type);
818 dim = isl_space_extend(dim,
819 dim->nparam + n, dim->n_in, dim->n_out);
820 if (dim && dim->nested[0] &&
821 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
824 if (dim && dim->nested[1] &&
825 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
830 return isl_space_extend(dim,
831 dim->nparam, dim->n_in + n, dim->n_out);
833 return isl_space_extend(dim,
834 dim->nparam, dim->n_in, dim->n_out + n);
836 isl_die(dim->ctx, isl_error_invalid,
837 "cannot add dimensions of specified type", goto error);
844 static int valid_dim_type(enum isl_dim_type type)
856 /* Insert "n" dimensions of type "type" at position "pos".
857 * If we are inserting parameters, then they are also inserted in
860 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
861 enum isl_dim_type type, unsigned pos, unsigned n)
868 return isl_space_reset(dim, type);
870 if (!valid_dim_type(type))
871 isl_die(dim->ctx, isl_error_invalid,
872 "cannot insert dimensions of specified type",
875 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
877 dim = isl_space_cow(dim);
882 enum isl_dim_type t, o = isl_dim_param;
885 ids = isl_calloc_array(dim->ctx, isl_id *,
886 dim->nparam + dim->n_in + dim->n_out + n);
890 s[isl_dim_param - o] = dim->nparam;
891 s[isl_dim_in - o] = dim->n_in;
892 s[isl_dim_out - o] = dim->n_out;
893 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
895 get_ids(dim, t, 0, s[t - o], ids + off);
898 get_ids(dim, t, 0, pos, ids + off);
900 get_ids(dim, t, pos, s[t - o] - pos, ids + off);
901 off += s[t - o] - pos;
906 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
909 case isl_dim_param: dim->nparam += n; break;
910 case isl_dim_in: dim->n_in += n; break;
911 case isl_dim_out: dim->n_out += n; break;
914 dim = isl_space_reset(dim, type);
916 if (type == isl_dim_param) {
917 if (dim && dim->nested[0] &&
918 !(dim->nested[0] = isl_space_insert_dims(dim->nested[0],
919 isl_dim_param, pos, n)))
921 if (dim && dim->nested[1] &&
922 !(dim->nested[1] = isl_space_insert_dims(dim->nested[1],
923 isl_dim_param, pos, n)))
933 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
934 enum isl_dim_type dst_type, unsigned dst_pos,
935 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
944 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
947 if (dst_type == src_type && dst_pos == src_pos)
950 isl_assert(dim->ctx, dst_type != src_type, goto error);
952 dim = isl_space_reset(dim, src_type);
953 dim = isl_space_reset(dim, dst_type);
955 dim = isl_space_cow(dim);
961 enum isl_dim_type t, o = isl_dim_param;
964 ids = isl_calloc_array(dim->ctx, isl_id *,
965 dim->nparam + dim->n_in + dim->n_out);
969 s[isl_dim_param - o] = dim->nparam;
970 s[isl_dim_in - o] = dim->n_in;
971 s[isl_dim_out - o] = dim->n_out;
972 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
974 get_ids(dim, t, 0, dst_pos, ids + off);
976 get_ids(dim, src_type, src_pos, n, ids + off);
978 get_ids(dim, t, dst_pos, s[t - o] - dst_pos,
980 off += s[t - o] - dst_pos;
981 } else if (t == src_type) {
982 get_ids(dim, t, 0, src_pos, ids + off);
984 get_ids(dim, t, src_pos + n,
985 s[t - o] - src_pos - n, ids + off);
986 off += s[t - o] - src_pos - n;
988 get_ids(dim, t, 0, s[t - o], ids + off);
994 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
998 case isl_dim_param: dim->nparam += n; break;
999 case isl_dim_in: dim->n_in += n; break;
1000 case isl_dim_out: dim->n_out += n; break;
1005 case isl_dim_param: dim->nparam -= n; break;
1006 case isl_dim_in: dim->n_in -= n; break;
1007 case isl_dim_out: dim->n_out -= n; break;
1011 if (dst_type != isl_dim_param && src_type != isl_dim_param)
1014 for (i = 0; i < 2; ++i) {
1015 if (!dim->nested[i])
1017 dim->nested[i] = isl_space_replace(dim->nested[i],
1018 isl_dim_param, dim);
1019 if (!dim->nested[i])
1025 isl_space_free(dim);
1029 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1030 __isl_take isl_space *right)
1034 if (!left || !right)
1037 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1039 isl_assert(left->ctx,
1040 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
1043 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1047 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1048 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1049 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1051 if (dim && left->tuple_id[0] &&
1052 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1054 if (dim && right->tuple_id[1] &&
1055 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1057 if (dim && left->nested[0] &&
1058 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1060 if (dim && right->nested[1] &&
1061 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1064 isl_space_free(left);
1065 isl_space_free(right);
1069 isl_space_free(left);
1070 isl_space_free(right);
1074 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1075 __isl_take isl_space *right)
1077 isl_space *dom1, *dom2, *nest1, *nest2;
1079 if (!left || !right)
1082 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1085 dom1 = isl_space_domain(isl_space_copy(left));
1086 dom2 = isl_space_domain(isl_space_copy(right));
1087 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1089 dom1 = isl_space_range(left);
1090 dom2 = isl_space_range(right);
1091 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1093 return isl_space_join(isl_space_reverse(nest1), nest2);
1095 isl_space_free(left);
1096 isl_space_free(right);
1100 /* Given two spaces { A -> C } and { B -> C }, construct the space
1103 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1104 __isl_take isl_space *right)
1106 isl_space *ran, *dom1, *dom2, *nest;
1108 if (!left || !right)
1111 if (!match(left, isl_dim_param, right, isl_dim_param))
1112 isl_die(left->ctx, isl_error_invalid,
1113 "parameters need to match", goto error);
1114 if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1115 isl_die(left->ctx, isl_error_invalid,
1116 "ranges need to match", goto error);
1118 ran = isl_space_range(isl_space_copy(left));
1120 dom1 = isl_space_domain(left);
1121 dom2 = isl_space_domain(right);
1122 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1124 return isl_space_join(isl_space_reverse(nest), ran);
1126 isl_space_free(left);
1127 isl_space_free(right);
1131 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1132 __isl_take isl_space *right)
1134 isl_space *dom, *ran1, *ran2, *nest;
1136 if (!left || !right)
1139 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1141 if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1142 isl_die(left->ctx, isl_error_invalid,
1143 "domains need to match", goto error);
1145 dom = isl_space_domain(isl_space_copy(left));
1147 ran1 = isl_space_range(left);
1148 ran2 = isl_space_range(right);
1149 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1151 return isl_space_join(isl_space_reverse(dom), nest);
1153 isl_space_free(left);
1154 isl_space_free(right);
1158 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1161 isl_id **ids = NULL;
1165 ctx = isl_space_get_ctx(dim);
1166 if (!isl_space_is_set(dim))
1167 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1168 dim = isl_space_cow(dim);
1172 ids = isl_calloc_array(dim->ctx, isl_id *,
1173 dim->nparam + dim->n_out + dim->n_out);
1176 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1177 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1179 dim->n_in = dim->n_out;
1183 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1184 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1186 isl_id_free(dim->tuple_id[0]);
1187 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1188 isl_space_free(dim->nested[0]);
1189 dim->nested[0] = isl_space_copy(dim->nested[1]);
1192 isl_space_free(dim);
1196 __isl_give isl_space *isl_space_map_from_domain_and_range(
1197 __isl_take isl_space *domain, __isl_take isl_space *range)
1199 if (!domain || !range)
1201 if (!isl_space_is_set(domain))
1202 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1203 "domain is not a set space", goto error);
1204 if (!isl_space_is_set(range))
1205 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1206 "range is not a set space", goto error);
1207 return isl_space_join(isl_space_reverse(domain), range);
1209 isl_space_free(domain);
1210 isl_space_free(range);
1214 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1215 enum isl_dim_type type,
1216 unsigned first, unsigned n, __isl_take isl_id **ids)
1220 for (i = 0; i < n ; ++i)
1221 dim = set_id(dim, type, first + i, ids[i]);
1226 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1230 isl_id **ids = NULL;
1235 if (match(dim, isl_dim_in, dim, isl_dim_out))
1238 dim = isl_space_cow(dim);
1242 id = dim->tuple_id[0];
1243 dim->tuple_id[0] = dim->tuple_id[1];
1244 dim->tuple_id[1] = id;
1246 nested = dim->nested[0];
1247 dim->nested[0] = dim->nested[1];
1248 dim->nested[1] = nested;
1251 ids = isl_alloc_array(dim->ctx, isl_id *,
1252 dim->n_in + dim->n_out);
1255 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1256 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1260 dim->n_in = dim->n_out;
1264 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1265 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1272 isl_space_free(dim);
1276 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1277 enum isl_dim_type type, unsigned first, unsigned num)
1285 return isl_space_reset(dim, type);
1287 if (!valid_dim_type(type))
1288 isl_die(dim->ctx, isl_error_invalid,
1289 "cannot drop dimensions of specified type", goto error);
1291 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1292 dim = isl_space_cow(dim);
1296 dim = extend_ids(dim);
1299 for (i = 0; i < num; ++i)
1300 isl_id_free(get_id(dim, type, first + i));
1301 for (i = first+num; i < n(dim, type); ++i)
1302 set_id(dim, type, i - num, get_id(dim, type, i));
1305 get_ids(dim, isl_dim_in, 0, dim->n_in,
1306 dim->ids + offset(dim, isl_dim_in) - num);
1308 get_ids(dim, isl_dim_out, 0, dim->n_out,
1309 dim->ids + offset(dim, isl_dim_out) - num);
1316 case isl_dim_param: dim->nparam -= num; break;
1317 case isl_dim_in: dim->n_in -= num; break;
1318 case isl_dim_out: dim->n_out -= num; break;
1321 dim = isl_space_reset(dim, type);
1322 if (type == isl_dim_param) {
1323 if (dim && dim->nested[0] &&
1324 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1325 isl_dim_param, first, num)))
1327 if (dim && dim->nested[1] &&
1328 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1329 isl_dim_param, first, num)))
1334 isl_space_free(dim);
1338 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1339 unsigned first, unsigned n)
1343 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1346 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1347 unsigned first, unsigned n)
1351 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1354 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1358 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1359 dim = isl_space_reverse(dim);
1360 dim = mark_as_set(dim);
1364 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1368 if (!isl_space_is_set(dim))
1369 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1370 "not a set space", goto error);
1371 dim = isl_space_reverse(dim);
1372 dim = isl_space_reset(dim, isl_dim_out);
1375 isl_space_free(dim);
1379 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1383 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1384 dim = mark_as_set(dim);
1388 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1392 if (!isl_space_is_set(dim))
1393 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1394 "not a set space", goto error);
1395 return isl_space_reset(dim, isl_dim_in);
1397 isl_space_free(dim);
1401 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1403 if (isl_space_is_params(space))
1405 space = isl_space_drop_dims(space,
1406 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1407 space = isl_space_drop_dims(space,
1408 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1409 space = mark_as_params(space);
1413 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1417 if (!isl_space_is_params(space))
1418 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1419 "not a parameter space", goto error);
1420 return isl_space_reset(space, isl_dim_set);
1422 isl_space_free(space);
1426 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1428 dim = isl_space_cow(dim);
1432 dim->n_out += dim->n_in;
1434 dim = isl_space_reset(dim, isl_dim_in);
1435 dim = isl_space_reset(dim, isl_dim_out);
1440 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1448 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1449 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1450 dim = isl_space_cow(dim);
1453 dim->n_out += dim->nparam + dim->n_in + n_div;
1457 for (i = 0; i < dim->n_id; ++i)
1458 isl_id_free(get_id(dim, isl_dim_out, i));
1460 dim = isl_space_reset(dim, isl_dim_in);
1461 dim = isl_space_reset(dim, isl_dim_out);
1466 /* Are the two spaces the same, including positions and names of parameters?
1468 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1474 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1475 isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1476 isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1479 /* Is space1 equal to the domain of space2?
1481 * In the internal version we also allow space2 to be the space of a set,
1482 * provided space1 is a parameter space.
1484 int isl_space_is_domain_internal(__isl_keep isl_space *space1,
1485 __isl_keep isl_space *space2)
1487 if (!space1 || !space2)
1489 if (!isl_space_is_set(space1))
1491 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1492 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1495 /* Is space1 equal to the domain of space2?
1497 int isl_space_is_domain(__isl_keep isl_space *space1,
1498 __isl_keep isl_space *space2)
1502 if (!isl_space_is_map(space2))
1504 return isl_space_is_domain_internal(space1, space2);
1507 /* Is space1 equal to the range of space2?
1509 * In the internal version, space2 is allowed to be the space of a set,
1510 * in which case it should be equal to space1.
1512 int isl_space_is_range_internal(__isl_keep isl_space *space1,
1513 __isl_keep isl_space *space2)
1515 if (!space1 || !space2)
1517 if (!isl_space_is_set(space1))
1519 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1520 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_out);
1523 /* Is space1 equal to the range of space2?
1525 int isl_space_is_range(__isl_keep isl_space *space1,
1526 __isl_keep isl_space *space2)
1530 if (!isl_space_is_map(space2))
1532 return isl_space_is_range_internal(space1, space2);
1535 int isl_space_compatible(__isl_keep isl_space *dim1,
1536 __isl_keep isl_space *dim2)
1538 return dim1->nparam == dim2->nparam &&
1539 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1542 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1550 hash = isl_hash_builtin(hash, dim->nparam);
1551 hash = isl_hash_builtin(hash, dim->n_in);
1552 hash = isl_hash_builtin(hash, dim->n_out);
1554 for (i = 0; i < dim->nparam; ++i) {
1555 id = get_id(dim, isl_dim_param, i);
1556 hash = isl_hash_id(hash, id);
1559 id = tuple_id(dim, isl_dim_in);
1560 hash = isl_hash_id(hash, id);
1561 id = tuple_id(dim, isl_dim_out);
1562 hash = isl_hash_id(hash, id);
1564 hash = isl_hash_dim(hash, dim->nested[0]);
1565 hash = isl_hash_dim(hash, dim->nested[1]);
1570 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1577 hash = isl_hash_init();
1578 hash = isl_hash_dim(hash, dim);
1583 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1588 if (!isl_space_is_set(dim))
1591 return dim->nested[1] != NULL;
1594 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1601 wrap = isl_space_set_alloc(dim->ctx,
1602 dim->nparam, dim->n_in + dim->n_out);
1604 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1605 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1606 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1611 wrap->nested[1] = dim;
1615 isl_space_free(dim);
1619 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1626 if (!isl_space_is_wrapping(dim))
1627 isl_die(dim->ctx, isl_error_invalid, "not a wrapping space",
1630 unwrap = isl_space_copy(dim->nested[1]);
1631 isl_space_free(dim);
1635 isl_space_free(dim);
1639 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1641 if (type != isl_dim_in && type != isl_dim_out)
1645 if (dim->tuple_id[type - isl_dim_in])
1647 if (dim->nested[type - isl_dim_in])
1652 int isl_space_may_be_set(__isl_keep isl_space *dim)
1656 if (isl_space_is_set(dim))
1658 if (isl_space_dim(dim, isl_dim_in) != 0)
1660 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1665 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1666 enum isl_dim_type type)
1668 if (!isl_space_is_named_or_nested(dim, type))
1671 dim = isl_space_cow(dim);
1675 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1676 dim->tuple_id[type - isl_dim_in] = NULL;
1677 isl_space_free(dim->nested[type - isl_dim_in]);
1678 dim->nested[type - isl_dim_in] = NULL;
1683 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1687 if (!dim->nested[0] && !dim->nested[1])
1691 dim = isl_space_reset(dim, isl_dim_in);
1692 if (dim && dim->nested[1])
1693 dim = isl_space_reset(dim, isl_dim_out);
1698 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1702 if (!dim->nested[0])
1705 return isl_space_reset(dim, isl_dim_in);
1708 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1712 if (!dim->nested[1])
1715 return isl_space_reset(dim, isl_dim_out);
1718 /* Replace the dimensions of the given type of dst by those of src.
1720 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1721 enum isl_dim_type type, __isl_keep isl_space *src)
1723 dst = isl_space_cow(dst);
1728 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1729 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1730 dst = copy_ids(dst, type, 0, src, type);
1732 if (dst && type == isl_dim_param) {
1734 for (i = 0; i <= 1; ++i) {
1735 if (!dst->nested[i])
1737 dst->nested[i] = isl_space_replace(dst->nested[i],
1739 if (!dst->nested[i])
1746 isl_space_free(dst);
1750 /* Given a dimension specification "dim" of a set, create a dimension
1751 * specification for the lift of the set. In particular, the result
1752 * is of the form [dim -> local[..]], with n_local variables in the
1753 * range of the wrapped map.
1755 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1757 isl_space *local_dim;
1762 local_dim = isl_space_dup(dim);
1763 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1764 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1765 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1766 dim = isl_space_join(isl_space_from_domain(dim),
1767 isl_space_from_range(local_dim));
1768 dim = isl_space_wrap(dim);
1769 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1774 int isl_space_can_zip(__isl_keep isl_space *dim)
1779 return dim->nested[0] && dim->nested[1];
1782 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1784 isl_space *dom, *ran;
1785 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1787 if (!isl_space_can_zip(dim))
1788 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1793 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1794 ran = isl_space_unwrap(isl_space_range(dim));
1795 dom_dom = isl_space_domain(isl_space_copy(dom));
1796 dom_ran = isl_space_range(dom);
1797 ran_dom = isl_space_domain(isl_space_copy(ran));
1798 ran_ran = isl_space_range(ran);
1799 dom = isl_space_join(isl_space_from_domain(dom_dom),
1800 isl_space_from_range(ran_dom));
1801 ran = isl_space_join(isl_space_from_domain(dom_ran),
1802 isl_space_from_range(ran_ran));
1803 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1804 isl_space_from_range(isl_space_wrap(ran)));
1806 isl_space_free(dim);
1810 /* Can we apply isl_space_curry to "space"?
1811 * That is, does it have a nested relation in its domain?
1813 int isl_space_can_curry(__isl_keep isl_space *space)
1818 return !!space->nested[0];
1821 /* Given a space (A -> B) -> C, return the corresponding space
1824 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
1826 isl_space *dom, *ran;
1827 isl_space *dom_dom, *dom_ran;
1832 if (!isl_space_can_curry(space))
1833 isl_die(space->ctx, isl_error_invalid,
1834 "space cannot be curried", goto error);
1836 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
1837 ran = isl_space_range(space);
1838 dom_dom = isl_space_domain(isl_space_copy(dom));
1839 dom_ran = isl_space_range(dom);
1840 ran = isl_space_join(isl_space_from_domain(dom_ran),
1841 isl_space_from_range(ran));
1842 return isl_space_join(isl_space_from_domain(dom_dom),
1843 isl_space_from_range(isl_space_wrap(ran)));
1845 isl_space_free(space);
1849 /* Can we apply isl_space_uncurry to "space"?
1850 * That is, does it have a nested relation in its range?
1852 int isl_space_can_uncurry(__isl_keep isl_space *space)
1857 return !!space->nested[1];
1860 /* Given a space A -> (B -> C), return the corresponding space
1863 __isl_give isl_space *isl_space_uncurry(__isl_take isl_space *space)
1865 isl_space *dom, *ran;
1866 isl_space *ran_dom, *ran_ran;
1871 if (!isl_space_can_uncurry(space))
1872 isl_die(space->ctx, isl_error_invalid,
1873 "space cannot be uncurried",
1874 return isl_space_free(space));
1876 dom = isl_space_domain(isl_space_copy(space));
1877 ran = isl_space_unwrap(isl_space_range(space));
1878 ran_dom = isl_space_domain(isl_space_copy(ran));
1879 ran_ran = isl_space_range(ran);
1880 dom = isl_space_join(isl_space_from_domain(dom),
1881 isl_space_from_range(ran_dom));
1882 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1883 isl_space_from_range(ran_ran));
1886 int isl_space_has_named_params(__isl_keep isl_space *dim)
1893 if (dim->nparam == 0)
1895 off = isl_space_offset(dim, isl_dim_param);
1896 if (off + dim->nparam > dim->n_id)
1898 for (i = 0; i < dim->nparam; ++i)
1899 if (!dim->ids[off + i])
1904 /* Align the initial parameters of dim1 to match the order in dim2.
1906 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1907 __isl_take isl_space *dim2)
1909 isl_reordering *exp;
1911 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1912 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1913 "parameter alignment requires named parameters",
1916 dim2 = isl_space_params(dim2);
1917 exp = isl_parameter_alignment_reordering(dim1, dim2);
1918 exp = isl_reordering_extend_space(exp, dim1);
1919 isl_space_free(dim2);
1922 dim1 = isl_space_copy(exp->dim);
1923 isl_reordering_free(exp);
1926 isl_space_free(dim1);
1927 isl_space_free(dim2);
1931 /* Given the space of set (domain), construct a space for a map
1932 * with as domain the given space and as range the range of "model".
1934 __isl_give isl_space *isl_space_extend_domain_with_range(
1935 __isl_take isl_space *space, __isl_take isl_space *model)
1940 space = isl_space_from_domain(space);
1941 space = isl_space_add_dims(space, isl_dim_out,
1942 isl_space_dim(model, isl_dim_out));
1943 if (isl_space_has_tuple_id(model, isl_dim_out))
1944 space = isl_space_set_tuple_id(space, isl_dim_out,
1945 isl_space_get_tuple_id(model, isl_dim_out));
1948 if (model->nested[1]) {
1949 isl_space *nested = isl_space_copy(model->nested[1]);
1950 int n_nested, n_space;
1951 nested = isl_space_align_params(nested, isl_space_copy(space));
1952 n_nested = isl_space_dim(nested, isl_dim_param);
1953 n_space = isl_space_dim(space, isl_dim_param);
1954 if (n_nested > n_space)
1955 nested = isl_space_drop_dims(nested, isl_dim_param,
1956 n_space, n_nested - n_space);
1959 space->nested[1] = nested;
1961 isl_space_free(model);
1964 isl_space_free(model);
1965 isl_space_free(space);