2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2013-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
17 #include <isl_space_private.h>
18 #include <isl_id_private.h>
19 #include <isl_reordering.h>
21 isl_ctx
*isl_space_get_ctx(__isl_keep isl_space
*dim
)
23 return dim
? dim
->ctx
: NULL
;
26 __isl_give isl_space
*isl_space_alloc(isl_ctx
*ctx
,
27 unsigned nparam
, unsigned n_in
, unsigned n_out
)
31 dim
= isl_alloc_type(ctx
, struct isl_space
);
42 dim
->tuple_id
[0] = NULL
;
43 dim
->tuple_id
[1] = NULL
;
45 dim
->nested
[0] = NULL
;
46 dim
->nested
[1] = NULL
;
54 /* Mark the space as being that of a set, by setting the domain tuple
57 static __isl_give isl_space
*mark_as_set(__isl_take isl_space
*space
)
59 space
= isl_space_cow(space
);
62 space
= isl_space_set_tuple_id(space
, isl_dim_in
, &isl_id_none
);
66 /* Is the space that of a set?
68 isl_bool
isl_space_is_set(__isl_keep isl_space
*space
)
71 return isl_bool_error
;
72 if (space
->n_in
!= 0 || space
->nested
[0])
73 return isl_bool_false
;
74 if (space
->tuple_id
[0] != &isl_id_none
)
75 return isl_bool_false
;
79 /* Is the given space that of a map?
81 isl_bool
isl_space_is_map(__isl_keep isl_space
*space
)
84 return isl_bool_error
;
85 return space
->tuple_id
[0] != &isl_id_none
&&
86 space
->tuple_id
[1] != &isl_id_none
;
89 __isl_give isl_space
*isl_space_set_alloc(isl_ctx
*ctx
,
90 unsigned nparam
, unsigned dim
)
93 space
= isl_space_alloc(ctx
, nparam
, 0, dim
);
94 space
= mark_as_set(space
);
98 /* Mark the space as being that of a parameter domain, by setting
99 * both tuples to isl_id_none.
101 static __isl_give isl_space
*mark_as_params(isl_space
*space
)
105 space
= isl_space_set_tuple_id(space
, isl_dim_in
, &isl_id_none
);
106 space
= isl_space_set_tuple_id(space
, isl_dim_out
, &isl_id_none
);
110 /* Is the space that of a parameter domain?
112 isl_bool
isl_space_is_params(__isl_keep isl_space
*space
)
115 return isl_bool_error
;
116 if (space
->n_in
!= 0 || space
->nested
[0] ||
117 space
->n_out
!= 0 || space
->nested
[1])
118 return isl_bool_false
;
119 if (space
->tuple_id
[0] != &isl_id_none
)
120 return isl_bool_false
;
121 if (space
->tuple_id
[1] != &isl_id_none
)
122 return isl_bool_false
;
123 return isl_bool_true
;
126 /* Create a space for a parameter domain.
128 __isl_give isl_space
*isl_space_params_alloc(isl_ctx
*ctx
, unsigned nparam
)
131 space
= isl_space_alloc(ctx
, nparam
, 0, 0);
132 space
= mark_as_params(space
);
136 static unsigned global_pos(__isl_keep isl_space
*dim
,
137 enum isl_dim_type type
, unsigned pos
)
139 struct isl_ctx
*ctx
= dim
->ctx
;
143 isl_assert(ctx
, pos
< dim
->nparam
,
144 return isl_space_dim(dim
, isl_dim_all
));
147 isl_assert(ctx
, pos
< dim
->n_in
,
148 return isl_space_dim(dim
, isl_dim_all
));
149 return pos
+ dim
->nparam
;
151 isl_assert(ctx
, pos
< dim
->n_out
,
152 return isl_space_dim(dim
, isl_dim_all
));
153 return pos
+ dim
->nparam
+ dim
->n_in
;
155 isl_assert(ctx
, 0, return isl_space_dim(dim
, isl_dim_all
));
157 return isl_space_dim(dim
, isl_dim_all
);
160 /* Extend length of ids array to the total number of dimensions.
162 static __isl_give isl_space
*extend_ids(__isl_take isl_space
*dim
)
167 if (isl_space_dim(dim
, isl_dim_all
) <= dim
->n_id
)
171 dim
->ids
= isl_calloc_array(dim
->ctx
,
172 isl_id
*, isl_space_dim(dim
, isl_dim_all
));
176 ids
= isl_realloc_array(dim
->ctx
, dim
->ids
,
177 isl_id
*, isl_space_dim(dim
, isl_dim_all
));
181 for (i
= dim
->n_id
; i
< isl_space_dim(dim
, isl_dim_all
); ++i
)
185 dim
->n_id
= isl_space_dim(dim
, isl_dim_all
);
193 static __isl_give isl_space
*set_id(__isl_take isl_space
*dim
,
194 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
196 dim
= isl_space_cow(dim
);
201 pos
= global_pos(dim
, type
, pos
);
202 if (pos
== isl_space_dim(dim
, isl_dim_all
))
205 if (pos
>= dim
->n_id
) {
208 dim
= extend_ids(dim
);
222 static __isl_keep isl_id
*get_id(__isl_keep isl_space
*dim
,
223 enum isl_dim_type type
, unsigned pos
)
228 pos
= global_pos(dim
, type
, pos
);
229 if (pos
== isl_space_dim(dim
, isl_dim_all
))
231 if (pos
>= dim
->n_id
)
233 return dim
->ids
[pos
];
236 static unsigned offset(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
239 case isl_dim_param
: return 0;
240 case isl_dim_in
: return dim
->nparam
;
241 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
246 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
249 case isl_dim_param
: return dim
->nparam
;
250 case isl_dim_in
: return dim
->n_in
;
251 case isl_dim_out
: return dim
->n_out
;
252 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
257 unsigned isl_space_dim(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
264 unsigned isl_space_offset(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
268 return offset(dim
, type
);
271 static __isl_give isl_space
*copy_ids(__isl_take isl_space
*dst
,
272 enum isl_dim_type dst_type
, unsigned offset
, __isl_keep isl_space
*src
,
273 enum isl_dim_type src_type
)
281 for (i
= 0; i
< n(src
, src_type
); ++i
) {
282 id
= get_id(src
, src_type
, i
);
285 dst
= set_id(dst
, dst_type
, offset
+ i
, isl_id_copy(id
));
292 __isl_take isl_space
*isl_space_dup(__isl_keep isl_space
*dim
)
297 dup
= isl_space_alloc(dim
->ctx
, dim
->nparam
, dim
->n_in
, dim
->n_out
);
300 if (dim
->tuple_id
[0] &&
301 !(dup
->tuple_id
[0] = isl_id_copy(dim
->tuple_id
[0])))
303 if (dim
->tuple_id
[1] &&
304 !(dup
->tuple_id
[1] = isl_id_copy(dim
->tuple_id
[1])))
306 if (dim
->nested
[0] && !(dup
->nested
[0] = isl_space_copy(dim
->nested
[0])))
308 if (dim
->nested
[1] && !(dup
->nested
[1] = isl_space_copy(dim
->nested
[1])))
312 dup
= copy_ids(dup
, isl_dim_param
, 0, dim
, isl_dim_param
);
313 dup
= copy_ids(dup
, isl_dim_in
, 0, dim
, isl_dim_in
);
314 dup
= copy_ids(dup
, isl_dim_out
, 0, dim
, isl_dim_out
);
321 __isl_give isl_space
*isl_space_cow(__isl_take isl_space
*dim
)
329 return isl_space_dup(dim
);
332 __isl_give isl_space
*isl_space_copy(__isl_keep isl_space
*dim
)
341 __isl_null isl_space
*isl_space_free(__isl_take isl_space
*space
)
348 if (--space
->ref
> 0)
351 isl_id_free(space
->tuple_id
[0]);
352 isl_id_free(space
->tuple_id
[1]);
354 isl_space_free(space
->nested
[0]);
355 isl_space_free(space
->nested
[1]);
357 for (i
= 0; i
< space
->n_id
; ++i
)
358 isl_id_free(space
->ids
[i
]);
360 isl_ctx_deref(space
->ctx
);
367 /* Check if "s" is a valid dimension or tuple name.
368 * We currently only forbid names that look like a number.
370 * s is assumed to be non-NULL.
372 static int name_ok(isl_ctx
*ctx
, const char *s
)
377 dummy
= strtol(s
, &p
, 0);
379 isl_die(ctx
, isl_error_invalid
, "name looks like a number",
385 /* Is it possible for the given dimension type to have a tuple id?
387 static int space_can_have_id(__isl_keep isl_space
*space
,
388 enum isl_dim_type type
)
392 if (isl_space_is_params(space
))
393 isl_die(space
->ctx
, isl_error_invalid
,
394 "parameter spaces don't have tuple ids", return 0);
395 if (isl_space_is_set(space
) && type
!= isl_dim_set
)
396 isl_die(space
->ctx
, isl_error_invalid
,
397 "set spaces can only have a set id", return 0);
398 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
399 isl_die(space
->ctx
, isl_error_invalid
,
400 "only input, output and set tuples can have ids",
406 /* Does the tuple have an id?
408 isl_bool
isl_space_has_tuple_id(__isl_keep isl_space
*dim
,
409 enum isl_dim_type type
)
411 if (!space_can_have_id(dim
, type
))
412 return isl_bool_error
;
413 return dim
->tuple_id
[type
- isl_dim_in
] != NULL
;
416 __isl_give isl_id
*isl_space_get_tuple_id(__isl_keep isl_space
*dim
,
417 enum isl_dim_type type
)
423 has_id
= isl_space_has_tuple_id(dim
, type
);
427 isl_die(dim
->ctx
, isl_error_invalid
,
428 "tuple has no id", return NULL
);
429 return isl_id_copy(dim
->tuple_id
[type
- isl_dim_in
]);
432 __isl_give isl_space
*isl_space_set_tuple_id(__isl_take isl_space
*dim
,
433 enum isl_dim_type type
, __isl_take isl_id
*id
)
435 dim
= isl_space_cow(dim
);
438 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
439 isl_die(dim
->ctx
, isl_error_invalid
,
440 "only input, output and set tuples can have names",
443 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
444 dim
->tuple_id
[type
- isl_dim_in
] = id
;
453 __isl_give isl_space
*isl_space_reset_tuple_id(__isl_take isl_space
*dim
,
454 enum isl_dim_type type
)
456 dim
= isl_space_cow(dim
);
459 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
460 isl_die(dim
->ctx
, isl_error_invalid
,
461 "only input, output and set tuples can have names",
464 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
465 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
473 /* Set the id of the given dimension of "space" to "id".
474 * If the dimension already has an id, then it is replaced.
475 * If the dimension is a parameter, then we need to change it
476 * in the nested spaces (if any) as well.
478 __isl_give isl_space
*isl_space_set_dim_id(__isl_take isl_space
*space
,
479 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
481 space
= isl_space_cow(space
);
485 if (type
== isl_dim_param
) {
488 for (i
= 0; i
< 2; ++i
) {
489 if (!space
->nested
[i
])
492 isl_space_set_dim_id(space
->nested
[i
],
493 type
, pos
, isl_id_copy(id
));
494 if (!space
->nested
[i
])
499 isl_id_free(get_id(space
, type
, pos
));
500 return set_id(space
, type
, pos
, id
);
503 isl_space_free(space
);
507 /* Reset the id of the given dimension of "space".
508 * If the dimension already has an id, then it is removed.
509 * If the dimension is a parameter, then we need to reset it
510 * in the nested spaces (if any) as well.
512 __isl_give isl_space
*isl_space_reset_dim_id(__isl_take isl_space
*space
,
513 enum isl_dim_type type
, unsigned pos
)
515 space
= isl_space_cow(space
);
519 if (type
== isl_dim_param
) {
522 for (i
= 0; i
< 2; ++i
) {
523 if (!space
->nested
[i
])
526 isl_space_reset_dim_id(space
->nested
[i
],
528 if (!space
->nested
[i
])
533 isl_id_free(get_id(space
, type
, pos
));
534 return set_id(space
, type
, pos
, NULL
);
536 isl_space_free(space
);
540 isl_bool
isl_space_has_dim_id(__isl_keep isl_space
*dim
,
541 enum isl_dim_type type
, unsigned pos
)
544 return isl_bool_error
;
545 return get_id(dim
, type
, pos
) != NULL
;
548 __isl_give isl_id
*isl_space_get_dim_id(__isl_keep isl_space
*dim
,
549 enum isl_dim_type type
, unsigned pos
)
553 if (!get_id(dim
, type
, pos
))
554 isl_die(dim
->ctx
, isl_error_invalid
,
555 "dim has no id", return NULL
);
556 return isl_id_copy(get_id(dim
, type
, pos
));
559 __isl_give isl_space
*isl_space_set_tuple_name(__isl_take isl_space
*dim
,
560 enum isl_dim_type type
, const char *s
)
568 return isl_space_reset_tuple_id(dim
, type
);
570 if (!name_ok(dim
->ctx
, s
))
573 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
574 return isl_space_set_tuple_id(dim
, type
, id
);
580 /* Does the tuple have a name?
582 isl_bool
isl_space_has_tuple_name(__isl_keep isl_space
*space
,
583 enum isl_dim_type type
)
587 if (!space_can_have_id(space
, type
))
588 return isl_bool_error
;
589 id
= space
->tuple_id
[type
- isl_dim_in
];
590 return id
&& id
->name
;
593 const char *isl_space_get_tuple_name(__isl_keep isl_space
*dim
,
594 enum isl_dim_type type
)
599 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
601 id
= dim
->tuple_id
[type
- isl_dim_in
];
602 return id
? id
->name
: NULL
;
605 __isl_give isl_space
*isl_space_set_dim_name(__isl_take isl_space
*dim
,
606 enum isl_dim_type type
, unsigned pos
,
614 return isl_space_reset_dim_id(dim
, type
, pos
);
615 if (!name_ok(dim
->ctx
, s
))
617 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
618 return isl_space_set_dim_id(dim
, type
, pos
, id
);
624 /* Does the given dimension have a name?
626 isl_bool
isl_space_has_dim_name(__isl_keep isl_space
*space
,
627 enum isl_dim_type type
, unsigned pos
)
632 return isl_bool_error
;
633 id
= get_id(space
, type
, pos
);
634 return id
&& id
->name
;
637 __isl_keep
const char *isl_space_get_dim_name(__isl_keep isl_space
*dim
,
638 enum isl_dim_type type
, unsigned pos
)
640 isl_id
*id
= get_id(dim
, type
, pos
);
641 return id
? id
->name
: NULL
;
644 int isl_space_find_dim_by_id(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
645 __isl_keep isl_id
*id
)
654 offset
= isl_space_offset(dim
, type
);
655 n
= isl_space_dim(dim
, type
);
656 for (i
= 0; i
< n
&& offset
+ i
< dim
->n_id
; ++i
)
657 if (dim
->ids
[offset
+ i
] == id
)
663 int isl_space_find_dim_by_name(__isl_keep isl_space
*space
,
664 enum isl_dim_type type
, const char *name
)
673 offset
= isl_space_offset(space
, type
);
674 n
= isl_space_dim(space
, type
);
675 for (i
= 0; i
< n
&& offset
+ i
< space
->n_id
; ++i
)
676 if (space
->ids
[offset
+ i
]->name
&&
677 !strcmp(space
->ids
[offset
+ i
]->name
, name
))
683 /* Reset the user pointer on all identifiers of parameters and tuples
686 __isl_give isl_space
*isl_space_reset_user(__isl_take isl_space
*space
)
696 ctx
= isl_space_get_ctx(space
);
698 for (i
= 0; i
< space
->nparam
&& i
< space
->n_id
; ++i
) {
699 if (!isl_id_get_user(space
->ids
[i
]))
701 space
= isl_space_cow(space
);
704 name
= isl_id_get_name(space
->ids
[i
]);
705 id
= isl_id_alloc(ctx
, name
, NULL
);
706 isl_id_free(space
->ids
[i
]);
709 return isl_space_free(space
);
712 for (i
= 0; i
< 2; ++i
) {
713 if (!space
->tuple_id
[i
])
715 if (!isl_id_get_user(space
->tuple_id
[i
]))
717 space
= isl_space_cow(space
);
720 name
= isl_id_get_name(space
->tuple_id
[i
]);
721 id
= isl_id_alloc(ctx
, name
, NULL
);
722 isl_id_free(space
->tuple_id
[i
]);
723 space
->tuple_id
[i
] = id
;
725 return isl_space_free(space
);
728 for (i
= 0; i
< 2; ++i
) {
729 if (!space
->nested
[i
])
731 space
= isl_space_cow(space
);
734 space
->nested
[i
] = isl_space_reset_user(space
->nested
[i
]);
735 if (!space
->nested
[i
])
736 return isl_space_free(space
);
742 static __isl_keep isl_id
*tuple_id(__isl_keep isl_space
*dim
,
743 enum isl_dim_type type
)
747 if (type
== isl_dim_in
)
748 return dim
->tuple_id
[0];
749 if (type
== isl_dim_out
)
750 return dim
->tuple_id
[1];
754 static __isl_keep isl_space
*nested(__isl_keep isl_space
*dim
,
755 enum isl_dim_type type
)
759 if (type
== isl_dim_in
)
760 return dim
->nested
[0];
761 if (type
== isl_dim_out
)
762 return dim
->nested
[1];
766 /* Are the two spaces the same, apart from positions and names of parameters?
768 static int isl_space_has_equal_tuples(__isl_keep isl_space
*space1
,
769 __isl_keep isl_space
*space2
)
771 if (!space1
|| !space2
)
773 if (space1
== space2
)
775 return isl_space_tuple_is_equal(space1
, isl_dim_in
,
776 space2
, isl_dim_in
) &&
777 isl_space_tuple_is_equal(space1
, isl_dim_out
,
778 space2
, isl_dim_out
);
781 /* Check if the tuple of type "type1" of "space1" is the same as
782 * the tuple of type "type2" of "space2".
784 * That is, check if the tuples have the same identifier, the same dimension
785 * and the same internal structure.
786 * The identifiers of the dimensions inside the tuples do not affect the result.
788 * Note that this function only checks the tuples themselves.
789 * If nested tuples are involved, then we need to be careful not
790 * to have result affected by possibly differing parameters
791 * in those nested tuples.
793 isl_bool
isl_space_tuple_is_equal(__isl_keep isl_space
*space1
,
794 enum isl_dim_type type1
, __isl_keep isl_space
*space2
,
795 enum isl_dim_type type2
)
798 isl_space
*nested1
, *nested2
;
800 if (!space1
|| !space2
)
801 return isl_bool_error
;
803 if (space1
== space2
&& type1
== type2
)
804 return isl_bool_true
;
806 if (n(space1
, type1
) != n(space2
, type2
))
807 return isl_bool_false
;
808 id1
= tuple_id(space1
, type1
);
809 id2
= tuple_id(space2
, type2
);
811 return isl_bool_false
;
812 if (id1
&& id1
!= id2
)
813 return isl_bool_false
;
814 nested1
= nested(space1
, type1
);
815 nested2
= nested(space2
, type2
);
816 if (!nested1
^ !nested2
)
817 return isl_bool_false
;
818 if (nested1
&& !isl_space_has_equal_tuples(nested1
, nested2
))
819 return isl_bool_false
;
820 return isl_bool_true
;
823 /* This is the old, undocumented, name for isl_space_tuple_is_equal.
824 * It will be removed at some point.
826 int isl_space_tuple_match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
827 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
829 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
832 static int match(__isl_keep isl_space
*dim1
, enum isl_dim_type dim1_type
,
833 __isl_keep isl_space
*dim2
, enum isl_dim_type dim2_type
)
837 if (dim1
== dim2
&& dim1_type
== dim2_type
)
840 if (!isl_space_tuple_is_equal(dim1
, dim1_type
, dim2
, dim2_type
))
843 if (!dim1
->ids
&& !dim2
->ids
)
846 for (i
= 0; i
< n(dim1
, dim1_type
); ++i
) {
847 if (get_id(dim1
, dim1_type
, i
) != get_id(dim2
, dim2_type
, i
))
853 int isl_space_match(__isl_keep isl_space
*dim1
, enum isl_dim_type dim1_type
,
854 __isl_keep isl_space
*dim2
, enum isl_dim_type dim2_type
)
859 return match(dim1
, dim1_type
, dim2
, dim2_type
);
862 static void get_ids(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
863 unsigned first
, unsigned n
, __isl_keep isl_id
**ids
)
867 for (i
= 0; i
< n
; ++i
)
868 ids
[i
] = get_id(dim
, type
, first
+ i
);
871 __isl_give isl_space
*isl_space_extend(__isl_take isl_space
*dim
,
872 unsigned nparam
, unsigned n_in
, unsigned n_out
)
878 if (dim
->nparam
== nparam
&& dim
->n_in
== n_in
&& dim
->n_out
== n_out
)
881 isl_assert(dim
->ctx
, dim
->nparam
<= nparam
, goto error
);
882 isl_assert(dim
->ctx
, dim
->n_in
<= n_in
, goto error
);
883 isl_assert(dim
->ctx
, dim
->n_out
<= n_out
, goto error
);
885 dim
= isl_space_cow(dim
);
890 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
891 nparam
+ n_in
+ n_out
);
894 get_ids(dim
, isl_dim_param
, 0, dim
->nparam
, ids
);
895 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
+ nparam
);
896 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ nparam
+ n_in
);
899 dim
->n_id
= nparam
+ n_in
+ n_out
;
901 dim
->nparam
= nparam
;
912 __isl_give isl_space
*isl_space_add_dims(__isl_take isl_space
*dim
,
913 enum isl_dim_type type
, unsigned n
)
915 dim
= isl_space_reset(dim
, type
);
920 dim
= isl_space_extend(dim
,
921 dim
->nparam
+ n
, dim
->n_in
, dim
->n_out
);
922 if (dim
&& dim
->nested
[0] &&
923 !(dim
->nested
[0] = isl_space_add_dims(dim
->nested
[0],
926 if (dim
&& dim
->nested
[1] &&
927 !(dim
->nested
[1] = isl_space_add_dims(dim
->nested
[1],
932 return isl_space_extend(dim
,
933 dim
->nparam
, dim
->n_in
+ n
, dim
->n_out
);
935 return isl_space_extend(dim
,
936 dim
->nparam
, dim
->n_in
, dim
->n_out
+ n
);
938 isl_die(dim
->ctx
, isl_error_invalid
,
939 "cannot add dimensions of specified type", goto error
);
946 static int valid_dim_type(enum isl_dim_type type
)
958 /* Insert "n" dimensions of type "type" at position "pos".
959 * If we are inserting parameters, then they are also inserted in
962 __isl_give isl_space
*isl_space_insert_dims(__isl_take isl_space
*dim
,
963 enum isl_dim_type type
, unsigned pos
, unsigned n
)
970 return isl_space_reset(dim
, type
);
972 if (!valid_dim_type(type
))
973 isl_die(dim
->ctx
, isl_error_invalid
,
974 "cannot insert dimensions of specified type",
977 isl_assert(dim
->ctx
, pos
<= isl_space_dim(dim
, type
), goto error
);
979 dim
= isl_space_cow(dim
);
984 enum isl_dim_type t
, o
= isl_dim_param
;
987 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
988 dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
);
992 s
[isl_dim_param
- o
] = dim
->nparam
;
993 s
[isl_dim_in
- o
] = dim
->n_in
;
994 s
[isl_dim_out
- o
] = dim
->n_out
;
995 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
997 get_ids(dim
, t
, 0, s
[t
- o
], ids
+ off
);
1000 get_ids(dim
, t
, 0, pos
, ids
+ off
);
1002 get_ids(dim
, t
, pos
, s
[t
- o
] - pos
, ids
+ off
);
1003 off
+= s
[t
- o
] - pos
;
1008 dim
->n_id
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
;
1011 case isl_dim_param
: dim
->nparam
+= n
; break;
1012 case isl_dim_in
: dim
->n_in
+= n
; break;
1013 case isl_dim_out
: dim
->n_out
+= n
; break;
1016 dim
= isl_space_reset(dim
, type
);
1018 if (type
== isl_dim_param
) {
1019 if (dim
&& dim
->nested
[0] &&
1020 !(dim
->nested
[0] = isl_space_insert_dims(dim
->nested
[0],
1021 isl_dim_param
, pos
, n
)))
1023 if (dim
&& dim
->nested
[1] &&
1024 !(dim
->nested
[1] = isl_space_insert_dims(dim
->nested
[1],
1025 isl_dim_param
, pos
, n
)))
1031 isl_space_free(dim
);
1035 __isl_give isl_space
*isl_space_move_dims(__isl_take isl_space
*dim
,
1036 enum isl_dim_type dst_type
, unsigned dst_pos
,
1037 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1044 dim
= isl_space_reset(dim
, src_type
);
1045 return isl_space_reset(dim
, dst_type
);
1048 isl_assert(dim
->ctx
, src_pos
+ n
<= isl_space_dim(dim
, src_type
),
1051 if (dst_type
== src_type
&& dst_pos
== src_pos
)
1054 isl_assert(dim
->ctx
, dst_type
!= src_type
, goto error
);
1056 dim
= isl_space_reset(dim
, src_type
);
1057 dim
= isl_space_reset(dim
, dst_type
);
1059 dim
= isl_space_cow(dim
);
1065 enum isl_dim_type t
, o
= isl_dim_param
;
1068 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
1069 dim
->nparam
+ dim
->n_in
+ dim
->n_out
);
1073 s
[isl_dim_param
- o
] = dim
->nparam
;
1074 s
[isl_dim_in
- o
] = dim
->n_in
;
1075 s
[isl_dim_out
- o
] = dim
->n_out
;
1076 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1077 if (t
== dst_type
) {
1078 get_ids(dim
, t
, 0, dst_pos
, ids
+ off
);
1080 get_ids(dim
, src_type
, src_pos
, n
, ids
+ off
);
1082 get_ids(dim
, t
, dst_pos
, s
[t
- o
] - dst_pos
,
1084 off
+= s
[t
- o
] - dst_pos
;
1085 } else if (t
== src_type
) {
1086 get_ids(dim
, t
, 0, src_pos
, ids
+ off
);
1088 get_ids(dim
, t
, src_pos
+ n
,
1089 s
[t
- o
] - src_pos
- n
, ids
+ off
);
1090 off
+= s
[t
- o
] - src_pos
- n
;
1092 get_ids(dim
, t
, 0, s
[t
- o
], ids
+ off
);
1098 dim
->n_id
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
1102 case isl_dim_param
: dim
->nparam
+= n
; break;
1103 case isl_dim_in
: dim
->n_in
+= n
; break;
1104 case isl_dim_out
: dim
->n_out
+= n
; break;
1109 case isl_dim_param
: dim
->nparam
-= n
; break;
1110 case isl_dim_in
: dim
->n_in
-= n
; break;
1111 case isl_dim_out
: dim
->n_out
-= n
; break;
1115 if (dst_type
!= isl_dim_param
&& src_type
!= isl_dim_param
)
1118 for (i
= 0; i
< 2; ++i
) {
1119 if (!dim
->nested
[i
])
1121 dim
->nested
[i
] = isl_space_replace(dim
->nested
[i
],
1122 isl_dim_param
, dim
);
1123 if (!dim
->nested
[i
])
1129 isl_space_free(dim
);
1133 __isl_give isl_space
*isl_space_join(__isl_take isl_space
*left
,
1134 __isl_take isl_space
*right
)
1138 if (!left
|| !right
)
1141 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
1143 isl_assert(left
->ctx
,
1144 isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_in
),
1147 dim
= isl_space_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
1151 dim
= copy_ids(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
1152 dim
= copy_ids(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
1153 dim
= copy_ids(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
1155 if (dim
&& left
->tuple_id
[0] &&
1156 !(dim
->tuple_id
[0] = isl_id_copy(left
->tuple_id
[0])))
1158 if (dim
&& right
->tuple_id
[1] &&
1159 !(dim
->tuple_id
[1] = isl_id_copy(right
->tuple_id
[1])))
1161 if (dim
&& left
->nested
[0] &&
1162 !(dim
->nested
[0] = isl_space_copy(left
->nested
[0])))
1164 if (dim
&& right
->nested
[1] &&
1165 !(dim
->nested
[1] = isl_space_copy(right
->nested
[1])))
1168 isl_space_free(left
);
1169 isl_space_free(right
);
1173 isl_space_free(left
);
1174 isl_space_free(right
);
1178 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1179 * { [A -> B] -> [C -> D] }.
1180 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1182 __isl_give isl_space
*isl_space_product(__isl_take isl_space
*left
,
1183 __isl_take isl_space
*right
)
1185 isl_space
*dom1
, *dom2
, *nest1
, *nest2
;
1188 if (!left
|| !right
)
1191 is_set
= isl_space_is_set(left
);
1192 if (is_set
!= isl_space_is_set(right
))
1193 isl_die(isl_space_get_ctx(left
), isl_error_invalid
,
1194 "expecting either two set spaces or two map spaces",
1197 return isl_space_range_product(left
, right
);
1199 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
1202 dom1
= isl_space_domain(isl_space_copy(left
));
1203 dom2
= isl_space_domain(isl_space_copy(right
));
1204 nest1
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1206 dom1
= isl_space_range(left
);
1207 dom2
= isl_space_range(right
);
1208 nest2
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1210 return isl_space_join(isl_space_reverse(nest1
), nest2
);
1212 isl_space_free(left
);
1213 isl_space_free(right
);
1217 /* Given two spaces { A -> C } and { B -> C }, construct the space
1220 __isl_give isl_space
*isl_space_domain_product(__isl_take isl_space
*left
,
1221 __isl_take isl_space
*right
)
1223 isl_space
*ran
, *dom1
, *dom2
, *nest
;
1225 if (!left
|| !right
)
1228 if (!match(left
, isl_dim_param
, right
, isl_dim_param
))
1229 isl_die(left
->ctx
, isl_error_invalid
,
1230 "parameters need to match", goto error
);
1231 if (!isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_out
))
1232 isl_die(left
->ctx
, isl_error_invalid
,
1233 "ranges need to match", goto error
);
1235 ran
= isl_space_range(isl_space_copy(left
));
1237 dom1
= isl_space_domain(left
);
1238 dom2
= isl_space_domain(right
);
1239 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1241 return isl_space_join(isl_space_reverse(nest
), ran
);
1243 isl_space_free(left
);
1244 isl_space_free(right
);
1248 __isl_give isl_space
*isl_space_range_product(__isl_take isl_space
*left
,
1249 __isl_take isl_space
*right
)
1251 isl_space
*dom
, *ran1
, *ran2
, *nest
;
1253 if (!left
|| !right
)
1256 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
1258 if (!isl_space_tuple_is_equal(left
, isl_dim_in
, right
, isl_dim_in
))
1259 isl_die(left
->ctx
, isl_error_invalid
,
1260 "domains need to match", goto error
);
1262 dom
= isl_space_domain(isl_space_copy(left
));
1264 ran1
= isl_space_range(left
);
1265 ran2
= isl_space_range(right
);
1266 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(ran1
), ran2
));
1268 return isl_space_join(isl_space_reverse(dom
), nest
);
1270 isl_space_free(left
);
1271 isl_space_free(right
);
1275 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1277 __isl_give isl_space
*isl_space_factor_domain(__isl_take isl_space
*space
)
1279 space
= isl_space_domain_factor_domain(space
);
1280 space
= isl_space_range_factor_domain(space
);
1284 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1286 __isl_give isl_space
*isl_space_factor_range(__isl_take isl_space
*space
)
1288 space
= isl_space_domain_factor_range(space
);
1289 space
= isl_space_range_factor_range(space
);
1293 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1295 __isl_give isl_space
*isl_space_domain_factor_domain(
1296 __isl_take isl_space
*space
)
1303 if (!isl_space_domain_is_wrapping(space
))
1304 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1305 "domain not a product", return isl_space_free(space
));
1307 nested
= space
->nested
[0];
1308 domain
= isl_space_copy(space
);
1309 domain
= isl_space_drop_dims(domain
, isl_dim_in
,
1310 nested
->n_in
, nested
->n_out
);
1312 return isl_space_free(space
);
1313 if (nested
->tuple_id
[0]) {
1314 domain
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[0]);
1315 if (!domain
->tuple_id
[0])
1318 if (nested
->nested
[0]) {
1319 domain
->nested
[0] = isl_space_copy(nested
->nested
[0]);
1320 if (!domain
->nested
[0])
1324 isl_space_free(space
);
1327 isl_space_free(space
);
1328 isl_space_free(domain
);
1332 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1334 __isl_give isl_space
*isl_space_domain_factor_range(
1335 __isl_take isl_space
*space
)
1342 if (!isl_space_domain_is_wrapping(space
))
1343 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1344 "domain not a product", return isl_space_free(space
));
1346 nested
= space
->nested
[0];
1347 range
= isl_space_copy(space
);
1348 range
= isl_space_drop_dims(range
, isl_dim_in
, 0, nested
->n_in
);
1350 return isl_space_free(space
);
1351 if (nested
->tuple_id
[1]) {
1352 range
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[1]);
1353 if (!range
->tuple_id
[0])
1356 if (nested
->nested
[1]) {
1357 range
->nested
[0] = isl_space_copy(nested
->nested
[1]);
1358 if (!range
->nested
[0])
1362 isl_space_free(space
);
1365 isl_space_free(space
);
1366 isl_space_free(range
);
1370 /* Given a space of the form A -> [B -> C], return the space A -> B.
1372 __isl_give isl_space
*isl_space_range_factor_domain(
1373 __isl_take isl_space
*space
)
1380 if (!isl_space_range_is_wrapping(space
))
1381 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1382 "range not a product", return isl_space_free(space
));
1384 nested
= space
->nested
[1];
1385 domain
= isl_space_copy(space
);
1386 domain
= isl_space_drop_dims(domain
, isl_dim_out
,
1387 nested
->n_in
, nested
->n_out
);
1389 return isl_space_free(space
);
1390 if (nested
->tuple_id
[0]) {
1391 domain
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[0]);
1392 if (!domain
->tuple_id
[1])
1395 if (nested
->nested
[0]) {
1396 domain
->nested
[1] = isl_space_copy(nested
->nested
[0]);
1397 if (!domain
->nested
[1])
1401 isl_space_free(space
);
1404 isl_space_free(space
);
1405 isl_space_free(domain
);
1409 /* Given a space of the form A -> [B -> C], return the space A -> C.
1411 __isl_give isl_space
*isl_space_range_factor_range(
1412 __isl_take isl_space
*space
)
1419 if (!isl_space_range_is_wrapping(space
))
1420 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1421 "range not a product", return isl_space_free(space
));
1423 nested
= space
->nested
[1];
1424 range
= isl_space_copy(space
);
1425 range
= isl_space_drop_dims(range
, isl_dim_out
, 0, nested
->n_in
);
1427 return isl_space_free(space
);
1428 if (nested
->tuple_id
[1]) {
1429 range
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[1]);
1430 if (!range
->tuple_id
[1])
1433 if (nested
->nested
[1]) {
1434 range
->nested
[1] = isl_space_copy(nested
->nested
[1]);
1435 if (!range
->nested
[1])
1439 isl_space_free(space
);
1442 isl_space_free(space
);
1443 isl_space_free(range
);
1447 __isl_give isl_space
*isl_space_map_from_set(__isl_take isl_space
*dim
)
1450 isl_id
**ids
= NULL
;
1454 ctx
= isl_space_get_ctx(dim
);
1455 if (!isl_space_is_set(dim
))
1456 isl_die(ctx
, isl_error_invalid
, "not a set space", goto error
);
1457 dim
= isl_space_cow(dim
);
1461 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
1462 dim
->nparam
+ dim
->n_out
+ dim
->n_out
);
1465 get_ids(dim
, isl_dim_param
, 0, dim
->nparam
, ids
);
1466 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->nparam
);
1468 dim
->n_in
= dim
->n_out
;
1472 dim
->n_id
= dim
->nparam
+ dim
->n_out
+ dim
->n_out
;
1473 dim
= copy_ids(dim
, isl_dim_out
, 0, dim
, isl_dim_in
);
1475 isl_id_free(dim
->tuple_id
[0]);
1476 dim
->tuple_id
[0] = isl_id_copy(dim
->tuple_id
[1]);
1477 isl_space_free(dim
->nested
[0]);
1478 dim
->nested
[0] = isl_space_copy(dim
->nested
[1]);
1481 isl_space_free(dim
);
1485 __isl_give isl_space
*isl_space_map_from_domain_and_range(
1486 __isl_take isl_space
*domain
, __isl_take isl_space
*range
)
1488 if (!domain
|| !range
)
1490 if (!isl_space_is_set(domain
))
1491 isl_die(isl_space_get_ctx(domain
), isl_error_invalid
,
1492 "domain is not a set space", goto error
);
1493 if (!isl_space_is_set(range
))
1494 isl_die(isl_space_get_ctx(range
), isl_error_invalid
,
1495 "range is not a set space", goto error
);
1496 return isl_space_join(isl_space_reverse(domain
), range
);
1498 isl_space_free(domain
);
1499 isl_space_free(range
);
1503 static __isl_give isl_space
*set_ids(__isl_take isl_space
*dim
,
1504 enum isl_dim_type type
,
1505 unsigned first
, unsigned n
, __isl_take isl_id
**ids
)
1509 for (i
= 0; i
< n
; ++i
)
1510 dim
= set_id(dim
, type
, first
+ i
, ids
[i
]);
1515 __isl_give isl_space
*isl_space_reverse(__isl_take isl_space
*dim
)
1519 isl_id
**ids
= NULL
;
1524 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
1527 dim
= isl_space_cow(dim
);
1531 id
= dim
->tuple_id
[0];
1532 dim
->tuple_id
[0] = dim
->tuple_id
[1];
1533 dim
->tuple_id
[1] = id
;
1535 nested
= dim
->nested
[0];
1536 dim
->nested
[0] = dim
->nested
[1];
1537 dim
->nested
[1] = nested
;
1540 int n_id
= dim
->n_in
+ dim
->n_out
;
1541 ids
= isl_alloc_array(dim
->ctx
, isl_id
*, n_id
);
1544 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
);
1545 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->n_in
);
1549 dim
->n_in
= dim
->n_out
;
1553 dim
= set_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
);
1554 dim
= set_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
+ dim
->n_out
);
1561 isl_space_free(dim
);
1565 __isl_give isl_space
*isl_space_drop_dims(__isl_take isl_space
*dim
,
1566 enum isl_dim_type type
, unsigned first
, unsigned num
)
1574 return isl_space_reset(dim
, type
);
1576 if (!valid_dim_type(type
))
1577 isl_die(dim
->ctx
, isl_error_invalid
,
1578 "cannot drop dimensions of specified type", goto error
);
1580 if (first
+ num
> n(dim
, type
) || first
+ num
< first
)
1581 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1582 "index out of bounds", return isl_space_free(dim
));
1583 dim
= isl_space_cow(dim
);
1587 dim
= extend_ids(dim
);
1590 for (i
= 0; i
< num
; ++i
)
1591 isl_id_free(get_id(dim
, type
, first
+ i
));
1592 for (i
= first
+num
; i
< n(dim
, type
); ++i
)
1593 set_id(dim
, type
, i
- num
, get_id(dim
, type
, i
));
1596 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
,
1597 dim
->ids
+ offset(dim
, isl_dim_in
) - num
);
1599 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
,
1600 dim
->ids
+ offset(dim
, isl_dim_out
) - num
);
1607 case isl_dim_param
: dim
->nparam
-= num
; break;
1608 case isl_dim_in
: dim
->n_in
-= num
; break;
1609 case isl_dim_out
: dim
->n_out
-= num
; break;
1612 dim
= isl_space_reset(dim
, type
);
1613 if (type
== isl_dim_param
) {
1614 if (dim
&& dim
->nested
[0] &&
1615 !(dim
->nested
[0] = isl_space_drop_dims(dim
->nested
[0],
1616 isl_dim_param
, first
, num
)))
1618 if (dim
&& dim
->nested
[1] &&
1619 !(dim
->nested
[1] = isl_space_drop_dims(dim
->nested
[1],
1620 isl_dim_param
, first
, num
)))
1625 isl_space_free(dim
);
1629 __isl_give isl_space
*isl_space_drop_inputs(__isl_take isl_space
*dim
,
1630 unsigned first
, unsigned n
)
1634 return isl_space_drop_dims(dim
, isl_dim_in
, first
, n
);
1637 __isl_give isl_space
*isl_space_drop_outputs(__isl_take isl_space
*dim
,
1638 unsigned first
, unsigned n
)
1642 return isl_space_drop_dims(dim
, isl_dim_out
, first
, n
);
1645 __isl_give isl_space
*isl_space_domain(__isl_take isl_space
*dim
)
1649 dim
= isl_space_drop_outputs(dim
, 0, dim
->n_out
);
1650 dim
= isl_space_reverse(dim
);
1651 dim
= mark_as_set(dim
);
1655 __isl_give isl_space
*isl_space_from_domain(__isl_take isl_space
*dim
)
1659 if (!isl_space_is_set(dim
))
1660 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1661 "not a set space", goto error
);
1662 dim
= isl_space_reverse(dim
);
1663 dim
= isl_space_reset(dim
, isl_dim_out
);
1666 isl_space_free(dim
);
1670 __isl_give isl_space
*isl_space_range(__isl_take isl_space
*dim
)
1674 dim
= isl_space_drop_inputs(dim
, 0, dim
->n_in
);
1675 dim
= mark_as_set(dim
);
1679 __isl_give isl_space
*isl_space_from_range(__isl_take isl_space
*dim
)
1683 if (!isl_space_is_set(dim
))
1684 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1685 "not a set space", goto error
);
1686 return isl_space_reset(dim
, isl_dim_in
);
1688 isl_space_free(dim
);
1692 /* Given a map space A -> B, return the map space [A -> B] -> A.
1694 __isl_give isl_space
*isl_space_domain_map(__isl_take isl_space
*space
)
1698 domain
= isl_space_from_range(isl_space_domain(isl_space_copy(space
)));
1699 space
= isl_space_from_domain(isl_space_wrap(space
));
1700 space
= isl_space_join(space
, domain
);
1705 /* Given a map space A -> B, return the map space [A -> B] -> B.
1707 __isl_give isl_space
*isl_space_range_map(__isl_take isl_space
*space
)
1711 range
= isl_space_from_range(isl_space_range(isl_space_copy(space
)));
1712 space
= isl_space_from_domain(isl_space_wrap(space
));
1713 space
= isl_space_join(space
, range
);
1718 __isl_give isl_space
*isl_space_params(__isl_take isl_space
*space
)
1720 if (isl_space_is_params(space
))
1722 space
= isl_space_drop_dims(space
,
1723 isl_dim_in
, 0, isl_space_dim(space
, isl_dim_in
));
1724 space
= isl_space_drop_dims(space
,
1725 isl_dim_out
, 0, isl_space_dim(space
, isl_dim_out
));
1726 space
= mark_as_params(space
);
1730 __isl_give isl_space
*isl_space_set_from_params(__isl_take isl_space
*space
)
1734 if (!isl_space_is_params(space
))
1735 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1736 "not a parameter space", goto error
);
1737 return isl_space_reset(space
, isl_dim_set
);
1739 isl_space_free(space
);
1743 __isl_give isl_space
*isl_space_as_set_space(__isl_take isl_space
*dim
)
1745 dim
= isl_space_cow(dim
);
1749 dim
->n_out
+= dim
->n_in
;
1751 dim
= isl_space_reset(dim
, isl_dim_in
);
1752 dim
= isl_space_reset(dim
, isl_dim_out
);
1757 __isl_give isl_space
*isl_space_underlying(__isl_take isl_space
*dim
,
1765 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_id
== 0)
1766 return isl_space_reset(isl_space_reset(dim
, isl_dim_in
), isl_dim_out
);
1767 dim
= isl_space_cow(dim
);
1770 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
1774 for (i
= 0; i
< dim
->n_id
; ++i
)
1775 isl_id_free(get_id(dim
, isl_dim_out
, i
));
1777 dim
= isl_space_reset(dim
, isl_dim_in
);
1778 dim
= isl_space_reset(dim
, isl_dim_out
);
1783 /* Are the two spaces the same, including positions and names of parameters?
1785 isl_bool
isl_space_is_equal(__isl_keep isl_space
*dim1
,
1786 __isl_keep isl_space
*dim2
)
1789 return isl_bool_error
;
1791 return isl_bool_true
;
1792 return match(dim1
, isl_dim_param
, dim2
, isl_dim_param
) &&
1793 isl_space_tuple_is_equal(dim1
, isl_dim_in
, dim2
, isl_dim_in
) &&
1794 isl_space_tuple_is_equal(dim1
, isl_dim_out
, dim2
, isl_dim_out
);
1797 /* Is space1 equal to the domain of space2?
1799 * In the internal version we also allow space2 to be the space of a set,
1800 * provided space1 is a parameter space.
1802 isl_bool
isl_space_is_domain_internal(__isl_keep isl_space
*space1
,
1803 __isl_keep isl_space
*space2
)
1805 if (!space1
|| !space2
)
1806 return isl_bool_error
;
1807 if (!isl_space_is_set(space1
))
1808 return isl_bool_false
;
1809 return match(space1
, isl_dim_param
, space2
, isl_dim_param
) &&
1810 isl_space_tuple_is_equal(space1
, isl_dim_set
,
1811 space2
, isl_dim_in
);
1814 /* Is space1 equal to the domain of space2?
1816 isl_bool
isl_space_is_domain(__isl_keep isl_space
*space1
,
1817 __isl_keep isl_space
*space2
)
1820 return isl_bool_error
;
1821 if (!isl_space_is_map(space2
))
1822 return isl_bool_false
;
1823 return isl_space_is_domain_internal(space1
, space2
);
1826 /* Is space1 equal to the range of space2?
1828 * In the internal version, space2 is allowed to be the space of a set,
1829 * in which case it should be equal to space1.
1831 isl_bool
isl_space_is_range_internal(__isl_keep isl_space
*space1
,
1832 __isl_keep isl_space
*space2
)
1834 if (!space1
|| !space2
)
1835 return isl_bool_error
;
1836 if (!isl_space_is_set(space1
))
1837 return isl_bool_false
;
1838 return match(space1
, isl_dim_param
, space2
, isl_dim_param
) &&
1839 isl_space_tuple_is_equal(space1
, isl_dim_set
,
1840 space2
, isl_dim_out
);
1843 /* Is space1 equal to the range of space2?
1845 isl_bool
isl_space_is_range(__isl_keep isl_space
*space1
,
1846 __isl_keep isl_space
*space2
)
1849 return isl_bool_error
;
1850 if (!isl_space_is_map(space2
))
1851 return isl_bool_false
;
1852 return isl_space_is_range_internal(space1
, space2
);
1855 int isl_space_compatible(__isl_keep isl_space
*dim1
,
1856 __isl_keep isl_space
*dim2
)
1858 return dim1
->nparam
== dim2
->nparam
&&
1859 dim1
->n_in
+ dim1
->n_out
== dim2
->n_in
+ dim2
->n_out
;
1862 static uint32_t isl_hash_dim(uint32_t hash
, __isl_keep isl_space
*dim
)
1870 isl_hash_byte(hash
, dim
->nparam
% 256);
1871 isl_hash_byte(hash
, dim
->n_in
% 256);
1872 isl_hash_byte(hash
, dim
->n_out
% 256);
1874 for (i
= 0; i
< dim
->nparam
; ++i
) {
1875 id
= get_id(dim
, isl_dim_param
, i
);
1876 hash
= isl_hash_id(hash
, id
);
1879 id
= tuple_id(dim
, isl_dim_in
);
1880 hash
= isl_hash_id(hash
, id
);
1881 id
= tuple_id(dim
, isl_dim_out
);
1882 hash
= isl_hash_id(hash
, id
);
1884 hash
= isl_hash_dim(hash
, dim
->nested
[0]);
1885 hash
= isl_hash_dim(hash
, dim
->nested
[1]);
1890 uint32_t isl_space_get_hash(__isl_keep isl_space
*dim
)
1897 hash
= isl_hash_init();
1898 hash
= isl_hash_dim(hash
, dim
);
1903 isl_bool
isl_space_is_wrapping(__isl_keep isl_space
*dim
)
1906 return isl_bool_error
;
1908 if (!isl_space_is_set(dim
))
1909 return isl_bool_false
;
1911 return dim
->nested
[1] != NULL
;
1914 /* Is "space" the space of a map where the domain is a wrapped map space?
1916 isl_bool
isl_space_domain_is_wrapping(__isl_keep isl_space
*space
)
1919 return isl_bool_error
;
1921 if (isl_space_is_set(space
))
1922 return isl_bool_false
;
1924 return space
->nested
[0] != NULL
;
1927 /* Is "space" the space of a map where the range is a wrapped map space?
1929 isl_bool
isl_space_range_is_wrapping(__isl_keep isl_space
*space
)
1932 return isl_bool_error
;
1934 if (isl_space_is_set(space
))
1935 return isl_bool_false
;
1937 return space
->nested
[1] != NULL
;
1940 __isl_give isl_space
*isl_space_wrap(__isl_take isl_space
*dim
)
1947 wrap
= isl_space_set_alloc(dim
->ctx
,
1948 dim
->nparam
, dim
->n_in
+ dim
->n_out
);
1950 wrap
= copy_ids(wrap
, isl_dim_param
, 0, dim
, isl_dim_param
);
1951 wrap
= copy_ids(wrap
, isl_dim_set
, 0, dim
, isl_dim_in
);
1952 wrap
= copy_ids(wrap
, isl_dim_set
, dim
->n_in
, dim
, isl_dim_out
);
1957 wrap
->nested
[1] = dim
;
1961 isl_space_free(dim
);
1965 __isl_give isl_space
*isl_space_unwrap(__isl_take isl_space
*dim
)
1972 if (!isl_space_is_wrapping(dim
))
1973 isl_die(dim
->ctx
, isl_error_invalid
, "not a wrapping space",
1976 unwrap
= isl_space_copy(dim
->nested
[1]);
1977 isl_space_free(dim
);
1981 isl_space_free(dim
);
1985 int isl_space_is_named_or_nested(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
1987 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
1991 if (dim
->tuple_id
[type
- isl_dim_in
])
1993 if (dim
->nested
[type
- isl_dim_in
])
1998 int isl_space_may_be_set(__isl_keep isl_space
*dim
)
2002 if (isl_space_is_set(dim
))
2004 if (isl_space_dim(dim
, isl_dim_in
) != 0)
2006 if (isl_space_is_named_or_nested(dim
, isl_dim_in
))
2011 __isl_give isl_space
*isl_space_reset(__isl_take isl_space
*dim
,
2012 enum isl_dim_type type
)
2014 if (!isl_space_is_named_or_nested(dim
, type
))
2017 dim
= isl_space_cow(dim
);
2021 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
2022 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
2023 isl_space_free(dim
->nested
[type
- isl_dim_in
]);
2024 dim
->nested
[type
- isl_dim_in
] = NULL
;
2029 __isl_give isl_space
*isl_space_flatten(__isl_take isl_space
*dim
)
2033 if (!dim
->nested
[0] && !dim
->nested
[1])
2037 dim
= isl_space_reset(dim
, isl_dim_in
);
2038 if (dim
&& dim
->nested
[1])
2039 dim
= isl_space_reset(dim
, isl_dim_out
);
2044 __isl_give isl_space
*isl_space_flatten_domain(__isl_take isl_space
*dim
)
2048 if (!dim
->nested
[0])
2051 return isl_space_reset(dim
, isl_dim_in
);
2054 __isl_give isl_space
*isl_space_flatten_range(__isl_take isl_space
*dim
)
2058 if (!dim
->nested
[1])
2061 return isl_space_reset(dim
, isl_dim_out
);
2064 /* Replace the dimensions of the given type of dst by those of src.
2066 __isl_give isl_space
*isl_space_replace(__isl_take isl_space
*dst
,
2067 enum isl_dim_type type
, __isl_keep isl_space
*src
)
2069 dst
= isl_space_cow(dst
);
2074 dst
= isl_space_drop_dims(dst
, type
, 0, isl_space_dim(dst
, type
));
2075 dst
= isl_space_add_dims(dst
, type
, isl_space_dim(src
, type
));
2076 dst
= copy_ids(dst
, type
, 0, src
, type
);
2078 if (dst
&& type
== isl_dim_param
) {
2080 for (i
= 0; i
<= 1; ++i
) {
2081 if (!dst
->nested
[i
])
2083 dst
->nested
[i
] = isl_space_replace(dst
->nested
[i
],
2085 if (!dst
->nested
[i
])
2092 isl_space_free(dst
);
2096 /* Given a dimension specification "dim" of a set, create a dimension
2097 * specification for the lift of the set. In particular, the result
2098 * is of the form [dim -> local[..]], with n_local variables in the
2099 * range of the wrapped map.
2101 __isl_give isl_space
*isl_space_lift(__isl_take isl_space
*dim
, unsigned n_local
)
2103 isl_space
*local_dim
;
2108 local_dim
= isl_space_dup(dim
);
2109 local_dim
= isl_space_drop_dims(local_dim
, isl_dim_set
, 0, dim
->n_out
);
2110 local_dim
= isl_space_add_dims(local_dim
, isl_dim_set
, n_local
);
2111 local_dim
= isl_space_set_tuple_name(local_dim
, isl_dim_set
, "local");
2112 dim
= isl_space_join(isl_space_from_domain(dim
),
2113 isl_space_from_range(local_dim
));
2114 dim
= isl_space_wrap(dim
);
2115 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, "lifted");
2120 isl_bool
isl_space_can_zip(__isl_keep isl_space
*dim
)
2123 return isl_bool_error
;
2125 return dim
->nested
[0] && dim
->nested
[1];
2128 __isl_give isl_space
*isl_space_zip(__isl_take isl_space
*dim
)
2130 isl_space
*dom
, *ran
;
2131 isl_space
*dom_dom
, *dom_ran
, *ran_dom
, *ran_ran
;
2133 if (!isl_space_can_zip(dim
))
2134 isl_die(dim
->ctx
, isl_error_invalid
, "dim cannot be zipped",
2139 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(dim
)));
2140 ran
= isl_space_unwrap(isl_space_range(dim
));
2141 dom_dom
= isl_space_domain(isl_space_copy(dom
));
2142 dom_ran
= isl_space_range(dom
);
2143 ran_dom
= isl_space_domain(isl_space_copy(ran
));
2144 ran_ran
= isl_space_range(ran
);
2145 dom
= isl_space_join(isl_space_from_domain(dom_dom
),
2146 isl_space_from_range(ran_dom
));
2147 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
2148 isl_space_from_range(ran_ran
));
2149 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
2150 isl_space_from_range(isl_space_wrap(ran
)));
2152 isl_space_free(dim
);
2156 /* Can we apply isl_space_curry to "space"?
2157 * That is, does it have a nested relation in its domain?
2159 isl_bool
isl_space_can_curry(__isl_keep isl_space
*space
)
2162 return isl_bool_error
;
2164 return !!space
->nested
[0];
2167 /* Given a space (A -> B) -> C, return the corresponding space
2170 __isl_give isl_space
*isl_space_curry(__isl_take isl_space
*space
)
2172 isl_space
*dom
, *ran
;
2173 isl_space
*dom_dom
, *dom_ran
;
2178 if (!isl_space_can_curry(space
))
2179 isl_die(space
->ctx
, isl_error_invalid
,
2180 "space cannot be curried", goto error
);
2182 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(space
)));
2183 ran
= isl_space_range(space
);
2184 dom_dom
= isl_space_domain(isl_space_copy(dom
));
2185 dom_ran
= isl_space_range(dom
);
2186 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
2187 isl_space_from_range(ran
));
2188 return isl_space_join(isl_space_from_domain(dom_dom
),
2189 isl_space_from_range(isl_space_wrap(ran
)));
2191 isl_space_free(space
);
2195 /* Can we apply isl_space_uncurry to "space"?
2196 * That is, does it have a nested relation in its range?
2198 isl_bool
isl_space_can_uncurry(__isl_keep isl_space
*space
)
2201 return isl_bool_error
;
2203 return !!space
->nested
[1];
2206 /* Given a space A -> (B -> C), return the corresponding space
2209 __isl_give isl_space
*isl_space_uncurry(__isl_take isl_space
*space
)
2211 isl_space
*dom
, *ran
;
2212 isl_space
*ran_dom
, *ran_ran
;
2217 if (!isl_space_can_uncurry(space
))
2218 isl_die(space
->ctx
, isl_error_invalid
,
2219 "space cannot be uncurried",
2220 return isl_space_free(space
));
2222 dom
= isl_space_domain(isl_space_copy(space
));
2223 ran
= isl_space_unwrap(isl_space_range(space
));
2224 ran_dom
= isl_space_domain(isl_space_copy(ran
));
2225 ran_ran
= isl_space_range(ran
);
2226 dom
= isl_space_join(isl_space_from_domain(dom
),
2227 isl_space_from_range(ran_dom
));
2228 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
2229 isl_space_from_range(ran_ran
));
2232 int isl_space_has_named_params(__isl_keep isl_space
*dim
)
2239 if (dim
->nparam
== 0)
2241 off
= isl_space_offset(dim
, isl_dim_param
);
2242 if (off
+ dim
->nparam
> dim
->n_id
)
2244 for (i
= 0; i
< dim
->nparam
; ++i
)
2245 if (!dim
->ids
[off
+ i
])
2250 /* Align the initial parameters of dim1 to match the order in dim2.
2252 __isl_give isl_space
*isl_space_align_params(__isl_take isl_space
*dim1
,
2253 __isl_take isl_space
*dim2
)
2255 isl_reordering
*exp
;
2257 if (!isl_space_has_named_params(dim1
) || !isl_space_has_named_params(dim2
))
2258 isl_die(isl_space_get_ctx(dim1
), isl_error_invalid
,
2259 "parameter alignment requires named parameters",
2262 dim2
= isl_space_params(dim2
);
2263 exp
= isl_parameter_alignment_reordering(dim1
, dim2
);
2264 exp
= isl_reordering_extend_space(exp
, dim1
);
2265 isl_space_free(dim2
);
2268 dim1
= isl_space_copy(exp
->dim
);
2269 isl_reordering_free(exp
);
2272 isl_space_free(dim1
);
2273 isl_space_free(dim2
);
2277 /* Given the space of set (domain), construct a space for a map
2278 * with as domain the given space and as range the range of "model".
2280 __isl_give isl_space
*isl_space_extend_domain_with_range(
2281 __isl_take isl_space
*space
, __isl_take isl_space
*model
)
2286 space
= isl_space_from_domain(space
);
2287 space
= isl_space_add_dims(space
, isl_dim_out
,
2288 isl_space_dim(model
, isl_dim_out
));
2289 if (isl_space_has_tuple_id(model
, isl_dim_out
))
2290 space
= isl_space_set_tuple_id(space
, isl_dim_out
,
2291 isl_space_get_tuple_id(model
, isl_dim_out
));
2294 if (model
->nested
[1]) {
2295 isl_space
*nested
= isl_space_copy(model
->nested
[1]);
2296 int n_nested
, n_space
;
2297 nested
= isl_space_align_params(nested
, isl_space_copy(space
));
2298 n_nested
= isl_space_dim(nested
, isl_dim_param
);
2299 n_space
= isl_space_dim(space
, isl_dim_param
);
2300 if (n_nested
> n_space
)
2301 nested
= isl_space_drop_dims(nested
, isl_dim_param
,
2302 n_space
, n_nested
- n_space
);
2305 space
->nested
[1] = nested
;
2307 isl_space_free(model
);
2310 isl_space_free(model
);
2311 isl_space_free(space
);
2315 /* Compare the "type" dimensions of two isl_spaces.
2317 * The order is fairly arbitrary.
2319 static int isl_space_cmp_type(__isl_keep isl_space
*space1
,
2320 __isl_keep isl_space
*space2
, enum isl_dim_type type
)
2323 isl_space
*nested1
, *nested2
;
2325 if (isl_space_dim(space1
, type
) != isl_space_dim(space2
, type
))
2326 return isl_space_dim(space1
, type
) -
2327 isl_space_dim(space2
, type
);
2329 cmp
= isl_id_cmp(tuple_id(space1
, type
), tuple_id(space2
, type
));
2333 nested1
= nested(space1
, type
);
2334 nested2
= nested(space2
, type
);
2335 if (!nested1
!= !nested2
)
2336 return !nested1
- !nested2
;
2339 return isl_space_cmp(nested1
, nested2
);
2344 /* Compare two isl_spaces.
2346 * The order is fairly arbitrary.
2348 int isl_space_cmp(__isl_keep isl_space
*space1
, __isl_keep isl_space
*space2
)
2353 if (space1
== space2
)
2360 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_param
);
2363 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_in
);
2366 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_out
);
2370 if (!space1
->ids
&& !space2
->ids
)
2373 for (i
= 0; i
< n(space1
, isl_dim_param
); ++i
) {
2374 cmp
= isl_id_cmp(get_id(space1
, isl_dim_param
, i
),
2375 get_id(space2
, isl_dim_param
, i
));