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_take 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
)
481 dummy
= strtol(s
, &p
, 0);
483 isl_die(ctx
, isl_error_invalid
, "name looks like a number",
489 /* Return a copy of the nested space at the given position.
491 static __isl_keep isl_space
*isl_space_get_nested(__isl_keep isl_space
*space
,
494 return isl_space_copy(isl_space_peek_nested(space
, pos
));
497 /* Return the nested space at the given position.
498 * This may be either a copy or the nested space itself
499 * if there is only one reference to "space".
500 * This allows the nested space to be modified inplace
501 * if both "space" and the nested space have only a single reference.
502 * The caller is not allowed to modify "space" between this call and
503 * a subsequent call to isl_space_restore_nested.
504 * The only exception is that isl_space_free can be called instead.
506 static __isl_give isl_space
*isl_space_take_nested(__isl_keep isl_space
*space
,
514 return isl_space_get_nested(space
, pos
);
515 nested
= space
->nested
[pos
];
516 space
->nested
[pos
] = NULL
;
520 /* Replace the nested space at the given position by "nested",
521 * where this nested space of "space" may be missing
522 * due to a preceding call to isl_space_take_nested.
523 * However, in this case, "space" only has a single reference and
524 * then the call to isl_space_cow has no effect.
526 static __isl_give isl_space
*isl_space_restore_nested(
527 __isl_take isl_space
*space
, int pos
, __isl_take isl_space
*nested
)
529 if (!space
|| !nested
)
532 if (space
->nested
[pos
] == nested
) {
533 isl_space_free(nested
);
537 space
= isl_space_cow(space
);
540 isl_space_free(space
->nested
[pos
]);
541 space
->nested
[pos
] = nested
;
545 isl_space_free(space
);
546 isl_space_free(nested
);
550 /* Is it possible for the given dimension type to have a tuple id?
552 static int space_can_have_id(__isl_keep isl_space
*space
,
553 enum isl_dim_type type
)
557 if (isl_space_is_params(space
))
558 isl_die(space
->ctx
, isl_error_invalid
,
559 "parameter spaces don't have tuple ids", return 0);
560 if (isl_space_is_set(space
) && type
!= isl_dim_set
)
561 isl_die(space
->ctx
, isl_error_invalid
,
562 "set spaces can only have a set id", return 0);
563 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
564 isl_die(space
->ctx
, isl_error_invalid
,
565 "only input, output and set tuples can have ids",
571 /* Does the tuple have an id?
573 isl_bool
isl_space_has_tuple_id(__isl_keep isl_space
*space
,
574 enum isl_dim_type type
)
576 if (!space_can_have_id(space
, type
))
577 return isl_bool_error
;
578 return isl_bool_ok(space
->tuple_id
[type
- isl_dim_in
] != NULL
);
581 __isl_give isl_id
*isl_space_get_tuple_id(__isl_keep isl_space
*space
,
582 enum isl_dim_type type
)
588 has_id
= isl_space_has_tuple_id(space
, type
);
592 isl_die(space
->ctx
, isl_error_invalid
,
593 "tuple has no id", return NULL
);
594 return isl_id_copy(space
->tuple_id
[type
- isl_dim_in
]);
597 __isl_give isl_space
*isl_space_set_tuple_id(__isl_take isl_space
*space
,
598 enum isl_dim_type type
, __isl_take isl_id
*id
)
600 space
= isl_space_cow(space
);
603 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
604 isl_die(space
->ctx
, isl_error_invalid
,
605 "only input, output and set tuples can have names",
608 isl_id_free(space
->tuple_id
[type
- isl_dim_in
]);
609 space
->tuple_id
[type
- isl_dim_in
] = id
;
614 isl_space_free(space
);
618 __isl_give isl_space
*isl_space_reset_tuple_id(__isl_take isl_space
*space
,
619 enum isl_dim_type type
)
621 space
= isl_space_cow(space
);
624 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
625 isl_die(space
->ctx
, isl_error_invalid
,
626 "only input, output and set tuples can have names",
629 isl_id_free(space
->tuple_id
[type
- isl_dim_in
]);
630 space
->tuple_id
[type
- isl_dim_in
] = NULL
;
634 isl_space_free(space
);
638 /* Set the id of the given dimension of "space" to "id".
639 * If the dimension already has an id, then it is replaced.
640 * If the dimension is a parameter, then we need to change it
641 * in the nested spaces (if any) as well.
643 __isl_give isl_space
*isl_space_set_dim_id(__isl_take isl_space
*space
,
644 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
646 space
= isl_space_cow(space
);
650 if (type
== isl_dim_param
) {
653 for (i
= 0; i
< 2; ++i
) {
654 if (!space
->nested
[i
])
657 isl_space_set_dim_id(space
->nested
[i
],
658 type
, pos
, isl_id_copy(id
));
659 if (!space
->nested
[i
])
664 isl_id_free(get_id(space
, type
, pos
));
665 return set_id(space
, type
, pos
, id
);
668 isl_space_free(space
);
672 /* Reset the id of the given dimension of "space".
673 * If the dimension already has an id, then it is removed.
674 * If the dimension is a parameter, then we need to reset it
675 * in the nested spaces (if any) as well.
677 __isl_give isl_space
*isl_space_reset_dim_id(__isl_take isl_space
*space
,
678 enum isl_dim_type type
, unsigned pos
)
680 space
= isl_space_cow(space
);
684 if (type
== isl_dim_param
) {
687 for (i
= 0; i
< 2; ++i
) {
688 if (!space
->nested
[i
])
691 isl_space_reset_dim_id(space
->nested
[i
],
693 if (!space
->nested
[i
])
698 isl_id_free(get_id(space
, type
, pos
));
699 return set_id(space
, type
, pos
, NULL
);
701 isl_space_free(space
);
705 isl_bool
isl_space_has_dim_id(__isl_keep isl_space
*space
,
706 enum isl_dim_type type
, unsigned pos
)
709 return isl_bool_error
;
710 return isl_bool_ok(get_id(space
, type
, pos
) != NULL
);
713 __isl_give isl_id
*isl_space_get_dim_id(__isl_keep isl_space
*space
,
714 enum isl_dim_type type
, unsigned pos
)
718 if (!get_id(space
, type
, pos
))
719 isl_die(space
->ctx
, isl_error_invalid
,
720 "dim has no id", return NULL
);
721 return isl_id_copy(get_id(space
, type
, pos
));
724 __isl_give isl_space
*isl_space_set_tuple_name(__isl_take isl_space
*space
,
725 enum isl_dim_type type
, const char *s
)
733 return isl_space_reset_tuple_id(space
, type
);
735 if (!name_ok(space
->ctx
, s
))
738 id
= isl_id_alloc(space
->ctx
, s
, NULL
);
739 return isl_space_set_tuple_id(space
, type
, id
);
741 isl_space_free(space
);
745 /* Does the tuple have a name?
747 isl_bool
isl_space_has_tuple_name(__isl_keep isl_space
*space
,
748 enum isl_dim_type type
)
752 if (!space_can_have_id(space
, type
))
753 return isl_bool_error
;
754 id
= space
->tuple_id
[type
- isl_dim_in
];
755 return isl_bool_ok(id
&& id
->name
);
758 __isl_keep
const char *isl_space_get_tuple_name(__isl_keep isl_space
*space
,
759 enum isl_dim_type type
)
764 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
766 id
= space
->tuple_id
[type
- isl_dim_in
];
767 return id
? id
->name
: NULL
;
770 __isl_give isl_space
*isl_space_set_dim_name(__isl_take isl_space
*space
,
771 enum isl_dim_type type
, unsigned pos
,
779 return isl_space_reset_dim_id(space
, type
, pos
);
780 if (!name_ok(space
->ctx
, s
))
782 id
= isl_id_alloc(space
->ctx
, s
, NULL
);
783 return isl_space_set_dim_id(space
, type
, pos
, id
);
785 isl_space_free(space
);
789 /* Does the given dimension have a name?
791 isl_bool
isl_space_has_dim_name(__isl_keep isl_space
*space
,
792 enum isl_dim_type type
, unsigned pos
)
797 return isl_bool_error
;
798 id
= get_id(space
, type
, pos
);
799 return isl_bool_ok(id
&& id
->name
);
802 __isl_keep
const char *isl_space_get_dim_name(__isl_keep isl_space
*space
,
803 enum isl_dim_type type
, unsigned pos
)
805 isl_id
*id
= get_id(space
, type
, pos
);
806 return id
? id
->name
: NULL
;
809 int isl_space_find_dim_by_id(__isl_keep isl_space
*space
,
810 enum isl_dim_type type
, __isl_keep isl_id
*id
)
816 n
= isl_space_dim(space
, type
);
820 offset
= isl_space_offset(space
, type
);
821 for (i
= 0; i
< n
&& offset
+ i
< space
->n_id
; ++i
)
822 if (space
->ids
[offset
+ i
] == id
)
828 int isl_space_find_dim_by_name(__isl_keep isl_space
*space
,
829 enum isl_dim_type type
, const char *name
)
835 n
= isl_space_dim(space
, type
);
839 offset
= isl_space_offset(space
, type
);
840 for (i
= 0; i
< n
&& offset
+ i
< space
->n_id
; ++i
) {
841 isl_id
*id
= get_id(space
, type
, i
);
842 if (id
&& id
->name
&& !strcmp(id
->name
, name
))
849 /* Reset the user pointer on all identifiers of parameters and tuples
852 __isl_give isl_space
*isl_space_reset_user(__isl_take isl_space
*space
)
862 ctx
= isl_space_get_ctx(space
);
864 for (i
= 0; i
< space
->nparam
&& i
< space
->n_id
; ++i
) {
865 if (!isl_id_get_user(space
->ids
[i
]))
867 space
= isl_space_cow(space
);
870 name
= isl_id_get_name(space
->ids
[i
]);
871 id
= isl_id_alloc(ctx
, name
, NULL
);
872 isl_id_free(space
->ids
[i
]);
875 return isl_space_free(space
);
878 for (i
= 0; i
< 2; ++i
) {
879 if (!space
->tuple_id
[i
])
881 if (!isl_id_get_user(space
->tuple_id
[i
]))
883 space
= isl_space_cow(space
);
886 name
= isl_id_get_name(space
->tuple_id
[i
]);
887 id
= isl_id_alloc(ctx
, name
, NULL
);
888 isl_id_free(space
->tuple_id
[i
]);
889 space
->tuple_id
[i
] = id
;
891 return isl_space_free(space
);
894 for (i
= 0; i
< 2; ++i
) {
897 if (!space
->nested
[i
])
899 nested
= isl_space_take_nested(space
, i
);
900 nested
= isl_space_reset_user(nested
);
901 space
= isl_space_restore_nested(space
, i
, nested
);
909 static __isl_keep isl_id
*tuple_id(__isl_keep isl_space
*space
,
910 enum isl_dim_type type
)
914 if (type
== isl_dim_in
)
915 return space
->tuple_id
[0];
916 if (type
== isl_dim_out
)
917 return space
->tuple_id
[1];
921 static __isl_keep isl_space
*nested(__isl_keep isl_space
*space
,
922 enum isl_dim_type type
)
926 if (type
== isl_dim_in
)
927 return space
->nested
[0];
928 if (type
== isl_dim_out
)
929 return space
->nested
[1];
933 /* Are the two spaces the same, apart from positions and names of parameters?
935 isl_bool
isl_space_has_equal_tuples(__isl_keep isl_space
*space1
,
936 __isl_keep isl_space
*space2
)
938 if (!space1
|| !space2
)
939 return isl_bool_error
;
940 if (space1
== space2
)
941 return isl_bool_true
;
942 return isl_space_tuple_is_equal(space1
, isl_dim_in
,
943 space2
, isl_dim_in
) &&
944 isl_space_tuple_is_equal(space1
, isl_dim_out
,
945 space2
, isl_dim_out
);
948 /* Check that a match involving "space" was successful.
949 * That is, check that "match" is equal to isl_bool_true.
951 static isl_stat
check_match(__isl_keep isl_space
*space
, isl_bool match
)
954 return isl_stat_error
;
956 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
957 "incompatible spaces", return isl_stat_error
);
962 /* Check that the two spaces are the same,
963 * apart from positions and names of parameters.
965 isl_stat
isl_space_check_equal_tuples(__isl_keep isl_space
*space1
,
966 __isl_keep isl_space
*space2
)
970 is_equal
= isl_space_has_equal_tuples(space1
, space2
);
971 return check_match(space1
, is_equal
);
974 /* Check if the tuple of type "type1" of "space1" is the same as
975 * the tuple of type "type2" of "space2".
977 * That is, check if the tuples have the same identifier, the same dimension
978 * and the same internal structure.
979 * The identifiers of the dimensions inside the tuples do not affect the result.
981 * Note that this function only checks the tuples themselves.
982 * If nested tuples are involved, then we need to be careful not
983 * to have result affected by possibly differing parameters
984 * in those nested tuples.
986 isl_bool
isl_space_tuple_is_equal(__isl_keep isl_space
*space1
,
987 enum isl_dim_type type1
, __isl_keep isl_space
*space2
,
988 enum isl_dim_type type2
)
991 isl_space
*nested1
, *nested2
;
993 if (!space1
|| !space2
)
994 return isl_bool_error
;
996 if (space1
== space2
&& type1
== type2
)
997 return isl_bool_true
;
999 if (n(space1
, type1
) != n(space2
, type2
))
1000 return isl_bool_false
;
1001 id1
= tuple_id(space1
, type1
);
1002 id2
= tuple_id(space2
, type2
);
1004 return isl_bool_false
;
1005 if (id1
&& id1
!= id2
)
1006 return isl_bool_false
;
1007 nested1
= nested(space1
, type1
);
1008 nested2
= nested(space2
, type2
);
1009 if (!nested1
^ !nested2
)
1010 return isl_bool_false
;
1011 if (nested1
&& !isl_space_has_equal_tuples(nested1
, nested2
))
1012 return isl_bool_false
;
1013 return isl_bool_true
;
1016 /* Is the tuple "inner" within the wrapped relation inside tuple "outer"
1017 * of "space1" equal to tuple "type2" of "space2"?
1019 isl_bool
isl_space_wrapped_tuple_is_equal(__isl_keep isl_space
*space1
,
1020 enum isl_dim_type outer
, enum isl_dim_type inner
,
1021 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
1027 return isl_bool_error
;
1028 if (outer
!= isl_dim_in
&& outer
!= isl_dim_out
)
1029 isl_die(isl_space_get_ctx(space1
), isl_error_invalid
,
1030 "only input, output and set tuples "
1031 "can have nested relations", return isl_bool_error
);
1032 pos
= outer
- isl_dim_in
;
1033 nested
= isl_space_peek_nested(space1
, pos
);
1034 return isl_space_tuple_is_equal(nested
, inner
, space2
, type2
);
1037 /* Check that the tuple "inner" within the wrapped relation inside tuple "outer"
1038 * of "space1" is equal to tuple "type2" of "space2".
1040 isl_stat
isl_space_check_wrapped_tuple_is_equal(__isl_keep isl_space
*space1
,
1041 enum isl_dim_type outer
, enum isl_dim_type inner
,
1042 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
1046 is_equal
= isl_space_wrapped_tuple_is_equal(space1
, outer
, inner
,
1048 return check_match(space1
, is_equal
);
1051 static isl_bool
match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
1052 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
1057 if (!space1
|| !space2
)
1058 return isl_bool_error
;
1060 if (space1
== space2
&& type1
== type2
)
1061 return isl_bool_true
;
1063 equal
= isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
1064 if (equal
< 0 || !equal
)
1067 if (!space1
->ids
&& !space2
->ids
)
1068 return isl_bool_true
;
1070 for (i
= 0; i
< n(space1
, type1
); ++i
) {
1071 if (get_id(space1
, type1
, i
) != get_id(space2
, type2
, i
))
1072 return isl_bool_false
;
1074 return isl_bool_true
;
1077 /* Do "space1" and "space2" have the same parameters?
1079 isl_bool
isl_space_has_equal_params(__isl_keep isl_space
*space1
,
1080 __isl_keep isl_space
*space2
)
1082 return match(space1
, isl_dim_param
, space2
, isl_dim_param
);
1085 /* Do "space1" and "space2" have the same identifiers for all
1086 * the tuple variables?
1088 isl_bool
isl_space_has_equal_ids(__isl_keep isl_space
*space1
,
1089 __isl_keep isl_space
*space2
)
1093 equal
= match(space1
, isl_dim_in
, space2
, isl_dim_in
);
1094 if (equal
< 0 || !equal
)
1096 return match(space1
, isl_dim_out
, space2
, isl_dim_out
);
1099 isl_bool
isl_space_match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
1100 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
1102 return match(space1
, type1
, space2
, type2
);
1105 static void get_ids(__isl_keep isl_space
*space
, enum isl_dim_type type
,
1106 unsigned first
, unsigned n
, __isl_keep isl_id
**ids
)
1110 for (i
= 0; i
< n
; ++i
)
1111 ids
[i
] = get_id(space
, type
, first
+ i
);
1114 static __isl_give isl_space
*space_extend(__isl_take isl_space
*space
,
1115 unsigned nparam
, unsigned n_in
, unsigned n_out
)
1117 isl_id
**ids
= NULL
;
1121 if (space
->nparam
== nparam
&&
1122 space
->n_in
== n_in
&& space
->n_out
== n_out
)
1125 isl_assert(space
->ctx
, space
->nparam
<= nparam
, goto error
);
1126 isl_assert(space
->ctx
, space
->n_in
<= n_in
, goto error
);
1127 isl_assert(space
->ctx
, space
->n_out
<= n_out
, goto error
);
1129 space
= isl_space_cow(space
);
1135 n
= nparam
+ n_in
+ n_out
;
1136 if (n
< nparam
|| n
< n_in
|| n
< n_out
)
1137 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1138 "overflow in total number of dimensions",
1140 ids
= isl_calloc_array(space
->ctx
, isl_id
*, n
);
1143 get_ids(space
, isl_dim_param
, 0, space
->nparam
, ids
);
1144 get_ids(space
, isl_dim_in
, 0, space
->n_in
, ids
+ nparam
);
1145 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
1146 ids
+ nparam
+ n_in
);
1149 space
->n_id
= nparam
+ n_in
+ n_out
;
1151 space
->nparam
= nparam
;
1153 space
->n_out
= n_out
;
1158 isl_space_free(space
);
1162 __isl_give isl_space
*isl_space_extend(__isl_take isl_space
*space
,
1163 unsigned nparam
, unsigned n_in
, unsigned n_out
)
1165 return space_extend(space
, nparam
, n_in
, n_out
);
1168 __isl_give isl_space
*isl_space_add_dims(__isl_take isl_space
*space
,
1169 enum isl_dim_type type
, unsigned n
)
1171 space
= isl_space_reset(space
, type
);
1176 space
= space_extend(space
,
1177 space
->nparam
+ n
, space
->n_in
, space
->n_out
);
1178 if (space
&& space
->nested
[0] &&
1179 !(space
->nested
[0] = isl_space_add_dims(space
->nested
[0],
1182 if (space
&& space
->nested
[1] &&
1183 !(space
->nested
[1] = isl_space_add_dims(space
->nested
[1],
1188 return space_extend(space
,
1189 space
->nparam
, space
->n_in
+ n
, space
->n_out
);
1191 return space_extend(space
,
1192 space
->nparam
, space
->n_in
, space
->n_out
+ n
);
1194 isl_die(space
->ctx
, isl_error_invalid
,
1195 "cannot add dimensions of specified type", goto error
);
1198 isl_space_free(space
);
1202 /* Add a parameter with identifier "id" to "space", provided
1203 * it does not already appear in "space".
1205 __isl_give isl_space
*isl_space_add_param_id(__isl_take isl_space
*space
,
1206 __isl_take isl_id
*id
)
1213 if (isl_space_find_dim_by_id(space
, isl_dim_param
, id
) >= 0) {
1218 pos
= isl_space_dim(space
, isl_dim_param
);
1221 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
1222 space
= isl_space_set_dim_id(space
, isl_dim_param
, pos
, id
);
1226 isl_space_free(space
);
1231 static int valid_dim_type(enum isl_dim_type type
)
1244 #define TYPE isl_space
1245 #include "check_type_range_templ.c"
1247 /* Insert "n" dimensions of type "type" at position "pos".
1248 * If we are inserting parameters, then they are also inserted in
1249 * any nested spaces.
1251 __isl_give isl_space
*isl_space_insert_dims(__isl_take isl_space
*space
,
1252 enum isl_dim_type type
, unsigned pos
, unsigned n
)
1255 isl_id
**ids
= NULL
;
1260 return isl_space_reset(space
, type
);
1262 ctx
= isl_space_get_ctx(space
);
1263 if (!valid_dim_type(type
))
1264 isl_die(ctx
, isl_error_invalid
,
1265 "cannot insert dimensions of specified type",
1268 if (isl_space_check_range(space
, type
, pos
, 0) < 0)
1269 return isl_space_free(space
);
1271 space
= isl_space_cow(space
);
1276 enum isl_dim_type t
, o
= isl_dim_param
;
1279 ids
= isl_calloc_array(ctx
, isl_id
*,
1280 space
->nparam
+ space
->n_in
+ space
->n_out
+ n
);
1284 s
[isl_dim_param
- o
] = space
->nparam
;
1285 s
[isl_dim_in
- o
] = space
->n_in
;
1286 s
[isl_dim_out
- o
] = space
->n_out
;
1287 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1289 get_ids(space
, t
, 0, s
[t
- o
], ids
+ off
);
1292 get_ids(space
, t
, 0, pos
, ids
+ off
);
1294 get_ids(space
, t
, pos
, s
[t
- o
] - pos
,
1296 off
+= s
[t
- o
] - pos
;
1301 space
->n_id
= space
->nparam
+ space
->n_in
+ space
->n_out
+ n
;
1304 case isl_dim_param
: space
->nparam
+= n
; break;
1305 case isl_dim_in
: space
->n_in
+= n
; break;
1306 case isl_dim_out
: space
->n_out
+= n
; break;
1309 space
= isl_space_reset(space
, type
);
1311 if (type
== isl_dim_param
) {
1312 if (space
&& space
->nested
[0] &&
1313 !(space
->nested
[0] = isl_space_insert_dims(space
->nested
[0],
1314 isl_dim_param
, pos
, n
)))
1316 if (space
&& space
->nested
[1] &&
1317 !(space
->nested
[1] = isl_space_insert_dims(space
->nested
[1],
1318 isl_dim_param
, pos
, n
)))
1324 isl_space_free(space
);
1328 __isl_give isl_space
*isl_space_move_dims(__isl_take isl_space
*space
,
1329 enum isl_dim_type dst_type
, unsigned dst_pos
,
1330 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1334 space
= isl_space_reset(space
, src_type
);
1335 space
= isl_space_reset(space
, dst_type
);
1341 if (isl_space_check_range(space
, src_type
, src_pos
, n
) < 0)
1342 return isl_space_free(space
);
1344 if (dst_type
== src_type
&& dst_pos
== src_pos
)
1347 isl_assert(space
->ctx
, dst_type
!= src_type
, goto error
);
1349 space
= isl_space_cow(space
);
1355 enum isl_dim_type t
, o
= isl_dim_param
;
1358 ids
= isl_calloc_array(space
->ctx
, isl_id
*,
1359 space
->nparam
+ space
->n_in
+ space
->n_out
);
1363 s
[isl_dim_param
- o
] = space
->nparam
;
1364 s
[isl_dim_in
- o
] = space
->n_in
;
1365 s
[isl_dim_out
- o
] = space
->n_out
;
1366 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1367 if (t
== dst_type
) {
1368 get_ids(space
, t
, 0, dst_pos
, ids
+ off
);
1370 get_ids(space
, src_type
, src_pos
, n
, ids
+ off
);
1372 get_ids(space
, t
, dst_pos
, s
[t
- o
] - dst_pos
,
1374 off
+= s
[t
- o
] - dst_pos
;
1375 } else if (t
== src_type
) {
1376 get_ids(space
, t
, 0, src_pos
, ids
+ off
);
1378 get_ids(space
, t
, src_pos
+ n
,
1379 s
[t
- o
] - src_pos
- n
, ids
+ off
);
1380 off
+= s
[t
- o
] - src_pos
- n
;
1382 get_ids(space
, t
, 0, s
[t
- o
], ids
+ off
);
1388 space
->n_id
= space
->nparam
+ space
->n_in
+ space
->n_out
;
1392 case isl_dim_param
: space
->nparam
+= n
; break;
1393 case isl_dim_in
: space
->n_in
+= n
; break;
1394 case isl_dim_out
: space
->n_out
+= n
; break;
1399 case isl_dim_param
: space
->nparam
-= n
; break;
1400 case isl_dim_in
: space
->n_in
-= n
; break;
1401 case isl_dim_out
: space
->n_out
-= n
; break;
1405 if (dst_type
!= isl_dim_param
&& src_type
!= isl_dim_param
)
1408 for (i
= 0; i
< 2; ++i
) {
1411 if (!space
->nested
[i
])
1413 nested
= isl_space_take_nested(space
, i
);
1414 nested
= isl_space_replace_params(nested
, space
);
1415 space
= isl_space_restore_nested(space
, i
, nested
);
1422 isl_space_free(space
);
1426 /* Check that "space1" and "space2" have the same parameters,
1427 * reporting an error if they do not.
1429 isl_stat
isl_space_check_equal_params(__isl_keep isl_space
*space1
,
1430 __isl_keep isl_space
*space2
)
1434 equal
= isl_space_has_equal_params(space1
, space2
);
1436 return isl_stat_error
;
1438 isl_die(isl_space_get_ctx(space1
), isl_error_invalid
,
1439 "parameters need to match", return isl_stat_error
);
1443 __isl_give isl_space
*isl_space_join(__isl_take isl_space
*left
,
1444 __isl_take isl_space
*right
)
1448 if (isl_space_check_equal_params(left
, right
) < 0)
1451 isl_assert(left
->ctx
,
1452 isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_in
),
1455 space
= isl_space_alloc(left
->ctx
,
1456 left
->nparam
, left
->n_in
, right
->n_out
);
1460 space
= copy_ids(space
, isl_dim_param
, 0, left
, isl_dim_param
);
1461 space
= copy_ids(space
, isl_dim_in
, 0, left
, isl_dim_in
);
1462 space
= copy_ids(space
, isl_dim_out
, 0, right
, isl_dim_out
);
1464 if (space
&& left
->tuple_id
[0] &&
1465 !(space
->tuple_id
[0] = isl_id_copy(left
->tuple_id
[0])))
1467 if (space
&& right
->tuple_id
[1] &&
1468 !(space
->tuple_id
[1] = isl_id_copy(right
->tuple_id
[1])))
1470 if (space
&& left
->nested
[0] &&
1471 !(space
->nested
[0] = isl_space_copy(left
->nested
[0])))
1473 if (space
&& right
->nested
[1] &&
1474 !(space
->nested
[1] = isl_space_copy(right
->nested
[1])))
1477 isl_space_free(left
);
1478 isl_space_free(right
);
1482 isl_space_free(left
);
1483 isl_space_free(right
);
1487 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1488 * { [A -> B] -> [C -> D] }.
1489 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1491 __isl_give isl_space
*isl_space_product(__isl_take isl_space
*left
,
1492 __isl_take isl_space
*right
)
1494 isl_space
*dom1
, *dom2
, *nest1
, *nest2
;
1497 if (!left
|| !right
)
1500 is_set
= isl_space_is_set(left
);
1501 if (is_set
!= isl_space_is_set(right
))
1502 isl_die(isl_space_get_ctx(left
), isl_error_invalid
,
1503 "expecting either two set spaces or two map spaces",
1506 return isl_space_range_product(left
, right
);
1508 if (isl_space_check_equal_params(left
, right
) < 0)
1511 dom1
= isl_space_domain(isl_space_copy(left
));
1512 dom2
= isl_space_domain(isl_space_copy(right
));
1513 nest1
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1515 dom1
= isl_space_range(left
);
1516 dom2
= isl_space_range(right
);
1517 nest2
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1519 return isl_space_join(isl_space_reverse(nest1
), nest2
);
1521 isl_space_free(left
);
1522 isl_space_free(right
);
1526 /* Given two spaces { A -> C } and { B -> C }, construct the space
1529 __isl_give isl_space
*isl_space_domain_product(__isl_take isl_space
*left
,
1530 __isl_take isl_space
*right
)
1532 isl_space
*ran
, *dom1
, *dom2
, *nest
;
1534 if (isl_space_check_equal_params(left
, right
) < 0)
1537 if (!isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_out
))
1538 isl_die(left
->ctx
, isl_error_invalid
,
1539 "ranges need to match", goto error
);
1541 ran
= isl_space_range(isl_space_copy(left
));
1543 dom1
= isl_space_domain(left
);
1544 dom2
= isl_space_domain(right
);
1545 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1547 return isl_space_join(isl_space_reverse(nest
), ran
);
1549 isl_space_free(left
);
1550 isl_space_free(right
);
1554 __isl_give isl_space
*isl_space_range_product(__isl_take isl_space
*left
,
1555 __isl_take isl_space
*right
)
1557 isl_space
*dom
, *ran1
, *ran2
, *nest
;
1559 if (isl_space_check_equal_params(left
, right
) < 0)
1562 if (!isl_space_tuple_is_equal(left
, isl_dim_in
, right
, isl_dim_in
))
1563 isl_die(left
->ctx
, isl_error_invalid
,
1564 "domains need to match", goto error
);
1566 dom
= isl_space_domain(isl_space_copy(left
));
1568 ran1
= isl_space_range(left
);
1569 ran2
= isl_space_range(right
);
1570 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(ran1
), ran2
));
1572 return isl_space_join(isl_space_reverse(dom
), nest
);
1574 isl_space_free(left
);
1575 isl_space_free(right
);
1579 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1581 __isl_give isl_space
*isl_space_domain_factor_domain(
1582 __isl_take isl_space
*space
)
1587 if (isl_space_check_domain_is_wrapping(space
) < 0)
1588 return isl_space_free(space
);
1590 nested
= space
->nested
[0];
1591 domain
= isl_space_copy(space
);
1592 domain
= isl_space_drop_dims(domain
, isl_dim_in
,
1593 nested
->n_in
, nested
->n_out
);
1595 return isl_space_free(space
);
1596 if (nested
->tuple_id
[0]) {
1597 domain
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[0]);
1598 if (!domain
->tuple_id
[0])
1601 if (nested
->nested
[0]) {
1602 domain
->nested
[0] = isl_space_copy(nested
->nested
[0]);
1603 if (!domain
->nested
[0])
1607 isl_space_free(space
);
1610 isl_space_free(space
);
1611 isl_space_free(domain
);
1615 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1617 __isl_give isl_space
*isl_space_domain_factor_range(
1618 __isl_take isl_space
*space
)
1623 if (isl_space_check_domain_is_wrapping(space
) < 0)
1624 return isl_space_free(space
);
1626 nested
= space
->nested
[0];
1627 range
= isl_space_copy(space
);
1628 range
= isl_space_drop_dims(range
, isl_dim_in
, 0, nested
->n_in
);
1630 return isl_space_free(space
);
1631 if (nested
->tuple_id
[1]) {
1632 range
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[1]);
1633 if (!range
->tuple_id
[0])
1636 if (nested
->nested
[1]) {
1637 range
->nested
[0] = isl_space_copy(nested
->nested
[1]);
1638 if (!range
->nested
[0])
1642 isl_space_free(space
);
1645 isl_space_free(space
);
1646 isl_space_free(range
);
1650 /* Internal function that selects the domain of the map that is
1651 * embedded in either a set space or the range of a map space.
1652 * In particular, given a space of the form [A -> B], return the space A.
1653 * Given a space of the form A -> [B -> C], return the space A -> B.
1655 static __isl_give isl_space
*range_factor_domain(__isl_take isl_space
*space
)
1663 nested
= space
->nested
[1];
1664 domain
= isl_space_copy(space
);
1665 domain
= isl_space_drop_dims(domain
, isl_dim_out
,
1666 nested
->n_in
, nested
->n_out
);
1668 return isl_space_free(space
);
1669 if (nested
->tuple_id
[0]) {
1670 domain
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[0]);
1671 if (!domain
->tuple_id
[1])
1674 if (nested
->nested
[0]) {
1675 domain
->nested
[1] = isl_space_copy(nested
->nested
[0]);
1676 if (!domain
->nested
[1])
1680 isl_space_free(space
);
1683 isl_space_free(space
);
1684 isl_space_free(domain
);
1688 /* Given a space of the form A -> [B -> C], return the space A -> B.
1690 __isl_give isl_space
*isl_space_range_factor_domain(
1691 __isl_take isl_space
*space
)
1693 if (isl_space_check_range_is_wrapping(space
) < 0)
1694 return isl_space_free(space
);
1696 return range_factor_domain(space
);
1699 /* Given a space of the form [A -> B], return the space A.
1701 static __isl_give isl_space
*set_factor_domain(__isl_take isl_space
*space
)
1705 if (!isl_space_is_wrapping(space
))
1706 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1707 "not a product", return isl_space_free(space
));
1709 return range_factor_domain(space
);
1712 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1713 * Given a space of the form [A -> B], return the space A.
1715 __isl_give isl_space
*isl_space_factor_domain(__isl_take isl_space
*space
)
1719 if (isl_space_is_set(space
))
1720 return set_factor_domain(space
);
1721 space
= isl_space_domain_factor_domain(space
);
1722 space
= isl_space_range_factor_domain(space
);
1726 /* Internal function that selects the range of the map that is
1727 * embedded in either a set space or the range of a map space.
1728 * In particular, given a space of the form [A -> B], return the space B.
1729 * Given a space of the form A -> [B -> C], return the space A -> C.
1731 static __isl_give isl_space
*range_factor_range(__isl_take isl_space
*space
)
1739 nested
= space
->nested
[1];
1740 range
= isl_space_copy(space
);
1741 range
= isl_space_drop_dims(range
, isl_dim_out
, 0, nested
->n_in
);
1743 return isl_space_free(space
);
1744 if (nested
->tuple_id
[1]) {
1745 range
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[1]);
1746 if (!range
->tuple_id
[1])
1749 if (nested
->nested
[1]) {
1750 range
->nested
[1] = isl_space_copy(nested
->nested
[1]);
1751 if (!range
->nested
[1])
1755 isl_space_free(space
);
1758 isl_space_free(space
);
1759 isl_space_free(range
);
1763 /* Given a space of the form A -> [B -> C], return the space A -> C.
1765 __isl_give isl_space
*isl_space_range_factor_range(
1766 __isl_take isl_space
*space
)
1768 if (isl_space_check_range_is_wrapping(space
) < 0)
1769 return isl_space_free(space
);
1771 return range_factor_range(space
);
1774 /* Given a space of the form [A -> B], return the space B.
1776 static __isl_give isl_space
*set_factor_range(__isl_take isl_space
*space
)
1780 if (!isl_space_is_wrapping(space
))
1781 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1782 "not a product", return isl_space_free(space
));
1784 return range_factor_range(space
);
1787 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1788 * Given a space of the form [A -> B], return the space B.
1790 __isl_give isl_space
*isl_space_factor_range(__isl_take isl_space
*space
)
1794 if (isl_space_is_set(space
))
1795 return set_factor_range(space
);
1796 space
= isl_space_domain_factor_range(space
);
1797 space
= isl_space_range_factor_range(space
);
1801 __isl_give isl_space
*isl_space_map_from_set(__isl_take isl_space
*space
)
1804 isl_id
**ids
= NULL
;
1809 ctx
= isl_space_get_ctx(space
);
1810 if (!isl_space_is_set(space
))
1811 isl_die(ctx
, isl_error_invalid
, "not a set space", goto error
);
1812 space
= isl_space_cow(space
);
1815 n_id
= space
->nparam
+ space
->n_out
+ space
->n_out
;
1816 if (n_id
> 0 && space
->ids
) {
1817 ids
= isl_calloc_array(space
->ctx
, isl_id
*, n_id
);
1820 get_ids(space
, isl_dim_param
, 0, space
->nparam
, ids
);
1821 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
1822 ids
+ space
->nparam
);
1824 space
->n_in
= space
->n_out
;
1829 space
= copy_ids(space
, isl_dim_out
, 0, space
, isl_dim_in
);
1831 isl_id_free(space
->tuple_id
[0]);
1832 space
->tuple_id
[0] = isl_id_copy(space
->tuple_id
[1]);
1833 isl_space_free(space
->nested
[0]);
1834 space
->nested
[0] = isl_space_copy(space
->nested
[1]);
1837 isl_space_free(space
);
1841 __isl_give isl_space
*isl_space_map_from_domain_and_range(
1842 __isl_take isl_space
*domain
, __isl_take isl_space
*range
)
1844 if (!domain
|| !range
)
1846 if (!isl_space_is_set(domain
))
1847 isl_die(isl_space_get_ctx(domain
), isl_error_invalid
,
1848 "domain is not a set space", goto error
);
1849 if (!isl_space_is_set(range
))
1850 isl_die(isl_space_get_ctx(range
), isl_error_invalid
,
1851 "range is not a set space", goto error
);
1852 return isl_space_join(isl_space_reverse(domain
), range
);
1854 isl_space_free(domain
);
1855 isl_space_free(range
);
1859 static __isl_give isl_space
*set_ids(__isl_take isl_space
*space
,
1860 enum isl_dim_type type
,
1861 unsigned first
, unsigned n
, __isl_take isl_id
**ids
)
1865 for (i
= 0; i
< n
; ++i
)
1866 space
= set_id(space
, type
, first
+ i
, ids
[i
]);
1871 __isl_give isl_space
*isl_space_reverse(__isl_take isl_space
*space
)
1876 isl_id
**ids
= NULL
;
1879 equal
= match(space
, isl_dim_in
, space
, isl_dim_out
);
1881 return isl_space_free(space
);
1885 space
= isl_space_cow(space
);
1889 id
= space
->tuple_id
[0];
1890 space
->tuple_id
[0] = space
->tuple_id
[1];
1891 space
->tuple_id
[1] = id
;
1893 nested
= space
->nested
[0];
1894 space
->nested
[0] = space
->nested
[1];
1895 space
->nested
[1] = nested
;
1898 int n_id
= space
->n_in
+ space
->n_out
;
1899 ids
= isl_alloc_array(space
->ctx
, isl_id
*, n_id
);
1902 get_ids(space
, isl_dim_in
, 0, space
->n_in
, ids
);
1903 get_ids(space
, isl_dim_out
, 0, space
->n_out
, ids
+ space
->n_in
);
1907 space
->n_in
= space
->n_out
;
1911 space
= set_ids(space
, isl_dim_out
, 0, space
->n_out
, ids
);
1912 space
= set_ids(space
, isl_dim_in
, 0, space
->n_in
,
1913 ids
+ space
->n_out
);
1920 isl_space_free(space
);
1924 /* Given a space A -> (B -> C), return the corresponding space
1927 * If the range tuple is named, then the name is only preserved
1928 * if B and C are equal tuples, in which case the output
1929 * of this function is identical to the input.
1931 __isl_give isl_space
*isl_space_range_reverse(__isl_take isl_space
*space
)
1936 if (isl_space_check_range_is_wrapping(space
) < 0)
1937 return isl_space_free(space
);
1939 nested
= isl_space_peek_nested(space
, 1);
1940 equal
= isl_space_tuple_is_equal(nested
, isl_dim_in
,
1941 nested
, isl_dim_out
);
1943 return isl_space_free(space
);
1945 nested
= isl_space_take_nested(space
, 1);
1946 nested
= isl_space_reverse(nested
);
1947 space
= isl_space_restore_nested(space
, 1, nested
);
1949 space
= isl_space_reset_tuple_id(space
, isl_dim_out
);
1954 __isl_give isl_space
*isl_space_drop_dims(__isl_take isl_space
*space
,
1955 enum isl_dim_type type
, unsigned first
, unsigned num
)
1963 return isl_space_reset(space
, type
);
1965 if (!valid_dim_type(type
))
1966 isl_die(space
->ctx
, isl_error_invalid
,
1967 "cannot drop dimensions of specified type", goto error
);
1969 if (isl_space_check_range(space
, type
, first
, num
) < 0)
1970 return isl_space_free(space
);
1971 space
= isl_space_cow(space
);
1975 space
= extend_ids(space
);
1978 for (i
= 0; i
< num
; ++i
)
1979 isl_id_free(get_id(space
, type
, first
+ i
));
1980 for (i
= first
+num
; i
< n(space
, type
); ++i
)
1981 set_id(space
, type
, i
- num
, get_id(space
, type
, i
));
1984 get_ids(space
, isl_dim_in
, 0, space
->n_in
,
1985 space
->ids
+ offset(space
, isl_dim_in
) - num
);
1987 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
1988 space
->ids
+ offset(space
, isl_dim_out
) - num
);
1995 case isl_dim_param
: space
->nparam
-= num
; break;
1996 case isl_dim_in
: space
->n_in
-= num
; break;
1997 case isl_dim_out
: space
->n_out
-= num
; break;
2000 space
= isl_space_reset(space
, type
);
2001 if (type
== isl_dim_param
) {
2002 if (space
&& space
->nested
[0] &&
2003 !(space
->nested
[0] = isl_space_drop_dims(space
->nested
[0],
2004 isl_dim_param
, first
, num
)))
2006 if (space
&& space
->nested
[1] &&
2007 !(space
->nested
[1] = isl_space_drop_dims(space
->nested
[1],
2008 isl_dim_param
, first
, num
)))
2013 isl_space_free(space
);
2017 __isl_give isl_space
*isl_space_drop_inputs(__isl_take isl_space
*space
,
2018 unsigned first
, unsigned n
)
2022 return isl_space_drop_dims(space
, isl_dim_in
, first
, n
);
2025 __isl_give isl_space
*isl_space_drop_outputs(__isl_take isl_space
*space
,
2026 unsigned first
, unsigned n
)
2030 return isl_space_drop_dims(space
, isl_dim_out
, first
, n
);
2033 /* Remove all parameters from "space".
2035 __isl_give isl_space
*isl_space_drop_all_params(__isl_take isl_space
*space
)
2039 nparam
= isl_space_dim(space
, isl_dim_param
);
2041 return isl_space_free(space
);
2042 return isl_space_drop_dims(space
, isl_dim_param
, 0, nparam
);
2045 __isl_give isl_space
*isl_space_domain(__isl_take isl_space
*space
)
2049 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, space
->n_out
);
2050 space
= isl_space_reverse(space
);
2051 space
= mark_as_set(space
);
2055 __isl_give isl_space
*isl_space_from_domain(__isl_take isl_space
*space
)
2059 if (!isl_space_is_set(space
))
2060 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2061 "not a set space", goto error
);
2062 space
= isl_space_reverse(space
);
2063 space
= isl_space_reset(space
, isl_dim_out
);
2066 isl_space_free(space
);
2070 __isl_give isl_space
*isl_space_range(__isl_take isl_space
*space
)
2074 space
= isl_space_drop_dims(space
, isl_dim_in
, 0, space
->n_in
);
2075 space
= mark_as_set(space
);
2079 __isl_give isl_space
*isl_space_from_range(__isl_take isl_space
*space
)
2083 if (!isl_space_is_set(space
))
2084 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2085 "not a set space", goto error
);
2086 return isl_space_reset(space
, isl_dim_in
);
2088 isl_space_free(space
);
2092 /* Given a map space A -> B, return the map space [A -> B] -> A.
2094 __isl_give isl_space
*isl_space_domain_map(__isl_take isl_space
*space
)
2098 domain
= isl_space_from_range(isl_space_domain(isl_space_copy(space
)));
2099 space
= isl_space_from_domain(isl_space_wrap(space
));
2100 space
= isl_space_join(space
, domain
);
2105 /* Given a map space A -> B, return the map space [A -> B] -> B.
2107 __isl_give isl_space
*isl_space_range_map(__isl_take isl_space
*space
)
2111 range
= isl_space_from_range(isl_space_range(isl_space_copy(space
)));
2112 space
= isl_space_from_domain(isl_space_wrap(space
));
2113 space
= isl_space_join(space
, range
);
2118 __isl_give isl_space
*isl_space_params(__isl_take isl_space
*space
)
2120 isl_size n_in
, n_out
;
2122 if (isl_space_is_params(space
))
2124 n_in
= isl_space_dim(space
, isl_dim_in
);
2125 n_out
= isl_space_dim(space
, isl_dim_out
);
2126 if (n_in
< 0 || n_out
< 0)
2127 return isl_space_free(space
);
2128 space
= isl_space_drop_dims(space
, isl_dim_in
, 0, n_in
);
2129 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, n_out
);
2130 space
= mark_as_params(space
);
2134 __isl_give isl_space
*isl_space_set_from_params(__isl_take isl_space
*space
)
2138 if (!isl_space_is_params(space
))
2139 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2140 "not a parameter space", goto error
);
2141 return isl_space_reset(space
, isl_dim_set
);
2143 isl_space_free(space
);
2147 /* Add an unnamed tuple of dimension "dim" to "space".
2148 * This requires "space" to be a parameter or set space.
2150 * In particular, if "space" is a parameter space, then return
2151 * a set space with the given dimension.
2152 * If "space" is a set space, then return a map space
2153 * with "space" as domain and a range of the given dimension.
2155 __isl_give isl_space
*isl_space_add_unnamed_tuple_ui(
2156 __isl_take isl_space
*space
, unsigned dim
)
2158 isl_bool is_params
, is_set
;
2160 is_params
= isl_space_is_params(space
);
2161 is_set
= isl_space_is_set(space
);
2162 if (is_params
< 0 || is_set
< 0)
2163 return isl_space_free(space
);
2164 if (!is_params
&& !is_set
)
2165 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2166 "cannot add tuple to map space",
2167 return isl_space_free(space
));
2169 space
= isl_space_set_from_params(space
);
2171 space
= isl_space_from_domain(space
);
2172 space
= isl_space_add_dims(space
, isl_dim_out
, dim
);
2176 /* Add a tuple of dimension "dim" and with tuple identifier "tuple_id"
2178 * This requires "space" to be a parameter or set space.
2180 __isl_give isl_space
*isl_space_add_named_tuple_id_ui(
2181 __isl_take isl_space
*space
, __isl_take isl_id
*tuple_id
, unsigned dim
)
2183 space
= isl_space_add_unnamed_tuple_ui(space
, dim
);
2184 space
= isl_space_set_tuple_id(space
, isl_dim_out
, tuple_id
);
2188 /* Check that the identifiers in "tuple" do not appear as parameters
2191 static isl_stat
check_fresh_params(__isl_keep isl_space
*space
,
2192 __isl_keep isl_multi_id
*tuple
)
2197 n
= isl_multi_id_size(tuple
);
2199 return isl_stat_error
;
2200 for (i
= 0; i
< n
; ++i
) {
2204 id
= isl_multi_id_get_at(tuple
, i
);
2206 return isl_stat_error
;
2207 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2210 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2211 "parameters not unique", return isl_stat_error
);
2217 /* Add the identifiers in "tuple" as parameters of "space"
2218 * that are known to be fresh.
2220 static __isl_give isl_space
*add_bind_params(__isl_take isl_space
*space
,
2221 __isl_keep isl_multi_id
*tuple
)
2226 first
= isl_space_dim(space
, isl_dim_param
);
2227 n
= isl_multi_id_size(tuple
);
2228 if (first
< 0 || n
< 0)
2229 return isl_space_free(space
);
2230 space
= isl_space_add_dims(space
, isl_dim_param
, n
);
2231 for (i
= 0; i
< n
; ++i
) {
2234 id
= isl_multi_id_get_at(tuple
, i
);
2235 space
= isl_space_set_dim_id(space
,
2236 isl_dim_param
, first
+ i
, id
);
2242 /* Internal function that removes the set tuple of "space",
2243 * which is assumed to correspond to the range space of "tuple", and
2244 * adds the identifiers in "tuple" as fresh parameters.
2245 * In other words, the set dimensions of "space" are reinterpreted
2246 * as parameters, but stay in the same global positions.
2248 __isl_give isl_space
*isl_space_bind_set(__isl_take isl_space
*space
,
2249 __isl_keep isl_multi_id
*tuple
)
2251 isl_space
*tuple_space
;
2253 if (isl_space_check_is_set(space
) < 0)
2254 return isl_space_free(space
);
2255 tuple_space
= isl_multi_id_peek_space(tuple
);
2256 if (isl_space_check_equal_tuples(tuple_space
, space
) < 0)
2257 return isl_space_free(space
);
2258 if (check_fresh_params(space
, tuple
) < 0)
2259 return isl_space_free(space
);
2260 space
= isl_space_params(space
);
2261 space
= add_bind_params(space
, tuple
);
2265 /* Internal function that removes the domain tuple of the map space "space",
2266 * which is assumed to correspond to the range space of "tuple", and
2267 * adds the identifiers in "tuple" as fresh parameters.
2268 * In other words, the domain dimensions of "space" are reinterpreted
2269 * as parameters, but stay in the same global positions.
2271 __isl_give isl_space
*isl_space_bind_map_domain(__isl_take isl_space
*space
,
2272 __isl_keep isl_multi_id
*tuple
)
2274 isl_space
*tuple_space
;
2276 if (isl_space_check_is_map(space
) < 0)
2277 return isl_space_free(space
);
2278 tuple_space
= isl_multi_id_peek_space(tuple
);
2279 if (isl_space_check_domain_tuples(tuple_space
, space
) < 0)
2280 return isl_space_free(space
);
2281 if (check_fresh_params(space
, tuple
) < 0)
2282 return isl_space_free(space
);
2283 space
= isl_space_range(space
);
2284 space
= add_bind_params(space
, tuple
);
2288 /* Internal function that, given a space of the form [A -> B] -> C and
2289 * a tuple of identifiers in A, returns a space B -> C with
2290 * the identifiers in "tuple" added as fresh parameters.
2291 * In other words, the domain dimensions of the wrapped relation
2292 * in the domain of "space" are reinterpreted
2293 * as parameters, but stay in the same global positions.
2295 __isl_give isl_space
*isl_space_bind_domain_wrapped_domain(
2296 __isl_take isl_space
*space
, __isl_keep isl_multi_id
*tuple
)
2298 isl_space
*tuple_space
;
2300 if (isl_space_check_is_map(space
) < 0)
2301 return isl_space_free(space
);
2302 tuple_space
= isl_multi_id_peek_space(tuple
);
2303 if (isl_space_check_domain_wrapped_domain_tuples(tuple_space
,
2305 return isl_space_free(space
);
2306 if (check_fresh_params(space
, tuple
) < 0)
2307 return isl_space_free(space
);
2308 space
= isl_space_domain_factor_range(space
);
2309 space
= add_bind_params(space
, tuple
);
2313 /* Insert a domain tuple in "space" corresponding to the set space "domain".
2314 * In particular, if "space" is a parameter space, then the result
2315 * is the set space "domain" combined with the parameters of "space".
2316 * If "space" is a set space, then the result
2317 * is a map space with "domain" as domain and the original space as range.
2319 static __isl_give isl_space
*isl_space_insert_domain(
2320 __isl_take isl_space
*space
, __isl_take isl_space
*domain
)
2324 domain
= isl_space_replace_params(domain
, space
);
2326 is_params
= isl_space_is_params(space
);
2327 if (is_params
< 0) {
2328 isl_space_free(domain
);
2329 space
= isl_space_free(space
);
2330 } else if (is_params
) {
2331 isl_space_free(space
);
2334 space
= isl_space_map_from_domain_and_range(domain
, space
);
2339 /* Internal function that introduces a domain in "space"
2340 * corresponding to the range space of "tuple".
2341 * In particular, if "space" is a parameter space, then the result
2342 * is a set space. If "space" is a set space, then the result
2343 * is a map space with the original space as range.
2344 * Parameters that correspond to the identifiers in "tuple" are removed.
2346 * The parameters are removed in reverse order (under the assumption
2347 * that they appear in the same order in "multi") because
2348 * it is slightly more efficient to remove parameters at the end.
2350 * For pretty-printing purposes, the identifiers of the set dimensions
2351 * of the introduced domain are set to the identifiers in "tuple".
2353 __isl_give isl_space
*isl_space_unbind_params_insert_domain(
2354 __isl_take isl_space
*space
, __isl_keep isl_multi_id
*tuple
)
2358 isl_space
*tuple_space
;
2360 n
= isl_multi_id_size(tuple
);
2361 if (!space
|| n
< 0)
2362 return isl_space_free(space
);
2363 for (i
= n
- 1; i
>= 0; --i
) {
2367 id
= isl_multi_id_get_id(tuple
, i
);
2369 return isl_space_free(space
);
2370 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
2374 space
= isl_space_drop_dims(space
, isl_dim_param
, pos
, 1);
2376 tuple_space
= isl_multi_id_get_space(tuple
);
2377 for (i
= 0; i
< n
; ++i
) {
2380 id
= isl_multi_id_get_id(tuple
, i
);
2381 tuple_space
= isl_space_set_dim_id(tuple_space
,
2382 isl_dim_set
, i
, id
);
2384 return isl_space_insert_domain(space
, tuple_space
);
2387 __isl_give isl_space
*isl_space_underlying(__isl_take isl_space
*space
,
2393 is_set
= isl_space_is_set(space
);
2395 return isl_space_free(space
);
2396 if (n_div
== 0 && is_set
&&
2397 space
->nparam
== 0 && space
->n_in
== 0 && space
->n_id
== 0)
2398 return isl_space_reset(space
, isl_dim_out
);
2399 space
= isl_space_cow(space
);
2402 space
->n_out
+= space
->nparam
+ space
->n_in
+ n_div
;
2406 for (i
= 0; i
< space
->n_id
; ++i
)
2407 isl_id_free(get_id(space
, isl_dim_out
, i
));
2409 space
= isl_space_reset(space
, isl_dim_in
);
2410 space
= isl_space_reset(space
, isl_dim_out
);
2411 space
= mark_as_set(space
);
2416 /* Are the two spaces the same, including positions and names of parameters?
2418 isl_bool
isl_space_is_equal(__isl_keep isl_space
*space1
,
2419 __isl_keep isl_space
*space2
)
2423 if (!space1
|| !space2
)
2424 return isl_bool_error
;
2425 if (space1
== space2
)
2426 return isl_bool_true
;
2427 equal
= isl_space_has_equal_params(space1
, space2
);
2428 if (equal
< 0 || !equal
)
2430 return isl_space_has_equal_tuples(space1
, space2
);
2433 /* Do the tuples of "space1" correspond to those of the domain of "space2"?
2434 * That is, is "space1" equal to the domain of "space2", ignoring parameters.
2436 * "space2" is allowed to be a set space, in which case "space1"
2437 * should be a parameter space.
2439 isl_bool
isl_space_has_domain_tuples(__isl_keep isl_space
*space1
,
2440 __isl_keep isl_space
*space2
)
2444 is_set
= isl_space_is_set(space1
);
2445 if (is_set
< 0 || !is_set
)
2447 return isl_space_tuple_is_equal(space1
, isl_dim_set
,
2448 space2
, isl_dim_in
);
2451 /* Do the tuples of "space1" correspond to those of the range of "space2"?
2452 * That is, is "space1" equal to the range of "space2", ignoring parameters.
2454 * "space2" is allowed to be the space of a set,
2455 * in which case it should be equal to "space1", ignoring parameters.
2457 isl_bool
isl_space_has_range_tuples(__isl_keep isl_space
*space1
,
2458 __isl_keep isl_space
*space2
)
2462 is_set
= isl_space_is_set(space1
);
2463 if (is_set
< 0 || !is_set
)
2465 return isl_space_tuple_is_equal(space1
, isl_dim_set
,
2466 space2
, isl_dim_out
);
2469 /* Check that the tuples of "space1" correspond to those
2470 * of the domain of "space2".
2471 * That is, check that "space1" is equal to the domain of "space2",
2472 * ignoring parameters.
2474 isl_stat
isl_space_check_domain_tuples(__isl_keep isl_space
*space1
,
2475 __isl_keep isl_space
*space2
)
2479 is_equal
= isl_space_has_domain_tuples(space1
, space2
);
2480 return check_match(space1
, is_equal
);
2483 /* Check that the tuples of "space1" correspond to those
2484 * of the domain of the wrapped relation in the domain of "space2".
2485 * That is, check that "space1" is equal to this domain,
2486 * ignoring parameters.
2488 isl_stat
isl_space_check_domain_wrapped_domain_tuples(
2489 __isl_keep isl_space
*space1
, __isl_keep isl_space
*space2
)
2494 domain
= isl_space_unwrap(isl_space_domain(isl_space_copy(space2
)));
2495 r
= isl_space_check_domain_tuples(space1
, domain
);
2496 isl_space_free(domain
);
2501 /* Is space1 equal to the domain of space2?
2503 * In the internal version we also allow space2 to be the space of a set,
2504 * provided space1 is a parameter space.
2506 isl_bool
isl_space_is_domain_internal(__isl_keep isl_space
*space1
,
2507 __isl_keep isl_space
*space2
)
2509 isl_bool equal_params
;
2511 if (!space1
|| !space2
)
2512 return isl_bool_error
;
2513 equal_params
= isl_space_has_equal_params(space1
, space2
);
2514 if (equal_params
< 0 || !equal_params
)
2515 return equal_params
;
2516 return isl_space_has_domain_tuples(space1
, space2
);
2519 /* Is space1 equal to the domain of space2?
2521 isl_bool
isl_space_is_domain(__isl_keep isl_space
*space1
,
2522 __isl_keep isl_space
*space2
)
2525 return isl_bool_error
;
2526 if (!isl_space_is_map(space2
))
2527 return isl_bool_false
;
2528 return isl_space_is_domain_internal(space1
, space2
);
2531 /* Is space1 equal to the range of space2?
2533 * In the internal version, space2 is allowed to be the space of a set,
2534 * in which case it should be equal to space1.
2536 isl_bool
isl_space_is_range_internal(__isl_keep isl_space
*space1
,
2537 __isl_keep isl_space
*space2
)
2539 isl_bool equal_params
;
2541 if (!space1
|| !space2
)
2542 return isl_bool_error
;
2543 equal_params
= isl_space_has_equal_params(space1
, space2
);
2544 if (equal_params
< 0 || !equal_params
)
2545 return equal_params
;
2546 return isl_space_has_range_tuples(space1
, space2
);
2549 /* Is space1 equal to the range of space2?
2551 isl_bool
isl_space_is_range(__isl_keep isl_space
*space1
,
2552 __isl_keep isl_space
*space2
)
2555 return isl_bool_error
;
2556 if (!isl_space_is_map(space2
))
2557 return isl_bool_false
;
2558 return isl_space_is_range_internal(space1
, space2
);
2561 /* Update "hash" by hashing in the parameters of "space".
2563 static uint32_t isl_hash_params(uint32_t hash
, __isl_keep isl_space
*space
)
2571 isl_hash_byte(hash
, space
->nparam
% 256);
2573 for (i
= 0; i
< space
->nparam
; ++i
) {
2574 id
= get_id(space
, isl_dim_param
, i
);
2575 hash
= isl_hash_id(hash
, id
);
2581 /* Update "hash" by hashing in the tuples of "space".
2582 * Changes in this function should be reflected in isl_hash_tuples_domain.
2584 static uint32_t isl_hash_tuples(uint32_t hash
, __isl_keep isl_space
*space
)
2591 isl_hash_byte(hash
, space
->n_in
% 256);
2592 isl_hash_byte(hash
, space
->n_out
% 256);
2594 id
= tuple_id(space
, isl_dim_in
);
2595 hash
= isl_hash_id(hash
, id
);
2596 id
= tuple_id(space
, isl_dim_out
);
2597 hash
= isl_hash_id(hash
, id
);
2599 hash
= isl_hash_tuples(hash
, space
->nested
[0]);
2600 hash
= isl_hash_tuples(hash
, space
->nested
[1]);
2605 /* Update "hash" by hashing in the domain tuple of "space".
2606 * The result of this function is equal to the result of applying
2607 * isl_hash_tuples to the domain of "space".
2609 static uint32_t isl_hash_tuples_domain(uint32_t hash
,
2610 __isl_keep isl_space
*space
)
2617 isl_hash_byte(hash
, 0);
2618 isl_hash_byte(hash
, space
->n_in
% 256);
2620 hash
= isl_hash_id(hash
, &isl_id_none
);
2621 id
= tuple_id(space
, isl_dim_in
);
2622 hash
= isl_hash_id(hash
, id
);
2624 hash
= isl_hash_tuples(hash
, space
->nested
[0]);
2629 /* Return a hash value that digests the tuples of "space",
2630 * i.e., that ignores the parameters.
2631 * Changes in this function should be reflected
2632 * in isl_space_get_tuple_domain_hash.
2634 uint32_t isl_space_get_tuple_hash(__isl_keep isl_space
*space
)
2641 hash
= isl_hash_init();
2642 hash
= isl_hash_tuples(hash
, space
);
2647 /* Return the hash value of "space".
2649 uint32_t isl_space_get_full_hash(__isl_keep isl_space
*space
)
2656 hash
= isl_hash_init();
2657 hash
= isl_hash_params(hash
, space
);
2658 hash
= isl_hash_tuples(hash
, space
);
2663 /* Return the hash value of the domain tuple of "space".
2664 * That is, isl_space_get_tuple_domain_hash(space) is equal to
2665 * isl_space_get_tuple_hash(isl_space_domain(space)).
2667 uint32_t isl_space_get_tuple_domain_hash(__isl_keep isl_space
*space
)
2674 hash
= isl_hash_init();
2675 hash
= isl_hash_tuples_domain(hash
, space
);
2680 /* Is "space" the space of a set wrapping a map space?
2682 isl_bool
isl_space_is_wrapping(__isl_keep isl_space
*space
)
2685 return isl_bool_error
;
2687 if (!isl_space_is_set(space
))
2688 return isl_bool_false
;
2690 return isl_bool_ok(space
->nested
[1] != NULL
);
2693 /* Is "space" the space of a map where the domain is a wrapped map space?
2695 isl_bool
isl_space_domain_is_wrapping(__isl_keep isl_space
*space
)
2698 return isl_bool_error
;
2700 if (isl_space_is_set(space
))
2701 return isl_bool_false
;
2703 return isl_bool_ok(space
->nested
[0] != NULL
);
2706 /* Is "space" the space of a map where the range is a wrapped map space?
2708 isl_bool
isl_space_range_is_wrapping(__isl_keep isl_space
*space
)
2711 return isl_bool_error
;
2713 if (isl_space_is_set(space
))
2714 return isl_bool_false
;
2716 return isl_bool_ok(space
->nested
[1] != NULL
);
2719 /* Is "space" a product of two spaces?
2720 * That is, is it a wrapping set space or a map space
2721 * with wrapping domain and range?
2723 isl_bool
isl_space_is_product(__isl_keep isl_space
*space
)
2726 isl_bool is_product
;
2728 is_set
= isl_space_is_set(space
);
2730 return isl_bool_error
;
2732 return isl_space_is_wrapping(space
);
2733 is_product
= isl_space_domain_is_wrapping(space
);
2734 if (is_product
< 0 || !is_product
)
2736 return isl_space_range_is_wrapping(space
);
2739 __isl_give isl_space
*isl_space_wrap(__isl_take isl_space
*space
)
2746 wrap
= isl_space_set_alloc(space
->ctx
,
2747 space
->nparam
, space
->n_in
+ space
->n_out
);
2749 wrap
= copy_ids(wrap
, isl_dim_param
, 0, space
, isl_dim_param
);
2750 wrap
= copy_ids(wrap
, isl_dim_set
, 0, space
, isl_dim_in
);
2751 wrap
= copy_ids(wrap
, isl_dim_set
, space
->n_in
, space
, isl_dim_out
);
2756 wrap
->nested
[1] = space
;
2760 isl_space_free(space
);
2764 __isl_give isl_space
*isl_space_unwrap(__isl_take isl_space
*space
)
2771 if (!isl_space_is_wrapping(space
))
2772 isl_die(space
->ctx
, isl_error_invalid
, "not a wrapping space",
2775 unwrap
= isl_space_copy(space
->nested
[1]);
2776 isl_space_free(space
);
2780 isl_space_free(space
);
2784 isl_bool
isl_space_is_named_or_nested(__isl_keep isl_space
*space
,
2785 enum isl_dim_type type
)
2787 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
2788 return isl_bool_false
;
2790 return isl_bool_error
;
2791 if (space
->tuple_id
[type
- isl_dim_in
])
2792 return isl_bool_true
;
2793 if (space
->nested
[type
- isl_dim_in
])
2794 return isl_bool_true
;
2795 return isl_bool_false
;
2798 isl_bool
isl_space_may_be_set(__isl_keep isl_space
*space
)
2804 return isl_bool_error
;
2805 if (isl_space_is_set(space
))
2806 return isl_bool_true
;
2807 n_in
= isl_space_dim(space
, isl_dim_in
);
2809 return isl_bool_error
;
2811 return isl_bool_false
;
2812 nested
= isl_space_is_named_or_nested(space
, isl_dim_in
);
2813 if (nested
< 0 || nested
)
2814 return isl_bool_not(nested
);
2815 return isl_bool_true
;
2818 __isl_give isl_space
*isl_space_reset(__isl_take isl_space
*space
,
2819 enum isl_dim_type type
)
2821 if (!isl_space_is_named_or_nested(space
, type
))
2824 space
= isl_space_cow(space
);
2828 isl_id_free(space
->tuple_id
[type
- isl_dim_in
]);
2829 space
->tuple_id
[type
- isl_dim_in
] = NULL
;
2830 isl_space_free(space
->nested
[type
- isl_dim_in
]);
2831 space
->nested
[type
- isl_dim_in
] = NULL
;
2836 __isl_give isl_space
*isl_space_flatten(__isl_take isl_space
*space
)
2840 if (!space
->nested
[0] && !space
->nested
[1])
2843 if (space
->nested
[0])
2844 space
= isl_space_reset(space
, isl_dim_in
);
2845 if (space
&& space
->nested
[1])
2846 space
= isl_space_reset(space
, isl_dim_out
);
2851 __isl_give isl_space
*isl_space_flatten_domain(__isl_take isl_space
*space
)
2855 if (!space
->nested
[0])
2858 return isl_space_reset(space
, isl_dim_in
);
2861 __isl_give isl_space
*isl_space_flatten_range(__isl_take isl_space
*space
)
2865 if (!space
->nested
[1])
2868 return isl_space_reset(space
, isl_dim_out
);
2871 /* Replace the parameters of dst by those of src.
2873 __isl_give isl_space
*isl_space_replace_params(__isl_take isl_space
*dst
,
2874 __isl_keep isl_space
*src
)
2876 isl_size dst_dim
, src_dim
;
2877 isl_bool equal_params
;
2878 enum isl_dim_type type
= isl_dim_param
;
2880 equal_params
= isl_space_has_equal_params(dst
, src
);
2881 if (equal_params
< 0)
2882 return isl_space_free(dst
);
2886 dst
= isl_space_cow(dst
);
2888 dst_dim
= isl_space_dim(dst
, type
);
2889 src_dim
= isl_space_dim(src
, type
);
2890 if (dst_dim
< 0 || src_dim
< 0)
2893 dst
= isl_space_drop_dims(dst
, type
, 0, dst_dim
);
2894 dst
= isl_space_add_dims(dst
, type
, src_dim
);
2895 dst
= copy_ids(dst
, type
, 0, src
, type
);
2899 for (i
= 0; i
<= 1; ++i
) {
2902 if (!dst
->nested
[i
])
2904 nested
= isl_space_take_nested(dst
, i
);
2905 nested
= isl_space_replace_params(nested
, src
);
2906 dst
= isl_space_restore_nested(dst
, i
, nested
);
2914 isl_space_free(dst
);
2918 /* Given two tuples ("dst_type" in "dst" and "src_type" in "src")
2919 * of the same size, check if any of the dimensions in the "dst" tuple
2920 * have no identifier, while the corresponding dimensions in "src"
2921 * does have an identifier,
2922 * If so, copy the identifier over to "dst".
2924 __isl_give isl_space
*isl_space_copy_ids_if_unset(__isl_take isl_space
*dst
,
2925 enum isl_dim_type dst_type
, __isl_keep isl_space
*src
,
2926 enum isl_dim_type src_type
)
2931 n
= isl_space_dim(dst
, dst_type
);
2933 return isl_space_free(dst
);
2934 for (i
= 0; i
< n
; ++i
) {
2938 set
= isl_space_has_dim_id(dst
, dst_type
, i
);
2940 return isl_space_free(dst
);
2944 set
= isl_space_has_dim_id(src
, src_type
, i
);
2946 return isl_space_free(dst
);
2950 id
= isl_space_get_dim_id(src
, src_type
, i
);
2951 dst
= isl_space_set_dim_id(dst
, dst_type
, i
, id
);
2957 /* Given a space "space" of a set, create a space
2958 * for the lift of the set. In particular, the result
2959 * is of the form lifted[space -> local[..]], with n_local variables in the
2960 * range of the wrapped map.
2962 __isl_give isl_space
*isl_space_lift(__isl_take isl_space
*space
,
2965 isl_space
*local_space
;
2970 local_space
= isl_space_dup(space
);
2971 local_space
= isl_space_drop_dims(local_space
, isl_dim_set
, 0,
2973 local_space
= isl_space_add_dims(local_space
, isl_dim_set
, n_local
);
2974 local_space
= isl_space_set_tuple_name(local_space
,
2975 isl_dim_set
, "local");
2976 space
= isl_space_join(isl_space_from_domain(space
),
2977 isl_space_from_range(local_space
));
2978 space
= isl_space_wrap(space
);
2979 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "lifted");
2984 isl_bool
isl_space_can_zip(__isl_keep isl_space
*space
)
2988 is_set
= isl_space_is_set(space
);
2990 return isl_bool_error
;
2992 return isl_bool_false
;
2993 return isl_space_is_product(space
);
2996 __isl_give isl_space
*isl_space_zip(__isl_take isl_space
*space
)
2998 isl_space
*dom
, *ran
;
2999 isl_space
*dom_dom
, *dom_ran
, *ran_dom
, *ran_ran
;
3001 if (!isl_space_can_zip(space
))
3002 isl_die(space
->ctx
, isl_error_invalid
, "space cannot be zipped",
3007 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(space
)));
3008 ran
= isl_space_unwrap(isl_space_range(space
));
3009 dom_dom
= isl_space_domain(isl_space_copy(dom
));
3010 dom_ran
= isl_space_range(dom
);
3011 ran_dom
= isl_space_domain(isl_space_copy(ran
));
3012 ran_ran
= isl_space_range(ran
);
3013 dom
= isl_space_join(isl_space_from_domain(dom_dom
),
3014 isl_space_from_range(ran_dom
));
3015 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
3016 isl_space_from_range(ran_ran
));
3017 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
3018 isl_space_from_range(isl_space_wrap(ran
)));
3020 isl_space_free(space
);
3024 /* Can we apply isl_space_curry to "space"?
3025 * That is, does is it have a map space with a nested relation in its domain?
3027 isl_bool
isl_space_can_curry(__isl_keep isl_space
*space
)
3029 return isl_space_domain_is_wrapping(space
);
3032 /* Given a space (A -> B) -> C, return the corresponding space
3035 __isl_give isl_space
*isl_space_curry(__isl_take isl_space
*space
)
3037 isl_space
*dom
, *ran
;
3038 isl_space
*dom_dom
, *dom_ran
;
3043 if (!isl_space_can_curry(space
))
3044 isl_die(space
->ctx
, isl_error_invalid
,
3045 "space cannot be curried", goto error
);
3047 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(space
)));
3048 ran
= isl_space_range(space
);
3049 dom_dom
= isl_space_domain(isl_space_copy(dom
));
3050 dom_ran
= isl_space_range(dom
);
3051 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
3052 isl_space_from_range(ran
));
3053 return isl_space_join(isl_space_from_domain(dom_dom
),
3054 isl_space_from_range(isl_space_wrap(ran
)));
3056 isl_space_free(space
);
3060 /* Can isl_space_range_curry be applied to "space"?
3061 * That is, does it have a nested relation in its range,
3062 * the domain of which is itself a nested relation?
3064 isl_bool
isl_space_can_range_curry(__isl_keep isl_space
*space
)
3069 return isl_bool_error
;
3070 can
= isl_space_range_is_wrapping(space
);
3071 if (can
< 0 || !can
)
3073 return isl_space_can_curry(space
->nested
[1]);
3076 /* Given a space A -> ((B -> C) -> D), return the corresponding space
3077 * A -> (B -> (C -> D)).
3079 __isl_give isl_space
*isl_space_range_curry(__isl_take isl_space
*space
)
3086 if (!isl_space_can_range_curry(space
))
3087 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3088 "space range cannot be curried",
3089 return isl_space_free(space
));
3091 nested
= isl_space_take_nested(space
, 1);
3092 nested
= isl_space_curry(nested
);
3093 space
= isl_space_restore_nested(space
, 1, nested
);
3098 /* Can we apply isl_space_uncurry to "space"?
3099 * That is, does it have a map space with a nested relation in its range?
3101 isl_bool
isl_space_can_uncurry(__isl_keep isl_space
*space
)
3103 return isl_space_range_is_wrapping(space
);
3106 /* Given a space A -> (B -> C), return the corresponding space
3109 __isl_give isl_space
*isl_space_uncurry(__isl_take isl_space
*space
)
3111 isl_space
*dom
, *ran
;
3112 isl_space
*ran_dom
, *ran_ran
;
3117 if (!isl_space_can_uncurry(space
))
3118 isl_die(space
->ctx
, isl_error_invalid
,
3119 "space cannot be uncurried",
3120 return isl_space_free(space
));
3122 dom
= isl_space_domain(isl_space_copy(space
));
3123 ran
= isl_space_unwrap(isl_space_range(space
));
3124 ran_dom
= isl_space_domain(isl_space_copy(ran
));
3125 ran_ran
= isl_space_range(ran
);
3126 dom
= isl_space_join(isl_space_from_domain(dom
),
3127 isl_space_from_range(ran_dom
));
3128 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
3129 isl_space_from_range(ran_ran
));
3132 isl_bool
isl_space_has_named_params(__isl_keep isl_space
*space
)
3138 return isl_bool_error
;
3139 if (space
->nparam
== 0)
3140 return isl_bool_true
;
3141 off
= isl_space_offset(space
, isl_dim_param
);
3142 if (off
+ space
->nparam
> space
->n_id
)
3143 return isl_bool_false
;
3144 for (i
= 0; i
< space
->nparam
; ++i
)
3145 if (!space
->ids
[off
+ i
])
3146 return isl_bool_false
;
3147 return isl_bool_true
;
3150 /* Check that "space" has only named parameters, reporting an error
3153 isl_stat
isl_space_check_named_params(__isl_keep isl_space
*space
)
3157 named
= isl_space_has_named_params(space
);
3159 return isl_stat_error
;
3161 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
3162 "unexpected unnamed parameters", return isl_stat_error
);
3167 /* Align the initial parameters of space1 to match the order in space2.
3169 __isl_give isl_space
*isl_space_align_params(__isl_take isl_space
*space1
,
3170 __isl_take isl_space
*space2
)
3172 isl_reordering
*exp
;
3174 if (isl_space_check_named_params(space1
) < 0 ||
3175 isl_space_check_named_params(space2
) < 0)
3178 exp
= isl_parameter_alignment_reordering(space1
, space2
);
3179 exp
= isl_reordering_extend_space(exp
, space1
);
3180 isl_space_free(space2
);
3181 space1
= isl_reordering_get_space(exp
);
3182 isl_reordering_free(exp
);
3185 isl_space_free(space1
);
3186 isl_space_free(space2
);
3190 /* Given the space of set (domain), construct a space for a map
3191 * with as domain the given space and as range the range of "model".
3193 __isl_give isl_space
*isl_space_extend_domain_with_range(
3194 __isl_take isl_space
*space
, __isl_take isl_space
*model
)
3201 space
= isl_space_from_domain(space
);
3202 n_out
= isl_space_dim(model
, isl_dim_out
);
3205 space
= isl_space_add_dims(space
, isl_dim_out
, n_out
);
3206 if (isl_space_has_tuple_id(model
, isl_dim_out
))
3207 space
= isl_space_set_tuple_id(space
, isl_dim_out
,
3208 isl_space_get_tuple_id(model
, isl_dim_out
));
3211 if (model
->nested
[1]) {
3212 isl_space
*nested
= isl_space_copy(model
->nested
[1]);
3213 isl_size n_nested
, n_space
;
3214 nested
= isl_space_align_params(nested
, isl_space_copy(space
));
3215 n_nested
= isl_space_dim(nested
, isl_dim_param
);
3216 n_space
= isl_space_dim(space
, isl_dim_param
);
3217 if (n_nested
< 0 || n_space
< 0)
3219 if (n_nested
> n_space
)
3220 nested
= isl_space_drop_dims(nested
, isl_dim_param
,
3221 n_space
, n_nested
- n_space
);
3224 space
->nested
[1] = nested
;
3226 isl_space_free(model
);
3229 isl_space_free(model
);
3230 isl_space_free(space
);
3234 /* Compare the "type" dimensions of two isl_spaces.
3236 * The order is fairly arbitrary.
3238 static int isl_space_cmp_type(__isl_keep isl_space
*space1
,
3239 __isl_keep isl_space
*space2
, enum isl_dim_type type
)
3242 isl_size dim1
, dim2
;
3243 isl_space
*nested1
, *nested2
;
3245 dim1
= isl_space_dim(space1
, type
);
3246 dim2
= isl_space_dim(space2
, type
);
3247 if (dim1
< 0 || dim2
< 0)
3252 cmp
= isl_id_cmp(tuple_id(space1
, type
), tuple_id(space2
, type
));
3256 nested1
= nested(space1
, type
);
3257 nested2
= nested(space2
, type
);
3258 if (!nested1
!= !nested2
)
3259 return !nested1
- !nested2
;
3262 return isl_space_cmp(nested1
, nested2
);
3267 /* Compare two isl_spaces.
3269 * The order is fairly arbitrary.
3271 int isl_space_cmp(__isl_keep isl_space
*space1
, __isl_keep isl_space
*space2
)
3276 if (space1
== space2
)
3283 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_param
);
3286 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_in
);
3289 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_out
);
3293 if (!space1
->ids
&& !space2
->ids
)
3296 for (i
= 0; i
< n(space1
, isl_dim_param
); ++i
) {
3297 cmp
= isl_id_cmp(get_id(space1
, isl_dim_param
, i
),
3298 get_id(space2
, isl_dim_param
, i
));