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_dim_private.h>
15 #include <isl_id_private.h>
16 #include <isl_reordering.h>
18 isl_ctx *isl_dim_get_ctx(__isl_keep isl_dim *dim)
20 return dim ? dim->ctx : NULL;
23 struct isl_dim *isl_dim_alloc(struct isl_ctx *ctx,
24 unsigned nparam, unsigned n_in, unsigned n_out)
28 dim = isl_alloc_type(ctx, struct isl_dim);
39 dim->tuple_id[0] = NULL;
40 dim->tuple_id[1] = NULL;
42 dim->nested[0] = NULL;
43 dim->nested[1] = NULL;
51 struct isl_dim *isl_dim_set_alloc(struct isl_ctx *ctx,
52 unsigned nparam, unsigned dim)
54 return isl_dim_alloc(ctx, nparam, 0, dim);
57 static unsigned global_pos(struct isl_dim *dim,
58 enum isl_dim_type type, unsigned pos)
60 struct isl_ctx *ctx = dim->ctx;
64 isl_assert(ctx, pos < dim->nparam, return isl_dim_total(dim));
67 isl_assert(ctx, pos < dim->n_in, return isl_dim_total(dim));
68 return pos + dim->nparam;
70 isl_assert(ctx, pos < dim->n_out, return isl_dim_total(dim));
71 return pos + dim->nparam + dim->n_in;
73 isl_assert(ctx, 0, return isl_dim_total(dim));
75 return isl_dim_total(dim);
78 /* Extend length of ids array to the total number of dimensions.
80 static __isl_give isl_dim *extend_ids(__isl_take isl_dim *dim)
85 if (isl_dim_total(dim) <= dim->n_id)
89 dim->ids = isl_calloc_array(dim->ctx,
90 isl_id *, isl_dim_total(dim));
94 ids = isl_realloc_array(dim->ctx, dim->ids,
95 isl_id *, isl_dim_total(dim));
99 for (i = dim->n_id; i < isl_dim_total(dim); ++i)
103 dim->n_id = isl_dim_total(dim);
111 static __isl_give isl_dim *set_id(__isl_take isl_dim *dim,
112 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
114 dim = isl_dim_cow(dim);
119 pos = global_pos(dim, type, pos);
120 if (pos == isl_dim_total(dim))
123 if (pos >= dim->n_id) {
126 dim = extend_ids(dim);
140 static __isl_keep isl_id *get_id(__isl_keep isl_dim *dim,
141 enum isl_dim_type type, unsigned pos)
146 pos = global_pos(dim, type, pos);
147 if (pos == isl_dim_total(dim))
149 if (pos >= dim->n_id)
151 return dim->ids[pos];
154 static unsigned offset(struct isl_dim *dim, enum isl_dim_type type)
157 case isl_dim_param: return 0;
158 case isl_dim_in: return dim->nparam;
159 case isl_dim_out: return dim->nparam + dim->n_in;
164 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
167 case isl_dim_param: return dim->nparam;
168 case isl_dim_in: return dim->n_in;
169 case isl_dim_out: return dim->n_out;
170 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
175 unsigned isl_dim_size(struct isl_dim *dim, enum isl_dim_type type)
182 unsigned isl_dim_offset(__isl_keep isl_dim *dim, enum isl_dim_type type)
186 return offset(dim, type);
189 static __isl_give isl_dim *copy_ids(__isl_take isl_dim *dst,
190 enum isl_dim_type dst_type, unsigned offset, __isl_keep isl_dim *src,
191 enum isl_dim_type src_type)
199 for (i = 0; i < n(src, src_type); ++i) {
200 id = get_id(src, src_type, i);
203 dst = set_id(dst, dst_type, offset + i, isl_id_copy(id));
210 struct isl_dim *isl_dim_dup(struct isl_dim *dim)
215 dup = isl_dim_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
216 if (dim->tuple_id[0] &&
217 !(dup->tuple_id[0] = isl_id_copy(dim->tuple_id[0])))
219 if (dim->tuple_id[1] &&
220 !(dup->tuple_id[1] = isl_id_copy(dim->tuple_id[1])))
222 if (dim->nested[0] && !(dup->nested[0] = isl_dim_copy(dim->nested[0])))
224 if (dim->nested[1] && !(dup->nested[1] = isl_dim_copy(dim->nested[1])))
228 dup = copy_ids(dup, isl_dim_param, 0, dim, isl_dim_param);
229 dup = copy_ids(dup, isl_dim_in, 0, dim, isl_dim_in);
230 dup = copy_ids(dup, isl_dim_out, 0, dim, isl_dim_out);
237 struct isl_dim *isl_dim_cow(struct isl_dim *dim)
245 return isl_dim_dup(dim);
248 struct isl_dim *isl_dim_copy(struct isl_dim *dim)
257 void isl_dim_free(struct isl_dim *dim)
267 isl_id_free(dim->tuple_id[0]);
268 isl_id_free(dim->tuple_id[1]);
270 isl_dim_free(dim->nested[0]);
271 isl_dim_free(dim->nested[1]);
273 for (i = 0; i < dim->n_id; ++i)
274 isl_id_free(dim->ids[i]);
276 isl_ctx_deref(dim->ctx);
281 static int name_ok(isl_ctx *ctx, const char *s)
286 dummy = strtol(s, &p, 0);
288 isl_die(ctx, isl_error_invalid, "name looks like a number",
294 int isl_dim_has_tuple_id(__isl_keep isl_dim *dim, enum isl_dim_type type)
298 if (type != isl_dim_in && type != isl_dim_out)
299 isl_die(dim->ctx, isl_error_invalid,
300 "only input, output and set tuples can have ids",
302 return dim->tuple_id[type - isl_dim_in] != NULL;
305 __isl_give isl_id *isl_dim_get_tuple_id(__isl_keep isl_dim *dim,
306 enum isl_dim_type type)
310 if (type != isl_dim_in && type != isl_dim_out)
311 isl_die(dim->ctx, isl_error_invalid,
312 "only input, output and set tuples can have ids",
314 if (!dim->tuple_id[type - isl_dim_in])
315 isl_die(dim->ctx, isl_error_invalid,
316 "tuple has no id", return NULL);
317 return isl_id_copy(dim->tuple_id[type - isl_dim_in]);
320 __isl_give isl_dim *isl_dim_set_tuple_id(__isl_take isl_dim *dim,
321 enum isl_dim_type type, __isl_take isl_id *id)
323 dim = isl_dim_cow(dim);
326 if (type != isl_dim_in && type != isl_dim_out)
327 isl_die(dim->ctx, isl_error_invalid,
328 "only input, output and set tuples can have names",
331 isl_id_free(dim->tuple_id[type - isl_dim_in]);
332 dim->tuple_id[type - isl_dim_in] = id;
341 __isl_give isl_dim *isl_dim_reset_tuple_id(__isl_take isl_dim *dim,
342 enum isl_dim_type type)
344 dim = isl_dim_cow(dim);
347 if (type != isl_dim_in && type != isl_dim_out)
348 isl_die(dim->ctx, isl_error_invalid,
349 "only input, output and set tuples can have names",
352 isl_id_free(dim->tuple_id[type - isl_dim_in]);
353 dim->tuple_id[type - isl_dim_in] = NULL;
361 __isl_give isl_dim *isl_dim_set_dim_id(__isl_take isl_dim *dim,
362 enum isl_dim_type type, unsigned pos, __isl_take isl_id *id)
364 dim = isl_dim_cow(dim);
367 isl_id_free(get_id(dim, type, pos));
368 return set_id(dim, type, pos, id);
375 int isl_dim_has_dim_id(__isl_keep isl_dim *dim,
376 enum isl_dim_type type, unsigned pos)
380 return get_id(dim, type, pos) != NULL;
383 __isl_give isl_id *isl_dim_get_dim_id(__isl_keep isl_dim *dim,
384 enum isl_dim_type type, unsigned pos)
388 if (!get_id(dim, type, pos))
389 isl_die(dim->ctx, isl_error_invalid,
390 "dim has no id", return NULL);
391 return isl_id_copy(get_id(dim, type, pos));
394 __isl_give isl_dim *isl_dim_set_tuple_name(__isl_take isl_dim *dim,
395 enum isl_dim_type type, const char *s)
403 return isl_dim_reset_tuple_id(dim, type);
405 if (!name_ok(dim->ctx, s))
408 id = isl_id_alloc(dim->ctx, s, NULL);
409 return isl_dim_set_tuple_id(dim, type, id);
415 const char *isl_dim_get_tuple_name(__isl_keep isl_dim *dim,
416 enum isl_dim_type type)
421 if (type != isl_dim_in && type != isl_dim_out)
423 id = dim->tuple_id[type - isl_dim_in];
424 return id ? id->name : NULL;
427 struct isl_dim *isl_dim_set_name(struct isl_dim *dim,
428 enum isl_dim_type type, unsigned pos,
435 if (!name_ok(dim->ctx, s))
437 id = isl_id_alloc(dim->ctx, s, NULL);
438 return isl_dim_set_dim_id(dim, type, pos, id);
444 const char *isl_dim_get_name(struct isl_dim *dim,
445 enum isl_dim_type type, unsigned pos)
447 isl_id *id = get_id(dim, type, pos);
448 return id ? id->name : NULL;
451 int isl_dim_find_dim_by_id(__isl_keep isl_dim *dim, enum isl_dim_type type,
452 __isl_keep isl_id *id)
461 offset = isl_dim_offset(dim, type);
462 n = isl_dim_size(dim, type);
463 for (i = 0; i < n && offset + i < dim->n_id; ++i)
464 if (dim->ids[offset + i] == id)
470 static __isl_keep isl_id *tuple_id(__isl_keep isl_dim *dim,
471 enum isl_dim_type type)
475 if (type == isl_dim_in)
476 return dim->tuple_id[0];
477 if (type == isl_dim_out)
478 return dim->tuple_id[1];
482 static __isl_keep isl_dim *nested(__isl_keep isl_dim *dim,
483 enum isl_dim_type type)
487 if (type == isl_dim_in)
488 return dim->nested[0];
489 if (type == isl_dim_out)
490 return dim->nested[1];
494 int isl_dim_tuple_match(__isl_keep isl_dim *dim1, enum isl_dim_type dim1_type,
495 __isl_keep isl_dim *dim2, enum isl_dim_type dim2_type)
498 isl_dim *nested1, *nested2;
503 if (n(dim1, dim1_type) != n(dim2, dim2_type))
505 id1 = tuple_id(dim1, dim1_type);
506 id2 = tuple_id(dim2, dim2_type);
509 if (id1 && id1 != id2)
511 nested1 = nested(dim1, dim1_type);
512 nested2 = nested(dim2, dim2_type);
513 if (!nested1 ^ !nested2)
515 if (nested1 && !isl_dim_equal(nested1, nested2))
520 static int match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
521 struct isl_dim *dim2, enum isl_dim_type dim2_type)
525 if (!isl_dim_tuple_match(dim1, dim1_type, dim2, dim2_type))
528 if (!dim1->ids && !dim2->ids)
531 for (i = 0; i < n(dim1, dim1_type); ++i) {
532 if (get_id(dim1, dim1_type, i) != get_id(dim2, dim2_type, i))
538 int isl_dim_match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
539 struct isl_dim *dim2, enum isl_dim_type dim2_type)
541 return match(dim1, dim1_type, dim2, dim2_type);
544 static void get_ids(struct isl_dim *dim, enum isl_dim_type type,
545 unsigned first, unsigned n, __isl_keep isl_id **ids)
549 for (i = 0; i < n ; ++i)
550 ids[i] = get_id(dim, type, first + i);
553 struct isl_dim *isl_dim_extend(struct isl_dim *dim,
554 unsigned nparam, unsigned n_in, unsigned n_out)
560 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
563 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
564 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
565 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
567 dim = isl_dim_cow(dim);
570 ids = isl_calloc_array(dim->ctx, isl_id *,
571 nparam + n_in + n_out);
574 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
575 get_ids(dim, isl_dim_in, 0, dim->n_in, ids + nparam);
576 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + nparam + n_in);
579 dim->n_id = nparam + n_in + n_out;
581 dim->nparam = nparam;
592 struct isl_dim *isl_dim_add(struct isl_dim *dim, enum isl_dim_type type,
597 dim = isl_dim_reset(dim, type);
600 dim = isl_dim_extend(dim,
601 dim->nparam + n, dim->n_in, dim->n_out);
602 if (dim && dim->nested[0] &&
603 !(dim->nested[0] = isl_dim_add(dim->nested[0],
606 if (dim && dim->nested[1] &&
607 !(dim->nested[1] = isl_dim_add(dim->nested[1],
612 return isl_dim_extend(dim,
613 dim->nparam, dim->n_in + n, dim->n_out);
615 return isl_dim_extend(dim,
616 dim->nparam, dim->n_in, dim->n_out + n);
618 isl_die(dim->ctx, isl_error_invalid,
619 "cannot add dimensions of specified type", goto error);
626 static int valid_dim_type(enum isl_dim_type type)
638 __isl_give isl_dim *isl_dim_insert(__isl_take isl_dim *dim,
639 enum isl_dim_type type, unsigned pos, unsigned n)
646 return isl_dim_reset(dim, type);
648 if (!valid_dim_type(type))
649 isl_die(dim->ctx, isl_error_invalid,
650 "cannot insert dimensions of specified type",
653 isl_assert(dim->ctx, pos <= isl_dim_size(dim, type), goto error);
655 dim = isl_dim_cow(dim);
663 int *size = s - isl_dim_param;
664 ids = isl_calloc_array(dim->ctx, isl_id *,
665 dim->nparam + dim->n_in + dim->n_out + n);
669 size[isl_dim_param] = dim->nparam;
670 size[isl_dim_in] = dim->n_in;
671 size[isl_dim_out] = dim->n_out;
672 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
674 get_ids(dim, t, 0, size[t], ids + off);
677 get_ids(dim, t, 0, pos, ids + off);
679 get_ids(dim, t, pos, size[t] - pos, ids + off);
680 off += size[t] - pos;
685 dim->n_id = dim->nparam + dim->n_in + dim->n_out + n;
688 case isl_dim_param: dim->nparam += n; break;
689 case isl_dim_in: dim->n_in += n; break;
690 case isl_dim_out: dim->n_out += n; break;
693 dim = isl_dim_reset(dim, type);
701 __isl_give isl_dim *isl_dim_move(__isl_take isl_dim *dim,
702 enum isl_dim_type dst_type, unsigned dst_pos,
703 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
712 isl_assert(dim->ctx, src_pos + n <= isl_dim_size(dim, src_type),
715 if (dst_type == src_type && dst_pos == src_pos)
718 isl_assert(dim->ctx, dst_type != src_type, goto error);
720 dim = isl_dim_reset(dim, src_type);
721 dim = isl_dim_reset(dim, dst_type);
723 dim = isl_dim_cow(dim);
732 int *size = s - isl_dim_param;
733 ids = isl_calloc_array(dim->ctx, isl_id *,
734 dim->nparam + dim->n_in + dim->n_out);
738 size[isl_dim_param] = dim->nparam;
739 size[isl_dim_in] = dim->n_in;
740 size[isl_dim_out] = dim->n_out;
741 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
743 get_ids(dim, t, 0, dst_pos, ids + off);
745 get_ids(dim, src_type, src_pos, n, ids + off);
747 get_ids(dim, t, dst_pos, size[t] - dst_pos,
749 off += size[t] - dst_pos;
750 } else if (t == src_type) {
751 get_ids(dim, t, 0, src_pos, ids + off);
753 get_ids(dim, t, src_pos + n,
754 size[t] - src_pos - n, ids + off);
755 off += size[t] - src_pos - n;
757 get_ids(dim, t, 0, size[t], ids + off);
763 dim->n_id = dim->nparam + dim->n_in + dim->n_out;
767 case isl_dim_param: dim->nparam += n; break;
768 case isl_dim_in: dim->n_in += n; break;
769 case isl_dim_out: dim->n_out += n; break;
774 case isl_dim_param: dim->nparam -= n; break;
775 case isl_dim_in: dim->n_in -= n; break;
776 case isl_dim_out: dim->n_out -= n; break;
780 if (dst_type != isl_dim_param && src_type != isl_dim_param)
783 for (i = 0; i < 2; ++i) {
786 dim->nested[i] = isl_dim_replace(dim->nested[i],
798 struct isl_dim *isl_dim_join(struct isl_dim *left, struct isl_dim *right)
805 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
807 isl_assert(left->ctx,
808 isl_dim_tuple_match(left, isl_dim_out, right, isl_dim_in),
811 dim = isl_dim_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
815 dim = copy_ids(dim, isl_dim_param, 0, left, isl_dim_param);
816 dim = copy_ids(dim, isl_dim_in, 0, left, isl_dim_in);
817 dim = copy_ids(dim, isl_dim_out, 0, right, isl_dim_out);
819 if (dim && left->tuple_id[0] &&
820 !(dim->tuple_id[0] = isl_id_copy(left->tuple_id[0])))
822 if (dim && right->tuple_id[1] &&
823 !(dim->tuple_id[1] = isl_id_copy(right->tuple_id[1])))
825 if (dim && left->nested[0] &&
826 !(dim->nested[0] = isl_dim_copy(left->nested[0])))
828 if (dim && right->nested[1] &&
829 !(dim->nested[1] = isl_dim_copy(right->nested[1])))
842 struct isl_dim *isl_dim_product(struct isl_dim *left, struct isl_dim *right)
844 isl_dim *dom1, *dom2, *nest1, *nest2;
849 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
852 dom1 = isl_dim_domain(isl_dim_copy(left));
853 dom2 = isl_dim_domain(isl_dim_copy(right));
854 nest1 = isl_dim_wrap(isl_dim_join(isl_dim_reverse(dom1), dom2));
856 dom1 = isl_dim_range(left);
857 dom2 = isl_dim_range(right);
858 nest2 = isl_dim_wrap(isl_dim_join(isl_dim_reverse(dom1), dom2));
860 return isl_dim_join(isl_dim_reverse(nest1), nest2);
867 __isl_give isl_dim *isl_dim_range_product(__isl_take isl_dim *left,
868 __isl_take isl_dim *right)
870 isl_dim *dom, *ran1, *ran2, *nest;
875 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
877 if (!isl_dim_tuple_match(left, isl_dim_in, right, isl_dim_in))
878 isl_die(left->ctx, isl_error_invalid,
879 "domains need to match", goto error);
881 dom = isl_dim_domain(isl_dim_copy(left));
883 ran1 = isl_dim_range(left);
884 ran2 = isl_dim_range(right);
885 nest = isl_dim_wrap(isl_dim_join(isl_dim_reverse(ran1), ran2));
887 return isl_dim_join(isl_dim_reverse(dom), nest);
894 __isl_give isl_dim *isl_dim_map_from_set(__isl_take isl_dim *dim)
900 isl_assert(dim->ctx, dim->n_in == 0, goto error);
901 if (dim->n_out == 0 && !isl_dim_is_named_or_nested(dim, isl_dim_out))
903 dim = isl_dim_cow(dim);
907 ids = isl_calloc_array(dim->ctx, isl_id *,
908 dim->nparam + dim->n_out + dim->n_out);
911 get_ids(dim, isl_dim_param, 0, dim->nparam, ids);
912 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->nparam);
914 dim->n_in = dim->n_out;
918 dim->n_id = dim->nparam + dim->n_out + dim->n_out;
919 dim = copy_ids(dim, isl_dim_out, 0, dim, isl_dim_in);
921 isl_id_free(dim->tuple_id[0]);
922 dim->tuple_id[0] = isl_id_copy(dim->tuple_id[1]);
923 isl_dim_free(dim->nested[0]);
924 dim->nested[0] = isl_dim_copy(dim->nested[1]);
931 static __isl_give isl_dim *set_ids(struct isl_dim *dim, enum isl_dim_type type,
932 unsigned first, unsigned n, __isl_take isl_id **ids)
936 for (i = 0; i < n ; ++i)
937 dim = set_id(dim, type, first + i, ids[i]);
942 struct isl_dim *isl_dim_reverse(struct isl_dim *dim)
951 if (match(dim, isl_dim_in, dim, isl_dim_out))
954 dim = isl_dim_cow(dim);
958 id = dim->tuple_id[0];
959 dim->tuple_id[0] = dim->tuple_id[1];
960 dim->tuple_id[1] = id;
962 nested = dim->nested[0];
963 dim->nested[0] = dim->nested[1];
964 dim->nested[1] = nested;
967 ids = isl_alloc_array(dim->ctx, isl_id *,
968 dim->n_in + dim->n_out);
971 get_ids(dim, isl_dim_in, 0, dim->n_in, ids);
972 get_ids(dim, isl_dim_out, 0, dim->n_out, ids + dim->n_in);
976 dim->n_in = dim->n_out;
980 dim = set_ids(dim, isl_dim_out, 0, dim->n_out, ids);
981 dim = set_ids(dim, isl_dim_in, 0, dim->n_in, ids + dim->n_out);
992 struct isl_dim *isl_dim_drop(struct isl_dim *dim, enum isl_dim_type type,
993 unsigned first, unsigned num)
1001 return isl_dim_reset(dim, type);
1003 if (!valid_dim_type(type))
1004 isl_die(dim->ctx, isl_error_invalid,
1005 "cannot drop dimensions of specified type", goto error);
1007 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
1008 dim = isl_dim_cow(dim);
1012 dim = extend_ids(dim);
1015 for (i = 0; i < num; ++i)
1016 isl_id_free(get_id(dim, type, first + i));
1017 for (i = first+num; i < n(dim, type); ++i)
1018 set_id(dim, type, i - num, get_id(dim, type, i));
1021 get_ids(dim, isl_dim_in, 0, dim->n_in,
1022 dim->ids + offset(dim, isl_dim_in) - num);
1024 get_ids(dim, isl_dim_out, 0, dim->n_out,
1025 dim->ids + offset(dim, isl_dim_out) - num);
1032 case isl_dim_param: dim->nparam -= num; break;
1033 case isl_dim_in: dim->n_in -= num; break;
1034 case isl_dim_out: dim->n_out -= num; break;
1037 dim = isl_dim_reset(dim, type);
1038 if (type == isl_dim_param) {
1039 if (dim && dim->nested[0] &&
1040 !(dim->nested[0] = isl_dim_drop(dim->nested[0],
1041 isl_dim_param, first, num)))
1043 if (dim && dim->nested[1] &&
1044 !(dim->nested[1] = isl_dim_drop(dim->nested[1],
1045 isl_dim_param, first, num)))
1054 struct isl_dim *isl_dim_drop_inputs(struct isl_dim *dim,
1055 unsigned first, unsigned n)
1059 return isl_dim_drop(dim, isl_dim_in, first, n);
1062 struct isl_dim *isl_dim_drop_outputs(struct isl_dim *dim,
1063 unsigned first, unsigned n)
1067 return isl_dim_drop(dim, isl_dim_out, first, n);
1070 struct isl_dim *isl_dim_domain(struct isl_dim *dim)
1074 dim = isl_dim_drop_outputs(dim, 0, dim->n_out);
1075 return isl_dim_reverse(dim);
1078 __isl_give isl_dim *isl_dim_from_domain(__isl_take isl_dim *dim)
1080 return isl_dim_reverse(dim);
1083 struct isl_dim *isl_dim_range(struct isl_dim *dim)
1087 return isl_dim_drop_inputs(dim, 0, dim->n_in);
1090 __isl_give isl_dim *isl_dim_from_range(__isl_take isl_dim *dim)
1095 __isl_give isl_dim *isl_dim_as_set_dim(__isl_take isl_dim *dim)
1097 dim = isl_dim_cow(dim);
1101 dim->n_out += dim->n_in;
1103 dim = isl_dim_reset(dim, isl_dim_in);
1104 dim = isl_dim_reset(dim, isl_dim_out);
1109 struct isl_dim *isl_dim_underlying(struct isl_dim *dim, unsigned n_div)
1116 dim->nparam == 0 && dim->n_in == 0 && dim->n_id == 0)
1117 return isl_dim_reset(isl_dim_reset(dim, isl_dim_in), isl_dim_out);
1118 dim = isl_dim_cow(dim);
1121 dim->n_out += dim->nparam + dim->n_in + n_div;
1125 for (i = 0; i < dim->n_id; ++i)
1126 isl_id_free(get_id(dim, isl_dim_out, i));
1128 dim = isl_dim_reset(dim, isl_dim_in);
1129 dim = isl_dim_reset(dim, isl_dim_out);
1134 unsigned isl_dim_total(struct isl_dim *dim)
1136 return dim ? dim->nparam + dim->n_in + dim->n_out : 0;
1139 int isl_dim_equal(struct isl_dim *dim1, struct isl_dim *dim2)
1143 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1144 isl_dim_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1145 isl_dim_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1148 int isl_dim_compatible(struct isl_dim *dim1, struct isl_dim *dim2)
1150 return dim1->nparam == dim2->nparam &&
1151 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1154 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_dim *dim)
1162 hash = isl_hash_builtin(hash, dim->nparam);
1163 hash = isl_hash_builtin(hash, dim->n_in);
1164 hash = isl_hash_builtin(hash, dim->n_out);
1166 for (i = 0; i < dim->nparam; ++i) {
1167 id = get_id(dim, isl_dim_param, i);
1168 hash = isl_hash_id(hash, id);
1171 id = tuple_id(dim, isl_dim_in);
1172 hash = isl_hash_id(hash, id);
1173 id = tuple_id(dim, isl_dim_out);
1174 hash = isl_hash_id(hash, id);
1176 hash = isl_hash_dim(hash, dim->nested[0]);
1177 hash = isl_hash_dim(hash, dim->nested[1]);
1182 uint32_t isl_dim_get_hash(__isl_keep isl_dim *dim)
1189 hash = isl_hash_init();
1190 hash = isl_hash_dim(hash, dim);
1195 int isl_dim_is_wrapping(__isl_keep isl_dim *dim)
1200 if (dim->n_in != 0 || dim->tuple_id[0] || dim->nested[0])
1203 return dim->nested[1] != NULL;
1206 __isl_give isl_dim *isl_dim_wrap(__isl_take isl_dim *dim)
1213 wrap = isl_dim_alloc(dim->ctx, dim->nparam, 0, dim->n_in + dim->n_out);
1215 wrap = copy_ids(wrap, isl_dim_param, 0, dim, isl_dim_param);
1216 wrap = copy_ids(wrap, isl_dim_set, 0, dim, isl_dim_in);
1217 wrap = copy_ids(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1222 wrap->nested[1] = dim;
1230 __isl_give isl_dim *isl_dim_unwrap(__isl_take isl_dim *dim)
1237 if (!isl_dim_is_wrapping(dim))
1238 isl_die(dim->ctx, isl_error_invalid, "not a wrapping dim",
1241 unwrap = isl_dim_copy(dim->nested[1]);
1250 int isl_dim_is_named_or_nested(__isl_keep isl_dim *dim, enum isl_dim_type type)
1252 if (type != isl_dim_in && type != isl_dim_out)
1256 if (dim->tuple_id[type - isl_dim_in])
1258 if (dim->nested[type - isl_dim_in])
1263 int isl_dim_may_be_set(__isl_keep isl_dim *dim)
1267 if (isl_dim_size(dim, isl_dim_in) != 0)
1269 if (isl_dim_is_named_or_nested(dim, isl_dim_in))
1274 __isl_give isl_dim *isl_dim_reset(__isl_take isl_dim *dim,
1275 enum isl_dim_type type)
1277 if (!isl_dim_is_named_or_nested(dim, type))
1280 dim = isl_dim_cow(dim);
1284 isl_id_free(dim->tuple_id[type - isl_dim_in]);
1285 dim->tuple_id[type - isl_dim_in] = NULL;
1286 isl_dim_free(dim->nested[type - isl_dim_in]);
1287 dim->nested[type - isl_dim_in] = NULL;
1292 __isl_give isl_dim *isl_dim_flatten(__isl_take isl_dim *dim)
1296 if (!dim->nested[0] && !dim->nested[1])
1300 dim = isl_dim_reset(dim, isl_dim_in);
1301 if (dim && dim->nested[1])
1302 dim = isl_dim_reset(dim, isl_dim_out);
1307 __isl_give isl_dim *isl_dim_flatten_range(__isl_take isl_dim *dim)
1311 if (!dim->nested[1])
1314 return isl_dim_reset(dim, isl_dim_out);
1317 /* Replace the dimensions of the given type of dst by those of src.
1319 __isl_give isl_dim *isl_dim_replace(__isl_take isl_dim *dst,
1320 enum isl_dim_type type, __isl_keep isl_dim *src)
1322 dst = isl_dim_cow(dst);
1327 dst = isl_dim_drop(dst, type, 0, isl_dim_size(dst, type));
1328 dst = isl_dim_add(dst, type, isl_dim_size(src, type));
1329 dst = copy_ids(dst, type, 0, src, type);
1331 if (dst && type == isl_dim_param) {
1333 for (i = 0; i <= 1; ++i) {
1334 if (!dst->nested[i])
1336 dst->nested[i] = isl_dim_replace(dst->nested[i],
1338 if (!dst->nested[i])
1349 /* Given a dimension specification "dim" of a set, create a dimension
1350 * specification for the lift of the set. In particular, the result
1351 * is of the form [dim -> local[..]], with n_local variables in the
1352 * range of the wrapped map.
1354 __isl_give isl_dim *isl_dim_lift(__isl_take isl_dim *dim, unsigned n_local)
1361 local_dim = isl_dim_dup(dim);
1362 local_dim = isl_dim_drop(local_dim, isl_dim_set, 0, dim->n_out);
1363 local_dim = isl_dim_add(local_dim, isl_dim_set, n_local);
1364 local_dim = isl_dim_set_tuple_name(local_dim, isl_dim_set, "local");
1365 dim = isl_dim_join(isl_dim_from_domain(dim),
1366 isl_dim_from_range(local_dim));
1367 dim = isl_dim_wrap(dim);
1368 dim = isl_dim_set_tuple_name(dim, isl_dim_set, "lifted");
1373 int isl_dim_can_zip(__isl_keep isl_dim *dim)
1378 return dim->nested[0] && dim->nested[1];
1381 __isl_give isl_dim *isl_dim_zip(__isl_take isl_dim *dim)
1384 isl_dim *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1386 if (!isl_dim_can_zip(dim))
1387 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1392 dom = isl_dim_unwrap(isl_dim_domain(isl_dim_copy(dim)));
1393 ran = isl_dim_unwrap(isl_dim_range(dim));
1394 dom_dom = isl_dim_domain(isl_dim_copy(dom));
1395 dom_ran = isl_dim_range(dom);
1396 ran_dom = isl_dim_domain(isl_dim_copy(ran));
1397 ran_ran = isl_dim_range(ran);
1398 dom = isl_dim_join(isl_dim_from_domain(dom_dom),
1399 isl_dim_from_range(ran_dom));
1400 ran = isl_dim_join(isl_dim_from_domain(dom_ran),
1401 isl_dim_from_range(ran_ran));
1402 return isl_dim_join(isl_dim_from_domain(isl_dim_wrap(dom)),
1403 isl_dim_from_range(isl_dim_wrap(ran)));
1409 int isl_dim_has_named_params(__isl_keep isl_dim *dim)
1416 if (dim->nparam == 0)
1418 off = isl_dim_offset(dim, isl_dim_param);
1419 if (off + dim->nparam > dim->n_id)
1421 for (i = 0; i < dim->nparam; ++i)
1422 if (!dim->ids[off + i])
1427 /* Align the initial parameters of dim1 to match the order in dim2.
1429 __isl_give isl_dim *isl_dim_align_params(__isl_take isl_dim *dim1,
1430 __isl_take isl_dim *dim2)
1432 isl_reordering *exp;
1434 if (!isl_dim_has_named_params(dim1) || !isl_dim_has_named_params(dim2))
1435 isl_die(isl_dim_get_ctx(dim1), isl_error_invalid,
1436 "parameter alignment requires named parameters",
1439 exp = isl_parameter_alignment_reordering(dim1, dim2);
1444 dim1 = isl_dim_copy(exp->dim);
1445 isl_reordering_free(exp);