2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2013-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
17 #include <isl_space_private.h>
18 #include <isl_id_private.h>
19 #include <isl_reordering.h>
21 isl_ctx
*isl_space_get_ctx(__isl_keep isl_space
*dim
)
23 return dim
? dim
->ctx
: NULL
;
26 __isl_give isl_space
*isl_space_alloc(isl_ctx
*ctx
,
27 unsigned nparam
, unsigned n_in
, unsigned n_out
)
31 dim
= isl_alloc_type(ctx
, struct isl_space
);
42 dim
->tuple_id
[0] = NULL
;
43 dim
->tuple_id
[1] = NULL
;
45 dim
->nested
[0] = NULL
;
46 dim
->nested
[1] = NULL
;
54 /* Mark the space as being that of a set, by setting the domain tuple
57 static __isl_give isl_space
*mark_as_set(__isl_take isl_space
*space
)
59 space
= isl_space_cow(space
);
62 space
= isl_space_set_tuple_id(space
, isl_dim_in
, &isl_id_none
);
66 /* Is the space that of a set?
68 isl_bool
isl_space_is_set(__isl_keep isl_space
*space
)
71 return isl_bool_error
;
72 if (space
->n_in
!= 0 || space
->nested
[0])
73 return isl_bool_false
;
74 if (space
->tuple_id
[0] != &isl_id_none
)
75 return isl_bool_false
;
79 /* Is the given space that of a map?
81 isl_bool
isl_space_is_map(__isl_keep isl_space
*space
)
84 return isl_bool_error
;
85 return space
->tuple_id
[0] != &isl_id_none
&&
86 space
->tuple_id
[1] != &isl_id_none
;
89 __isl_give isl_space
*isl_space_set_alloc(isl_ctx
*ctx
,
90 unsigned nparam
, unsigned dim
)
93 space
= isl_space_alloc(ctx
, nparam
, 0, dim
);
94 space
= mark_as_set(space
);
98 /* Mark the space as being that of a parameter domain, by setting
99 * both tuples to isl_id_none.
101 static __isl_give isl_space
*mark_as_params(isl_space
*space
)
105 space
= isl_space_set_tuple_id(space
, isl_dim_in
, &isl_id_none
);
106 space
= isl_space_set_tuple_id(space
, isl_dim_out
, &isl_id_none
);
110 /* Is the space that of a parameter domain?
112 isl_bool
isl_space_is_params(__isl_keep isl_space
*space
)
115 return isl_bool_error
;
116 if (space
->n_in
!= 0 || space
->nested
[0] ||
117 space
->n_out
!= 0 || space
->nested
[1])
118 return isl_bool_false
;
119 if (space
->tuple_id
[0] != &isl_id_none
)
120 return isl_bool_false
;
121 if (space
->tuple_id
[1] != &isl_id_none
)
122 return isl_bool_false
;
123 return isl_bool_true
;
126 /* Create a space for a parameter domain.
128 __isl_give isl_space
*isl_space_params_alloc(isl_ctx
*ctx
, unsigned nparam
)
131 space
= isl_space_alloc(ctx
, nparam
, 0, 0);
132 space
= mark_as_params(space
);
136 static unsigned global_pos(__isl_keep isl_space
*dim
,
137 enum isl_dim_type type
, unsigned pos
)
139 struct isl_ctx
*ctx
= dim
->ctx
;
143 isl_assert(ctx
, pos
< dim
->nparam
,
144 return isl_space_dim(dim
, isl_dim_all
));
147 isl_assert(ctx
, pos
< dim
->n_in
,
148 return isl_space_dim(dim
, isl_dim_all
));
149 return pos
+ dim
->nparam
;
151 isl_assert(ctx
, pos
< dim
->n_out
,
152 return isl_space_dim(dim
, isl_dim_all
));
153 return pos
+ dim
->nparam
+ dim
->n_in
;
155 isl_assert(ctx
, 0, return isl_space_dim(dim
, isl_dim_all
));
157 return isl_space_dim(dim
, isl_dim_all
);
160 /* Extend length of ids array to the total number of dimensions.
162 static __isl_give isl_space
*extend_ids(__isl_take isl_space
*dim
)
167 if (isl_space_dim(dim
, isl_dim_all
) <= dim
->n_id
)
171 dim
->ids
= isl_calloc_array(dim
->ctx
,
172 isl_id
*, isl_space_dim(dim
, isl_dim_all
));
176 ids
= isl_realloc_array(dim
->ctx
, dim
->ids
,
177 isl_id
*, isl_space_dim(dim
, isl_dim_all
));
181 for (i
= dim
->n_id
; i
< isl_space_dim(dim
, isl_dim_all
); ++i
)
185 dim
->n_id
= isl_space_dim(dim
, isl_dim_all
);
193 static __isl_give isl_space
*set_id(__isl_take isl_space
*dim
,
194 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
196 dim
= isl_space_cow(dim
);
201 pos
= global_pos(dim
, type
, pos
);
202 if (pos
== isl_space_dim(dim
, isl_dim_all
))
205 if (pos
>= dim
->n_id
) {
208 dim
= extend_ids(dim
);
222 static __isl_keep isl_id
*get_id(__isl_keep isl_space
*dim
,
223 enum isl_dim_type type
, unsigned pos
)
228 pos
= global_pos(dim
, type
, pos
);
229 if (pos
== isl_space_dim(dim
, isl_dim_all
))
231 if (pos
>= dim
->n_id
)
233 return dim
->ids
[pos
];
236 static unsigned offset(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
239 case isl_dim_param
: return 0;
240 case isl_dim_in
: return dim
->nparam
;
241 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
246 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
249 case isl_dim_param
: return dim
->nparam
;
250 case isl_dim_in
: return dim
->n_in
;
251 case isl_dim_out
: return dim
->n_out
;
252 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
257 unsigned isl_space_dim(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
264 unsigned isl_space_offset(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
268 return offset(dim
, type
);
271 static __isl_give isl_space
*copy_ids(__isl_take isl_space
*dst
,
272 enum isl_dim_type dst_type
, unsigned offset
, __isl_keep isl_space
*src
,
273 enum isl_dim_type src_type
)
281 for (i
= 0; i
< n(src
, src_type
); ++i
) {
282 id
= get_id(src
, src_type
, i
);
285 dst
= set_id(dst
, dst_type
, offset
+ i
, isl_id_copy(id
));
292 __isl_take isl_space
*isl_space_dup(__isl_keep isl_space
*dim
)
297 dup
= isl_space_alloc(dim
->ctx
, dim
->nparam
, dim
->n_in
, dim
->n_out
);
300 if (dim
->tuple_id
[0] &&
301 !(dup
->tuple_id
[0] = isl_id_copy(dim
->tuple_id
[0])))
303 if (dim
->tuple_id
[1] &&
304 !(dup
->tuple_id
[1] = isl_id_copy(dim
->tuple_id
[1])))
306 if (dim
->nested
[0] && !(dup
->nested
[0] = isl_space_copy(dim
->nested
[0])))
308 if (dim
->nested
[1] && !(dup
->nested
[1] = isl_space_copy(dim
->nested
[1])))
312 dup
= copy_ids(dup
, isl_dim_param
, 0, dim
, isl_dim_param
);
313 dup
= copy_ids(dup
, isl_dim_in
, 0, dim
, isl_dim_in
);
314 dup
= copy_ids(dup
, isl_dim_out
, 0, dim
, isl_dim_out
);
321 __isl_give isl_space
*isl_space_cow(__isl_take isl_space
*dim
)
329 return isl_space_dup(dim
);
332 __isl_give isl_space
*isl_space_copy(__isl_keep isl_space
*dim
)
341 __isl_null isl_space
*isl_space_free(__isl_take isl_space
*space
)
348 if (--space
->ref
> 0)
351 isl_id_free(space
->tuple_id
[0]);
352 isl_id_free(space
->tuple_id
[1]);
354 isl_space_free(space
->nested
[0]);
355 isl_space_free(space
->nested
[1]);
357 for (i
= 0; i
< space
->n_id
; ++i
)
358 isl_id_free(space
->ids
[i
]);
360 isl_ctx_deref(space
->ctx
);
367 /* Check if "s" is a valid dimension or tuple name.
368 * We currently only forbid names that look like a number.
370 * s is assumed to be non-NULL.
372 static int name_ok(isl_ctx
*ctx
, const char *s
)
377 dummy
= strtol(s
, &p
, 0);
379 isl_die(ctx
, isl_error_invalid
, "name looks like a number",
385 /* Is it possible for the given dimension type to have a tuple id?
387 static int space_can_have_id(__isl_keep isl_space
*space
,
388 enum isl_dim_type type
)
392 if (isl_space_is_params(space
))
393 isl_die(space
->ctx
, isl_error_invalid
,
394 "parameter spaces don't have tuple ids", return 0);
395 if (isl_space_is_set(space
) && type
!= isl_dim_set
)
396 isl_die(space
->ctx
, isl_error_invalid
,
397 "set spaces can only have a set id", return 0);
398 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
399 isl_die(space
->ctx
, isl_error_invalid
,
400 "only input, output and set tuples can have ids",
406 /* Does the tuple have an id?
408 isl_bool
isl_space_has_tuple_id(__isl_keep isl_space
*dim
,
409 enum isl_dim_type type
)
411 if (!space_can_have_id(dim
, type
))
412 return isl_bool_error
;
413 return dim
->tuple_id
[type
- isl_dim_in
] != NULL
;
416 __isl_give isl_id
*isl_space_get_tuple_id(__isl_keep isl_space
*dim
,
417 enum isl_dim_type type
)
423 has_id
= isl_space_has_tuple_id(dim
, type
);
427 isl_die(dim
->ctx
, isl_error_invalid
,
428 "tuple has no id", return NULL
);
429 return isl_id_copy(dim
->tuple_id
[type
- isl_dim_in
]);
432 __isl_give isl_space
*isl_space_set_tuple_id(__isl_take isl_space
*dim
,
433 enum isl_dim_type type
, __isl_take isl_id
*id
)
435 dim
= isl_space_cow(dim
);
438 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
439 isl_die(dim
->ctx
, isl_error_invalid
,
440 "only input, output and set tuples can have names",
443 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
444 dim
->tuple_id
[type
- isl_dim_in
] = id
;
453 __isl_give isl_space
*isl_space_reset_tuple_id(__isl_take isl_space
*dim
,
454 enum isl_dim_type type
)
456 dim
= isl_space_cow(dim
);
459 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
460 isl_die(dim
->ctx
, isl_error_invalid
,
461 "only input, output and set tuples can have names",
464 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
465 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
473 /* Set the id of the given dimension of "space" to "id".
474 * If the dimension already has an id, then it is replaced.
475 * If the dimension is a parameter, then we need to change it
476 * in the nested spaces (if any) as well.
478 __isl_give isl_space
*isl_space_set_dim_id(__isl_take isl_space
*space
,
479 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
481 space
= isl_space_cow(space
);
485 if (type
== isl_dim_param
) {
488 for (i
= 0; i
< 2; ++i
) {
489 if (!space
->nested
[i
])
492 isl_space_set_dim_id(space
->nested
[i
],
493 type
, pos
, isl_id_copy(id
));
494 if (!space
->nested
[i
])
499 isl_id_free(get_id(space
, type
, pos
));
500 return set_id(space
, type
, pos
, id
);
503 isl_space_free(space
);
507 /* Reset the id of the given dimension of "space".
508 * If the dimension already has an id, then it is removed.
509 * If the dimension is a parameter, then we need to reset it
510 * in the nested spaces (if any) as well.
512 __isl_give isl_space
*isl_space_reset_dim_id(__isl_take isl_space
*space
,
513 enum isl_dim_type type
, unsigned pos
)
515 space
= isl_space_cow(space
);
519 if (type
== isl_dim_param
) {
522 for (i
= 0; i
< 2; ++i
) {
523 if (!space
->nested
[i
])
526 isl_space_reset_dim_id(space
->nested
[i
],
528 if (!space
->nested
[i
])
533 isl_id_free(get_id(space
, type
, pos
));
534 return set_id(space
, type
, pos
, NULL
);
536 isl_space_free(space
);
540 isl_bool
isl_space_has_dim_id(__isl_keep isl_space
*dim
,
541 enum isl_dim_type type
, unsigned pos
)
544 return isl_bool_error
;
545 return get_id(dim
, type
, pos
) != NULL
;
548 __isl_give isl_id
*isl_space_get_dim_id(__isl_keep isl_space
*dim
,
549 enum isl_dim_type type
, unsigned pos
)
553 if (!get_id(dim
, type
, pos
))
554 isl_die(dim
->ctx
, isl_error_invalid
,
555 "dim has no id", return NULL
);
556 return isl_id_copy(get_id(dim
, type
, pos
));
559 __isl_give isl_space
*isl_space_set_tuple_name(__isl_take isl_space
*dim
,
560 enum isl_dim_type type
, const char *s
)
568 return isl_space_reset_tuple_id(dim
, type
);
570 if (!name_ok(dim
->ctx
, s
))
573 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
574 return isl_space_set_tuple_id(dim
, type
, id
);
580 /* Does the tuple have a name?
582 isl_bool
isl_space_has_tuple_name(__isl_keep isl_space
*space
,
583 enum isl_dim_type type
)
587 if (!space_can_have_id(space
, type
))
588 return isl_bool_error
;
589 id
= space
->tuple_id
[type
- isl_dim_in
];
590 return id
&& id
->name
;
593 const char *isl_space_get_tuple_name(__isl_keep isl_space
*dim
,
594 enum isl_dim_type type
)
599 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
601 id
= dim
->tuple_id
[type
- isl_dim_in
];
602 return id
? id
->name
: NULL
;
605 __isl_give isl_space
*isl_space_set_dim_name(__isl_take isl_space
*dim
,
606 enum isl_dim_type type
, unsigned pos
,
614 return isl_space_reset_dim_id(dim
, type
, pos
);
615 if (!name_ok(dim
->ctx
, s
))
617 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
618 return isl_space_set_dim_id(dim
, type
, pos
, id
);
624 /* Does the given dimension have a name?
626 isl_bool
isl_space_has_dim_name(__isl_keep isl_space
*space
,
627 enum isl_dim_type type
, unsigned pos
)
632 return isl_bool_error
;
633 id
= get_id(space
, type
, pos
);
634 return id
&& id
->name
;
637 __isl_keep
const char *isl_space_get_dim_name(__isl_keep isl_space
*dim
,
638 enum isl_dim_type type
, unsigned pos
)
640 isl_id
*id
= get_id(dim
, type
, pos
);
641 return id
? id
->name
: NULL
;
644 int isl_space_find_dim_by_id(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
645 __isl_keep isl_id
*id
)
654 offset
= isl_space_offset(dim
, type
);
655 n
= isl_space_dim(dim
, type
);
656 for (i
= 0; i
< n
&& offset
+ i
< dim
->n_id
; ++i
)
657 if (dim
->ids
[offset
+ i
] == id
)
663 int isl_space_find_dim_by_name(__isl_keep isl_space
*space
,
664 enum isl_dim_type type
, const char *name
)
673 offset
= isl_space_offset(space
, type
);
674 n
= isl_space_dim(space
, type
);
675 for (i
= 0; i
< n
&& offset
+ i
< space
->n_id
; ++i
) {
676 isl_id
*id
= get_id(space
, type
, i
);
677 if (id
&& id
->name
&& !strcmp(id
->name
, name
))
684 /* Reset the user pointer on all identifiers of parameters and tuples
687 __isl_give isl_space
*isl_space_reset_user(__isl_take isl_space
*space
)
697 ctx
= isl_space_get_ctx(space
);
699 for (i
= 0; i
< space
->nparam
&& i
< space
->n_id
; ++i
) {
700 if (!isl_id_get_user(space
->ids
[i
]))
702 space
= isl_space_cow(space
);
705 name
= isl_id_get_name(space
->ids
[i
]);
706 id
= isl_id_alloc(ctx
, name
, NULL
);
707 isl_id_free(space
->ids
[i
]);
710 return isl_space_free(space
);
713 for (i
= 0; i
< 2; ++i
) {
714 if (!space
->tuple_id
[i
])
716 if (!isl_id_get_user(space
->tuple_id
[i
]))
718 space
= isl_space_cow(space
);
721 name
= isl_id_get_name(space
->tuple_id
[i
]);
722 id
= isl_id_alloc(ctx
, name
, NULL
);
723 isl_id_free(space
->tuple_id
[i
]);
724 space
->tuple_id
[i
] = id
;
726 return isl_space_free(space
);
729 for (i
= 0; i
< 2; ++i
) {
730 if (!space
->nested
[i
])
732 space
= isl_space_cow(space
);
735 space
->nested
[i
] = isl_space_reset_user(space
->nested
[i
]);
736 if (!space
->nested
[i
])
737 return isl_space_free(space
);
743 static __isl_keep isl_id
*tuple_id(__isl_keep isl_space
*dim
,
744 enum isl_dim_type type
)
748 if (type
== isl_dim_in
)
749 return dim
->tuple_id
[0];
750 if (type
== isl_dim_out
)
751 return dim
->tuple_id
[1];
755 static __isl_keep isl_space
*nested(__isl_keep isl_space
*dim
,
756 enum isl_dim_type type
)
760 if (type
== isl_dim_in
)
761 return dim
->nested
[0];
762 if (type
== isl_dim_out
)
763 return dim
->nested
[1];
767 /* Are the two spaces the same, apart from positions and names of parameters?
769 isl_bool
isl_space_has_equal_tuples(__isl_keep isl_space
*space1
,
770 __isl_keep isl_space
*space2
)
772 if (!space1
|| !space2
)
773 return isl_bool_error
;
774 if (space1
== space2
)
775 return isl_bool_true
;
776 return isl_space_tuple_is_equal(space1
, isl_dim_in
,
777 space2
, isl_dim_in
) &&
778 isl_space_tuple_is_equal(space1
, isl_dim_out
,
779 space2
, isl_dim_out
);
782 /* Check if the tuple of type "type1" of "space1" is the same as
783 * the tuple of type "type2" of "space2".
785 * That is, check if the tuples have the same identifier, the same dimension
786 * and the same internal structure.
787 * The identifiers of the dimensions inside the tuples do not affect the result.
789 * Note that this function only checks the tuples themselves.
790 * If nested tuples are involved, then we need to be careful not
791 * to have result affected by possibly differing parameters
792 * in those nested tuples.
794 isl_bool
isl_space_tuple_is_equal(__isl_keep isl_space
*space1
,
795 enum isl_dim_type type1
, __isl_keep isl_space
*space2
,
796 enum isl_dim_type type2
)
799 isl_space
*nested1
, *nested2
;
801 if (!space1
|| !space2
)
802 return isl_bool_error
;
804 if (space1
== space2
&& type1
== type2
)
805 return isl_bool_true
;
807 if (n(space1
, type1
) != n(space2
, type2
))
808 return isl_bool_false
;
809 id1
= tuple_id(space1
, type1
);
810 id2
= tuple_id(space2
, type2
);
812 return isl_bool_false
;
813 if (id1
&& id1
!= id2
)
814 return isl_bool_false
;
815 nested1
= nested(space1
, type1
);
816 nested2
= nested(space2
, type2
);
817 if (!nested1
^ !nested2
)
818 return isl_bool_false
;
819 if (nested1
&& !isl_space_has_equal_tuples(nested1
, nested2
))
820 return isl_bool_false
;
821 return isl_bool_true
;
824 /* This is the old, undocumented, name for isl_space_tuple_is_equal.
825 * It will be removed at some point.
827 int isl_space_tuple_match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
828 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
830 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
833 static isl_bool
match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
834 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
838 if (space1
== space2
&& type1
== type2
)
839 return isl_bool_true
;
841 if (!isl_space_tuple_is_equal(space1
, type1
, space2
, type2
))
842 return isl_bool_false
;
844 if (!space1
->ids
&& !space2
->ids
)
845 return isl_bool_true
;
847 for (i
= 0; i
< n(space1
, type1
); ++i
) {
848 if (get_id(space1
, type1
, i
) != get_id(space2
, type2
, i
))
849 return isl_bool_false
;
851 return isl_bool_true
;
854 isl_bool
isl_space_match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
855 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
857 if (!space1
|| !space2
)
858 return isl_bool_error
;
860 return match(space1
, type1
, space2
, type2
);
863 static void get_ids(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
864 unsigned first
, unsigned n
, __isl_keep isl_id
**ids
)
868 for (i
= 0; i
< n
; ++i
)
869 ids
[i
] = get_id(dim
, type
, first
+ i
);
872 static __isl_give isl_space
*space_extend(__isl_take isl_space
*space
,
873 unsigned nparam
, unsigned n_in
, unsigned n_out
)
879 if (space
->nparam
== nparam
&&
880 space
->n_in
== n_in
&& space
->n_out
== n_out
)
883 isl_assert(space
->ctx
, space
->nparam
<= nparam
, goto error
);
884 isl_assert(space
->ctx
, space
->n_in
<= n_in
, goto error
);
885 isl_assert(space
->ctx
, space
->n_out
<= n_out
, goto error
);
887 space
= isl_space_cow(space
);
893 n
= nparam
+ n_in
+ n_out
;
894 if (n
< nparam
|| n
< n_in
|| n
< n_out
)
895 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
896 "overflow in total number of dimensions",
898 ids
= isl_calloc_array(space
->ctx
, isl_id
*, n
);
901 get_ids(space
, isl_dim_param
, 0, space
->nparam
, ids
);
902 get_ids(space
, isl_dim_in
, 0, space
->n_in
, ids
+ nparam
);
903 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
904 ids
+ nparam
+ n_in
);
907 space
->n_id
= nparam
+ n_in
+ n_out
;
909 space
->nparam
= nparam
;
911 space
->n_out
= n_out
;
916 isl_space_free(space
);
920 __isl_give isl_space
*isl_space_extend(__isl_take isl_space
*space
,
921 unsigned nparam
, unsigned n_in
, unsigned n_out
)
923 return space_extend(space
, nparam
, n_in
, n_out
);
926 __isl_give isl_space
*isl_space_add_dims(__isl_take isl_space
*space
,
927 enum isl_dim_type type
, unsigned n
)
929 space
= isl_space_reset(space
, type
);
934 space
= space_extend(space
,
935 space
->nparam
+ n
, space
->n_in
, space
->n_out
);
936 if (space
&& space
->nested
[0] &&
937 !(space
->nested
[0] = isl_space_add_dims(space
->nested
[0],
940 if (space
&& space
->nested
[1] &&
941 !(space
->nested
[1] = isl_space_add_dims(space
->nested
[1],
946 return space_extend(space
,
947 space
->nparam
, space
->n_in
+ n
, space
->n_out
);
949 return space_extend(space
,
950 space
->nparam
, space
->n_in
, space
->n_out
+ n
);
952 isl_die(space
->ctx
, isl_error_invalid
,
953 "cannot add dimensions of specified type", goto error
);
956 isl_space_free(space
);
960 static int valid_dim_type(enum isl_dim_type type
)
972 /* Insert "n" dimensions of type "type" at position "pos".
973 * If we are inserting parameters, then they are also inserted in
976 __isl_give isl_space
*isl_space_insert_dims(__isl_take isl_space
*dim
,
977 enum isl_dim_type type
, unsigned pos
, unsigned n
)
984 return isl_space_reset(dim
, type
);
986 if (!valid_dim_type(type
))
987 isl_die(dim
->ctx
, isl_error_invalid
,
988 "cannot insert dimensions of specified type",
991 isl_assert(dim
->ctx
, pos
<= isl_space_dim(dim
, type
), goto error
);
993 dim
= isl_space_cow(dim
);
998 enum isl_dim_type t
, o
= isl_dim_param
;
1001 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
1002 dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
);
1006 s
[isl_dim_param
- o
] = dim
->nparam
;
1007 s
[isl_dim_in
- o
] = dim
->n_in
;
1008 s
[isl_dim_out
- o
] = dim
->n_out
;
1009 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1011 get_ids(dim
, t
, 0, s
[t
- o
], ids
+ off
);
1014 get_ids(dim
, t
, 0, pos
, ids
+ off
);
1016 get_ids(dim
, t
, pos
, s
[t
- o
] - pos
, ids
+ off
);
1017 off
+= s
[t
- o
] - pos
;
1022 dim
->n_id
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
;
1025 case isl_dim_param
: dim
->nparam
+= n
; break;
1026 case isl_dim_in
: dim
->n_in
+= n
; break;
1027 case isl_dim_out
: dim
->n_out
+= n
; break;
1030 dim
= isl_space_reset(dim
, type
);
1032 if (type
== isl_dim_param
) {
1033 if (dim
&& dim
->nested
[0] &&
1034 !(dim
->nested
[0] = isl_space_insert_dims(dim
->nested
[0],
1035 isl_dim_param
, pos
, n
)))
1037 if (dim
&& dim
->nested
[1] &&
1038 !(dim
->nested
[1] = isl_space_insert_dims(dim
->nested
[1],
1039 isl_dim_param
, pos
, n
)))
1045 isl_space_free(dim
);
1049 __isl_give isl_space
*isl_space_move_dims(__isl_take isl_space
*dim
,
1050 enum isl_dim_type dst_type
, unsigned dst_pos
,
1051 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1058 dim
= isl_space_reset(dim
, src_type
);
1059 return isl_space_reset(dim
, dst_type
);
1062 isl_assert(dim
->ctx
, src_pos
+ n
<= isl_space_dim(dim
, src_type
),
1065 if (dst_type
== src_type
&& dst_pos
== src_pos
)
1068 isl_assert(dim
->ctx
, dst_type
!= src_type
, goto error
);
1070 dim
= isl_space_reset(dim
, src_type
);
1071 dim
= isl_space_reset(dim
, dst_type
);
1073 dim
= isl_space_cow(dim
);
1079 enum isl_dim_type t
, o
= isl_dim_param
;
1082 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
1083 dim
->nparam
+ dim
->n_in
+ dim
->n_out
);
1087 s
[isl_dim_param
- o
] = dim
->nparam
;
1088 s
[isl_dim_in
- o
] = dim
->n_in
;
1089 s
[isl_dim_out
- o
] = dim
->n_out
;
1090 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1091 if (t
== dst_type
) {
1092 get_ids(dim
, t
, 0, dst_pos
, ids
+ off
);
1094 get_ids(dim
, src_type
, src_pos
, n
, ids
+ off
);
1096 get_ids(dim
, t
, dst_pos
, s
[t
- o
] - dst_pos
,
1098 off
+= s
[t
- o
] - dst_pos
;
1099 } else if (t
== src_type
) {
1100 get_ids(dim
, t
, 0, src_pos
, ids
+ off
);
1102 get_ids(dim
, t
, src_pos
+ n
,
1103 s
[t
- o
] - src_pos
- n
, ids
+ off
);
1104 off
+= s
[t
- o
] - src_pos
- n
;
1106 get_ids(dim
, t
, 0, s
[t
- o
], ids
+ off
);
1112 dim
->n_id
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
1116 case isl_dim_param
: dim
->nparam
+= n
; break;
1117 case isl_dim_in
: dim
->n_in
+= n
; break;
1118 case isl_dim_out
: dim
->n_out
+= n
; break;
1123 case isl_dim_param
: dim
->nparam
-= n
; break;
1124 case isl_dim_in
: dim
->n_in
-= n
; break;
1125 case isl_dim_out
: dim
->n_out
-= n
; break;
1129 if (dst_type
!= isl_dim_param
&& src_type
!= isl_dim_param
)
1132 for (i
= 0; i
< 2; ++i
) {
1133 if (!dim
->nested
[i
])
1135 dim
->nested
[i
] = isl_space_replace(dim
->nested
[i
],
1136 isl_dim_param
, dim
);
1137 if (!dim
->nested
[i
])
1143 isl_space_free(dim
);
1147 __isl_give isl_space
*isl_space_join(__isl_take isl_space
*left
,
1148 __isl_take isl_space
*right
)
1152 if (!left
|| !right
)
1155 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
1157 isl_assert(left
->ctx
,
1158 isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_in
),
1161 dim
= isl_space_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
1165 dim
= copy_ids(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
1166 dim
= copy_ids(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
1167 dim
= copy_ids(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
1169 if (dim
&& left
->tuple_id
[0] &&
1170 !(dim
->tuple_id
[0] = isl_id_copy(left
->tuple_id
[0])))
1172 if (dim
&& right
->tuple_id
[1] &&
1173 !(dim
->tuple_id
[1] = isl_id_copy(right
->tuple_id
[1])))
1175 if (dim
&& left
->nested
[0] &&
1176 !(dim
->nested
[0] = isl_space_copy(left
->nested
[0])))
1178 if (dim
&& right
->nested
[1] &&
1179 !(dim
->nested
[1] = isl_space_copy(right
->nested
[1])))
1182 isl_space_free(left
);
1183 isl_space_free(right
);
1187 isl_space_free(left
);
1188 isl_space_free(right
);
1192 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1193 * { [A -> B] -> [C -> D] }.
1194 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1196 __isl_give isl_space
*isl_space_product(__isl_take isl_space
*left
,
1197 __isl_take isl_space
*right
)
1199 isl_space
*dom1
, *dom2
, *nest1
, *nest2
;
1202 if (!left
|| !right
)
1205 is_set
= isl_space_is_set(left
);
1206 if (is_set
!= isl_space_is_set(right
))
1207 isl_die(isl_space_get_ctx(left
), isl_error_invalid
,
1208 "expecting either two set spaces or two map spaces",
1211 return isl_space_range_product(left
, right
);
1213 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
1216 dom1
= isl_space_domain(isl_space_copy(left
));
1217 dom2
= isl_space_domain(isl_space_copy(right
));
1218 nest1
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1220 dom1
= isl_space_range(left
);
1221 dom2
= isl_space_range(right
);
1222 nest2
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1224 return isl_space_join(isl_space_reverse(nest1
), nest2
);
1226 isl_space_free(left
);
1227 isl_space_free(right
);
1231 /* Given two spaces { A -> C } and { B -> C }, construct the space
1234 __isl_give isl_space
*isl_space_domain_product(__isl_take isl_space
*left
,
1235 __isl_take isl_space
*right
)
1237 isl_space
*ran
, *dom1
, *dom2
, *nest
;
1239 if (!left
|| !right
)
1242 if (!match(left
, isl_dim_param
, right
, isl_dim_param
))
1243 isl_die(left
->ctx
, isl_error_invalid
,
1244 "parameters need to match", goto error
);
1245 if (!isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_out
))
1246 isl_die(left
->ctx
, isl_error_invalid
,
1247 "ranges need to match", goto error
);
1249 ran
= isl_space_range(isl_space_copy(left
));
1251 dom1
= isl_space_domain(left
);
1252 dom2
= isl_space_domain(right
);
1253 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1255 return isl_space_join(isl_space_reverse(nest
), ran
);
1257 isl_space_free(left
);
1258 isl_space_free(right
);
1262 __isl_give isl_space
*isl_space_range_product(__isl_take isl_space
*left
,
1263 __isl_take isl_space
*right
)
1265 isl_space
*dom
, *ran1
, *ran2
, *nest
;
1267 if (!left
|| !right
)
1270 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
1272 if (!isl_space_tuple_is_equal(left
, isl_dim_in
, right
, isl_dim_in
))
1273 isl_die(left
->ctx
, isl_error_invalid
,
1274 "domains need to match", goto error
);
1276 dom
= isl_space_domain(isl_space_copy(left
));
1278 ran1
= isl_space_range(left
);
1279 ran2
= isl_space_range(right
);
1280 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(ran1
), ran2
));
1282 return isl_space_join(isl_space_reverse(dom
), nest
);
1284 isl_space_free(left
);
1285 isl_space_free(right
);
1289 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1291 __isl_give isl_space
*isl_space_factor_domain(__isl_take isl_space
*space
)
1293 space
= isl_space_domain_factor_domain(space
);
1294 space
= isl_space_range_factor_domain(space
);
1298 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1300 __isl_give isl_space
*isl_space_domain_factor_domain(
1301 __isl_take isl_space
*space
)
1308 if (!isl_space_domain_is_wrapping(space
))
1309 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1310 "domain not a product", return isl_space_free(space
));
1312 nested
= space
->nested
[0];
1313 domain
= isl_space_copy(space
);
1314 domain
= isl_space_drop_dims(domain
, isl_dim_in
,
1315 nested
->n_in
, nested
->n_out
);
1317 return isl_space_free(space
);
1318 if (nested
->tuple_id
[0]) {
1319 domain
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[0]);
1320 if (!domain
->tuple_id
[0])
1323 if (nested
->nested
[0]) {
1324 domain
->nested
[0] = isl_space_copy(nested
->nested
[0]);
1325 if (!domain
->nested
[0])
1329 isl_space_free(space
);
1332 isl_space_free(space
);
1333 isl_space_free(domain
);
1337 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1339 __isl_give isl_space
*isl_space_domain_factor_range(
1340 __isl_take isl_space
*space
)
1347 if (!isl_space_domain_is_wrapping(space
))
1348 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1349 "domain not a product", return isl_space_free(space
));
1351 nested
= space
->nested
[0];
1352 range
= isl_space_copy(space
);
1353 range
= isl_space_drop_dims(range
, isl_dim_in
, 0, nested
->n_in
);
1355 return isl_space_free(space
);
1356 if (nested
->tuple_id
[1]) {
1357 range
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[1]);
1358 if (!range
->tuple_id
[0])
1361 if (nested
->nested
[1]) {
1362 range
->nested
[0] = isl_space_copy(nested
->nested
[1]);
1363 if (!range
->nested
[0])
1367 isl_space_free(space
);
1370 isl_space_free(space
);
1371 isl_space_free(range
);
1375 /* Given a space of the form A -> [B -> C], return the space A -> B.
1377 __isl_give isl_space
*isl_space_range_factor_domain(
1378 __isl_take isl_space
*space
)
1385 if (!isl_space_range_is_wrapping(space
))
1386 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1387 "range not a product", return isl_space_free(space
));
1389 nested
= space
->nested
[1];
1390 domain
= isl_space_copy(space
);
1391 domain
= isl_space_drop_dims(domain
, isl_dim_out
,
1392 nested
->n_in
, nested
->n_out
);
1394 return isl_space_free(space
);
1395 if (nested
->tuple_id
[0]) {
1396 domain
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[0]);
1397 if (!domain
->tuple_id
[1])
1400 if (nested
->nested
[0]) {
1401 domain
->nested
[1] = isl_space_copy(nested
->nested
[0]);
1402 if (!domain
->nested
[1])
1406 isl_space_free(space
);
1409 isl_space_free(space
);
1410 isl_space_free(domain
);
1414 /* Internal function that selects the range of the map that is
1415 * embedded in either a set space or the range of a map space.
1416 * In particular, given a space of the form [A -> B], return the space B.
1417 * Given a space of the form A -> [B -> C], return the space A -> C.
1419 static __isl_give isl_space
*range_factor_range(__isl_take isl_space
*space
)
1427 nested
= space
->nested
[1];
1428 range
= isl_space_copy(space
);
1429 range
= isl_space_drop_dims(range
, isl_dim_out
, 0, nested
->n_in
);
1431 return isl_space_free(space
);
1432 if (nested
->tuple_id
[1]) {
1433 range
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[1]);
1434 if (!range
->tuple_id
[1])
1437 if (nested
->nested
[1]) {
1438 range
->nested
[1] = isl_space_copy(nested
->nested
[1]);
1439 if (!range
->nested
[1])
1443 isl_space_free(space
);
1446 isl_space_free(space
);
1447 isl_space_free(range
);
1451 /* Given a space of the form A -> [B -> C], return the space A -> C.
1453 __isl_give isl_space
*isl_space_range_factor_range(
1454 __isl_take isl_space
*space
)
1458 if (!isl_space_range_is_wrapping(space
))
1459 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1460 "range not a product", return isl_space_free(space
));
1462 return range_factor_range(space
);
1465 /* Given a space of the form [A -> B], return the space B.
1467 static __isl_give isl_space
*set_factor_range(__isl_take isl_space
*space
)
1471 if (!isl_space_is_wrapping(space
))
1472 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1473 "not a product", return isl_space_free(space
));
1475 return range_factor_range(space
);
1478 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1479 * Given a space of the form [A -> B], return the space B.
1481 __isl_give isl_space
*isl_space_factor_range(__isl_take isl_space
*space
)
1485 if (isl_space_is_set(space
))
1486 return set_factor_range(space
);
1487 space
= isl_space_domain_factor_range(space
);
1488 space
= isl_space_range_factor_range(space
);
1492 __isl_give isl_space
*isl_space_map_from_set(__isl_take isl_space
*dim
)
1495 isl_id
**ids
= NULL
;
1499 ctx
= isl_space_get_ctx(dim
);
1500 if (!isl_space_is_set(dim
))
1501 isl_die(ctx
, isl_error_invalid
, "not a set space", goto error
);
1502 dim
= isl_space_cow(dim
);
1506 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
1507 dim
->nparam
+ dim
->n_out
+ dim
->n_out
);
1510 get_ids(dim
, isl_dim_param
, 0, dim
->nparam
, ids
);
1511 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->nparam
);
1513 dim
->n_in
= dim
->n_out
;
1517 dim
->n_id
= dim
->nparam
+ dim
->n_out
+ dim
->n_out
;
1518 dim
= copy_ids(dim
, isl_dim_out
, 0, dim
, isl_dim_in
);
1520 isl_id_free(dim
->tuple_id
[0]);
1521 dim
->tuple_id
[0] = isl_id_copy(dim
->tuple_id
[1]);
1522 isl_space_free(dim
->nested
[0]);
1523 dim
->nested
[0] = isl_space_copy(dim
->nested
[1]);
1526 isl_space_free(dim
);
1530 __isl_give isl_space
*isl_space_map_from_domain_and_range(
1531 __isl_take isl_space
*domain
, __isl_take isl_space
*range
)
1533 if (!domain
|| !range
)
1535 if (!isl_space_is_set(domain
))
1536 isl_die(isl_space_get_ctx(domain
), isl_error_invalid
,
1537 "domain is not a set space", goto error
);
1538 if (!isl_space_is_set(range
))
1539 isl_die(isl_space_get_ctx(range
), isl_error_invalid
,
1540 "range is not a set space", goto error
);
1541 return isl_space_join(isl_space_reverse(domain
), range
);
1543 isl_space_free(domain
);
1544 isl_space_free(range
);
1548 static __isl_give isl_space
*set_ids(__isl_take isl_space
*dim
,
1549 enum isl_dim_type type
,
1550 unsigned first
, unsigned n
, __isl_take isl_id
**ids
)
1554 for (i
= 0; i
< n
; ++i
)
1555 dim
= set_id(dim
, type
, first
+ i
, ids
[i
]);
1560 __isl_give isl_space
*isl_space_reverse(__isl_take isl_space
*dim
)
1564 isl_id
**ids
= NULL
;
1569 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
1572 dim
= isl_space_cow(dim
);
1576 id
= dim
->tuple_id
[0];
1577 dim
->tuple_id
[0] = dim
->tuple_id
[1];
1578 dim
->tuple_id
[1] = id
;
1580 nested
= dim
->nested
[0];
1581 dim
->nested
[0] = dim
->nested
[1];
1582 dim
->nested
[1] = nested
;
1585 int n_id
= dim
->n_in
+ dim
->n_out
;
1586 ids
= isl_alloc_array(dim
->ctx
, isl_id
*, n_id
);
1589 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
);
1590 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->n_in
);
1594 dim
->n_in
= dim
->n_out
;
1598 dim
= set_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
);
1599 dim
= set_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
+ dim
->n_out
);
1606 isl_space_free(dim
);
1610 __isl_give isl_space
*isl_space_drop_dims(__isl_take isl_space
*dim
,
1611 enum isl_dim_type type
, unsigned first
, unsigned num
)
1619 return isl_space_reset(dim
, type
);
1621 if (!valid_dim_type(type
))
1622 isl_die(dim
->ctx
, isl_error_invalid
,
1623 "cannot drop dimensions of specified type", goto error
);
1625 if (first
+ num
> n(dim
, type
) || first
+ num
< first
)
1626 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1627 "index out of bounds", return isl_space_free(dim
));
1628 dim
= isl_space_cow(dim
);
1632 dim
= extend_ids(dim
);
1635 for (i
= 0; i
< num
; ++i
)
1636 isl_id_free(get_id(dim
, type
, first
+ i
));
1637 for (i
= first
+num
; i
< n(dim
, type
); ++i
)
1638 set_id(dim
, type
, i
- num
, get_id(dim
, type
, i
));
1641 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
,
1642 dim
->ids
+ offset(dim
, isl_dim_in
) - num
);
1644 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
,
1645 dim
->ids
+ offset(dim
, isl_dim_out
) - num
);
1652 case isl_dim_param
: dim
->nparam
-= num
; break;
1653 case isl_dim_in
: dim
->n_in
-= num
; break;
1654 case isl_dim_out
: dim
->n_out
-= num
; break;
1657 dim
= isl_space_reset(dim
, type
);
1658 if (type
== isl_dim_param
) {
1659 if (dim
&& dim
->nested
[0] &&
1660 !(dim
->nested
[0] = isl_space_drop_dims(dim
->nested
[0],
1661 isl_dim_param
, first
, num
)))
1663 if (dim
&& dim
->nested
[1] &&
1664 !(dim
->nested
[1] = isl_space_drop_dims(dim
->nested
[1],
1665 isl_dim_param
, first
, num
)))
1670 isl_space_free(dim
);
1674 __isl_give isl_space
*isl_space_drop_inputs(__isl_take isl_space
*dim
,
1675 unsigned first
, unsigned n
)
1679 return isl_space_drop_dims(dim
, isl_dim_in
, first
, n
);
1682 __isl_give isl_space
*isl_space_drop_outputs(__isl_take isl_space
*dim
,
1683 unsigned first
, unsigned n
)
1687 return isl_space_drop_dims(dim
, isl_dim_out
, first
, n
);
1690 __isl_give isl_space
*isl_space_domain(__isl_take isl_space
*dim
)
1694 dim
= isl_space_drop_outputs(dim
, 0, dim
->n_out
);
1695 dim
= isl_space_reverse(dim
);
1696 dim
= mark_as_set(dim
);
1700 __isl_give isl_space
*isl_space_from_domain(__isl_take isl_space
*dim
)
1704 if (!isl_space_is_set(dim
))
1705 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1706 "not a set space", goto error
);
1707 dim
= isl_space_reverse(dim
);
1708 dim
= isl_space_reset(dim
, isl_dim_out
);
1711 isl_space_free(dim
);
1715 __isl_give isl_space
*isl_space_range(__isl_take isl_space
*dim
)
1719 dim
= isl_space_drop_inputs(dim
, 0, dim
->n_in
);
1720 dim
= mark_as_set(dim
);
1724 __isl_give isl_space
*isl_space_from_range(__isl_take isl_space
*dim
)
1728 if (!isl_space_is_set(dim
))
1729 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1730 "not a set space", goto error
);
1731 return isl_space_reset(dim
, isl_dim_in
);
1733 isl_space_free(dim
);
1737 /* Given a map space A -> B, return the map space [A -> B] -> A.
1739 __isl_give isl_space
*isl_space_domain_map(__isl_take isl_space
*space
)
1743 domain
= isl_space_from_range(isl_space_domain(isl_space_copy(space
)));
1744 space
= isl_space_from_domain(isl_space_wrap(space
));
1745 space
= isl_space_join(space
, domain
);
1750 /* Given a map space A -> B, return the map space [A -> B] -> B.
1752 __isl_give isl_space
*isl_space_range_map(__isl_take isl_space
*space
)
1756 range
= isl_space_from_range(isl_space_range(isl_space_copy(space
)));
1757 space
= isl_space_from_domain(isl_space_wrap(space
));
1758 space
= isl_space_join(space
, range
);
1763 __isl_give isl_space
*isl_space_params(__isl_take isl_space
*space
)
1765 if (isl_space_is_params(space
))
1767 space
= isl_space_drop_dims(space
,
1768 isl_dim_in
, 0, isl_space_dim(space
, isl_dim_in
));
1769 space
= isl_space_drop_dims(space
,
1770 isl_dim_out
, 0, isl_space_dim(space
, isl_dim_out
));
1771 space
= mark_as_params(space
);
1775 __isl_give isl_space
*isl_space_set_from_params(__isl_take isl_space
*space
)
1779 if (!isl_space_is_params(space
))
1780 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1781 "not a parameter space", goto error
);
1782 return isl_space_reset(space
, isl_dim_set
);
1784 isl_space_free(space
);
1788 __isl_give isl_space
*isl_space_underlying(__isl_take isl_space
*dim
,
1796 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_id
== 0)
1797 return isl_space_reset(isl_space_reset(dim
, isl_dim_in
), isl_dim_out
);
1798 dim
= isl_space_cow(dim
);
1801 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
1805 for (i
= 0; i
< dim
->n_id
; ++i
)
1806 isl_id_free(get_id(dim
, isl_dim_out
, i
));
1808 dim
= isl_space_reset(dim
, isl_dim_in
);
1809 dim
= isl_space_reset(dim
, isl_dim_out
);
1814 /* Are the two spaces the same, including positions and names of parameters?
1816 isl_bool
isl_space_is_equal(__isl_keep isl_space
*dim1
,
1817 __isl_keep isl_space
*dim2
)
1820 return isl_bool_error
;
1822 return isl_bool_true
;
1823 return match(dim1
, isl_dim_param
, dim2
, isl_dim_param
) &&
1824 isl_space_tuple_is_equal(dim1
, isl_dim_in
, dim2
, isl_dim_in
) &&
1825 isl_space_tuple_is_equal(dim1
, isl_dim_out
, dim2
, isl_dim_out
);
1828 /* Is space1 equal to the domain of space2?
1830 * In the internal version we also allow space2 to be the space of a set,
1831 * provided space1 is a parameter space.
1833 isl_bool
isl_space_is_domain_internal(__isl_keep isl_space
*space1
,
1834 __isl_keep isl_space
*space2
)
1836 if (!space1
|| !space2
)
1837 return isl_bool_error
;
1838 if (!isl_space_is_set(space1
))
1839 return isl_bool_false
;
1840 return match(space1
, isl_dim_param
, space2
, isl_dim_param
) &&
1841 isl_space_tuple_is_equal(space1
, isl_dim_set
,
1842 space2
, isl_dim_in
);
1845 /* Is space1 equal to the domain of space2?
1847 isl_bool
isl_space_is_domain(__isl_keep isl_space
*space1
,
1848 __isl_keep isl_space
*space2
)
1851 return isl_bool_error
;
1852 if (!isl_space_is_map(space2
))
1853 return isl_bool_false
;
1854 return isl_space_is_domain_internal(space1
, space2
);
1857 /* Is space1 equal to the range of space2?
1859 * In the internal version, space2 is allowed to be the space of a set,
1860 * in which case it should be equal to space1.
1862 isl_bool
isl_space_is_range_internal(__isl_keep isl_space
*space1
,
1863 __isl_keep isl_space
*space2
)
1865 if (!space1
|| !space2
)
1866 return isl_bool_error
;
1867 if (!isl_space_is_set(space1
))
1868 return isl_bool_false
;
1869 return match(space1
, isl_dim_param
, space2
, isl_dim_param
) &&
1870 isl_space_tuple_is_equal(space1
, isl_dim_set
,
1871 space2
, isl_dim_out
);
1874 /* Is space1 equal to the range of space2?
1876 isl_bool
isl_space_is_range(__isl_keep isl_space
*space1
,
1877 __isl_keep isl_space
*space2
)
1880 return isl_bool_error
;
1881 if (!isl_space_is_map(space2
))
1882 return isl_bool_false
;
1883 return isl_space_is_range_internal(space1
, space2
);
1886 /* Update "hash" by hashing in "space".
1887 * Changes in this function should be reflected in isl_hash_space_domain.
1889 static uint32_t isl_hash_space(uint32_t hash
, __isl_keep isl_space
*space
)
1897 isl_hash_byte(hash
, space
->nparam
% 256);
1898 isl_hash_byte(hash
, space
->n_in
% 256);
1899 isl_hash_byte(hash
, space
->n_out
% 256);
1901 for (i
= 0; i
< space
->nparam
; ++i
) {
1902 id
= get_id(space
, isl_dim_param
, i
);
1903 hash
= isl_hash_id(hash
, id
);
1906 id
= tuple_id(space
, isl_dim_in
);
1907 hash
= isl_hash_id(hash
, id
);
1908 id
= tuple_id(space
, isl_dim_out
);
1909 hash
= isl_hash_id(hash
, id
);
1911 hash
= isl_hash_space(hash
, space
->nested
[0]);
1912 hash
= isl_hash_space(hash
, space
->nested
[1]);
1917 /* Update "hash" by hashing in the domain of "space".
1918 * The result of this function is equal to the result of applying
1919 * isl_hash_space to the domain of "space".
1921 static uint32_t isl_hash_space_domain(uint32_t hash
,
1922 __isl_keep isl_space
*space
)
1930 isl_hash_byte(hash
, space
->nparam
% 256);
1931 isl_hash_byte(hash
, 0);
1932 isl_hash_byte(hash
, space
->n_in
% 256);
1934 for (i
= 0; i
< space
->nparam
; ++i
) {
1935 id
= get_id(space
, isl_dim_param
, i
);
1936 hash
= isl_hash_id(hash
, id
);
1939 hash
= isl_hash_id(hash
, &isl_id_none
);
1940 id
= tuple_id(space
, isl_dim_in
);
1941 hash
= isl_hash_id(hash
, id
);
1943 hash
= isl_hash_space(hash
, space
->nested
[0]);
1948 uint32_t isl_space_get_hash(__isl_keep isl_space
*dim
)
1955 hash
= isl_hash_init();
1956 hash
= isl_hash_space(hash
, dim
);
1961 /* Return the hash value of the domain of "space".
1962 * That is, isl_space_get_domain_hash(space) is equal to
1963 * isl_space_get_hash(isl_space_domain(space)).
1965 uint32_t isl_space_get_domain_hash(__isl_keep isl_space
*space
)
1972 hash
= isl_hash_init();
1973 hash
= isl_hash_space_domain(hash
, space
);
1978 isl_bool
isl_space_is_wrapping(__isl_keep isl_space
*dim
)
1981 return isl_bool_error
;
1983 if (!isl_space_is_set(dim
))
1984 return isl_bool_false
;
1986 return dim
->nested
[1] != NULL
;
1989 /* Is "space" the space of a map where the domain is a wrapped map space?
1991 isl_bool
isl_space_domain_is_wrapping(__isl_keep isl_space
*space
)
1994 return isl_bool_error
;
1996 if (isl_space_is_set(space
))
1997 return isl_bool_false
;
1999 return space
->nested
[0] != NULL
;
2002 /* Is "space" the space of a map where the range is a wrapped map space?
2004 isl_bool
isl_space_range_is_wrapping(__isl_keep isl_space
*space
)
2007 return isl_bool_error
;
2009 if (isl_space_is_set(space
))
2010 return isl_bool_false
;
2012 return space
->nested
[1] != NULL
;
2015 __isl_give isl_space
*isl_space_wrap(__isl_take isl_space
*dim
)
2022 wrap
= isl_space_set_alloc(dim
->ctx
,
2023 dim
->nparam
, dim
->n_in
+ dim
->n_out
);
2025 wrap
= copy_ids(wrap
, isl_dim_param
, 0, dim
, isl_dim_param
);
2026 wrap
= copy_ids(wrap
, isl_dim_set
, 0, dim
, isl_dim_in
);
2027 wrap
= copy_ids(wrap
, isl_dim_set
, dim
->n_in
, dim
, isl_dim_out
);
2032 wrap
->nested
[1] = dim
;
2036 isl_space_free(dim
);
2040 __isl_give isl_space
*isl_space_unwrap(__isl_take isl_space
*dim
)
2047 if (!isl_space_is_wrapping(dim
))
2048 isl_die(dim
->ctx
, isl_error_invalid
, "not a wrapping space",
2051 unwrap
= isl_space_copy(dim
->nested
[1]);
2052 isl_space_free(dim
);
2056 isl_space_free(dim
);
2060 isl_bool
isl_space_is_named_or_nested(__isl_keep isl_space
*space
,
2061 enum isl_dim_type type
)
2063 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
2064 return isl_bool_false
;
2066 return isl_bool_error
;
2067 if (space
->tuple_id
[type
- isl_dim_in
])
2068 return isl_bool_true
;
2069 if (space
->nested
[type
- isl_dim_in
])
2070 return isl_bool_true
;
2071 return isl_bool_false
;
2074 isl_bool
isl_space_may_be_set(__isl_keep isl_space
*space
)
2079 return isl_bool_error
;
2080 if (isl_space_is_set(space
))
2081 return isl_bool_true
;
2082 if (isl_space_dim(space
, isl_dim_in
) != 0)
2083 return isl_bool_false
;
2084 nested
= isl_space_is_named_or_nested(space
, isl_dim_in
);
2085 if (nested
< 0 || nested
)
2086 return isl_bool_not(nested
);
2087 return isl_bool_true
;
2090 __isl_give isl_space
*isl_space_reset(__isl_take isl_space
*dim
,
2091 enum isl_dim_type type
)
2093 if (!isl_space_is_named_or_nested(dim
, type
))
2096 dim
= isl_space_cow(dim
);
2100 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
2101 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
2102 isl_space_free(dim
->nested
[type
- isl_dim_in
]);
2103 dim
->nested
[type
- isl_dim_in
] = NULL
;
2108 __isl_give isl_space
*isl_space_flatten(__isl_take isl_space
*dim
)
2112 if (!dim
->nested
[0] && !dim
->nested
[1])
2116 dim
= isl_space_reset(dim
, isl_dim_in
);
2117 if (dim
&& dim
->nested
[1])
2118 dim
= isl_space_reset(dim
, isl_dim_out
);
2123 __isl_give isl_space
*isl_space_flatten_domain(__isl_take isl_space
*dim
)
2127 if (!dim
->nested
[0])
2130 return isl_space_reset(dim
, isl_dim_in
);
2133 __isl_give isl_space
*isl_space_flatten_range(__isl_take isl_space
*dim
)
2137 if (!dim
->nested
[1])
2140 return isl_space_reset(dim
, isl_dim_out
);
2143 /* Replace the dimensions of the given type of dst by those of src.
2145 __isl_give isl_space
*isl_space_replace(__isl_take isl_space
*dst
,
2146 enum isl_dim_type type
, __isl_keep isl_space
*src
)
2148 dst
= isl_space_cow(dst
);
2153 dst
= isl_space_drop_dims(dst
, type
, 0, isl_space_dim(dst
, type
));
2154 dst
= isl_space_add_dims(dst
, type
, isl_space_dim(src
, type
));
2155 dst
= copy_ids(dst
, type
, 0, src
, type
);
2157 if (dst
&& type
== isl_dim_param
) {
2159 for (i
= 0; i
<= 1; ++i
) {
2160 if (!dst
->nested
[i
])
2162 dst
->nested
[i
] = isl_space_replace(dst
->nested
[i
],
2164 if (!dst
->nested
[i
])
2171 isl_space_free(dst
);
2175 /* Given a dimension specification "dim" of a set, create a dimension
2176 * specification for the lift of the set. In particular, the result
2177 * is of the form [dim -> local[..]], with n_local variables in the
2178 * range of the wrapped map.
2180 __isl_give isl_space
*isl_space_lift(__isl_take isl_space
*dim
, unsigned n_local
)
2182 isl_space
*local_dim
;
2187 local_dim
= isl_space_dup(dim
);
2188 local_dim
= isl_space_drop_dims(local_dim
, isl_dim_set
, 0, dim
->n_out
);
2189 local_dim
= isl_space_add_dims(local_dim
, isl_dim_set
, n_local
);
2190 local_dim
= isl_space_set_tuple_name(local_dim
, isl_dim_set
, "local");
2191 dim
= isl_space_join(isl_space_from_domain(dim
),
2192 isl_space_from_range(local_dim
));
2193 dim
= isl_space_wrap(dim
);
2194 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, "lifted");
2199 isl_bool
isl_space_can_zip(__isl_keep isl_space
*dim
)
2202 return isl_bool_error
;
2204 return dim
->nested
[0] && dim
->nested
[1];
2207 __isl_give isl_space
*isl_space_zip(__isl_take isl_space
*dim
)
2209 isl_space
*dom
, *ran
;
2210 isl_space
*dom_dom
, *dom_ran
, *ran_dom
, *ran_ran
;
2212 if (!isl_space_can_zip(dim
))
2213 isl_die(dim
->ctx
, isl_error_invalid
, "dim cannot be zipped",
2218 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(dim
)));
2219 ran
= isl_space_unwrap(isl_space_range(dim
));
2220 dom_dom
= isl_space_domain(isl_space_copy(dom
));
2221 dom_ran
= isl_space_range(dom
);
2222 ran_dom
= isl_space_domain(isl_space_copy(ran
));
2223 ran_ran
= isl_space_range(ran
);
2224 dom
= isl_space_join(isl_space_from_domain(dom_dom
),
2225 isl_space_from_range(ran_dom
));
2226 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
2227 isl_space_from_range(ran_ran
));
2228 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
2229 isl_space_from_range(isl_space_wrap(ran
)));
2231 isl_space_free(dim
);
2235 /* Can we apply isl_space_curry to "space"?
2236 * That is, does it have a nested relation in its domain?
2238 isl_bool
isl_space_can_curry(__isl_keep isl_space
*space
)
2241 return isl_bool_error
;
2243 return !!space
->nested
[0];
2246 /* Given a space (A -> B) -> C, return the corresponding space
2249 __isl_give isl_space
*isl_space_curry(__isl_take isl_space
*space
)
2251 isl_space
*dom
, *ran
;
2252 isl_space
*dom_dom
, *dom_ran
;
2257 if (!isl_space_can_curry(space
))
2258 isl_die(space
->ctx
, isl_error_invalid
,
2259 "space cannot be curried", goto error
);
2261 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(space
)));
2262 ran
= isl_space_range(space
);
2263 dom_dom
= isl_space_domain(isl_space_copy(dom
));
2264 dom_ran
= isl_space_range(dom
);
2265 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
2266 isl_space_from_range(ran
));
2267 return isl_space_join(isl_space_from_domain(dom_dom
),
2268 isl_space_from_range(isl_space_wrap(ran
)));
2270 isl_space_free(space
);
2274 /* Can isl_space_range_curry be applied to "space"?
2275 * That is, does it have a nested relation in its range,
2276 * the domain of which is itself a nested relation?
2278 isl_bool
isl_space_can_range_curry(__isl_keep isl_space
*space
)
2283 return isl_bool_error
;
2284 can
= isl_space_range_is_wrapping(space
);
2285 if (can
< 0 || !can
)
2287 return isl_space_can_curry(space
->nested
[1]);
2290 /* Given a space A -> ((B -> C) -> D), return the corresponding space
2291 * A -> (B -> (C -> D)).
2293 __isl_give isl_space
*isl_space_range_curry(__isl_take isl_space
*space
)
2298 if (!isl_space_can_range_curry(space
))
2299 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2300 "space range cannot be curried",
2301 return isl_space_free(space
));
2303 space
= isl_space_cow(space
);
2306 space
->nested
[1] = isl_space_curry(space
->nested
[1]);
2307 if (!space
->nested
[1])
2308 return isl_space_free(space
);
2313 /* Can we apply isl_space_uncurry to "space"?
2314 * That is, does it have a nested relation in its range?
2316 isl_bool
isl_space_can_uncurry(__isl_keep isl_space
*space
)
2319 return isl_bool_error
;
2321 return !!space
->nested
[1];
2324 /* Given a space A -> (B -> C), return the corresponding space
2327 __isl_give isl_space
*isl_space_uncurry(__isl_take isl_space
*space
)
2329 isl_space
*dom
, *ran
;
2330 isl_space
*ran_dom
, *ran_ran
;
2335 if (!isl_space_can_uncurry(space
))
2336 isl_die(space
->ctx
, isl_error_invalid
,
2337 "space cannot be uncurried",
2338 return isl_space_free(space
));
2340 dom
= isl_space_domain(isl_space_copy(space
));
2341 ran
= isl_space_unwrap(isl_space_range(space
));
2342 ran_dom
= isl_space_domain(isl_space_copy(ran
));
2343 ran_ran
= isl_space_range(ran
);
2344 dom
= isl_space_join(isl_space_from_domain(dom
),
2345 isl_space_from_range(ran_dom
));
2346 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
2347 isl_space_from_range(ran_ran
));
2350 isl_bool
isl_space_has_named_params(__isl_keep isl_space
*space
)
2356 return isl_bool_error
;
2357 if (space
->nparam
== 0)
2358 return isl_bool_true
;
2359 off
= isl_space_offset(space
, isl_dim_param
);
2360 if (off
+ space
->nparam
> space
->n_id
)
2361 return isl_bool_false
;
2362 for (i
= 0; i
< space
->nparam
; ++i
)
2363 if (!space
->ids
[off
+ i
])
2364 return isl_bool_false
;
2365 return isl_bool_true
;
2368 /* Check that "space" has only named parameters, reporting an error
2371 isl_stat
isl_space_check_named_params(__isl_keep isl_space
*space
)
2375 named
= isl_space_has_named_params(space
);
2377 return isl_stat_error
;
2379 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2380 "unaligned unnamed parameters", return isl_stat_error
);
2385 /* Align the initial parameters of dim1 to match the order in dim2.
2387 __isl_give isl_space
*isl_space_align_params(__isl_take isl_space
*dim1
,
2388 __isl_take isl_space
*dim2
)
2390 isl_reordering
*exp
;
2392 if (!isl_space_has_named_params(dim1
) || !isl_space_has_named_params(dim2
))
2393 isl_die(isl_space_get_ctx(dim1
), isl_error_invalid
,
2394 "parameter alignment requires named parameters",
2397 dim2
= isl_space_params(dim2
);
2398 exp
= isl_parameter_alignment_reordering(dim1
, dim2
);
2399 exp
= isl_reordering_extend_space(exp
, dim1
);
2400 isl_space_free(dim2
);
2403 dim1
= isl_space_copy(exp
->dim
);
2404 isl_reordering_free(exp
);
2407 isl_space_free(dim1
);
2408 isl_space_free(dim2
);
2412 /* Given the space of set (domain), construct a space for a map
2413 * with as domain the given space and as range the range of "model".
2415 __isl_give isl_space
*isl_space_extend_domain_with_range(
2416 __isl_take isl_space
*space
, __isl_take isl_space
*model
)
2421 space
= isl_space_from_domain(space
);
2422 space
= isl_space_add_dims(space
, isl_dim_out
,
2423 isl_space_dim(model
, isl_dim_out
));
2424 if (isl_space_has_tuple_id(model
, isl_dim_out
))
2425 space
= isl_space_set_tuple_id(space
, isl_dim_out
,
2426 isl_space_get_tuple_id(model
, isl_dim_out
));
2429 if (model
->nested
[1]) {
2430 isl_space
*nested
= isl_space_copy(model
->nested
[1]);
2431 int n_nested
, n_space
;
2432 nested
= isl_space_align_params(nested
, isl_space_copy(space
));
2433 n_nested
= isl_space_dim(nested
, isl_dim_param
);
2434 n_space
= isl_space_dim(space
, isl_dim_param
);
2435 if (n_nested
> n_space
)
2436 nested
= isl_space_drop_dims(nested
, isl_dim_param
,
2437 n_space
, n_nested
- n_space
);
2440 space
->nested
[1] = nested
;
2442 isl_space_free(model
);
2445 isl_space_free(model
);
2446 isl_space_free(space
);
2450 /* Compare the "type" dimensions of two isl_spaces.
2452 * The order is fairly arbitrary.
2454 static int isl_space_cmp_type(__isl_keep isl_space
*space1
,
2455 __isl_keep isl_space
*space2
, enum isl_dim_type type
)
2458 isl_space
*nested1
, *nested2
;
2460 if (isl_space_dim(space1
, type
) != isl_space_dim(space2
, type
))
2461 return isl_space_dim(space1
, type
) -
2462 isl_space_dim(space2
, type
);
2464 cmp
= isl_id_cmp(tuple_id(space1
, type
), tuple_id(space2
, type
));
2468 nested1
= nested(space1
, type
);
2469 nested2
= nested(space2
, type
);
2470 if (!nested1
!= !nested2
)
2471 return !nested1
- !nested2
;
2474 return isl_space_cmp(nested1
, nested2
);
2479 /* Compare two isl_spaces.
2481 * The order is fairly arbitrary.
2483 int isl_space_cmp(__isl_keep isl_space
*space1
, __isl_keep isl_space
*space2
)
2488 if (space1
== space2
)
2495 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_param
);
2498 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_in
);
2501 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_out
);
2505 if (!space1
->ids
&& !space2
->ids
)
2508 for (i
= 0; i
< n(space1
, isl_dim_param
); ++i
) {
2509 cmp
= isl_id_cmp(get_id(space1
, isl_dim_param
, i
),
2510 get_id(space2
, isl_dim_param
, i
));