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>
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_name[0] = NULL;
40 dim->tuple_name[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 names array to the total number of dimensions.
80 static __isl_give isl_dim *extend_names(__isl_take isl_dim *dim)
82 struct isl_name **names;
85 if (isl_dim_total(dim) <= dim->n_name)
89 dim->names = isl_calloc_array(dim->ctx,
90 struct isl_name *, isl_dim_total(dim));
94 names = isl_realloc_array(dim->ctx, dim->names,
95 struct isl_name *, isl_dim_total(dim));
99 for (i = dim->n_name; i < isl_dim_total(dim); ++i)
100 dim->names[i] = NULL;
103 dim->n_name = isl_dim_total(dim);
111 static struct isl_dim *set_name(struct isl_dim *dim,
112 enum isl_dim_type type, unsigned pos,
113 struct isl_name *name)
115 struct isl_ctx *ctx = dim->ctx;
116 dim = isl_dim_cow(dim);
121 pos = global_pos(dim, type, pos);
122 if (pos == isl_dim_total(dim))
125 if (pos >= dim->n_name) {
128 dim = extend_names(dim);
133 dim->names[pos] = name;
137 isl_name_free(ctx, name);
142 static struct isl_name *get_name(struct isl_dim *dim,
143 enum isl_dim_type type, unsigned pos)
148 pos = global_pos(dim, type, pos);
149 if (pos == isl_dim_total(dim))
151 if (pos >= dim->n_name)
153 return dim->names[pos];
156 static unsigned offset(struct isl_dim *dim, enum isl_dim_type type)
159 case isl_dim_param: return 0;
160 case isl_dim_in: return dim->nparam;
161 case isl_dim_out: return dim->nparam + dim->n_in;
166 static unsigned n(struct isl_dim *dim, enum isl_dim_type type)
169 case isl_dim_param: return dim->nparam;
170 case isl_dim_in: return dim->n_in;
171 case isl_dim_out: return dim->n_out;
172 case isl_dim_all: return dim->nparam + dim->n_in + dim->n_out;
177 unsigned isl_dim_size(struct isl_dim *dim, enum isl_dim_type type)
184 unsigned isl_dim_offset(__isl_keep isl_dim *dim, enum isl_dim_type type)
188 return offset(dim, type);
191 static struct isl_dim *copy_names(struct isl_dim *dst,
192 enum isl_dim_type dst_type, unsigned offset, struct isl_dim *src,
193 enum isl_dim_type src_type)
196 struct isl_name *name;
201 for (i = 0; i < n(src, src_type); ++i) {
202 name = get_name(src, src_type, i);
205 dst = set_name(dst, dst_type, offset + i,
206 isl_name_copy(dst->ctx, name));
213 struct isl_dim *isl_dim_dup(struct isl_dim *dim)
218 dup = isl_dim_alloc(dim->ctx, dim->nparam, dim->n_in, dim->n_out);
219 if (dim->tuple_name[0] &&
220 !(dup->tuple_name[0] = isl_name_copy(dim->ctx, dim->tuple_name[0])))
222 if (dim->tuple_name[1] &&
223 !(dup->tuple_name[1] = isl_name_copy(dim->ctx, dim->tuple_name[1])))
225 if (dim->nested[0] && !(dup->nested[0] = isl_dim_copy(dim->nested[0])))
227 if (dim->nested[1] && !(dup->nested[1] = isl_dim_copy(dim->nested[1])))
231 dup = copy_names(dup, isl_dim_param, 0, dim, isl_dim_param);
232 dup = copy_names(dup, isl_dim_in, 0, dim, isl_dim_in);
233 dup = copy_names(dup, isl_dim_out, 0, dim, isl_dim_out);
240 struct isl_dim *isl_dim_cow(struct isl_dim *dim)
248 return isl_dim_dup(dim);
251 struct isl_dim *isl_dim_copy(struct isl_dim *dim)
260 void isl_dim_free(struct isl_dim *dim)
270 isl_name_free(dim->ctx, dim->tuple_name[0]);
271 isl_name_free(dim->ctx, dim->tuple_name[1]);
273 isl_dim_free(dim->nested[0]);
274 isl_dim_free(dim->nested[1]);
276 for (i = 0; i < dim->n_name; ++i)
277 isl_name_free(dim->ctx, dim->names[i]);
279 isl_ctx_deref(dim->ctx);
284 static int name_ok(isl_ctx *ctx, const char *s)
289 dummy = strtol(s, &p, 0);
291 isl_die(ctx, isl_error_invalid, "name looks like a number",
297 __isl_give isl_dim *isl_dim_set_tuple_name(__isl_take isl_dim *dim,
298 enum isl_dim_type type, const char *s)
300 struct isl_name *name;
302 dim = isl_dim_cow(dim);
305 if (type != isl_dim_in && type != isl_dim_out)
306 isl_die(dim->ctx, isl_error_invalid,
307 "only input, output and set tuples can have names",
312 if (!name_ok(dim->ctx, s))
314 name = isl_name_get(dim->ctx, s);
319 isl_name_free(dim->ctx, dim->tuple_name[type - isl_dim_in]);
320 dim->tuple_name[type - isl_dim_in] = name;
328 const char *isl_dim_get_tuple_name(__isl_keep isl_dim *dim,
329 enum isl_dim_type type)
331 struct isl_name *name;
334 if (type != isl_dim_in && type != isl_dim_out)
336 name = dim->tuple_name[type - isl_dim_in];
337 return name ? name->name : NULL;
340 struct isl_dim *isl_dim_set_name(struct isl_dim *dim,
341 enum isl_dim_type type, unsigned pos,
344 struct isl_name *name;
348 if (!name_ok(dim->ctx, s))
350 isl_name_free(dim->ctx, get_name(dim, type, pos));
351 name = isl_name_get(dim->ctx, s);
354 return set_name(dim, type, pos, name);
360 const char *isl_dim_get_name(struct isl_dim *dim,
361 enum isl_dim_type type, unsigned pos)
363 struct isl_name *name = get_name(dim, type, pos);
364 return name ? name->name : NULL;
367 static struct isl_name *tuple_name(__isl_keep isl_dim *dim,
368 enum isl_dim_type type)
372 if (type == isl_dim_in)
373 return dim->tuple_name[0];
374 if (type == isl_dim_out)
375 return dim->tuple_name[1];
379 static __isl_keep isl_dim *nested(__isl_keep isl_dim *dim,
380 enum isl_dim_type type)
384 if (type == isl_dim_in)
385 return dim->nested[0];
386 if (type == isl_dim_out)
387 return dim->nested[1];
391 int isl_dim_tuple_match(__isl_keep isl_dim *dim1, enum isl_dim_type dim1_type,
392 __isl_keep isl_dim *dim2, enum isl_dim_type dim2_type)
394 struct isl_name *name1, *name2;
395 isl_dim *nested1, *nested2;
400 if (n(dim1, dim1_type) != n(dim2, dim2_type))
402 name1 = tuple_name(dim1, dim1_type);
403 name2 = tuple_name(dim2, dim2_type);
406 if (name1 && name1->name != name2->name)
408 nested1 = nested(dim1, dim1_type);
409 nested2 = nested(dim2, dim2_type);
410 if (!nested1 ^ !nested2)
412 if (nested1 && !isl_dim_equal(nested1, nested2))
417 static int match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
418 struct isl_dim *dim2, enum isl_dim_type dim2_type)
422 if (!isl_dim_tuple_match(dim1, dim1_type, dim2, dim2_type))
425 if (!dim1->names && !dim2->names)
428 for (i = 0; i < n(dim1, dim1_type); ++i) {
429 if (get_name(dim1, dim1_type, i) !=
430 get_name(dim2, dim2_type, i))
436 int isl_dim_match(struct isl_dim *dim1, enum isl_dim_type dim1_type,
437 struct isl_dim *dim2, enum isl_dim_type dim2_type)
439 return match(dim1, dim1_type, dim2, dim2_type);
442 static void get_names(struct isl_dim *dim, enum isl_dim_type type,
443 unsigned first, unsigned n, struct isl_name **names)
447 for (i = 0; i < n ; ++i)
448 names[i] = get_name(dim, type, first+i);
451 struct isl_dim *isl_dim_extend(struct isl_dim *dim,
452 unsigned nparam, unsigned n_in, unsigned n_out)
454 struct isl_name **names = NULL;
458 if (dim->nparam == nparam && dim->n_in == n_in && dim->n_out == n_out)
461 isl_assert(dim->ctx, dim->nparam <= nparam, goto error);
462 isl_assert(dim->ctx, dim->n_in <= n_in, goto error);
463 isl_assert(dim->ctx, dim->n_out <= n_out, goto error);
465 dim = isl_dim_cow(dim);
468 names = isl_calloc_array(dim->ctx, struct isl_name *,
469 nparam + n_in + n_out);
472 get_names(dim, isl_dim_param, 0, dim->nparam, names);
473 get_names(dim, isl_dim_in, 0, dim->n_in, names + nparam);
474 get_names(dim, isl_dim_out, 0, dim->n_out,
475 names + nparam + n_in);
478 dim->n_name = nparam + n_in + n_out;
480 dim->nparam = nparam;
491 struct isl_dim *isl_dim_add(struct isl_dim *dim, enum isl_dim_type type,
496 dim = isl_dim_reset(dim, type);
499 dim = isl_dim_extend(dim,
500 dim->nparam + n, dim->n_in, dim->n_out);
501 if (dim && dim->nested[0] &&
502 !(dim->nested[0] = isl_dim_add(dim->nested[0],
505 if (dim && dim->nested[1] &&
506 !(dim->nested[1] = isl_dim_add(dim->nested[1],
511 return isl_dim_extend(dim,
512 dim->nparam, dim->n_in + n, dim->n_out);
514 return isl_dim_extend(dim,
515 dim->nparam, dim->n_in, dim->n_out + n);
517 isl_die(dim->ctx, isl_error_invalid,
518 "cannot add dimensions of specified type", goto error);
525 static int valid_dim_type(enum isl_dim_type type)
537 __isl_give isl_dim *isl_dim_insert(__isl_take isl_dim *dim,
538 enum isl_dim_type type, unsigned pos, unsigned n)
540 struct isl_name **names = NULL;
545 return isl_dim_reset(dim, type);
547 if (!valid_dim_type(type))
548 isl_die(dim->ctx, isl_error_invalid,
549 "cannot insert dimensions of specified type",
552 isl_assert(dim->ctx, pos <= isl_dim_size(dim, type), goto error);
554 dim = isl_dim_cow(dim);
562 int *size = s - isl_dim_param;
563 names = isl_calloc_array(dim->ctx, struct isl_name *,
564 dim->nparam + dim->n_in + dim->n_out + n);
568 size[isl_dim_param] = dim->nparam;
569 size[isl_dim_in] = dim->n_in;
570 size[isl_dim_out] = dim->n_out;
571 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
573 get_names(dim, t, 0, size[t], names + off);
576 get_names(dim, t, 0, pos, names + off);
578 get_names(dim, t, pos, size[t]-pos, names+off);
579 off += size[t] - pos;
584 dim->n_name = dim->nparam + dim->n_in + dim->n_out + n;
587 case isl_dim_param: dim->nparam += n; break;
588 case isl_dim_in: dim->n_in += n; break;
589 case isl_dim_out: dim->n_out += n; break;
592 dim = isl_dim_reset(dim, type);
600 __isl_give isl_dim *isl_dim_move(__isl_take isl_dim *dim,
601 enum isl_dim_type dst_type, unsigned dst_pos,
602 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
611 isl_assert(dim->ctx, src_pos + n <= isl_dim_size(dim, src_type),
614 if (dst_type == src_type && dst_pos == src_pos)
617 isl_assert(dim->ctx, dst_type != src_type, goto error);
619 dim = isl_dim_reset(dim, src_type);
620 dim = isl_dim_reset(dim, dst_type);
622 dim = isl_dim_cow(dim);
627 struct isl_name **names;
631 int *size = s - isl_dim_param;
632 names = isl_calloc_array(dim->ctx, struct isl_name *,
633 dim->nparam + dim->n_in + dim->n_out);
637 size[isl_dim_param] = dim->nparam;
638 size[isl_dim_in] = dim->n_in;
639 size[isl_dim_out] = dim->n_out;
640 for (t = isl_dim_param; t <= isl_dim_out; ++t) {
642 get_names(dim, t, 0, dst_pos, names + off);
644 get_names(dim, src_type, src_pos, n, names+off);
646 get_names(dim, t, dst_pos, size[t] - dst_pos,
648 off += size[t] - dst_pos;
649 } else if (t == src_type) {
650 get_names(dim, t, 0, src_pos, names + off);
652 get_names(dim, t, src_pos + n,
653 size[t] - src_pos - n, names + off);
654 off += size[t] - src_pos - n;
656 get_names(dim, t, 0, size[t], names + off);
662 dim->n_name = dim->nparam + dim->n_in + dim->n_out;
666 case isl_dim_param: dim->nparam += n; break;
667 case isl_dim_in: dim->n_in += n; break;
668 case isl_dim_out: dim->n_out += n; break;
673 case isl_dim_param: dim->nparam -= n; break;
674 case isl_dim_in: dim->n_in -= n; break;
675 case isl_dim_out: dim->n_out -= n; break;
679 if (dst_type != isl_dim_param && src_type != isl_dim_param)
682 for (i = 0; i < 2; ++i) {
685 dim->nested[i] = isl_dim_replace(dim->nested[i],
697 struct isl_dim *isl_dim_join(struct isl_dim *left, struct isl_dim *right)
704 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
706 isl_assert(left->ctx,
707 isl_dim_tuple_match(left, isl_dim_out, right, isl_dim_in),
710 dim = isl_dim_alloc(left->ctx, left->nparam, left->n_in, right->n_out);
714 dim = copy_names(dim, isl_dim_param, 0, left, isl_dim_param);
715 dim = copy_names(dim, isl_dim_in, 0, left, isl_dim_in);
716 dim = copy_names(dim, isl_dim_out, 0, right, isl_dim_out);
718 if (dim && left->tuple_name[0] &&
719 !(dim->tuple_name[0] = isl_name_copy(dim->ctx, left->tuple_name[0])))
721 if (dim && right->tuple_name[1] &&
722 !(dim->tuple_name[1] = isl_name_copy(dim->ctx, right->tuple_name[1])))
724 if (dim && left->nested[0] &&
725 !(dim->nested[0] = isl_dim_copy(left->nested[0])))
727 if (dim && right->nested[1] &&
728 !(dim->nested[1] = isl_dim_copy(right->nested[1])))
741 struct isl_dim *isl_dim_product(struct isl_dim *left, struct isl_dim *right)
743 isl_dim *dom1, *dom2, *nest1, *nest2;
748 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
751 dom1 = isl_dim_domain(isl_dim_copy(left));
752 dom2 = isl_dim_domain(isl_dim_copy(right));
753 nest1 = isl_dim_wrap(isl_dim_join(isl_dim_reverse(dom1), dom2));
755 dom1 = isl_dim_range(left);
756 dom2 = isl_dim_range(right);
757 nest2 = isl_dim_wrap(isl_dim_join(isl_dim_reverse(dom1), dom2));
759 return isl_dim_join(isl_dim_reverse(nest1), nest2);
766 __isl_give isl_dim *isl_dim_range_product(__isl_take isl_dim *left,
767 __isl_take isl_dim *right)
769 isl_dim *dom, *ran1, *ran2, *nest;
774 isl_assert(left->ctx, match(left, isl_dim_param, right, isl_dim_param),
776 if (!isl_dim_tuple_match(left, isl_dim_in, right, isl_dim_in))
777 isl_die(left->ctx, isl_error_invalid,
778 "domains need to match", goto error);
780 dom = isl_dim_domain(isl_dim_copy(left));
782 ran1 = isl_dim_range(left);
783 ran2 = isl_dim_range(right);
784 nest = isl_dim_wrap(isl_dim_join(isl_dim_reverse(ran1), ran2));
786 return isl_dim_join(isl_dim_reverse(dom), nest);
793 __isl_give isl_dim *isl_dim_map_from_set(__isl_take isl_dim *dim)
795 struct isl_name **names = NULL;
799 isl_assert(dim->ctx, dim->n_in == 0, goto error);
800 if (dim->n_out == 0 && !isl_dim_is_named_or_nested(dim, isl_dim_out))
802 dim = isl_dim_cow(dim);
806 names = isl_calloc_array(dim->ctx, struct isl_name *,
807 dim->nparam + dim->n_out + dim->n_out);
810 get_names(dim, isl_dim_param, 0, dim->nparam, names);
811 get_names(dim, isl_dim_out, 0, dim->n_out, names + dim->nparam);
813 dim->n_in = dim->n_out;
817 dim->n_name = dim->nparam + dim->n_out + dim->n_out;
818 dim = copy_names(dim, isl_dim_out, 0, dim, isl_dim_in);
820 isl_name_free(dim->ctx, dim->tuple_name[0]);
821 dim->tuple_name[0] = isl_name_copy(dim->ctx, dim->tuple_name[1]);
822 isl_dim_free(dim->nested[0]);
823 dim->nested[0] = isl_dim_copy(dim->nested[1]);
830 static struct isl_dim *set_names(struct isl_dim *dim, enum isl_dim_type type,
831 unsigned first, unsigned n, struct isl_name **names)
835 for (i = 0; i < n ; ++i)
836 dim = set_name(dim, type, first+i, names[i]);
841 struct isl_dim *isl_dim_reverse(struct isl_dim *dim)
845 struct isl_name **names = NULL;
846 struct isl_name *name;
850 if (match(dim, isl_dim_in, dim, isl_dim_out))
853 dim = isl_dim_cow(dim);
857 name = dim->tuple_name[0];
858 dim->tuple_name[0] = dim->tuple_name[1];
859 dim->tuple_name[1] = name;
861 nested = dim->nested[0];
862 dim->nested[0] = dim->nested[1];
863 dim->nested[1] = nested;
866 names = isl_alloc_array(dim->ctx, struct isl_name *,
867 dim->n_in + dim->n_out);
870 get_names(dim, isl_dim_in, 0, dim->n_in, names);
871 get_names(dim, isl_dim_out, 0, dim->n_out, names + dim->n_in);
875 dim->n_in = dim->n_out;
879 dim = set_names(dim, isl_dim_out, 0, dim->n_out, names);
880 dim = set_names(dim, isl_dim_in, 0, dim->n_in, names + dim->n_out);
891 struct isl_dim *isl_dim_drop(struct isl_dim *dim, enum isl_dim_type type,
892 unsigned first, unsigned num)
900 return isl_dim_reset(dim, type);
902 if (!valid_dim_type(type))
903 isl_die(dim->ctx, isl_error_invalid,
904 "cannot drop dimensions of specified type", goto error);
906 isl_assert(dim->ctx, first + num <= n(dim, type), goto error);
907 dim = isl_dim_cow(dim);
911 dim = extend_names(dim);
914 for (i = 0; i < num; ++i)
915 isl_name_free(dim->ctx, get_name(dim, type, first+i));
916 for (i = first+num; i < n(dim, type); ++i)
917 set_name(dim, type, i - num, get_name(dim, type, i));
920 get_names(dim, isl_dim_in, 0, dim->n_in,
921 dim->names + offset(dim, isl_dim_in) - num);
923 get_names(dim, isl_dim_out, 0, dim->n_out,
924 dim->names + offset(dim, isl_dim_out) - num);
931 case isl_dim_param: dim->nparam -= num; break;
932 case isl_dim_in: dim->n_in -= num; break;
933 case isl_dim_out: dim->n_out -= num; break;
936 dim = isl_dim_reset(dim, type);
937 if (type == isl_dim_param) {
938 if (dim && dim->nested[0] &&
939 !(dim->nested[0] = isl_dim_drop(dim->nested[0],
940 isl_dim_param, first, num)))
942 if (dim && dim->nested[1] &&
943 !(dim->nested[1] = isl_dim_drop(dim->nested[1],
944 isl_dim_param, first, num)))
953 struct isl_dim *isl_dim_drop_inputs(struct isl_dim *dim,
954 unsigned first, unsigned n)
958 return isl_dim_drop(dim, isl_dim_in, first, n);
961 struct isl_dim *isl_dim_drop_outputs(struct isl_dim *dim,
962 unsigned first, unsigned n)
966 return isl_dim_drop(dim, isl_dim_out, first, n);
969 struct isl_dim *isl_dim_domain(struct isl_dim *dim)
973 dim = isl_dim_drop_outputs(dim, 0, dim->n_out);
974 return isl_dim_reverse(dim);
977 __isl_give isl_dim *isl_dim_from_domain(__isl_take isl_dim *dim)
979 return isl_dim_reverse(dim);
982 struct isl_dim *isl_dim_range(struct isl_dim *dim)
986 return isl_dim_drop_inputs(dim, 0, dim->n_in);
989 __isl_give isl_dim *isl_dim_from_range(__isl_take isl_dim *dim)
994 __isl_give isl_dim *isl_dim_as_set_dim(__isl_take isl_dim *dim)
996 dim = isl_dim_cow(dim);
1000 dim->n_out += dim->n_in;
1002 dim = isl_dim_reset(dim, isl_dim_in);
1003 dim = isl_dim_reset(dim, isl_dim_out);
1008 struct isl_dim *isl_dim_underlying(struct isl_dim *dim, unsigned n_div)
1015 dim->nparam == 0 && dim->n_in == 0 && dim->n_name == 0)
1016 return isl_dim_reset(isl_dim_reset(dim, isl_dim_in), isl_dim_out);
1017 dim = isl_dim_cow(dim);
1020 dim->n_out += dim->nparam + dim->n_in + n_div;
1024 for (i = 0; i < dim->n_name; ++i)
1025 isl_name_free(dim->ctx, get_name(dim, isl_dim_out, i));
1027 dim = isl_dim_reset(dim, isl_dim_in);
1028 dim = isl_dim_reset(dim, isl_dim_out);
1033 unsigned isl_dim_total(struct isl_dim *dim)
1035 return dim ? dim->nparam + dim->n_in + dim->n_out : 0;
1038 int isl_dim_equal(struct isl_dim *dim1, struct isl_dim *dim2)
1042 return match(dim1, isl_dim_param, dim2, isl_dim_param) &&
1043 isl_dim_tuple_match(dim1, isl_dim_in, dim2, isl_dim_in) &&
1044 isl_dim_tuple_match(dim1, isl_dim_out, dim2, isl_dim_out);
1047 int isl_dim_compatible(struct isl_dim *dim1, struct isl_dim *dim2)
1049 return dim1->nparam == dim2->nparam &&
1050 dim1->n_in + dim1->n_out == dim2->n_in + dim2->n_out;
1053 static uint32_t isl_hash_dim(uint32_t hash, __isl_keep isl_dim *dim)
1056 struct isl_name *name;
1061 hash = isl_hash_builtin(hash, dim->nparam);
1062 hash = isl_hash_builtin(hash, dim->n_in);
1063 hash = isl_hash_builtin(hash, dim->n_out);
1065 for (i = 0; i < dim->nparam; ++i) {
1066 name = get_name(dim, isl_dim_param, i);
1067 hash = isl_hash_name(hash, name);
1070 name = tuple_name(dim, isl_dim_in);
1071 hash = isl_hash_name(hash, name);
1072 name = tuple_name(dim, isl_dim_out);
1073 hash = isl_hash_name(hash, name);
1075 hash = isl_hash_dim(hash, dim->nested[0]);
1076 hash = isl_hash_dim(hash, dim->nested[1]);
1081 uint32_t isl_dim_get_hash(__isl_keep isl_dim *dim)
1088 hash = isl_hash_init();
1089 hash = isl_hash_dim(hash, dim);
1094 int isl_dim_is_wrapping(__isl_keep isl_dim *dim)
1099 if (dim->n_in != 0 || dim->tuple_name[0] || dim->nested[0])
1102 return dim->nested[1] != NULL;
1105 __isl_give isl_dim *isl_dim_wrap(__isl_take isl_dim *dim)
1112 wrap = isl_dim_alloc(dim->ctx, dim->nparam, 0, dim->n_in + dim->n_out);
1114 wrap = copy_names(wrap, isl_dim_param, 0, dim, isl_dim_param);
1115 wrap = copy_names(wrap, isl_dim_set, 0, dim, isl_dim_in);
1116 wrap = copy_names(wrap, isl_dim_set, dim->n_in, dim, isl_dim_out);
1121 wrap->nested[1] = dim;
1129 __isl_give isl_dim *isl_dim_unwrap(__isl_take isl_dim *dim)
1136 if (!isl_dim_is_wrapping(dim))
1137 isl_die(dim->ctx, isl_error_invalid, "not a wrapping dim",
1140 unwrap = isl_dim_copy(dim->nested[1]);
1149 int isl_dim_is_named_or_nested(__isl_keep isl_dim *dim, enum isl_dim_type type)
1151 if (type != isl_dim_in && type != isl_dim_out)
1155 if (dim->tuple_name[type - isl_dim_in])
1157 if (dim->nested[type - isl_dim_in])
1162 int isl_dim_may_be_set(__isl_keep isl_dim *dim)
1166 if (isl_dim_size(dim, isl_dim_in) != 0)
1168 if (isl_dim_is_named_or_nested(dim, isl_dim_in))
1173 __isl_give isl_dim *isl_dim_reset(__isl_take isl_dim *dim,
1174 enum isl_dim_type type)
1176 if (!isl_dim_is_named_or_nested(dim, type))
1179 dim = isl_dim_cow(dim);
1183 isl_name_free(dim->ctx, dim->tuple_name[type - isl_dim_in]);
1184 dim->tuple_name[type - isl_dim_in] = NULL;
1185 isl_dim_free(dim->nested[type - isl_dim_in]);
1186 dim->nested[type - isl_dim_in] = NULL;
1191 __isl_give isl_dim *isl_dim_flatten(__isl_take isl_dim *dim)
1195 if (!dim->nested[0] && !dim->nested[1])
1199 dim = isl_dim_reset(dim, isl_dim_in);
1200 if (dim && dim->nested[1])
1201 dim = isl_dim_reset(dim, isl_dim_out);
1206 __isl_give isl_dim *isl_dim_flatten_range(__isl_take isl_dim *dim)
1210 if (!dim->nested[1])
1213 return isl_dim_reset(dim, isl_dim_out);
1216 /* Replace the dimensions of the given type of dst by those of src.
1218 __isl_give isl_dim *isl_dim_replace(__isl_take isl_dim *dst,
1219 enum isl_dim_type type, __isl_keep isl_dim *src)
1221 dst = isl_dim_cow(dst);
1226 dst = isl_dim_drop(dst, type, 0, isl_dim_size(dst, type));
1227 dst = isl_dim_add(dst, type, isl_dim_size(src, type));
1228 dst = copy_names(dst, type, 0, src, type);
1230 if (dst && type == isl_dim_param) {
1232 for (i = 0; i <= 1; ++i) {
1233 if (!dst->nested[i])
1235 dst->nested[i] = isl_dim_replace(dst->nested[i],
1237 if (!dst->nested[i])
1248 /* Given a dimension specification "dim" of a set, create a dimension
1249 * specification for the lift of the set. In particular, the result
1250 * is of the form [dim -> local[..]], with n_local variables in the
1251 * range of the wrapped map.
1253 __isl_give isl_dim *isl_dim_lift(__isl_take isl_dim *dim, unsigned n_local)
1260 local_dim = isl_dim_dup(dim);
1261 local_dim = isl_dim_drop(local_dim, isl_dim_set, 0, dim->n_out);
1262 local_dim = isl_dim_add(local_dim, isl_dim_set, n_local);
1263 local_dim = isl_dim_set_tuple_name(local_dim, isl_dim_set, "local");
1264 dim = isl_dim_join(isl_dim_from_domain(dim),
1265 isl_dim_from_range(local_dim));
1266 dim = isl_dim_wrap(dim);
1267 dim = isl_dim_set_tuple_name(dim, isl_dim_set, "lifted");
1272 int isl_dim_can_zip(__isl_keep isl_dim *dim)
1277 return dim->nested[0] && dim->nested[1];
1280 __isl_give isl_dim *isl_dim_zip(__isl_take isl_dim *dim)
1283 isl_dim *dom_dom, *dom_ran, *ran_dom, *ran_ran;
1285 if (!isl_dim_can_zip(dim))
1286 isl_die(dim->ctx, isl_error_invalid, "dim cannot be zipped",
1291 dom = isl_dim_unwrap(isl_dim_domain(isl_dim_copy(dim)));
1292 ran = isl_dim_unwrap(isl_dim_range(dim));
1293 dom_dom = isl_dim_domain(isl_dim_copy(dom));
1294 dom_ran = isl_dim_range(dom);
1295 ran_dom = isl_dim_domain(isl_dim_copy(ran));
1296 ran_ran = isl_dim_range(ran);
1297 dom = isl_dim_join(isl_dim_from_domain(dom_dom),
1298 isl_dim_from_range(ran_dom));
1299 ran = isl_dim_join(isl_dim_from_domain(dom_ran),
1300 isl_dim_from_range(ran_ran));
1301 return isl_dim_join(isl_dim_from_domain(isl_dim_wrap(dom)),
1302 isl_dim_from_range(isl_dim_wrap(ran)));
1308 int isl_dim_has_named_params(__isl_keep isl_dim *dim)
1315 if (dim->nparam == 0)
1317 off = isl_dim_offset(dim, isl_dim_param);
1318 if (off + dim->nparam > dim->n_name)
1320 for (i = 0; i < dim->nparam; ++i)
1321 if (!dim->names[off + i])
1326 /* Align the initial parameters of dim1 to match the order in dim2.
1328 __isl_give isl_dim *isl_dim_align_params(__isl_take isl_dim *dim1,
1329 __isl_take isl_dim *dim2)
1331 isl_reordering *exp;
1333 if (!isl_dim_has_named_params(dim1) || !isl_dim_has_named_params(dim2))
1334 isl_die(isl_dim_get_ctx(dim1), isl_error_invalid,
1335 "parameter alignment requires named parameters",
1338 exp = isl_parameter_alignment_reordering(dim1, dim2);
1343 dim1 = isl_dim_copy(exp->dim);
1344 isl_reordering_free(exp);