2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2013-2014 Ecole Normale Superieure
5 * Copyright 2018-2019 Cerebras Systems
7 * Use of this software is governed by the MIT license
9 * Written by Sven Verdoolaege, K.U.Leuven, Departement
10 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
12 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
13 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
14 * and Cerebras Systems, 175 S San Antonio Rd, Los Altos, CA, USA
19 #include <isl_space_private.h>
20 #include <isl_id_private.h>
21 #include <isl_reordering.h>
23 isl_ctx
*isl_space_get_ctx(__isl_keep isl_space
*space
)
25 return space
? space
->ctx
: NULL
;
28 __isl_give isl_space
*isl_space_alloc(isl_ctx
*ctx
,
29 unsigned nparam
, unsigned n_in
, unsigned n_out
)
33 space
= isl_alloc_type(ctx
, struct isl_space
);
40 space
->nparam
= nparam
;
44 space
->tuple_id
[0] = NULL
;
45 space
->tuple_id
[1] = NULL
;
47 space
->nested
[0] = NULL
;
48 space
->nested
[1] = NULL
;
56 /* Mark the space as being that of a set, by setting the domain tuple
59 static __isl_give isl_space
*mark_as_set(__isl_take isl_space
*space
)
61 space
= isl_space_cow(space
);
64 space
= isl_space_set_tuple_id(space
, isl_dim_in
, &isl_id_none
);
68 /* Is the space that of a set?
70 isl_bool
isl_space_is_set(__isl_keep isl_space
*space
)
73 return isl_bool_error
;
74 if (space
->n_in
!= 0 || space
->nested
[0])
75 return isl_bool_false
;
76 if (space
->tuple_id
[0] != &isl_id_none
)
77 return isl_bool_false
;
81 /* Check that "space" is a set space.
83 isl_stat
isl_space_check_is_set(__isl_keep isl_space
*space
)
87 is_set
= isl_space_is_set(space
);
89 return isl_stat_error
;
91 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
92 "space is not a set", return isl_stat_error
);
96 /* Is the given space that of a map?
98 isl_bool
isl_space_is_map(__isl_keep isl_space
*space
)
103 return isl_bool_error
;
104 r
= space
->tuple_id
[0] != &isl_id_none
&&
105 space
->tuple_id
[1] != &isl_id_none
;
106 return isl_bool_ok(r
);
109 /* Check that "space" is the space of a map.
111 static isl_stat
isl_space_check_is_map(__isl_keep isl_space
*space
)
115 is_space
= isl_space_is_map(space
);
117 return isl_stat_error
;
119 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
120 "expecting map space", return isl_stat_error
);
124 /* Check that "space" is the space of a map
125 * where the domain is a wrapped map space.
127 isl_stat
isl_space_check_domain_is_wrapping(__isl_keep isl_space
*space
)
131 wrapping
= isl_space_domain_is_wrapping(space
);
133 return isl_stat_error
;
135 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
136 "domain not a product", return isl_stat_error
);
140 /* Check that "space" is the space of a map
141 * where the range is a wrapped map space.
143 isl_stat
isl_space_check_range_is_wrapping(__isl_keep isl_space
*space
)
147 wrapping
= isl_space_range_is_wrapping(space
);
149 return isl_stat_error
;
151 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
152 "range not a product", return isl_stat_error
);
156 __isl_give isl_space
*isl_space_set_alloc(isl_ctx
*ctx
,
157 unsigned nparam
, unsigned dim
)
160 space
= isl_space_alloc(ctx
, nparam
, 0, dim
);
161 space
= mark_as_set(space
);
165 /* Mark the space as being that of a parameter domain, by setting
166 * both tuples to isl_id_none.
168 static __isl_give isl_space
*mark_as_params(isl_space
*space
)
172 space
= isl_space_set_tuple_id(space
, isl_dim_in
, &isl_id_none
);
173 space
= isl_space_set_tuple_id(space
, isl_dim_out
, &isl_id_none
);
177 /* Is the space that of a parameter domain?
179 isl_bool
isl_space_is_params(__isl_keep isl_space
*space
)
182 return isl_bool_error
;
183 if (space
->n_in
!= 0 || space
->nested
[0] ||
184 space
->n_out
!= 0 || space
->nested
[1])
185 return isl_bool_false
;
186 if (space
->tuple_id
[0] != &isl_id_none
)
187 return isl_bool_false
;
188 if (space
->tuple_id
[1] != &isl_id_none
)
189 return isl_bool_false
;
190 return isl_bool_true
;
193 /* Create a space for a parameter domain.
195 __isl_give isl_space
*isl_space_params_alloc(isl_ctx
*ctx
, unsigned nparam
)
198 space
= isl_space_alloc(ctx
, nparam
, 0, 0);
199 space
= mark_as_params(space
);
203 /* Create a space for a parameter domain, without any parameters.
205 __isl_give isl_space
*isl_space_unit(isl_ctx
*ctx
)
207 return isl_space_params_alloc(ctx
, 0);
210 static isl_size
global_pos(__isl_keep isl_space
*space
,
211 enum isl_dim_type type
, unsigned pos
)
213 if (isl_space_check_range(space
, type
, pos
, 1) < 0)
214 return isl_size_error
;
220 return pos
+ space
->nparam
;
222 return pos
+ space
->nparam
+ space
->n_in
;
224 isl_assert(isl_space_get_ctx(space
), 0, return isl_size_error
);
226 return isl_size_error
;
229 /* Extend length of ids array to the total number of dimensions.
231 static __isl_give isl_space
*extend_ids(__isl_take isl_space
*space
)
237 dim
= isl_space_dim(space
, isl_dim_all
);
239 return isl_space_free(space
);
240 if (dim
<= space
->n_id
)
244 space
->ids
= isl_calloc_array(space
->ctx
, isl_id
*, dim
);
248 ids
= isl_realloc_array(space
->ctx
, space
->ids
, isl_id
*, dim
);
252 for (i
= space
->n_id
; i
< dim
; ++i
)
253 space
->ids
[i
] = NULL
;
260 isl_space_free(space
);
264 static __isl_give isl_space
*set_id(__isl_take isl_space
*space
,
265 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
269 space
= isl_space_cow(space
);
271 gpos
= global_pos(space
, type
, pos
);
275 if (gpos
>= space
->n_id
) {
278 space
= extend_ids(space
);
283 space
->ids
[gpos
] = id
;
288 isl_space_free(space
);
292 static __isl_keep isl_id
*get_id(__isl_keep isl_space
*space
,
293 enum isl_dim_type type
, unsigned pos
)
297 gpos
= global_pos(space
, type
, pos
);
300 if (gpos
>= space
->n_id
)
302 return space
->ids
[gpos
];
305 /* Return the nested space at the given position.
307 static __isl_keep isl_space
*isl_space_peek_nested(__isl_keep isl_space
*space
,
312 if (!space
->nested
[pos
])
313 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
314 "no nested space", return NULL
);
315 return space
->nested
[pos
];
318 static unsigned offset(__isl_keep isl_space
*space
, enum isl_dim_type type
)
321 case isl_dim_param
: return 0;
322 case isl_dim_in
: return space
->nparam
;
323 case isl_dim_out
: return space
->nparam
+ space
->n_in
;
328 static unsigned n(__isl_keep isl_space
*space
, enum isl_dim_type type
)
331 case isl_dim_param
: return space
->nparam
;
332 case isl_dim_in
: return space
->n_in
;
333 case isl_dim_out
: return space
->n_out
;
335 return space
->nparam
+ space
->n_in
+ space
->n_out
;
340 isl_size
isl_space_dim(__isl_keep isl_space
*space
, enum isl_dim_type type
)
343 return isl_size_error
;
344 return n(space
, type
);
347 /* Return the dimensionality of tuple "inner" within the wrapped relation
348 * inside tuple "outer".
350 isl_size
isl_space_wrapped_dim(__isl_keep isl_space
*space
,
351 enum isl_dim_type outer
, enum isl_dim_type inner
)
356 return isl_size_error
;
357 if (outer
!= isl_dim_in
&& outer
!= isl_dim_out
)
358 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
359 "only input, output and set tuples "
360 "can have nested relations", return isl_size_error
);
361 pos
= outer
- isl_dim_in
;
362 return isl_space_dim(isl_space_peek_nested(space
, pos
), inner
);
365 unsigned isl_space_offset(__isl_keep isl_space
*space
, enum isl_dim_type type
)
369 return offset(space
, type
);
372 static __isl_give isl_space
*copy_ids(__isl_take isl_space
*dst
,
373 enum isl_dim_type dst_type
, unsigned offset
, __isl_keep isl_space
*src
,
374 enum isl_dim_type src_type
)
382 for (i
= 0; i
< n(src
, src_type
); ++i
) {
383 id
= get_id(src
, src_type
, i
);
386 dst
= set_id(dst
, dst_type
, offset
+ i
, isl_id_copy(id
));
393 __isl_give isl_space
*isl_space_dup(__isl_keep isl_space
*space
)
398 dup
= isl_space_alloc(space
->ctx
,
399 space
->nparam
, space
->n_in
, space
->n_out
);
402 if (space
->tuple_id
[0] &&
403 !(dup
->tuple_id
[0] = isl_id_copy(space
->tuple_id
[0])))
405 if (space
->tuple_id
[1] &&
406 !(dup
->tuple_id
[1] = isl_id_copy(space
->tuple_id
[1])))
408 if (space
->nested
[0] &&
409 !(dup
->nested
[0] = isl_space_copy(space
->nested
[0])))
411 if (space
->nested
[1] &&
412 !(dup
->nested
[1] = isl_space_copy(space
->nested
[1])))
416 dup
= copy_ids(dup
, isl_dim_param
, 0, space
, isl_dim_param
);
417 dup
= copy_ids(dup
, isl_dim_in
, 0, space
, isl_dim_in
);
418 dup
= copy_ids(dup
, isl_dim_out
, 0, space
, isl_dim_out
);
425 __isl_give isl_space
*isl_space_cow(__isl_take isl_space
*space
)
433 return isl_space_dup(space
);
436 __isl_give isl_space
*isl_space_copy(__isl_keep isl_space
*space
)
445 __isl_null isl_space
*isl_space_free(__isl_take isl_space
*space
)
452 if (--space
->ref
> 0)
455 isl_id_free(space
->tuple_id
[0]);
456 isl_id_free(space
->tuple_id
[1]);
458 isl_space_free(space
->nested
[0]);
459 isl_space_free(space
->nested
[1]);
461 for (i
= 0; i
< space
->n_id
; ++i
)
462 isl_id_free(space
->ids
[i
]);
464 isl_ctx_deref(space
->ctx
);
471 /* Check if "s" is a valid dimension or tuple name.
472 * We currently only forbid names that look like a number.
474 * s is assumed to be non-NULL.
476 static int name_ok(isl_ctx
*ctx
, const char *s
)
482 isl_die(ctx
, isl_error_invalid
, "name looks like a number",
488 /* Return a copy of the nested space at the given position.
490 static __isl_keep isl_space
*isl_space_get_nested(__isl_keep isl_space
*space
,
493 return isl_space_copy(isl_space_peek_nested(space
, pos
));
496 /* Return the nested space at the given position.
497 * This may be either a copy or the nested space itself
498 * if there is only one reference to "space".
499 * This allows the nested space to be modified inplace
500 * if both "space" and the nested space have only a single reference.
501 * The caller is not allowed to modify "space" between this call and
502 * a subsequent call to isl_space_restore_nested.
503 * The only exception is that isl_space_free can be called instead.
505 static __isl_give isl_space
*isl_space_take_nested(__isl_keep isl_space
*space
,
513 return isl_space_get_nested(space
, pos
);
514 nested
= space
->nested
[pos
];
515 space
->nested
[pos
] = NULL
;
519 /* Replace the nested space at the given position by "nested",
520 * where this nested space of "space" may be missing
521 * due to a preceding call to isl_space_take_nested.
522 * However, in this case, "space" only has a single reference and
523 * then the call to isl_space_cow has no effect.
525 static __isl_give isl_space
*isl_space_restore_nested(
526 __isl_take isl_space
*space
, int pos
, __isl_take isl_space
*nested
)
528 if (!space
|| !nested
)
531 if (space
->nested
[pos
] == nested
) {
532 isl_space_free(nested
);
536 space
= isl_space_cow(space
);
539 isl_space_free(space
->nested
[pos
]);
540 space
->nested
[pos
] = nested
;
544 isl_space_free(space
);
545 isl_space_free(nested
);
549 /* Is it possible for the given dimension type to have a tuple id?
551 static int space_can_have_id(__isl_keep isl_space
*space
,
552 enum isl_dim_type type
)
556 if (isl_space_is_params(space
))
557 isl_die(space
->ctx
, isl_error_invalid
,
558 "parameter spaces don't have tuple ids", return 0);
559 if (isl_space_is_set(space
) && type
!= isl_dim_set
)
560 isl_die(space
->ctx
, isl_error_invalid
,
561 "set spaces can only have a set id", return 0);
562 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
563 isl_die(space
->ctx
, isl_error_invalid
,
564 "only input, output and set tuples can have ids",
570 /* Does the tuple have an id?
572 isl_bool
isl_space_has_tuple_id(__isl_keep isl_space
*space
,
573 enum isl_dim_type type
)
575 if (!space_can_have_id(space
, type
))
576 return isl_bool_error
;
577 return isl_bool_ok(space
->tuple_id
[type
- isl_dim_in
] != NULL
);
580 /* Does the domain tuple of the map space "space" have an identifier?
582 isl_bool
isl_space_has_domain_tuple_id(__isl_keep isl_space
*space
)
584 if (isl_space_check_is_map(space
) < 0)
585 return isl_bool_error
;
586 return isl_space_has_tuple_id(space
, isl_dim_in
);
589 /* Does the range tuple of the map space "space" have an identifier?
591 isl_bool
isl_space_has_range_tuple_id(__isl_keep isl_space
*space
)
593 if (isl_space_check_is_map(space
) < 0)
594 return isl_bool_error
;
595 return isl_space_has_tuple_id(space
, isl_dim_out
);
598 __isl_give isl_id
*isl_space_get_tuple_id(__isl_keep isl_space
*space
,
599 enum isl_dim_type type
)
605 has_id
= isl_space_has_tuple_id(space
, type
);
609 isl_die(space
->ctx
, isl_error_invalid
,
610 "tuple has no id", return NULL
);
611 return isl_id_copy(space
->tuple_id
[type
- isl_dim_in
]);
614 /* Return the identifier of the domain tuple of the map space "space",
615 * assuming it has one.
617 __isl_give isl_id
*isl_space_get_domain_tuple_id(
618 __isl_keep isl_space
*space
)
620 if (isl_space_check_is_map(space
) < 0)
622 return isl_space_get_tuple_id(space
, isl_dim_in
);
625 /* Return the identifier of the range tuple of the map space "space",
626 * assuming it has one.
628 __isl_give isl_id
*isl_space_get_range_tuple_id(
629 __isl_keep isl_space
*space
)
631 if (isl_space_check_is_map(space
) < 0)
633 return isl_space_get_tuple_id(space
, isl_dim_out
);
636 __isl_give isl_space
*isl_space_set_tuple_id(__isl_take isl_space
*space
,
637 enum isl_dim_type type
, __isl_take isl_id
*id
)
639 space
= isl_space_cow(space
);
642 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
643 isl_die(space
->ctx
, isl_error_invalid
,
644 "only input, output and set tuples can have names",
647 isl_id_free(space
->tuple_id
[type
- isl_dim_in
]);
648 space
->tuple_id
[type
- isl_dim_in
] = id
;
653 isl_space_free(space
);
657 /* Replace the identifier of the domain tuple of the map space "space"
660 __isl_give isl_space
*isl_space_set_domain_tuple_id(
661 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
663 if (isl_space_check_is_map(space
) < 0)
664 space
= isl_space_free(space
);
665 return isl_space_set_tuple_id(space
, isl_dim_in
, id
);
668 /* Replace the identifier of the range tuple of the map space "space"
671 __isl_give isl_space
*isl_space_set_range_tuple_id(
672 __isl_take isl_space
*space
, __isl_take isl_id
*id
)
674 if (isl_space_check_is_map(space
) < 0)
675 space
= isl_space_free(space
);
676 return isl_space_set_tuple_id(space
, isl_dim_out
, id
);
679 __isl_give isl_space
*isl_space_reset_tuple_id(__isl_take isl_space
*space
,
680 enum isl_dim_type type
)
682 space
= isl_space_cow(space
);
685 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
686 isl_die(space
->ctx
, isl_error_invalid
,
687 "only input, output and set tuples can have names",
690 isl_id_free(space
->tuple_id
[type
- isl_dim_in
]);
691 space
->tuple_id
[type
- isl_dim_in
] = NULL
;
695 isl_space_free(space
);
699 /* Set the id of the given dimension of "space" to "id".
700 * If the dimension already has an id, then it is replaced.
701 * If the dimension is a parameter, then we need to change it
702 * in the nested spaces (if any) as well.
704 __isl_give isl_space
*isl_space_set_dim_id(__isl_take isl_space
*space
,
705 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
707 space
= isl_space_cow(space
);
711 if (type
== isl_dim_param
) {
714 for (i
= 0; i
< 2; ++i
) {
715 if (!space
->nested
[i
])
718 isl_space_set_dim_id(space
->nested
[i
],
719 type
, pos
, isl_id_copy(id
));
720 if (!space
->nested
[i
])
725 isl_id_free(get_id(space
, type
, pos
));
726 return set_id(space
, type
, pos
, id
);
729 isl_space_free(space
);
733 /* Reset the id of the given dimension of "space".
734 * If the dimension already has an id, then it is removed.
735 * If the dimension is a parameter, then we need to reset it
736 * in the nested spaces (if any) as well.
738 __isl_give isl_space
*isl_space_reset_dim_id(__isl_take isl_space
*space
,
739 enum isl_dim_type type
, unsigned pos
)
741 space
= isl_space_cow(space
);
745 if (type
== isl_dim_param
) {
748 for (i
= 0; i
< 2; ++i
) {
749 if (!space
->nested
[i
])
752 isl_space_reset_dim_id(space
->nested
[i
],
754 if (!space
->nested
[i
])
759 isl_id_free(get_id(space
, type
, pos
));
760 return set_id(space
, type
, pos
, NULL
);
762 isl_space_free(space
);
766 isl_bool
isl_space_has_dim_id(__isl_keep isl_space
*space
,
767 enum isl_dim_type type
, unsigned pos
)
770 return isl_bool_error
;
771 return isl_bool_ok(get_id(space
, type
, pos
) != NULL
);
774 __isl_give isl_id
*isl_space_get_dim_id(__isl_keep isl_space
*space
,
775 enum isl_dim_type type
, unsigned pos
)
779 if (!get_id(space
, type
, pos
))
780 isl_die(space
->ctx
, isl_error_invalid
,
781 "dim has no id", return NULL
);
782 return isl_id_copy(get_id(space
, type
, pos
));
785 __isl_give isl_space
*isl_space_set_tuple_name(__isl_take isl_space
*space
,
786 enum isl_dim_type type
, const char *s
)
794 return isl_space_reset_tuple_id(space
, type
);
796 if (!name_ok(space
->ctx
, s
))
799 id
= isl_id_alloc(space
->ctx
, s
, NULL
);
800 return isl_space_set_tuple_id(space
, type
, id
);
802 isl_space_free(space
);
806 /* Does the tuple have a name?
808 isl_bool
isl_space_has_tuple_name(__isl_keep isl_space
*space
,
809 enum isl_dim_type type
)
813 if (!space_can_have_id(space
, type
))
814 return isl_bool_error
;
815 id
= space
->tuple_id
[type
- isl_dim_in
];
816 return isl_bool_ok(id
&& id
->name
);
819 __isl_keep
const char *isl_space_get_tuple_name(__isl_keep isl_space
*space
,
820 enum isl_dim_type type
)
825 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
827 id
= space
->tuple_id
[type
- isl_dim_in
];
828 return id
? id
->name
: NULL
;
831 __isl_give isl_space
*isl_space_set_dim_name(__isl_take isl_space
*space
,
832 enum isl_dim_type type
, unsigned pos
,
840 return isl_space_reset_dim_id(space
, type
, pos
);
841 if (!name_ok(space
->ctx
, s
))
843 id
= isl_id_alloc(space
->ctx
, s
, NULL
);
844 return isl_space_set_dim_id(space
, type
, pos
, id
);
846 isl_space_free(space
);
850 /* Does the given dimension have a name?
852 isl_bool
isl_space_has_dim_name(__isl_keep isl_space
*space
,
853 enum isl_dim_type type
, unsigned pos
)
858 return isl_bool_error
;
859 id
= get_id(space
, type
, pos
);
860 return isl_bool_ok(id
&& id
->name
);
863 __isl_keep
const char *isl_space_get_dim_name(__isl_keep isl_space
*space
,
864 enum isl_dim_type type
, unsigned pos
)
866 isl_id
*id
= get_id(space
, type
, pos
);
867 return id
? id
->name
: NULL
;
870 int isl_space_find_dim_by_id(__isl_keep isl_space
*space
,
871 enum isl_dim_type type
, __isl_keep isl_id
*id
)
877 n
= isl_space_dim(space
, type
);
881 offset
= isl_space_offset(space
, type
);
882 for (i
= 0; i
< n
&& offset
+ i
< space
->n_id
; ++i
)
883 if (space
->ids
[offset
+ i
] == id
)
889 int isl_space_find_dim_by_name(__isl_keep isl_space
*space
,
890 enum isl_dim_type type
, const char *name
)
896 n
= isl_space_dim(space
, type
);
900 offset
= isl_space_offset(space
, type
);
901 for (i
= 0; i
< n
&& offset
+ i
< space
->n_id
; ++i
) {
902 isl_id
*id
= get_id(space
, type
, i
);
903 if (id
&& id
->name
&& !strcmp(id
->name
, name
))
910 /* Reset the user pointer on all identifiers of parameters and tuples
913 __isl_give isl_space
*isl_space_reset_user(__isl_take isl_space
*space
)
923 ctx
= isl_space_get_ctx(space
);
925 for (i
= 0; i
< space
->nparam
&& i
< space
->n_id
; ++i
) {
926 if (!isl_id_get_user(space
->ids
[i
]))
928 space
= isl_space_cow(space
);
931 name
= isl_id_get_name(space
->ids
[i
]);
932 id
= isl_id_alloc(ctx
, name
, NULL
);
933 isl_id_free(space
->ids
[i
]);
936 return isl_space_free(space
);
939 for (i
= 0; i
< 2; ++i
) {
940 if (!space
->tuple_id
[i
])
942 if (!isl_id_get_user(space
->tuple_id
[i
]))
944 space
= isl_space_cow(space
);
947 name
= isl_id_get_name(space
->tuple_id
[i
]);
948 id
= isl_id_alloc(ctx
, name
, NULL
);
949 isl_id_free(space
->tuple_id
[i
]);
950 space
->tuple_id
[i
] = id
;
952 return isl_space_free(space
);
955 for (i
= 0; i
< 2; ++i
) {
958 if (!space
->nested
[i
])
960 nested
= isl_space_take_nested(space
, i
);
961 nested
= isl_space_reset_user(nested
);
962 space
= isl_space_restore_nested(space
, i
, nested
);
970 static __isl_keep isl_id
*tuple_id(__isl_keep isl_space
*space
,
971 enum isl_dim_type type
)
975 if (type
== isl_dim_in
)
976 return space
->tuple_id
[0];
977 if (type
== isl_dim_out
)
978 return space
->tuple_id
[1];
982 static __isl_keep isl_space
*nested(__isl_keep isl_space
*space
,
983 enum isl_dim_type type
)
987 if (type
== isl_dim_in
)
988 return space
->nested
[0];
989 if (type
== isl_dim_out
)
990 return space
->nested
[1];
994 /* Are the two spaces the same, apart from positions and names of parameters?
996 isl_bool
isl_space_has_equal_tuples(__isl_keep isl_space
*space1
,
997 __isl_keep isl_space
*space2
)
999 if (!space1
|| !space2
)
1000 return isl_bool_error
;
1001 if (space1
== space2
)
1002 return isl_bool_true
;
1003 return isl_space_tuple_is_equal(space1
, isl_dim_in
,
1004 space2
, isl_dim_in
) &&
1005 isl_space_tuple_is_equal(space1
, isl_dim_out
,
1006 space2
, isl_dim_out
);
1009 /* Check that a match involving "space" was successful.
1010 * That is, check that "match" is equal to isl_bool_true.
1012 static isl_stat
check_match(__isl_keep isl_space
*space
, isl_bool match
)
1015 return isl_stat_error
;
1017 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1018 "incompatible spaces", return isl_stat_error
);
1023 /* Check that the two spaces are the same,
1024 * apart from positions and names of parameters.
1026 isl_stat
isl_space_check_equal_tuples(__isl_keep isl_space
*space1
,
1027 __isl_keep isl_space
*space2
)
1031 is_equal
= isl_space_has_equal_tuples(space1
, space2
);
1032 return check_match(space1
, is_equal
);
1035 /* Check if the tuple of type "type1" of "space1" is the same as
1036 * the tuple of type "type2" of "space2".
1038 * That is, check if the tuples have the same identifier, the same dimension
1039 * and the same internal structure.
1040 * The identifiers of the dimensions inside the tuples do not affect the result.
1042 * Note that this function only checks the tuples themselves.
1043 * If nested tuples are involved, then we need to be careful not
1044 * to have result affected by possibly differing parameters
1045 * in those nested tuples.
1047 isl_bool
isl_space_tuple_is_equal(__isl_keep isl_space
*space1
,
1048 enum isl_dim_type type1
, __isl_keep isl_space
*space2
,
1049 enum isl_dim_type type2
)
1052 isl_space
*nested1
, *nested2
;
1054 if (!space1
|| !space2
)
1055 return isl_bool_error
;
1057 if (space1
== space2
&& type1
== type2
)
1058 return isl_bool_true
;
1060 if (n(space1
, type1
) != n(space2
, type2
))
1061 return isl_bool_false
;
1062 id1
= tuple_id(space1
, type1
);
1063 id2
= tuple_id(space2
, type2
);
1065 return isl_bool_false
;
1066 if (id1
&& id1
!= id2
)
1067 return isl_bool_false
;
1068 nested1
= nested(space1
, type1
);
1069 nested2
= nested(space2
, type2
);
1070 if (!nested1
^ !nested2
)
1071 return isl_bool_false
;
1072 if (nested1
&& !isl_space_has_equal_tuples(nested1
, nested2
))
1073 return isl_bool_false
;
1074 return isl_bool_true
;
1077 /* Is the tuple "inner" within the wrapped relation inside tuple "outer"
1078 * of "space1" equal to tuple "type2" of "space2"?
1080 isl_bool
isl_space_wrapped_tuple_is_equal(__isl_keep isl_space
*space1
,
1081 enum isl_dim_type outer
, enum isl_dim_type inner
,
1082 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
1088 return isl_bool_error
;
1089 if (outer
!= isl_dim_in
&& outer
!= isl_dim_out
)
1090 isl_die(isl_space_get_ctx(space1
), isl_error_invalid
,
1091 "only input, output and set tuples "
1092 "can have nested relations", return isl_bool_error
);
1093 pos
= outer
- isl_dim_in
;
1094 nested
= isl_space_peek_nested(space1
, pos
);
1095 return isl_space_tuple_is_equal(nested
, inner
, space2
, type2
);
1098 /* Check that the tuple "inner" within the wrapped relation inside tuple "outer"
1099 * of "space1" is equal to tuple "type2" of "space2".
1101 isl_stat
isl_space_check_wrapped_tuple_is_equal(__isl_keep isl_space
*space1
,
1102 enum isl_dim_type outer
, enum isl_dim_type inner
,
1103 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
1107 is_equal
= isl_space_wrapped_tuple_is_equal(space1
, outer
, inner
,
1109 return check_match(space1
, is_equal
);
1112 static isl_bool
match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
1113 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
1118 if (!space1
|| !space2
)
1119 return isl_bool_error
;
1121 if (space1
== space2
&& type1
== type2
)
1122 return isl_bool_true
;
1124 equal
= isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
1125 if (equal
< 0 || !equal
)
1128 if (!space1
->ids
&& !space2
->ids
)
1129 return isl_bool_true
;
1131 for (i
= 0; i
< n(space1
, type1
); ++i
) {
1132 if (get_id(space1
, type1
, i
) != get_id(space2
, type2
, i
))
1133 return isl_bool_false
;
1135 return isl_bool_true
;
1138 /* Do "space1" and "space2" have the same parameters?
1140 isl_bool
isl_space_has_equal_params(__isl_keep isl_space
*space1
,
1141 __isl_keep isl_space
*space2
)
1143 return match(space1
, isl_dim_param
, space2
, isl_dim_param
);
1146 /* Do "space1" and "space2" have the same identifiers for all
1147 * the tuple variables?
1149 isl_bool
isl_space_has_equal_ids(__isl_keep isl_space
*space1
,
1150 __isl_keep isl_space
*space2
)
1154 equal
= match(space1
, isl_dim_in
, space2
, isl_dim_in
);
1155 if (equal
< 0 || !equal
)
1157 return match(space1
, isl_dim_out
, space2
, isl_dim_out
);
1160 isl_bool
isl_space_match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
1161 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
1163 return match(space1
, type1
, space2
, type2
);
1166 static void get_ids(__isl_keep isl_space
*space
, enum isl_dim_type type
,
1167 unsigned first
, unsigned n
, __isl_keep isl_id
**ids
)
1171 for (i
= 0; i
< n
; ++i
)
1172 ids
[i
] = get_id(space
, type
, first
+ i
);
1175 static __isl_give isl_space
*space_extend(__isl_take isl_space
*space
,
1176 unsigned nparam
, unsigned n_in
, unsigned n_out
)
1178 isl_id
**ids
= NULL
;
1182 if (space
->nparam
== nparam
&&
1183 space
->n_in
== n_in
&& space
->n_out
== n_out
)
1186 isl_assert(space
->ctx
, space
->nparam
<= nparam
, goto error
);
1187 isl_assert(space
->ctx
, space
->n_in
<= n_in
, goto error
);
1188 isl_assert(space
->ctx
, space
->n_out
<= n_out
, goto error
);
1190 space
= isl_space_cow(space
);
1196 n
= nparam
+ n_in
+ n_out
;
1197 if (n
< nparam
|| n
< n_in
|| n
< n_out
)
1198 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1199 "overflow in total number of dimensions",
1201 ids
= isl_calloc_array(space
->ctx
, isl_id
*, n
);
1204 get_ids(space
, isl_dim_param
, 0, space
->nparam
, ids
);
1205 get_ids(space
, isl_dim_in
, 0, space
->n_in
, ids
+ nparam
);
1206 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
1207 ids
+ nparam
+ n_in
);
1210 space
->n_id
= nparam
+ n_in
+ n_out
;
1212 space
->nparam
= nparam
;
1214 space
->n_out
= n_out
;
1219 isl_space_free(space
);
1223 __isl_give isl_space
*isl_space_extend(__isl_take isl_space
*space
,
1224 unsigned nparam
, unsigned n_in
, unsigned n_out
)
1226 return space_extend(space
, nparam
, n_in
, n_out
);
1229 __isl_give isl_space
*isl_space_add_dims(__isl_take isl_space
*space
,
1230 enum isl_dim_type type
, unsigned n
)
1232 space
= isl_space_reset(space
, type
);
1237 space
= space_extend(space
,
1238 space
->nparam
+ n
, space
->n_in
, space
->n_out
);
1239 if (space
&& space
->nested
[0] &&
1240 !(space
->nested
[0] = isl_space_add_dims(space
->nested
[0],
1243 if (space
&& space
->nested
[1] &&
1244 !(space
->nested
[1] = isl_space_add_dims(space
->nested
[1],
1249 return space_extend(space
,
1250 space
->nparam
, space
->n_in
+ n
, space
->n_out
);
1252 return space_extend(space
,
1253 space
->nparam
, space
->n_in
, space
->n_out
+ n
);
1255 isl_die(space
->ctx
, isl_error_invalid
,
1256 "cannot add dimensions of specified type", goto error
);
1259 isl_space_free(space
);
1263 /* Add a parameter with identifier "id" to "space", provided
1264 * it does not already appear in "space".
1266 __isl_give isl_space
*isl_space_add_param_id(__isl_take isl_space
*space
,
1267 __isl_take isl_id
*id
)
1274 if (isl_space_find_dim_by_id(space
, isl_dim_param
, id
) >= 0) {
1279 pos
= isl_space_dim(space
, isl_dim_param
);
1282 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
1283 space
= isl_space_set_dim_id(space
, isl_dim_param
, pos
, id
);
1287 isl_space_free(space
);
1292 static int valid_dim_type(enum isl_dim_type type
)
1305 #define TYPE isl_space
1306 #include "check_type_range_templ.c"
1308 /* Insert "n" dimensions of type "type" at position "pos".
1309 * If we are inserting parameters, then they are also inserted in
1310 * any nested spaces.
1312 __isl_give isl_space
*isl_space_insert_dims(__isl_take isl_space
*space
,
1313 enum isl_dim_type type
, unsigned pos
, unsigned n
)
1316 isl_id
**ids
= NULL
;
1321 return isl_space_reset(space
, type
);
1323 ctx
= isl_space_get_ctx(space
);
1324 if (!valid_dim_type(type
))
1325 isl_die(ctx
, isl_error_invalid
,
1326 "cannot insert dimensions of specified type",
1329 if (isl_space_check_range(space
, type
, pos
, 0) < 0)
1330 return isl_space_free(space
);
1332 space
= isl_space_cow(space
);
1337 enum isl_dim_type t
, o
= isl_dim_param
;
1340 ids
= isl_calloc_array(ctx
, isl_id
*,
1341 space
->nparam
+ space
->n_in
+ space
->n_out
+ n
);
1345 s
[isl_dim_param
- o
] = space
->nparam
;
1346 s
[isl_dim_in
- o
] = space
->n_in
;
1347 s
[isl_dim_out
- o
] = space
->n_out
;
1348 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1350 get_ids(space
, t
, 0, s
[t
- o
], ids
+ off
);
1353 get_ids(space
, t
, 0, pos
, ids
+ off
);
1355 get_ids(space
, t
, pos
, s
[t
- o
] - pos
,
1357 off
+= s
[t
- o
] - pos
;
1362 space
->n_id
= space
->nparam
+ space
->n_in
+ space
->n_out
+ n
;
1365 case isl_dim_param
: space
->nparam
+= n
; break;
1366 case isl_dim_in
: space
->n_in
+= n
; break;
1367 case isl_dim_out
: space
->n_out
+= n
; break;
1370 space
= isl_space_reset(space
, type
);
1372 if (type
== isl_dim_param
) {
1373 if (space
&& space
->nested
[0] &&
1374 !(space
->nested
[0] = isl_space_insert_dims(space
->nested
[0],
1375 isl_dim_param
, pos
, n
)))
1377 if (space
&& space
->nested
[1] &&
1378 !(space
->nested
[1] = isl_space_insert_dims(space
->nested
[1],
1379 isl_dim_param
, pos
, n
)))
1385 isl_space_free(space
);
1389 __isl_give isl_space
*isl_space_move_dims(__isl_take isl_space
*space
,
1390 enum isl_dim_type dst_type
, unsigned dst_pos
,
1391 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1395 space
= isl_space_reset(space
, src_type
);
1396 space
= isl_space_reset(space
, dst_type
);
1402 if (isl_space_check_range(space
, src_type
, src_pos
, n
) < 0)
1403 return isl_space_free(space
);
1405 if (dst_type
== src_type
&& dst_pos
== src_pos
)
1408 isl_assert(space
->ctx
, dst_type
!= src_type
, goto error
);
1410 space
= isl_space_cow(space
);
1416 enum isl_dim_type t
, o
= isl_dim_param
;
1419 ids
= isl_calloc_array(space
->ctx
, isl_id
*,
1420 space
->nparam
+ space
->n_in
+ space
->n_out
);
1424 s
[isl_dim_param
- o
] = space
->nparam
;
1425 s
[isl_dim_in
- o
] = space
->n_in
;
1426 s
[isl_dim_out
- o
] = space
->n_out
;
1427 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1428 if (t
== dst_type
) {
1429 get_ids(space
, t
, 0, dst_pos
, ids
+ off
);
1431 get_ids(space
, src_type
, src_pos
, n
, ids
+ off
);
1433 get_ids(space
, t
, dst_pos
, s
[t
- o
] - dst_pos
,
1435 off
+= s
[t
- o
] - dst_pos
;
1436 } else if (t
== src_type
) {
1437 get_ids(space
, t
, 0, src_pos
, ids
+ off
);
1439 get_ids(space
, t
, src_pos
+ n
,
1440 s
[t
- o
] - src_pos
- n
, ids
+ off
);
1441 off
+= s
[t
- o
] - src_pos
- n
;
1443 get_ids(space
, t
, 0, s
[t
- o
], ids
+ off
);
1449 space
->n_id
= space
->nparam
+ space
->n_in
+ space
->n_out
;
1453 case isl_dim_param
: space
->nparam
+= n
; break;
1454 case isl_dim_in
: space
->n_in
+= n
; break;
1455 case isl_dim_out
: space
->n_out
+= n
; break;
1460 case isl_dim_param
: space
->nparam
-= n
; break;
1461 case isl_dim_in
: space
->n_in
-= n
; break;
1462 case isl_dim_out
: space
->n_out
-= n
; break;
1466 if (dst_type
!= isl_dim_param
&& src_type
!= isl_dim_param
)
1469 for (i
= 0; i
< 2; ++i
) {
1472 if (!space
->nested
[i
])
1474 nested
= isl_space_take_nested(space
, i
);
1475 nested
= isl_space_replace_params(nested
, space
);
1476 space
= isl_space_restore_nested(space
, i
, nested
);
1483 isl_space_free(space
);
1487 /* Check that "space1" and "space2" have the same parameters,
1488 * reporting an error if they do not.
1490 isl_stat
isl_space_check_equal_params(__isl_keep isl_space
*space1
,
1491 __isl_keep isl_space
*space2
)
1495 equal
= isl_space_has_equal_params(space1
, space2
);
1497 return isl_stat_error
;
1499 isl_die(isl_space_get_ctx(space1
), isl_error_invalid
,
1500 "parameters need to match", return isl_stat_error
);
1504 __isl_give isl_space
*isl_space_join(__isl_take isl_space
*left
,
1505 __isl_take isl_space
*right
)
1509 if (isl_space_check_equal_params(left
, right
) < 0)
1512 isl_assert(left
->ctx
,
1513 isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_in
),
1516 space
= isl_space_alloc(left
->ctx
,
1517 left
->nparam
, left
->n_in
, right
->n_out
);
1521 space
= copy_ids(space
, isl_dim_param
, 0, left
, isl_dim_param
);
1522 space
= copy_ids(space
, isl_dim_in
, 0, left
, isl_dim_in
);
1523 space
= copy_ids(space
, isl_dim_out
, 0, right
, isl_dim_out
);
1525 if (space
&& left
->tuple_id
[0] &&
1526 !(space
->tuple_id
[0] = isl_id_copy(left
->tuple_id
[0])))
1528 if (space
&& right
->tuple_id
[1] &&
1529 !(space
->tuple_id
[1] = isl_id_copy(right
->tuple_id
[1])))
1531 if (space
&& left
->nested
[0] &&
1532 !(space
->nested
[0] = isl_space_copy(left
->nested
[0])))
1534 if (space
&& right
->nested
[1] &&
1535 !(space
->nested
[1] = isl_space_copy(right
->nested
[1])))
1538 isl_space_free(left
);
1539 isl_space_free(right
);
1543 isl_space_free(left
);
1544 isl_space_free(right
);
1548 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1549 * { [A -> B] -> [C -> D] }.
1550 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1552 __isl_give isl_space
*isl_space_product(__isl_take isl_space
*left
,
1553 __isl_take isl_space
*right
)
1555 isl_space
*dom1
, *dom2
, *nest1
, *nest2
;
1558 if (!left
|| !right
)
1561 is_set
= isl_space_is_set(left
);
1562 if (is_set
!= isl_space_is_set(right
))
1563 isl_die(isl_space_get_ctx(left
), isl_error_invalid
,
1564 "expecting either two set spaces or two map spaces",
1567 return isl_space_range_product(left
, right
);
1569 if (isl_space_check_equal_params(left
, right
) < 0)
1572 dom1
= isl_space_domain(isl_space_copy(left
));
1573 dom2
= isl_space_domain(isl_space_copy(right
));
1574 nest1
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1576 dom1
= isl_space_range(left
);
1577 dom2
= isl_space_range(right
);
1578 nest2
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1580 return isl_space_join(isl_space_reverse(nest1
), nest2
);
1582 isl_space_free(left
);
1583 isl_space_free(right
);
1587 /* Given two spaces { A -> C } and { B -> C }, construct the space
1590 __isl_give isl_space
*isl_space_domain_product(__isl_take isl_space
*left
,
1591 __isl_take isl_space
*right
)
1593 isl_space
*ran
, *dom1
, *dom2
, *nest
;
1595 if (isl_space_check_equal_params(left
, right
) < 0)
1598 if (!isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_out
))
1599 isl_die(left
->ctx
, isl_error_invalid
,
1600 "ranges need to match", goto error
);
1602 ran
= isl_space_range(isl_space_copy(left
));
1604 dom1
= isl_space_domain(left
);
1605 dom2
= isl_space_domain(right
);
1606 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1608 return isl_space_join(isl_space_reverse(nest
), ran
);
1610 isl_space_free(left
);
1611 isl_space_free(right
);
1615 __isl_give isl_space
*isl_space_range_product(__isl_take isl_space
*left
,
1616 __isl_take isl_space
*right
)
1618 isl_space
*dom
, *ran1
, *ran2
, *nest
;
1620 if (isl_space_check_equal_params(left
, right
) < 0)
1623 if (!isl_space_tuple_is_equal(left
, isl_dim_in
, right
, isl_dim_in
))
1624 isl_die(left
->ctx
, isl_error_invalid
,
1625 "domains need to match", goto error
);
1627 dom
= isl_space_domain(isl_space_copy(left
));
1629 ran1
= isl_space_range(left
);
1630 ran2
= isl_space_range(right
);
1631 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(ran1
), ran2
));
1633 return isl_space_join(isl_space_reverse(dom
), nest
);
1635 isl_space_free(left
);
1636 isl_space_free(right
);
1640 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1642 __isl_give isl_space
*isl_space_domain_factor_domain(
1643 __isl_take isl_space
*space
)
1648 if (isl_space_check_domain_is_wrapping(space
) < 0)
1649 return isl_space_free(space
);
1651 nested
= space
->nested
[0];
1652 domain
= isl_space_copy(space
);
1653 domain
= isl_space_drop_dims(domain
, isl_dim_in
,
1654 nested
->n_in
, nested
->n_out
);
1656 return isl_space_free(space
);
1657 if (nested
->tuple_id
[0]) {
1658 domain
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[0]);
1659 if (!domain
->tuple_id
[0])
1662 if (nested
->nested
[0]) {
1663 domain
->nested
[0] = isl_space_copy(nested
->nested
[0]);
1664 if (!domain
->nested
[0])
1668 isl_space_free(space
);
1671 isl_space_free(space
);
1672 isl_space_free(domain
);
1676 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1678 __isl_give isl_space
*isl_space_domain_factor_range(
1679 __isl_take isl_space
*space
)
1684 if (isl_space_check_domain_is_wrapping(space
) < 0)
1685 return isl_space_free(space
);
1687 nested
= space
->nested
[0];
1688 range
= isl_space_copy(space
);
1689 range
= isl_space_drop_dims(range
, isl_dim_in
, 0, nested
->n_in
);
1691 return isl_space_free(space
);
1692 if (nested
->tuple_id
[1]) {
1693 range
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[1]);
1694 if (!range
->tuple_id
[0])
1697 if (nested
->nested
[1]) {
1698 range
->nested
[0] = isl_space_copy(nested
->nested
[1]);
1699 if (!range
->nested
[0])
1703 isl_space_free(space
);
1706 isl_space_free(space
);
1707 isl_space_free(range
);
1711 /* Internal function that selects the domain of the map that is
1712 * embedded in either a set space or the range of a map space.
1713 * In particular, given a space of the form [A -> B], return the space A.
1714 * Given a space of the form A -> [B -> C], return the space A -> B.
1716 static __isl_give isl_space
*range_factor_domain(__isl_take isl_space
*space
)
1724 nested
= space
->nested
[1];
1725 domain
= isl_space_copy(space
);
1726 domain
= isl_space_drop_dims(domain
, isl_dim_out
,
1727 nested
->n_in
, nested
->n_out
);
1729 return isl_space_free(space
);
1730 if (nested
->tuple_id
[0]) {
1731 domain
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[0]);
1732 if (!domain
->tuple_id
[1])
1735 if (nested
->nested
[0]) {
1736 domain
->nested
[1] = isl_space_copy(nested
->nested
[0]);
1737 if (!domain
->nested
[1])
1741 isl_space_free(space
);
1744 isl_space_free(space
);
1745 isl_space_free(domain
);
1749 /* Given a space of the form A -> [B -> C], return the space A -> B.
1751 __isl_give isl_space
*isl_space_range_factor_domain(
1752 __isl_take isl_space
*space
)
1754 if (isl_space_check_range_is_wrapping(space
) < 0)
1755 return isl_space_free(space
);
1757 return range_factor_domain(space
);
1760 /* Given a space of the form [A -> B], return the space A.
1762 static __isl_give isl_space
*set_factor_domain(__isl_take isl_space
*space
)
1766 if (!isl_space_is_wrapping(space
))
1767 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1768 "not a product", return isl_space_free(space
));
1770 return range_factor_domain(space
);
1773 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1774 * Given a space of the form [A -> B], return the space A.
1776 __isl_give isl_space
*isl_space_factor_domain(__isl_take isl_space
*space
)
1780 if (isl_space_is_set(space
))
1781 return set_factor_domain(space
);
1782 space
= isl_space_domain_factor_domain(space
);
1783 space
= isl_space_range_factor_domain(space
);
1787 /* Internal function that selects the range of the map that is
1788 * embedded in either a set space or the range of a map space.
1789 * In particular, given a space of the form [A -> B], return the space B.
1790 * Given a space of the form A -> [B -> C], return the space A -> C.
1792 static __isl_give isl_space
*range_factor_range(__isl_take isl_space
*space
)
1800 nested
= space
->nested
[1];
1801 range
= isl_space_copy(space
);
1802 range
= isl_space_drop_dims(range
, isl_dim_out
, 0, nested
->n_in
);
1804 return isl_space_free(space
);
1805 if (nested
->tuple_id
[1]) {
1806 range
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[1]);
1807 if (!range
->tuple_id
[1])
1810 if (nested
->nested
[1]) {
1811 range
->nested
[1] = isl_space_copy(nested
->nested
[1]);
1812 if (!range
->nested
[1])
1816 isl_space_free(space
);
1819 isl_space_free(space
);
1820 isl_space_free(range
);
1824 /* Given a space of the form A -> [B -> C], return the space A -> C.
1826 __isl_give isl_space
*isl_space_range_factor_range(
1827 __isl_take isl_space
*space
)
1829 if (isl_space_check_range_is_wrapping(space
) < 0)
1830 return isl_space_free(space
);
1832 return range_factor_range(space
);
1835 /* Given a space of the form [A -> B], return the space B.
1837 static __isl_give isl_space
*set_factor_range(__isl_take isl_space
*space
)
1841 if (!isl_space_is_wrapping(space
))
1842 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1843 "not a product", return isl_space_free(space
));
1845 return range_factor_range(space
);
1848 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1849 * Given a space of the form [A -> B], return the space B.
1851 __isl_give isl_space
*isl_space_factor_range(__isl_take isl_space
*space
)
1855 if (isl_space_is_set(space
))
1856 return set_factor_range(space
);
1857 space
= isl_space_domain_factor_range(space
);
1858 space
= isl_space_range_factor_range(space
);
1862 /* Given a space of the form [A -> B] -> C, return the space A.
1864 __isl_give isl_space
*isl_space_domain_wrapped_domain(
1865 __isl_take isl_space
*space
)
1867 return isl_space_factor_domain(isl_space_domain(space
));
1870 /* Given a space of the form [A -> B] -> C, return the space B.
1872 __isl_give isl_space
*isl_space_domain_wrapped_range(
1873 __isl_take isl_space
*space
)
1875 return isl_space_factor_range(isl_space_domain(space
));
1878 /* Given a space of the form A -> [B -> C], return the space B.
1880 __isl_give isl_space
*isl_space_range_wrapped_domain(
1881 __isl_take isl_space
*space
)
1883 return isl_space_factor_domain(isl_space_range(space
));
1886 /* Given a space of the form A -> [B -> C], return the space C.
1888 __isl_give isl_space
*isl_space_range_wrapped_range(
1889 __isl_take isl_space
*space
)
1891 return isl_space_factor_range(isl_space_range(space
));
1894 __isl_give isl_space
*isl_space_map_from_set(__isl_take isl_space
*space
)
1897 isl_id
**ids
= NULL
;
1902 ctx
= isl_space_get_ctx(space
);
1903 if (!isl_space_is_set(space
))
1904 isl_die(ctx
, isl_error_invalid
, "not a set space", goto error
);
1905 space
= isl_space_cow(space
);
1908 n_id
= space
->nparam
+ space
->n_out
+ space
->n_out
;
1909 if (n_id
> 0 && space
->ids
) {
1910 ids
= isl_calloc_array(space
->ctx
, isl_id
*, n_id
);
1913 get_ids(space
, isl_dim_param
, 0, space
->nparam
, ids
);
1914 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
1915 ids
+ space
->nparam
);
1917 space
->n_in
= space
->n_out
;
1922 space
= copy_ids(space
, isl_dim_out
, 0, space
, isl_dim_in
);
1924 isl_id_free(space
->tuple_id
[0]);
1925 space
->tuple_id
[0] = isl_id_copy(space
->tuple_id
[1]);
1926 isl_space_free(space
->nested
[0]);
1927 space
->nested
[0] = isl_space_copy(space
->nested
[1]);
1930 isl_space_free(space
);
1934 __isl_give isl_space
*isl_space_map_from_domain_and_range(
1935 __isl_take isl_space
*domain
, __isl_take isl_space
*range
)
1937 if (!domain
|| !range
)
1939 if (!isl_space_is_set(domain
))
1940 isl_die(isl_space_get_ctx(domain
), isl_error_invalid
,
1941 "domain is not a set space", goto error
);
1942 if (!isl_space_is_set(range
))
1943 isl_die(isl_space_get_ctx(range
), isl_error_invalid
,
1944 "range is not a set space", goto error
);
1945 return isl_space_join(isl_space_reverse(domain
), range
);
1947 isl_space_free(domain
);
1948 isl_space_free(range
);
1952 static __isl_give isl_space
*set_ids(__isl_take isl_space
*space
,
1953 enum isl_dim_type type
,
1954 unsigned first
, unsigned n
, __isl_take isl_id
**ids
)
1958 for (i
= 0; i
< n
; ++i
)
1959 space
= set_id(space
, type
, first
+ i
, ids
[i
]);
1964 __isl_give isl_space
*isl_space_reverse(__isl_take isl_space
*space
)
1969 isl_id
**ids
= NULL
;
1972 equal
= match(space
, isl_dim_in
, space
, isl_dim_out
);
1974 return isl_space_free(space
);
1978 space
= isl_space_cow(space
);
1982 id
= space
->tuple_id
[0];
1983 space
->tuple_id
[0] = space
->tuple_id
[1];
1984 space
->tuple_id
[1] = id
;
1986 nested
= space
->nested
[0];
1987 space
->nested
[0] = space
->nested
[1];
1988 space
->nested
[1] = nested
;
1991 int n_id
= space
->n_in
+ space
->n_out
;
1992 ids
= isl_alloc_array(space
->ctx
, isl_id
*, n_id
);
1995 get_ids(space
, isl_dim_in
, 0, space
->n_in
, ids
);
1996 get_ids(space
, isl_dim_out
, 0, space
->n_out
, ids
+ space
->n_in
);
2000 space
->n_in
= space
->n_out
;
2004 space
= set_ids(space
, isl_dim_out
, 0, space
->n_out
, ids
);
2005 space
= set_ids(space
, isl_dim_in
, 0, space
->n_in
,
2006 ids
+ space
->n_out
);
2013 isl_space_free(space
);
2017 /* Given a space A -> (B -> C), return the corresponding space
2020 * If the range tuple is named, then the name is only preserved
2021 * if B and C are equal tuples, in which case the output
2022 * of this function is identical to the input.
2024 __isl_give isl_space
*isl_space_range_reverse(__isl_take isl_space
*space
)
2029 if (isl_space_check_range_is_wrapping(space
) < 0)
2030 return isl_space_free(space
);
2032 nested
= isl_space_peek_nested(space
, 1);
2033 equal
= isl_space_tuple_is_equal(nested
, isl_dim_in
,
2034 nested
, isl_dim_out
);
2036 return isl_space_free(space
);
2038 nested
= isl_space_take_nested(space
, 1);
2039 nested
= isl_space_reverse(nested
);
2040 space
= isl_space_restore_nested(space
, 1, nested
);
2042 space
= isl_space_reset_tuple_id(space
, isl_dim_out
);
2047 __isl_give isl_space
*isl_space_drop_dims(__isl_take isl_space
*space
,
2048 enum isl_dim_type type
, unsigned first
, unsigned num
)
2056 return isl_space_reset(space
, type
);
2058 if (!valid_dim_type(type
))
2059 isl_die(space
->ctx
, isl_error_invalid
,
2060 "cannot drop dimensions of specified type", goto error
);
2062 if (isl_space_check_range(space
, type
, first
, num
) < 0)
2063 return isl_space_free(space
);
2064 space
= isl_space_cow(space
);
2068 space
= extend_ids(space
);
2071 for (i
= 0; i
< num
; ++i
)
2072 isl_id_free(get_id(space
, type
, first
+ i
));
2073 for (i
= first
+num
; i
< n(space
, type
); ++i
)
2074 set_id(space
, type
, i
- num
, get_id(space
, type
, i
));
2077 get_ids(space
, isl_dim_in
, 0, space
->n_in
,
2078 space
->ids
+ offset(space
, isl_dim_in
) - num
);
2080 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
2081 space
->ids
+ offset(space
, isl_dim_out
) - num
);
2088 case isl_dim_param
: space
->nparam
-= num
; break;
2089 case isl_dim_in
: space
->n_in
-= num
; break;
2090 case isl_dim_out
: space
->n_out
-= num
; break;
2093 space
= isl_space_reset(space
, type
);
2094 if (type
== isl_dim_param
) {
2095 if (space
&& space
->nested
[0] &&
2096 !(space
->nested
[0] = isl_space_drop_dims(space
->nested
[0],
2097 isl_dim_param
, first
, num
)))
2099 if (space
&& space
->nested
[1] &&
2100 !(space
->nested
[1] = isl_space_drop_dims(space
->nested
[1],
2101 isl_dim_param
, first
, num
)))
2106 isl_space_free(space
);
2110 __isl_give isl_space
*isl_space_drop_inputs(__isl_take isl_space
*space
,
2111 unsigned first
, unsigned n
)
2115 return isl_space_drop_dims(space
, isl_dim_in
, first
, n
);
2118 __isl_give isl_space
*isl_space_drop_outputs(__isl_take isl_space
*space
,
2119 unsigned first
, unsigned n
)
2123 return isl_space_drop_dims(space
, isl_dim_out
, first
, n
);
2126 /* Remove all parameters from "space".
2128 __isl_give isl_space
*isl_space_drop_all_params(__isl_take isl_space
*space
)
2132 nparam
= isl_space_dim(space
, isl_dim_param
);
2134 return isl_space_free(space
);
2135 return isl_space_drop_dims(space
, isl_dim_param
, 0, nparam
);
2138 __isl_give isl_space
*isl_space_domain(__isl_take isl_space
*space
)
2142 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, space
->n_out
);
2143 space
= isl_space_reverse(space
);
2144 space
= mark_as_set(space
);
2148 __isl_give isl_space
*isl_space_from_domain(__isl_take isl_space
*space
)
2152 if (!isl_space_is_set(space
))
2153 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2154 "not a set space", goto error
);
2155 space
= isl_space_reverse(space
);
2156 space
= isl_space_reset(space
, isl_dim_out
);
2159 isl_space_free(space
);
2163 __isl_give isl_space
*isl_space_range(__isl_take isl_space
*space
)
2167 space
= isl_space_drop_dims(space
, isl_dim_in
, 0, space
->n_in
);
2168 space
= mark_as_set(space
);
2172 __isl_give isl_space
*isl_space_from_range(__isl_take isl_space
*space
)
2176 if (!isl_space_is_set(space
))
2177 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2178 "not a set space", goto error
);
2179 return isl_space_reset(space
, isl_dim_in
);
2181 isl_space_free(space
);
2185 /* Given a map space A -> B, return the map space [A -> B] -> A.
2187 __isl_give isl_space
*isl_space_domain_map(__isl_take isl_space
*space
)
2191 domain
= isl_space_from_range(isl_space_domain(isl_space_copy(space
)));
2192 space
= isl_space_from_domain(isl_space_wrap(space
));
2193 space
= isl_space_join(space
, domain
);
2198 /* Given a map space A -> B, return the map space [A -> B] -> B.
2200 __isl_give isl_space
*isl_space_range_map(__isl_take isl_space
*space
)
2204 range
= isl_space_from_range(isl_space_range(isl_space_copy(space
)));
2205 space
= isl_space_from_domain(isl_space_wrap(space
));
2206 space
= isl_space_join(space
, range
);
2211 __isl_give isl_space
*isl_space_params(__isl_take isl_space
*space
)
2213 isl_size n_in
, n_out
;
2215 if (isl_space_is_params(space
))
2217 n_in
= isl_space_dim(space
, isl_dim_in
);
2218 n_out
= isl_space_dim(space
, isl_dim_out
);
2219 if (n_in
< 0 || n_out
< 0)
2220 return isl_space_free(space
);
2221 space
= isl_space_drop_dims(space
, isl_dim_in
, 0, n_in
);
2222 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, n_out
);
2223 space
= mark_as_params(space
);
2227 __isl_give isl_space
*isl_space_set_from_params(__isl_take isl_space
*space
)
2231 if (!isl_space_is_params(space
))
2232 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2233 "not a parameter space", goto error
);
2234 return isl_space_reset(space
, isl_dim_set
);
2236 isl_space_free(space
);
2240 /* Add an unnamed tuple of dimension "dim" to "space".
2241 * This requires "space" to be a parameter or set space.
2243 * In particular, if "space" is a parameter space, then return
2244 * a set space with the given dimension.
2245 * If "space" is a set space, then return a map space
2246 * with "space" as domain and a range of the given dimension.
2248 __isl_give isl_space
*isl_space_add_unnamed_tuple_ui(
2249 __isl_take isl_space
*space
, unsigned dim
)
2251 isl_bool is_params
, is_set
;
2253 is_params
= isl_space_is_params(space
);
2254 is_set
= isl_space_is_set(space
);
2255 if (is_params
< 0 || is_set
< 0)
2256 return isl_space_free(space
);
2257 if (!is_params
&& !is_set
)
2258 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2259 "cannot add tuple to map space",
2260 return isl_space_free(space
));
2262 space
= isl_space_set_from_params(space
);
2264 space
= isl_space_from_domain(space
);
2265 space
= isl_space_add_dims(space
, isl_dim_out
, dim
);
2269 /* Add a tuple of dimension "dim" and with tuple identifier "tuple_id"
2271 * This requires "space" to be a parameter or set space.
2273 __isl_give isl_space
*isl_space_add_named_tuple_id_ui(
2274 __isl_take isl_space
*space
, __isl_take isl_id
*tuple_id
, unsigned dim
)
2276 space
= isl_space_add_unnamed_tuple_ui(space
, dim
);
2277 space
= isl_space_set_tuple_id(space
, isl_dim_out
, tuple_id
);
2281 /* Check that the identifiers in "tuple" do not appear as parameters
2284 static isl_stat
check_fresh_params(__isl_keep isl_space
*space
,
2285 __isl_keep isl_multi_id
*tuple
)
2290 n
= isl_multi_id_size(tuple
);
2292 return isl_stat_error
;
2293 for (i
= 0; i
< n
; ++i
) {
2297 id
= isl_multi_id_get_at(tuple
, i
);
2299 return isl_stat_error
;
2300 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2303 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2304 "parameters not unique", return isl_stat_error
);
2310 /* Add the identifiers in "tuple" as parameters of "space"
2311 * that are known to be fresh.
2313 static __isl_give isl_space
*add_bind_params(__isl_take isl_space
*space
,
2314 __isl_keep isl_multi_id
*tuple
)
2319 first
= isl_space_dim(space
, isl_dim_param
);
2320 n
= isl_multi_id_size(tuple
);
2321 if (first
< 0 || n
< 0)
2322 return isl_space_free(space
);
2323 space
= isl_space_add_dims(space
, isl_dim_param
, n
);
2324 for (i
= 0; i
< n
; ++i
) {
2327 id
= isl_multi_id_get_at(tuple
, i
);
2328 space
= isl_space_set_dim_id(space
,
2329 isl_dim_param
, first
+ i
, id
);
2335 /* Internal function that removes the set tuple of "space",
2336 * which is assumed to correspond to the range space of "tuple", and
2337 * adds the identifiers in "tuple" as fresh parameters.
2338 * In other words, the set dimensions of "space" are reinterpreted
2339 * as parameters, but stay in the same global positions.
2341 __isl_give isl_space
*isl_space_bind_set(__isl_take isl_space
*space
,
2342 __isl_keep isl_multi_id
*tuple
)
2344 isl_space
*tuple_space
;
2346 if (isl_space_check_is_set(space
) < 0)
2347 return isl_space_free(space
);
2348 tuple_space
= isl_multi_id_peek_space(tuple
);
2349 if (isl_space_check_equal_tuples(tuple_space
, space
) < 0)
2350 return isl_space_free(space
);
2351 if (check_fresh_params(space
, tuple
) < 0)
2352 return isl_space_free(space
);
2353 space
= isl_space_params(space
);
2354 space
= add_bind_params(space
, tuple
);
2358 /* Internal function that removes the domain tuple of the map space "space",
2359 * which is assumed to correspond to the range space of "tuple", and
2360 * adds the identifiers in "tuple" as fresh parameters.
2361 * In other words, the domain dimensions of "space" are reinterpreted
2362 * as parameters, but stay in the same global positions.
2364 __isl_give isl_space
*isl_space_bind_map_domain(__isl_take isl_space
*space
,
2365 __isl_keep isl_multi_id
*tuple
)
2367 isl_space
*tuple_space
;
2369 if (isl_space_check_is_map(space
) < 0)
2370 return isl_space_free(space
);
2371 tuple_space
= isl_multi_id_peek_space(tuple
);
2372 if (isl_space_check_domain_tuples(tuple_space
, space
) < 0)
2373 return isl_space_free(space
);
2374 if (check_fresh_params(space
, tuple
) < 0)
2375 return isl_space_free(space
);
2376 space
= isl_space_range(space
);
2377 space
= add_bind_params(space
, tuple
);
2381 /* Internal function that, given a space of the form [A -> B] -> C and
2382 * a tuple of identifiers in A, returns a space B -> C with
2383 * the identifiers in "tuple" added as fresh parameters.
2384 * In other words, the domain dimensions of the wrapped relation
2385 * in the domain of "space" are reinterpreted
2386 * as parameters, but stay in the same global positions.
2388 __isl_give isl_space
*isl_space_bind_domain_wrapped_domain(
2389 __isl_take isl_space
*space
, __isl_keep isl_multi_id
*tuple
)
2391 isl_space
*tuple_space
;
2393 if (isl_space_check_is_map(space
) < 0)
2394 return isl_space_free(space
);
2395 tuple_space
= isl_multi_id_peek_space(tuple
);
2396 if (isl_space_check_domain_wrapped_domain_tuples(tuple_space
,
2398 return isl_space_free(space
);
2399 if (check_fresh_params(space
, tuple
) < 0)
2400 return isl_space_free(space
);
2401 space
= isl_space_domain_factor_range(space
);
2402 space
= add_bind_params(space
, tuple
);
2406 /* Insert a domain tuple in "space" corresponding to the set space "domain".
2407 * In particular, if "space" is a parameter space, then the result
2408 * is the set space "domain" combined with the parameters of "space".
2409 * If "space" is a set space, then the result
2410 * is a map space with "domain" as domain and the original space as range.
2412 static __isl_give isl_space
*isl_space_insert_domain(
2413 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
2417 domain
= isl_space_replace_params(domain
, space
);
2419 is_params
= isl_space_is_params(space
);
2420 if (is_params
< 0) {
2421 isl_space_free(domain
);
2422 space
= isl_space_free(space
);
2423 } else if (is_params
) {
2424 isl_space_free(space
);
2427 space
= isl_space_map_from_domain_and_range(domain
, space
);
2432 /* Internal function that introduces a domain in "space"
2433 * corresponding to the range space of "tuple".
2434 * In particular, if "space" is a parameter space, then the result
2435 * is a set space. If "space" is a set space, then the result
2436 * is a map space with the original space as range.
2437 * Parameters that correspond to the identifiers in "tuple" are removed.
2439 * The parameters are removed in reverse order (under the assumption
2440 * that they appear in the same order in "multi") because
2441 * it is slightly more efficient to remove parameters at the end.
2443 * For pretty-printing purposes, the identifiers of the set dimensions
2444 * of the introduced domain are set to the identifiers in "tuple".
2446 __isl_give isl_space
*isl_space_unbind_params_insert_domain(
2447 __isl_take isl_space
*space
, __isl_keep isl_multi_id
*tuple
)
2451 isl_space
*tuple_space
;
2453 n
= isl_multi_id_size(tuple
);
2454 if (!space
|| n
< 0)
2455 return isl_space_free(space
);
2456 for (i
= n
- 1; i
>= 0; --i
) {
2460 id
= isl_multi_id_get_id(tuple
, i
);
2462 return isl_space_free(space
);
2463 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2467 space
= isl_space_drop_dims(space
, isl_dim_param
, pos
, 1);
2469 tuple_space
= isl_multi_id_get_space(tuple
);
2470 for (i
= 0; i
< n
; ++i
) {
2473 id
= isl_multi_id_get_id(tuple
, i
);
2474 tuple_space
= isl_space_set_dim_id(tuple_space
,
2475 isl_dim_set
, i
, id
);
2477 return isl_space_insert_domain(space
, tuple_space
);
2480 __isl_give isl_space
*isl_space_underlying(__isl_take isl_space
*space
,
2486 is_set
= isl_space_is_set(space
);
2488 return isl_space_free(space
);
2489 if (n_div
== 0 && is_set
&&
2490 space
->nparam
== 0 && space
->n_in
== 0 && space
->n_id
== 0)
2491 return isl_space_reset(space
, isl_dim_out
);
2492 space
= isl_space_cow(space
);
2495 space
->n_out
+= space
->nparam
+ space
->n_in
+ n_div
;
2499 for (i
= 0; i
< space
->n_id
; ++i
)
2500 isl_id_free(get_id(space
, isl_dim_out
, i
));
2502 space
= isl_space_reset(space
, isl_dim_in
);
2503 space
= isl_space_reset(space
, isl_dim_out
);
2504 space
= mark_as_set(space
);
2509 /* Are the two spaces the same, including positions and names of parameters?
2511 isl_bool
isl_space_is_equal(__isl_keep isl_space
*space1
,
2512 __isl_keep isl_space
*space2
)
2516 if (!space1
|| !space2
)
2517 return isl_bool_error
;
2518 if (space1
== space2
)
2519 return isl_bool_true
;
2520 equal
= isl_space_has_equal_params(space1
, space2
);
2521 if (equal
< 0 || !equal
)
2523 return isl_space_has_equal_tuples(space1
, space2
);
2526 /* Do the tuples of "space1" correspond to those of the domain of "space2"?
2527 * That is, is "space1" equal to the domain of "space2", ignoring parameters.
2529 * "space2" is allowed to be a set space, in which case "space1"
2530 * should be a parameter space.
2532 isl_bool
isl_space_has_domain_tuples(__isl_keep isl_space
*space1
,
2533 __isl_keep isl_space
*space2
)
2537 is_set
= isl_space_is_set(space1
);
2538 if (is_set
< 0 || !is_set
)
2540 return isl_space_tuple_is_equal(space1
, isl_dim_set
,
2541 space2
, isl_dim_in
);
2544 /* Do the tuples of "space1" correspond to those of the range of "space2"?
2545 * That is, is "space1" equal to the range of "space2", ignoring parameters.
2547 * "space2" is allowed to be the space of a set,
2548 * in which case it should be equal to "space1", ignoring parameters.
2550 isl_bool
isl_space_has_range_tuples(__isl_keep isl_space
*space1
,
2551 __isl_keep isl_space
*space2
)
2555 is_set
= isl_space_is_set(space1
);
2556 if (is_set
< 0 || !is_set
)
2558 return isl_space_tuple_is_equal(space1
, isl_dim_set
,
2559 space2
, isl_dim_out
);
2562 /* Check that the tuples of "space1" correspond to those
2563 * of the domain of "space2".
2564 * That is, check that "space1" is equal to the domain of "space2",
2565 * ignoring parameters.
2567 isl_stat
isl_space_check_domain_tuples(__isl_keep isl_space
*space1
,
2568 __isl_keep isl_space
*space2
)
2572 is_equal
= isl_space_has_domain_tuples(space1
, space2
);
2573 return check_match(space1
, is_equal
);
2576 /* Check that the tuples of "space1" correspond to those
2577 * of the domain of the wrapped relation in the domain of "space2".
2578 * That is, check that "space1" is equal to this domain,
2579 * ignoring parameters.
2581 isl_stat
isl_space_check_domain_wrapped_domain_tuples(
2582 __isl_keep isl_space
*space1
, __isl_keep isl_space
*space2
)
2587 domain
= isl_space_unwrap(isl_space_domain(isl_space_copy(space2
)));
2588 r
= isl_space_check_domain_tuples(space1
, domain
);
2589 isl_space_free(domain
);
2594 /* Is space1 equal to the domain of space2?
2596 * In the internal version we also allow space2 to be the space of a set,
2597 * provided space1 is a parameter space.
2599 isl_bool
isl_space_is_domain_internal(__isl_keep isl_space
*space1
,
2600 __isl_keep isl_space
*space2
)
2602 isl_bool equal_params
;
2604 if (!space1
|| !space2
)
2605 return isl_bool_error
;
2606 equal_params
= isl_space_has_equal_params(space1
, space2
);
2607 if (equal_params
< 0 || !equal_params
)
2608 return equal_params
;
2609 return isl_space_has_domain_tuples(space1
, space2
);
2612 /* Is space1 equal to the domain of space2?
2614 isl_bool
isl_space_is_domain(__isl_keep isl_space
*space1
,
2615 __isl_keep isl_space
*space2
)
2618 return isl_bool_error
;
2619 if (!isl_space_is_map(space2
))
2620 return isl_bool_false
;
2621 return isl_space_is_domain_internal(space1
, space2
);
2624 /* Is space1 equal to the range of space2?
2626 * In the internal version, space2 is allowed to be the space of a set,
2627 * in which case it should be equal to space1.
2629 isl_bool
isl_space_is_range_internal(__isl_keep isl_space
*space1
,
2630 __isl_keep isl_space
*space2
)
2632 isl_bool equal_params
;
2634 if (!space1
|| !space2
)
2635 return isl_bool_error
;
2636 equal_params
= isl_space_has_equal_params(space1
, space2
);
2637 if (equal_params
< 0 || !equal_params
)
2638 return equal_params
;
2639 return isl_space_has_range_tuples(space1
, space2
);
2642 /* Is space1 equal to the range of space2?
2644 isl_bool
isl_space_is_range(__isl_keep isl_space
*space1
,
2645 __isl_keep isl_space
*space2
)
2648 return isl_bool_error
;
2649 if (!isl_space_is_map(space2
))
2650 return isl_bool_false
;
2651 return isl_space_is_range_internal(space1
, space2
);
2654 /* Update "hash" by hashing in the parameters of "space".
2656 static uint32_t isl_hash_params(uint32_t hash
, __isl_keep isl_space
*space
)
2664 isl_hash_byte(hash
, space
->nparam
% 256);
2666 for (i
= 0; i
< space
->nparam
; ++i
) {
2667 id
= get_id(space
, isl_dim_param
, i
);
2668 hash
= isl_hash_id(hash
, id
);
2674 /* Update "hash" by hashing in the tuples of "space".
2675 * Changes in this function should be reflected in isl_hash_tuples_domain.
2677 static uint32_t isl_hash_tuples(uint32_t hash
, __isl_keep isl_space
*space
)
2684 isl_hash_byte(hash
, space
->n_in
% 256);
2685 isl_hash_byte(hash
, space
->n_out
% 256);
2687 id
= tuple_id(space
, isl_dim_in
);
2688 hash
= isl_hash_id(hash
, id
);
2689 id
= tuple_id(space
, isl_dim_out
);
2690 hash
= isl_hash_id(hash
, id
);
2692 hash
= isl_hash_tuples(hash
, space
->nested
[0]);
2693 hash
= isl_hash_tuples(hash
, space
->nested
[1]);
2698 /* Update "hash" by hashing in the domain tuple of "space".
2699 * The result of this function is equal to the result of applying
2700 * isl_hash_tuples to the domain of "space".
2702 static uint32_t isl_hash_tuples_domain(uint32_t hash
,
2703 __isl_keep isl_space
*space
)
2710 isl_hash_byte(hash
, 0);
2711 isl_hash_byte(hash
, space
->n_in
% 256);
2713 hash
= isl_hash_id(hash
, &isl_id_none
);
2714 id
= tuple_id(space
, isl_dim_in
);
2715 hash
= isl_hash_id(hash
, id
);
2717 hash
= isl_hash_tuples(hash
, space
->nested
[0]);
2722 /* Return a hash value that digests the tuples of "space",
2723 * i.e., that ignores the parameters.
2724 * Changes in this function should be reflected
2725 * in isl_space_get_tuple_domain_hash.
2727 uint32_t isl_space_get_tuple_hash(__isl_keep isl_space
*space
)
2734 hash
= isl_hash_init();
2735 hash
= isl_hash_tuples(hash
, space
);
2740 /* Return the hash value of "space".
2742 uint32_t isl_space_get_full_hash(__isl_keep isl_space
*space
)
2749 hash
= isl_hash_init();
2750 hash
= isl_hash_params(hash
, space
);
2751 hash
= isl_hash_tuples(hash
, space
);
2756 /* Return the hash value of the domain tuple of "space".
2757 * That is, isl_space_get_tuple_domain_hash(space) is equal to
2758 * isl_space_get_tuple_hash(isl_space_domain(space)).
2760 uint32_t isl_space_get_tuple_domain_hash(__isl_keep isl_space
*space
)
2767 hash
= isl_hash_init();
2768 hash
= isl_hash_tuples_domain(hash
, space
);
2773 /* Is "space" the space of a set wrapping a map space?
2775 isl_bool
isl_space_is_wrapping(__isl_keep isl_space
*space
)
2778 return isl_bool_error
;
2780 if (!isl_space_is_set(space
))
2781 return isl_bool_false
;
2783 return isl_bool_ok(space
->nested
[1] != NULL
);
2786 /* Is "space" the space of a map where the domain is a wrapped map space?
2788 isl_bool
isl_space_domain_is_wrapping(__isl_keep isl_space
*space
)
2791 return isl_bool_error
;
2793 if (isl_space_is_set(space
))
2794 return isl_bool_false
;
2796 return isl_bool_ok(space
->nested
[0] != NULL
);
2799 /* Is "space" the space of a map where the range is a wrapped map space?
2801 isl_bool
isl_space_range_is_wrapping(__isl_keep isl_space
*space
)
2804 return isl_bool_error
;
2806 if (isl_space_is_set(space
))
2807 return isl_bool_false
;
2809 return isl_bool_ok(space
->nested
[1] != NULL
);
2812 /* Is "space" a product of two spaces?
2813 * That is, is it a wrapping set space or a map space
2814 * with wrapping domain and range?
2816 isl_bool
isl_space_is_product(__isl_keep isl_space
*space
)
2819 isl_bool is_product
;
2821 is_set
= isl_space_is_set(space
);
2823 return isl_bool_error
;
2825 return isl_space_is_wrapping(space
);
2826 is_product
= isl_space_domain_is_wrapping(space
);
2827 if (is_product
< 0 || !is_product
)
2829 return isl_space_range_is_wrapping(space
);
2832 __isl_give isl_space
*isl_space_wrap(__isl_take isl_space
*space
)
2839 wrap
= isl_space_set_alloc(space
->ctx
,
2840 space
->nparam
, space
->n_in
+ space
->n_out
);
2842 wrap
= copy_ids(wrap
, isl_dim_param
, 0, space
, isl_dim_param
);
2843 wrap
= copy_ids(wrap
, isl_dim_set
, 0, space
, isl_dim_in
);
2844 wrap
= copy_ids(wrap
, isl_dim_set
, space
->n_in
, space
, isl_dim_out
);
2849 wrap
->nested
[1] = space
;
2853 isl_space_free(space
);
2857 __isl_give isl_space
*isl_space_unwrap(__isl_take isl_space
*space
)
2864 if (!isl_space_is_wrapping(space
))
2865 isl_die(space
->ctx
, isl_error_invalid
, "not a wrapping space",
2868 unwrap
= isl_space_copy(space
->nested
[1]);
2869 isl_space_free(space
);
2873 isl_space_free(space
);
2877 isl_bool
isl_space_is_named_or_nested(__isl_keep isl_space
*space
,
2878 enum isl_dim_type type
)
2880 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
2881 return isl_bool_false
;
2883 return isl_bool_error
;
2884 if (space
->tuple_id
[type
- isl_dim_in
])
2885 return isl_bool_true
;
2886 if (space
->nested
[type
- isl_dim_in
])
2887 return isl_bool_true
;
2888 return isl_bool_false
;
2891 isl_bool
isl_space_may_be_set(__isl_keep isl_space
*space
)
2897 return isl_bool_error
;
2898 if (isl_space_is_set(space
))
2899 return isl_bool_true
;
2900 n_in
= isl_space_dim(space
, isl_dim_in
);
2902 return isl_bool_error
;
2904 return isl_bool_false
;
2905 nested
= isl_space_is_named_or_nested(space
, isl_dim_in
);
2906 if (nested
< 0 || nested
)
2907 return isl_bool_not(nested
);
2908 return isl_bool_true
;
2911 __isl_give isl_space
*isl_space_reset(__isl_take isl_space
*space
,
2912 enum isl_dim_type type
)
2914 if (!isl_space_is_named_or_nested(space
, type
))
2917 space
= isl_space_cow(space
);
2921 isl_id_free(space
->tuple_id
[type
- isl_dim_in
]);
2922 space
->tuple_id
[type
- isl_dim_in
] = NULL
;
2923 isl_space_free(space
->nested
[type
- isl_dim_in
]);
2924 space
->nested
[type
- isl_dim_in
] = NULL
;
2929 __isl_give isl_space
*isl_space_flatten(__isl_take isl_space
*space
)
2933 if (!space
->nested
[0] && !space
->nested
[1])
2936 if (space
->nested
[0])
2937 space
= isl_space_reset(space
, isl_dim_in
);
2938 if (space
&& space
->nested
[1])
2939 space
= isl_space_reset(space
, isl_dim_out
);
2944 __isl_give isl_space
*isl_space_flatten_domain(__isl_take isl_space
*space
)
2948 if (!space
->nested
[0])
2951 return isl_space_reset(space
, isl_dim_in
);
2954 __isl_give isl_space
*isl_space_flatten_range(__isl_take isl_space
*space
)
2958 if (!space
->nested
[1])
2961 return isl_space_reset(space
, isl_dim_out
);
2964 /* Replace the parameters of dst by those of src.
2966 __isl_give isl_space
*isl_space_replace_params(__isl_take isl_space
*dst
,
2967 __isl_keep isl_space
*src
)
2969 isl_size dst_dim
, src_dim
;
2970 isl_bool equal_params
;
2971 enum isl_dim_type type
= isl_dim_param
;
2973 equal_params
= isl_space_has_equal_params(dst
, src
);
2974 if (equal_params
< 0)
2975 return isl_space_free(dst
);
2979 dst
= isl_space_cow(dst
);
2981 dst_dim
= isl_space_dim(dst
, type
);
2982 src_dim
= isl_space_dim(src
, type
);
2983 if (dst_dim
< 0 || src_dim
< 0)
2986 dst
= isl_space_drop_dims(dst
, type
, 0, dst_dim
);
2987 dst
= isl_space_add_dims(dst
, type
, src_dim
);
2988 dst
= copy_ids(dst
, type
, 0, src
, type
);
2992 for (i
= 0; i
<= 1; ++i
) {
2995 if (!dst
->nested
[i
])
2997 nested
= isl_space_take_nested(dst
, i
);
2998 nested
= isl_space_replace_params(nested
, src
);
2999 dst
= isl_space_restore_nested(dst
, i
, nested
);
3007 isl_space_free(dst
);
3011 /* Given two tuples ("dst_type" in "dst" and "src_type" in "src")
3012 * of the same size, check if any of the dimensions in the "dst" tuple
3013 * have no identifier, while the corresponding dimensions in "src"
3014 * does have an identifier,
3015 * If so, copy the identifier over to "dst".
3017 __isl_give isl_space
*isl_space_copy_ids_if_unset(__isl_take isl_space
*dst
,
3018 enum isl_dim_type dst_type
, __isl_keep isl_space
*src
,
3019 enum isl_dim_type src_type
)
3024 n
= isl_space_dim(dst
, dst_type
);
3026 return isl_space_free(dst
);
3027 for (i
= 0; i
< n
; ++i
) {
3031 set
= isl_space_has_dim_id(dst
, dst_type
, i
);
3033 return isl_space_free(dst
);
3037 set
= isl_space_has_dim_id(src
, src_type
, i
);
3039 return isl_space_free(dst
);
3043 id
= isl_space_get_dim_id(src
, src_type
, i
);
3044 dst
= isl_space_set_dim_id(dst
, dst_type
, i
, id
);
3050 /* Given a space "space" of a set, create a space
3051 * for the lift of the set. In particular, the result
3052 * is of the form lifted[space -> local[..]], with n_local variables in the
3053 * range of the wrapped map.
3055 __isl_give isl_space
*isl_space_lift(__isl_take isl_space
*space
,
3058 isl_space
*local_space
;
3063 local_space
= isl_space_dup(space
);
3064 local_space
= isl_space_drop_dims(local_space
, isl_dim_set
, 0,
3066 local_space
= isl_space_add_dims(local_space
, isl_dim_set
, n_local
);
3067 local_space
= isl_space_set_tuple_name(local_space
,
3068 isl_dim_set
, "local");
3069 space
= isl_space_join(isl_space_from_domain(space
),
3070 isl_space_from_range(local_space
));
3071 space
= isl_space_wrap(space
);
3072 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "lifted");
3077 isl_bool
isl_space_can_zip(__isl_keep isl_space
*space
)
3081 is_set
= isl_space_is_set(space
);
3083 return isl_bool_error
;
3085 return isl_bool_false
;
3086 return isl_space_is_product(space
);
3089 __isl_give isl_space
*isl_space_zip(__isl_take isl_space
*space
)
3091 isl_space
*dom
, *ran
;
3092 isl_space
*dom_dom
, *dom_ran
, *ran_dom
, *ran_ran
;
3094 if (!isl_space_can_zip(space
))
3095 isl_die(space
->ctx
, isl_error_invalid
, "space cannot be zipped",
3100 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(space
)));
3101 ran
= isl_space_unwrap(isl_space_range(space
));
3102 dom_dom
= isl_space_domain(isl_space_copy(dom
));
3103 dom_ran
= isl_space_range(dom
);
3104 ran_dom
= isl_space_domain(isl_space_copy(ran
));
3105 ran_ran
= isl_space_range(ran
);
3106 dom
= isl_space_join(isl_space_from_domain(dom_dom
),
3107 isl_space_from_range(ran_dom
));
3108 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
3109 isl_space_from_range(ran_ran
));
3110 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
3111 isl_space_from_range(isl_space_wrap(ran
)));
3113 isl_space_free(space
);
3117 /* Can we apply isl_space_curry to "space"?
3118 * That is, does is it have a map space with a nested relation in its domain?
3120 isl_bool
isl_space_can_curry(__isl_keep isl_space
*space
)
3122 return isl_space_domain_is_wrapping(space
);
3125 /* Given a space (A -> B) -> C, return the corresponding space
3128 __isl_give isl_space
*isl_space_curry(__isl_take isl_space
*space
)
3130 isl_space
*dom
, *ran
;
3131 isl_space
*dom_dom
, *dom_ran
;
3136 if (!isl_space_can_curry(space
))
3137 isl_die(space
->ctx
, isl_error_invalid
,
3138 "space cannot be curried", goto error
);
3140 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(space
)));
3141 ran
= isl_space_range(space
);
3142 dom_dom
= isl_space_domain(isl_space_copy(dom
));
3143 dom_ran
= isl_space_range(dom
);
3144 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
3145 isl_space_from_range(ran
));
3146 return isl_space_join(isl_space_from_domain(dom_dom
),
3147 isl_space_from_range(isl_space_wrap(ran
)));
3149 isl_space_free(space
);
3153 /* Can isl_space_range_curry be applied to "space"?
3154 * That is, does it have a nested relation in its range,
3155 * the domain of which is itself a nested relation?
3157 isl_bool
isl_space_can_range_curry(__isl_keep isl_space
*space
)
3162 return isl_bool_error
;
3163 can
= isl_space_range_is_wrapping(space
);
3164 if (can
< 0 || !can
)
3166 return isl_space_can_curry(space
->nested
[1]);
3169 /* Given a space A -> ((B -> C) -> D), return the corresponding space
3170 * A -> (B -> (C -> D)).
3172 __isl_give isl_space
*isl_space_range_curry(__isl_take isl_space
*space
)
3179 if (!isl_space_can_range_curry(space
))
3180 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3181 "space range cannot be curried",
3182 return isl_space_free(space
));
3184 nested
= isl_space_take_nested(space
, 1);
3185 nested
= isl_space_curry(nested
);
3186 space
= isl_space_restore_nested(space
, 1, nested
);
3191 /* Can we apply isl_space_uncurry to "space"?
3192 * That is, does it have a map space with a nested relation in its range?
3194 isl_bool
isl_space_can_uncurry(__isl_keep isl_space
*space
)
3196 return isl_space_range_is_wrapping(space
);
3199 /* Given a space A -> (B -> C), return the corresponding space
3202 __isl_give isl_space
*isl_space_uncurry(__isl_take isl_space
*space
)
3204 isl_space
*dom
, *ran
;
3205 isl_space
*ran_dom
, *ran_ran
;
3210 if (!isl_space_can_uncurry(space
))
3211 isl_die(space
->ctx
, isl_error_invalid
,
3212 "space cannot be uncurried",
3213 return isl_space_free(space
));
3215 dom
= isl_space_domain(isl_space_copy(space
));
3216 ran
= isl_space_unwrap(isl_space_range(space
));
3217 ran_dom
= isl_space_domain(isl_space_copy(ran
));
3218 ran_ran
= isl_space_range(ran
);
3219 dom
= isl_space_join(isl_space_from_domain(dom
),
3220 isl_space_from_range(ran_dom
));
3221 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
3222 isl_space_from_range(ran_ran
));
3225 isl_bool
isl_space_has_named_params(__isl_keep isl_space
*space
)
3231 return isl_bool_error
;
3232 if (space
->nparam
== 0)
3233 return isl_bool_true
;
3234 off
= isl_space_offset(space
, isl_dim_param
);
3235 if (off
+ space
->nparam
> space
->n_id
)
3236 return isl_bool_false
;
3237 for (i
= 0; i
< space
->nparam
; ++i
)
3238 if (!space
->ids
[off
+ i
])
3239 return isl_bool_false
;
3240 return isl_bool_true
;
3243 /* Check that "space" has only named parameters, reporting an error
3246 isl_stat
isl_space_check_named_params(__isl_keep isl_space
*space
)
3250 named
= isl_space_has_named_params(space
);
3252 return isl_stat_error
;
3254 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3255 "unexpected unnamed parameters", return isl_stat_error
);
3260 /* Align the initial parameters of space1 to match the order in space2.
3262 __isl_give isl_space
*isl_space_align_params(__isl_take isl_space
*space1
,
3263 __isl_take isl_space
*space2
)
3265 isl_reordering
*exp
;
3267 if (isl_space_check_named_params(space1
) < 0 ||
3268 isl_space_check_named_params(space2
) < 0)
3271 exp
= isl_parameter_alignment_reordering(space1
, space2
);
3272 isl_space_free(space1
);
3273 isl_space_free(space2
);
3274 space1
= isl_reordering_get_space(exp
);
3275 isl_reordering_free(exp
);
3278 isl_space_free(space1
);
3279 isl_space_free(space2
);
3283 /* Given the space of set (domain), construct a space for a map
3284 * with as domain the given space and as range the range of "model".
3286 __isl_give isl_space
*isl_space_extend_domain_with_range(
3287 __isl_take isl_space
*space
, __isl_take isl_space
*model
)
3294 space
= isl_space_from_domain(space
);
3295 n_out
= isl_space_dim(model
, isl_dim_out
);
3298 space
= isl_space_add_dims(space
, isl_dim_out
, n_out
);
3299 if (isl_space_has_tuple_id(model
, isl_dim_out
))
3300 space
= isl_space_set_tuple_id(space
, isl_dim_out
,
3301 isl_space_get_tuple_id(model
, isl_dim_out
));
3304 if (model
->nested
[1]) {
3305 isl_space
*nested
= isl_space_copy(model
->nested
[1]);
3306 isl_size n_nested
, n_space
;
3307 nested
= isl_space_align_params(nested
, isl_space_copy(space
));
3308 n_nested
= isl_space_dim(nested
, isl_dim_param
);
3309 n_space
= isl_space_dim(space
, isl_dim_param
);
3310 if (n_nested
< 0 || n_space
< 0)
3312 if (n_nested
> n_space
)
3313 nested
= isl_space_drop_dims(nested
, isl_dim_param
,
3314 n_space
, n_nested
- n_space
);
3317 space
->nested
[1] = nested
;
3319 isl_space_free(model
);
3322 isl_space_free(model
);
3323 isl_space_free(space
);
3327 /* Compare the "type" dimensions of two isl_spaces.
3329 * The order is fairly arbitrary.
3331 static int isl_space_cmp_type(__isl_keep isl_space
*space1
,
3332 __isl_keep isl_space
*space2
, enum isl_dim_type type
)
3335 isl_size dim1
, dim2
;
3336 isl_space
*nested1
, *nested2
;
3338 dim1
= isl_space_dim(space1
, type
);
3339 dim2
= isl_space_dim(space2
, type
);
3340 if (dim1
< 0 || dim2
< 0)
3345 cmp
= isl_id_cmp(tuple_id(space1
, type
), tuple_id(space2
, type
));
3349 nested1
= nested(space1
, type
);
3350 nested2
= nested(space2
, type
);
3351 if (!nested1
!= !nested2
)
3352 return !nested1
- !nested2
;
3355 return isl_space_cmp(nested1
, nested2
);
3360 /* Compare two isl_spaces.
3362 * The order is fairly arbitrary.
3364 int isl_space_cmp(__isl_keep isl_space
*space1
, __isl_keep isl_space
*space2
)
3369 if (space1
== space2
)
3376 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_param
);
3379 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_in
);
3382 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_out
);
3386 if (!space1
->ids
&& !space2
->ids
)
3389 for (i
= 0; i
< n(space1
, isl_dim_param
); ++i
) {
3390 cmp
= isl_id_cmp(get_id(space1
, isl_dim_param
, i
),
3391 get_id(space2
, isl_dim_param
, i
));