2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
4 * Copyright 2013-2014 Ecole Normale Superieure
6 * Use of this software is governed by the MIT license
8 * Written by Sven Verdoolaege, K.U.Leuven, Departement
9 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
11 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
12 * and Ecole Normale Superieure, 45 rue d’Ulm, 75230 Paris, France
17 #include <isl_space_private.h>
18 #include <isl_id_private.h>
19 #include <isl_reordering.h>
21 isl_ctx
*isl_space_get_ctx(__isl_keep isl_space
*dim
)
23 return dim
? dim
->ctx
: NULL
;
26 __isl_give isl_space
*isl_space_alloc(isl_ctx
*ctx
,
27 unsigned nparam
, unsigned n_in
, unsigned n_out
)
31 dim
= isl_alloc_type(ctx
, struct isl_space
);
42 dim
->tuple_id
[0] = NULL
;
43 dim
->tuple_id
[1] = NULL
;
45 dim
->nested
[0] = NULL
;
46 dim
->nested
[1] = NULL
;
54 /* Mark the space as being that of a set, by setting the domain tuple
57 static __isl_give isl_space
*mark_as_set(__isl_take isl_space
*space
)
59 space
= isl_space_cow(space
);
62 space
= isl_space_set_tuple_id(space
, isl_dim_in
, &isl_id_none
);
66 /* Is the space that of a set?
68 isl_bool
isl_space_is_set(__isl_keep isl_space
*space
)
71 return isl_bool_error
;
72 if (space
->n_in
!= 0 || space
->nested
[0])
73 return isl_bool_false
;
74 if (space
->tuple_id
[0] != &isl_id_none
)
75 return isl_bool_false
;
79 /* Is the given space that of a map?
81 isl_bool
isl_space_is_map(__isl_keep isl_space
*space
)
84 return isl_bool_error
;
85 return space
->tuple_id
[0] != &isl_id_none
&&
86 space
->tuple_id
[1] != &isl_id_none
;
89 __isl_give isl_space
*isl_space_set_alloc(isl_ctx
*ctx
,
90 unsigned nparam
, unsigned dim
)
93 space
= isl_space_alloc(ctx
, nparam
, 0, dim
);
94 space
= mark_as_set(space
);
98 /* Mark the space as being that of a parameter domain, by setting
99 * both tuples to isl_id_none.
101 static __isl_give isl_space
*mark_as_params(isl_space
*space
)
105 space
= isl_space_set_tuple_id(space
, isl_dim_in
, &isl_id_none
);
106 space
= isl_space_set_tuple_id(space
, isl_dim_out
, &isl_id_none
);
110 /* Is the space that of a parameter domain?
112 isl_bool
isl_space_is_params(__isl_keep isl_space
*space
)
115 return isl_bool_error
;
116 if (space
->n_in
!= 0 || space
->nested
[0] ||
117 space
->n_out
!= 0 || space
->nested
[1])
118 return isl_bool_false
;
119 if (space
->tuple_id
[0] != &isl_id_none
)
120 return isl_bool_false
;
121 if (space
->tuple_id
[1] != &isl_id_none
)
122 return isl_bool_false
;
123 return isl_bool_true
;
126 /* Create a space for a parameter domain.
128 __isl_give isl_space
*isl_space_params_alloc(isl_ctx
*ctx
, unsigned nparam
)
131 space
= isl_space_alloc(ctx
, nparam
, 0, 0);
132 space
= mark_as_params(space
);
136 static unsigned global_pos(__isl_keep isl_space
*dim
,
137 enum isl_dim_type type
, unsigned pos
)
139 struct isl_ctx
*ctx
= dim
->ctx
;
143 isl_assert(ctx
, pos
< dim
->nparam
,
144 return isl_space_dim(dim
, isl_dim_all
));
147 isl_assert(ctx
, pos
< dim
->n_in
,
148 return isl_space_dim(dim
, isl_dim_all
));
149 return pos
+ dim
->nparam
;
151 isl_assert(ctx
, pos
< dim
->n_out
,
152 return isl_space_dim(dim
, isl_dim_all
));
153 return pos
+ dim
->nparam
+ dim
->n_in
;
155 isl_assert(ctx
, 0, return isl_space_dim(dim
, isl_dim_all
));
157 return isl_space_dim(dim
, isl_dim_all
);
160 /* Extend length of ids array to the total number of dimensions.
162 static __isl_give isl_space
*extend_ids(__isl_take isl_space
*dim
)
167 if (isl_space_dim(dim
, isl_dim_all
) <= dim
->n_id
)
171 dim
->ids
= isl_calloc_array(dim
->ctx
,
172 isl_id
*, isl_space_dim(dim
, isl_dim_all
));
176 ids
= isl_realloc_array(dim
->ctx
, dim
->ids
,
177 isl_id
*, isl_space_dim(dim
, isl_dim_all
));
181 for (i
= dim
->n_id
; i
< isl_space_dim(dim
, isl_dim_all
); ++i
)
185 dim
->n_id
= isl_space_dim(dim
, isl_dim_all
);
193 static __isl_give isl_space
*set_id(__isl_take isl_space
*dim
,
194 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
196 dim
= isl_space_cow(dim
);
201 pos
= global_pos(dim
, type
, pos
);
202 if (pos
== isl_space_dim(dim
, isl_dim_all
))
205 if (pos
>= dim
->n_id
) {
208 dim
= extend_ids(dim
);
222 static __isl_keep isl_id
*get_id(__isl_keep isl_space
*dim
,
223 enum isl_dim_type type
, unsigned pos
)
228 pos
= global_pos(dim
, type
, pos
);
229 if (pos
== isl_space_dim(dim
, isl_dim_all
))
231 if (pos
>= dim
->n_id
)
233 return dim
->ids
[pos
];
236 static unsigned offset(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
239 case isl_dim_param
: return 0;
240 case isl_dim_in
: return dim
->nparam
;
241 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
246 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
249 case isl_dim_param
: return dim
->nparam
;
250 case isl_dim_in
: return dim
->n_in
;
251 case isl_dim_out
: return dim
->n_out
;
252 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
257 unsigned isl_space_dim(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
264 unsigned isl_space_offset(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
268 return offset(dim
, type
);
271 static __isl_give isl_space
*copy_ids(__isl_take isl_space
*dst
,
272 enum isl_dim_type dst_type
, unsigned offset
, __isl_keep isl_space
*src
,
273 enum isl_dim_type src_type
)
281 for (i
= 0; i
< n(src
, src_type
); ++i
) {
282 id
= get_id(src
, src_type
, i
);
285 dst
= set_id(dst
, dst_type
, offset
+ i
, isl_id_copy(id
));
292 __isl_take isl_space
*isl_space_dup(__isl_keep isl_space
*dim
)
297 dup
= isl_space_alloc(dim
->ctx
, dim
->nparam
, dim
->n_in
, dim
->n_out
);
300 if (dim
->tuple_id
[0] &&
301 !(dup
->tuple_id
[0] = isl_id_copy(dim
->tuple_id
[0])))
303 if (dim
->tuple_id
[1] &&
304 !(dup
->tuple_id
[1] = isl_id_copy(dim
->tuple_id
[1])))
306 if (dim
->nested
[0] && !(dup
->nested
[0] = isl_space_copy(dim
->nested
[0])))
308 if (dim
->nested
[1] && !(dup
->nested
[1] = isl_space_copy(dim
->nested
[1])))
312 dup
= copy_ids(dup
, isl_dim_param
, 0, dim
, isl_dim_param
);
313 dup
= copy_ids(dup
, isl_dim_in
, 0, dim
, isl_dim_in
);
314 dup
= copy_ids(dup
, isl_dim_out
, 0, dim
, isl_dim_out
);
321 __isl_give isl_space
*isl_space_cow(__isl_take isl_space
*dim
)
329 return isl_space_dup(dim
);
332 __isl_give isl_space
*isl_space_copy(__isl_keep isl_space
*dim
)
341 __isl_null isl_space
*isl_space_free(__isl_take isl_space
*space
)
348 if (--space
->ref
> 0)
351 isl_id_free(space
->tuple_id
[0]);
352 isl_id_free(space
->tuple_id
[1]);
354 isl_space_free(space
->nested
[0]);
355 isl_space_free(space
->nested
[1]);
357 for (i
= 0; i
< space
->n_id
; ++i
)
358 isl_id_free(space
->ids
[i
]);
360 isl_ctx_deref(space
->ctx
);
367 /* Check if "s" is a valid dimension or tuple name.
368 * We currently only forbid names that look like a number.
370 * s is assumed to be non-NULL.
372 static int name_ok(isl_ctx
*ctx
, const char *s
)
377 dummy
= strtol(s
, &p
, 0);
379 isl_die(ctx
, isl_error_invalid
, "name looks like a number",
385 /* Is it possible for the given dimension type to have a tuple id?
387 static int space_can_have_id(__isl_keep isl_space
*space
,
388 enum isl_dim_type type
)
392 if (isl_space_is_params(space
))
393 isl_die(space
->ctx
, isl_error_invalid
,
394 "parameter spaces don't have tuple ids", return 0);
395 if (isl_space_is_set(space
) && type
!= isl_dim_set
)
396 isl_die(space
->ctx
, isl_error_invalid
,
397 "set spaces can only have a set id", return 0);
398 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
399 isl_die(space
->ctx
, isl_error_invalid
,
400 "only input, output and set tuples can have ids",
406 /* Does the tuple have an id?
408 isl_bool
isl_space_has_tuple_id(__isl_keep isl_space
*dim
,
409 enum isl_dim_type type
)
411 if (!space_can_have_id(dim
, type
))
412 return isl_bool_error
;
413 return dim
->tuple_id
[type
- isl_dim_in
] != NULL
;
416 __isl_give isl_id
*isl_space_get_tuple_id(__isl_keep isl_space
*dim
,
417 enum isl_dim_type type
)
423 has_id
= isl_space_has_tuple_id(dim
, type
);
427 isl_die(dim
->ctx
, isl_error_invalid
,
428 "tuple has no id", return NULL
);
429 return isl_id_copy(dim
->tuple_id
[type
- isl_dim_in
]);
432 __isl_give isl_space
*isl_space_set_tuple_id(__isl_take isl_space
*dim
,
433 enum isl_dim_type type
, __isl_take isl_id
*id
)
435 dim
= isl_space_cow(dim
);
438 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
439 isl_die(dim
->ctx
, isl_error_invalid
,
440 "only input, output and set tuples can have names",
443 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
444 dim
->tuple_id
[type
- isl_dim_in
] = id
;
453 __isl_give isl_space
*isl_space_reset_tuple_id(__isl_take isl_space
*dim
,
454 enum isl_dim_type type
)
456 dim
= isl_space_cow(dim
);
459 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
460 isl_die(dim
->ctx
, isl_error_invalid
,
461 "only input, output and set tuples can have names",
464 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
465 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
473 /* Set the id of the given dimension of "space" to "id".
474 * If the dimension already has an id, then it is replaced.
475 * If the dimension is a parameter, then we need to change it
476 * in the nested spaces (if any) as well.
478 __isl_give isl_space
*isl_space_set_dim_id(__isl_take isl_space
*space
,
479 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
481 space
= isl_space_cow(space
);
485 if (type
== isl_dim_param
) {
488 for (i
= 0; i
< 2; ++i
) {
489 if (!space
->nested
[i
])
492 isl_space_set_dim_id(space
->nested
[i
],
493 type
, pos
, isl_id_copy(id
));
494 if (!space
->nested
[i
])
499 isl_id_free(get_id(space
, type
, pos
));
500 return set_id(space
, type
, pos
, id
);
503 isl_space_free(space
);
507 /* Reset the id of the given dimension of "space".
508 * If the dimension already has an id, then it is removed.
509 * If the dimension is a parameter, then we need to reset it
510 * in the nested spaces (if any) as well.
512 __isl_give isl_space
*isl_space_reset_dim_id(__isl_take isl_space
*space
,
513 enum isl_dim_type type
, unsigned pos
)
515 space
= isl_space_cow(space
);
519 if (type
== isl_dim_param
) {
522 for (i
= 0; i
< 2; ++i
) {
523 if (!space
->nested
[i
])
526 isl_space_reset_dim_id(space
->nested
[i
],
528 if (!space
->nested
[i
])
533 isl_id_free(get_id(space
, type
, pos
));
534 return set_id(space
, type
, pos
, NULL
);
536 isl_space_free(space
);
540 isl_bool
isl_space_has_dim_id(__isl_keep isl_space
*dim
,
541 enum isl_dim_type type
, unsigned pos
)
544 return isl_bool_error
;
545 return get_id(dim
, type
, pos
) != NULL
;
548 __isl_give isl_id
*isl_space_get_dim_id(__isl_keep isl_space
*dim
,
549 enum isl_dim_type type
, unsigned pos
)
553 if (!get_id(dim
, type
, pos
))
554 isl_die(dim
->ctx
, isl_error_invalid
,
555 "dim has no id", return NULL
);
556 return isl_id_copy(get_id(dim
, type
, pos
));
559 __isl_give isl_space
*isl_space_set_tuple_name(__isl_take isl_space
*dim
,
560 enum isl_dim_type type
, const char *s
)
568 return isl_space_reset_tuple_id(dim
, type
);
570 if (!name_ok(dim
->ctx
, s
))
573 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
574 return isl_space_set_tuple_id(dim
, type
, id
);
580 /* Does the tuple have a name?
582 isl_bool
isl_space_has_tuple_name(__isl_keep isl_space
*space
,
583 enum isl_dim_type type
)
587 if (!space_can_have_id(space
, type
))
588 return isl_bool_error
;
589 id
= space
->tuple_id
[type
- isl_dim_in
];
590 return id
&& id
->name
;
593 const char *isl_space_get_tuple_name(__isl_keep isl_space
*dim
,
594 enum isl_dim_type type
)
599 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
601 id
= dim
->tuple_id
[type
- isl_dim_in
];
602 return id
? id
->name
: NULL
;
605 __isl_give isl_space
*isl_space_set_dim_name(__isl_take isl_space
*dim
,
606 enum isl_dim_type type
, unsigned pos
,
614 return isl_space_reset_dim_id(dim
, type
, pos
);
615 if (!name_ok(dim
->ctx
, s
))
617 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
618 return isl_space_set_dim_id(dim
, type
, pos
, id
);
624 /* Does the given dimension have a name?
626 isl_bool
isl_space_has_dim_name(__isl_keep isl_space
*space
,
627 enum isl_dim_type type
, unsigned pos
)
632 return isl_bool_error
;
633 id
= get_id(space
, type
, pos
);
634 return id
&& id
->name
;
637 __isl_keep
const char *isl_space_get_dim_name(__isl_keep isl_space
*dim
,
638 enum isl_dim_type type
, unsigned pos
)
640 isl_id
*id
= get_id(dim
, type
, pos
);
641 return id
? id
->name
: NULL
;
644 int isl_space_find_dim_by_id(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
645 __isl_keep isl_id
*id
)
654 offset
= isl_space_offset(dim
, type
);
655 n
= isl_space_dim(dim
, type
);
656 for (i
= 0; i
< n
&& offset
+ i
< dim
->n_id
; ++i
)
657 if (dim
->ids
[offset
+ i
] == id
)
663 int isl_space_find_dim_by_name(__isl_keep isl_space
*space
,
664 enum isl_dim_type type
, const char *name
)
673 offset
= isl_space_offset(space
, type
);
674 n
= isl_space_dim(space
, type
);
675 for (i
= 0; i
< n
&& offset
+ i
< space
->n_id
; ++i
)
676 if (space
->ids
[offset
+ i
]->name
&&
677 !strcmp(space
->ids
[offset
+ i
]->name
, name
))
683 /* Reset the user pointer on all identifiers of parameters and tuples
686 __isl_give isl_space
*isl_space_reset_user(__isl_take isl_space
*space
)
696 ctx
= isl_space_get_ctx(space
);
698 for (i
= 0; i
< space
->nparam
&& i
< space
->n_id
; ++i
) {
699 if (!isl_id_get_user(space
->ids
[i
]))
701 space
= isl_space_cow(space
);
704 name
= isl_id_get_name(space
->ids
[i
]);
705 id
= isl_id_alloc(ctx
, name
, NULL
);
706 isl_id_free(space
->ids
[i
]);
709 return isl_space_free(space
);
712 for (i
= 0; i
< 2; ++i
) {
713 if (!space
->tuple_id
[i
])
715 if (!isl_id_get_user(space
->tuple_id
[i
]))
717 space
= isl_space_cow(space
);
720 name
= isl_id_get_name(space
->tuple_id
[i
]);
721 id
= isl_id_alloc(ctx
, name
, NULL
);
722 isl_id_free(space
->tuple_id
[i
]);
723 space
->tuple_id
[i
] = id
;
725 return isl_space_free(space
);
728 for (i
= 0; i
< 2; ++i
) {
729 if (!space
->nested
[i
])
731 space
= isl_space_cow(space
);
734 space
->nested
[i
] = isl_space_reset_user(space
->nested
[i
]);
735 if (!space
->nested
[i
])
736 return isl_space_free(space
);
742 static __isl_keep isl_id
*tuple_id(__isl_keep isl_space
*dim
,
743 enum isl_dim_type type
)
747 if (type
== isl_dim_in
)
748 return dim
->tuple_id
[0];
749 if (type
== isl_dim_out
)
750 return dim
->tuple_id
[1];
754 static __isl_keep isl_space
*nested(__isl_keep isl_space
*dim
,
755 enum isl_dim_type type
)
759 if (type
== isl_dim_in
)
760 return dim
->nested
[0];
761 if (type
== isl_dim_out
)
762 return dim
->nested
[1];
766 /* Are the two spaces the same, apart from positions and names of parameters?
768 static int isl_space_has_equal_tuples(__isl_keep isl_space
*space1
,
769 __isl_keep isl_space
*space2
)
771 if (!space1
|| !space2
)
773 if (space1
== space2
)
775 return isl_space_tuple_is_equal(space1
, isl_dim_in
,
776 space2
, isl_dim_in
) &&
777 isl_space_tuple_is_equal(space1
, isl_dim_out
,
778 space2
, isl_dim_out
);
781 /* Check if the tuple of type "type1" of "space1" is the same as
782 * the tuple of type "type2" of "space2".
784 * That is, check if the tuples have the same identifier, the same dimension
785 * and the same internal structure.
786 * The identifiers of the dimensions inside the tuples do not affect the result.
788 * Note that this function only checks the tuples themselves.
789 * If nested tuples are involved, then we need to be careful not
790 * to have result affected by possibly differing parameters
791 * in those nested tuples.
793 isl_bool
isl_space_tuple_is_equal(__isl_keep isl_space
*space1
,
794 enum isl_dim_type type1
, __isl_keep isl_space
*space2
,
795 enum isl_dim_type type2
)
798 isl_space
*nested1
, *nested2
;
800 if (!space1
|| !space2
)
801 return isl_bool_error
;
803 if (space1
== space2
&& type1
== type2
)
804 return isl_bool_true
;
806 if (n(space1
, type1
) != n(space2
, type2
))
807 return isl_bool_false
;
808 id1
= tuple_id(space1
, type1
);
809 id2
= tuple_id(space2
, type2
);
811 return isl_bool_false
;
812 if (id1
&& id1
!= id2
)
813 return isl_bool_false
;
814 nested1
= nested(space1
, type1
);
815 nested2
= nested(space2
, type2
);
816 if (!nested1
^ !nested2
)
817 return isl_bool_false
;
818 if (nested1
&& !isl_space_has_equal_tuples(nested1
, nested2
))
819 return isl_bool_false
;
820 return isl_bool_true
;
823 /* This is the old, undocumented, name for isl_space_tuple_is_equal.
824 * It will be removed at some point.
826 int isl_space_tuple_match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
827 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
829 return isl_space_tuple_is_equal(space1
, type1
, space2
, type2
);
832 static int match(__isl_keep isl_space
*dim1
, enum isl_dim_type dim1_type
,
833 __isl_keep isl_space
*dim2
, enum isl_dim_type dim2_type
)
837 if (dim1
== dim2
&& dim1_type
== dim2_type
)
840 if (!isl_space_tuple_is_equal(dim1
, dim1_type
, dim2
, dim2_type
))
843 if (!dim1
->ids
&& !dim2
->ids
)
846 for (i
= 0; i
< n(dim1
, dim1_type
); ++i
) {
847 if (get_id(dim1
, dim1_type
, i
) != get_id(dim2
, dim2_type
, i
))
853 int isl_space_match(__isl_keep isl_space
*dim1
, enum isl_dim_type dim1_type
,
854 __isl_keep isl_space
*dim2
, enum isl_dim_type dim2_type
)
859 return match(dim1
, dim1_type
, dim2
, dim2_type
);
862 static void get_ids(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
863 unsigned first
, unsigned n
, __isl_keep isl_id
**ids
)
867 for (i
= 0; i
< n
; ++i
)
868 ids
[i
] = get_id(dim
, type
, first
+ i
);
871 __isl_give isl_space
*isl_space_extend(__isl_take isl_space
*space
,
872 unsigned nparam
, unsigned n_in
, unsigned n_out
)
878 if (space
->nparam
== nparam
&&
879 space
->n_in
== n_in
&& space
->n_out
== n_out
)
882 isl_assert(space
->ctx
, space
->nparam
<= nparam
, goto error
);
883 isl_assert(space
->ctx
, space
->n_in
<= n_in
, goto error
);
884 isl_assert(space
->ctx
, space
->n_out
<= n_out
, goto error
);
886 space
= isl_space_cow(space
);
892 n
= nparam
+ n_in
+ n_out
;
893 if (n
< nparam
|| n
< n_in
|| n
< n_out
)
894 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
895 "overflow in total number of dimensions",
897 ids
= isl_calloc_array(space
->ctx
, isl_id
*, n
);
900 get_ids(space
, isl_dim_param
, 0, space
->nparam
, ids
);
901 get_ids(space
, isl_dim_in
, 0, space
->n_in
, ids
+ nparam
);
902 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
903 ids
+ nparam
+ n_in
);
906 space
->n_id
= nparam
+ n_in
+ n_out
;
908 space
->nparam
= nparam
;
910 space
->n_out
= n_out
;
915 isl_space_free(space
);
919 __isl_give isl_space
*isl_space_add_dims(__isl_take isl_space
*dim
,
920 enum isl_dim_type type
, unsigned n
)
922 dim
= isl_space_reset(dim
, type
);
927 dim
= isl_space_extend(dim
,
928 dim
->nparam
+ n
, dim
->n_in
, dim
->n_out
);
929 if (dim
&& dim
->nested
[0] &&
930 !(dim
->nested
[0] = isl_space_add_dims(dim
->nested
[0],
933 if (dim
&& dim
->nested
[1] &&
934 !(dim
->nested
[1] = isl_space_add_dims(dim
->nested
[1],
939 return isl_space_extend(dim
,
940 dim
->nparam
, dim
->n_in
+ n
, dim
->n_out
);
942 return isl_space_extend(dim
,
943 dim
->nparam
, dim
->n_in
, dim
->n_out
+ n
);
945 isl_die(dim
->ctx
, isl_error_invalid
,
946 "cannot add dimensions of specified type", goto error
);
953 static int valid_dim_type(enum isl_dim_type type
)
965 /* Insert "n" dimensions of type "type" at position "pos".
966 * If we are inserting parameters, then they are also inserted in
969 __isl_give isl_space
*isl_space_insert_dims(__isl_take isl_space
*dim
,
970 enum isl_dim_type type
, unsigned pos
, unsigned n
)
977 return isl_space_reset(dim
, type
);
979 if (!valid_dim_type(type
))
980 isl_die(dim
->ctx
, isl_error_invalid
,
981 "cannot insert dimensions of specified type",
984 isl_assert(dim
->ctx
, pos
<= isl_space_dim(dim
, type
), goto error
);
986 dim
= isl_space_cow(dim
);
991 enum isl_dim_type t
, o
= isl_dim_param
;
994 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
995 dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
);
999 s
[isl_dim_param
- o
] = dim
->nparam
;
1000 s
[isl_dim_in
- o
] = dim
->n_in
;
1001 s
[isl_dim_out
- o
] = dim
->n_out
;
1002 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1004 get_ids(dim
, t
, 0, s
[t
- o
], ids
+ off
);
1007 get_ids(dim
, t
, 0, pos
, ids
+ off
);
1009 get_ids(dim
, t
, pos
, s
[t
- o
] - pos
, ids
+ off
);
1010 off
+= s
[t
- o
] - pos
;
1015 dim
->n_id
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
;
1018 case isl_dim_param
: dim
->nparam
+= n
; break;
1019 case isl_dim_in
: dim
->n_in
+= n
; break;
1020 case isl_dim_out
: dim
->n_out
+= n
; break;
1023 dim
= isl_space_reset(dim
, type
);
1025 if (type
== isl_dim_param
) {
1026 if (dim
&& dim
->nested
[0] &&
1027 !(dim
->nested
[0] = isl_space_insert_dims(dim
->nested
[0],
1028 isl_dim_param
, pos
, n
)))
1030 if (dim
&& dim
->nested
[1] &&
1031 !(dim
->nested
[1] = isl_space_insert_dims(dim
->nested
[1],
1032 isl_dim_param
, pos
, n
)))
1038 isl_space_free(dim
);
1042 __isl_give isl_space
*isl_space_move_dims(__isl_take isl_space
*dim
,
1043 enum isl_dim_type dst_type
, unsigned dst_pos
,
1044 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1051 dim
= isl_space_reset(dim
, src_type
);
1052 return isl_space_reset(dim
, dst_type
);
1055 isl_assert(dim
->ctx
, src_pos
+ n
<= isl_space_dim(dim
, src_type
),
1058 if (dst_type
== src_type
&& dst_pos
== src_pos
)
1061 isl_assert(dim
->ctx
, dst_type
!= src_type
, goto error
);
1063 dim
= isl_space_reset(dim
, src_type
);
1064 dim
= isl_space_reset(dim
, dst_type
);
1066 dim
= isl_space_cow(dim
);
1072 enum isl_dim_type t
, o
= isl_dim_param
;
1075 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
1076 dim
->nparam
+ dim
->n_in
+ dim
->n_out
);
1080 s
[isl_dim_param
- o
] = dim
->nparam
;
1081 s
[isl_dim_in
- o
] = dim
->n_in
;
1082 s
[isl_dim_out
- o
] = dim
->n_out
;
1083 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1084 if (t
== dst_type
) {
1085 get_ids(dim
, t
, 0, dst_pos
, ids
+ off
);
1087 get_ids(dim
, src_type
, src_pos
, n
, ids
+ off
);
1089 get_ids(dim
, t
, dst_pos
, s
[t
- o
] - dst_pos
,
1091 off
+= s
[t
- o
] - dst_pos
;
1092 } else if (t
== src_type
) {
1093 get_ids(dim
, t
, 0, src_pos
, ids
+ off
);
1095 get_ids(dim
, t
, src_pos
+ n
,
1096 s
[t
- o
] - src_pos
- n
, ids
+ off
);
1097 off
+= s
[t
- o
] - src_pos
- n
;
1099 get_ids(dim
, t
, 0, s
[t
- o
], ids
+ off
);
1105 dim
->n_id
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
1109 case isl_dim_param
: dim
->nparam
+= n
; break;
1110 case isl_dim_in
: dim
->n_in
+= n
; break;
1111 case isl_dim_out
: dim
->n_out
+= n
; break;
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;
1122 if (dst_type
!= isl_dim_param
&& src_type
!= isl_dim_param
)
1125 for (i
= 0; i
< 2; ++i
) {
1126 if (!dim
->nested
[i
])
1128 dim
->nested
[i
] = isl_space_replace(dim
->nested
[i
],
1129 isl_dim_param
, dim
);
1130 if (!dim
->nested
[i
])
1136 isl_space_free(dim
);
1140 __isl_give isl_space
*isl_space_join(__isl_take isl_space
*left
,
1141 __isl_take isl_space
*right
)
1145 if (!left
|| !right
)
1148 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
1150 isl_assert(left
->ctx
,
1151 isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_in
),
1154 dim
= isl_space_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
1158 dim
= copy_ids(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
1159 dim
= copy_ids(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
1160 dim
= copy_ids(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
1162 if (dim
&& left
->tuple_id
[0] &&
1163 !(dim
->tuple_id
[0] = isl_id_copy(left
->tuple_id
[0])))
1165 if (dim
&& right
->tuple_id
[1] &&
1166 !(dim
->tuple_id
[1] = isl_id_copy(right
->tuple_id
[1])))
1168 if (dim
&& left
->nested
[0] &&
1169 !(dim
->nested
[0] = isl_space_copy(left
->nested
[0])))
1171 if (dim
&& right
->nested
[1] &&
1172 !(dim
->nested
[1] = isl_space_copy(right
->nested
[1])))
1175 isl_space_free(left
);
1176 isl_space_free(right
);
1180 isl_space_free(left
);
1181 isl_space_free(right
);
1185 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1186 * { [A -> B] -> [C -> D] }.
1187 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1189 __isl_give isl_space
*isl_space_product(__isl_take isl_space
*left
,
1190 __isl_take isl_space
*right
)
1192 isl_space
*dom1
, *dom2
, *nest1
, *nest2
;
1195 if (!left
|| !right
)
1198 is_set
= isl_space_is_set(left
);
1199 if (is_set
!= isl_space_is_set(right
))
1200 isl_die(isl_space_get_ctx(left
), isl_error_invalid
,
1201 "expecting either two set spaces or two map spaces",
1204 return isl_space_range_product(left
, right
);
1206 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
1209 dom1
= isl_space_domain(isl_space_copy(left
));
1210 dom2
= isl_space_domain(isl_space_copy(right
));
1211 nest1
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1213 dom1
= isl_space_range(left
);
1214 dom2
= isl_space_range(right
);
1215 nest2
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1217 return isl_space_join(isl_space_reverse(nest1
), nest2
);
1219 isl_space_free(left
);
1220 isl_space_free(right
);
1224 /* Given two spaces { A -> C } and { B -> C }, construct the space
1227 __isl_give isl_space
*isl_space_domain_product(__isl_take isl_space
*left
,
1228 __isl_take isl_space
*right
)
1230 isl_space
*ran
, *dom1
, *dom2
, *nest
;
1232 if (!left
|| !right
)
1235 if (!match(left
, isl_dim_param
, right
, isl_dim_param
))
1236 isl_die(left
->ctx
, isl_error_invalid
,
1237 "parameters need to match", goto error
);
1238 if (!isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_out
))
1239 isl_die(left
->ctx
, isl_error_invalid
,
1240 "ranges need to match", goto error
);
1242 ran
= isl_space_range(isl_space_copy(left
));
1244 dom1
= isl_space_domain(left
);
1245 dom2
= isl_space_domain(right
);
1246 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1248 return isl_space_join(isl_space_reverse(nest
), ran
);
1250 isl_space_free(left
);
1251 isl_space_free(right
);
1255 __isl_give isl_space
*isl_space_range_product(__isl_take isl_space
*left
,
1256 __isl_take isl_space
*right
)
1258 isl_space
*dom
, *ran1
, *ran2
, *nest
;
1260 if (!left
|| !right
)
1263 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
1265 if (!isl_space_tuple_is_equal(left
, isl_dim_in
, right
, isl_dim_in
))
1266 isl_die(left
->ctx
, isl_error_invalid
,
1267 "domains need to match", goto error
);
1269 dom
= isl_space_domain(isl_space_copy(left
));
1271 ran1
= isl_space_range(left
);
1272 ran2
= isl_space_range(right
);
1273 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(ran1
), ran2
));
1275 return isl_space_join(isl_space_reverse(dom
), nest
);
1277 isl_space_free(left
);
1278 isl_space_free(right
);
1282 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1284 __isl_give isl_space
*isl_space_factor_domain(__isl_take isl_space
*space
)
1286 space
= isl_space_domain_factor_domain(space
);
1287 space
= isl_space_range_factor_domain(space
);
1291 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1293 __isl_give isl_space
*isl_space_domain_factor_domain(
1294 __isl_take isl_space
*space
)
1301 if (!isl_space_domain_is_wrapping(space
))
1302 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1303 "domain not a product", return isl_space_free(space
));
1305 nested
= space
->nested
[0];
1306 domain
= isl_space_copy(space
);
1307 domain
= isl_space_drop_dims(domain
, isl_dim_in
,
1308 nested
->n_in
, nested
->n_out
);
1310 return isl_space_free(space
);
1311 if (nested
->tuple_id
[0]) {
1312 domain
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[0]);
1313 if (!domain
->tuple_id
[0])
1316 if (nested
->nested
[0]) {
1317 domain
->nested
[0] = isl_space_copy(nested
->nested
[0]);
1318 if (!domain
->nested
[0])
1322 isl_space_free(space
);
1325 isl_space_free(space
);
1326 isl_space_free(domain
);
1330 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1332 __isl_give isl_space
*isl_space_domain_factor_range(
1333 __isl_take isl_space
*space
)
1340 if (!isl_space_domain_is_wrapping(space
))
1341 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1342 "domain not a product", return isl_space_free(space
));
1344 nested
= space
->nested
[0];
1345 range
= isl_space_copy(space
);
1346 range
= isl_space_drop_dims(range
, isl_dim_in
, 0, nested
->n_in
);
1348 return isl_space_free(space
);
1349 if (nested
->tuple_id
[1]) {
1350 range
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[1]);
1351 if (!range
->tuple_id
[0])
1354 if (nested
->nested
[1]) {
1355 range
->nested
[0] = isl_space_copy(nested
->nested
[1]);
1356 if (!range
->nested
[0])
1360 isl_space_free(space
);
1363 isl_space_free(space
);
1364 isl_space_free(range
);
1368 /* Given a space of the form A -> [B -> C], return the space A -> B.
1370 __isl_give isl_space
*isl_space_range_factor_domain(
1371 __isl_take isl_space
*space
)
1378 if (!isl_space_range_is_wrapping(space
))
1379 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1380 "range not a product", return isl_space_free(space
));
1382 nested
= space
->nested
[1];
1383 domain
= isl_space_copy(space
);
1384 domain
= isl_space_drop_dims(domain
, isl_dim_out
,
1385 nested
->n_in
, nested
->n_out
);
1387 return isl_space_free(space
);
1388 if (nested
->tuple_id
[0]) {
1389 domain
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[0]);
1390 if (!domain
->tuple_id
[1])
1393 if (nested
->nested
[0]) {
1394 domain
->nested
[1] = isl_space_copy(nested
->nested
[0]);
1395 if (!domain
->nested
[1])
1399 isl_space_free(space
);
1402 isl_space_free(space
);
1403 isl_space_free(domain
);
1407 /* Internal function that selects the range of the map that is
1408 * embedded in either a set space or the range of a map space.
1409 * In particular, given a space of the form [A -> B], return the space B.
1410 * Given a space of the form A -> [B -> C], return the space A -> C.
1412 static __isl_give isl_space
*range_factor_range(__isl_take isl_space
*space
)
1420 nested
= space
->nested
[1];
1421 range
= isl_space_copy(space
);
1422 range
= isl_space_drop_dims(range
, isl_dim_out
, 0, nested
->n_in
);
1424 return isl_space_free(space
);
1425 if (nested
->tuple_id
[1]) {
1426 range
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[1]);
1427 if (!range
->tuple_id
[1])
1430 if (nested
->nested
[1]) {
1431 range
->nested
[1] = isl_space_copy(nested
->nested
[1]);
1432 if (!range
->nested
[1])
1436 isl_space_free(space
);
1439 isl_space_free(space
);
1440 isl_space_free(range
);
1444 /* Given a space of the form A -> [B -> C], return the space A -> C.
1446 __isl_give isl_space
*isl_space_range_factor_range(
1447 __isl_take isl_space
*space
)
1451 if (!isl_space_range_is_wrapping(space
))
1452 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1453 "range not a product", return isl_space_free(space
));
1455 return range_factor_range(space
);
1458 /* Given a space of the form [A -> B], return the space B.
1460 static __isl_give isl_space
*set_factor_range(__isl_take isl_space
*space
)
1464 if (!isl_space_is_wrapping(space
))
1465 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1466 "not a product", return isl_space_free(space
));
1468 return range_factor_range(space
);
1471 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1472 * Given a space of the form [A -> B], return the space B.
1474 __isl_give isl_space
*isl_space_factor_range(__isl_take isl_space
*space
)
1478 if (isl_space_is_set(space
))
1479 return set_factor_range(space
);
1480 space
= isl_space_domain_factor_range(space
);
1481 space
= isl_space_range_factor_range(space
);
1485 __isl_give isl_space
*isl_space_map_from_set(__isl_take isl_space
*dim
)
1488 isl_id
**ids
= NULL
;
1492 ctx
= isl_space_get_ctx(dim
);
1493 if (!isl_space_is_set(dim
))
1494 isl_die(ctx
, isl_error_invalid
, "not a set space", goto error
);
1495 dim
= isl_space_cow(dim
);
1499 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
1500 dim
->nparam
+ dim
->n_out
+ dim
->n_out
);
1503 get_ids(dim
, isl_dim_param
, 0, dim
->nparam
, ids
);
1504 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->nparam
);
1506 dim
->n_in
= dim
->n_out
;
1510 dim
->n_id
= dim
->nparam
+ dim
->n_out
+ dim
->n_out
;
1511 dim
= copy_ids(dim
, isl_dim_out
, 0, dim
, isl_dim_in
);
1513 isl_id_free(dim
->tuple_id
[0]);
1514 dim
->tuple_id
[0] = isl_id_copy(dim
->tuple_id
[1]);
1515 isl_space_free(dim
->nested
[0]);
1516 dim
->nested
[0] = isl_space_copy(dim
->nested
[1]);
1519 isl_space_free(dim
);
1523 __isl_give isl_space
*isl_space_map_from_domain_and_range(
1524 __isl_take isl_space
*domain
, __isl_take isl_space
*range
)
1526 if (!domain
|| !range
)
1528 if (!isl_space_is_set(domain
))
1529 isl_die(isl_space_get_ctx(domain
), isl_error_invalid
,
1530 "domain is not a set space", goto error
);
1531 if (!isl_space_is_set(range
))
1532 isl_die(isl_space_get_ctx(range
), isl_error_invalid
,
1533 "range is not a set space", goto error
);
1534 return isl_space_join(isl_space_reverse(domain
), range
);
1536 isl_space_free(domain
);
1537 isl_space_free(range
);
1541 static __isl_give isl_space
*set_ids(__isl_take isl_space
*dim
,
1542 enum isl_dim_type type
,
1543 unsigned first
, unsigned n
, __isl_take isl_id
**ids
)
1547 for (i
= 0; i
< n
; ++i
)
1548 dim
= set_id(dim
, type
, first
+ i
, ids
[i
]);
1553 __isl_give isl_space
*isl_space_reverse(__isl_take isl_space
*dim
)
1557 isl_id
**ids
= NULL
;
1562 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
1565 dim
= isl_space_cow(dim
);
1569 id
= dim
->tuple_id
[0];
1570 dim
->tuple_id
[0] = dim
->tuple_id
[1];
1571 dim
->tuple_id
[1] = id
;
1573 nested
= dim
->nested
[0];
1574 dim
->nested
[0] = dim
->nested
[1];
1575 dim
->nested
[1] = nested
;
1578 int n_id
= dim
->n_in
+ dim
->n_out
;
1579 ids
= isl_alloc_array(dim
->ctx
, isl_id
*, n_id
);
1582 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
);
1583 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->n_in
);
1587 dim
->n_in
= dim
->n_out
;
1591 dim
= set_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
);
1592 dim
= set_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
+ dim
->n_out
);
1599 isl_space_free(dim
);
1603 __isl_give isl_space
*isl_space_drop_dims(__isl_take isl_space
*dim
,
1604 enum isl_dim_type type
, unsigned first
, unsigned num
)
1612 return isl_space_reset(dim
, type
);
1614 if (!valid_dim_type(type
))
1615 isl_die(dim
->ctx
, isl_error_invalid
,
1616 "cannot drop dimensions of specified type", goto error
);
1618 if (first
+ num
> n(dim
, type
) || first
+ num
< first
)
1619 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1620 "index out of bounds", return isl_space_free(dim
));
1621 dim
= isl_space_cow(dim
);
1625 dim
= extend_ids(dim
);
1628 for (i
= 0; i
< num
; ++i
)
1629 isl_id_free(get_id(dim
, type
, first
+ i
));
1630 for (i
= first
+num
; i
< n(dim
, type
); ++i
)
1631 set_id(dim
, type
, i
- num
, get_id(dim
, type
, i
));
1634 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
,
1635 dim
->ids
+ offset(dim
, isl_dim_in
) - num
);
1637 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
,
1638 dim
->ids
+ offset(dim
, isl_dim_out
) - num
);
1645 case isl_dim_param
: dim
->nparam
-= num
; break;
1646 case isl_dim_in
: dim
->n_in
-= num
; break;
1647 case isl_dim_out
: dim
->n_out
-= num
; break;
1650 dim
= isl_space_reset(dim
, type
);
1651 if (type
== isl_dim_param
) {
1652 if (dim
&& dim
->nested
[0] &&
1653 !(dim
->nested
[0] = isl_space_drop_dims(dim
->nested
[0],
1654 isl_dim_param
, first
, num
)))
1656 if (dim
&& dim
->nested
[1] &&
1657 !(dim
->nested
[1] = isl_space_drop_dims(dim
->nested
[1],
1658 isl_dim_param
, first
, num
)))
1663 isl_space_free(dim
);
1667 __isl_give isl_space
*isl_space_drop_inputs(__isl_take isl_space
*dim
,
1668 unsigned first
, unsigned n
)
1672 return isl_space_drop_dims(dim
, isl_dim_in
, first
, n
);
1675 __isl_give isl_space
*isl_space_drop_outputs(__isl_take isl_space
*dim
,
1676 unsigned first
, unsigned n
)
1680 return isl_space_drop_dims(dim
, isl_dim_out
, first
, n
);
1683 __isl_give isl_space
*isl_space_domain(__isl_take isl_space
*dim
)
1687 dim
= isl_space_drop_outputs(dim
, 0, dim
->n_out
);
1688 dim
= isl_space_reverse(dim
);
1689 dim
= mark_as_set(dim
);
1693 __isl_give isl_space
*isl_space_from_domain(__isl_take isl_space
*dim
)
1697 if (!isl_space_is_set(dim
))
1698 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1699 "not a set space", goto error
);
1700 dim
= isl_space_reverse(dim
);
1701 dim
= isl_space_reset(dim
, isl_dim_out
);
1704 isl_space_free(dim
);
1708 __isl_give isl_space
*isl_space_range(__isl_take isl_space
*dim
)
1712 dim
= isl_space_drop_inputs(dim
, 0, dim
->n_in
);
1713 dim
= mark_as_set(dim
);
1717 __isl_give isl_space
*isl_space_from_range(__isl_take isl_space
*dim
)
1721 if (!isl_space_is_set(dim
))
1722 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1723 "not a set space", goto error
);
1724 return isl_space_reset(dim
, isl_dim_in
);
1726 isl_space_free(dim
);
1730 /* Given a map space A -> B, return the map space [A -> B] -> A.
1732 __isl_give isl_space
*isl_space_domain_map(__isl_take isl_space
*space
)
1736 domain
= isl_space_from_range(isl_space_domain(isl_space_copy(space
)));
1737 space
= isl_space_from_domain(isl_space_wrap(space
));
1738 space
= isl_space_join(space
, domain
);
1743 /* Given a map space A -> B, return the map space [A -> B] -> B.
1745 __isl_give isl_space
*isl_space_range_map(__isl_take isl_space
*space
)
1749 range
= isl_space_from_range(isl_space_range(isl_space_copy(space
)));
1750 space
= isl_space_from_domain(isl_space_wrap(space
));
1751 space
= isl_space_join(space
, range
);
1756 __isl_give isl_space
*isl_space_params(__isl_take isl_space
*space
)
1758 if (isl_space_is_params(space
))
1760 space
= isl_space_drop_dims(space
,
1761 isl_dim_in
, 0, isl_space_dim(space
, isl_dim_in
));
1762 space
= isl_space_drop_dims(space
,
1763 isl_dim_out
, 0, isl_space_dim(space
, isl_dim_out
));
1764 space
= mark_as_params(space
);
1768 __isl_give isl_space
*isl_space_set_from_params(__isl_take isl_space
*space
)
1772 if (!isl_space_is_params(space
))
1773 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1774 "not a parameter space", goto error
);
1775 return isl_space_reset(space
, isl_dim_set
);
1777 isl_space_free(space
);
1781 __isl_give isl_space
*isl_space_as_set_space(__isl_take isl_space
*dim
)
1783 dim
= isl_space_cow(dim
);
1787 dim
->n_out
+= dim
->n_in
;
1789 dim
= isl_space_reset(dim
, isl_dim_in
);
1790 dim
= isl_space_reset(dim
, isl_dim_out
);
1795 __isl_give isl_space
*isl_space_underlying(__isl_take isl_space
*dim
,
1803 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_id
== 0)
1804 return isl_space_reset(isl_space_reset(dim
, isl_dim_in
), isl_dim_out
);
1805 dim
= isl_space_cow(dim
);
1808 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
1812 for (i
= 0; i
< dim
->n_id
; ++i
)
1813 isl_id_free(get_id(dim
, isl_dim_out
, i
));
1815 dim
= isl_space_reset(dim
, isl_dim_in
);
1816 dim
= isl_space_reset(dim
, isl_dim_out
);
1821 /* Are the two spaces the same, including positions and names of parameters?
1823 isl_bool
isl_space_is_equal(__isl_keep isl_space
*dim1
,
1824 __isl_keep isl_space
*dim2
)
1827 return isl_bool_error
;
1829 return isl_bool_true
;
1830 return match(dim1
, isl_dim_param
, dim2
, isl_dim_param
) &&
1831 isl_space_tuple_is_equal(dim1
, isl_dim_in
, dim2
, isl_dim_in
) &&
1832 isl_space_tuple_is_equal(dim1
, isl_dim_out
, dim2
, isl_dim_out
);
1835 /* Is space1 equal to the domain of space2?
1837 * In the internal version we also allow space2 to be the space of a set,
1838 * provided space1 is a parameter space.
1840 isl_bool
isl_space_is_domain_internal(__isl_keep isl_space
*space1
,
1841 __isl_keep isl_space
*space2
)
1843 if (!space1
|| !space2
)
1844 return isl_bool_error
;
1845 if (!isl_space_is_set(space1
))
1846 return isl_bool_false
;
1847 return match(space1
, isl_dim_param
, space2
, isl_dim_param
) &&
1848 isl_space_tuple_is_equal(space1
, isl_dim_set
,
1849 space2
, isl_dim_in
);
1852 /* Is space1 equal to the domain of space2?
1854 isl_bool
isl_space_is_domain(__isl_keep isl_space
*space1
,
1855 __isl_keep isl_space
*space2
)
1858 return isl_bool_error
;
1859 if (!isl_space_is_map(space2
))
1860 return isl_bool_false
;
1861 return isl_space_is_domain_internal(space1
, space2
);
1864 /* Is space1 equal to the range of space2?
1866 * In the internal version, space2 is allowed to be the space of a set,
1867 * in which case it should be equal to space1.
1869 isl_bool
isl_space_is_range_internal(__isl_keep isl_space
*space1
,
1870 __isl_keep isl_space
*space2
)
1872 if (!space1
|| !space2
)
1873 return isl_bool_error
;
1874 if (!isl_space_is_set(space1
))
1875 return isl_bool_false
;
1876 return match(space1
, isl_dim_param
, space2
, isl_dim_param
) &&
1877 isl_space_tuple_is_equal(space1
, isl_dim_set
,
1878 space2
, isl_dim_out
);
1881 /* Is space1 equal to the range of space2?
1883 isl_bool
isl_space_is_range(__isl_keep isl_space
*space1
,
1884 __isl_keep isl_space
*space2
)
1887 return isl_bool_error
;
1888 if (!isl_space_is_map(space2
))
1889 return isl_bool_false
;
1890 return isl_space_is_range_internal(space1
, space2
);
1893 int isl_space_compatible(__isl_keep isl_space
*dim1
,
1894 __isl_keep isl_space
*dim2
)
1896 return dim1
->nparam
== dim2
->nparam
&&
1897 dim1
->n_in
+ dim1
->n_out
== dim2
->n_in
+ dim2
->n_out
;
1900 /* Update "hash" by hashing in "space".
1901 * Changes in this function should be reflected in isl_hash_space_domain.
1903 static uint32_t isl_hash_space(uint32_t hash
, __isl_keep isl_space
*space
)
1911 isl_hash_byte(hash
, space
->nparam
% 256);
1912 isl_hash_byte(hash
, space
->n_in
% 256);
1913 isl_hash_byte(hash
, space
->n_out
% 256);
1915 for (i
= 0; i
< space
->nparam
; ++i
) {
1916 id
= get_id(space
, isl_dim_param
, i
);
1917 hash
= isl_hash_id(hash
, id
);
1920 id
= tuple_id(space
, isl_dim_in
);
1921 hash
= isl_hash_id(hash
, id
);
1922 id
= tuple_id(space
, isl_dim_out
);
1923 hash
= isl_hash_id(hash
, id
);
1925 hash
= isl_hash_space(hash
, space
->nested
[0]);
1926 hash
= isl_hash_space(hash
, space
->nested
[1]);
1931 /* Update "hash" by hashing in the domain of "space".
1932 * The result of this function is equal to the result of applying
1933 * isl_hash_space to the domain of "space".
1935 static uint32_t isl_hash_space_domain(uint32_t hash
,
1936 __isl_keep isl_space
*space
)
1944 isl_hash_byte(hash
, space
->nparam
% 256);
1945 isl_hash_byte(hash
, 0);
1946 isl_hash_byte(hash
, space
->n_in
% 256);
1948 for (i
= 0; i
< space
->nparam
; ++i
) {
1949 id
= get_id(space
, isl_dim_param
, i
);
1950 hash
= isl_hash_id(hash
, id
);
1953 hash
= isl_hash_id(hash
, &isl_id_none
);
1954 id
= tuple_id(space
, isl_dim_in
);
1955 hash
= isl_hash_id(hash
, id
);
1957 hash
= isl_hash_space(hash
, space
->nested
[0]);
1962 uint32_t isl_space_get_hash(__isl_keep isl_space
*dim
)
1969 hash
= isl_hash_init();
1970 hash
= isl_hash_space(hash
, dim
);
1975 /* Return the hash value of the domain of "space".
1976 * That is, isl_space_get_domain_hash(space) is equal to
1977 * isl_space_get_hash(isl_space_domain(space)).
1979 uint32_t isl_space_get_domain_hash(__isl_keep isl_space
*space
)
1986 hash
= isl_hash_init();
1987 hash
= isl_hash_space_domain(hash
, space
);
1992 isl_bool
isl_space_is_wrapping(__isl_keep isl_space
*dim
)
1995 return isl_bool_error
;
1997 if (!isl_space_is_set(dim
))
1998 return isl_bool_false
;
2000 return dim
->nested
[1] != NULL
;
2003 /* Is "space" the space of a map where the domain is a wrapped map space?
2005 isl_bool
isl_space_domain_is_wrapping(__isl_keep isl_space
*space
)
2008 return isl_bool_error
;
2010 if (isl_space_is_set(space
))
2011 return isl_bool_false
;
2013 return space
->nested
[0] != NULL
;
2016 /* Is "space" the space of a map where the range is a wrapped map space?
2018 isl_bool
isl_space_range_is_wrapping(__isl_keep isl_space
*space
)
2021 return isl_bool_error
;
2023 if (isl_space_is_set(space
))
2024 return isl_bool_false
;
2026 return space
->nested
[1] != NULL
;
2029 __isl_give isl_space
*isl_space_wrap(__isl_take isl_space
*dim
)
2036 wrap
= isl_space_set_alloc(dim
->ctx
,
2037 dim
->nparam
, dim
->n_in
+ dim
->n_out
);
2039 wrap
= copy_ids(wrap
, isl_dim_param
, 0, dim
, isl_dim_param
);
2040 wrap
= copy_ids(wrap
, isl_dim_set
, 0, dim
, isl_dim_in
);
2041 wrap
= copy_ids(wrap
, isl_dim_set
, dim
->n_in
, dim
, isl_dim_out
);
2046 wrap
->nested
[1] = dim
;
2050 isl_space_free(dim
);
2054 __isl_give isl_space
*isl_space_unwrap(__isl_take isl_space
*dim
)
2061 if (!isl_space_is_wrapping(dim
))
2062 isl_die(dim
->ctx
, isl_error_invalid
, "not a wrapping space",
2065 unwrap
= isl_space_copy(dim
->nested
[1]);
2066 isl_space_free(dim
);
2070 isl_space_free(dim
);
2074 int isl_space_is_named_or_nested(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
2076 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
2080 if (dim
->tuple_id
[type
- isl_dim_in
])
2082 if (dim
->nested
[type
- isl_dim_in
])
2087 int isl_space_may_be_set(__isl_keep isl_space
*dim
)
2091 if (isl_space_is_set(dim
))
2093 if (isl_space_dim(dim
, isl_dim_in
) != 0)
2095 if (isl_space_is_named_or_nested(dim
, isl_dim_in
))
2100 __isl_give isl_space
*isl_space_reset(__isl_take isl_space
*dim
,
2101 enum isl_dim_type type
)
2103 if (!isl_space_is_named_or_nested(dim
, type
))
2106 dim
= isl_space_cow(dim
);
2110 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
2111 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
2112 isl_space_free(dim
->nested
[type
- isl_dim_in
]);
2113 dim
->nested
[type
- isl_dim_in
] = NULL
;
2118 __isl_give isl_space
*isl_space_flatten(__isl_take isl_space
*dim
)
2122 if (!dim
->nested
[0] && !dim
->nested
[1])
2126 dim
= isl_space_reset(dim
, isl_dim_in
);
2127 if (dim
&& dim
->nested
[1])
2128 dim
= isl_space_reset(dim
, isl_dim_out
);
2133 __isl_give isl_space
*isl_space_flatten_domain(__isl_take isl_space
*dim
)
2137 if (!dim
->nested
[0])
2140 return isl_space_reset(dim
, isl_dim_in
);
2143 __isl_give isl_space
*isl_space_flatten_range(__isl_take isl_space
*dim
)
2147 if (!dim
->nested
[1])
2150 return isl_space_reset(dim
, isl_dim_out
);
2153 /* Replace the dimensions of the given type of dst by those of src.
2155 __isl_give isl_space
*isl_space_replace(__isl_take isl_space
*dst
,
2156 enum isl_dim_type type
, __isl_keep isl_space
*src
)
2158 dst
= isl_space_cow(dst
);
2163 dst
= isl_space_drop_dims(dst
, type
, 0, isl_space_dim(dst
, type
));
2164 dst
= isl_space_add_dims(dst
, type
, isl_space_dim(src
, type
));
2165 dst
= copy_ids(dst
, type
, 0, src
, type
);
2167 if (dst
&& type
== isl_dim_param
) {
2169 for (i
= 0; i
<= 1; ++i
) {
2170 if (!dst
->nested
[i
])
2172 dst
->nested
[i
] = isl_space_replace(dst
->nested
[i
],
2174 if (!dst
->nested
[i
])
2181 isl_space_free(dst
);
2185 /* Given a dimension specification "dim" of a set, create a dimension
2186 * specification for the lift of the set. In particular, the result
2187 * is of the form [dim -> local[..]], with n_local variables in the
2188 * range of the wrapped map.
2190 __isl_give isl_space
*isl_space_lift(__isl_take isl_space
*dim
, unsigned n_local
)
2192 isl_space
*local_dim
;
2197 local_dim
= isl_space_dup(dim
);
2198 local_dim
= isl_space_drop_dims(local_dim
, isl_dim_set
, 0, dim
->n_out
);
2199 local_dim
= isl_space_add_dims(local_dim
, isl_dim_set
, n_local
);
2200 local_dim
= isl_space_set_tuple_name(local_dim
, isl_dim_set
, "local");
2201 dim
= isl_space_join(isl_space_from_domain(dim
),
2202 isl_space_from_range(local_dim
));
2203 dim
= isl_space_wrap(dim
);
2204 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, "lifted");
2209 isl_bool
isl_space_can_zip(__isl_keep isl_space
*dim
)
2212 return isl_bool_error
;
2214 return dim
->nested
[0] && dim
->nested
[1];
2217 __isl_give isl_space
*isl_space_zip(__isl_take isl_space
*dim
)
2219 isl_space
*dom
, *ran
;
2220 isl_space
*dom_dom
, *dom_ran
, *ran_dom
, *ran_ran
;
2222 if (!isl_space_can_zip(dim
))
2223 isl_die(dim
->ctx
, isl_error_invalid
, "dim cannot be zipped",
2228 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(dim
)));
2229 ran
= isl_space_unwrap(isl_space_range(dim
));
2230 dom_dom
= isl_space_domain(isl_space_copy(dom
));
2231 dom_ran
= isl_space_range(dom
);
2232 ran_dom
= isl_space_domain(isl_space_copy(ran
));
2233 ran_ran
= isl_space_range(ran
);
2234 dom
= isl_space_join(isl_space_from_domain(dom_dom
),
2235 isl_space_from_range(ran_dom
));
2236 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
2237 isl_space_from_range(ran_ran
));
2238 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
2239 isl_space_from_range(isl_space_wrap(ran
)));
2241 isl_space_free(dim
);
2245 /* Can we apply isl_space_curry to "space"?
2246 * That is, does it have a nested relation in its domain?
2248 isl_bool
isl_space_can_curry(__isl_keep isl_space
*space
)
2251 return isl_bool_error
;
2253 return !!space
->nested
[0];
2256 /* Given a space (A -> B) -> C, return the corresponding space
2259 __isl_give isl_space
*isl_space_curry(__isl_take isl_space
*space
)
2261 isl_space
*dom
, *ran
;
2262 isl_space
*dom_dom
, *dom_ran
;
2267 if (!isl_space_can_curry(space
))
2268 isl_die(space
->ctx
, isl_error_invalid
,
2269 "space cannot be curried", goto error
);
2271 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(space
)));
2272 ran
= isl_space_range(space
);
2273 dom_dom
= isl_space_domain(isl_space_copy(dom
));
2274 dom_ran
= isl_space_range(dom
);
2275 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
2276 isl_space_from_range(ran
));
2277 return isl_space_join(isl_space_from_domain(dom_dom
),
2278 isl_space_from_range(isl_space_wrap(ran
)));
2280 isl_space_free(space
);
2284 /* Can we apply isl_space_uncurry to "space"?
2285 * That is, does it have a nested relation in its range?
2287 isl_bool
isl_space_can_uncurry(__isl_keep isl_space
*space
)
2290 return isl_bool_error
;
2292 return !!space
->nested
[1];
2295 /* Given a space A -> (B -> C), return the corresponding space
2298 __isl_give isl_space
*isl_space_uncurry(__isl_take isl_space
*space
)
2300 isl_space
*dom
, *ran
;
2301 isl_space
*ran_dom
, *ran_ran
;
2306 if (!isl_space_can_uncurry(space
))
2307 isl_die(space
->ctx
, isl_error_invalid
,
2308 "space cannot be uncurried",
2309 return isl_space_free(space
));
2311 dom
= isl_space_domain(isl_space_copy(space
));
2312 ran
= isl_space_unwrap(isl_space_range(space
));
2313 ran_dom
= isl_space_domain(isl_space_copy(ran
));
2314 ran_ran
= isl_space_range(ran
);
2315 dom
= isl_space_join(isl_space_from_domain(dom
),
2316 isl_space_from_range(ran_dom
));
2317 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
2318 isl_space_from_range(ran_ran
));
2321 int isl_space_has_named_params(__isl_keep isl_space
*dim
)
2328 if (dim
->nparam
== 0)
2330 off
= isl_space_offset(dim
, isl_dim_param
);
2331 if (off
+ dim
->nparam
> dim
->n_id
)
2333 for (i
= 0; i
< dim
->nparam
; ++i
)
2334 if (!dim
->ids
[off
+ i
])
2339 /* Align the initial parameters of dim1 to match the order in dim2.
2341 __isl_give isl_space
*isl_space_align_params(__isl_take isl_space
*dim1
,
2342 __isl_take isl_space
*dim2
)
2344 isl_reordering
*exp
;
2346 if (!isl_space_has_named_params(dim1
) || !isl_space_has_named_params(dim2
))
2347 isl_die(isl_space_get_ctx(dim1
), isl_error_invalid
,
2348 "parameter alignment requires named parameters",
2351 dim2
= isl_space_params(dim2
);
2352 exp
= isl_parameter_alignment_reordering(dim1
, dim2
);
2353 exp
= isl_reordering_extend_space(exp
, dim1
);
2354 isl_space_free(dim2
);
2357 dim1
= isl_space_copy(exp
->dim
);
2358 isl_reordering_free(exp
);
2361 isl_space_free(dim1
);
2362 isl_space_free(dim2
);
2366 /* Given the space of set (domain), construct a space for a map
2367 * with as domain the given space and as range the range of "model".
2369 __isl_give isl_space
*isl_space_extend_domain_with_range(
2370 __isl_take isl_space
*space
, __isl_take isl_space
*model
)
2375 space
= isl_space_from_domain(space
);
2376 space
= isl_space_add_dims(space
, isl_dim_out
,
2377 isl_space_dim(model
, isl_dim_out
));
2378 if (isl_space_has_tuple_id(model
, isl_dim_out
))
2379 space
= isl_space_set_tuple_id(space
, isl_dim_out
,
2380 isl_space_get_tuple_id(model
, isl_dim_out
));
2383 if (model
->nested
[1]) {
2384 isl_space
*nested
= isl_space_copy(model
->nested
[1]);
2385 int n_nested
, n_space
;
2386 nested
= isl_space_align_params(nested
, isl_space_copy(space
));
2387 n_nested
= isl_space_dim(nested
, isl_dim_param
);
2388 n_space
= isl_space_dim(space
, isl_dim_param
);
2389 if (n_nested
> n_space
)
2390 nested
= isl_space_drop_dims(nested
, isl_dim_param
,
2391 n_space
, n_nested
- n_space
);
2394 space
->nested
[1] = nested
;
2396 isl_space_free(model
);
2399 isl_space_free(model
);
2400 isl_space_free(space
);
2404 /* Compare the "type" dimensions of two isl_spaces.
2406 * The order is fairly arbitrary.
2408 static int isl_space_cmp_type(__isl_keep isl_space
*space1
,
2409 __isl_keep isl_space
*space2
, enum isl_dim_type type
)
2412 isl_space
*nested1
, *nested2
;
2414 if (isl_space_dim(space1
, type
) != isl_space_dim(space2
, type
))
2415 return isl_space_dim(space1
, type
) -
2416 isl_space_dim(space2
, type
);
2418 cmp
= isl_id_cmp(tuple_id(space1
, type
), tuple_id(space2
, type
));
2422 nested1
= nested(space1
, type
);
2423 nested2
= nested(space2
, type
);
2424 if (!nested1
!= !nested2
)
2425 return !nested1
- !nested2
;
2428 return isl_space_cmp(nested1
, nested2
);
2433 /* Compare two isl_spaces.
2435 * The order is fairly arbitrary.
2437 int isl_space_cmp(__isl_keep isl_space
*space1
, __isl_keep isl_space
*space2
)
2442 if (space1
== space2
)
2449 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_param
);
2452 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_in
);
2455 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_out
);
2459 if (!space1
->ids
&& !space2
->ids
)
2462 for (i
= 0; i
< n(space1
, isl_dim_param
); ++i
) {
2463 cmp
= isl_id_cmp(get_id(space1
, isl_dim_param
, i
),
2464 get_id(space2
, isl_dim_param
, i
));