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 /* Check if "s" is a valid dimension or tuple name.
351 * We currently only forbid names that look like a number.
353 * s is assumed to be non-NULL.
355 static int name_ok(isl_ctx *ctx, const char *s)
360 dummy = strtol(s, &p, 0);
362 isl_die(ctx, isl_error_invalid, "name looks like a number",
368 /* Is it possible for the given dimension type to have a tuple id?
370 static int space_can_have_id(__isl_keep isl_space *space,
371 enum isl_dim_type type)
375 if (isl_space_is_params(space))
376 isl_die(space->ctx, isl_error_invalid,
377 "parameter spaces don't have tuple ids", return 0);
378 if (isl_space_is_set(space) && type != isl_dim_set)
379 isl_die(space->ctx, isl_error_invalid,
380 "set spaces can only have a set id", return 0);
381 if (type != isl_dim_in && type != isl_dim_out)
382 isl_die(space->ctx, isl_error_invalid,
383 "only input, output and set tuples can have ids",
389 /* Does the tuple have an id?
391 int isl_space_has_tuple_id(__isl_keep isl_space *dim, enum isl_dim_type type)
393 if (!space_can_have_id(dim, type))
395 return dim->tuple_id[type - isl_dim_in] != NULL;
398 __isl_give isl_id *isl_space_get_tuple_id(__isl_keep isl_space *dim,
399 enum isl_dim_type type)
405 has_id = isl_space_has_tuple_id(dim, type);
409 isl_die(dim->ctx, isl_error_invalid,
410 "tuple has no id", return NULL);
411 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
414 __isl_give isl_space *isl_space_set_tuple_id(__isl_take isl_space *dim,
415 enum isl_dim_type type, __isl_take isl_id *id)
417 dim = isl_space_cow(dim);
420 if (type != isl_dim_in && type != isl_dim_out)
421 isl_die(dim->ctx, isl_error_invalid,
422 "only input, output and set tuples can have names",
425 isl_id_free(dim->tuple_id[type - isl_dim_in]);
426 dim->tuple_id[type - isl_dim_in] = id;
435 __isl_give isl_space *isl_space_reset_tuple_id(__isl_take isl_space *dim,
436 enum isl_dim_type type)
438 dim = isl_space_cow(dim);
441 if (type != isl_dim_in && type != isl_dim_out)
442 isl_die(dim->ctx, isl_error_invalid,
443 "only input, output and set tuples can have names",
446 isl_id_free(dim->tuple_id[type - isl_dim_in]);
447 dim->tuple_id[type - isl_dim_in] = NULL;
455 /* Set the id of the given dimension of "space" to "id".
456 * If the dimension already has an id, then it is replaced.
457 * If the dimension is a parameter, then we need to change it
458 * in the nested spaces (if any) as well.
460 __isl_give isl_space *isl_space_set_dim_id(__isl_take isl_space *space,
461 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
463 space = isl_space_cow(space);
467 if (type == isl_dim_param) {
470 for (i = 0; i < 2; ++i) {
471 if (!space->nested[i])
474 isl_space_set_dim_id(space->nested[i],
475 type, pos, isl_id_copy(id));
476 if (!space->nested[i])
481 isl_id_free(get_id(space, type, pos));
482 return set_id(space, type, pos, id);
485 isl_space_free(space);
489 /* Reset the id of the given dimension of "space".
490 * If the dimension already has an id, then it is removed.
491 * If the dimension is a parameter, then we need to reset it
492 * in the nested spaces (if any) as well.
494 __isl_give isl_space *isl_space_reset_dim_id(__isl_take isl_space *space,
495 enum isl_dim_type type, unsigned pos)
497 space = isl_space_cow(space);
501 if (type == isl_dim_param) {
504 for (i = 0; i < 2; ++i) {
505 if (!space->nested[i])
508 isl_space_reset_dim_id(space->nested[i],
510 if (!space->nested[i])
515 isl_id_free(get_id(space, type, pos));
516 return set_id(space, type, pos, NULL);
518 isl_space_free(space);
522 int isl_space_has_dim_id(__isl_keep isl_space *dim,
523 enum isl_dim_type type, unsigned pos)
527 return get_id(dim, type, pos) != NULL;
530 __isl_give isl_id *isl_space_get_dim_id(__isl_keep isl_space *dim,
531 enum isl_dim_type type, unsigned pos)
535 if (!get_id(dim, type, pos))
536 isl_die(dim->ctx, isl_error_invalid,
537 "dim has no id", return NULL);
538 return isl_id_copy(get_id(dim, type, pos));
541 __isl_give isl_space *isl_space_set_tuple_name(__isl_take isl_space *dim,
542 enum isl_dim_type type, const char *s)
550 return isl_space_reset_tuple_id(dim, type);
552 if (!name_ok(dim->ctx, s))
555 id = isl_id_alloc(dim->ctx, s, NULL);
556 return isl_space_set_tuple_id(dim, type, id);
562 /* Does the tuple have a name?
564 int isl_space_has_tuple_name(__isl_keep isl_space *space,
565 enum isl_dim_type type)
569 if (!space_can_have_id(space, type))
571 id = space->tuple_id[type - isl_dim_in];
572 return id && id->name;
575 const char *isl_space_get_tuple_name(__isl_keep isl_space *dim,
576 enum isl_dim_type type)
581 if (type != isl_dim_in && type != isl_dim_out)
583 id = dim->tuple_id[type - isl_dim_in];
584 return id ? id->name : NULL;
587 __isl_give isl_space *isl_space_set_dim_name(__isl_take isl_space *dim,
588 enum isl_dim_type type, unsigned pos,
596 return isl_space_reset_dim_id(dim, type, pos);
597 if (!name_ok(dim->ctx, s))
599 id = isl_id_alloc(dim->ctx, s, NULL);
600 return isl_space_set_dim_id(dim, type, pos, id);
606 /* Does the given dimension have a name?
608 int isl_space_has_dim_name(__isl_keep isl_space *space,
609 enum isl_dim_type type, unsigned pos)
615 id = get_id(space, type, pos);
616 return id && id->name;
619 __isl_keep const char *isl_space_get_dim_name(__isl_keep isl_space *dim,
620 enum isl_dim_type type, unsigned pos)
622 isl_id *id = get_id(dim, type, pos);
623 return id ? id->name : NULL;
626 int isl_space_find_dim_by_id(__isl_keep isl_space *dim, enum isl_dim_type type,
627 __isl_keep isl_id *id)
636 offset = isl_space_offset(dim, type);
637 n = isl_space_dim(dim, type);
638 for (i = 0; i < n && offset + i < dim->n_id; ++i)
639 if (dim->ids[offset + i] == id)
645 int isl_space_find_dim_by_name(__isl_keep isl_space *space,
646 enum isl_dim_type type, const char *name)
655 offset = isl_space_offset(space, type);
656 n = isl_space_dim(space, type);
657 for (i = 0; i < n && offset + i < space->n_id; ++i)
658 if (space->ids[offset + i]->name &&
659 !strcmp(space->ids[offset + i]->name, name))
665 static __isl_keep isl_id *tuple_id(__isl_keep isl_space *dim,
666 enum isl_dim_type type)
670 if (type == isl_dim_in)
671 return dim->tuple_id[0];
672 if (type == isl_dim_out)
673 return dim->tuple_id[1];
677 static __isl_keep isl_space *nested(__isl_keep isl_space *dim,
678 enum isl_dim_type type)
682 if (type == isl_dim_in)
683 return dim->nested[0];
684 if (type == isl_dim_out)
685 return dim->nested[1];
689 int isl_space_tuple_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
690 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
693 isl_space *nested1, *nested2;
698 if (dim1 == dim2 && dim1_type == dim2_type)
701 if (n(dim1, dim1_type) != n(dim2, dim2_type))
703 id1 = tuple_id(dim1, dim1_type);
704 id2 = tuple_id(dim2, dim2_type);
707 if (id1 && id1 != id2)
709 nested1 = nested(dim1, dim1_type);
710 nested2 = nested(dim2, dim2_type);
711 if (!nested1 ^ !nested2)
713 if (nested1 && !isl_space_is_equal(nested1, nested2))
718 static int match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
719 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
723 if (dim1 == dim2 && dim1_type == dim2_type)
726 if (!isl_space_tuple_match(dim1, dim1_type, dim2, dim2_type))
729 if (!dim1->ids && !dim2->ids)
732 for (i = 0; i < n(dim1, dim1_type); ++i) {
733 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
739 int isl_space_match(__isl_keep isl_space *dim1, enum isl_dim_type dim1_type,
740 __isl_keep isl_space *dim2, enum isl_dim_type dim2_type)
745 return match(dim1, dim1_type, dim2, dim2_type);
748 static void get_ids(__isl_keep isl_space *dim, enum isl_dim_type type,
749 unsigned first, unsigned n, __isl_keep isl_id **ids)
753 for (i = 0; i < n ; ++i)
754 ids[i] = get_id(dim, type, first + i);
757 __isl_give isl_space *isl_space_extend(__isl_take isl_space *dim,
758 unsigned nparam, unsigned n_in, unsigned n_out)
764 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
767 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
768 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
769 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
771 dim = isl_space_cow(dim);
774 ids = isl_calloc_array(dim->ctx, isl_id *,
775 nparam + n_in + n_out);
778 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
779 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
780 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
783 dim->n_id = nparam + n_in + n_out;
785 dim->nparam = nparam;
796 __isl_give isl_space *isl_space_add_dims(__isl_take isl_space *dim,
797 enum isl_dim_type type, unsigned n)
801 dim = isl_space_reset(dim, type);
804 dim = isl_space_extend(dim,
805 dim->nparam + n, dim->n_in, dim->n_out);
806 if (dim && dim->nested[0] &&
807 !(dim->nested[0] = isl_space_add_dims(dim->nested[0],
810 if (dim && dim->nested[1] &&
811 !(dim->nested[1] = isl_space_add_dims(dim->nested[1],
816 return isl_space_extend(dim,
817 dim->nparam, dim->n_in + n, dim->n_out);
819 return isl_space_extend(dim,
820 dim->nparam, dim->n_in, dim->n_out + n);
822 isl_die(dim->ctx, isl_error_invalid,
823 "cannot add dimensions of specified type", goto error);
830 static int valid_dim_type(enum isl_dim_type type)
842 __isl_give isl_space *isl_space_insert_dims(__isl_take isl_space *dim,
843 enum isl_dim_type type, unsigned pos, unsigned n)
850 return isl_space_reset(dim, type);
852 if (!valid_dim_type(type))
853 isl_die(dim->ctx, isl_error_invalid,
854 "cannot insert dimensions of specified type",
857 isl_assert(dim->ctx, pos <= isl_space_dim(dim, type), goto error);
859 dim = isl_space_cow(dim);
867 int *size = s - isl_dim_param;
868 ids = isl_calloc_array(dim->ctx, isl_id *,
869 dim->nparam + dim->n_in + dim->n_out + n);
873 size[isl_dim_param] = dim->nparam;
874 size[isl_dim_in] = dim->n_in;
875 size[isl_dim_out] = dim->n_out;
876 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
878 get_ids(dim, t, 0, size[t], ids + off);
881 get_ids(dim, t, 0, pos, ids + off);
883 get_ids(dim, t, pos, size[t] - pos, ids + off);
884 off += size[t] - pos;
889 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
892 case isl_dim_param: dim->nparam += n; break;
893 case isl_dim_in: dim->n_in += n; break;
894 case isl_dim_out: dim->n_out += n; break;
897 dim = isl_space_reset(dim, type);
905 __isl_give isl_space *isl_space_move_dims(__isl_take isl_space *dim,
906 enum isl_dim_type dst_type, unsigned dst_pos,
907 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
916 isl_assert(dim->ctx, src_pos + n <= isl_space_dim(dim, src_type),
919 if (dst_type == src_type && dst_pos == src_pos)
922 isl_assert(dim->ctx, dst_type != src_type, goto error);
924 dim = isl_space_reset(dim, src_type);
925 dim = isl_space_reset(dim, dst_type);
927 dim = isl_space_cow(dim);
936 int *size = s - isl_dim_param;
937 ids = isl_calloc_array(dim->ctx, isl_id *,
938 dim->nparam + dim->n_in + dim->n_out);
942 size[isl_dim_param] = dim->nparam;
943 size[isl_dim_in] = dim->n_in;
944 size[isl_dim_out] = dim->n_out;
945 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
947 get_ids(dim, t, 0, dst_pos, ids + off);
949 get_ids(dim, src_type, src_pos, n, ids + off);
951 get_ids(dim, t, dst_pos, size[t] - dst_pos,
953 off += size[t] - dst_pos;
954 } else if (t == src_type) {
955 get_ids(dim, t, 0, src_pos, ids + off);
957 get_ids(dim, t, src_pos + n,
958 size[t] - src_pos - n, ids + off);
959 off += size[t] - src_pos - n;
961 get_ids(dim, t, 0, size[t], ids + off);
967 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
971 case isl_dim_param: dim->nparam += n; break;
972 case isl_dim_in: dim->n_in += n; break;
973 case isl_dim_out: dim->n_out += n; break;
978 case isl_dim_param: dim->nparam -= n; break;
979 case isl_dim_in: dim->n_in -= n; break;
980 case isl_dim_out: dim->n_out -= n; break;
984 if (dst_type != isl_dim_param && src_type != isl_dim_param)
987 for (i = 0; i < 2; ++i) {
990 dim->nested[i] = isl_space_replace(dim->nested[i],
1002 __isl_give isl_space *isl_space_join(__isl_take isl_space *left,
1003 __isl_take isl_space *right)
1007 if (!left || !right)
1010 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1012 isl_assert(left->ctx,
1013 isl_space_tuple_match(left, isl_dim_out, right, isl_dim_in),
1016 dim = isl_space_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
1020 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
1021 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
1022 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
1024 if (dim && left->tuple_id[0] &&
1025 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
1027 if (dim && right->tuple_id[1] &&
1028 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
1030 if (dim && left->nested[0] &&
1031 !(dim->nested[0] = isl_space_copy(left->nested[0])))
1033 if (dim && right->nested[1] &&
1034 !(dim->nested[1] = isl_space_copy(right->nested[1])))
1037 isl_space_free(left);
1038 isl_space_free(right);
1042 isl_space_free(left);
1043 isl_space_free(right);
1047 __isl_give isl_space *isl_space_product(__isl_take isl_space *left,
1048 __isl_take isl_space *right)
1050 isl_space *dom1, *dom2, *nest1, *nest2;
1052 if (!left || !right)
1055 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1058 dom1 = isl_space_domain(isl_space_copy(left));
1059 dom2 = isl_space_domain(isl_space_copy(right));
1060 nest1 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1062 dom1 = isl_space_range(left);
1063 dom2 = isl_space_range(right);
1064 nest2 = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1066 return isl_space_join(isl_space_reverse(nest1), nest2);
1068 isl_space_free(left);
1069 isl_space_free(right);
1073 /* Given two spaces { A -> C } and { B -> C }, construct the space
1076 __isl_give isl_space *isl_space_domain_product(__isl_take isl_space *left,
1077 __isl_take isl_space *right)
1079 isl_space *ran, *dom1, *dom2, *nest;
1081 if (!left || !right)
1084 if (!match(left, isl_dim_param, right, isl_dim_param))
1085 isl_die(left->ctx, isl_error_invalid,
1086 "parameters need to match", goto error);
1087 if (!isl_space_tuple_match(left, isl_dim_out, right, isl_dim_out))
1088 isl_die(left->ctx, isl_error_invalid,
1089 "ranges need to match", goto error);
1091 ran = isl_space_range(isl_space_copy(left));
1093 dom1 = isl_space_domain(left);
1094 dom2 = isl_space_domain(right);
1095 nest = isl_space_wrap(isl_space_join(isl_space_reverse(dom1), dom2));
1097 return isl_space_join(isl_space_reverse(nest), ran);
1099 isl_space_free(left);
1100 isl_space_free(right);
1104 __isl_give isl_space *isl_space_range_product(__isl_take isl_space *left,
1105 __isl_take isl_space *right)
1107 isl_space *dom, *ran1, *ran2, *nest;
1109 if (!left || !right)
1112 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
1114 if (!isl_space_tuple_match(left, isl_dim_in, right, isl_dim_in))
1115 isl_die(left->ctx, isl_error_invalid,
1116 "domains need to match", goto error);
1118 dom = isl_space_domain(isl_space_copy(left));
1120 ran1 = isl_space_range(left);
1121 ran2 = isl_space_range(right);
1122 nest = isl_space_wrap(isl_space_join(isl_space_reverse(ran1), ran2));
1124 return isl_space_join(isl_space_reverse(dom), nest);
1126 isl_space_free(left);
1127 isl_space_free(right);
1131 __isl_give isl_space *isl_space_map_from_set(__isl_take isl_space *dim)
1134 isl_id **ids = NULL;
1138 ctx = isl_space_get_ctx(dim);
1139 if (!isl_space_is_set(dim))
1140 isl_die(ctx, isl_error_invalid, "not a set space", goto error);
1141 dim = isl_space_cow(dim);
1145 ids = isl_calloc_array(dim->ctx, isl_id *,
1146 dim->nparam + dim->n_out + dim->n_out);
1149 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
1150 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
1152 dim->n_in = dim->n_out;
1156 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
1157 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
1159 isl_id_free(dim->tuple_id[0]);
1160 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
1161 isl_space_free(dim->nested[0]);
1162 dim->nested[0] = isl_space_copy(dim->nested[1]);
1165 isl_space_free(dim);
1169 __isl_give isl_space *isl_space_map_from_domain_and_range(
1170 __isl_take isl_space *domain, __isl_take isl_space *range)
1172 if (!domain || !range)
1174 if (!isl_space_is_set(domain))
1175 isl_die(isl_space_get_ctx(domain), isl_error_invalid,
1176 "domain is not a set space", goto error);
1177 if (!isl_space_is_set(range))
1178 isl_die(isl_space_get_ctx(range), isl_error_invalid,
1179 "range is not a set space", goto error);
1180 return isl_space_join(isl_space_reverse(domain), range);
1182 isl_space_free(domain);
1183 isl_space_free(range);
1187 static __isl_give isl_space *set_ids(__isl_take isl_space *dim,
1188 enum isl_dim_type type,
1189 unsigned first, unsigned n, __isl_take isl_id **ids)
1193 for (i = 0; i < n ; ++i)
1194 dim = set_id(dim, type, first + i, ids[i]);
1199 __isl_give isl_space *isl_space_reverse(__isl_take isl_space *dim)
1203 isl_id **ids = NULL;
1208 if (match(dim, isl_dim_in, dim, isl_dim_out))
1211 dim = isl_space_cow(dim);
1215 id = dim->tuple_id[0];
1216 dim->tuple_id[0] = dim->tuple_id[1];
1217 dim->tuple_id[1] = id;
1219 nested = dim->nested[0];
1220 dim->nested[0] = dim->nested[1];
1221 dim->nested[1] = nested;
1224 ids = isl_alloc_array(dim->ctx, isl_id *,
1225 dim->n_in + dim->n_out);
1228 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
1229 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
1233 dim->n_in = dim->n_out;
1237 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
1238 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
1245 isl_space_free(dim);
1249 __isl_give isl_space *isl_space_drop_dims(__isl_take isl_space *dim,
1250 enum isl_dim_type type, unsigned first, unsigned num)
1258 return isl_space_reset(dim, type);
1260 if (!valid_dim_type(type))
1261 isl_die(dim->ctx, isl_error_invalid,
1262 "cannot drop dimensions of specified type", goto error);
1264 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1265 dim = isl_space_cow(dim);
1269 dim = extend_ids(dim);
1272 for (i = 0; i < num; ++i)
1273 isl_id_free(get_id(dim, type, first + i));
1274 for (i = first+num; i < n(dim, type); ++i)
1275 set_id(dim, type, i - num, get_id(dim, type, i));
1278 get_ids(dim, isl_dim_in, 0, dim->n_in,
1279 dim->ids + offset(dim, isl_dim_in) - num);
1281 get_ids(dim, isl_dim_out, 0, dim->n_out,
1282 dim->ids + offset(dim, isl_dim_out) - num);
1289 case isl_dim_param: dim->nparam -= num; break;
1290 case isl_dim_in: dim->n_in -= num; break;
1291 case isl_dim_out: dim->n_out -= num; break;
1294 dim = isl_space_reset(dim, type);
1295 if (type == isl_dim_param) {
1296 if (dim && dim->nested[0] &&
1297 !(dim->nested[0] = isl_space_drop_dims(dim->nested[0],
1298 isl_dim_param, first, num)))
1300 if (dim && dim->nested[1] &&
1301 !(dim->nested[1] = isl_space_drop_dims(dim->nested[1],
1302 isl_dim_param, first, num)))
1307 isl_space_free(dim);
1311 __isl_give isl_space *isl_space_drop_inputs(__isl_take isl_space *dim,
1312 unsigned first, unsigned n)
1316 return isl_space_drop_dims(dim, isl_dim_in, first, n);
1319 __isl_give isl_space *isl_space_drop_outputs(__isl_take isl_space *dim,
1320 unsigned first, unsigned n)
1324 return isl_space_drop_dims(dim, isl_dim_out, first, n);
1327 __isl_give isl_space *isl_space_domain(__isl_take isl_space *dim)
1331 dim = isl_space_drop_outputs(dim, 0, dim->n_out);
1332 dim = isl_space_reverse(dim);
1333 dim = mark_as_set(dim);
1337 __isl_give isl_space *isl_space_from_domain(__isl_take isl_space *dim)
1341 if (!isl_space_is_set(dim))
1342 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1343 "not a set space", goto error);
1344 dim = isl_space_reverse(dim);
1345 dim = isl_space_reset(dim, isl_dim_out);
1348 isl_space_free(dim);
1352 __isl_give isl_space *isl_space_range(__isl_take isl_space *dim)
1356 dim = isl_space_drop_inputs(dim, 0, dim->n_in);
1357 dim = mark_as_set(dim);
1361 __isl_give isl_space *isl_space_from_range(__isl_take isl_space *dim)
1365 if (!isl_space_is_set(dim))
1366 isl_die(isl_space_get_ctx(dim), isl_error_invalid,
1367 "not a set space", goto error);
1368 return isl_space_reset(dim, isl_dim_in);
1370 isl_space_free(dim);
1374 __isl_give isl_space *isl_space_params(__isl_take isl_space *space)
1376 if (isl_space_is_params(space))
1378 space = isl_space_drop_dims(space,
1379 isl_dim_in, 0, isl_space_dim(space, isl_dim_in));
1380 space = isl_space_drop_dims(space,
1381 isl_dim_out, 0, isl_space_dim(space, isl_dim_out));
1382 space = mark_as_params(space);
1386 __isl_give isl_space *isl_space_set_from_params(__isl_take isl_space *space)
1390 if (!isl_space_is_params(space))
1391 isl_die(isl_space_get_ctx(space), isl_error_invalid,
1392 "not a parameter space", goto error);
1393 return isl_space_reset(space, isl_dim_set);
1395 isl_space_free(space);
1399 __isl_give isl_space *isl_space_as_set_space(__isl_take isl_space *dim)
1401 dim = isl_space_cow(dim);
1405 dim->n_out += dim->n_in;
1407 dim = isl_space_reset(dim, isl_dim_in);
1408 dim = isl_space_reset(dim, isl_dim_out);
1413 __isl_give isl_space *isl_space_underlying(__isl_take isl_space *dim,
1421 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1422 return isl_space_reset(isl_space_reset(dim, isl_dim_in), isl_dim_out);
1423 dim = isl_space_cow(dim);
1426 dim->n_out += dim->nparam + dim->n_in + n_div;
1430 for (i = 0; i < dim->n_id; ++i)
1431 isl_id_free(get_id(dim, isl_dim_out, i));
1433 dim = isl_space_reset(dim, isl_dim_in);
1434 dim = isl_space_reset(dim, isl_dim_out);
1439 int isl_space_is_equal(__isl_keep isl_space *dim1, __isl_keep isl_space *dim2)
1445 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1446 isl_space_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1447 isl_space_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1450 /* Is space1 equal to the domain of space2?
1452 int isl_space_is_domain(__isl_keep isl_space *space1,
1453 __isl_keep isl_space *space2)
1455 if (!space1 || !space2)
1457 if (!isl_space_is_set(space1))
1459 return match(space1, isl_dim_param, space2, isl_dim_param) &&
1460 isl_space_tuple_match(space1, isl_dim_set, space2, isl_dim_in);
1463 int isl_space_compatible(__isl_keep isl_space *dim1,
1464 __isl_keep isl_space *dim2)
1466 return dim1->nparam == dim2->nparam &&
1467 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1470 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_space *dim)
1478 hash = isl_hash_builtin(hash, dim->nparam);
1479 hash = isl_hash_builtin(hash, dim->n_in);
1480 hash = isl_hash_builtin(hash, dim->n_out);
1482 for (i = 0; i < dim->nparam; ++i) {
1483 id = get_id(dim, isl_dim_param, i);
1484 hash = isl_hash_id(hash, id);
1487 id = tuple_id(dim, isl_dim_in);
1488 hash = isl_hash_id(hash, id);
1489 id = tuple_id(dim, isl_dim_out);
1490 hash = isl_hash_id(hash, id);
1492 hash = isl_hash_dim(hash, dim->nested[0]);
1493 hash = isl_hash_dim(hash, dim->nested[1]);
1498 uint32_t isl_space_get_hash(__isl_keep isl_space *dim)
1505 hash = isl_hash_init();
1506 hash = isl_hash_dim(hash, dim);
1511 int isl_space_is_wrapping(__isl_keep isl_space *dim)
1516 if (!isl_space_is_set(dim))
1519 return dim->nested[1] != NULL;
1522 __isl_give isl_space *isl_space_wrap(__isl_take isl_space *dim)
1529 wrap = isl_space_set_alloc(dim->ctx,
1530 dim->nparam, dim->n_in + dim->n_out);
1532 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1533 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1534 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1539 wrap->nested[1] = dim;
1543 isl_space_free(dim);
1547 __isl_give isl_space *isl_space_unwrap(__isl_take isl_space *dim)
1554 if (!isl_space_is_wrapping(dim))
1555 isl_die(dim->ctx, isl_error_invalid, "not a wrapping dim",
1558 unwrap = isl_space_copy(dim->nested[1]);
1559 isl_space_free(dim);
1563 isl_space_free(dim);
1567 int isl_space_is_named_or_nested(__isl_keep isl_space *dim, enum isl_dim_type type)
1569 if (type != isl_dim_in && type != isl_dim_out)
1573 if (dim->tuple_id[type - isl_dim_in])
1575 if (dim->nested[type - isl_dim_in])
1580 int isl_space_may_be_set(__isl_keep isl_space *dim)
1584 if (isl_space_is_set(dim))
1586 if (isl_space_dim(dim, isl_dim_in) != 0)
1588 if (isl_space_is_named_or_nested(dim, isl_dim_in))
1593 __isl_give isl_space *isl_space_reset(__isl_take isl_space *dim,
1594 enum isl_dim_type type)
1596 if (!isl_space_is_named_or_nested(dim, type))
1599 dim = isl_space_cow(dim);
1603 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1604 dim->tuple_id[type - isl_dim_in] = NULL;
1605 isl_space_free(dim->nested[type - isl_dim_in]);
1606 dim->nested[type - isl_dim_in] = NULL;
1611 __isl_give isl_space *isl_space_flatten(__isl_take isl_space *dim)
1615 if (!dim->nested[0] && !dim->nested[1])
1619 dim = isl_space_reset(dim, isl_dim_in);
1620 if (dim && dim->nested[1])
1621 dim = isl_space_reset(dim, isl_dim_out);
1626 __isl_give isl_space *isl_space_flatten_domain(__isl_take isl_space *dim)
1630 if (!dim->nested[0])
1633 return isl_space_reset(dim, isl_dim_in);
1636 __isl_give isl_space *isl_space_flatten_range(__isl_take isl_space *dim)
1640 if (!dim->nested[1])
1643 return isl_space_reset(dim, isl_dim_out);
1646 /* Replace the dimensions of the given type of dst by those of src.
1648 __isl_give isl_space *isl_space_replace(__isl_take isl_space *dst,
1649 enum isl_dim_type type, __isl_keep isl_space *src)
1651 dst = isl_space_cow(dst);
1656 dst = isl_space_drop_dims(dst, type, 0, isl_space_dim(dst, type));
1657 dst = isl_space_add_dims(dst, type, isl_space_dim(src, type));
1658 dst = copy_ids(dst, type, 0, src, type);
1660 if (dst && type == isl_dim_param) {
1662 for (i = 0; i <= 1; ++i) {
1663 if (!dst->nested[i])
1665 dst->nested[i] = isl_space_replace(dst->nested[i],
1667 if (!dst->nested[i])
1674 isl_space_free(dst);
1678 /* Given a dimension specification "dim" of a set, create a dimension
1679 * specification for the lift of the set. In particular, the result
1680 * is of the form [dim -> local[..]], with n_local variables in the
1681 * range of the wrapped map.
1683 __isl_give isl_space *isl_space_lift(__isl_take isl_space *dim, unsigned n_local)
1685 isl_space *local_dim;
1690 local_dim = isl_space_dup(dim);
1691 local_dim = isl_space_drop_dims(local_dim, isl_dim_set, 0, dim->n_out);
1692 local_dim = isl_space_add_dims(local_dim, isl_dim_set, n_local);
1693 local_dim = isl_space_set_tuple_name(local_dim, isl_dim_set, "local");
1694 dim = isl_space_join(isl_space_from_domain(dim),
1695 isl_space_from_range(local_dim));
1696 dim = isl_space_wrap(dim);
1697 dim = isl_space_set_tuple_name(dim, isl_dim_set, "lifted");
1702 int isl_space_can_zip(__isl_keep isl_space *dim)
1707 return dim->nested[0] && dim->nested[1];
1710 __isl_give isl_space *isl_space_zip(__isl_take isl_space *dim)
1712 isl_space *dom, *ran;
1713 isl_space *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1715 if (!isl_space_can_zip(dim))
1716 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1721 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(dim)));
1722 ran = isl_space_unwrap(isl_space_range(dim));
1723 dom_dom = isl_space_domain(isl_space_copy(dom));
1724 dom_ran = isl_space_range(dom);
1725 ran_dom = isl_space_domain(isl_space_copy(ran));
1726 ran_ran = isl_space_range(ran);
1727 dom = isl_space_join(isl_space_from_domain(dom_dom),
1728 isl_space_from_range(ran_dom));
1729 ran = isl_space_join(isl_space_from_domain(dom_ran),
1730 isl_space_from_range(ran_ran));
1731 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom)),
1732 isl_space_from_range(isl_space_wrap(ran)));
1734 isl_space_free(dim);
1738 /* Can we apply isl_space_curry to "space"?
1739 * That is, does it have a nested relation in its domain?
1741 int isl_space_can_curry(__isl_keep isl_space *space)
1746 return !!space->nested[0];
1749 /* Given a space (A -> B) -> C, return the corresponding space
1752 __isl_give isl_space *isl_space_curry(__isl_take isl_space *space)
1754 isl_space *dom, *ran;
1755 isl_space *dom_dom, *dom_ran;
1760 if (!isl_space_can_curry(space))
1761 isl_die(space->ctx, isl_error_invalid,
1762 "space cannot be curried", goto error);
1764 dom = isl_space_unwrap(isl_space_domain(isl_space_copy(space)));
1765 ran = isl_space_range(space);
1766 dom_dom = isl_space_domain(isl_space_copy(dom));
1767 dom_ran = isl_space_range(dom);
1768 ran = isl_space_join(isl_space_from_domain(dom_ran),
1769 isl_space_from_range(ran));
1770 return isl_space_join(isl_space_from_domain(dom_dom),
1771 isl_space_from_range(isl_space_wrap(ran)));
1773 isl_space_free(space);
1777 int isl_space_has_named_params(__isl_keep isl_space *dim)
1784 if (dim->nparam == 0)
1786 off = isl_space_offset(dim, isl_dim_param);
1787 if (off + dim->nparam > dim->n_id)
1789 for (i = 0; i < dim->nparam; ++i)
1790 if (!dim->ids[off + i])
1795 /* Align the initial parameters of dim1 to match the order in dim2.
1797 __isl_give isl_space *isl_space_align_params(__isl_take isl_space *dim1,
1798 __isl_take isl_space *dim2)
1800 isl_reordering *exp;
1802 if (!isl_space_has_named_params(dim1) || !isl_space_has_named_params(dim2))
1803 isl_die(isl_space_get_ctx(dim1), isl_error_invalid,
1804 "parameter alignment requires named parameters",
1807 dim2 = isl_space_params(dim2);
1808 exp = isl_parameter_alignment_reordering(dim1, dim2);
1809 exp = isl_reordering_extend_space(exp, dim1);
1810 isl_space_free(dim2);
1813 dim1 = isl_space_copy(exp->dim);
1814 isl_reordering_free(exp);
1817 isl_space_free(dim1);
1818 isl_space_free(dim2);
1822 /* Given the space of set (domain), construct a space for a map
1823 * with as domain the given space and as range the range of "model".
1825 __isl_give isl_space *isl_space_extend_domain_with_range(
1826 __isl_take isl_space *domain, __isl_take isl_space *model)
1830 space = isl_space_from_domain(domain);
1831 space = isl_space_add_dims(space, isl_dim_out,
1832 isl_space_dim(model, isl_dim_out));
1833 if (isl_space_has_tuple_id(model, isl_dim_out))
1834 space = isl_space_set_tuple_id(space, isl_dim_out,
1835 isl_space_get_tuple_id(model, isl_dim_out));
1836 isl_space_free(model);