2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_dim_private.h>
15 #include <isl_id_private.h>
16 #include <isl_reordering.h>
18 isl_ctx
*isl_dim_get_ctx(__isl_keep isl_dim
*dim
)
20 return dim
? dim
->ctx
: NULL
;
23 struct isl_dim
*isl_dim_alloc(struct isl_ctx
*ctx
,
24 unsigned nparam
, unsigned n_in
, unsigned n_out
)
28 dim
= isl_alloc_type(ctx
, struct isl_dim
);
39 dim
->tuple_id
[0] = NULL
;
40 dim
->tuple_id
[1] = NULL
;
42 dim
->nested
[0] = NULL
;
43 dim
->nested
[1] = NULL
;
51 struct isl_dim
*isl_dim_set_alloc(struct isl_ctx
*ctx
,
52 unsigned nparam
, unsigned dim
)
54 return isl_dim_alloc(ctx
, nparam
, 0, dim
);
57 static unsigned global_pos(struct isl_dim
*dim
,
58 enum isl_dim_type type
, unsigned pos
)
60 struct isl_ctx
*ctx
= dim
->ctx
;
64 isl_assert(ctx
, pos
< dim
->nparam
, return isl_dim_total(dim
));
67 isl_assert(ctx
, pos
< dim
->n_in
, return isl_dim_total(dim
));
68 return pos
+ dim
->nparam
;
70 isl_assert(ctx
, pos
< dim
->n_out
, return isl_dim_total(dim
));
71 return pos
+ dim
->nparam
+ dim
->n_in
;
73 isl_assert(ctx
, 0, return isl_dim_total(dim
));
75 return isl_dim_total(dim
);
78 /* Extend length of ids array to the total number of dimensions.
80 static __isl_give isl_dim
*extend_ids(__isl_take isl_dim
*dim
)
85 if (isl_dim_total(dim
) <= dim
->n_id
)
89 dim
->ids
= isl_calloc_array(dim
->ctx
,
90 isl_id
*, isl_dim_total(dim
));
94 ids
= isl_realloc_array(dim
->ctx
, dim
->ids
,
95 isl_id
*, isl_dim_total(dim
));
99 for (i
= dim
->n_id
; i
< isl_dim_total(dim
); ++i
)
103 dim
->n_id
= isl_dim_total(dim
);
111 static __isl_give isl_dim
*set_id(__isl_take isl_dim
*dim
,
112 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
114 dim
= isl_dim_cow(dim
);
119 pos
= global_pos(dim
, type
, pos
);
120 if (pos
== isl_dim_total(dim
))
123 if (pos
>= dim
->n_id
) {
126 dim
= extend_ids(dim
);
140 static __isl_keep isl_id
*get_id(__isl_keep isl_dim
*dim
,
141 enum isl_dim_type type
, unsigned pos
)
146 pos
= global_pos(dim
, type
, pos
);
147 if (pos
== isl_dim_total(dim
))
149 if (pos
>= dim
->n_id
)
151 return dim
->ids
[pos
];
154 static unsigned offset(struct isl_dim
*dim
, enum isl_dim_type type
)
157 case isl_dim_param
: return 0;
158 case isl_dim_in
: return dim
->nparam
;
159 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
164 static unsigned n(struct isl_dim
*dim
, enum isl_dim_type type
)
167 case isl_dim_param
: return dim
->nparam
;
168 case isl_dim_in
: return dim
->n_in
;
169 case isl_dim_out
: return dim
->n_out
;
170 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
175 unsigned isl_dim_size(struct isl_dim
*dim
, enum isl_dim_type type
)
182 unsigned isl_dim_offset(__isl_keep isl_dim
*dim
, enum isl_dim_type type
)
186 return offset(dim
, type
);
189 static __isl_give isl_dim
*copy_ids(__isl_take isl_dim
*dst
,
190 enum isl_dim_type dst_type
, unsigned offset
, __isl_keep isl_dim
*src
,
191 enum isl_dim_type src_type
)
199 for (i
= 0; i
< n(src
, src_type
); ++i
) {
200 id
= get_id(src
, src_type
, i
);
203 dst
= set_id(dst
, dst_type
, offset
+ i
, isl_id_copy(id
));
210 struct isl_dim
*isl_dim_dup(struct isl_dim
*dim
)
215 dup
= isl_dim_alloc(dim
->ctx
, dim
->nparam
, dim
->n_in
, dim
->n_out
);
216 if (dim
->tuple_id
[0] &&
217 !(dup
->tuple_id
[0] = isl_id_copy(dim
->tuple_id
[0])))
219 if (dim
->tuple_id
[1] &&
220 !(dup
->tuple_id
[1] = isl_id_copy(dim
->tuple_id
[1])))
222 if (dim
->nested
[0] && !(dup
->nested
[0] = isl_dim_copy(dim
->nested
[0])))
224 if (dim
->nested
[1] && !(dup
->nested
[1] = isl_dim_copy(dim
->nested
[1])))
228 dup
= copy_ids(dup
, isl_dim_param
, 0, dim
, isl_dim_param
);
229 dup
= copy_ids(dup
, isl_dim_in
, 0, dim
, isl_dim_in
);
230 dup
= copy_ids(dup
, isl_dim_out
, 0, dim
, isl_dim_out
);
237 struct isl_dim
*isl_dim_cow(struct isl_dim
*dim
)
245 return isl_dim_dup(dim
);
248 struct isl_dim
*isl_dim_copy(struct isl_dim
*dim
)
257 void isl_dim_free(struct isl_dim
*dim
)
267 isl_id_free(dim
->tuple_id
[0]);
268 isl_id_free(dim
->tuple_id
[1]);
270 isl_dim_free(dim
->nested
[0]);
271 isl_dim_free(dim
->nested
[1]);
273 for (i
= 0; i
< dim
->n_id
; ++i
)
274 isl_id_free(dim
->ids
[i
]);
276 isl_ctx_deref(dim
->ctx
);
281 static int name_ok(isl_ctx
*ctx
, const char *s
)
286 dummy
= strtol(s
, &p
, 0);
288 isl_die(ctx
, isl_error_invalid
, "name looks like a number",
294 __isl_give isl_id
*isl_dim_get_tuple_id(__isl_keep isl_dim
*dim
,
295 enum isl_dim_type type
)
299 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
301 return isl_id_copy(dim
->tuple_id
[type
- isl_dim_in
]);
304 __isl_give isl_dim
*isl_dim_set_tuple_id(__isl_take isl_dim
*dim
,
305 enum isl_dim_type type
, __isl_take isl_id
*id
)
307 dim
= isl_dim_cow(dim
);
310 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
311 isl_die(dim
->ctx
, isl_error_invalid
,
312 "only input, output and set tuples can have names",
315 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
316 dim
->tuple_id
[type
- isl_dim_in
] = id
;
325 __isl_give isl_dim
*isl_dim_reset_tuple_id(__isl_take isl_dim
*dim
,
326 enum isl_dim_type type
)
328 dim
= isl_dim_cow(dim
);
331 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
332 isl_die(dim
->ctx
, isl_error_invalid
,
333 "only input, output and set tuples can have names",
336 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
337 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
345 __isl_give isl_dim
*isl_dim_set_dim_id(__isl_take isl_dim
*dim
,
346 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
348 dim
= isl_dim_cow(dim
);
351 isl_id_free(get_id(dim
, type
, pos
));
352 return set_id(dim
, type
, pos
, id
);
359 __isl_give isl_id
*isl_dim_get_dim_id(__isl_keep isl_dim
*dim
,
360 enum isl_dim_type type
, unsigned pos
)
362 return isl_id_copy(get_id(dim
, type
, pos
));
365 __isl_give isl_dim
*isl_dim_set_tuple_name(__isl_take isl_dim
*dim
,
366 enum isl_dim_type type
, const char *s
)
374 return isl_dim_reset_tuple_id(dim
, type
);
376 if (!name_ok(dim
->ctx
, s
))
379 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
380 return isl_dim_set_tuple_id(dim
, type
, id
);
386 const char *isl_dim_get_tuple_name(__isl_keep isl_dim
*dim
,
387 enum isl_dim_type type
)
392 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
394 id
= dim
->tuple_id
[type
- isl_dim_in
];
395 return id
? id
->name
: NULL
;
398 struct isl_dim
*isl_dim_set_name(struct isl_dim
*dim
,
399 enum isl_dim_type type
, unsigned pos
,
406 if (!name_ok(dim
->ctx
, s
))
408 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
409 return isl_dim_set_dim_id(dim
, type
, pos
, id
);
415 const char *isl_dim_get_name(struct isl_dim
*dim
,
416 enum isl_dim_type type
, unsigned pos
)
418 isl_id
*id
= get_id(dim
, type
, pos
);
419 return id
? id
->name
: NULL
;
422 int isl_dim_find_dim_by_id(__isl_keep isl_dim
*dim
, enum isl_dim_type type
,
423 __isl_keep isl_id
*id
)
432 offset
= isl_dim_offset(dim
, type
);
433 n
= isl_dim_size(dim
, type
);
434 for (i
= 0; i
< n
&& offset
+ i
< dim
->n_id
; ++i
)
435 if (dim
->ids
[offset
+ i
] == id
)
441 static __isl_keep isl_id
*tuple_id(__isl_keep isl_dim
*dim
,
442 enum isl_dim_type type
)
446 if (type
== isl_dim_in
)
447 return dim
->tuple_id
[0];
448 if (type
== isl_dim_out
)
449 return dim
->tuple_id
[1];
453 static __isl_keep isl_dim
*nested(__isl_keep isl_dim
*dim
,
454 enum isl_dim_type type
)
458 if (type
== isl_dim_in
)
459 return dim
->nested
[0];
460 if (type
== isl_dim_out
)
461 return dim
->nested
[1];
465 int isl_dim_tuple_match(__isl_keep isl_dim
*dim1
, enum isl_dim_type dim1_type
,
466 __isl_keep isl_dim
*dim2
, enum isl_dim_type dim2_type
)
469 isl_dim
*nested1
, *nested2
;
471 if (n(dim1
, dim1_type
) != n(dim2
, dim2_type
))
473 id1
= tuple_id(dim1
, dim1_type
);
474 id2
= tuple_id(dim2
, dim2_type
);
477 if (id1
&& id1
!= id2
)
479 nested1
= nested(dim1
, dim1_type
);
480 nested2
= nested(dim2
, dim2_type
);
481 if (!nested1
^ !nested2
)
483 if (nested1
&& !isl_dim_equal(nested1
, nested2
))
488 static int match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
489 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
493 if (!isl_dim_tuple_match(dim1
, dim1_type
, dim2
, dim2_type
))
496 if (!dim1
->ids
&& !dim2
->ids
)
499 for (i
= 0; i
< n(dim1
, dim1_type
); ++i
) {
500 if (get_id(dim1
, dim1_type
, i
) != get_id(dim2
, dim2_type
, i
))
506 int isl_dim_match(struct isl_dim
*dim1
, enum isl_dim_type dim1_type
,
507 struct isl_dim
*dim2
, enum isl_dim_type dim2_type
)
509 return match(dim1
, dim1_type
, dim2
, dim2_type
);
512 static void get_ids(struct isl_dim
*dim
, enum isl_dim_type type
,
513 unsigned first
, unsigned n
, __isl_keep isl_id
**ids
)
517 for (i
= 0; i
< n
; ++i
)
518 ids
[i
] = get_id(dim
, type
, first
+ i
);
521 struct isl_dim
*isl_dim_extend(struct isl_dim
*dim
,
522 unsigned nparam
, unsigned n_in
, unsigned n_out
)
528 if (dim
->nparam
== nparam
&& dim
->n_in
== n_in
&& dim
->n_out
== n_out
)
531 isl_assert(dim
->ctx
, dim
->nparam
<= nparam
, goto error
);
532 isl_assert(dim
->ctx
, dim
->n_in
<= n_in
, goto error
);
533 isl_assert(dim
->ctx
, dim
->n_out
<= n_out
, goto error
);
535 dim
= isl_dim_cow(dim
);
538 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
539 nparam
+ n_in
+ n_out
);
542 get_ids(dim
, isl_dim_param
, 0, dim
->nparam
, ids
);
543 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
+ nparam
);
544 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ nparam
+ n_in
);
547 dim
->n_id
= nparam
+ n_in
+ n_out
;
549 dim
->nparam
= nparam
;
560 struct isl_dim
*isl_dim_add(struct isl_dim
*dim
, enum isl_dim_type type
,
565 dim
= isl_dim_reset(dim
, type
);
568 dim
= isl_dim_extend(dim
,
569 dim
->nparam
+ n
, dim
->n_in
, dim
->n_out
);
570 if (dim
&& dim
->nested
[0] &&
571 !(dim
->nested
[0] = isl_dim_add(dim
->nested
[0],
574 if (dim
&& dim
->nested
[1] &&
575 !(dim
->nested
[1] = isl_dim_add(dim
->nested
[1],
580 return isl_dim_extend(dim
,
581 dim
->nparam
, dim
->n_in
+ n
, dim
->n_out
);
583 return isl_dim_extend(dim
,
584 dim
->nparam
, dim
->n_in
, dim
->n_out
+ n
);
586 isl_die(dim
->ctx
, isl_error_invalid
,
587 "cannot add dimensions of specified type", goto error
);
594 static int valid_dim_type(enum isl_dim_type type
)
606 __isl_give isl_dim
*isl_dim_insert(__isl_take isl_dim
*dim
,
607 enum isl_dim_type type
, unsigned pos
, unsigned n
)
614 return isl_dim_reset(dim
, type
);
616 if (!valid_dim_type(type
))
617 isl_die(dim
->ctx
, isl_error_invalid
,
618 "cannot insert dimensions of specified type",
621 isl_assert(dim
->ctx
, pos
<= isl_dim_size(dim
, type
), goto error
);
623 dim
= isl_dim_cow(dim
);
631 int *size
= s
- isl_dim_param
;
632 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
633 dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
);
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_ids(dim
, t
, 0, size
[t
], ids
+ off
);
645 get_ids(dim
, t
, 0, pos
, ids
+ off
);
647 get_ids(dim
, t
, pos
, size
[t
] - pos
, ids
+ off
);
648 off
+= size
[t
] - pos
;
653 dim
->n_id
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
;
656 case isl_dim_param
: dim
->nparam
+= n
; break;
657 case isl_dim_in
: dim
->n_in
+= n
; break;
658 case isl_dim_out
: dim
->n_out
+= n
; break;
661 dim
= isl_dim_reset(dim
, type
);
669 __isl_give isl_dim
*isl_dim_move(__isl_take isl_dim
*dim
,
670 enum isl_dim_type dst_type
, unsigned dst_pos
,
671 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
680 isl_assert(dim
->ctx
, src_pos
+ n
<= isl_dim_size(dim
, src_type
),
683 if (dst_type
== src_type
&& dst_pos
== src_pos
)
686 isl_assert(dim
->ctx
, dst_type
!= src_type
, goto error
);
688 dim
= isl_dim_reset(dim
, src_type
);
689 dim
= isl_dim_reset(dim
, dst_type
);
691 dim
= isl_dim_cow(dim
);
700 int *size
= s
- isl_dim_param
;
701 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
702 dim
->nparam
+ dim
->n_in
+ dim
->n_out
);
706 size
[isl_dim_param
] = dim
->nparam
;
707 size
[isl_dim_in
] = dim
->n_in
;
708 size
[isl_dim_out
] = dim
->n_out
;
709 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
711 get_ids(dim
, t
, 0, dst_pos
, ids
+ off
);
713 get_ids(dim
, src_type
, src_pos
, n
, ids
+ off
);
715 get_ids(dim
, t
, dst_pos
, size
[t
] - dst_pos
,
717 off
+= size
[t
] - dst_pos
;
718 } else if (t
== src_type
) {
719 get_ids(dim
, t
, 0, src_pos
, ids
+ off
);
721 get_ids(dim
, t
, src_pos
+ n
,
722 size
[t
] - src_pos
- n
, ids
+ off
);
723 off
+= size
[t
] - src_pos
- n
;
725 get_ids(dim
, t
, 0, size
[t
], ids
+ off
);
731 dim
->n_id
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
735 case isl_dim_param
: dim
->nparam
+= n
; break;
736 case isl_dim_in
: dim
->n_in
+= n
; break;
737 case isl_dim_out
: dim
->n_out
+= n
; break;
742 case isl_dim_param
: dim
->nparam
-= n
; break;
743 case isl_dim_in
: dim
->n_in
-= n
; break;
744 case isl_dim_out
: dim
->n_out
-= n
; break;
748 if (dst_type
!= isl_dim_param
&& src_type
!= isl_dim_param
)
751 for (i
= 0; i
< 2; ++i
) {
754 dim
->nested
[i
] = isl_dim_replace(dim
->nested
[i
],
766 struct isl_dim
*isl_dim_join(struct isl_dim
*left
, struct isl_dim
*right
)
773 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
775 isl_assert(left
->ctx
,
776 isl_dim_tuple_match(left
, isl_dim_out
, right
, isl_dim_in
),
779 dim
= isl_dim_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
783 dim
= copy_ids(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
784 dim
= copy_ids(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
785 dim
= copy_ids(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
787 if (dim
&& left
->tuple_id
[0] &&
788 !(dim
->tuple_id
[0] = isl_id_copy(left
->tuple_id
[0])))
790 if (dim
&& right
->tuple_id
[1] &&
791 !(dim
->tuple_id
[1] = isl_id_copy(right
->tuple_id
[1])))
793 if (dim
&& left
->nested
[0] &&
794 !(dim
->nested
[0] = isl_dim_copy(left
->nested
[0])))
796 if (dim
&& right
->nested
[1] &&
797 !(dim
->nested
[1] = isl_dim_copy(right
->nested
[1])))
810 struct isl_dim
*isl_dim_product(struct isl_dim
*left
, struct isl_dim
*right
)
812 isl_dim
*dom1
, *dom2
, *nest1
, *nest2
;
817 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
820 dom1
= isl_dim_domain(isl_dim_copy(left
));
821 dom2
= isl_dim_domain(isl_dim_copy(right
));
822 nest1
= isl_dim_wrap(isl_dim_join(isl_dim_reverse(dom1
), dom2
));
824 dom1
= isl_dim_range(left
);
825 dom2
= isl_dim_range(right
);
826 nest2
= isl_dim_wrap(isl_dim_join(isl_dim_reverse(dom1
), dom2
));
828 return isl_dim_join(isl_dim_reverse(nest1
), nest2
);
835 __isl_give isl_dim
*isl_dim_range_product(__isl_take isl_dim
*left
,
836 __isl_take isl_dim
*right
)
838 isl_dim
*dom
, *ran1
, *ran2
, *nest
;
843 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
845 if (!isl_dim_match(left
, isl_dim_in
, right
, isl_dim_in
))
846 isl_die(left
->ctx
, isl_error_invalid
,
847 "domains need to match", goto error
);
849 dom
= isl_dim_domain(isl_dim_copy(left
));
851 ran1
= isl_dim_range(left
);
852 ran2
= isl_dim_range(right
);
853 nest
= isl_dim_wrap(isl_dim_join(isl_dim_reverse(ran1
), ran2
));
855 return isl_dim_join(isl_dim_reverse(dom
), nest
);
862 __isl_give isl_dim
*isl_dim_map_from_set(__isl_take isl_dim
*dim
)
868 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
869 if (dim
->n_out
== 0 && !isl_dim_is_named_or_nested(dim
, isl_dim_out
))
871 dim
= isl_dim_cow(dim
);
875 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
876 dim
->nparam
+ dim
->n_out
+ dim
->n_out
);
879 get_ids(dim
, isl_dim_param
, 0, dim
->nparam
, ids
);
880 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->nparam
);
882 dim
->n_in
= dim
->n_out
;
886 dim
->n_id
= dim
->nparam
+ dim
->n_out
+ dim
->n_out
;
887 dim
= copy_ids(dim
, isl_dim_out
, 0, dim
, isl_dim_in
);
889 isl_id_free(dim
->tuple_id
[0]);
890 dim
->tuple_id
[0] = isl_id_copy(dim
->tuple_id
[1]);
891 isl_dim_free(dim
->nested
[0]);
892 dim
->nested
[0] = isl_dim_copy(dim
->nested
[1]);
899 static __isl_give isl_dim
*set_ids(struct isl_dim
*dim
, enum isl_dim_type type
,
900 unsigned first
, unsigned n
, __isl_take isl_id
**ids
)
904 for (i
= 0; i
< n
; ++i
)
905 dim
= set_id(dim
, type
, first
+ i
, ids
[i
]);
910 struct isl_dim
*isl_dim_reverse(struct isl_dim
*dim
)
919 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
922 dim
= isl_dim_cow(dim
);
926 id
= dim
->tuple_id
[0];
927 dim
->tuple_id
[0] = dim
->tuple_id
[1];
928 dim
->tuple_id
[1] = id
;
930 nested
= dim
->nested
[0];
931 dim
->nested
[0] = dim
->nested
[1];
932 dim
->nested
[1] = nested
;
935 ids
= isl_alloc_array(dim
->ctx
, isl_id
*,
936 dim
->n_in
+ dim
->n_out
);
939 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
);
940 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->n_in
);
944 dim
->n_in
= dim
->n_out
;
948 dim
= set_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
);
949 dim
= set_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
+ dim
->n_out
);
960 struct isl_dim
*isl_dim_drop(struct isl_dim
*dim
, enum isl_dim_type type
,
961 unsigned first
, unsigned num
)
969 return isl_dim_reset(dim
, type
);
971 if (!valid_dim_type(type
))
972 isl_die(dim
->ctx
, isl_error_invalid
,
973 "cannot drop dimensions of specified type", goto error
);
975 isl_assert(dim
->ctx
, first
+ num
<= n(dim
, type
), goto error
);
976 dim
= isl_dim_cow(dim
);
980 dim
= extend_ids(dim
);
983 for (i
= 0; i
< num
; ++i
)
984 isl_id_free(get_id(dim
, type
, first
+ i
));
985 for (i
= first
+num
; i
< n(dim
, type
); ++i
)
986 set_id(dim
, type
, i
- num
, get_id(dim
, type
, i
));
989 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
,
990 dim
->ids
+ offset(dim
, isl_dim_in
) - num
);
992 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
,
993 dim
->ids
+ offset(dim
, isl_dim_out
) - num
);
1000 case isl_dim_param
: dim
->nparam
-= num
; break;
1001 case isl_dim_in
: dim
->n_in
-= num
; break;
1002 case isl_dim_out
: dim
->n_out
-= num
; break;
1005 dim
= isl_dim_reset(dim
, type
);
1006 if (type
== isl_dim_param
) {
1007 if (dim
&& dim
->nested
[0] &&
1008 !(dim
->nested
[0] = isl_dim_drop(dim
->nested
[0],
1009 isl_dim_param
, first
, num
)))
1011 if (dim
&& dim
->nested
[1] &&
1012 !(dim
->nested
[1] = isl_dim_drop(dim
->nested
[1],
1013 isl_dim_param
, first
, num
)))
1022 struct isl_dim
*isl_dim_drop_inputs(struct isl_dim
*dim
,
1023 unsigned first
, unsigned n
)
1027 return isl_dim_drop(dim
, isl_dim_in
, first
, n
);
1030 struct isl_dim
*isl_dim_drop_outputs(struct isl_dim
*dim
,
1031 unsigned first
, unsigned n
)
1035 return isl_dim_drop(dim
, isl_dim_out
, first
, n
);
1038 struct isl_dim
*isl_dim_domain(struct isl_dim
*dim
)
1042 dim
= isl_dim_drop_outputs(dim
, 0, dim
->n_out
);
1043 return isl_dim_reverse(dim
);
1046 __isl_give isl_dim
*isl_dim_from_domain(__isl_take isl_dim
*dim
)
1048 return isl_dim_reverse(dim
);
1051 struct isl_dim
*isl_dim_range(struct isl_dim
*dim
)
1055 return isl_dim_drop_inputs(dim
, 0, dim
->n_in
);
1058 __isl_give isl_dim
*isl_dim_from_range(__isl_take isl_dim
*dim
)
1063 __isl_give isl_dim
*isl_dim_as_set_dim(__isl_take isl_dim
*dim
)
1065 dim
= isl_dim_cow(dim
);
1069 dim
->n_out
+= dim
->n_in
;
1071 dim
= isl_dim_reset(dim
, isl_dim_in
);
1072 dim
= isl_dim_reset(dim
, isl_dim_out
);
1077 struct isl_dim
*isl_dim_underlying(struct isl_dim
*dim
, unsigned n_div
)
1084 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_id
== 0)
1085 return isl_dim_reset(isl_dim_reset(dim
, isl_dim_in
), isl_dim_out
);
1086 dim
= isl_dim_cow(dim
);
1089 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
1093 for (i
= 0; i
< dim
->n_id
; ++i
)
1094 isl_id_free(get_id(dim
, isl_dim_out
, i
));
1096 dim
= isl_dim_reset(dim
, isl_dim_in
);
1097 dim
= isl_dim_reset(dim
, isl_dim_out
);
1102 unsigned isl_dim_total(struct isl_dim
*dim
)
1104 return dim
? dim
->nparam
+ dim
->n_in
+ dim
->n_out
: 0;
1107 int isl_dim_equal(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
1109 return match(dim1
, isl_dim_param
, dim2
, isl_dim_param
) &&
1110 isl_dim_tuple_match(dim1
, isl_dim_in
, dim2
, isl_dim_in
) &&
1111 isl_dim_tuple_match(dim1
, isl_dim_out
, dim2
, isl_dim_out
);
1114 int isl_dim_compatible(struct isl_dim
*dim1
, struct isl_dim
*dim2
)
1116 return dim1
->nparam
== dim2
->nparam
&&
1117 dim1
->n_in
+ dim1
->n_out
== dim2
->n_in
+ dim2
->n_out
;
1120 static uint32_t isl_hash_dim(uint32_t hash
, __isl_keep isl_dim
*dim
)
1128 hash
= isl_hash_builtin(hash
, dim
->nparam
);
1129 hash
= isl_hash_builtin(hash
, dim
->n_in
);
1130 hash
= isl_hash_builtin(hash
, dim
->n_out
);
1132 for (i
= 0; i
< dim
->nparam
; ++i
) {
1133 id
= get_id(dim
, isl_dim_param
, i
);
1134 hash
= isl_hash_id(hash
, id
);
1137 id
= tuple_id(dim
, isl_dim_in
);
1138 hash
= isl_hash_id(hash
, id
);
1139 id
= tuple_id(dim
, isl_dim_out
);
1140 hash
= isl_hash_id(hash
, id
);
1142 hash
= isl_hash_dim(hash
, dim
->nested
[0]);
1143 hash
= isl_hash_dim(hash
, dim
->nested
[1]);
1148 uint32_t isl_dim_get_hash(__isl_keep isl_dim
*dim
)
1155 hash
= isl_hash_init();
1156 hash
= isl_hash_dim(hash
, dim
);
1161 int isl_dim_is_wrapping(__isl_keep isl_dim
*dim
)
1166 if (dim
->n_in
!= 0 || dim
->tuple_id
[0] || dim
->nested
[0])
1169 return dim
->nested
[1] != NULL
;
1172 __isl_give isl_dim
*isl_dim_wrap(__isl_take isl_dim
*dim
)
1179 wrap
= isl_dim_alloc(dim
->ctx
, dim
->nparam
, 0, dim
->n_in
+ dim
->n_out
);
1181 wrap
= copy_ids(wrap
, isl_dim_param
, 0, dim
, isl_dim_param
);
1182 wrap
= copy_ids(wrap
, isl_dim_set
, 0, dim
, isl_dim_in
);
1183 wrap
= copy_ids(wrap
, isl_dim_set
, dim
->n_in
, dim
, isl_dim_out
);
1188 wrap
->nested
[1] = dim
;
1196 __isl_give isl_dim
*isl_dim_unwrap(__isl_take isl_dim
*dim
)
1203 if (!isl_dim_is_wrapping(dim
))
1204 isl_die(dim
->ctx
, isl_error_invalid
, "not a wrapping dim",
1207 unwrap
= isl_dim_copy(dim
->nested
[1]);
1216 int isl_dim_is_named_or_nested(__isl_keep isl_dim
*dim
, enum isl_dim_type type
)
1218 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
1222 if (dim
->tuple_id
[type
- isl_dim_in
])
1224 if (dim
->nested
[type
- isl_dim_in
])
1229 int isl_dim_may_be_set(__isl_keep isl_dim
*dim
)
1233 if (isl_dim_size(dim
, isl_dim_in
) != 0)
1235 if (isl_dim_is_named_or_nested(dim
, isl_dim_in
))
1240 __isl_give isl_dim
*isl_dim_reset(__isl_take isl_dim
*dim
,
1241 enum isl_dim_type type
)
1243 if (!isl_dim_is_named_or_nested(dim
, type
))
1246 dim
= isl_dim_cow(dim
);
1250 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
1251 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
1252 isl_dim_free(dim
->nested
[type
- isl_dim_in
]);
1253 dim
->nested
[type
- isl_dim_in
] = NULL
;
1258 __isl_give isl_dim
*isl_dim_flatten(__isl_take isl_dim
*dim
)
1262 if (!dim
->nested
[0] && !dim
->nested
[1])
1266 dim
= isl_dim_reset(dim
, isl_dim_in
);
1267 if (dim
&& dim
->nested
[1])
1268 dim
= isl_dim_reset(dim
, isl_dim_out
);
1273 __isl_give isl_dim
*isl_dim_flatten_range(__isl_take isl_dim
*dim
)
1277 if (!dim
->nested
[1])
1280 return isl_dim_reset(dim
, isl_dim_out
);
1283 /* Replace the dimensions of the given type of dst by those of src.
1285 __isl_give isl_dim
*isl_dim_replace(__isl_take isl_dim
*dst
,
1286 enum isl_dim_type type
, __isl_keep isl_dim
*src
)
1288 dst
= isl_dim_cow(dst
);
1293 dst
= isl_dim_drop(dst
, type
, 0, isl_dim_size(dst
, type
));
1294 dst
= isl_dim_add(dst
, type
, isl_dim_size(src
, type
));
1295 dst
= copy_ids(dst
, type
, 0, src
, type
);
1297 if (dst
&& type
== isl_dim_param
) {
1299 for (i
= 0; i
<= 1; ++i
) {
1300 if (!dst
->nested
[i
])
1302 dst
->nested
[i
] = isl_dim_replace(dst
->nested
[i
],
1304 if (!dst
->nested
[i
])
1315 /* Given a dimension specification "dim" of a set, create a dimension
1316 * specification for the lift of the set. In particular, the result
1317 * is of the form [dim -> local[..]], with n_local variables in the
1318 * range of the wrapped map.
1320 __isl_give isl_dim
*isl_dim_lift(__isl_take isl_dim
*dim
, unsigned n_local
)
1327 local_dim
= isl_dim_dup(dim
);
1328 local_dim
= isl_dim_drop(local_dim
, isl_dim_set
, 0, dim
->n_out
);
1329 local_dim
= isl_dim_add(local_dim
, isl_dim_set
, n_local
);
1330 local_dim
= isl_dim_set_tuple_name(local_dim
, isl_dim_set
, "local");
1331 dim
= isl_dim_join(isl_dim_from_domain(dim
),
1332 isl_dim_from_range(local_dim
));
1333 dim
= isl_dim_wrap(dim
);
1334 dim
= isl_dim_set_tuple_name(dim
, isl_dim_set
, "lifted");
1339 int isl_dim_can_zip(__isl_keep isl_dim
*dim
)
1344 return dim
->nested
[0] && dim
->nested
[1];
1347 __isl_give isl_dim
*isl_dim_zip(__isl_take isl_dim
*dim
)
1350 isl_dim
*dom_dom
, *dom_ran
, *ran_dom
, *ran_ran
;
1352 if (!isl_dim_can_zip(dim
))
1353 isl_die(dim
->ctx
, isl_error_invalid
, "dim cannot be zipped",
1358 dom
= isl_dim_unwrap(isl_dim_domain(isl_dim_copy(dim
)));
1359 ran
= isl_dim_unwrap(isl_dim_range(dim
));
1360 dom_dom
= isl_dim_domain(isl_dim_copy(dom
));
1361 dom_ran
= isl_dim_range(dom
);
1362 ran_dom
= isl_dim_domain(isl_dim_copy(ran
));
1363 ran_ran
= isl_dim_range(ran
);
1364 dom
= isl_dim_join(isl_dim_from_domain(dom_dom
),
1365 isl_dim_from_range(ran_dom
));
1366 ran
= isl_dim_join(isl_dim_from_domain(dom_ran
),
1367 isl_dim_from_range(ran_ran
));
1368 return isl_dim_join(isl_dim_from_domain(isl_dim_wrap(dom
)),
1369 isl_dim_from_range(isl_dim_wrap(ran
)));
1375 int isl_dim_has_named_params(__isl_keep isl_dim
*dim
)
1382 if (dim
->nparam
== 0)
1384 off
= isl_dim_offset(dim
, isl_dim_param
);
1385 if (off
+ dim
->nparam
> dim
->n_id
)
1387 for (i
= 0; i
< dim
->nparam
; ++i
)
1388 if (!dim
->ids
[off
+ i
])
1393 /* Align the initial parameters of dim1 to match the order in dim2.
1395 __isl_give isl_dim
*isl_dim_align_params(__isl_take isl_dim
*dim1
,
1396 __isl_take isl_dim
*dim2
)
1398 isl_reordering
*exp
;
1400 if (!isl_dim_has_named_params(dim1
) || !isl_dim_has_named_params(dim2
))
1401 isl_die(isl_dim_get_ctx(dim1
), isl_error_invalid
,
1402 "parameter alignment requires named parameters",
1405 exp
= isl_parameter_alignment_reordering(dim1
, dim2
);
1410 dim1
= isl_dim_copy(exp
->dim
);
1411 isl_reordering_free(exp
);