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>
17 isl_ctx
*isl_dim_get_ctx(__isl_keep isl_dim
*dim
)
19 return dim
? dim
->ctx
: NULL
;
22 struct isl_dim
*isl_dim_alloc(struct isl_ctx
*ctx
,
23 unsigned nparam
, unsigned n_in
, unsigned n_out
)
27 dim
= isl_alloc_type(ctx
, struct isl_dim
);
38 dim
->tuple_name
[0] = NULL
;
39 dim
->tuple_name
[1] = NULL
;
41 dim
->nested
[0] = NULL
;
42 dim
->nested
[1] = NULL
;
50 struct isl_dim
*isl_dim_set_alloc(struct isl_ctx
*ctx
,
51 unsigned nparam
, unsigned dim
)
53 return isl_dim_alloc(ctx
, nparam
, 0, dim
);
56 static unsigned global_pos(struct isl_dim
*dim
,
57 enum isl_dim_type type
, unsigned pos
)
59 struct isl_ctx
*ctx
= dim
->ctx
;
63 isl_assert(ctx
, pos
< dim
->nparam
, return isl_dim_total(dim
));
66 isl_assert(ctx
, pos
< dim
->n_in
, return isl_dim_total(dim
));
67 return pos
+ dim
->nparam
;
69 isl_assert(ctx
, pos
< dim
->n_out
, return isl_dim_total(dim
));
70 return pos
+ dim
->nparam
+ dim
->n_in
;
72 isl_assert(ctx
, 0, return isl_dim_total(dim
));
74 return isl_dim_total(dim
);
77 /* Extend length of names array to the total number of dimensions.
79 static __isl_give isl_dim
*extend_names(__isl_take isl_dim
*dim
)
81 struct isl_name
**names
;
84 if (isl_dim_total(dim
) <= dim
->n_name
)
88 dim
->names
= isl_calloc_array(dim
->ctx
,
89 struct isl_name
*, isl_dim_total(dim
));
93 names
= isl_realloc_array(dim
->ctx
, dim
->names
,
94 struct isl_name
*, isl_dim_total(dim
));
98 for (i
= dim
->n_name
; i
< isl_dim_total(dim
); ++i
)
102 dim
->n_name
= isl_dim_total(dim
);
110 static struct isl_dim
*set_name(struct isl_dim
*dim
,
111 enum isl_dim_type type
, unsigned pos
,
112 struct isl_name
*name
)
114 struct isl_ctx
*ctx
= dim
->ctx
;
115 dim
= isl_dim_cow(dim
);
120 pos
= global_pos(dim
, type
, pos
);
121 if (pos
== isl_dim_total(dim
))
124 if (pos
>= dim
->n_name
) {
127 dim
= extend_names(dim
);
132 dim
->names
[pos
] = name
;
136 isl_name_free(ctx
, name
);
141 static struct isl_name
*get_name(struct isl_dim
*dim
,
142 enum isl_dim_type type
, unsigned pos
)
147 pos
= global_pos(dim
, type
, pos
);
148 if (pos
== isl_dim_total(dim
))
150 if (pos
>= dim
->n_name
)
152 return dim
->names
[pos
];
155 static unsigned offset(struct isl_dim
*dim
, enum isl_dim_type type
)
158 case isl_dim_param
: return 0;
159 case isl_dim_in
: return dim
->nparam
;
160 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
165 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
168 case isl_dim_param
: return dim
->nparam
;
169 case isl_dim_in
: return dim
->n_in
;
170 case isl_dim_out
: return dim
->n_out
;
171 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
176 unsigned isl_dim_size(struct isl_dim
*dim
, enum isl_dim_type type
)
183 unsigned isl_dim_offset(__isl_keep isl_dim
*dim
, enum isl_dim_type type
)
187 return offset(dim
, type
);
190 static struct isl_dim
*copy_names(struct isl_dim
*dst
,
191 enum isl_dim_type dst_type
, unsigned offset
, struct isl_dim
*src
,
192 enum isl_dim_type src_type
)
195 struct isl_name
*name
;
200 for (i
= 0; i
< n(src
, src_type
); ++i
) {
201 name
= get_name(src
, src_type
, i
);
204 dst
= set_name(dst
, dst_type
, offset
+ i
,
205 isl_name_copy(dst
->ctx
, name
));
212 struct isl_dim
*isl_dim_dup(struct isl_dim
*dim
)
217 dup
= isl_dim_alloc(dim
->ctx
, dim
->nparam
, dim
->n_in
, dim
->n_out
);
218 if (dim
->tuple_name
[0] &&
219 !(dup
->tuple_name
[0] = isl_name_copy(dim
->ctx
, dim
->tuple_name
[0])))
221 if (dim
->tuple_name
[1] &&
222 !(dup
->tuple_name
[1] = isl_name_copy(dim
->ctx
, dim
->tuple_name
[1])))
224 if (dim
->nested
[0] && !(dup
->nested
[0] = isl_dim_copy(dim
->nested
[0])))
226 if (dim
->nested
[1] && !(dup
->nested
[1] = isl_dim_copy(dim
->nested
[1])))
230 dup
= copy_names(dup
, isl_dim_param
, 0, dim
, isl_dim_param
);
231 dup
= copy_names(dup
, isl_dim_in
, 0, dim
, isl_dim_in
);
232 dup
= copy_names(dup
, isl_dim_out
, 0, dim
, isl_dim_out
);
239 struct isl_dim
*isl_dim_cow(struct isl_dim
*dim
)
247 return isl_dim_dup(dim
);
250 struct isl_dim
*isl_dim_copy(struct isl_dim
*dim
)
259 void isl_dim_free(struct isl_dim
*dim
)
269 isl_name_free(dim
->ctx
, dim
->tuple_name
[0]);
270 isl_name_free(dim
->ctx
, dim
->tuple_name
[1]);
272 isl_dim_free(dim
->nested
[0]);
273 isl_dim_free(dim
->nested
[1]);
275 for (i
= 0; i
< dim
->n_name
; ++i
)
276 isl_name_free(dim
->ctx
, dim
->names
[i
]);
278 isl_ctx_deref(dim
->ctx
);
283 static int name_ok(isl_ctx
*ctx
, const char *s
)
288 dummy
= strtol(s
, &p
, 0);
290 isl_die(ctx
, isl_error_invalid
, "name looks like a number",
296 __isl_give isl_dim
*isl_dim_set_tuple_name(__isl_take isl_dim
*dim
,
297 enum isl_dim_type type
, const char *s
)
299 struct isl_name
*name
;
301 dim
= isl_dim_cow(dim
);
304 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
305 isl_die(dim
->ctx
, isl_error_invalid
,
306 "only input, output and set tuples can have names",
311 if (!name_ok(dim
->ctx
, s
))
313 name
= isl_name_get(dim
->ctx
, s
);
318 isl_name_free(dim
->ctx
, dim
->tuple_name
[type
- isl_dim_in
]);
319 dim
->tuple_name
[type
- isl_dim_in
] = name
;
327 const char *isl_dim_get_tuple_name(__isl_keep isl_dim
*dim
,
328 enum isl_dim_type type
)
330 struct isl_name
*name
;
333 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
335 name
= dim
->tuple_name
[type
- isl_dim_in
];
336 return name
? name
->name
: NULL
;
339 struct isl_dim
*isl_dim_set_name(struct isl_dim
*dim
,
340 enum isl_dim_type type
, unsigned pos
,
343 struct isl_name
*name
;
347 if (!name_ok(dim
->ctx
, s
))
349 isl_name_free(dim
->ctx
, get_name(dim
, type
, pos
));
350 name
= isl_name_get(dim
->ctx
, s
);
353 return set_name(dim
, type
, pos
, name
);
359 const char *isl_dim_get_name(struct isl_dim
*dim
,
360 enum isl_dim_type type
, unsigned pos
)
362 struct isl_name
*name
= get_name(dim
, type
, pos
);
363 return name
? name
->name
: NULL
;
366 static struct isl_name
*tuple_name(__isl_keep isl_dim
*dim
,
367 enum isl_dim_type type
)
371 if (type
== isl_dim_in
)
372 return dim
->tuple_name
[0];
373 if (type
== isl_dim_out
)
374 return dim
->tuple_name
[1];
378 static __isl_keep isl_dim
*nested(__isl_keep isl_dim
*dim
,
379 enum isl_dim_type type
)
383 if (type
== isl_dim_in
)
384 return dim
->nested
[0];
385 if (type
== isl_dim_out
)
386 return dim
->nested
[1];
390 int isl_dim_tuple_match(__isl_keep isl_dim
*dim1
, enum isl_dim_type dim1_type
,
391 __isl_keep isl_dim
*dim2
, enum isl_dim_type dim2_type
)
393 struct isl_name
*name1
, *name2
;
394 isl_dim
*nested1
, *nested2
;
396 if (n(dim1
, dim1_type
) != n(dim2
, dim2_type
))
398 name1
= tuple_name(dim1
, dim1_type
);
399 name2
= tuple_name(dim2
, dim2_type
);
402 if (name1
&& name1
->name
!= name2
->name
)
404 nested1
= nested(dim1
, dim1_type
);
405 nested2
= nested(dim2
, dim2_type
);
406 if (!nested1
^ !nested2
)
408 if (nested1
&& !isl_dim_equal(nested1
, nested2
))
413 static int match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
414 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
418 if (!isl_dim_tuple_match(dim1
, dim1_type
, dim2
, dim2_type
))
421 if (!dim1
->names
&& !dim2
->names
)
424 for (i
= 0; i
< n(dim1
, dim1_type
); ++i
) {
425 if (get_name(dim1
, dim1_type
, i
) !=
426 get_name(dim2
, dim2_type
, i
))
432 int isl_dim_match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
433 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
435 return match(dim1
, dim1_type
, dim2
, dim2_type
);
438 static void get_names(struct isl_dim
*dim
, enum isl_dim_type type
,
439 unsigned first
, unsigned n
, struct isl_name
**names
)
443 for (i
= 0; i
< n
; ++i
)
444 names
[i
] = get_name(dim
, type
, first
+i
);
447 struct isl_dim
*isl_dim_extend(struct isl_dim
*dim
,
448 unsigned nparam
, unsigned n_in
, unsigned n_out
)
450 struct isl_name
**names
= NULL
;
454 if (dim
->nparam
== nparam
&& dim
->n_in
== n_in
&& dim
->n_out
== n_out
)
457 isl_assert(dim
->ctx
, dim
->nparam
<= nparam
, goto error
);
458 isl_assert(dim
->ctx
, dim
->n_in
<= n_in
, goto error
);
459 isl_assert(dim
->ctx
, dim
->n_out
<= n_out
, goto error
);
461 dim
= isl_dim_cow(dim
);
464 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
465 nparam
+ n_in
+ n_out
);
468 get_names(dim
, isl_dim_param
, 0, dim
->nparam
, names
);
469 get_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
+ nparam
);
470 get_names(dim
, isl_dim_out
, 0, dim
->n_out
,
471 names
+ nparam
+ n_in
);
474 dim
->n_name
= nparam
+ n_in
+ n_out
;
476 dim
->nparam
= nparam
;
487 struct isl_dim
*isl_dim_add(struct isl_dim
*dim
, enum isl_dim_type type
,
492 dim
= isl_dim_reset(dim
, type
);
495 dim
= isl_dim_extend(dim
,
496 dim
->nparam
+ n
, dim
->n_in
, dim
->n_out
);
497 if (dim
&& dim
->nested
[0] &&
498 !(dim
->nested
[0] = isl_dim_add(dim
->nested
[0],
501 if (dim
&& dim
->nested
[1] &&
502 !(dim
->nested
[1] = isl_dim_add(dim
->nested
[1],
507 return isl_dim_extend(dim
,
508 dim
->nparam
, dim
->n_in
+ n
, dim
->n_out
);
510 return isl_dim_extend(dim
,
511 dim
->nparam
, dim
->n_in
, dim
->n_out
+ n
);
513 isl_die(dim
->ctx
, isl_error_invalid
,
514 "cannot add dimensions of specified type", goto error
);
521 static int valid_dim_type(enum isl_dim_type type
)
533 __isl_give isl_dim
*isl_dim_insert(__isl_take isl_dim
*dim
,
534 enum isl_dim_type type
, unsigned pos
, unsigned n
)
536 struct isl_name
**names
= NULL
;
541 return isl_dim_reset(dim
, type
);
543 if (!valid_dim_type(type
))
544 isl_die(dim
->ctx
, isl_error_invalid
,
545 "cannot insert dimensions of specified type",
548 isl_assert(dim
->ctx
, pos
<= isl_dim_size(dim
, type
), goto error
);
550 dim
= isl_dim_cow(dim
);
558 int *size
= s
- isl_dim_param
;
559 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
560 dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
);
564 size
[isl_dim_param
] = dim
->nparam
;
565 size
[isl_dim_in
] = dim
->n_in
;
566 size
[isl_dim_out
] = dim
->n_out
;
567 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
569 get_names(dim
, t
, 0, size
[t
], names
+ off
);
572 get_names(dim
, t
, 0, pos
, names
+ off
);
574 get_names(dim
, t
, pos
, size
[t
]-pos
, names
+off
);
575 off
+= size
[t
] - pos
;
580 dim
->n_name
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
;
583 case isl_dim_param
: dim
->nparam
+= n
; break;
584 case isl_dim_in
: dim
->n_in
+= n
; break;
585 case isl_dim_out
: dim
->n_out
+= n
; break;
588 dim
= isl_dim_reset(dim
, type
);
596 __isl_give isl_dim
*isl_dim_move(__isl_take isl_dim
*dim
,
597 enum isl_dim_type dst_type
, unsigned dst_pos
,
598 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
607 isl_assert(dim
->ctx
, src_pos
+ n
<= isl_dim_size(dim
, src_type
),
610 if (dst_type
== src_type
&& dst_pos
== src_pos
)
613 isl_assert(dim
->ctx
, dst_type
!= src_type
, goto error
);
615 dim
= isl_dim_reset(dim
, src_type
);
616 dim
= isl_dim_reset(dim
, dst_type
);
618 dim
= isl_dim_cow(dim
);
623 struct isl_name
**names
;
627 int *size
= s
- isl_dim_param
;
628 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
629 dim
->nparam
+ dim
->n_in
+ dim
->n_out
);
633 size
[isl_dim_param
] = dim
->nparam
;
634 size
[isl_dim_in
] = dim
->n_in
;
635 size
[isl_dim_out
] = dim
->n_out
;
636 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
638 get_names(dim
, t
, 0, dst_pos
, names
+ off
);
640 get_names(dim
, src_type
, src_pos
, n
, names
+off
);
642 get_names(dim
, t
, dst_pos
, size
[t
] - dst_pos
,
644 off
+= size
[t
] - dst_pos
;
645 } else if (t
== src_type
) {
646 get_names(dim
, t
, 0, src_pos
, names
+ off
);
648 get_names(dim
, t
, src_pos
+ n
,
649 size
[t
] - src_pos
- n
, names
+ off
);
650 off
+= size
[t
] - src_pos
- n
;
652 get_names(dim
, t
, 0, size
[t
], names
+ off
);
658 dim
->n_name
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
662 case isl_dim_param
: dim
->nparam
+= n
; break;
663 case isl_dim_in
: dim
->n_in
+= n
; break;
664 case isl_dim_out
: dim
->n_out
+= n
; break;
669 case isl_dim_param
: dim
->nparam
-= n
; break;
670 case isl_dim_in
: dim
->n_in
-= n
; break;
671 case isl_dim_out
: dim
->n_out
-= n
; break;
675 if (dst_type
!= isl_dim_param
&& src_type
!= isl_dim_param
)
678 for (i
= 0; i
< 2; ++i
) {
681 dim
->nested
[i
] = isl_dim_replace(dim
->nested
[i
],
693 struct isl_dim
*isl_dim_join(struct isl_dim
*left
, struct isl_dim
*right
)
700 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
702 isl_assert(left
->ctx
,
703 isl_dim_tuple_match(left
, isl_dim_out
, right
, isl_dim_in
),
706 dim
= isl_dim_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
710 dim
= copy_names(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
711 dim
= copy_names(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
712 dim
= copy_names(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
714 if (dim
&& left
->tuple_name
[0] &&
715 !(dim
->tuple_name
[0] = isl_name_copy(dim
->ctx
, left
->tuple_name
[0])))
717 if (dim
&& right
->tuple_name
[1] &&
718 !(dim
->tuple_name
[1] = isl_name_copy(dim
->ctx
, right
->tuple_name
[1])))
720 if (dim
&& left
->nested
[0] &&
721 !(dim
->nested
[0] = isl_dim_copy(left
->nested
[0])))
723 if (dim
&& right
->nested
[1] &&
724 !(dim
->nested
[1] = isl_dim_copy(right
->nested
[1])))
737 struct isl_dim
*isl_dim_product(struct isl_dim
*left
, struct isl_dim
*right
)
739 isl_dim
*dom1
, *dom2
, *nest1
, *nest2
;
744 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
747 dom1
= isl_dim_domain(isl_dim_copy(left
));
748 dom2
= isl_dim_domain(isl_dim_copy(right
));
749 nest1
= isl_dim_wrap(isl_dim_join(isl_dim_reverse(dom1
), dom2
));
751 dom1
= isl_dim_range(left
);
752 dom2
= isl_dim_range(right
);
753 nest2
= isl_dim_wrap(isl_dim_join(isl_dim_reverse(dom1
), dom2
));
755 return isl_dim_join(isl_dim_reverse(nest1
), nest2
);
762 __isl_give isl_dim
*isl_dim_range_product(__isl_take isl_dim
*left
,
763 __isl_take isl_dim
*right
)
765 isl_dim
*dom
, *ran1
, *ran2
, *nest
;
770 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
772 if (!isl_dim_match(left
, isl_dim_in
, right
, isl_dim_in
))
773 isl_die(left
->ctx
, isl_error_invalid
,
774 "domains need to match", goto error
);
776 dom
= isl_dim_domain(isl_dim_copy(left
));
778 ran1
= isl_dim_range(left
);
779 ran2
= isl_dim_range(right
);
780 nest
= isl_dim_wrap(isl_dim_join(isl_dim_reverse(ran1
), ran2
));
782 return isl_dim_join(isl_dim_reverse(dom
), nest
);
789 __isl_give isl_dim
*isl_dim_map_from_set(__isl_take isl_dim
*dim
)
791 struct isl_name
**names
= NULL
;
795 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
796 if (dim
->n_out
== 0 && !isl_dim_is_named_or_nested(dim
, isl_dim_out
))
798 dim
= isl_dim_cow(dim
);
802 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
803 dim
->nparam
+ dim
->n_out
+ dim
->n_out
);
806 get_names(dim
, isl_dim_param
, 0, dim
->nparam
, names
);
807 get_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
+ dim
->nparam
);
809 dim
->n_in
= dim
->n_out
;
813 dim
->n_name
= dim
->nparam
+ dim
->n_out
+ dim
->n_out
;
814 dim
= copy_names(dim
, isl_dim_out
, 0, dim
, isl_dim_in
);
816 isl_name_free(dim
->ctx
, dim
->tuple_name
[0]);
817 dim
->tuple_name
[0] = isl_name_copy(dim
->ctx
, dim
->tuple_name
[1]);
818 isl_dim_free(dim
->nested
[0]);
819 dim
->nested
[0] = isl_dim_copy(dim
->nested
[1]);
826 static struct isl_dim
*set_names(struct isl_dim
*dim
, enum isl_dim_type type
,
827 unsigned first
, unsigned n
, struct isl_name
**names
)
831 for (i
= 0; i
< n
; ++i
)
832 dim
= set_name(dim
, type
, first
+i
, names
[i
]);
837 struct isl_dim
*isl_dim_reverse(struct isl_dim
*dim
)
841 struct isl_name
**names
= NULL
;
842 struct isl_name
*name
;
846 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
849 dim
= isl_dim_cow(dim
);
853 name
= dim
->tuple_name
[0];
854 dim
->tuple_name
[0] = dim
->tuple_name
[1];
855 dim
->tuple_name
[1] = name
;
857 nested
= dim
->nested
[0];
858 dim
->nested
[0] = dim
->nested
[1];
859 dim
->nested
[1] = nested
;
862 names
= isl_alloc_array(dim
->ctx
, struct isl_name
*,
863 dim
->n_in
+ dim
->n_out
);
866 get_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
);
867 get_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
+ dim
->n_in
);
871 dim
->n_in
= dim
->n_out
;
875 dim
= set_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
);
876 dim
= set_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
+ dim
->n_out
);
887 struct isl_dim
*isl_dim_drop(struct isl_dim
*dim
, enum isl_dim_type type
,
888 unsigned first
, unsigned num
)
896 return isl_dim_reset(dim
, type
);
898 if (!valid_dim_type(type
))
899 isl_die(dim
->ctx
, isl_error_invalid
,
900 "cannot drop dimensions of specified type", goto error
);
902 isl_assert(dim
->ctx
, first
+ num
<= n(dim
, type
), goto error
);
903 dim
= isl_dim_cow(dim
);
907 dim
= extend_names(dim
);
910 for (i
= 0; i
< num
; ++i
)
911 isl_name_free(dim
->ctx
, get_name(dim
, type
, first
+i
));
912 for (i
= first
+num
; i
< n(dim
, type
); ++i
)
913 set_name(dim
, type
, i
- num
, get_name(dim
, type
, i
));
916 get_names(dim
, isl_dim_in
, 0, dim
->n_in
,
917 dim
->names
+ offset(dim
, isl_dim_in
) - num
);
919 get_names(dim
, isl_dim_out
, 0, dim
->n_out
,
920 dim
->names
+ offset(dim
, isl_dim_out
) - num
);
927 case isl_dim_param
: dim
->nparam
-= num
; break;
928 case isl_dim_in
: dim
->n_in
-= num
; break;
929 case isl_dim_out
: dim
->n_out
-= num
; break;
932 dim
= isl_dim_reset(dim
, type
);
933 if (type
== isl_dim_param
) {
934 if (dim
&& dim
->nested
[0] &&
935 !(dim
->nested
[0] = isl_dim_drop(dim
->nested
[0],
936 isl_dim_param
, first
, num
)))
938 if (dim
&& dim
->nested
[1] &&
939 !(dim
->nested
[1] = isl_dim_drop(dim
->nested
[1],
940 isl_dim_param
, first
, num
)))
949 struct isl_dim
*isl_dim_drop_inputs(struct isl_dim
*dim
,
950 unsigned first
, unsigned n
)
954 return isl_dim_drop(dim
, isl_dim_in
, first
, n
);
957 struct isl_dim
*isl_dim_drop_outputs(struct isl_dim
*dim
,
958 unsigned first
, unsigned n
)
962 return isl_dim_drop(dim
, isl_dim_out
, first
, n
);
965 struct isl_dim
*isl_dim_domain(struct isl_dim
*dim
)
969 dim
= isl_dim_drop_outputs(dim
, 0, dim
->n_out
);
970 return isl_dim_reverse(dim
);
973 __isl_give isl_dim
*isl_dim_from_domain(__isl_take isl_dim
*dim
)
975 return isl_dim_reverse(dim
);
978 struct isl_dim
*isl_dim_range(struct isl_dim
*dim
)
982 return isl_dim_drop_inputs(dim
, 0, dim
->n_in
);
985 __isl_give isl_dim
*isl_dim_from_range(__isl_take isl_dim
*dim
)
990 __isl_give isl_dim
*isl_dim_as_set_dim(__isl_take isl_dim
*dim
)
992 dim
= isl_dim_cow(dim
);
996 dim
->n_out
+= dim
->n_in
;
998 dim
= isl_dim_reset(dim
, isl_dim_in
);
999 dim
= isl_dim_reset(dim
, isl_dim_out
);
1004 struct isl_dim
*isl_dim_underlying(struct isl_dim
*dim
, unsigned n_div
)
1011 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_name
== 0)
1012 return isl_dim_reset(isl_dim_reset(dim
, isl_dim_in
), isl_dim_out
);
1013 dim
= isl_dim_cow(dim
);
1016 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
1020 for (i
= 0; i
< dim
->n_name
; ++i
)
1021 isl_name_free(dim
->ctx
, get_name(dim
, isl_dim_out
, i
));
1023 dim
= isl_dim_reset(dim
, isl_dim_in
);
1024 dim
= isl_dim_reset(dim
, isl_dim_out
);
1029 unsigned isl_dim_total(struct isl_dim
*dim
)
1031 return dim
? dim
->nparam
+ dim
->n_in
+ dim
->n_out
: 0;
1034 int isl_dim_equal(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
1036 return match(dim1
, isl_dim_param
, dim2
, isl_dim_param
) &&
1037 isl_dim_tuple_match(dim1
, isl_dim_in
, dim2
, isl_dim_in
) &&
1038 isl_dim_tuple_match(dim1
, isl_dim_out
, dim2
, isl_dim_out
);
1041 int isl_dim_compatible(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
1043 return dim1
->nparam
== dim2
->nparam
&&
1044 dim1
->n_in
+ dim1
->n_out
== dim2
->n_in
+ dim2
->n_out
;
1047 static uint32_t isl_hash_dim(uint32_t hash
, __isl_keep isl_dim
*dim
)
1050 struct isl_name
*name
;
1055 hash
= isl_hash_builtin(hash
, dim
->nparam
);
1056 hash
= isl_hash_builtin(hash
, dim
->n_in
);
1057 hash
= isl_hash_builtin(hash
, dim
->n_out
);
1059 for (i
= 0; i
< dim
->nparam
; ++i
) {
1060 name
= get_name(dim
, isl_dim_param
, i
);
1061 hash
= isl_hash_name(hash
, name
);
1064 name
= tuple_name(dim
, isl_dim_in
);
1065 hash
= isl_hash_name(hash
, name
);
1066 name
= tuple_name(dim
, isl_dim_out
);
1067 hash
= isl_hash_name(hash
, name
);
1069 hash
= isl_hash_dim(hash
, dim
->nested
[0]);
1070 hash
= isl_hash_dim(hash
, dim
->nested
[1]);
1075 uint32_t isl_dim_get_hash(__isl_keep isl_dim
*dim
)
1082 hash
= isl_hash_init();
1083 hash
= isl_hash_dim(hash
, dim
);
1088 int isl_dim_is_wrapping(__isl_keep isl_dim
*dim
)
1093 if (dim
->n_in
!= 0 || dim
->tuple_name
[0] || dim
->nested
[0])
1096 return dim
->nested
[1] != NULL
;
1099 __isl_give isl_dim
*isl_dim_wrap(__isl_take isl_dim
*dim
)
1106 wrap
= isl_dim_alloc(dim
->ctx
, dim
->nparam
, 0, dim
->n_in
+ dim
->n_out
);
1108 wrap
= copy_names(wrap
, isl_dim_param
, 0, dim
, isl_dim_param
);
1109 wrap
= copy_names(wrap
, isl_dim_set
, 0, dim
, isl_dim_in
);
1110 wrap
= copy_names(wrap
, isl_dim_set
, dim
->n_in
, dim
, isl_dim_out
);
1115 wrap
->nested
[1] = dim
;
1123 __isl_give isl_dim
*isl_dim_unwrap(__isl_take isl_dim
*dim
)
1130 if (!isl_dim_is_wrapping(dim
))
1131 isl_die(dim
->ctx
, isl_error_invalid
, "not a wrapping dim",
1134 unwrap
= isl_dim_copy(dim
->nested
[1]);
1143 int isl_dim_is_named_or_nested(__isl_keep isl_dim
*dim
, enum isl_dim_type type
)
1145 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
1149 if (dim
->tuple_name
[type
- isl_dim_in
])
1151 if (dim
->nested
[type
- isl_dim_in
])
1156 int isl_dim_may_be_set(__isl_keep isl_dim
*dim
)
1160 if (isl_dim_size(dim
, isl_dim_in
) != 0)
1162 if (isl_dim_is_named_or_nested(dim
, isl_dim_in
))
1167 __isl_give isl_dim
*isl_dim_reset(__isl_take isl_dim
*dim
,
1168 enum isl_dim_type type
)
1170 if (!isl_dim_is_named_or_nested(dim
, type
))
1173 dim
= isl_dim_cow(dim
);
1177 isl_name_free(dim
->ctx
, dim
->tuple_name
[type
- isl_dim_in
]);
1178 dim
->tuple_name
[type
- isl_dim_in
] = NULL
;
1179 isl_dim_free(dim
->nested
[type
- isl_dim_in
]);
1180 dim
->nested
[type
- isl_dim_in
] = NULL
;
1185 __isl_give isl_dim
*isl_dim_flatten(__isl_take isl_dim
*dim
)
1189 if (!dim
->nested
[0] && !dim
->nested
[1])
1193 dim
= isl_dim_reset(dim
, isl_dim_in
);
1194 if (dim
&& dim
->nested
[1])
1195 dim
= isl_dim_reset(dim
, isl_dim_out
);
1200 /* Replace the dimensions of the given type of dst by those of src.
1202 __isl_give isl_dim
*isl_dim_replace(__isl_take isl_dim
*dst
,
1203 enum isl_dim_type type
, __isl_keep isl_dim
*src
)
1205 dst
= isl_dim_cow(dst
);
1210 dst
= isl_dim_drop(dst
, type
, 0, isl_dim_size(dst
, type
));
1211 dst
= isl_dim_add(dst
, type
, isl_dim_size(src
, type
));
1212 dst
= copy_names(dst
, type
, 0, src
, type
);
1214 if (dst
&& type
== isl_dim_param
) {
1216 for (i
= 0; i
<= 1; ++i
) {
1217 if (!dst
->nested
[i
])
1219 dst
->nested
[i
] = isl_dim_replace(dst
->nested
[i
],
1221 if (!dst
->nested
[i
])
1232 /* Given a dimension specification "dim" of a set, create a dimension
1233 * specification for the lift of the set. In particular, the result
1234 * is of the form [dim -> local[..]], with n_local variables in the
1235 * range of the wrapped map.
1237 __isl_give isl_dim
*isl_dim_lift(__isl_take isl_dim
*dim
, unsigned n_local
)
1244 local_dim
= isl_dim_dup(dim
);
1245 local_dim
= isl_dim_drop(local_dim
, isl_dim_set
, 0, dim
->n_out
);
1246 local_dim
= isl_dim_add(local_dim
, isl_dim_set
, n_local
);
1247 local_dim
= isl_dim_set_tuple_name(local_dim
, isl_dim_set
, "local");
1248 dim
= isl_dim_join(isl_dim_from_domain(dim
),
1249 isl_dim_from_range(local_dim
));
1250 dim
= isl_dim_wrap(dim
);
1251 dim
= isl_dim_set_tuple_name(dim
, isl_dim_set
, "lifted");
1256 int isl_dim_can_zip(__isl_keep isl_dim
*dim
)
1261 return dim
->nested
[0] && dim
->nested
[1];
1264 __isl_give isl_dim
*isl_dim_zip(__isl_take isl_dim
*dim
)
1267 isl_dim
*dom_dom
, *dom_ran
, *ran_dom
, *ran_ran
;
1269 if (!isl_dim_can_zip(dim
))
1270 isl_die(dim
->ctx
, isl_error_invalid
, "dim cannot be zipped",
1275 dom
= isl_dim_unwrap(isl_dim_domain(isl_dim_copy(dim
)));
1276 ran
= isl_dim_unwrap(isl_dim_range(dim
));
1277 dom_dom
= isl_dim_domain(isl_dim_copy(dom
));
1278 dom_ran
= isl_dim_range(dom
);
1279 ran_dom
= isl_dim_domain(isl_dim_copy(ran
));
1280 ran_ran
= isl_dim_range(ran
);
1281 dom
= isl_dim_join(isl_dim_from_domain(dom_dom
),
1282 isl_dim_from_range(ran_dom
));
1283 ran
= isl_dim_join(isl_dim_from_domain(dom_ran
),
1284 isl_dim_from_range(ran_ran
));
1285 return isl_dim_join(isl_dim_from_domain(isl_dim_wrap(dom
)),
1286 isl_dim_from_range(isl_dim_wrap(ran
)));
1292 int isl_dim_has_named_params(__isl_keep isl_dim
*dim
)
1299 if (dim
->nparam
== 0)
1301 off
= isl_dim_offset(dim
, isl_dim_param
);
1302 if (off
+ dim
->nparam
> dim
->n_name
)
1304 for (i
= 0; i
< dim
->nparam
; ++i
)
1305 if (!dim
->names
[off
+ i
])