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
;
397 if (n(dim1
, dim1_type
) != n(dim2
, dim2_type
))
399 name1
= tuple_name(dim1
, dim1_type
);
400 name2
= tuple_name(dim2
, dim2_type
);
403 if (name1
&& name1
->name
!= name2
->name
)
405 nested1
= nested(dim1
, dim1_type
);
406 nested2
= nested(dim2
, dim2_type
);
407 if (!nested1
^ !nested2
)
409 if (nested1
&& !isl_dim_equal(nested1
, nested2
))
414 static int match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
415 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
419 if (!isl_dim_tuple_match(dim1
, dim1_type
, dim2
, dim2_type
))
422 if (!dim1
->names
&& !dim2
->names
)
425 for (i
= 0; i
< n(dim1
, dim1_type
); ++i
) {
426 if (get_name(dim1
, dim1_type
, i
) !=
427 get_name(dim2
, dim2_type
, i
))
433 int isl_dim_match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
434 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
436 return match(dim1
, dim1_type
, dim2
, dim2_type
);
439 static void get_names(struct isl_dim
*dim
, enum isl_dim_type type
,
440 unsigned first
, unsigned n
, struct isl_name
**names
)
444 for (i
= 0; i
< n
; ++i
)
445 names
[i
] = get_name(dim
, type
, first
+i
);
448 struct isl_dim
*isl_dim_extend(struct isl_dim
*dim
,
449 unsigned nparam
, unsigned n_in
, unsigned n_out
)
451 struct isl_name
**names
= NULL
;
455 if (dim
->nparam
== nparam
&& dim
->n_in
== n_in
&& dim
->n_out
== n_out
)
458 isl_assert(dim
->ctx
, dim
->nparam
<= nparam
, goto error
);
459 isl_assert(dim
->ctx
, dim
->n_in
<= n_in
, goto error
);
460 isl_assert(dim
->ctx
, dim
->n_out
<= n_out
, goto error
);
462 dim
= isl_dim_cow(dim
);
465 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
466 nparam
+ n_in
+ n_out
);
469 get_names(dim
, isl_dim_param
, 0, dim
->nparam
, names
);
470 get_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
+ nparam
);
471 get_names(dim
, isl_dim_out
, 0, dim
->n_out
,
472 names
+ nparam
+ n_in
);
475 dim
->n_name
= nparam
+ n_in
+ n_out
;
477 dim
->nparam
= nparam
;
488 struct isl_dim
*isl_dim_add(struct isl_dim
*dim
, enum isl_dim_type type
,
493 dim
= isl_dim_reset(dim
, type
);
496 dim
= isl_dim_extend(dim
,
497 dim
->nparam
+ n
, dim
->n_in
, dim
->n_out
);
498 if (dim
&& dim
->nested
[0] &&
499 !(dim
->nested
[0] = isl_dim_add(dim
->nested
[0],
502 if (dim
&& dim
->nested
[1] &&
503 !(dim
->nested
[1] = isl_dim_add(dim
->nested
[1],
508 return isl_dim_extend(dim
,
509 dim
->nparam
, dim
->n_in
+ n
, dim
->n_out
);
511 return isl_dim_extend(dim
,
512 dim
->nparam
, dim
->n_in
, dim
->n_out
+ n
);
514 isl_die(dim
->ctx
, isl_error_invalid
,
515 "cannot add dimensions of specified type", goto error
);
522 static int valid_dim_type(enum isl_dim_type type
)
534 __isl_give isl_dim
*isl_dim_insert(__isl_take isl_dim
*dim
,
535 enum isl_dim_type type
, unsigned pos
, unsigned n
)
537 struct isl_name
**names
= NULL
;
542 return isl_dim_reset(dim
, type
);
544 if (!valid_dim_type(type
))
545 isl_die(dim
->ctx
, isl_error_invalid
,
546 "cannot insert dimensions of specified type",
549 isl_assert(dim
->ctx
, pos
<= isl_dim_size(dim
, type
), goto error
);
551 dim
= isl_dim_cow(dim
);
559 int *size
= s
- isl_dim_param
;
560 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
561 dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
);
565 size
[isl_dim_param
] = dim
->nparam
;
566 size
[isl_dim_in
] = dim
->n_in
;
567 size
[isl_dim_out
] = dim
->n_out
;
568 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
570 get_names(dim
, t
, 0, size
[t
], names
+ off
);
573 get_names(dim
, t
, 0, pos
, names
+ off
);
575 get_names(dim
, t
, pos
, size
[t
]-pos
, names
+off
);
576 off
+= size
[t
] - pos
;
581 dim
->n_name
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
;
584 case isl_dim_param
: dim
->nparam
+= n
; break;
585 case isl_dim_in
: dim
->n_in
+= n
; break;
586 case isl_dim_out
: dim
->n_out
+= n
; break;
589 dim
= isl_dim_reset(dim
, type
);
597 __isl_give isl_dim
*isl_dim_move(__isl_take isl_dim
*dim
,
598 enum isl_dim_type dst_type
, unsigned dst_pos
,
599 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
608 isl_assert(dim
->ctx
, src_pos
+ n
<= isl_dim_size(dim
, src_type
),
611 if (dst_type
== src_type
&& dst_pos
== src_pos
)
614 isl_assert(dim
->ctx
, dst_type
!= src_type
, goto error
);
616 dim
= isl_dim_reset(dim
, src_type
);
617 dim
= isl_dim_reset(dim
, dst_type
);
619 dim
= isl_dim_cow(dim
);
624 struct isl_name
**names
;
628 int *size
= s
- isl_dim_param
;
629 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
630 dim
->nparam
+ dim
->n_in
+ dim
->n_out
);
634 size
[isl_dim_param
] = dim
->nparam
;
635 size
[isl_dim_in
] = dim
->n_in
;
636 size
[isl_dim_out
] = dim
->n_out
;
637 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
639 get_names(dim
, t
, 0, dst_pos
, names
+ off
);
641 get_names(dim
, src_type
, src_pos
, n
, names
+off
);
643 get_names(dim
, t
, dst_pos
, size
[t
] - dst_pos
,
645 off
+= size
[t
] - dst_pos
;
646 } else if (t
== src_type
) {
647 get_names(dim
, t
, 0, src_pos
, names
+ off
);
649 get_names(dim
, t
, src_pos
+ n
,
650 size
[t
] - src_pos
- n
, names
+ off
);
651 off
+= size
[t
] - src_pos
- n
;
653 get_names(dim
, t
, 0, size
[t
], names
+ off
);
659 dim
->n_name
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
663 case isl_dim_param
: dim
->nparam
+= n
; break;
664 case isl_dim_in
: dim
->n_in
+= n
; break;
665 case isl_dim_out
: dim
->n_out
+= n
; break;
670 case isl_dim_param
: dim
->nparam
-= n
; break;
671 case isl_dim_in
: dim
->n_in
-= n
; break;
672 case isl_dim_out
: dim
->n_out
-= n
; break;
676 if (dst_type
!= isl_dim_param
&& src_type
!= isl_dim_param
)
679 for (i
= 0; i
< 2; ++i
) {
682 dim
->nested
[i
] = isl_dim_replace(dim
->nested
[i
],
694 struct isl_dim
*isl_dim_join(struct isl_dim
*left
, struct isl_dim
*right
)
701 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
703 isl_assert(left
->ctx
,
704 isl_dim_tuple_match(left
, isl_dim_out
, right
, isl_dim_in
),
707 dim
= isl_dim_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
711 dim
= copy_names(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
712 dim
= copy_names(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
713 dim
= copy_names(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
715 if (dim
&& left
->tuple_name
[0] &&
716 !(dim
->tuple_name
[0] = isl_name_copy(dim
->ctx
, left
->tuple_name
[0])))
718 if (dim
&& right
->tuple_name
[1] &&
719 !(dim
->tuple_name
[1] = isl_name_copy(dim
->ctx
, right
->tuple_name
[1])))
721 if (dim
&& left
->nested
[0] &&
722 !(dim
->nested
[0] = isl_dim_copy(left
->nested
[0])))
724 if (dim
&& right
->nested
[1] &&
725 !(dim
->nested
[1] = isl_dim_copy(right
->nested
[1])))
738 struct isl_dim
*isl_dim_product(struct isl_dim
*left
, struct isl_dim
*right
)
740 isl_dim
*dom1
, *dom2
, *nest1
, *nest2
;
745 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
748 dom1
= isl_dim_domain(isl_dim_copy(left
));
749 dom2
= isl_dim_domain(isl_dim_copy(right
));
750 nest1
= isl_dim_wrap(isl_dim_join(isl_dim_reverse(dom1
), dom2
));
752 dom1
= isl_dim_range(left
);
753 dom2
= isl_dim_range(right
);
754 nest2
= isl_dim_wrap(isl_dim_join(isl_dim_reverse(dom1
), dom2
));
756 return isl_dim_join(isl_dim_reverse(nest1
), nest2
);
763 __isl_give isl_dim
*isl_dim_range_product(__isl_take isl_dim
*left
,
764 __isl_take isl_dim
*right
)
766 isl_dim
*dom
, *ran1
, *ran2
, *nest
;
771 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
773 if (!isl_dim_match(left
, isl_dim_in
, right
, isl_dim_in
))
774 isl_die(left
->ctx
, isl_error_invalid
,
775 "domains need to match", goto error
);
777 dom
= isl_dim_domain(isl_dim_copy(left
));
779 ran1
= isl_dim_range(left
);
780 ran2
= isl_dim_range(right
);
781 nest
= isl_dim_wrap(isl_dim_join(isl_dim_reverse(ran1
), ran2
));
783 return isl_dim_join(isl_dim_reverse(dom
), nest
);
790 __isl_give isl_dim
*isl_dim_map_from_set(__isl_take isl_dim
*dim
)
792 struct isl_name
**names
= NULL
;
796 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
797 if (dim
->n_out
== 0 && !isl_dim_is_named_or_nested(dim
, isl_dim_out
))
799 dim
= isl_dim_cow(dim
);
803 names
= isl_calloc_array(dim
->ctx
, struct isl_name
*,
804 dim
->nparam
+ dim
->n_out
+ dim
->n_out
);
807 get_names(dim
, isl_dim_param
, 0, dim
->nparam
, names
);
808 get_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
+ dim
->nparam
);
810 dim
->n_in
= dim
->n_out
;
814 dim
->n_name
= dim
->nparam
+ dim
->n_out
+ dim
->n_out
;
815 dim
= copy_names(dim
, isl_dim_out
, 0, dim
, isl_dim_in
);
817 isl_name_free(dim
->ctx
, dim
->tuple_name
[0]);
818 dim
->tuple_name
[0] = isl_name_copy(dim
->ctx
, dim
->tuple_name
[1]);
819 isl_dim_free(dim
->nested
[0]);
820 dim
->nested
[0] = isl_dim_copy(dim
->nested
[1]);
827 static struct isl_dim
*set_names(struct isl_dim
*dim
, enum isl_dim_type type
,
828 unsigned first
, unsigned n
, struct isl_name
**names
)
832 for (i
= 0; i
< n
; ++i
)
833 dim
= set_name(dim
, type
, first
+i
, names
[i
]);
838 struct isl_dim
*isl_dim_reverse(struct isl_dim
*dim
)
842 struct isl_name
**names
= NULL
;
843 struct isl_name
*name
;
847 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
850 dim
= isl_dim_cow(dim
);
854 name
= dim
->tuple_name
[0];
855 dim
->tuple_name
[0] = dim
->tuple_name
[1];
856 dim
->tuple_name
[1] = name
;
858 nested
= dim
->nested
[0];
859 dim
->nested
[0] = dim
->nested
[1];
860 dim
->nested
[1] = nested
;
863 names
= isl_alloc_array(dim
->ctx
, struct isl_name
*,
864 dim
->n_in
+ dim
->n_out
);
867 get_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
);
868 get_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
+ dim
->n_in
);
872 dim
->n_in
= dim
->n_out
;
876 dim
= set_names(dim
, isl_dim_out
, 0, dim
->n_out
, names
);
877 dim
= set_names(dim
, isl_dim_in
, 0, dim
->n_in
, names
+ dim
->n_out
);
888 struct isl_dim
*isl_dim_drop(struct isl_dim
*dim
, enum isl_dim_type type
,
889 unsigned first
, unsigned num
)
897 return isl_dim_reset(dim
, type
);
899 if (!valid_dim_type(type
))
900 isl_die(dim
->ctx
, isl_error_invalid
,
901 "cannot drop dimensions of specified type", goto error
);
903 isl_assert(dim
->ctx
, first
+ num
<= n(dim
, type
), goto error
);
904 dim
= isl_dim_cow(dim
);
908 dim
= extend_names(dim
);
911 for (i
= 0; i
< num
; ++i
)
912 isl_name_free(dim
->ctx
, get_name(dim
, type
, first
+i
));
913 for (i
= first
+num
; i
< n(dim
, type
); ++i
)
914 set_name(dim
, type
, i
- num
, get_name(dim
, type
, i
));
917 get_names(dim
, isl_dim_in
, 0, dim
->n_in
,
918 dim
->names
+ offset(dim
, isl_dim_in
) - num
);
920 get_names(dim
, isl_dim_out
, 0, dim
->n_out
,
921 dim
->names
+ offset(dim
, isl_dim_out
) - num
);
928 case isl_dim_param
: dim
->nparam
-= num
; break;
929 case isl_dim_in
: dim
->n_in
-= num
; break;
930 case isl_dim_out
: dim
->n_out
-= num
; break;
933 dim
= isl_dim_reset(dim
, type
);
934 if (type
== isl_dim_param
) {
935 if (dim
&& dim
->nested
[0] &&
936 !(dim
->nested
[0] = isl_dim_drop(dim
->nested
[0],
937 isl_dim_param
, first
, num
)))
939 if (dim
&& dim
->nested
[1] &&
940 !(dim
->nested
[1] = isl_dim_drop(dim
->nested
[1],
941 isl_dim_param
, first
, num
)))
950 struct isl_dim
*isl_dim_drop_inputs(struct isl_dim
*dim
,
951 unsigned first
, unsigned n
)
955 return isl_dim_drop(dim
, isl_dim_in
, first
, n
);
958 struct isl_dim
*isl_dim_drop_outputs(struct isl_dim
*dim
,
959 unsigned first
, unsigned n
)
963 return isl_dim_drop(dim
, isl_dim_out
, first
, n
);
966 struct isl_dim
*isl_dim_domain(struct isl_dim
*dim
)
970 dim
= isl_dim_drop_outputs(dim
, 0, dim
->n_out
);
971 return isl_dim_reverse(dim
);
974 __isl_give isl_dim
*isl_dim_from_domain(__isl_take isl_dim
*dim
)
976 return isl_dim_reverse(dim
);
979 struct isl_dim
*isl_dim_range(struct isl_dim
*dim
)
983 return isl_dim_drop_inputs(dim
, 0, dim
->n_in
);
986 __isl_give isl_dim
*isl_dim_from_range(__isl_take isl_dim
*dim
)
991 __isl_give isl_dim
*isl_dim_as_set_dim(__isl_take isl_dim
*dim
)
993 dim
= isl_dim_cow(dim
);
997 dim
->n_out
+= dim
->n_in
;
999 dim
= isl_dim_reset(dim
, isl_dim_in
);
1000 dim
= isl_dim_reset(dim
, isl_dim_out
);
1005 struct isl_dim
*isl_dim_underlying(struct isl_dim
*dim
, unsigned n_div
)
1012 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_name
== 0)
1013 return isl_dim_reset(isl_dim_reset(dim
, isl_dim_in
), isl_dim_out
);
1014 dim
= isl_dim_cow(dim
);
1017 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
1021 for (i
= 0; i
< dim
->n_name
; ++i
)
1022 isl_name_free(dim
->ctx
, get_name(dim
, isl_dim_out
, i
));
1024 dim
= isl_dim_reset(dim
, isl_dim_in
);
1025 dim
= isl_dim_reset(dim
, isl_dim_out
);
1030 unsigned isl_dim_total(struct isl_dim
*dim
)
1032 return dim
? dim
->nparam
+ dim
->n_in
+ dim
->n_out
: 0;
1035 int isl_dim_equal(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
1037 return match(dim1
, isl_dim_param
, dim2
, isl_dim_param
) &&
1038 isl_dim_tuple_match(dim1
, isl_dim_in
, dim2
, isl_dim_in
) &&
1039 isl_dim_tuple_match(dim1
, isl_dim_out
, dim2
, isl_dim_out
);
1042 int isl_dim_compatible(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
1044 return dim1
->nparam
== dim2
->nparam
&&
1045 dim1
->n_in
+ dim1
->n_out
== dim2
->n_in
+ dim2
->n_out
;
1048 static uint32_t isl_hash_dim(uint32_t hash
, __isl_keep isl_dim
*dim
)
1051 struct isl_name
*name
;
1056 hash
= isl_hash_builtin(hash
, dim
->nparam
);
1057 hash
= isl_hash_builtin(hash
, dim
->n_in
);
1058 hash
= isl_hash_builtin(hash
, dim
->n_out
);
1060 for (i
= 0; i
< dim
->nparam
; ++i
) {
1061 name
= get_name(dim
, isl_dim_param
, i
);
1062 hash
= isl_hash_name(hash
, name
);
1065 name
= tuple_name(dim
, isl_dim_in
);
1066 hash
= isl_hash_name(hash
, name
);
1067 name
= tuple_name(dim
, isl_dim_out
);
1068 hash
= isl_hash_name(hash
, name
);
1070 hash
= isl_hash_dim(hash
, dim
->nested
[0]);
1071 hash
= isl_hash_dim(hash
, dim
->nested
[1]);
1076 uint32_t isl_dim_get_hash(__isl_keep isl_dim
*dim
)
1083 hash
= isl_hash_init();
1084 hash
= isl_hash_dim(hash
, dim
);
1089 int isl_dim_is_wrapping(__isl_keep isl_dim
*dim
)
1094 if (dim
->n_in
!= 0 || dim
->tuple_name
[0] || dim
->nested
[0])
1097 return dim
->nested
[1] != NULL
;
1100 __isl_give isl_dim
*isl_dim_wrap(__isl_take isl_dim
*dim
)
1107 wrap
= isl_dim_alloc(dim
->ctx
, dim
->nparam
, 0, dim
->n_in
+ dim
->n_out
);
1109 wrap
= copy_names(wrap
, isl_dim_param
, 0, dim
, isl_dim_param
);
1110 wrap
= copy_names(wrap
, isl_dim_set
, 0, dim
, isl_dim_in
);
1111 wrap
= copy_names(wrap
, isl_dim_set
, dim
->n_in
, dim
, isl_dim_out
);
1116 wrap
->nested
[1] = dim
;
1124 __isl_give isl_dim
*isl_dim_unwrap(__isl_take isl_dim
*dim
)
1131 if (!isl_dim_is_wrapping(dim
))
1132 isl_die(dim
->ctx
, isl_error_invalid
, "not a wrapping dim",
1135 unwrap
= isl_dim_copy(dim
->nested
[1]);
1144 int isl_dim_is_named_or_nested(__isl_keep isl_dim
*dim
, enum isl_dim_type type
)
1146 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
1150 if (dim
->tuple_name
[type
- isl_dim_in
])
1152 if (dim
->nested
[type
- isl_dim_in
])
1157 int isl_dim_may_be_set(__isl_keep isl_dim
*dim
)
1161 if (isl_dim_size(dim
, isl_dim_in
) != 0)
1163 if (isl_dim_is_named_or_nested(dim
, isl_dim_in
))
1168 __isl_give isl_dim
*isl_dim_reset(__isl_take isl_dim
*dim
,
1169 enum isl_dim_type type
)
1171 if (!isl_dim_is_named_or_nested(dim
, type
))
1174 dim
= isl_dim_cow(dim
);
1178 isl_name_free(dim
->ctx
, dim
->tuple_name
[type
- isl_dim_in
]);
1179 dim
->tuple_name
[type
- isl_dim_in
] = NULL
;
1180 isl_dim_free(dim
->nested
[type
- isl_dim_in
]);
1181 dim
->nested
[type
- isl_dim_in
] = NULL
;
1186 __isl_give isl_dim
*isl_dim_flatten(__isl_take isl_dim
*dim
)
1190 if (!dim
->nested
[0] && !dim
->nested
[1])
1194 dim
= isl_dim_reset(dim
, isl_dim_in
);
1195 if (dim
&& dim
->nested
[1])
1196 dim
= isl_dim_reset(dim
, isl_dim_out
);
1201 __isl_give isl_dim
*isl_dim_flatten_range(__isl_take isl_dim
*dim
)
1205 if (!dim
->nested
[1])
1208 return isl_dim_reset(dim
, isl_dim_out
);
1211 /* Replace the dimensions of the given type of dst by those of src.
1213 __isl_give isl_dim
*isl_dim_replace(__isl_take isl_dim
*dst
,
1214 enum isl_dim_type type
, __isl_keep isl_dim
*src
)
1216 dst
= isl_dim_cow(dst
);
1221 dst
= isl_dim_drop(dst
, type
, 0, isl_dim_size(dst
, type
));
1222 dst
= isl_dim_add(dst
, type
, isl_dim_size(src
, type
));
1223 dst
= copy_names(dst
, type
, 0, src
, type
);
1225 if (dst
&& type
== isl_dim_param
) {
1227 for (i
= 0; i
<= 1; ++i
) {
1228 if (!dst
->nested
[i
])
1230 dst
->nested
[i
] = isl_dim_replace(dst
->nested
[i
],
1232 if (!dst
->nested
[i
])
1243 /* Given a dimension specification "dim" of a set, create a dimension
1244 * specification for the lift of the set. In particular, the result
1245 * is of the form [dim -> local[..]], with n_local variables in the
1246 * range of the wrapped map.
1248 __isl_give isl_dim
*isl_dim_lift(__isl_take isl_dim
*dim
, unsigned n_local
)
1255 local_dim
= isl_dim_dup(dim
);
1256 local_dim
= isl_dim_drop(local_dim
, isl_dim_set
, 0, dim
->n_out
);
1257 local_dim
= isl_dim_add(local_dim
, isl_dim_set
, n_local
);
1258 local_dim
= isl_dim_set_tuple_name(local_dim
, isl_dim_set
, "local");
1259 dim
= isl_dim_join(isl_dim_from_domain(dim
),
1260 isl_dim_from_range(local_dim
));
1261 dim
= isl_dim_wrap(dim
);
1262 dim
= isl_dim_set_tuple_name(dim
, isl_dim_set
, "lifted");
1267 int isl_dim_can_zip(__isl_keep isl_dim
*dim
)
1272 return dim
->nested
[0] && dim
->nested
[1];
1275 __isl_give isl_dim
*isl_dim_zip(__isl_take isl_dim
*dim
)
1278 isl_dim
*dom_dom
, *dom_ran
, *ran_dom
, *ran_ran
;
1280 if (!isl_dim_can_zip(dim
))
1281 isl_die(dim
->ctx
, isl_error_invalid
, "dim cannot be zipped",
1286 dom
= isl_dim_unwrap(isl_dim_domain(isl_dim_copy(dim
)));
1287 ran
= isl_dim_unwrap(isl_dim_range(dim
));
1288 dom_dom
= isl_dim_domain(isl_dim_copy(dom
));
1289 dom_ran
= isl_dim_range(dom
);
1290 ran_dom
= isl_dim_domain(isl_dim_copy(ran
));
1291 ran_ran
= isl_dim_range(ran
);
1292 dom
= isl_dim_join(isl_dim_from_domain(dom_dom
),
1293 isl_dim_from_range(ran_dom
));
1294 ran
= isl_dim_join(isl_dim_from_domain(dom_ran
),
1295 isl_dim_from_range(ran_ran
));
1296 return isl_dim_join(isl_dim_from_domain(isl_dim_wrap(dom
)),
1297 isl_dim_from_range(isl_dim_wrap(ran
)));
1303 int isl_dim_has_named_params(__isl_keep isl_dim
*dim
)
1310 if (dim
->nparam
== 0)
1312 off
= isl_dim_offset(dim
, isl_dim_param
);
1313 if (off
+ dim
->nparam
> dim
->n_name
)
1315 for (i
= 0; i
< dim
->nparam
; ++i
)
1316 if (!dim
->names
[off
+ i
])
1321 /* Align the initial parameters of dim1 to match the order in dim2.
1323 __isl_give isl_dim
*isl_dim_align_params(__isl_take isl_dim
*dim1
,
1324 __isl_take isl_dim
*dim2
)
1326 isl_reordering
*exp
;
1328 if (!isl_dim_has_named_params(dim1
) || !isl_dim_has_named_params(dim2
))
1329 isl_die(isl_dim_get_ctx(dim1
), isl_error_invalid
,
1330 "parameter alignment requires named parameters",
1333 exp
= isl_parameter_alignment_reordering(dim1
, dim2
);
1338 dim1
= isl_dim_copy(exp
->dim
);
1339 isl_reordering_free(exp
);