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
;
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 struct isl_dim
*copy_names(struct isl_dim
*dst
,
190 enum isl_dim_type dst_type
, unsigned offset
, struct isl_dim
*src
,
191 enum isl_dim_type src_type
)
194 struct isl_name
*name
;
199 for (i
= 0; i
< n(src
, src_type
); ++i
) {
200 name
= get_name(src
, src_type
, i
);
203 dst
= set_name(dst
, dst_type
, offset
+ i
,
204 isl_name_copy(dst
->ctx
, name
));
211 struct isl_dim
*isl_dim_dup(struct isl_dim
*dim
)
216 dup
= isl_dim_alloc(dim
->ctx
, dim
->nparam
, dim
->n_in
, dim
->n_out
);
217 if (dim
->tuple_name
[0] &&
218 !(dup
->tuple_name
[0] = isl_name_copy(dim
->ctx
, dim
->tuple_name
[0])))
220 if (dim
->tuple_name
[1] &&
221 !(dup
->tuple_name
[1] = isl_name_copy(dim
->ctx
, dim
->tuple_name
[1])))
223 if (dim
->nested
[0] && !(dup
->nested
[0] = isl_dim_copy(dim
->nested
[0])))
225 if (dim
->nested
[1] && !(dup
->nested
[1] = isl_dim_copy(dim
->nested
[1])))
229 dup
= copy_names(dup
, isl_dim_param
, 0, dim
, isl_dim_param
);
230 dup
= copy_names(dup
, isl_dim_in
, 0, dim
, isl_dim_in
);
231 dup
= copy_names(dup
, isl_dim_out
, 0, dim
, isl_dim_out
);
238 struct isl_dim
*isl_dim_cow(struct isl_dim
*dim
)
246 return isl_dim_dup(dim
);
249 struct isl_dim
*isl_dim_copy(struct isl_dim
*dim
)
258 void isl_dim_free(struct isl_dim
*dim
)
268 isl_name_free(dim
->ctx
, dim
->tuple_name
[0]);
269 isl_name_free(dim
->ctx
, dim
->tuple_name
[1]);
271 isl_dim_free(dim
->nested
[0]);
272 isl_dim_free(dim
->nested
[1]);
274 for (i
= 0; i
< dim
->n_name
; ++i
)
275 isl_name_free(dim
->ctx
, dim
->names
[i
]);
277 isl_ctx_deref(dim
->ctx
);
282 static int name_ok(isl_ctx
*ctx
, const char *s
)
287 dummy
= strtol(s
, &p
, 0);
289 isl_die(ctx
, isl_error_invalid
, "name looks like a number",
295 __isl_give isl_dim
*isl_dim_set_tuple_name(__isl_take isl_dim
*dim
,
296 enum isl_dim_type type
, const char *s
)
298 struct isl_name
*name
;
300 dim
= isl_dim_cow(dim
);
303 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
304 isl_die(dim
->ctx
, isl_error_invalid
,
305 "only input, output and set tuples can have names",
310 if (!name_ok(dim
->ctx
, s
))
312 name
= isl_name_get(dim
->ctx
, s
);
317 isl_name_free(dim
->ctx
, dim
->tuple_name
[type
- isl_dim_in
]);
318 dim
->tuple_name
[type
- isl_dim_in
] = name
;
326 const char *isl_dim_get_tuple_name(__isl_keep isl_dim
*dim
,
327 enum isl_dim_type type
)
329 struct isl_name
*name
;
332 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
334 name
= dim
->tuple_name
[type
- isl_dim_in
];
335 return name
? name
->name
: NULL
;
338 struct isl_dim
*isl_dim_set_name(struct isl_dim
*dim
,
339 enum isl_dim_type type
, unsigned pos
,
342 struct isl_name
*name
;
346 if (!name_ok(dim
->ctx
, s
))
348 isl_name_free(dim
->ctx
, get_name(dim
, type
, pos
));
349 name
= isl_name_get(dim
->ctx
, s
);
352 return set_name(dim
, type
, pos
, name
);
358 const char *isl_dim_get_name(struct isl_dim
*dim
,
359 enum isl_dim_type type
, unsigned pos
)
361 struct isl_name
*name
= get_name(dim
, type
, pos
);
362 return name
? name
->name
: NULL
;
365 static struct isl_name
*tuple_name(__isl_keep isl_dim
*dim
,
366 enum isl_dim_type type
)
370 if (type
== isl_dim_in
)
371 return dim
->tuple_name
[0];
372 if (type
== isl_dim_out
)
373 return dim
->tuple_name
[1];
377 static __isl_keep isl_dim
*nested(__isl_keep isl_dim
*dim
,
378 enum isl_dim_type type
)
382 if (type
== isl_dim_in
)
383 return dim
->nested
[0];
384 if (type
== isl_dim_out
)
385 return dim
->nested
[1];
389 int isl_dim_tuple_match(__isl_keep isl_dim
*dim1
, enum isl_dim_type dim1_type
,
390 __isl_keep isl_dim
*dim2
, enum isl_dim_type dim2_type
)
392 struct isl_name
*name1
, *name2
;
393 isl_dim
*nested1
, *nested2
;
395 if (n(dim1
, dim1_type
) != n(dim2
, dim2_type
))
397 name1
= tuple_name(dim1
, dim1_type
);
398 name2
= tuple_name(dim2
, dim2_type
);
401 if (name1
&& name1
->name
!= name2
->name
)
403 nested1
= nested(dim1
, dim1_type
);
404 nested2
= nested(dim2
, dim2_type
);
405 if (!nested1
^ !nested2
)
407 if (nested1
&& !isl_dim_equal(nested1
, nested2
))
412 static int match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
413 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
417 if (!isl_dim_tuple_match(dim1
, dim1_type
, dim2
, dim2_type
))
420 if (!dim1
->names
&& !dim2
->names
)
423 for (i
= 0; i
< n(dim1
, dim1_type
); ++i
) {
424 if (get_name(dim1
, dim1_type
, i
) !=
425 get_name(dim2
, dim2_type
, i
))
431 int isl_dim_match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
432 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
434 return match(dim1
, dim1_type
, dim2
, dim2_type
);
437 static void get_names(struct isl_dim
*dim
, enum isl_dim_type type
,
438 unsigned first
, unsigned n
, struct isl_name
**names
)
442 for (i
= 0; i
< n
; ++i
)
443 names
[i
] = get_name(dim
, type
, first
+i
);
446 struct isl_dim
*isl_dim_extend(struct isl_dim
*dim
,
447 unsigned nparam
, unsigned n_in
, unsigned n_out
)
449 struct isl_name
**names
= NULL
;
453 if (dim
->nparam
== nparam
&& dim
->n_in
== n_in
&& dim
->n_out
== n_out
)
456 isl_assert(dim
->ctx
, dim
->nparam
<= nparam
, goto error
);
457 isl_assert(dim
->ctx
, dim
->n_in
<= n_in
, goto error
);
458 isl_assert(dim
->ctx
, dim
->n_out
<= n_out
, goto error
);
460 dim
= isl_dim_cow(dim
);
463 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
464 nparam
+ n_in
+ n_out
);
467 get_names(dim
, isl_dim_param
, 0, dim
->nparam
, names
);
468 get_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
+ nparam
);
469 get_names(dim
, isl_dim_out
, 0, dim
->n_out
,
470 names
+ nparam
+ n_in
);
473 dim
->n_name
= nparam
+ n_in
+ n_out
;
475 dim
->nparam
= nparam
;
486 struct isl_dim
*isl_dim_add(struct isl_dim
*dim
, enum isl_dim_type type
,
491 dim
= isl_dim_reset(dim
, type
);
494 dim
= isl_dim_extend(dim
,
495 dim
->nparam
+ n
, dim
->n_in
, dim
->n_out
);
496 if (dim
&& dim
->nested
[0] &&
497 !(dim
->nested
[0] = isl_dim_add(dim
->nested
[0],
500 if (dim
&& dim
->nested
[1] &&
501 !(dim
->nested
[1] = isl_dim_add(dim
->nested
[1],
506 return isl_dim_extend(dim
,
507 dim
->nparam
, dim
->n_in
+ n
, dim
->n_out
);
509 return isl_dim_extend(dim
,
510 dim
->nparam
, dim
->n_in
, dim
->n_out
+ n
);
518 __isl_give isl_dim
*isl_dim_insert(__isl_take isl_dim
*dim
,
519 enum isl_dim_type type
, unsigned pos
, unsigned n
)
521 struct isl_name
**names
= NULL
;
526 return isl_dim_reset(dim
, type
);
528 isl_assert(dim
->ctx
, pos
<= isl_dim_size(dim
, type
), goto error
);
530 dim
= isl_dim_cow(dim
);
538 int *size
= s
- isl_dim_param
;
539 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
540 dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
);
544 size
[isl_dim_param
] = dim
->nparam
;
545 size
[isl_dim_in
] = dim
->n_in
;
546 size
[isl_dim_out
] = dim
->n_out
;
547 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
549 get_names(dim
, t
, 0, size
[t
], names
+ off
);
552 get_names(dim
, t
, 0, pos
, names
+ off
);
554 get_names(dim
, t
, pos
, size
[t
]-pos
, names
+off
);
555 off
+= size
[t
] - pos
;
560 dim
->n_name
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
;
563 case isl_dim_param
: dim
->nparam
+= n
; break;
564 case isl_dim_in
: dim
->n_in
+= n
; break;
565 case isl_dim_out
: dim
->n_out
+= n
; break;
567 dim
= isl_dim_reset(dim
, type
);
575 __isl_give isl_dim
*isl_dim_move(__isl_take isl_dim
*dim
,
576 enum isl_dim_type dst_type
, unsigned dst_pos
,
577 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
586 isl_assert(dim
->ctx
, src_pos
+ n
<= isl_dim_size(dim
, src_type
),
589 if (dst_type
== src_type
&& dst_pos
== src_pos
)
592 isl_assert(dim
->ctx
, dst_type
!= src_type
, goto error
);
594 dim
= isl_dim_reset(dim
, src_type
);
595 dim
= isl_dim_reset(dim
, dst_type
);
597 dim
= isl_dim_cow(dim
);
602 struct isl_name
**names
;
606 int *size
= s
- isl_dim_param
;
607 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
608 dim
->nparam
+ dim
->n_in
+ dim
->n_out
);
612 size
[isl_dim_param
] = dim
->nparam
;
613 size
[isl_dim_in
] = dim
->n_in
;
614 size
[isl_dim_out
] = dim
->n_out
;
615 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
617 get_names(dim
, t
, 0, dst_pos
, names
+ off
);
619 get_names(dim
, src_type
, src_pos
, n
, names
+off
);
621 get_names(dim
, t
, dst_pos
, size
[t
] - dst_pos
,
623 off
+= size
[t
] - dst_pos
;
624 } else if (t
== src_type
) {
625 get_names(dim
, t
, 0, src_pos
, names
+ off
);
627 get_names(dim
, t
, src_pos
+ n
,
628 size
[t
] - src_pos
- n
, names
+ off
);
629 off
+= size
[t
] - src_pos
- n
;
631 get_names(dim
, t
, 0, size
[t
], names
+ off
);
637 dim
->n_name
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
641 case isl_dim_param
: dim
->nparam
+= n
; break;
642 case isl_dim_in
: dim
->n_in
+= n
; break;
643 case isl_dim_out
: dim
->n_out
+= n
; break;
647 case isl_dim_param
: dim
->nparam
-= n
; break;
648 case isl_dim_in
: dim
->n_in
-= n
; break;
649 case isl_dim_out
: dim
->n_out
-= n
; break;
652 if (dst_type
!= isl_dim_param
&& src_type
!= isl_dim_param
)
655 for (i
= 0; i
< 2; ++i
) {
658 dim
->nested
[i
] = isl_dim_replace(dim
->nested
[i
],
670 struct isl_dim
*isl_dim_join(struct isl_dim
*left
, struct isl_dim
*right
)
677 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
679 isl_assert(left
->ctx
,
680 isl_dim_tuple_match(left
, isl_dim_out
, right
, isl_dim_in
),
683 dim
= isl_dim_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
687 dim
= copy_names(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
688 dim
= copy_names(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
689 dim
= copy_names(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
691 if (dim
&& left
->tuple_name
[0] &&
692 !(dim
->tuple_name
[0] = isl_name_copy(dim
->ctx
, left
->tuple_name
[0])))
694 if (dim
&& right
->tuple_name
[1] &&
695 !(dim
->tuple_name
[1] = isl_name_copy(dim
->ctx
, right
->tuple_name
[1])))
697 if (dim
&& left
->nested
[0] &&
698 !(dim
->nested
[0] = isl_dim_copy(left
->nested
[0])))
700 if (dim
&& right
->nested
[1] &&
701 !(dim
->nested
[1] = isl_dim_copy(right
->nested
[1])))
714 struct isl_dim
*isl_dim_product(struct isl_dim
*left
, struct isl_dim
*right
)
716 isl_dim
*dom1
, *dom2
, *nest1
, *nest2
;
721 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
724 dom1
= isl_dim_domain(isl_dim_copy(left
));
725 dom2
= isl_dim_domain(isl_dim_copy(right
));
726 nest1
= isl_dim_wrap(isl_dim_join(isl_dim_reverse(dom1
), dom2
));
728 dom1
= isl_dim_range(left
);
729 dom2
= isl_dim_range(right
);
730 nest2
= isl_dim_wrap(isl_dim_join(isl_dim_reverse(dom1
), dom2
));
732 return isl_dim_join(isl_dim_reverse(nest1
), nest2
);
739 __isl_give isl_dim
*isl_dim_range_product(__isl_take isl_dim
*left
,
740 __isl_take isl_dim
*right
)
742 isl_dim
*dom
, *ran1
, *ran2
, *nest
;
747 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
749 if (!isl_dim_match(left
, isl_dim_in
, right
, isl_dim_in
))
750 isl_die(left
->ctx
, isl_error_invalid
,
751 "domains need to match", goto error
);
753 dom
= isl_dim_domain(isl_dim_copy(left
));
755 ran1
= isl_dim_range(left
);
756 ran2
= isl_dim_range(right
);
757 nest
= isl_dim_wrap(isl_dim_join(isl_dim_reverse(ran1
), ran2
));
759 return isl_dim_join(isl_dim_reverse(dom
), nest
);
766 __isl_give isl_dim
*isl_dim_map_from_set(__isl_take isl_dim
*dim
)
768 struct isl_name
**names
= NULL
;
772 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
773 if (dim
->n_out
== 0 && !isl_dim_is_named_or_nested(dim
, isl_dim_out
))
775 dim
= isl_dim_cow(dim
);
779 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
780 dim
->nparam
+ dim
->n_out
+ dim
->n_out
);
783 get_names(dim
, isl_dim_param
, 0, dim
->nparam
, names
);
784 get_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
+ dim
->nparam
);
786 dim
->n_in
= dim
->n_out
;
790 dim
->n_name
= dim
->nparam
+ dim
->n_out
+ dim
->n_out
;
791 dim
= copy_names(dim
, isl_dim_out
, 0, dim
, isl_dim_in
);
793 isl_name_free(dim
->ctx
, dim
->tuple_name
[0]);
794 dim
->tuple_name
[0] = isl_name_copy(dim
->ctx
, dim
->tuple_name
[1]);
795 isl_dim_free(dim
->nested
[0]);
796 dim
->nested
[0] = isl_dim_copy(dim
->nested
[1]);
803 static struct isl_dim
*set_names(struct isl_dim
*dim
, enum isl_dim_type type
,
804 unsigned first
, unsigned n
, struct isl_name
**names
)
808 for (i
= 0; i
< n
; ++i
)
809 dim
= set_name(dim
, type
, first
+i
, names
[i
]);
814 struct isl_dim
*isl_dim_reverse(struct isl_dim
*dim
)
818 struct isl_name
**names
= NULL
;
819 struct isl_name
*name
;
823 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
826 dim
= isl_dim_cow(dim
);
830 name
= dim
->tuple_name
[0];
831 dim
->tuple_name
[0] = dim
->tuple_name
[1];
832 dim
->tuple_name
[1] = name
;
834 nested
= dim
->nested
[0];
835 dim
->nested
[0] = dim
->nested
[1];
836 dim
->nested
[1] = nested
;
839 names
= isl_alloc_array(dim
->ctx
, struct isl_name
*,
840 dim
->n_in
+ dim
->n_out
);
843 get_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
);
844 get_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
+ dim
->n_in
);
848 dim
->n_in
= dim
->n_out
;
852 dim
= set_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
);
853 dim
= set_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
+ dim
->n_out
);
864 struct isl_dim
*isl_dim_drop(struct isl_dim
*dim
, enum isl_dim_type type
,
865 unsigned first
, unsigned num
)
873 return isl_dim_reset(dim
, type
);
875 isl_assert(dim
->ctx
, first
+ num
<= n(dim
, type
), goto error
);
876 dim
= isl_dim_cow(dim
);
880 dim
= extend_names(dim
);
883 for (i
= 0; i
< num
; ++i
)
884 isl_name_free(dim
->ctx
, get_name(dim
, type
, first
+i
));
885 for (i
= first
+num
; i
< n(dim
, type
); ++i
)
886 set_name(dim
, type
, i
- num
, get_name(dim
, type
, i
));
889 get_names(dim
, isl_dim_in
, 0, dim
->n_in
,
890 dim
->names
+ offset(dim
, isl_dim_in
) - num
);
892 get_names(dim
, isl_dim_out
, 0, dim
->n_out
,
893 dim
->names
+ offset(dim
, isl_dim_out
) - num
);
900 case isl_dim_param
: dim
->nparam
-= num
; break;
901 case isl_dim_in
: dim
->n_in
-= num
; break;
902 case isl_dim_out
: dim
->n_out
-= num
; break;
904 dim
= isl_dim_reset(dim
, type
);
905 if (type
== isl_dim_param
) {
906 if (dim
&& dim
->nested
[0] &&
907 !(dim
->nested
[0] = isl_dim_drop(dim
->nested
[0],
908 isl_dim_param
, first
, num
)))
910 if (dim
&& dim
->nested
[1] &&
911 !(dim
->nested
[1] = isl_dim_drop(dim
->nested
[1],
912 isl_dim_param
, first
, num
)))
921 struct isl_dim
*isl_dim_drop_inputs(struct isl_dim
*dim
,
922 unsigned first
, unsigned n
)
926 return isl_dim_drop(dim
, isl_dim_in
, first
, n
);
929 struct isl_dim
*isl_dim_drop_outputs(struct isl_dim
*dim
,
930 unsigned first
, unsigned n
)
934 return isl_dim_drop(dim
, isl_dim_out
, first
, n
);
937 struct isl_dim
*isl_dim_domain(struct isl_dim
*dim
)
941 dim
= isl_dim_drop_outputs(dim
, 0, dim
->n_out
);
942 return isl_dim_reverse(dim
);
945 __isl_give isl_dim
*isl_dim_from_domain(__isl_take isl_dim
*dim
)
947 return isl_dim_reverse(dim
);
950 struct isl_dim
*isl_dim_range(struct isl_dim
*dim
)
954 return isl_dim_drop_inputs(dim
, 0, dim
->n_in
);
957 __isl_give isl_dim
*isl_dim_from_range(__isl_take isl_dim
*dim
)
962 __isl_give isl_dim
*isl_dim_as_set_dim(__isl_take isl_dim
*dim
)
964 dim
= isl_dim_cow(dim
);
968 dim
->n_out
+= dim
->n_in
;
970 dim
= isl_dim_reset(dim
, isl_dim_in
);
971 dim
= isl_dim_reset(dim
, isl_dim_out
);
976 struct isl_dim
*isl_dim_underlying(struct isl_dim
*dim
, unsigned n_div
)
983 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_name
== 0)
984 return isl_dim_reset(isl_dim_reset(dim
, isl_dim_in
), isl_dim_out
);
985 dim
= isl_dim_cow(dim
);
988 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
992 for (i
= 0; i
< dim
->n_name
; ++i
)
993 isl_name_free(dim
->ctx
, get_name(dim
, isl_dim_out
, i
));
995 dim
= isl_dim_reset(dim
, isl_dim_in
);
996 dim
= isl_dim_reset(dim
, isl_dim_out
);
1001 unsigned isl_dim_total(struct isl_dim
*dim
)
1003 return dim
? dim
->nparam
+ dim
->n_in
+ dim
->n_out
: 0;
1006 int isl_dim_equal(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
1008 return match(dim1
, isl_dim_param
, dim2
, isl_dim_param
) &&
1009 isl_dim_tuple_match(dim1
, isl_dim_in
, dim2
, isl_dim_in
) &&
1010 isl_dim_tuple_match(dim1
, isl_dim_out
, dim2
, isl_dim_out
);
1013 int isl_dim_compatible(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
1015 return dim1
->nparam
== dim2
->nparam
&&
1016 dim1
->n_in
+ dim1
->n_out
== dim2
->n_in
+ dim2
->n_out
;
1019 static uint32_t isl_hash_dim(uint32_t hash
, __isl_keep isl_dim
*dim
)
1022 struct isl_name
*name
;
1027 hash
= isl_hash_builtin(hash
, dim
->nparam
);
1028 hash
= isl_hash_builtin(hash
, dim
->n_in
);
1029 hash
= isl_hash_builtin(hash
, dim
->n_out
);
1031 for (i
= 0; i
< dim
->nparam
; ++i
) {
1032 name
= get_name(dim
, isl_dim_param
, i
);
1033 hash
= isl_hash_name(hash
, name
);
1036 name
= tuple_name(dim
, isl_dim_in
);
1037 hash
= isl_hash_name(hash
, name
);
1038 name
= tuple_name(dim
, isl_dim_out
);
1039 hash
= isl_hash_name(hash
, name
);
1041 hash
= isl_hash_dim(hash
, dim
->nested
[0]);
1042 hash
= isl_hash_dim(hash
, dim
->nested
[1]);
1047 uint32_t isl_dim_get_hash(__isl_keep isl_dim
*dim
)
1054 hash
= isl_hash_init();
1055 hash
= isl_hash_dim(hash
, dim
);
1060 int isl_dim_is_wrapping(__isl_keep isl_dim
*dim
)
1065 if (dim
->n_in
!= 0 || dim
->tuple_name
[0] || dim
->nested
[0])
1068 return dim
->nested
[1] != NULL
;
1071 __isl_give isl_dim
*isl_dim_wrap(__isl_take isl_dim
*dim
)
1078 wrap
= isl_dim_alloc(dim
->ctx
, dim
->nparam
, 0, dim
->n_in
+ dim
->n_out
);
1080 wrap
= copy_names(wrap
, isl_dim_param
, 0, dim
, isl_dim_param
);
1081 wrap
= copy_names(wrap
, isl_dim_set
, 0, dim
, isl_dim_in
);
1082 wrap
= copy_names(wrap
, isl_dim_set
, dim
->n_in
, dim
, isl_dim_out
);
1087 wrap
->nested
[1] = dim
;
1095 __isl_give isl_dim
*isl_dim_unwrap(__isl_take isl_dim
*dim
)
1102 if (!isl_dim_is_wrapping(dim
))
1103 isl_die(dim
->ctx
, isl_error_invalid
, "not a wrapping dim",
1106 unwrap
= isl_dim_copy(dim
->nested
[1]);
1115 int isl_dim_is_named_or_nested(__isl_keep isl_dim
*dim
, enum isl_dim_type type
)
1117 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
1121 if (dim
->tuple_name
[type
- isl_dim_in
])
1123 if (dim
->nested
[type
- isl_dim_in
])
1128 __isl_give isl_dim
*isl_dim_reset(__isl_take isl_dim
*dim
,
1129 enum isl_dim_type type
)
1131 if (!isl_dim_is_named_or_nested(dim
, type
))
1134 dim
= isl_dim_cow(dim
);
1138 isl_name_free(dim
->ctx
, dim
->tuple_name
[type
- isl_dim_in
]);
1139 dim
->tuple_name
[type
- isl_dim_in
] = NULL
;
1140 isl_dim_free(dim
->nested
[type
- isl_dim_in
]);
1141 dim
->nested
[type
- isl_dim_in
] = NULL
;
1146 __isl_give isl_dim
*isl_dim_flatten(__isl_take isl_dim
*dim
)
1150 if (!dim
->nested
[0] && !dim
->nested
[1])
1154 dim
= isl_dim_reset(dim
, isl_dim_in
);
1155 if (dim
&& dim
->nested
[1])
1156 dim
= isl_dim_reset(dim
, isl_dim_out
);
1161 /* Replace the dimensions of the given type of dst by those of src.
1163 __isl_give isl_dim
*isl_dim_replace(__isl_take isl_dim
*dst
,
1164 enum isl_dim_type type
, __isl_keep isl_dim
*src
)
1166 dst
= isl_dim_cow(dst
);
1171 dst
= isl_dim_drop(dst
, type
, 0, isl_dim_size(dst
, type
));
1172 dst
= isl_dim_add(dst
, type
, isl_dim_size(src
, type
));
1173 dst
= copy_names(dst
, type
, 0, src
, type
);
1175 if (dst
&& type
== isl_dim_param
) {
1177 for (i
= 0; i
<= 1; ++i
) {
1178 if (!dst
->nested
[i
])
1180 dst
->nested
[i
] = isl_dim_replace(dst
->nested
[i
],
1182 if (!dst
->nested
[i
])
1193 /* Given a dimension specification "dim" of a set, create a dimension
1194 * specification for the lift of the set. In particular, the result
1195 * is of the form [dim -> local[..]], with n_local variables in the
1196 * range of the wrapped map.
1198 __isl_give isl_dim
*isl_dim_lift(__isl_take isl_dim
*dim
, unsigned n_local
)
1205 local_dim
= isl_dim_dup(dim
);
1206 local_dim
= isl_dim_drop(local_dim
, isl_dim_set
, 0, dim
->n_out
);
1207 local_dim
= isl_dim_add(local_dim
, isl_dim_set
, n_local
);
1208 local_dim
= isl_dim_set_tuple_name(local_dim
, isl_dim_set
, "local");
1209 dim
= isl_dim_join(isl_dim_from_domain(dim
),
1210 isl_dim_from_range(local_dim
));
1211 dim
= isl_dim_wrap(dim
);
1212 dim
= isl_dim_set_tuple_name(dim
, isl_dim_set
, "lifted");