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_space_private.h>
15 #include <isl_id_private.h>
16 #include <isl_reordering.h>
18 isl_ctx
*isl_space_get_ctx(__isl_keep isl_space
*dim
)
20 return dim
? dim
->ctx
: NULL
;
23 __isl_give isl_space
*isl_space_alloc(isl_ctx
*ctx
,
24 unsigned nparam
, unsigned n_in
, unsigned n_out
)
28 dim
= isl_alloc_type(ctx
, struct isl_space
);
39 dim
->tuple_id
[0] = NULL
;
40 dim
->tuple_id
[1] = NULL
;
42 dim
->nested
[0] = NULL
;
43 dim
->nested
[1] = NULL
;
51 __isl_give isl_space
*isl_space_set_alloc(isl_ctx
*ctx
,
52 unsigned nparam
, unsigned dim
)
54 return isl_space_alloc(ctx
, nparam
, 0, dim
);
57 static unsigned global_pos(__isl_keep isl_space
*dim
,
58 enum isl_dim_type type
, unsigned pos
)
60 struct isl_ctx
*ctx
= dim
->ctx
;
64 isl_assert(ctx
, pos
< dim
->nparam
,
65 return isl_space_dim(dim
, isl_dim_all
));
68 isl_assert(ctx
, pos
< dim
->n_in
,
69 return isl_space_dim(dim
, isl_dim_all
));
70 return pos
+ dim
->nparam
;
72 isl_assert(ctx
, pos
< dim
->n_out
,
73 return isl_space_dim(dim
, isl_dim_all
));
74 return pos
+ dim
->nparam
+ dim
->n_in
;
76 isl_assert(ctx
, 0, return isl_space_dim(dim
, isl_dim_all
));
78 return isl_space_dim(dim
, isl_dim_all
);
81 /* Extend length of ids array to the total number of dimensions.
83 static __isl_give isl_space
*extend_ids(__isl_take isl_space
*dim
)
88 if (isl_space_dim(dim
, isl_dim_all
) <= dim
->n_id
)
92 dim
->ids
= isl_calloc_array(dim
->ctx
,
93 isl_id
*, isl_space_dim(dim
, isl_dim_all
));
97 ids
= isl_realloc_array(dim
->ctx
, dim
->ids
,
98 isl_id
*, isl_space_dim(dim
, isl_dim_all
));
102 for (i
= dim
->n_id
; i
< isl_space_dim(dim
, isl_dim_all
); ++i
)
106 dim
->n_id
= isl_space_dim(dim
, isl_dim_all
);
114 static __isl_give isl_space
*set_id(__isl_take isl_space
*dim
,
115 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
117 dim
= isl_space_cow(dim
);
122 pos
= global_pos(dim
, type
, pos
);
123 if (pos
== isl_space_dim(dim
, isl_dim_all
))
126 if (pos
>= dim
->n_id
) {
129 dim
= extend_ids(dim
);
143 static __isl_keep isl_id
*get_id(__isl_keep isl_space
*dim
,
144 enum isl_dim_type type
, unsigned pos
)
149 pos
= global_pos(dim
, type
, pos
);
150 if (pos
== isl_space_dim(dim
, isl_dim_all
))
152 if (pos
>= dim
->n_id
)
154 return dim
->ids
[pos
];
157 static unsigned offset(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
160 case isl_dim_param
: return 0;
161 case isl_dim_in
: return dim
->nparam
;
162 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
167 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
170 case isl_dim_param
: return dim
->nparam
;
171 case isl_dim_in
: return dim
->n_in
;
172 case isl_dim_out
: return dim
->n_out
;
173 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
178 unsigned isl_space_dim(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
185 unsigned isl_space_offset(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
189 return offset(dim
, type
);
192 static __isl_give isl_space
*copy_ids(__isl_take isl_space
*dst
,
193 enum isl_dim_type dst_type
, unsigned offset
, __isl_keep isl_space
*src
,
194 enum isl_dim_type src_type
)
202 for (i
= 0; i
< n(src
, src_type
); ++i
) {
203 id
= get_id(src
, src_type
, i
);
206 dst
= set_id(dst
, dst_type
, offset
+ i
, isl_id_copy(id
));
213 __isl_take isl_space
*isl_space_dup(__isl_keep isl_space
*dim
)
218 dup
= isl_space_alloc(dim
->ctx
, dim
->nparam
, dim
->n_in
, dim
->n_out
);
219 if (dim
->tuple_id
[0] &&
220 !(dup
->tuple_id
[0] = isl_id_copy(dim
->tuple_id
[0])))
222 if (dim
->tuple_id
[1] &&
223 !(dup
->tuple_id
[1] = isl_id_copy(dim
->tuple_id
[1])))
225 if (dim
->nested
[0] && !(dup
->nested
[0] = isl_space_copy(dim
->nested
[0])))
227 if (dim
->nested
[1] && !(dup
->nested
[1] = isl_space_copy(dim
->nested
[1])))
231 dup
= copy_ids(dup
, isl_dim_param
, 0, dim
, isl_dim_param
);
232 dup
= copy_ids(dup
, isl_dim_in
, 0, dim
, isl_dim_in
);
233 dup
= copy_ids(dup
, isl_dim_out
, 0, dim
, isl_dim_out
);
240 __isl_give isl_space
*isl_space_cow(__isl_take isl_space
*dim
)
248 return isl_space_dup(dim
);
251 __isl_give isl_space
*isl_space_copy(__isl_keep isl_space
*dim
)
260 void isl_space_free(__isl_take isl_space
*dim
)
270 isl_id_free(dim
->tuple_id
[0]);
271 isl_id_free(dim
->tuple_id
[1]);
273 isl_space_free(dim
->nested
[0]);
274 isl_space_free(dim
->nested
[1]);
276 for (i
= 0; i
< dim
->n_id
; ++i
)
277 isl_id_free(dim
->ids
[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 int isl_space_has_tuple_id(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
301 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
302 isl_die(dim
->ctx
, isl_error_invalid
,
303 "only input, output and set tuples can have ids",
305 return dim
->tuple_id
[type
- isl_dim_in
] != NULL
;
308 __isl_give isl_id
*isl_space_get_tuple_id(__isl_keep isl_space
*dim
,
309 enum isl_dim_type type
)
313 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
314 isl_die(dim
->ctx
, isl_error_invalid
,
315 "only input, output and set tuples can have ids",
317 if (!dim
->tuple_id
[type
- isl_dim_in
])
318 isl_die(dim
->ctx
, isl_error_invalid
,
319 "tuple has no id", return NULL
);
320 return isl_id_copy(dim
->tuple_id
[type
- isl_dim_in
]);
323 __isl_give isl_space
*isl_space_set_tuple_id(__isl_take isl_space
*dim
,
324 enum isl_dim_type type
, __isl_take isl_id
*id
)
326 dim
= isl_space_cow(dim
);
329 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
330 isl_die(dim
->ctx
, isl_error_invalid
,
331 "only input, output and set tuples can have names",
334 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
335 dim
->tuple_id
[type
- isl_dim_in
] = id
;
344 __isl_give isl_space
*isl_space_reset_tuple_id(__isl_take isl_space
*dim
,
345 enum isl_dim_type type
)
347 dim
= isl_space_cow(dim
);
350 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
351 isl_die(dim
->ctx
, isl_error_invalid
,
352 "only input, output and set tuples can have names",
355 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
356 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
364 __isl_give isl_space
*isl_space_set_dim_id(__isl_take isl_space
*dim
,
365 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
367 dim
= isl_space_cow(dim
);
370 isl_id_free(get_id(dim
, type
, pos
));
371 return set_id(dim
, type
, pos
, id
);
378 int isl_space_has_dim_id(__isl_keep isl_space
*dim
,
379 enum isl_dim_type type
, unsigned pos
)
383 return get_id(dim
, type
, pos
) != NULL
;
386 __isl_give isl_id
*isl_space_get_dim_id(__isl_keep isl_space
*dim
,
387 enum isl_dim_type type
, unsigned pos
)
391 if (!get_id(dim
, type
, pos
))
392 isl_die(dim
->ctx
, isl_error_invalid
,
393 "dim has no id", return NULL
);
394 return isl_id_copy(get_id(dim
, type
, pos
));
397 __isl_give isl_space
*isl_space_set_tuple_name(__isl_take isl_space
*dim
,
398 enum isl_dim_type type
, const char *s
)
406 return isl_space_reset_tuple_id(dim
, type
);
408 if (!name_ok(dim
->ctx
, s
))
411 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
412 return isl_space_set_tuple_id(dim
, type
, id
);
418 const char *isl_space_get_tuple_name(__isl_keep isl_space
*dim
,
419 enum isl_dim_type type
)
424 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
426 id
= dim
->tuple_id
[type
- isl_dim_in
];
427 return id
? id
->name
: NULL
;
430 __isl_give isl_space
*isl_space_set_dim_name(__isl_take isl_space
*dim
,
431 enum isl_dim_type type
, unsigned pos
,
438 if (!name_ok(dim
->ctx
, s
))
440 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
441 return isl_space_set_dim_id(dim
, type
, pos
, id
);
447 __isl_keep
const char *isl_space_get_dim_name(__isl_keep isl_space
*dim
,
448 enum isl_dim_type type
, unsigned pos
)
450 isl_id
*id
= get_id(dim
, type
, pos
);
451 return id
? id
->name
: NULL
;
454 int isl_space_find_dim_by_id(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
455 __isl_keep isl_id
*id
)
464 offset
= isl_space_offset(dim
, type
);
465 n
= isl_space_dim(dim
, type
);
466 for (i
= 0; i
< n
&& offset
+ i
< dim
->n_id
; ++i
)
467 if (dim
->ids
[offset
+ i
] == id
)
473 static __isl_keep isl_id
*tuple_id(__isl_keep isl_space
*dim
,
474 enum isl_dim_type type
)
478 if (type
== isl_dim_in
)
479 return dim
->tuple_id
[0];
480 if (type
== isl_dim_out
)
481 return dim
->tuple_id
[1];
485 static __isl_keep isl_space
*nested(__isl_keep isl_space
*dim
,
486 enum isl_dim_type type
)
490 if (type
== isl_dim_in
)
491 return dim
->nested
[0];
492 if (type
== isl_dim_out
)
493 return dim
->nested
[1];
497 int isl_space_tuple_match(__isl_keep isl_space
*dim1
, enum isl_dim_type dim1_type
,
498 __isl_keep isl_space
*dim2
, enum isl_dim_type dim2_type
)
501 isl_space
*nested1
, *nested2
;
506 if (n(dim1
, dim1_type
) != n(dim2
, dim2_type
))
508 id1
= tuple_id(dim1
, dim1_type
);
509 id2
= tuple_id(dim2
, dim2_type
);
512 if (id1
&& id1
!= id2
)
514 nested1
= nested(dim1
, dim1_type
);
515 nested2
= nested(dim2
, dim2_type
);
516 if (!nested1
^ !nested2
)
518 if (nested1
&& !isl_space_is_equal(nested1
, nested2
))
523 static int match(__isl_keep isl_space
*dim1
, enum isl_dim_type dim1_type
,
524 __isl_keep isl_space
*dim2
, enum isl_dim_type dim2_type
)
528 if (!isl_space_tuple_match(dim1
, dim1_type
, dim2
, dim2_type
))
531 if (!dim1
->ids
&& !dim2
->ids
)
534 for (i
= 0; i
< n(dim1
, dim1_type
); ++i
) {
535 if (get_id(dim1
, dim1_type
, i
) != get_id(dim2
, dim2_type
, i
))
541 int isl_space_match(__isl_keep isl_space
*dim1
, enum isl_dim_type dim1_type
,
542 __isl_keep isl_space
*dim2
, enum isl_dim_type dim2_type
)
544 return match(dim1
, dim1_type
, dim2
, dim2_type
);
547 static void get_ids(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
548 unsigned first
, unsigned n
, __isl_keep isl_id
**ids
)
552 for (i
= 0; i
< n
; ++i
)
553 ids
[i
] = get_id(dim
, type
, first
+ i
);
556 __isl_give isl_space
*isl_space_extend(__isl_take isl_space
*dim
,
557 unsigned nparam
, unsigned n_in
, unsigned n_out
)
563 if (dim
->nparam
== nparam
&& dim
->n_in
== n_in
&& dim
->n_out
== n_out
)
566 isl_assert(dim
->ctx
, dim
->nparam
<= nparam
, goto error
);
567 isl_assert(dim
->ctx
, dim
->n_in
<= n_in
, goto error
);
568 isl_assert(dim
->ctx
, dim
->n_out
<= n_out
, goto error
);
570 dim
= isl_space_cow(dim
);
573 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
574 nparam
+ n_in
+ n_out
);
577 get_ids(dim
, isl_dim_param
, 0, dim
->nparam
, ids
);
578 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
+ nparam
);
579 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ nparam
+ n_in
);
582 dim
->n_id
= nparam
+ n_in
+ n_out
;
584 dim
->nparam
= nparam
;
595 __isl_give isl_space
*isl_space_add_dims(__isl_take isl_space
*dim
,
596 enum isl_dim_type type
, unsigned n
)
600 dim
= isl_space_reset(dim
, type
);
603 dim
= isl_space_extend(dim
,
604 dim
->nparam
+ n
, dim
->n_in
, dim
->n_out
);
605 if (dim
&& dim
->nested
[0] &&
606 !(dim
->nested
[0] = isl_space_add_dims(dim
->nested
[0],
609 if (dim
&& dim
->nested
[1] &&
610 !(dim
->nested
[1] = isl_space_add_dims(dim
->nested
[1],
615 return isl_space_extend(dim
,
616 dim
->nparam
, dim
->n_in
+ n
, dim
->n_out
);
618 return isl_space_extend(dim
,
619 dim
->nparam
, dim
->n_in
, dim
->n_out
+ n
);
621 isl_die(dim
->ctx
, isl_error_invalid
,
622 "cannot add dimensions of specified type", goto error
);
629 static int valid_dim_type(enum isl_dim_type type
)
641 __isl_give isl_space
*isl_space_insert_dims(__isl_take isl_space
*dim
,
642 enum isl_dim_type type
, unsigned pos
, unsigned n
)
649 return isl_space_reset(dim
, type
);
651 if (!valid_dim_type(type
))
652 isl_die(dim
->ctx
, isl_error_invalid
,
653 "cannot insert dimensions of specified type",
656 isl_assert(dim
->ctx
, pos
<= isl_space_dim(dim
, type
), goto error
);
658 dim
= isl_space_cow(dim
);
666 int *size
= s
- isl_dim_param
;
667 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
668 dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
);
672 size
[isl_dim_param
] = dim
->nparam
;
673 size
[isl_dim_in
] = dim
->n_in
;
674 size
[isl_dim_out
] = dim
->n_out
;
675 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
677 get_ids(dim
, t
, 0, size
[t
], ids
+ off
);
680 get_ids(dim
, t
, 0, pos
, ids
+ off
);
682 get_ids(dim
, t
, pos
, size
[t
] - pos
, ids
+ off
);
683 off
+= size
[t
] - pos
;
688 dim
->n_id
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
;
691 case isl_dim_param
: dim
->nparam
+= n
; break;
692 case isl_dim_in
: dim
->n_in
+= n
; break;
693 case isl_dim_out
: dim
->n_out
+= n
; break;
696 dim
= isl_space_reset(dim
, type
);
704 __isl_give isl_space
*isl_space_move_dims(__isl_take isl_space
*dim
,
705 enum isl_dim_type dst_type
, unsigned dst_pos
,
706 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
715 isl_assert(dim
->ctx
, src_pos
+ n
<= isl_space_dim(dim
, src_type
),
718 if (dst_type
== src_type
&& dst_pos
== src_pos
)
721 isl_assert(dim
->ctx
, dst_type
!= src_type
, goto error
);
723 dim
= isl_space_reset(dim
, src_type
);
724 dim
= isl_space_reset(dim
, dst_type
);
726 dim
= isl_space_cow(dim
);
735 int *size
= s
- isl_dim_param
;
736 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
737 dim
->nparam
+ dim
->n_in
+ dim
->n_out
);
741 size
[isl_dim_param
] = dim
->nparam
;
742 size
[isl_dim_in
] = dim
->n_in
;
743 size
[isl_dim_out
] = dim
->n_out
;
744 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
746 get_ids(dim
, t
, 0, dst_pos
, ids
+ off
);
748 get_ids(dim
, src_type
, src_pos
, n
, ids
+ off
);
750 get_ids(dim
, t
, dst_pos
, size
[t
] - dst_pos
,
752 off
+= size
[t
] - dst_pos
;
753 } else if (t
== src_type
) {
754 get_ids(dim
, t
, 0, src_pos
, ids
+ off
);
756 get_ids(dim
, t
, src_pos
+ n
,
757 size
[t
] - src_pos
- n
, ids
+ off
);
758 off
+= size
[t
] - src_pos
- n
;
760 get_ids(dim
, t
, 0, size
[t
], ids
+ off
);
766 dim
->n_id
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
770 case isl_dim_param
: dim
->nparam
+= n
; break;
771 case isl_dim_in
: dim
->n_in
+= n
; break;
772 case isl_dim_out
: dim
->n_out
+= n
; break;
777 case isl_dim_param
: dim
->nparam
-= n
; break;
778 case isl_dim_in
: dim
->n_in
-= n
; break;
779 case isl_dim_out
: dim
->n_out
-= n
; break;
783 if (dst_type
!= isl_dim_param
&& src_type
!= isl_dim_param
)
786 for (i
= 0; i
< 2; ++i
) {
789 dim
->nested
[i
] = isl_space_replace(dim
->nested
[i
],
801 __isl_give isl_space
*isl_space_join(__isl_take isl_space
*left
,
802 __isl_take isl_space
*right
)
809 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
811 isl_assert(left
->ctx
,
812 isl_space_tuple_match(left
, isl_dim_out
, right
, isl_dim_in
),
815 dim
= isl_space_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
819 dim
= copy_ids(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
820 dim
= copy_ids(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
821 dim
= copy_ids(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
823 if (dim
&& left
->tuple_id
[0] &&
824 !(dim
->tuple_id
[0] = isl_id_copy(left
->tuple_id
[0])))
826 if (dim
&& right
->tuple_id
[1] &&
827 !(dim
->tuple_id
[1] = isl_id_copy(right
->tuple_id
[1])))
829 if (dim
&& left
->nested
[0] &&
830 !(dim
->nested
[0] = isl_space_copy(left
->nested
[0])))
832 if (dim
&& right
->nested
[1] &&
833 !(dim
->nested
[1] = isl_space_copy(right
->nested
[1])))
836 isl_space_free(left
);
837 isl_space_free(right
);
841 isl_space_free(left
);
842 isl_space_free(right
);
846 __isl_give isl_space
*isl_space_product(__isl_take isl_space
*left
,
847 __isl_take isl_space
*right
)
849 isl_space
*dom1
, *dom2
, *nest1
, *nest2
;
854 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
857 dom1
= isl_space_domain(isl_space_copy(left
));
858 dom2
= isl_space_domain(isl_space_copy(right
));
859 nest1
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
861 dom1
= isl_space_range(left
);
862 dom2
= isl_space_range(right
);
863 nest2
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
865 return isl_space_join(isl_space_reverse(nest1
), nest2
);
867 isl_space_free(left
);
868 isl_space_free(right
);
872 /* Given two spaces { A -> C } and { B -> C }, construct the space
875 __isl_give isl_space
*isl_space_domain_product(__isl_take isl_space
*left
,
876 __isl_take isl_space
*right
)
878 isl_space
*ran
, *dom1
, *dom2
, *nest
;
883 if (!match(left
, isl_dim_param
, right
, isl_dim_param
))
884 isl_die(left
->ctx
, isl_error_invalid
,
885 "parameters need to match", goto error
);
886 if (!isl_space_tuple_match(left
, isl_dim_out
, right
, isl_dim_out
))
887 isl_die(left
->ctx
, isl_error_invalid
,
888 "ranges need to match", goto error
);
890 ran
= isl_space_range(isl_space_copy(left
));
892 dom1
= isl_space_domain(left
);
893 dom2
= isl_space_domain(right
);
894 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
896 return isl_space_join(isl_space_reverse(nest
), ran
);
898 isl_space_free(left
);
899 isl_space_free(right
);
903 __isl_give isl_space
*isl_space_range_product(__isl_take isl_space
*left
,
904 __isl_take isl_space
*right
)
906 isl_space
*dom
, *ran1
, *ran2
, *nest
;
911 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
913 if (!isl_space_tuple_match(left
, isl_dim_in
, right
, isl_dim_in
))
914 isl_die(left
->ctx
, isl_error_invalid
,
915 "domains need to match", goto error
);
917 dom
= isl_space_domain(isl_space_copy(left
));
919 ran1
= isl_space_range(left
);
920 ran2
= isl_space_range(right
);
921 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(ran1
), ran2
));
923 return isl_space_join(isl_space_reverse(dom
), nest
);
925 isl_space_free(left
);
926 isl_space_free(right
);
930 __isl_give isl_space
*isl_space_map_from_set(__isl_take isl_space
*dim
)
936 isl_assert(dim
->ctx
, dim
->n_in
== 0, goto error
);
937 if (dim
->n_out
== 0 && !isl_space_is_named_or_nested(dim
, isl_dim_out
))
939 dim
= isl_space_cow(dim
);
943 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
944 dim
->nparam
+ dim
->n_out
+ dim
->n_out
);
947 get_ids(dim
, isl_dim_param
, 0, dim
->nparam
, ids
);
948 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->nparam
);
950 dim
->n_in
= dim
->n_out
;
954 dim
->n_id
= dim
->nparam
+ dim
->n_out
+ dim
->n_out
;
955 dim
= copy_ids(dim
, isl_dim_out
, 0, dim
, isl_dim_in
);
957 isl_id_free(dim
->tuple_id
[0]);
958 dim
->tuple_id
[0] = isl_id_copy(dim
->tuple_id
[1]);
959 isl_space_free(dim
->nested
[0]);
960 dim
->nested
[0] = isl_space_copy(dim
->nested
[1]);
967 static __isl_give isl_space
*set_ids(__isl_take isl_space
*dim
,
968 enum isl_dim_type type
,
969 unsigned first
, unsigned n
, __isl_take isl_id
**ids
)
973 for (i
= 0; i
< n
; ++i
)
974 dim
= set_id(dim
, type
, first
+ i
, ids
[i
]);
979 __isl_give isl_space
*isl_space_reverse(__isl_take isl_space
*dim
)
988 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
991 dim
= isl_space_cow(dim
);
995 id
= dim
->tuple_id
[0];
996 dim
->tuple_id
[0] = dim
->tuple_id
[1];
997 dim
->tuple_id
[1] = id
;
999 nested
= dim
->nested
[0];
1000 dim
->nested
[0] = dim
->nested
[1];
1001 dim
->nested
[1] = nested
;
1004 ids
= isl_alloc_array(dim
->ctx
, isl_id
*,
1005 dim
->n_in
+ dim
->n_out
);
1008 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
);
1009 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->n_in
);
1013 dim
->n_in
= dim
->n_out
;
1017 dim
= set_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
);
1018 dim
= set_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
+ dim
->n_out
);
1025 isl_space_free(dim
);
1029 __isl_give isl_space
*isl_space_drop_dims(__isl_take isl_space
*dim
,
1030 enum isl_dim_type type
, unsigned first
, unsigned num
)
1038 return isl_space_reset(dim
, type
);
1040 if (!valid_dim_type(type
))
1041 isl_die(dim
->ctx
, isl_error_invalid
,
1042 "cannot drop dimensions of specified type", goto error
);
1044 isl_assert(dim
->ctx
, first
+ num
<= n(dim
, type
), goto error
);
1045 dim
= isl_space_cow(dim
);
1049 dim
= extend_ids(dim
);
1052 for (i
= 0; i
< num
; ++i
)
1053 isl_id_free(get_id(dim
, type
, first
+ i
));
1054 for (i
= first
+num
; i
< n(dim
, type
); ++i
)
1055 set_id(dim
, type
, i
- num
, get_id(dim
, type
, i
));
1058 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
,
1059 dim
->ids
+ offset(dim
, isl_dim_in
) - num
);
1061 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
,
1062 dim
->ids
+ offset(dim
, isl_dim_out
) - num
);
1069 case isl_dim_param
: dim
->nparam
-= num
; break;
1070 case isl_dim_in
: dim
->n_in
-= num
; break;
1071 case isl_dim_out
: dim
->n_out
-= num
; break;
1074 dim
= isl_space_reset(dim
, type
);
1075 if (type
== isl_dim_param
) {
1076 if (dim
&& dim
->nested
[0] &&
1077 !(dim
->nested
[0] = isl_space_drop_dims(dim
->nested
[0],
1078 isl_dim_param
, first
, num
)))
1080 if (dim
&& dim
->nested
[1] &&
1081 !(dim
->nested
[1] = isl_space_drop_dims(dim
->nested
[1],
1082 isl_dim_param
, first
, num
)))
1087 isl_space_free(dim
);
1091 __isl_give isl_space
*isl_space_drop_inputs(__isl_take isl_space
*dim
,
1092 unsigned first
, unsigned n
)
1096 return isl_space_drop_dims(dim
, isl_dim_in
, first
, n
);
1099 __isl_give isl_space
*isl_space_drop_outputs(__isl_take isl_space
*dim
,
1100 unsigned first
, unsigned n
)
1104 return isl_space_drop_dims(dim
, isl_dim_out
, first
, n
);
1107 __isl_give isl_space
*isl_space_domain(__isl_take isl_space
*dim
)
1111 dim
= isl_space_drop_outputs(dim
, 0, dim
->n_out
);
1112 return isl_space_reverse(dim
);
1115 __isl_give isl_space
*isl_space_from_domain(__isl_take isl_space
*dim
)
1117 return isl_space_reverse(dim
);
1120 __isl_give isl_space
*isl_space_range(__isl_take isl_space
*dim
)
1124 return isl_space_drop_inputs(dim
, 0, dim
->n_in
);
1127 __isl_give isl_space
*isl_space_from_range(__isl_take isl_space
*dim
)
1132 __isl_give isl_space
*isl_space_params(__isl_take isl_space
*space
)
1134 space
= isl_space_drop_dims(space
,
1135 isl_dim_in
, 0, isl_space_dim(space
, isl_dim_in
));
1136 space
= isl_space_drop_dims(space
,
1137 isl_dim_out
, 0, isl_space_dim(space
, isl_dim_out
));
1141 __isl_give isl_space
*isl_space_as_set_space(__isl_take isl_space
*dim
)
1143 dim
= isl_space_cow(dim
);
1147 dim
->n_out
+= dim
->n_in
;
1149 dim
= isl_space_reset(dim
, isl_dim_in
);
1150 dim
= isl_space_reset(dim
, isl_dim_out
);
1155 __isl_give isl_space
*isl_space_underlying(__isl_take isl_space
*dim
,
1163 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_id
== 0)
1164 return isl_space_reset(isl_space_reset(dim
, isl_dim_in
), isl_dim_out
);
1165 dim
= isl_space_cow(dim
);
1168 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
1172 for (i
= 0; i
< dim
->n_id
; ++i
)
1173 isl_id_free(get_id(dim
, isl_dim_out
, i
));
1175 dim
= isl_space_reset(dim
, isl_dim_in
);
1176 dim
= isl_space_reset(dim
, isl_dim_out
);
1181 int isl_space_is_equal(__isl_keep isl_space
*dim1
, __isl_keep isl_space
*dim2
)
1185 return match(dim1
, isl_dim_param
, dim2
, isl_dim_param
) &&
1186 isl_space_tuple_match(dim1
, isl_dim_in
, dim2
, isl_dim_in
) &&
1187 isl_space_tuple_match(dim1
, isl_dim_out
, dim2
, isl_dim_out
);
1190 int isl_space_compatible(__isl_keep isl_space
*dim1
,
1191 __isl_keep isl_space
*dim2
)
1193 return dim1
->nparam
== dim2
->nparam
&&
1194 dim1
->n_in
+ dim1
->n_out
== dim2
->n_in
+ dim2
->n_out
;
1197 static uint32_t isl_hash_dim(uint32_t hash
, __isl_keep isl_space
*dim
)
1205 hash
= isl_hash_builtin(hash
, dim
->nparam
);
1206 hash
= isl_hash_builtin(hash
, dim
->n_in
);
1207 hash
= isl_hash_builtin(hash
, dim
->n_out
);
1209 for (i
= 0; i
< dim
->nparam
; ++i
) {
1210 id
= get_id(dim
, isl_dim_param
, i
);
1211 hash
= isl_hash_id(hash
, id
);
1214 id
= tuple_id(dim
, isl_dim_in
);
1215 hash
= isl_hash_id(hash
, id
);
1216 id
= tuple_id(dim
, isl_dim_out
);
1217 hash
= isl_hash_id(hash
, id
);
1219 hash
= isl_hash_dim(hash
, dim
->nested
[0]);
1220 hash
= isl_hash_dim(hash
, dim
->nested
[1]);
1225 uint32_t isl_space_get_hash(__isl_keep isl_space
*dim
)
1232 hash
= isl_hash_init();
1233 hash
= isl_hash_dim(hash
, dim
);
1238 int isl_space_is_wrapping(__isl_keep isl_space
*dim
)
1243 if (dim
->n_in
!= 0 || dim
->tuple_id
[0] || dim
->nested
[0])
1246 return dim
->nested
[1] != NULL
;
1249 __isl_give isl_space
*isl_space_wrap(__isl_take isl_space
*dim
)
1256 wrap
= isl_space_alloc(dim
->ctx
, dim
->nparam
, 0, dim
->n_in
+ dim
->n_out
);
1258 wrap
= copy_ids(wrap
, isl_dim_param
, 0, dim
, isl_dim_param
);
1259 wrap
= copy_ids(wrap
, isl_dim_set
, 0, dim
, isl_dim_in
);
1260 wrap
= copy_ids(wrap
, isl_dim_set
, dim
->n_in
, dim
, isl_dim_out
);
1265 wrap
->nested
[1] = dim
;
1269 isl_space_free(dim
);
1273 __isl_give isl_space
*isl_space_unwrap(__isl_take isl_space
*dim
)
1280 if (!isl_space_is_wrapping(dim
))
1281 isl_die(dim
->ctx
, isl_error_invalid
, "not a wrapping dim",
1284 unwrap
= isl_space_copy(dim
->nested
[1]);
1285 isl_space_free(dim
);
1289 isl_space_free(dim
);
1293 int isl_space_is_named_or_nested(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
1295 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
1299 if (dim
->tuple_id
[type
- isl_dim_in
])
1301 if (dim
->nested
[type
- isl_dim_in
])
1306 int isl_space_may_be_set(__isl_keep isl_space
*dim
)
1310 if (isl_space_dim(dim
, isl_dim_in
) != 0)
1312 if (isl_space_is_named_or_nested(dim
, isl_dim_in
))
1317 __isl_give isl_space
*isl_space_reset(__isl_take isl_space
*dim
,
1318 enum isl_dim_type type
)
1320 if (!isl_space_is_named_or_nested(dim
, type
))
1323 dim
= isl_space_cow(dim
);
1327 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
1328 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
1329 isl_space_free(dim
->nested
[type
- isl_dim_in
]);
1330 dim
->nested
[type
- isl_dim_in
] = NULL
;
1335 __isl_give isl_space
*isl_space_flatten(__isl_take isl_space
*dim
)
1339 if (!dim
->nested
[0] && !dim
->nested
[1])
1343 dim
= isl_space_reset(dim
, isl_dim_in
);
1344 if (dim
&& dim
->nested
[1])
1345 dim
= isl_space_reset(dim
, isl_dim_out
);
1350 __isl_give isl_space
*isl_space_flatten_domain(__isl_take isl_space
*dim
)
1354 if (!dim
->nested
[0])
1357 return isl_space_reset(dim
, isl_dim_in
);
1360 __isl_give isl_space
*isl_space_flatten_range(__isl_take isl_space
*dim
)
1364 if (!dim
->nested
[1])
1367 return isl_space_reset(dim
, isl_dim_out
);
1370 /* Replace the dimensions of the given type of dst by those of src.
1372 __isl_give isl_space
*isl_space_replace(__isl_take isl_space
*dst
,
1373 enum isl_dim_type type
, __isl_keep isl_space
*src
)
1375 dst
= isl_space_cow(dst
);
1380 dst
= isl_space_drop_dims(dst
, type
, 0, isl_space_dim(dst
, type
));
1381 dst
= isl_space_add_dims(dst
, type
, isl_space_dim(src
, type
));
1382 dst
= copy_ids(dst
, type
, 0, src
, type
);
1384 if (dst
&& type
== isl_dim_param
) {
1386 for (i
= 0; i
<= 1; ++i
) {
1387 if (!dst
->nested
[i
])
1389 dst
->nested
[i
] = isl_space_replace(dst
->nested
[i
],
1391 if (!dst
->nested
[i
])
1398 isl_space_free(dst
);
1402 /* Given a dimension specification "dim" of a set, create a dimension
1403 * specification for the lift of the set. In particular, the result
1404 * is of the form [dim -> local[..]], with n_local variables in the
1405 * range of the wrapped map.
1407 __isl_give isl_space
*isl_space_lift(__isl_take isl_space
*dim
, unsigned n_local
)
1409 isl_space
*local_dim
;
1414 local_dim
= isl_space_dup(dim
);
1415 local_dim
= isl_space_drop_dims(local_dim
, isl_dim_set
, 0, dim
->n_out
);
1416 local_dim
= isl_space_add_dims(local_dim
, isl_dim_set
, n_local
);
1417 local_dim
= isl_space_set_tuple_name(local_dim
, isl_dim_set
, "local");
1418 dim
= isl_space_join(isl_space_from_domain(dim
),
1419 isl_space_from_range(local_dim
));
1420 dim
= isl_space_wrap(dim
);
1421 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, "lifted");
1426 int isl_space_can_zip(__isl_keep isl_space
*dim
)
1431 return dim
->nested
[0] && dim
->nested
[1];
1434 __isl_give isl_space
*isl_space_zip(__isl_take isl_space
*dim
)
1436 isl_space
*dom
, *ran
;
1437 isl_space
*dom_dom
, *dom_ran
, *ran_dom
, *ran_ran
;
1439 if (!isl_space_can_zip(dim
))
1440 isl_die(dim
->ctx
, isl_error_invalid
, "dim cannot be zipped",
1445 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(dim
)));
1446 ran
= isl_space_unwrap(isl_space_range(dim
));
1447 dom_dom
= isl_space_domain(isl_space_copy(dom
));
1448 dom_ran
= isl_space_range(dom
);
1449 ran_dom
= isl_space_domain(isl_space_copy(ran
));
1450 ran_ran
= isl_space_range(ran
);
1451 dom
= isl_space_join(isl_space_from_domain(dom_dom
),
1452 isl_space_from_range(ran_dom
));
1453 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
1454 isl_space_from_range(ran_ran
));
1455 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
1456 isl_space_from_range(isl_space_wrap(ran
)));
1458 isl_space_free(dim
);
1462 int isl_space_has_named_params(__isl_keep isl_space
*dim
)
1469 if (dim
->nparam
== 0)
1471 off
= isl_space_offset(dim
, isl_dim_param
);
1472 if (off
+ dim
->nparam
> dim
->n_id
)
1474 for (i
= 0; i
< dim
->nparam
; ++i
)
1475 if (!dim
->ids
[off
+ i
])
1480 /* Align the initial parameters of dim1 to match the order in dim2.
1482 __isl_give isl_space
*isl_space_align_params(__isl_take isl_space
*dim1
,
1483 __isl_take isl_space
*dim2
)
1485 isl_reordering
*exp
;
1487 if (!isl_space_has_named_params(dim1
) || !isl_space_has_named_params(dim2
))
1488 isl_die(isl_space_get_ctx(dim1
), isl_error_invalid
,
1489 "parameter alignment requires named parameters",
1492 dim2
= isl_space_params(dim2
);
1493 exp
= isl_parameter_alignment_reordering(dim1
, dim2
);
1494 exp
= isl_reordering_extend_space(exp
, dim1
);
1495 isl_space_free(dim2
);
1498 dim1
= isl_space_copy(exp
->dim
);
1499 isl_reordering_free(exp
);
1502 isl_space_free(dim1
);
1503 isl_space_free(dim2
);