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 __isl_keep
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 isl_id
*id
= get_id(space
, type
, i
);
677 if (id
&& id
->name
&& !strcmp(id
->name
, name
))
684 /* Reset the user pointer on all identifiers of parameters and tuples
687 __isl_give isl_space
*isl_space_reset_user(__isl_take isl_space
*space
)
697 ctx
= isl_space_get_ctx(space
);
699 for (i
= 0; i
< space
->nparam
&& i
< space
->n_id
; ++i
) {
700 if (!isl_id_get_user(space
->ids
[i
]))
702 space
= isl_space_cow(space
);
705 name
= isl_id_get_name(space
->ids
[i
]);
706 id
= isl_id_alloc(ctx
, name
, NULL
);
707 isl_id_free(space
->ids
[i
]);
710 return isl_space_free(space
);
713 for (i
= 0; i
< 2; ++i
) {
714 if (!space
->tuple_id
[i
])
716 if (!isl_id_get_user(space
->tuple_id
[i
]))
718 space
= isl_space_cow(space
);
721 name
= isl_id_get_name(space
->tuple_id
[i
]);
722 id
= isl_id_alloc(ctx
, name
, NULL
);
723 isl_id_free(space
->tuple_id
[i
]);
724 space
->tuple_id
[i
] = id
;
726 return isl_space_free(space
);
729 for (i
= 0; i
< 2; ++i
) {
730 if (!space
->nested
[i
])
732 space
= isl_space_cow(space
);
735 space
->nested
[i
] = isl_space_reset_user(space
->nested
[i
]);
736 if (!space
->nested
[i
])
737 return isl_space_free(space
);
743 static __isl_keep isl_id
*tuple_id(__isl_keep isl_space
*dim
,
744 enum isl_dim_type type
)
748 if (type
== isl_dim_in
)
749 return dim
->tuple_id
[0];
750 if (type
== isl_dim_out
)
751 return dim
->tuple_id
[1];
755 static __isl_keep isl_space
*nested(__isl_keep isl_space
*dim
,
756 enum isl_dim_type type
)
760 if (type
== isl_dim_in
)
761 return dim
->nested
[0];
762 if (type
== isl_dim_out
)
763 return dim
->nested
[1];
767 /* Are the two spaces the same, apart from positions and names of parameters?
769 isl_bool
isl_space_has_equal_tuples(__isl_keep isl_space
*space1
,
770 __isl_keep isl_space
*space2
)
772 if (!space1
|| !space2
)
773 return isl_bool_error
;
774 if (space1
== space2
)
775 return isl_bool_true
;
776 return isl_space_tuple_is_equal(space1
, isl_dim_in
,
777 space2
, isl_dim_in
) &&
778 isl_space_tuple_is_equal(space1
, isl_dim_out
,
779 space2
, isl_dim_out
);
782 /* Check if the tuple of type "type1" of "space1" is the same as
783 * the tuple of type "type2" of "space2".
785 * That is, check if the tuples have the same identifier, the same dimension
786 * and the same internal structure.
787 * The identifiers of the dimensions inside the tuples do not affect the result.
789 * Note that this function only checks the tuples themselves.
790 * If nested tuples are involved, then we need to be careful not
791 * to have result affected by possibly differing parameters
792 * in those nested tuples.
794 isl_bool
isl_space_tuple_is_equal(__isl_keep isl_space
*space1
,
795 enum isl_dim_type type1
, __isl_keep isl_space
*space2
,
796 enum isl_dim_type type2
)
799 isl_space
*nested1
, *nested2
;
801 if (!space1
|| !space2
)
802 return isl_bool_error
;
804 if (space1
== space2
&& type1
== type2
)
805 return isl_bool_true
;
807 if (n(space1
, type1
) != n(space2
, type2
))
808 return isl_bool_false
;
809 id1
= tuple_id(space1
, type1
);
810 id2
= tuple_id(space2
, type2
);
812 return isl_bool_false
;
813 if (id1
&& id1
!= id2
)
814 return isl_bool_false
;
815 nested1
= nested(space1
, type1
);
816 nested2
= nested(space2
, type2
);
817 if (!nested1
^ !nested2
)
818 return isl_bool_false
;
819 if (nested1
&& !isl_space_has_equal_tuples(nested1
, nested2
))
820 return isl_bool_false
;
821 return isl_bool_true
;
824 /* This is the old, undocumented, name for isl_space_tuple_is_equal.
825 * It will be removed at some point.
827 int isl_space_tuple_match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
828 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
830 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
833 static isl_bool
match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
834 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
838 if (space1
== space2
&& type1
== type2
)
839 return isl_bool_true
;
841 if (!isl_space_tuple_is_equal(space1
, type1
, space2
, type2
))
842 return isl_bool_false
;
844 if (!space1
->ids
&& !space2
->ids
)
845 return isl_bool_true
;
847 for (i
= 0; i
< n(space1
, type1
); ++i
) {
848 if (get_id(space1
, type1
, i
) != get_id(space2
, type2
, i
))
849 return isl_bool_false
;
851 return isl_bool_true
;
854 /* Do "space1" and "space2" have the same parameters?
856 isl_bool
isl_space_has_equal_params(__isl_keep isl_space
*space1
,
857 __isl_keep isl_space
*space2
)
859 if (!space1
|| !space2
)
860 return isl_bool_error
;
862 return match(space1
, isl_dim_param
, space2
, isl_dim_param
);
865 /* Do "space1" and "space2" have the same identifiers for all
866 * the tuple variables?
868 isl_bool
isl_space_has_equal_ids(__isl_keep isl_space
*space1
,
869 __isl_keep isl_space
*space2
)
873 if (!space1
|| !space2
)
874 return isl_bool_error
;
876 equal
= match(space1
, isl_dim_in
, space2
, isl_dim_in
);
877 if (equal
< 0 || !equal
)
879 return match(space1
, isl_dim_out
, space2
, isl_dim_out
);
882 isl_bool
isl_space_match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
883 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
885 if (!space1
|| !space2
)
886 return isl_bool_error
;
888 return match(space1
, type1
, space2
, type2
);
891 static void get_ids(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
892 unsigned first
, unsigned n
, __isl_keep isl_id
**ids
)
896 for (i
= 0; i
< n
; ++i
)
897 ids
[i
] = get_id(dim
, type
, first
+ i
);
900 static __isl_give isl_space
*space_extend(__isl_take isl_space
*space
,
901 unsigned nparam
, unsigned n_in
, unsigned n_out
)
907 if (space
->nparam
== nparam
&&
908 space
->n_in
== n_in
&& space
->n_out
== n_out
)
911 isl_assert(space
->ctx
, space
->nparam
<= nparam
, goto error
);
912 isl_assert(space
->ctx
, space
->n_in
<= n_in
, goto error
);
913 isl_assert(space
->ctx
, space
->n_out
<= n_out
, goto error
);
915 space
= isl_space_cow(space
);
921 n
= nparam
+ n_in
+ n_out
;
922 if (n
< nparam
|| n
< n_in
|| n
< n_out
)
923 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
924 "overflow in total number of dimensions",
926 ids
= isl_calloc_array(space
->ctx
, isl_id
*, n
);
929 get_ids(space
, isl_dim_param
, 0, space
->nparam
, ids
);
930 get_ids(space
, isl_dim_in
, 0, space
->n_in
, ids
+ nparam
);
931 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
932 ids
+ nparam
+ n_in
);
935 space
->n_id
= nparam
+ n_in
+ n_out
;
937 space
->nparam
= nparam
;
939 space
->n_out
= n_out
;
944 isl_space_free(space
);
948 __isl_give isl_space
*isl_space_extend(__isl_take isl_space
*space
,
949 unsigned nparam
, unsigned n_in
, unsigned n_out
)
951 return space_extend(space
, nparam
, n_in
, n_out
);
954 __isl_give isl_space
*isl_space_add_dims(__isl_take isl_space
*space
,
955 enum isl_dim_type type
, unsigned n
)
957 space
= isl_space_reset(space
, type
);
962 space
= space_extend(space
,
963 space
->nparam
+ n
, space
->n_in
, space
->n_out
);
964 if (space
&& space
->nested
[0] &&
965 !(space
->nested
[0] = isl_space_add_dims(space
->nested
[0],
968 if (space
&& space
->nested
[1] &&
969 !(space
->nested
[1] = isl_space_add_dims(space
->nested
[1],
974 return space_extend(space
,
975 space
->nparam
, space
->n_in
+ n
, space
->n_out
);
977 return space_extend(space
,
978 space
->nparam
, space
->n_in
, space
->n_out
+ n
);
980 isl_die(space
->ctx
, isl_error_invalid
,
981 "cannot add dimensions of specified type", goto error
);
984 isl_space_free(space
);
988 static int valid_dim_type(enum isl_dim_type type
)
1000 /* Insert "n" dimensions of type "type" at position "pos".
1001 * If we are inserting parameters, then they are also inserted in
1002 * any nested spaces.
1004 __isl_give isl_space
*isl_space_insert_dims(__isl_take isl_space
*dim
,
1005 enum isl_dim_type type
, unsigned pos
, unsigned n
)
1007 isl_id
**ids
= NULL
;
1012 return isl_space_reset(dim
, type
);
1014 if (!valid_dim_type(type
))
1015 isl_die(dim
->ctx
, isl_error_invalid
,
1016 "cannot insert dimensions of specified type",
1019 isl_assert(dim
->ctx
, pos
<= isl_space_dim(dim
, type
), goto error
);
1021 dim
= isl_space_cow(dim
);
1026 enum isl_dim_type t
, o
= isl_dim_param
;
1029 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
1030 dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
);
1034 s
[isl_dim_param
- o
] = dim
->nparam
;
1035 s
[isl_dim_in
- o
] = dim
->n_in
;
1036 s
[isl_dim_out
- o
] = dim
->n_out
;
1037 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1039 get_ids(dim
, t
, 0, s
[t
- o
], ids
+ off
);
1042 get_ids(dim
, t
, 0, pos
, ids
+ off
);
1044 get_ids(dim
, t
, pos
, s
[t
- o
] - pos
, ids
+ off
);
1045 off
+= s
[t
- o
] - pos
;
1050 dim
->n_id
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
;
1053 case isl_dim_param
: dim
->nparam
+= n
; break;
1054 case isl_dim_in
: dim
->n_in
+= n
; break;
1055 case isl_dim_out
: dim
->n_out
+= n
; break;
1058 dim
= isl_space_reset(dim
, type
);
1060 if (type
== isl_dim_param
) {
1061 if (dim
&& dim
->nested
[0] &&
1062 !(dim
->nested
[0] = isl_space_insert_dims(dim
->nested
[0],
1063 isl_dim_param
, pos
, n
)))
1065 if (dim
&& dim
->nested
[1] &&
1066 !(dim
->nested
[1] = isl_space_insert_dims(dim
->nested
[1],
1067 isl_dim_param
, pos
, n
)))
1073 isl_space_free(dim
);
1077 __isl_give isl_space
*isl_space_move_dims(__isl_take isl_space
*space
,
1078 enum isl_dim_type dst_type
, unsigned dst_pos
,
1079 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1083 space
= isl_space_reset(space
, src_type
);
1084 space
= isl_space_reset(space
, dst_type
);
1090 isl_assert(space
->ctx
, src_pos
+ n
<= isl_space_dim(space
, src_type
),
1093 if (dst_type
== src_type
&& dst_pos
== src_pos
)
1096 isl_assert(space
->ctx
, dst_type
!= src_type
, goto error
);
1098 space
= isl_space_cow(space
);
1104 enum isl_dim_type t
, o
= isl_dim_param
;
1107 ids
= isl_calloc_array(space
->ctx
, isl_id
*,
1108 space
->nparam
+ space
->n_in
+ space
->n_out
);
1112 s
[isl_dim_param
- o
] = space
->nparam
;
1113 s
[isl_dim_in
- o
] = space
->n_in
;
1114 s
[isl_dim_out
- o
] = space
->n_out
;
1115 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1116 if (t
== dst_type
) {
1117 get_ids(space
, t
, 0, dst_pos
, ids
+ off
);
1119 get_ids(space
, src_type
, src_pos
, n
, ids
+ off
);
1121 get_ids(space
, t
, dst_pos
, s
[t
- o
] - dst_pos
,
1123 off
+= s
[t
- o
] - dst_pos
;
1124 } else if (t
== src_type
) {
1125 get_ids(space
, t
, 0, src_pos
, ids
+ off
);
1127 get_ids(space
, t
, src_pos
+ n
,
1128 s
[t
- o
] - src_pos
- n
, ids
+ off
);
1129 off
+= s
[t
- o
] - src_pos
- n
;
1131 get_ids(space
, t
, 0, s
[t
- o
], ids
+ off
);
1137 space
->n_id
= space
->nparam
+ space
->n_in
+ space
->n_out
;
1141 case isl_dim_param
: space
->nparam
+= n
; break;
1142 case isl_dim_in
: space
->n_in
+= n
; break;
1143 case isl_dim_out
: space
->n_out
+= n
; break;
1148 case isl_dim_param
: space
->nparam
-= n
; break;
1149 case isl_dim_in
: space
->n_in
-= n
; break;
1150 case isl_dim_out
: space
->n_out
-= n
; break;
1154 if (dst_type
!= isl_dim_param
&& src_type
!= isl_dim_param
)
1157 for (i
= 0; i
< 2; ++i
) {
1158 if (!space
->nested
[i
])
1160 space
->nested
[i
] = isl_space_replace(space
->nested
[i
],
1161 isl_dim_param
, space
);
1162 if (!space
->nested
[i
])
1168 isl_space_free(space
);
1172 /* Check that "space1" and "space2" have the same parameters,
1173 * reporting an error if they do not.
1175 isl_stat
isl_space_check_equal_params(__isl_keep isl_space
*space1
,
1176 __isl_keep isl_space
*space2
)
1180 equal
= isl_space_has_equal_params(space1
, space2
);
1182 return isl_stat_error
;
1184 isl_die(isl_space_get_ctx(space1
), isl_error_invalid
,
1185 "parameters need to match", return isl_stat_error
);
1189 __isl_give isl_space
*isl_space_join(__isl_take isl_space
*left
,
1190 __isl_take isl_space
*right
)
1194 if (isl_space_check_equal_params(left
, right
) < 0)
1197 isl_assert(left
->ctx
,
1198 isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_in
),
1201 dim
= isl_space_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
1205 dim
= copy_ids(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
1206 dim
= copy_ids(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
1207 dim
= copy_ids(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
1209 if (dim
&& left
->tuple_id
[0] &&
1210 !(dim
->tuple_id
[0] = isl_id_copy(left
->tuple_id
[0])))
1212 if (dim
&& right
->tuple_id
[1] &&
1213 !(dim
->tuple_id
[1] = isl_id_copy(right
->tuple_id
[1])))
1215 if (dim
&& left
->nested
[0] &&
1216 !(dim
->nested
[0] = isl_space_copy(left
->nested
[0])))
1218 if (dim
&& right
->nested
[1] &&
1219 !(dim
->nested
[1] = isl_space_copy(right
->nested
[1])))
1222 isl_space_free(left
);
1223 isl_space_free(right
);
1227 isl_space_free(left
);
1228 isl_space_free(right
);
1232 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1233 * { [A -> B] -> [C -> D] }.
1234 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1236 __isl_give isl_space
*isl_space_product(__isl_take isl_space
*left
,
1237 __isl_take isl_space
*right
)
1239 isl_space
*dom1
, *dom2
, *nest1
, *nest2
;
1242 if (!left
|| !right
)
1245 is_set
= isl_space_is_set(left
);
1246 if (is_set
!= isl_space_is_set(right
))
1247 isl_die(isl_space_get_ctx(left
), isl_error_invalid
,
1248 "expecting either two set spaces or two map spaces",
1251 return isl_space_range_product(left
, right
);
1253 if (isl_space_check_equal_params(left
, right
) < 0)
1256 dom1
= isl_space_domain(isl_space_copy(left
));
1257 dom2
= isl_space_domain(isl_space_copy(right
));
1258 nest1
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1260 dom1
= isl_space_range(left
);
1261 dom2
= isl_space_range(right
);
1262 nest2
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1264 return isl_space_join(isl_space_reverse(nest1
), nest2
);
1266 isl_space_free(left
);
1267 isl_space_free(right
);
1271 /* Given two spaces { A -> C } and { B -> C }, construct the space
1274 __isl_give isl_space
*isl_space_domain_product(__isl_take isl_space
*left
,
1275 __isl_take isl_space
*right
)
1277 isl_space
*ran
, *dom1
, *dom2
, *nest
;
1279 if (isl_space_check_equal_params(left
, right
) < 0)
1282 if (!isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_out
))
1283 isl_die(left
->ctx
, isl_error_invalid
,
1284 "ranges need to match", goto error
);
1286 ran
= isl_space_range(isl_space_copy(left
));
1288 dom1
= isl_space_domain(left
);
1289 dom2
= isl_space_domain(right
);
1290 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1292 return isl_space_join(isl_space_reverse(nest
), ran
);
1294 isl_space_free(left
);
1295 isl_space_free(right
);
1299 __isl_give isl_space
*isl_space_range_product(__isl_take isl_space
*left
,
1300 __isl_take isl_space
*right
)
1302 isl_space
*dom
, *ran1
, *ran2
, *nest
;
1304 if (isl_space_check_equal_params(left
, right
) < 0)
1307 if (!isl_space_tuple_is_equal(left
, isl_dim_in
, right
, isl_dim_in
))
1308 isl_die(left
->ctx
, isl_error_invalid
,
1309 "domains need to match", goto error
);
1311 dom
= isl_space_domain(isl_space_copy(left
));
1313 ran1
= isl_space_range(left
);
1314 ran2
= isl_space_range(right
);
1315 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(ran1
), ran2
));
1317 return isl_space_join(isl_space_reverse(dom
), nest
);
1319 isl_space_free(left
);
1320 isl_space_free(right
);
1324 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1326 __isl_give isl_space
*isl_space_factor_domain(__isl_take isl_space
*space
)
1328 space
= isl_space_domain_factor_domain(space
);
1329 space
= isl_space_range_factor_domain(space
);
1333 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1335 __isl_give isl_space
*isl_space_domain_factor_domain(
1336 __isl_take isl_space
*space
)
1343 if (!isl_space_domain_is_wrapping(space
))
1344 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1345 "domain not a product", return isl_space_free(space
));
1347 nested
= space
->nested
[0];
1348 domain
= isl_space_copy(space
);
1349 domain
= isl_space_drop_dims(domain
, isl_dim_in
,
1350 nested
->n_in
, nested
->n_out
);
1352 return isl_space_free(space
);
1353 if (nested
->tuple_id
[0]) {
1354 domain
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[0]);
1355 if (!domain
->tuple_id
[0])
1358 if (nested
->nested
[0]) {
1359 domain
->nested
[0] = isl_space_copy(nested
->nested
[0]);
1360 if (!domain
->nested
[0])
1364 isl_space_free(space
);
1367 isl_space_free(space
);
1368 isl_space_free(domain
);
1372 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1374 __isl_give isl_space
*isl_space_domain_factor_range(
1375 __isl_take isl_space
*space
)
1382 if (!isl_space_domain_is_wrapping(space
))
1383 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1384 "domain not a product", return isl_space_free(space
));
1386 nested
= space
->nested
[0];
1387 range
= isl_space_copy(space
);
1388 range
= isl_space_drop_dims(range
, isl_dim_in
, 0, nested
->n_in
);
1390 return isl_space_free(space
);
1391 if (nested
->tuple_id
[1]) {
1392 range
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[1]);
1393 if (!range
->tuple_id
[0])
1396 if (nested
->nested
[1]) {
1397 range
->nested
[0] = isl_space_copy(nested
->nested
[1]);
1398 if (!range
->nested
[0])
1402 isl_space_free(space
);
1405 isl_space_free(space
);
1406 isl_space_free(range
);
1410 /* Given a space of the form A -> [B -> C], return the space A -> B.
1412 __isl_give isl_space
*isl_space_range_factor_domain(
1413 __isl_take isl_space
*space
)
1420 if (!isl_space_range_is_wrapping(space
))
1421 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1422 "range not a product", return isl_space_free(space
));
1424 nested
= space
->nested
[1];
1425 domain
= isl_space_copy(space
);
1426 domain
= isl_space_drop_dims(domain
, isl_dim_out
,
1427 nested
->n_in
, nested
->n_out
);
1429 return isl_space_free(space
);
1430 if (nested
->tuple_id
[0]) {
1431 domain
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[0]);
1432 if (!domain
->tuple_id
[1])
1435 if (nested
->nested
[0]) {
1436 domain
->nested
[1] = isl_space_copy(nested
->nested
[0]);
1437 if (!domain
->nested
[1])
1441 isl_space_free(space
);
1444 isl_space_free(space
);
1445 isl_space_free(domain
);
1449 /* Internal function that selects the range of the map that is
1450 * embedded in either a set space or the range of a map space.
1451 * In particular, given a space of the form [A -> B], return the space B.
1452 * Given a space of the form A -> [B -> C], return the space A -> C.
1454 static __isl_give isl_space
*range_factor_range(__isl_take isl_space
*space
)
1462 nested
= space
->nested
[1];
1463 range
= isl_space_copy(space
);
1464 range
= isl_space_drop_dims(range
, isl_dim_out
, 0, nested
->n_in
);
1466 return isl_space_free(space
);
1467 if (nested
->tuple_id
[1]) {
1468 range
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[1]);
1469 if (!range
->tuple_id
[1])
1472 if (nested
->nested
[1]) {
1473 range
->nested
[1] = isl_space_copy(nested
->nested
[1]);
1474 if (!range
->nested
[1])
1478 isl_space_free(space
);
1481 isl_space_free(space
);
1482 isl_space_free(range
);
1486 /* Given a space of the form A -> [B -> C], return the space A -> C.
1488 __isl_give isl_space
*isl_space_range_factor_range(
1489 __isl_take isl_space
*space
)
1493 if (!isl_space_range_is_wrapping(space
))
1494 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1495 "range not a product", return isl_space_free(space
));
1497 return range_factor_range(space
);
1500 /* Given a space of the form [A -> B], return the space B.
1502 static __isl_give isl_space
*set_factor_range(__isl_take isl_space
*space
)
1506 if (!isl_space_is_wrapping(space
))
1507 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1508 "not a product", return isl_space_free(space
));
1510 return range_factor_range(space
);
1513 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1514 * Given a space of the form [A -> B], return the space B.
1516 __isl_give isl_space
*isl_space_factor_range(__isl_take isl_space
*space
)
1520 if (isl_space_is_set(space
))
1521 return set_factor_range(space
);
1522 space
= isl_space_domain_factor_range(space
);
1523 space
= isl_space_range_factor_range(space
);
1527 __isl_give isl_space
*isl_space_map_from_set(__isl_take isl_space
*space
)
1530 isl_id
**ids
= NULL
;
1535 ctx
= isl_space_get_ctx(space
);
1536 if (!isl_space_is_set(space
))
1537 isl_die(ctx
, isl_error_invalid
, "not a set space", goto error
);
1538 space
= isl_space_cow(space
);
1541 n_id
= space
->nparam
+ space
->n_out
+ space
->n_out
;
1542 if (n_id
> 0 && space
->ids
) {
1543 ids
= isl_calloc_array(space
->ctx
, isl_id
*, n_id
);
1546 get_ids(space
, isl_dim_param
, 0, space
->nparam
, ids
);
1547 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
1548 ids
+ space
->nparam
);
1550 space
->n_in
= space
->n_out
;
1555 space
= copy_ids(space
, isl_dim_out
, 0, space
, isl_dim_in
);
1557 isl_id_free(space
->tuple_id
[0]);
1558 space
->tuple_id
[0] = isl_id_copy(space
->tuple_id
[1]);
1559 isl_space_free(space
->nested
[0]);
1560 space
->nested
[0] = isl_space_copy(space
->nested
[1]);
1563 isl_space_free(space
);
1567 __isl_give isl_space
*isl_space_map_from_domain_and_range(
1568 __isl_take isl_space
*domain
, __isl_take isl_space
*range
)
1570 if (!domain
|| !range
)
1572 if (!isl_space_is_set(domain
))
1573 isl_die(isl_space_get_ctx(domain
), isl_error_invalid
,
1574 "domain is not a set space", goto error
);
1575 if (!isl_space_is_set(range
))
1576 isl_die(isl_space_get_ctx(range
), isl_error_invalid
,
1577 "range is not a set space", goto error
);
1578 return isl_space_join(isl_space_reverse(domain
), range
);
1580 isl_space_free(domain
);
1581 isl_space_free(range
);
1585 static __isl_give isl_space
*set_ids(__isl_take isl_space
*dim
,
1586 enum isl_dim_type type
,
1587 unsigned first
, unsigned n
, __isl_take isl_id
**ids
)
1591 for (i
= 0; i
< n
; ++i
)
1592 dim
= set_id(dim
, type
, first
+ i
, ids
[i
]);
1597 __isl_give isl_space
*isl_space_reverse(__isl_take isl_space
*dim
)
1601 isl_id
**ids
= NULL
;
1606 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
1609 dim
= isl_space_cow(dim
);
1613 id
= dim
->tuple_id
[0];
1614 dim
->tuple_id
[0] = dim
->tuple_id
[1];
1615 dim
->tuple_id
[1] = id
;
1617 nested
= dim
->nested
[0];
1618 dim
->nested
[0] = dim
->nested
[1];
1619 dim
->nested
[1] = nested
;
1622 int n_id
= dim
->n_in
+ dim
->n_out
;
1623 ids
= isl_alloc_array(dim
->ctx
, isl_id
*, n_id
);
1626 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
);
1627 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->n_in
);
1631 dim
->n_in
= dim
->n_out
;
1635 dim
= set_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
);
1636 dim
= set_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
+ dim
->n_out
);
1643 isl_space_free(dim
);
1647 __isl_give isl_space
*isl_space_drop_dims(__isl_take isl_space
*dim
,
1648 enum isl_dim_type type
, unsigned first
, unsigned num
)
1656 return isl_space_reset(dim
, type
);
1658 if (!valid_dim_type(type
))
1659 isl_die(dim
->ctx
, isl_error_invalid
,
1660 "cannot drop dimensions of specified type", goto error
);
1662 if (first
+ num
> n(dim
, type
) || first
+ num
< first
)
1663 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1664 "index out of bounds", return isl_space_free(dim
));
1665 dim
= isl_space_cow(dim
);
1669 dim
= extend_ids(dim
);
1672 for (i
= 0; i
< num
; ++i
)
1673 isl_id_free(get_id(dim
, type
, first
+ i
));
1674 for (i
= first
+num
; i
< n(dim
, type
); ++i
)
1675 set_id(dim
, type
, i
- num
, get_id(dim
, type
, i
));
1678 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
,
1679 dim
->ids
+ offset(dim
, isl_dim_in
) - num
);
1681 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
,
1682 dim
->ids
+ offset(dim
, isl_dim_out
) - num
);
1689 case isl_dim_param
: dim
->nparam
-= num
; break;
1690 case isl_dim_in
: dim
->n_in
-= num
; break;
1691 case isl_dim_out
: dim
->n_out
-= num
; break;
1694 dim
= isl_space_reset(dim
, type
);
1695 if (type
== isl_dim_param
) {
1696 if (dim
&& dim
->nested
[0] &&
1697 !(dim
->nested
[0] = isl_space_drop_dims(dim
->nested
[0],
1698 isl_dim_param
, first
, num
)))
1700 if (dim
&& dim
->nested
[1] &&
1701 !(dim
->nested
[1] = isl_space_drop_dims(dim
->nested
[1],
1702 isl_dim_param
, first
, num
)))
1707 isl_space_free(dim
);
1711 __isl_give isl_space
*isl_space_drop_inputs(__isl_take isl_space
*dim
,
1712 unsigned first
, unsigned n
)
1716 return isl_space_drop_dims(dim
, isl_dim_in
, first
, n
);
1719 __isl_give isl_space
*isl_space_drop_outputs(__isl_take isl_space
*dim
,
1720 unsigned first
, unsigned n
)
1724 return isl_space_drop_dims(dim
, isl_dim_out
, first
, n
);
1727 __isl_give isl_space
*isl_space_domain(__isl_take isl_space
*space
)
1731 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, space
->n_out
);
1732 space
= isl_space_reverse(space
);
1733 space
= mark_as_set(space
);
1737 __isl_give isl_space
*isl_space_from_domain(__isl_take isl_space
*dim
)
1741 if (!isl_space_is_set(dim
))
1742 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1743 "not a set space", goto error
);
1744 dim
= isl_space_reverse(dim
);
1745 dim
= isl_space_reset(dim
, isl_dim_out
);
1748 isl_space_free(dim
);
1752 __isl_give isl_space
*isl_space_range(__isl_take isl_space
*space
)
1756 space
= isl_space_drop_dims(space
, isl_dim_in
, 0, space
->n_in
);
1757 space
= mark_as_set(space
);
1761 __isl_give isl_space
*isl_space_from_range(__isl_take isl_space
*dim
)
1765 if (!isl_space_is_set(dim
))
1766 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1767 "not a set space", goto error
);
1768 return isl_space_reset(dim
, isl_dim_in
);
1770 isl_space_free(dim
);
1774 /* Given a map space A -> B, return the map space [A -> B] -> A.
1776 __isl_give isl_space
*isl_space_domain_map(__isl_take isl_space
*space
)
1780 domain
= isl_space_from_range(isl_space_domain(isl_space_copy(space
)));
1781 space
= isl_space_from_domain(isl_space_wrap(space
));
1782 space
= isl_space_join(space
, domain
);
1787 /* Given a map space A -> B, return the map space [A -> B] -> B.
1789 __isl_give isl_space
*isl_space_range_map(__isl_take isl_space
*space
)
1793 range
= isl_space_from_range(isl_space_range(isl_space_copy(space
)));
1794 space
= isl_space_from_domain(isl_space_wrap(space
));
1795 space
= isl_space_join(space
, range
);
1800 __isl_give isl_space
*isl_space_params(__isl_take isl_space
*space
)
1802 if (isl_space_is_params(space
))
1804 space
= isl_space_drop_dims(space
,
1805 isl_dim_in
, 0, isl_space_dim(space
, isl_dim_in
));
1806 space
= isl_space_drop_dims(space
,
1807 isl_dim_out
, 0, isl_space_dim(space
, isl_dim_out
));
1808 space
= mark_as_params(space
);
1812 __isl_give isl_space
*isl_space_set_from_params(__isl_take isl_space
*space
)
1816 if (!isl_space_is_params(space
))
1817 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1818 "not a parameter space", goto error
);
1819 return isl_space_reset(space
, isl_dim_set
);
1821 isl_space_free(space
);
1825 __isl_give isl_space
*isl_space_underlying(__isl_take isl_space
*dim
,
1833 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_id
== 0)
1834 return isl_space_reset(isl_space_reset(dim
, isl_dim_in
), isl_dim_out
);
1835 dim
= isl_space_cow(dim
);
1838 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
1842 for (i
= 0; i
< dim
->n_id
; ++i
)
1843 isl_id_free(get_id(dim
, isl_dim_out
, i
));
1845 dim
= isl_space_reset(dim
, isl_dim_in
);
1846 dim
= isl_space_reset(dim
, isl_dim_out
);
1851 /* Are the two spaces the same, including positions and names of parameters?
1853 isl_bool
isl_space_is_equal(__isl_keep isl_space
*space1
,
1854 __isl_keep isl_space
*space2
)
1858 if (!space1
|| !space2
)
1859 return isl_bool_error
;
1860 if (space1
== space2
)
1861 return isl_bool_true
;
1862 equal
= isl_space_has_equal_params(space1
, space2
);
1863 if (equal
< 0 || !equal
)
1865 return isl_space_has_equal_tuples(space1
, space2
);
1868 /* Is space1 equal to the domain of space2?
1870 * In the internal version we also allow space2 to be the space of a set,
1871 * provided space1 is a parameter space.
1873 isl_bool
isl_space_is_domain_internal(__isl_keep isl_space
*space1
,
1874 __isl_keep isl_space
*space2
)
1876 isl_bool equal_params
;
1878 if (!space1
|| !space2
)
1879 return isl_bool_error
;
1880 if (!isl_space_is_set(space1
))
1881 return isl_bool_false
;
1882 equal_params
= isl_space_has_equal_params(space1
, space2
);
1883 if (equal_params
< 0 || !equal_params
)
1884 return equal_params
;
1885 return isl_space_tuple_is_equal(space1
, isl_dim_set
,
1886 space2
, isl_dim_in
);
1889 /* Is space1 equal to the domain of space2?
1891 isl_bool
isl_space_is_domain(__isl_keep isl_space
*space1
,
1892 __isl_keep isl_space
*space2
)
1895 return isl_bool_error
;
1896 if (!isl_space_is_map(space2
))
1897 return isl_bool_false
;
1898 return isl_space_is_domain_internal(space1
, space2
);
1901 /* Is space1 equal to the range of space2?
1903 * In the internal version, space2 is allowed to be the space of a set,
1904 * in which case it should be equal to space1.
1906 isl_bool
isl_space_is_range_internal(__isl_keep isl_space
*space1
,
1907 __isl_keep isl_space
*space2
)
1909 isl_bool equal_params
;
1911 if (!space1
|| !space2
)
1912 return isl_bool_error
;
1913 if (!isl_space_is_set(space1
))
1914 return isl_bool_false
;
1915 equal_params
= isl_space_has_equal_params(space1
, space2
);
1916 if (equal_params
< 0 || !equal_params
)
1917 return equal_params
;
1918 return isl_space_tuple_is_equal(space1
, isl_dim_set
,
1919 space2
, isl_dim_out
);
1922 /* Is space1 equal to the range of space2?
1924 isl_bool
isl_space_is_range(__isl_keep isl_space
*space1
,
1925 __isl_keep isl_space
*space2
)
1928 return isl_bool_error
;
1929 if (!isl_space_is_map(space2
))
1930 return isl_bool_false
;
1931 return isl_space_is_range_internal(space1
, space2
);
1934 /* Update "hash" by hashing in "space".
1935 * Changes in this function should be reflected in isl_hash_space_domain.
1937 static uint32_t isl_hash_space(uint32_t hash
, __isl_keep isl_space
*space
)
1945 isl_hash_byte(hash
, space
->nparam
% 256);
1946 isl_hash_byte(hash
, space
->n_in
% 256);
1947 isl_hash_byte(hash
, space
->n_out
% 256);
1949 for (i
= 0; i
< space
->nparam
; ++i
) {
1950 id
= get_id(space
, isl_dim_param
, i
);
1951 hash
= isl_hash_id(hash
, id
);
1954 id
= tuple_id(space
, isl_dim_in
);
1955 hash
= isl_hash_id(hash
, id
);
1956 id
= tuple_id(space
, isl_dim_out
);
1957 hash
= isl_hash_id(hash
, id
);
1959 hash
= isl_hash_space(hash
, space
->nested
[0]);
1960 hash
= isl_hash_space(hash
, space
->nested
[1]);
1965 /* Update "hash" by hashing in the domain of "space".
1966 * The result of this function is equal to the result of applying
1967 * isl_hash_space to the domain of "space".
1969 static uint32_t isl_hash_space_domain(uint32_t hash
,
1970 __isl_keep isl_space
*space
)
1978 isl_hash_byte(hash
, space
->nparam
% 256);
1979 isl_hash_byte(hash
, 0);
1980 isl_hash_byte(hash
, space
->n_in
% 256);
1982 for (i
= 0; i
< space
->nparam
; ++i
) {
1983 id
= get_id(space
, isl_dim_param
, i
);
1984 hash
= isl_hash_id(hash
, id
);
1987 hash
= isl_hash_id(hash
, &isl_id_none
);
1988 id
= tuple_id(space
, isl_dim_in
);
1989 hash
= isl_hash_id(hash
, id
);
1991 hash
= isl_hash_space(hash
, space
->nested
[0]);
1996 uint32_t isl_space_get_hash(__isl_keep isl_space
*dim
)
2003 hash
= isl_hash_init();
2004 hash
= isl_hash_space(hash
, dim
);
2009 /* Return the hash value of the domain of "space".
2010 * That is, isl_space_get_domain_hash(space) is equal to
2011 * isl_space_get_hash(isl_space_domain(space)).
2013 uint32_t isl_space_get_domain_hash(__isl_keep isl_space
*space
)
2020 hash
= isl_hash_init();
2021 hash
= isl_hash_space_domain(hash
, space
);
2026 isl_bool
isl_space_is_wrapping(__isl_keep isl_space
*dim
)
2029 return isl_bool_error
;
2031 if (!isl_space_is_set(dim
))
2032 return isl_bool_false
;
2034 return dim
->nested
[1] != NULL
;
2037 /* Is "space" the space of a map where the domain is a wrapped map space?
2039 isl_bool
isl_space_domain_is_wrapping(__isl_keep isl_space
*space
)
2042 return isl_bool_error
;
2044 if (isl_space_is_set(space
))
2045 return isl_bool_false
;
2047 return space
->nested
[0] != NULL
;
2050 /* Is "space" the space of a map where the range is a wrapped map space?
2052 isl_bool
isl_space_range_is_wrapping(__isl_keep isl_space
*space
)
2055 return isl_bool_error
;
2057 if (isl_space_is_set(space
))
2058 return isl_bool_false
;
2060 return space
->nested
[1] != NULL
;
2063 __isl_give isl_space
*isl_space_wrap(__isl_take isl_space
*dim
)
2070 wrap
= isl_space_set_alloc(dim
->ctx
,
2071 dim
->nparam
, dim
->n_in
+ dim
->n_out
);
2073 wrap
= copy_ids(wrap
, isl_dim_param
, 0, dim
, isl_dim_param
);
2074 wrap
= copy_ids(wrap
, isl_dim_set
, 0, dim
, isl_dim_in
);
2075 wrap
= copy_ids(wrap
, isl_dim_set
, dim
->n_in
, dim
, isl_dim_out
);
2080 wrap
->nested
[1] = dim
;
2084 isl_space_free(dim
);
2088 __isl_give isl_space
*isl_space_unwrap(__isl_take isl_space
*dim
)
2095 if (!isl_space_is_wrapping(dim
))
2096 isl_die(dim
->ctx
, isl_error_invalid
, "not a wrapping space",
2099 unwrap
= isl_space_copy(dim
->nested
[1]);
2100 isl_space_free(dim
);
2104 isl_space_free(dim
);
2108 isl_bool
isl_space_is_named_or_nested(__isl_keep isl_space
*space
,
2109 enum isl_dim_type type
)
2111 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
2112 return isl_bool_false
;
2114 return isl_bool_error
;
2115 if (space
->tuple_id
[type
- isl_dim_in
])
2116 return isl_bool_true
;
2117 if (space
->nested
[type
- isl_dim_in
])
2118 return isl_bool_true
;
2119 return isl_bool_false
;
2122 isl_bool
isl_space_may_be_set(__isl_keep isl_space
*space
)
2127 return isl_bool_error
;
2128 if (isl_space_is_set(space
))
2129 return isl_bool_true
;
2130 if (isl_space_dim(space
, isl_dim_in
) != 0)
2131 return isl_bool_false
;
2132 nested
= isl_space_is_named_or_nested(space
, isl_dim_in
);
2133 if (nested
< 0 || nested
)
2134 return isl_bool_not(nested
);
2135 return isl_bool_true
;
2138 __isl_give isl_space
*isl_space_reset(__isl_take isl_space
*dim
,
2139 enum isl_dim_type type
)
2141 if (!isl_space_is_named_or_nested(dim
, type
))
2144 dim
= isl_space_cow(dim
);
2148 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
2149 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
2150 isl_space_free(dim
->nested
[type
- isl_dim_in
]);
2151 dim
->nested
[type
- isl_dim_in
] = NULL
;
2156 __isl_give isl_space
*isl_space_flatten(__isl_take isl_space
*dim
)
2160 if (!dim
->nested
[0] && !dim
->nested
[1])
2164 dim
= isl_space_reset(dim
, isl_dim_in
);
2165 if (dim
&& dim
->nested
[1])
2166 dim
= isl_space_reset(dim
, isl_dim_out
);
2171 __isl_give isl_space
*isl_space_flatten_domain(__isl_take isl_space
*dim
)
2175 if (!dim
->nested
[0])
2178 return isl_space_reset(dim
, isl_dim_in
);
2181 __isl_give isl_space
*isl_space_flatten_range(__isl_take isl_space
*dim
)
2185 if (!dim
->nested
[1])
2188 return isl_space_reset(dim
, isl_dim_out
);
2191 /* Replace the dimensions of the given type of dst by those of src.
2193 __isl_give isl_space
*isl_space_replace(__isl_take isl_space
*dst
,
2194 enum isl_dim_type type
, __isl_keep isl_space
*src
)
2196 dst
= isl_space_cow(dst
);
2201 dst
= isl_space_drop_dims(dst
, type
, 0, isl_space_dim(dst
, type
));
2202 dst
= isl_space_add_dims(dst
, type
, isl_space_dim(src
, type
));
2203 dst
= copy_ids(dst
, type
, 0, src
, type
);
2205 if (dst
&& type
== isl_dim_param
) {
2207 for (i
= 0; i
<= 1; ++i
) {
2208 if (!dst
->nested
[i
])
2210 dst
->nested
[i
] = isl_space_replace(dst
->nested
[i
],
2212 if (!dst
->nested
[i
])
2219 isl_space_free(dst
);
2223 /* Given a dimension specification "dim" of a set, create a dimension
2224 * specification for the lift of the set. In particular, the result
2225 * is of the form [dim -> local[..]], with n_local variables in the
2226 * range of the wrapped map.
2228 __isl_give isl_space
*isl_space_lift(__isl_take isl_space
*dim
, unsigned n_local
)
2230 isl_space
*local_dim
;
2235 local_dim
= isl_space_dup(dim
);
2236 local_dim
= isl_space_drop_dims(local_dim
, isl_dim_set
, 0, dim
->n_out
);
2237 local_dim
= isl_space_add_dims(local_dim
, isl_dim_set
, n_local
);
2238 local_dim
= isl_space_set_tuple_name(local_dim
, isl_dim_set
, "local");
2239 dim
= isl_space_join(isl_space_from_domain(dim
),
2240 isl_space_from_range(local_dim
));
2241 dim
= isl_space_wrap(dim
);
2242 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, "lifted");
2247 isl_bool
isl_space_can_zip(__isl_keep isl_space
*dim
)
2250 return isl_bool_error
;
2252 return dim
->nested
[0] && dim
->nested
[1];
2255 __isl_give isl_space
*isl_space_zip(__isl_take isl_space
*dim
)
2257 isl_space
*dom
, *ran
;
2258 isl_space
*dom_dom
, *dom_ran
, *ran_dom
, *ran_ran
;
2260 if (!isl_space_can_zip(dim
))
2261 isl_die(dim
->ctx
, isl_error_invalid
, "dim cannot be zipped",
2266 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(dim
)));
2267 ran
= isl_space_unwrap(isl_space_range(dim
));
2268 dom_dom
= isl_space_domain(isl_space_copy(dom
));
2269 dom_ran
= isl_space_range(dom
);
2270 ran_dom
= isl_space_domain(isl_space_copy(ran
));
2271 ran_ran
= isl_space_range(ran
);
2272 dom
= isl_space_join(isl_space_from_domain(dom_dom
),
2273 isl_space_from_range(ran_dom
));
2274 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
2275 isl_space_from_range(ran_ran
));
2276 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
2277 isl_space_from_range(isl_space_wrap(ran
)));
2279 isl_space_free(dim
);
2283 /* Can we apply isl_space_curry to "space"?
2284 * That is, does it have a nested relation in its domain?
2286 isl_bool
isl_space_can_curry(__isl_keep isl_space
*space
)
2289 return isl_bool_error
;
2291 return !!space
->nested
[0];
2294 /* Given a space (A -> B) -> C, return the corresponding space
2297 __isl_give isl_space
*isl_space_curry(__isl_take isl_space
*space
)
2299 isl_space
*dom
, *ran
;
2300 isl_space
*dom_dom
, *dom_ran
;
2305 if (!isl_space_can_curry(space
))
2306 isl_die(space
->ctx
, isl_error_invalid
,
2307 "space cannot be curried", goto error
);
2309 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(space
)));
2310 ran
= isl_space_range(space
);
2311 dom_dom
= isl_space_domain(isl_space_copy(dom
));
2312 dom_ran
= isl_space_range(dom
);
2313 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
2314 isl_space_from_range(ran
));
2315 return isl_space_join(isl_space_from_domain(dom_dom
),
2316 isl_space_from_range(isl_space_wrap(ran
)));
2318 isl_space_free(space
);
2322 /* Can isl_space_range_curry be applied to "space"?
2323 * That is, does it have a nested relation in its range,
2324 * the domain of which is itself a nested relation?
2326 isl_bool
isl_space_can_range_curry(__isl_keep isl_space
*space
)
2331 return isl_bool_error
;
2332 can
= isl_space_range_is_wrapping(space
);
2333 if (can
< 0 || !can
)
2335 return isl_space_can_curry(space
->nested
[1]);
2338 /* Given a space A -> ((B -> C) -> D), return the corresponding space
2339 * A -> (B -> (C -> D)).
2341 __isl_give isl_space
*isl_space_range_curry(__isl_take isl_space
*space
)
2346 if (!isl_space_can_range_curry(space
))
2347 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2348 "space range cannot be curried",
2349 return isl_space_free(space
));
2351 space
= isl_space_cow(space
);
2354 space
->nested
[1] = isl_space_curry(space
->nested
[1]);
2355 if (!space
->nested
[1])
2356 return isl_space_free(space
);
2361 /* Can we apply isl_space_uncurry to "space"?
2362 * That is, does it have a nested relation in its range?
2364 isl_bool
isl_space_can_uncurry(__isl_keep isl_space
*space
)
2367 return isl_bool_error
;
2369 return !!space
->nested
[1];
2372 /* Given a space A -> (B -> C), return the corresponding space
2375 __isl_give isl_space
*isl_space_uncurry(__isl_take isl_space
*space
)
2377 isl_space
*dom
, *ran
;
2378 isl_space
*ran_dom
, *ran_ran
;
2383 if (!isl_space_can_uncurry(space
))
2384 isl_die(space
->ctx
, isl_error_invalid
,
2385 "space cannot be uncurried",
2386 return isl_space_free(space
));
2388 dom
= isl_space_domain(isl_space_copy(space
));
2389 ran
= isl_space_unwrap(isl_space_range(space
));
2390 ran_dom
= isl_space_domain(isl_space_copy(ran
));
2391 ran_ran
= isl_space_range(ran
);
2392 dom
= isl_space_join(isl_space_from_domain(dom
),
2393 isl_space_from_range(ran_dom
));
2394 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
2395 isl_space_from_range(ran_ran
));
2398 isl_bool
isl_space_has_named_params(__isl_keep isl_space
*space
)
2404 return isl_bool_error
;
2405 if (space
->nparam
== 0)
2406 return isl_bool_true
;
2407 off
= isl_space_offset(space
, isl_dim_param
);
2408 if (off
+ space
->nparam
> space
->n_id
)
2409 return isl_bool_false
;
2410 for (i
= 0; i
< space
->nparam
; ++i
)
2411 if (!space
->ids
[off
+ i
])
2412 return isl_bool_false
;
2413 return isl_bool_true
;
2416 /* Check that "space" has only named parameters, reporting an error
2419 isl_stat
isl_space_check_named_params(__isl_keep isl_space
*space
)
2423 named
= isl_space_has_named_params(space
);
2425 return isl_stat_error
;
2427 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2428 "unaligned unnamed parameters", return isl_stat_error
);
2433 /* Align the initial parameters of dim1 to match the order in dim2.
2435 __isl_give isl_space
*isl_space_align_params(__isl_take isl_space
*dim1
,
2436 __isl_take isl_space
*dim2
)
2438 isl_reordering
*exp
;
2440 if (!isl_space_has_named_params(dim1
) || !isl_space_has_named_params(dim2
))
2441 isl_die(isl_space_get_ctx(dim1
), isl_error_invalid
,
2442 "parameter alignment requires named parameters",
2445 dim2
= isl_space_params(dim2
);
2446 exp
= isl_parameter_alignment_reordering(dim1
, dim2
);
2447 exp
= isl_reordering_extend_space(exp
, dim1
);
2448 isl_space_free(dim2
);
2451 dim1
= isl_space_copy(exp
->dim
);
2452 isl_reordering_free(exp
);
2455 isl_space_free(dim1
);
2456 isl_space_free(dim2
);
2460 /* Given the space of set (domain), construct a space for a map
2461 * with as domain the given space and as range the range of "model".
2463 __isl_give isl_space
*isl_space_extend_domain_with_range(
2464 __isl_take isl_space
*space
, __isl_take isl_space
*model
)
2469 space
= isl_space_from_domain(space
);
2470 space
= isl_space_add_dims(space
, isl_dim_out
,
2471 isl_space_dim(model
, isl_dim_out
));
2472 if (isl_space_has_tuple_id(model
, isl_dim_out
))
2473 space
= isl_space_set_tuple_id(space
, isl_dim_out
,
2474 isl_space_get_tuple_id(model
, isl_dim_out
));
2477 if (model
->nested
[1]) {
2478 isl_space
*nested
= isl_space_copy(model
->nested
[1]);
2479 int n_nested
, n_space
;
2480 nested
= isl_space_align_params(nested
, isl_space_copy(space
));
2481 n_nested
= isl_space_dim(nested
, isl_dim_param
);
2482 n_space
= isl_space_dim(space
, isl_dim_param
);
2483 if (n_nested
> n_space
)
2484 nested
= isl_space_drop_dims(nested
, isl_dim_param
,
2485 n_space
, n_nested
- n_space
);
2488 space
->nested
[1] = nested
;
2490 isl_space_free(model
);
2493 isl_space_free(model
);
2494 isl_space_free(space
);
2498 /* Compare the "type" dimensions of two isl_spaces.
2500 * The order is fairly arbitrary.
2502 static int isl_space_cmp_type(__isl_keep isl_space
*space1
,
2503 __isl_keep isl_space
*space2
, enum isl_dim_type type
)
2506 isl_space
*nested1
, *nested2
;
2508 if (isl_space_dim(space1
, type
) != isl_space_dim(space2
, type
))
2509 return isl_space_dim(space1
, type
) -
2510 isl_space_dim(space2
, type
);
2512 cmp
= isl_id_cmp(tuple_id(space1
, type
), tuple_id(space2
, type
));
2516 nested1
= nested(space1
, type
);
2517 nested2
= nested(space2
, type
);
2518 if (!nested1
!= !nested2
)
2519 return !nested1
- !nested2
;
2522 return isl_space_cmp(nested1
, nested2
);
2527 /* Compare two isl_spaces.
2529 * The order is fairly arbitrary.
2531 int isl_space_cmp(__isl_keep isl_space
*space1
, __isl_keep isl_space
*space2
)
2536 if (space1
== space2
)
2543 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_param
);
2546 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_in
);
2549 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_out
);
2553 if (!space1
->ids
&& !space2
->ids
)
2556 for (i
= 0; i
< n(space1
, isl_dim_param
); ++i
) {
2557 cmp
= isl_id_cmp(get_id(space1
, isl_dim_param
, i
),
2558 get_id(space2
, isl_dim_param
, i
));