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
*space
, enum isl_dim_type type
)
261 return n(space
, type
);
264 unsigned isl_space_offset(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
268 return offset(dim
, type
);
271 static __isl_give isl_space
*copy_ids(__isl_take isl_space
*dst
,
272 enum isl_dim_type dst_type
, unsigned offset
, __isl_keep isl_space
*src
,
273 enum isl_dim_type src_type
)
281 for (i
= 0; i
< n(src
, src_type
); ++i
) {
282 id
= get_id(src
, src_type
, i
);
285 dst
= set_id(dst
, dst_type
, offset
+ i
, isl_id_copy(id
));
292 __isl_take isl_space
*isl_space_dup(__isl_keep isl_space
*dim
)
297 dup
= isl_space_alloc(dim
->ctx
, dim
->nparam
, dim
->n_in
, dim
->n_out
);
300 if (dim
->tuple_id
[0] &&
301 !(dup
->tuple_id
[0] = isl_id_copy(dim
->tuple_id
[0])))
303 if (dim
->tuple_id
[1] &&
304 !(dup
->tuple_id
[1] = isl_id_copy(dim
->tuple_id
[1])))
306 if (dim
->nested
[0] && !(dup
->nested
[0] = isl_space_copy(dim
->nested
[0])))
308 if (dim
->nested
[1] && !(dup
->nested
[1] = isl_space_copy(dim
->nested
[1])))
312 dup
= copy_ids(dup
, isl_dim_param
, 0, dim
, isl_dim_param
);
313 dup
= copy_ids(dup
, isl_dim_in
, 0, dim
, isl_dim_in
);
314 dup
= copy_ids(dup
, isl_dim_out
, 0, dim
, isl_dim_out
);
321 __isl_give isl_space
*isl_space_cow(__isl_take isl_space
*dim
)
329 return isl_space_dup(dim
);
332 __isl_give isl_space
*isl_space_copy(__isl_keep isl_space
*dim
)
341 __isl_null isl_space
*isl_space_free(__isl_take isl_space
*space
)
348 if (--space
->ref
> 0)
351 isl_id_free(space
->tuple_id
[0]);
352 isl_id_free(space
->tuple_id
[1]);
354 isl_space_free(space
->nested
[0]);
355 isl_space_free(space
->nested
[1]);
357 for (i
= 0; i
< space
->n_id
; ++i
)
358 isl_id_free(space
->ids
[i
]);
360 isl_ctx_deref(space
->ctx
);
367 /* Check if "s" is a valid dimension or tuple name.
368 * We currently only forbid names that look like a number.
370 * s is assumed to be non-NULL.
372 static int name_ok(isl_ctx
*ctx
, const char *s
)
377 dummy
= strtol(s
, &p
, 0);
379 isl_die(ctx
, isl_error_invalid
, "name looks like a number",
385 /* Is it possible for the given dimension type to have a tuple id?
387 static int space_can_have_id(__isl_keep isl_space
*space
,
388 enum isl_dim_type type
)
392 if (isl_space_is_params(space
))
393 isl_die(space
->ctx
, isl_error_invalid
,
394 "parameter spaces don't have tuple ids", return 0);
395 if (isl_space_is_set(space
) && type
!= isl_dim_set
)
396 isl_die(space
->ctx
, isl_error_invalid
,
397 "set spaces can only have a set id", return 0);
398 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
399 isl_die(space
->ctx
, isl_error_invalid
,
400 "only input, output and set tuples can have ids",
406 /* Does the tuple have an id?
408 isl_bool
isl_space_has_tuple_id(__isl_keep isl_space
*dim
,
409 enum isl_dim_type type
)
411 if (!space_can_have_id(dim
, type
))
412 return isl_bool_error
;
413 return dim
->tuple_id
[type
- isl_dim_in
] != NULL
;
416 __isl_give isl_id
*isl_space_get_tuple_id(__isl_keep isl_space
*dim
,
417 enum isl_dim_type type
)
423 has_id
= isl_space_has_tuple_id(dim
, type
);
427 isl_die(dim
->ctx
, isl_error_invalid
,
428 "tuple has no id", return NULL
);
429 return isl_id_copy(dim
->tuple_id
[type
- isl_dim_in
]);
432 __isl_give isl_space
*isl_space_set_tuple_id(__isl_take isl_space
*dim
,
433 enum isl_dim_type type
, __isl_take isl_id
*id
)
435 dim
= isl_space_cow(dim
);
438 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
439 isl_die(dim
->ctx
, isl_error_invalid
,
440 "only input, output and set tuples can have names",
443 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
444 dim
->tuple_id
[type
- isl_dim_in
] = id
;
453 __isl_give isl_space
*isl_space_reset_tuple_id(__isl_take isl_space
*dim
,
454 enum isl_dim_type type
)
456 dim
= isl_space_cow(dim
);
459 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
460 isl_die(dim
->ctx
, isl_error_invalid
,
461 "only input, output and set tuples can have names",
464 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
465 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
473 /* Set the id of the given dimension of "space" to "id".
474 * If the dimension already has an id, then it is replaced.
475 * If the dimension is a parameter, then we need to change it
476 * in the nested spaces (if any) as well.
478 __isl_give isl_space
*isl_space_set_dim_id(__isl_take isl_space
*space
,
479 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
481 space
= isl_space_cow(space
);
485 if (type
== isl_dim_param
) {
488 for (i
= 0; i
< 2; ++i
) {
489 if (!space
->nested
[i
])
492 isl_space_set_dim_id(space
->nested
[i
],
493 type
, pos
, isl_id_copy(id
));
494 if (!space
->nested
[i
])
499 isl_id_free(get_id(space
, type
, pos
));
500 return set_id(space
, type
, pos
, id
);
503 isl_space_free(space
);
507 /* Reset the id of the given dimension of "space".
508 * If the dimension already has an id, then it is removed.
509 * If the dimension is a parameter, then we need to reset it
510 * in the nested spaces (if any) as well.
512 __isl_give isl_space
*isl_space_reset_dim_id(__isl_take isl_space
*space
,
513 enum isl_dim_type type
, unsigned pos
)
515 space
= isl_space_cow(space
);
519 if (type
== isl_dim_param
) {
522 for (i
= 0; i
< 2; ++i
) {
523 if (!space
->nested
[i
])
526 isl_space_reset_dim_id(space
->nested
[i
],
528 if (!space
->nested
[i
])
533 isl_id_free(get_id(space
, type
, pos
));
534 return set_id(space
, type
, pos
, NULL
);
536 isl_space_free(space
);
540 isl_bool
isl_space_has_dim_id(__isl_keep isl_space
*dim
,
541 enum isl_dim_type type
, unsigned pos
)
544 return isl_bool_error
;
545 return get_id(dim
, type
, pos
) != NULL
;
548 __isl_give isl_id
*isl_space_get_dim_id(__isl_keep isl_space
*dim
,
549 enum isl_dim_type type
, unsigned pos
)
553 if (!get_id(dim
, type
, pos
))
554 isl_die(dim
->ctx
, isl_error_invalid
,
555 "dim has no id", return NULL
);
556 return isl_id_copy(get_id(dim
, type
, pos
));
559 __isl_give isl_space
*isl_space_set_tuple_name(__isl_take isl_space
*dim
,
560 enum isl_dim_type type
, const char *s
)
568 return isl_space_reset_tuple_id(dim
, type
);
570 if (!name_ok(dim
->ctx
, s
))
573 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
574 return isl_space_set_tuple_id(dim
, type
, id
);
580 /* Does the tuple have a name?
582 isl_bool
isl_space_has_tuple_name(__isl_keep isl_space
*space
,
583 enum isl_dim_type type
)
587 if (!space_can_have_id(space
, type
))
588 return isl_bool_error
;
589 id
= space
->tuple_id
[type
- isl_dim_in
];
590 return id
&& id
->name
;
593 __isl_keep
const char *isl_space_get_tuple_name(__isl_keep isl_space
*dim
,
594 enum isl_dim_type type
)
599 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
601 id
= dim
->tuple_id
[type
- isl_dim_in
];
602 return id
? id
->name
: NULL
;
605 __isl_give isl_space
*isl_space_set_dim_name(__isl_take isl_space
*dim
,
606 enum isl_dim_type type
, unsigned pos
,
614 return isl_space_reset_dim_id(dim
, type
, pos
);
615 if (!name_ok(dim
->ctx
, s
))
617 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
618 return isl_space_set_dim_id(dim
, type
, pos
, id
);
624 /* Does the given dimension have a name?
626 isl_bool
isl_space_has_dim_name(__isl_keep isl_space
*space
,
627 enum isl_dim_type type
, unsigned pos
)
632 return isl_bool_error
;
633 id
= get_id(space
, type
, pos
);
634 return id
&& id
->name
;
637 __isl_keep
const char *isl_space_get_dim_name(__isl_keep isl_space
*dim
,
638 enum isl_dim_type type
, unsigned pos
)
640 isl_id
*id
= get_id(dim
, type
, pos
);
641 return id
? id
->name
: NULL
;
644 int isl_space_find_dim_by_id(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
645 __isl_keep isl_id
*id
)
654 offset
= isl_space_offset(dim
, type
);
655 n
= isl_space_dim(dim
, type
);
656 for (i
= 0; i
< n
&& offset
+ i
< dim
->n_id
; ++i
)
657 if (dim
->ids
[offset
+ i
] == id
)
663 int isl_space_find_dim_by_name(__isl_keep isl_space
*space
,
664 enum isl_dim_type type
, const char *name
)
673 offset
= isl_space_offset(space
, type
);
674 n
= isl_space_dim(space
, type
);
675 for (i
= 0; i
< n
&& offset
+ i
< space
->n_id
; ++i
) {
676 isl_id
*id
= get_id(space
, type
, i
);
677 if (id
&& id
->name
&& !strcmp(id
->name
, name
))
684 /* Reset the user pointer on all identifiers of parameters and tuples
687 __isl_give isl_space
*isl_space_reset_user(__isl_take isl_space
*space
)
697 ctx
= isl_space_get_ctx(space
);
699 for (i
= 0; i
< space
->nparam
&& i
< space
->n_id
; ++i
) {
700 if (!isl_id_get_user(space
->ids
[i
]))
702 space
= isl_space_cow(space
);
705 name
= isl_id_get_name(space
->ids
[i
]);
706 id
= isl_id_alloc(ctx
, name
, NULL
);
707 isl_id_free(space
->ids
[i
]);
710 return isl_space_free(space
);
713 for (i
= 0; i
< 2; ++i
) {
714 if (!space
->tuple_id
[i
])
716 if (!isl_id_get_user(space
->tuple_id
[i
]))
718 space
= isl_space_cow(space
);
721 name
= isl_id_get_name(space
->tuple_id
[i
]);
722 id
= isl_id_alloc(ctx
, name
, NULL
);
723 isl_id_free(space
->tuple_id
[i
]);
724 space
->tuple_id
[i
] = id
;
726 return isl_space_free(space
);
729 for (i
= 0; i
< 2; ++i
) {
730 if (!space
->nested
[i
])
732 space
= isl_space_cow(space
);
735 space
->nested
[i
] = isl_space_reset_user(space
->nested
[i
]);
736 if (!space
->nested
[i
])
737 return isl_space_free(space
);
743 static __isl_keep isl_id
*tuple_id(__isl_keep isl_space
*dim
,
744 enum isl_dim_type type
)
748 if (type
== isl_dim_in
)
749 return dim
->tuple_id
[0];
750 if (type
== isl_dim_out
)
751 return dim
->tuple_id
[1];
755 static __isl_keep isl_space
*nested(__isl_keep isl_space
*dim
,
756 enum isl_dim_type type
)
760 if (type
== isl_dim_in
)
761 return dim
->nested
[0];
762 if (type
== isl_dim_out
)
763 return dim
->nested
[1];
767 /* Are the two spaces the same, apart from positions and names of parameters?
769 isl_bool
isl_space_has_equal_tuples(__isl_keep isl_space
*space1
,
770 __isl_keep isl_space
*space2
)
772 if (!space1
|| !space2
)
773 return isl_bool_error
;
774 if (space1
== space2
)
775 return isl_bool_true
;
776 return isl_space_tuple_is_equal(space1
, isl_dim_in
,
777 space2
, isl_dim_in
) &&
778 isl_space_tuple_is_equal(space1
, isl_dim_out
,
779 space2
, isl_dim_out
);
782 /* Check if the tuple of type "type1" of "space1" is the same as
783 * the tuple of type "type2" of "space2".
785 * That is, check if the tuples have the same identifier, the same dimension
786 * and the same internal structure.
787 * The identifiers of the dimensions inside the tuples do not affect the result.
789 * Note that this function only checks the tuples themselves.
790 * If nested tuples are involved, then we need to be careful not
791 * to have result affected by possibly differing parameters
792 * in those nested tuples.
794 isl_bool
isl_space_tuple_is_equal(__isl_keep isl_space
*space1
,
795 enum isl_dim_type type1
, __isl_keep isl_space
*space2
,
796 enum isl_dim_type type2
)
799 isl_space
*nested1
, *nested2
;
801 if (!space1
|| !space2
)
802 return isl_bool_error
;
804 if (space1
== space2
&& type1
== type2
)
805 return isl_bool_true
;
807 if (n(space1
, type1
) != n(space2
, type2
))
808 return isl_bool_false
;
809 id1
= tuple_id(space1
, type1
);
810 id2
= tuple_id(space2
, type2
);
812 return isl_bool_false
;
813 if (id1
&& id1
!= id2
)
814 return isl_bool_false
;
815 nested1
= nested(space1
, type1
);
816 nested2
= nested(space2
, type2
);
817 if (!nested1
^ !nested2
)
818 return isl_bool_false
;
819 if (nested1
&& !isl_space_has_equal_tuples(nested1
, nested2
))
820 return isl_bool_false
;
821 return isl_bool_true
;
824 static isl_bool
match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
825 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
829 if (space1
== space2
&& type1
== type2
)
830 return isl_bool_true
;
832 if (!isl_space_tuple_is_equal(space1
, type1
, space2
, type2
))
833 return isl_bool_false
;
835 if (!space1
->ids
&& !space2
->ids
)
836 return isl_bool_true
;
838 for (i
= 0; i
< n(space1
, type1
); ++i
) {
839 if (get_id(space1
, type1
, i
) != get_id(space2
, type2
, i
))
840 return isl_bool_false
;
842 return isl_bool_true
;
845 /* Do "space1" and "space2" have the same parameters?
847 isl_bool
isl_space_has_equal_params(__isl_keep isl_space
*space1
,
848 __isl_keep isl_space
*space2
)
850 if (!space1
|| !space2
)
851 return isl_bool_error
;
853 return match(space1
, isl_dim_param
, space2
, isl_dim_param
);
856 /* Do "space1" and "space2" have the same identifiers for all
857 * the tuple variables?
859 isl_bool
isl_space_has_equal_ids(__isl_keep isl_space
*space1
,
860 __isl_keep isl_space
*space2
)
864 if (!space1
|| !space2
)
865 return isl_bool_error
;
867 equal
= match(space1
, isl_dim_in
, space2
, isl_dim_in
);
868 if (equal
< 0 || !equal
)
870 return match(space1
, isl_dim_out
, space2
, isl_dim_out
);
873 isl_bool
isl_space_match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
874 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
876 if (!space1
|| !space2
)
877 return isl_bool_error
;
879 return match(space1
, type1
, space2
, type2
);
882 static void get_ids(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
883 unsigned first
, unsigned n
, __isl_keep isl_id
**ids
)
887 for (i
= 0; i
< n
; ++i
)
888 ids
[i
] = get_id(dim
, type
, first
+ i
);
891 static __isl_give isl_space
*space_extend(__isl_take isl_space
*space
,
892 unsigned nparam
, unsigned n_in
, unsigned n_out
)
898 if (space
->nparam
== nparam
&&
899 space
->n_in
== n_in
&& space
->n_out
== n_out
)
902 isl_assert(space
->ctx
, space
->nparam
<= nparam
, goto error
);
903 isl_assert(space
->ctx
, space
->n_in
<= n_in
, goto error
);
904 isl_assert(space
->ctx
, space
->n_out
<= n_out
, goto error
);
906 space
= isl_space_cow(space
);
912 n
= nparam
+ n_in
+ n_out
;
913 if (n
< nparam
|| n
< n_in
|| n
< n_out
)
914 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
915 "overflow in total number of dimensions",
917 ids
= isl_calloc_array(space
->ctx
, isl_id
*, n
);
920 get_ids(space
, isl_dim_param
, 0, space
->nparam
, ids
);
921 get_ids(space
, isl_dim_in
, 0, space
->n_in
, ids
+ nparam
);
922 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
923 ids
+ nparam
+ n_in
);
926 space
->n_id
= nparam
+ n_in
+ n_out
;
928 space
->nparam
= nparam
;
930 space
->n_out
= n_out
;
935 isl_space_free(space
);
939 __isl_give isl_space
*isl_space_extend(__isl_take isl_space
*space
,
940 unsigned nparam
, unsigned n_in
, unsigned n_out
)
942 return space_extend(space
, nparam
, n_in
, n_out
);
945 __isl_give isl_space
*isl_space_add_dims(__isl_take isl_space
*space
,
946 enum isl_dim_type type
, unsigned n
)
948 space
= isl_space_reset(space
, type
);
953 space
= space_extend(space
,
954 space
->nparam
+ n
, space
->n_in
, space
->n_out
);
955 if (space
&& space
->nested
[0] &&
956 !(space
->nested
[0] = isl_space_add_dims(space
->nested
[0],
959 if (space
&& space
->nested
[1] &&
960 !(space
->nested
[1] = isl_space_add_dims(space
->nested
[1],
965 return space_extend(space
,
966 space
->nparam
, space
->n_in
+ n
, space
->n_out
);
968 return space_extend(space
,
969 space
->nparam
, space
->n_in
, space
->n_out
+ n
);
971 isl_die(space
->ctx
, isl_error_invalid
,
972 "cannot add dimensions of specified type", goto error
);
975 isl_space_free(space
);
979 /* Add a parameter with identifier "id" to "space", provided
980 * it does not already appear in "space".
982 __isl_give isl_space
*isl_space_add_param_id(__isl_take isl_space
*space
,
983 __isl_take isl_id
*id
)
990 if (isl_space_find_dim_by_id(space
, isl_dim_param
, id
) >= 0) {
995 pos
= isl_space_dim(space
, isl_dim_param
);
996 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
997 space
= isl_space_set_dim_id(space
, isl_dim_param
, pos
, id
);
1001 isl_space_free(space
);
1006 static int valid_dim_type(enum isl_dim_type type
)
1018 /* Insert "n" dimensions of type "type" at position "pos".
1019 * If we are inserting parameters, then they are also inserted in
1020 * any nested spaces.
1022 __isl_give isl_space
*isl_space_insert_dims(__isl_take isl_space
*space
,
1023 enum isl_dim_type type
, unsigned pos
, unsigned n
)
1026 isl_id
**ids
= NULL
;
1032 return isl_space_reset(space
, type
);
1034 ctx
= isl_space_get_ctx(space
);
1035 if (!valid_dim_type(type
))
1036 isl_die(ctx
, isl_error_invalid
,
1037 "cannot insert dimensions of specified type",
1040 total
= isl_space_dim(space
, isl_dim_all
);
1041 if (total
+ n
< total
)
1042 isl_die(ctx
, isl_error_invalid
,
1043 "overflow in total number of dimensions",
1044 return isl_space_free(space
));
1045 isl_assert(ctx
, pos
<= isl_space_dim(space
, type
), goto error
);
1047 space
= isl_space_cow(space
);
1052 enum isl_dim_type t
, o
= isl_dim_param
;
1055 ids
= isl_calloc_array(ctx
, isl_id
*,
1056 space
->nparam
+ space
->n_in
+ space
->n_out
+ n
);
1060 s
[isl_dim_param
- o
] = space
->nparam
;
1061 s
[isl_dim_in
- o
] = space
->n_in
;
1062 s
[isl_dim_out
- o
] = space
->n_out
;
1063 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1065 get_ids(space
, t
, 0, s
[t
- o
], ids
+ off
);
1068 get_ids(space
, t
, 0, pos
, ids
+ off
);
1070 get_ids(space
, t
, pos
, s
[t
- o
] - pos
,
1072 off
+= s
[t
- o
] - pos
;
1077 space
->n_id
= space
->nparam
+ space
->n_in
+ space
->n_out
+ n
;
1080 case isl_dim_param
: space
->nparam
+= n
; break;
1081 case isl_dim_in
: space
->n_in
+= n
; break;
1082 case isl_dim_out
: space
->n_out
+= n
; break;
1085 space
= isl_space_reset(space
, type
);
1087 if (type
== isl_dim_param
) {
1088 if (space
&& space
->nested
[0] &&
1089 !(space
->nested
[0] = isl_space_insert_dims(space
->nested
[0],
1090 isl_dim_param
, pos
, n
)))
1092 if (space
&& space
->nested
[1] &&
1093 !(space
->nested
[1] = isl_space_insert_dims(space
->nested
[1],
1094 isl_dim_param
, pos
, n
)))
1100 isl_space_free(space
);
1104 __isl_give isl_space
*isl_space_move_dims(__isl_take isl_space
*space
,
1105 enum isl_dim_type dst_type
, unsigned dst_pos
,
1106 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1110 space
= isl_space_reset(space
, src_type
);
1111 space
= isl_space_reset(space
, dst_type
);
1117 isl_assert(space
->ctx
, src_pos
+ n
<= isl_space_dim(space
, src_type
),
1120 if (dst_type
== src_type
&& dst_pos
== src_pos
)
1123 isl_assert(space
->ctx
, dst_type
!= src_type
, goto error
);
1125 space
= isl_space_cow(space
);
1131 enum isl_dim_type t
, o
= isl_dim_param
;
1134 ids
= isl_calloc_array(space
->ctx
, isl_id
*,
1135 space
->nparam
+ space
->n_in
+ space
->n_out
);
1139 s
[isl_dim_param
- o
] = space
->nparam
;
1140 s
[isl_dim_in
- o
] = space
->n_in
;
1141 s
[isl_dim_out
- o
] = space
->n_out
;
1142 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1143 if (t
== dst_type
) {
1144 get_ids(space
, t
, 0, dst_pos
, ids
+ off
);
1146 get_ids(space
, src_type
, src_pos
, n
, ids
+ off
);
1148 get_ids(space
, t
, dst_pos
, s
[t
- o
] - dst_pos
,
1150 off
+= s
[t
- o
] - dst_pos
;
1151 } else if (t
== src_type
) {
1152 get_ids(space
, t
, 0, src_pos
, ids
+ off
);
1154 get_ids(space
, t
, src_pos
+ n
,
1155 s
[t
- o
] - src_pos
- n
, ids
+ off
);
1156 off
+= s
[t
- o
] - src_pos
- n
;
1158 get_ids(space
, t
, 0, s
[t
- o
], ids
+ off
);
1164 space
->n_id
= space
->nparam
+ space
->n_in
+ space
->n_out
;
1168 case isl_dim_param
: space
->nparam
+= n
; break;
1169 case isl_dim_in
: space
->n_in
+= n
; break;
1170 case isl_dim_out
: space
->n_out
+= n
; break;
1175 case isl_dim_param
: space
->nparam
-= n
; break;
1176 case isl_dim_in
: space
->n_in
-= n
; break;
1177 case isl_dim_out
: space
->n_out
-= n
; break;
1181 if (dst_type
!= isl_dim_param
&& src_type
!= isl_dim_param
)
1184 for (i
= 0; i
< 2; ++i
) {
1185 if (!space
->nested
[i
])
1187 space
->nested
[i
] = isl_space_replace_params(space
->nested
[i
],
1189 if (!space
->nested
[i
])
1195 isl_space_free(space
);
1199 /* Check that "space1" and "space2" have the same parameters,
1200 * reporting an error if they do not.
1202 isl_stat
isl_space_check_equal_params(__isl_keep isl_space
*space1
,
1203 __isl_keep isl_space
*space2
)
1207 equal
= isl_space_has_equal_params(space1
, space2
);
1209 return isl_stat_error
;
1211 isl_die(isl_space_get_ctx(space1
), isl_error_invalid
,
1212 "parameters need to match", return isl_stat_error
);
1216 __isl_give isl_space
*isl_space_join(__isl_take isl_space
*left
,
1217 __isl_take isl_space
*right
)
1221 if (isl_space_check_equal_params(left
, right
) < 0)
1224 isl_assert(left
->ctx
,
1225 isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_in
),
1228 dim
= isl_space_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
1232 dim
= copy_ids(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
1233 dim
= copy_ids(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
1234 dim
= copy_ids(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
1236 if (dim
&& left
->tuple_id
[0] &&
1237 !(dim
->tuple_id
[0] = isl_id_copy(left
->tuple_id
[0])))
1239 if (dim
&& right
->tuple_id
[1] &&
1240 !(dim
->tuple_id
[1] = isl_id_copy(right
->tuple_id
[1])))
1242 if (dim
&& left
->nested
[0] &&
1243 !(dim
->nested
[0] = isl_space_copy(left
->nested
[0])))
1245 if (dim
&& right
->nested
[1] &&
1246 !(dim
->nested
[1] = isl_space_copy(right
->nested
[1])))
1249 isl_space_free(left
);
1250 isl_space_free(right
);
1254 isl_space_free(left
);
1255 isl_space_free(right
);
1259 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1260 * { [A -> B] -> [C -> D] }.
1261 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1263 __isl_give isl_space
*isl_space_product(__isl_take isl_space
*left
,
1264 __isl_take isl_space
*right
)
1266 isl_space
*dom1
, *dom2
, *nest1
, *nest2
;
1269 if (!left
|| !right
)
1272 is_set
= isl_space_is_set(left
);
1273 if (is_set
!= isl_space_is_set(right
))
1274 isl_die(isl_space_get_ctx(left
), isl_error_invalid
,
1275 "expecting either two set spaces or two map spaces",
1278 return isl_space_range_product(left
, right
);
1280 if (isl_space_check_equal_params(left
, right
) < 0)
1283 dom1
= isl_space_domain(isl_space_copy(left
));
1284 dom2
= isl_space_domain(isl_space_copy(right
));
1285 nest1
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1287 dom1
= isl_space_range(left
);
1288 dom2
= isl_space_range(right
);
1289 nest2
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1291 return isl_space_join(isl_space_reverse(nest1
), nest2
);
1293 isl_space_free(left
);
1294 isl_space_free(right
);
1298 /* Given two spaces { A -> C } and { B -> C }, construct the space
1301 __isl_give isl_space
*isl_space_domain_product(__isl_take isl_space
*left
,
1302 __isl_take isl_space
*right
)
1304 isl_space
*ran
, *dom1
, *dom2
, *nest
;
1306 if (isl_space_check_equal_params(left
, right
) < 0)
1309 if (!isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_out
))
1310 isl_die(left
->ctx
, isl_error_invalid
,
1311 "ranges need to match", goto error
);
1313 ran
= isl_space_range(isl_space_copy(left
));
1315 dom1
= isl_space_domain(left
);
1316 dom2
= isl_space_domain(right
);
1317 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1319 return isl_space_join(isl_space_reverse(nest
), ran
);
1321 isl_space_free(left
);
1322 isl_space_free(right
);
1326 __isl_give isl_space
*isl_space_range_product(__isl_take isl_space
*left
,
1327 __isl_take isl_space
*right
)
1329 isl_space
*dom
, *ran1
, *ran2
, *nest
;
1331 if (isl_space_check_equal_params(left
, right
) < 0)
1334 if (!isl_space_tuple_is_equal(left
, isl_dim_in
, right
, isl_dim_in
))
1335 isl_die(left
->ctx
, isl_error_invalid
,
1336 "domains need to match", goto error
);
1338 dom
= isl_space_domain(isl_space_copy(left
));
1340 ran1
= isl_space_range(left
);
1341 ran2
= isl_space_range(right
);
1342 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(ran1
), ran2
));
1344 return isl_space_join(isl_space_reverse(dom
), nest
);
1346 isl_space_free(left
);
1347 isl_space_free(right
);
1351 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1353 __isl_give isl_space
*isl_space_domain_factor_domain(
1354 __isl_take isl_space
*space
)
1361 if (!isl_space_domain_is_wrapping(space
))
1362 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1363 "domain not a product", return isl_space_free(space
));
1365 nested
= space
->nested
[0];
1366 domain
= isl_space_copy(space
);
1367 domain
= isl_space_drop_dims(domain
, isl_dim_in
,
1368 nested
->n_in
, nested
->n_out
);
1370 return isl_space_free(space
);
1371 if (nested
->tuple_id
[0]) {
1372 domain
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[0]);
1373 if (!domain
->tuple_id
[0])
1376 if (nested
->nested
[0]) {
1377 domain
->nested
[0] = isl_space_copy(nested
->nested
[0]);
1378 if (!domain
->nested
[0])
1382 isl_space_free(space
);
1385 isl_space_free(space
);
1386 isl_space_free(domain
);
1390 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1392 __isl_give isl_space
*isl_space_domain_factor_range(
1393 __isl_take isl_space
*space
)
1400 if (!isl_space_domain_is_wrapping(space
))
1401 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1402 "domain not a product", return isl_space_free(space
));
1404 nested
= space
->nested
[0];
1405 range
= isl_space_copy(space
);
1406 range
= isl_space_drop_dims(range
, isl_dim_in
, 0, nested
->n_in
);
1408 return isl_space_free(space
);
1409 if (nested
->tuple_id
[1]) {
1410 range
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[1]);
1411 if (!range
->tuple_id
[0])
1414 if (nested
->nested
[1]) {
1415 range
->nested
[0] = isl_space_copy(nested
->nested
[1]);
1416 if (!range
->nested
[0])
1420 isl_space_free(space
);
1423 isl_space_free(space
);
1424 isl_space_free(range
);
1428 /* Internal function that selects the domain of the map that is
1429 * embedded in either a set space or the range of a map space.
1430 * In particular, given a space of the form [A -> B], return the space A.
1431 * Given a space of the form A -> [B -> C], return the space A -> B.
1433 static __isl_give isl_space
*range_factor_domain(__isl_take isl_space
*space
)
1441 nested
= space
->nested
[1];
1442 domain
= isl_space_copy(space
);
1443 domain
= isl_space_drop_dims(domain
, isl_dim_out
,
1444 nested
->n_in
, nested
->n_out
);
1446 return isl_space_free(space
);
1447 if (nested
->tuple_id
[0]) {
1448 domain
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[0]);
1449 if (!domain
->tuple_id
[1])
1452 if (nested
->nested
[0]) {
1453 domain
->nested
[1] = isl_space_copy(nested
->nested
[0]);
1454 if (!domain
->nested
[1])
1458 isl_space_free(space
);
1461 isl_space_free(space
);
1462 isl_space_free(domain
);
1466 /* Given a space of the form A -> [B -> C], return the space A -> B.
1468 __isl_give isl_space
*isl_space_range_factor_domain(
1469 __isl_take isl_space
*space
)
1473 if (!isl_space_range_is_wrapping(space
))
1474 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1475 "range not a product", return isl_space_free(space
));
1477 return range_factor_domain(space
);
1480 /* Given a space of the form [A -> B], return the space A.
1482 static __isl_give isl_space
*set_factor_domain(__isl_take isl_space
*space
)
1486 if (!isl_space_is_wrapping(space
))
1487 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1488 "not a product", return isl_space_free(space
));
1490 return range_factor_domain(space
);
1493 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1494 * Given a space of the form [A -> B], return the space A.
1496 __isl_give isl_space
*isl_space_factor_domain(__isl_take isl_space
*space
)
1500 if (isl_space_is_set(space
))
1501 return set_factor_domain(space
);
1502 space
= isl_space_domain_factor_domain(space
);
1503 space
= isl_space_range_factor_domain(space
);
1507 /* Internal function that selects the range of the map that is
1508 * embedded in either a set space or the range of a map space.
1509 * In particular, given a space of the form [A -> B], return the space B.
1510 * Given a space of the form A -> [B -> C], return the space A -> C.
1512 static __isl_give isl_space
*range_factor_range(__isl_take isl_space
*space
)
1520 nested
= space
->nested
[1];
1521 range
= isl_space_copy(space
);
1522 range
= isl_space_drop_dims(range
, isl_dim_out
, 0, nested
->n_in
);
1524 return isl_space_free(space
);
1525 if (nested
->tuple_id
[1]) {
1526 range
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[1]);
1527 if (!range
->tuple_id
[1])
1530 if (nested
->nested
[1]) {
1531 range
->nested
[1] = isl_space_copy(nested
->nested
[1]);
1532 if (!range
->nested
[1])
1536 isl_space_free(space
);
1539 isl_space_free(space
);
1540 isl_space_free(range
);
1544 /* Given a space of the form A -> [B -> C], return the space A -> C.
1546 __isl_give isl_space
*isl_space_range_factor_range(
1547 __isl_take isl_space
*space
)
1551 if (!isl_space_range_is_wrapping(space
))
1552 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1553 "range not a product", return isl_space_free(space
));
1555 return range_factor_range(space
);
1558 /* Given a space of the form [A -> B], return the space B.
1560 static __isl_give isl_space
*set_factor_range(__isl_take isl_space
*space
)
1564 if (!isl_space_is_wrapping(space
))
1565 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1566 "not a product", return isl_space_free(space
));
1568 return range_factor_range(space
);
1571 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1572 * Given a space of the form [A -> B], return the space B.
1574 __isl_give isl_space
*isl_space_factor_range(__isl_take isl_space
*space
)
1578 if (isl_space_is_set(space
))
1579 return set_factor_range(space
);
1580 space
= isl_space_domain_factor_range(space
);
1581 space
= isl_space_range_factor_range(space
);
1585 __isl_give isl_space
*isl_space_map_from_set(__isl_take isl_space
*space
)
1588 isl_id
**ids
= NULL
;
1593 ctx
= isl_space_get_ctx(space
);
1594 if (!isl_space_is_set(space
))
1595 isl_die(ctx
, isl_error_invalid
, "not a set space", goto error
);
1596 space
= isl_space_cow(space
);
1599 n_id
= space
->nparam
+ space
->n_out
+ space
->n_out
;
1600 if (n_id
> 0 && space
->ids
) {
1601 ids
= isl_calloc_array(space
->ctx
, isl_id
*, n_id
);
1604 get_ids(space
, isl_dim_param
, 0, space
->nparam
, ids
);
1605 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
1606 ids
+ space
->nparam
);
1608 space
->n_in
= space
->n_out
;
1613 space
= copy_ids(space
, isl_dim_out
, 0, space
, isl_dim_in
);
1615 isl_id_free(space
->tuple_id
[0]);
1616 space
->tuple_id
[0] = isl_id_copy(space
->tuple_id
[1]);
1617 isl_space_free(space
->nested
[0]);
1618 space
->nested
[0] = isl_space_copy(space
->nested
[1]);
1621 isl_space_free(space
);
1625 __isl_give isl_space
*isl_space_map_from_domain_and_range(
1626 __isl_take isl_space
*domain
, __isl_take isl_space
*range
)
1628 if (!domain
|| !range
)
1630 if (!isl_space_is_set(domain
))
1631 isl_die(isl_space_get_ctx(domain
), isl_error_invalid
,
1632 "domain is not a set space", goto error
);
1633 if (!isl_space_is_set(range
))
1634 isl_die(isl_space_get_ctx(range
), isl_error_invalid
,
1635 "range is not a set space", goto error
);
1636 return isl_space_join(isl_space_reverse(domain
), range
);
1638 isl_space_free(domain
);
1639 isl_space_free(range
);
1643 static __isl_give isl_space
*set_ids(__isl_take isl_space
*dim
,
1644 enum isl_dim_type type
,
1645 unsigned first
, unsigned n
, __isl_take isl_id
**ids
)
1649 for (i
= 0; i
< n
; ++i
)
1650 dim
= set_id(dim
, type
, first
+ i
, ids
[i
]);
1655 __isl_give isl_space
*isl_space_reverse(__isl_take isl_space
*dim
)
1659 isl_id
**ids
= NULL
;
1664 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
1667 dim
= isl_space_cow(dim
);
1671 id
= dim
->tuple_id
[0];
1672 dim
->tuple_id
[0] = dim
->tuple_id
[1];
1673 dim
->tuple_id
[1] = id
;
1675 nested
= dim
->nested
[0];
1676 dim
->nested
[0] = dim
->nested
[1];
1677 dim
->nested
[1] = nested
;
1680 int n_id
= dim
->n_in
+ dim
->n_out
;
1681 ids
= isl_alloc_array(dim
->ctx
, isl_id
*, n_id
);
1684 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
);
1685 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->n_in
);
1689 dim
->n_in
= dim
->n_out
;
1693 dim
= set_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
);
1694 dim
= set_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
+ dim
->n_out
);
1701 isl_space_free(dim
);
1705 __isl_give isl_space
*isl_space_drop_dims(__isl_take isl_space
*space
,
1706 enum isl_dim_type type
, unsigned first
, unsigned num
)
1714 return isl_space_reset(space
, type
);
1716 if (!valid_dim_type(type
))
1717 isl_die(space
->ctx
, isl_error_invalid
,
1718 "cannot drop dimensions of specified type", goto error
);
1720 if (first
+ num
> n(space
, type
) || first
+ num
< first
)
1721 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1722 "index out of bounds", return isl_space_free(space
));
1723 space
= isl_space_cow(space
);
1727 space
= extend_ids(space
);
1730 for (i
= 0; i
< num
; ++i
)
1731 isl_id_free(get_id(space
, type
, first
+ i
));
1732 for (i
= first
+num
; i
< n(space
, type
); ++i
)
1733 set_id(space
, type
, i
- num
, get_id(space
, type
, i
));
1736 get_ids(space
, isl_dim_in
, 0, space
->n_in
,
1737 space
->ids
+ offset(space
, isl_dim_in
) - num
);
1739 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
1740 space
->ids
+ offset(space
, isl_dim_out
) - num
);
1747 case isl_dim_param
: space
->nparam
-= num
; break;
1748 case isl_dim_in
: space
->n_in
-= num
; break;
1749 case isl_dim_out
: space
->n_out
-= num
; break;
1752 space
= isl_space_reset(space
, type
);
1753 if (type
== isl_dim_param
) {
1754 if (space
&& space
->nested
[0] &&
1755 !(space
->nested
[0] = isl_space_drop_dims(space
->nested
[0],
1756 isl_dim_param
, first
, num
)))
1758 if (space
&& space
->nested
[1] &&
1759 !(space
->nested
[1] = isl_space_drop_dims(space
->nested
[1],
1760 isl_dim_param
, first
, num
)))
1765 isl_space_free(space
);
1769 __isl_give isl_space
*isl_space_drop_inputs(__isl_take isl_space
*dim
,
1770 unsigned first
, unsigned n
)
1774 return isl_space_drop_dims(dim
, isl_dim_in
, first
, n
);
1777 __isl_give isl_space
*isl_space_drop_outputs(__isl_take isl_space
*dim
,
1778 unsigned first
, unsigned n
)
1782 return isl_space_drop_dims(dim
, isl_dim_out
, first
, n
);
1785 __isl_give isl_space
*isl_space_domain(__isl_take isl_space
*space
)
1789 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, space
->n_out
);
1790 space
= isl_space_reverse(space
);
1791 space
= mark_as_set(space
);
1795 __isl_give isl_space
*isl_space_from_domain(__isl_take isl_space
*dim
)
1799 if (!isl_space_is_set(dim
))
1800 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1801 "not a set space", goto error
);
1802 dim
= isl_space_reverse(dim
);
1803 dim
= isl_space_reset(dim
, isl_dim_out
);
1806 isl_space_free(dim
);
1810 __isl_give isl_space
*isl_space_range(__isl_take isl_space
*space
)
1814 space
= isl_space_drop_dims(space
, isl_dim_in
, 0, space
->n_in
);
1815 space
= mark_as_set(space
);
1819 __isl_give isl_space
*isl_space_from_range(__isl_take isl_space
*dim
)
1823 if (!isl_space_is_set(dim
))
1824 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1825 "not a set space", goto error
);
1826 return isl_space_reset(dim
, isl_dim_in
);
1828 isl_space_free(dim
);
1832 /* Given a map space A -> B, return the map space [A -> B] -> A.
1834 __isl_give isl_space
*isl_space_domain_map(__isl_take isl_space
*space
)
1838 domain
= isl_space_from_range(isl_space_domain(isl_space_copy(space
)));
1839 space
= isl_space_from_domain(isl_space_wrap(space
));
1840 space
= isl_space_join(space
, domain
);
1845 /* Given a map space A -> B, return the map space [A -> B] -> B.
1847 __isl_give isl_space
*isl_space_range_map(__isl_take isl_space
*space
)
1851 range
= isl_space_from_range(isl_space_range(isl_space_copy(space
)));
1852 space
= isl_space_from_domain(isl_space_wrap(space
));
1853 space
= isl_space_join(space
, range
);
1858 __isl_give isl_space
*isl_space_params(__isl_take isl_space
*space
)
1860 if (isl_space_is_params(space
))
1862 space
= isl_space_drop_dims(space
,
1863 isl_dim_in
, 0, isl_space_dim(space
, isl_dim_in
));
1864 space
= isl_space_drop_dims(space
,
1865 isl_dim_out
, 0, isl_space_dim(space
, isl_dim_out
));
1866 space
= mark_as_params(space
);
1870 __isl_give isl_space
*isl_space_set_from_params(__isl_take isl_space
*space
)
1874 if (!isl_space_is_params(space
))
1875 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1876 "not a parameter space", goto error
);
1877 return isl_space_reset(space
, isl_dim_set
);
1879 isl_space_free(space
);
1883 __isl_give isl_space
*isl_space_underlying(__isl_take isl_space
*dim
,
1891 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_id
== 0)
1892 return isl_space_reset(isl_space_reset(dim
, isl_dim_in
), isl_dim_out
);
1893 dim
= isl_space_cow(dim
);
1896 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
1900 for (i
= 0; i
< dim
->n_id
; ++i
)
1901 isl_id_free(get_id(dim
, isl_dim_out
, i
));
1903 dim
= isl_space_reset(dim
, isl_dim_in
);
1904 dim
= isl_space_reset(dim
, isl_dim_out
);
1909 /* Are the two spaces the same, including positions and names of parameters?
1911 isl_bool
isl_space_is_equal(__isl_keep isl_space
*space1
,
1912 __isl_keep isl_space
*space2
)
1916 if (!space1
|| !space2
)
1917 return isl_bool_error
;
1918 if (space1
== space2
)
1919 return isl_bool_true
;
1920 equal
= isl_space_has_equal_params(space1
, space2
);
1921 if (equal
< 0 || !equal
)
1923 return isl_space_has_equal_tuples(space1
, space2
);
1926 /* Do the tuples of "space1" correspond to those of the domain of "space2"?
1927 * That is, is "space1" eqaul to the domain of "space2", ignoring parameters.
1929 * "space2" is allowed to be a set space, in which case "space1"
1930 * should be a parameter space.
1932 isl_bool
isl_space_has_domain_tuples(__isl_keep isl_space
*space1
,
1933 __isl_keep isl_space
*space2
)
1937 is_set
= isl_space_is_set(space1
);
1938 if (is_set
< 0 || !is_set
)
1940 return isl_space_tuple_is_equal(space1
, isl_dim_set
,
1941 space2
, isl_dim_in
);
1944 /* Is space1 equal to the domain of space2?
1946 * In the internal version we also allow space2 to be the space of a set,
1947 * provided space1 is a parameter space.
1949 isl_bool
isl_space_is_domain_internal(__isl_keep isl_space
*space1
,
1950 __isl_keep isl_space
*space2
)
1952 isl_bool equal_params
;
1954 if (!space1
|| !space2
)
1955 return isl_bool_error
;
1956 equal_params
= isl_space_has_equal_params(space1
, space2
);
1957 if (equal_params
< 0 || !equal_params
)
1958 return equal_params
;
1959 return isl_space_has_domain_tuples(space1
, space2
);
1962 /* Is space1 equal to the domain of space2?
1964 isl_bool
isl_space_is_domain(__isl_keep isl_space
*space1
,
1965 __isl_keep isl_space
*space2
)
1968 return isl_bool_error
;
1969 if (!isl_space_is_map(space2
))
1970 return isl_bool_false
;
1971 return isl_space_is_domain_internal(space1
, space2
);
1974 /* Is space1 equal to the range of space2?
1976 * In the internal version, space2 is allowed to be the space of a set,
1977 * in which case it should be equal to space1.
1979 isl_bool
isl_space_is_range_internal(__isl_keep isl_space
*space1
,
1980 __isl_keep isl_space
*space2
)
1982 isl_bool equal_params
;
1984 if (!space1
|| !space2
)
1985 return isl_bool_error
;
1986 if (!isl_space_is_set(space1
))
1987 return isl_bool_false
;
1988 equal_params
= isl_space_has_equal_params(space1
, space2
);
1989 if (equal_params
< 0 || !equal_params
)
1990 return equal_params
;
1991 return isl_space_tuple_is_equal(space1
, isl_dim_set
,
1992 space2
, isl_dim_out
);
1995 /* Is space1 equal to the range of space2?
1997 isl_bool
isl_space_is_range(__isl_keep isl_space
*space1
,
1998 __isl_keep isl_space
*space2
)
2001 return isl_bool_error
;
2002 if (!isl_space_is_map(space2
))
2003 return isl_bool_false
;
2004 return isl_space_is_range_internal(space1
, space2
);
2007 /* Update "hash" by hashing in the parameters of "space".
2009 static uint32_t isl_hash_params(uint32_t hash
, __isl_keep isl_space
*space
)
2017 isl_hash_byte(hash
, space
->nparam
% 256);
2019 for (i
= 0; i
< space
->nparam
; ++i
) {
2020 id
= get_id(space
, isl_dim_param
, i
);
2021 hash
= isl_hash_id(hash
, id
);
2027 /* Update "hash" by hashing in the tuples of "space".
2028 * Changes in this function should be reflected in isl_hash_tuples_domain.
2030 static uint32_t isl_hash_tuples(uint32_t hash
, __isl_keep isl_space
*space
)
2037 isl_hash_byte(hash
, space
->n_in
% 256);
2038 isl_hash_byte(hash
, space
->n_out
% 256);
2040 id
= tuple_id(space
, isl_dim_in
);
2041 hash
= isl_hash_id(hash
, id
);
2042 id
= tuple_id(space
, isl_dim_out
);
2043 hash
= isl_hash_id(hash
, id
);
2045 hash
= isl_hash_tuples(hash
, space
->nested
[0]);
2046 hash
= isl_hash_tuples(hash
, space
->nested
[1]);
2051 /* Update "hash" by hashing in the domain tuple of "space".
2052 * The result of this function is equal to the result of applying
2053 * isl_hash_tuples to the domain of "space".
2055 static uint32_t isl_hash_tuples_domain(uint32_t hash
,
2056 __isl_keep isl_space
*space
)
2063 isl_hash_byte(hash
, 0);
2064 isl_hash_byte(hash
, space
->n_in
% 256);
2066 hash
= isl_hash_id(hash
, &isl_id_none
);
2067 id
= tuple_id(space
, isl_dim_in
);
2068 hash
= isl_hash_id(hash
, id
);
2070 hash
= isl_hash_tuples(hash
, space
->nested
[0]);
2075 /* Return a hash value that digests the tuples of "space",
2076 * i.e., that ignores the parameters.
2078 uint32_t isl_space_get_tuple_hash(__isl_keep isl_space
*space
)
2085 hash
= isl_hash_init();
2086 hash
= isl_hash_tuples(hash
, space
);
2091 uint32_t isl_space_get_hash(__isl_keep isl_space
*space
)
2098 hash
= isl_hash_init();
2099 hash
= isl_hash_params(hash
, space
);
2100 hash
= isl_hash_tuples(hash
, space
);
2105 /* Return the hash value of the domain of "space".
2106 * That is, isl_space_get_domain_hash(space) is equal to
2107 * isl_space_get_hash(isl_space_domain(space)).
2109 uint32_t isl_space_get_domain_hash(__isl_keep isl_space
*space
)
2116 hash
= isl_hash_init();
2117 hash
= isl_hash_params(hash
, space
);
2118 hash
= isl_hash_tuples_domain(hash
, space
);
2123 isl_bool
isl_space_is_wrapping(__isl_keep isl_space
*dim
)
2126 return isl_bool_error
;
2128 if (!isl_space_is_set(dim
))
2129 return isl_bool_false
;
2131 return dim
->nested
[1] != NULL
;
2134 /* Is "space" the space of a map where the domain is a wrapped map space?
2136 isl_bool
isl_space_domain_is_wrapping(__isl_keep isl_space
*space
)
2139 return isl_bool_error
;
2141 if (isl_space_is_set(space
))
2142 return isl_bool_false
;
2144 return space
->nested
[0] != NULL
;
2147 /* Is "space" the space of a map where the range is a wrapped map space?
2149 isl_bool
isl_space_range_is_wrapping(__isl_keep isl_space
*space
)
2152 return isl_bool_error
;
2154 if (isl_space_is_set(space
))
2155 return isl_bool_false
;
2157 return space
->nested
[1] != NULL
;
2160 /* Is "space" a product of two spaces?
2161 * That is, is it a wrapping set space or a map space
2162 * with wrapping domain and range?
2164 isl_bool
isl_space_is_product(__isl_keep isl_space
*space
)
2167 isl_bool is_product
;
2169 is_set
= isl_space_is_set(space
);
2171 return isl_bool_error
;
2173 return isl_space_is_wrapping(space
);
2174 is_product
= isl_space_domain_is_wrapping(space
);
2175 if (is_product
< 0 || !is_product
)
2177 return isl_space_range_is_wrapping(space
);
2180 __isl_give isl_space
*isl_space_wrap(__isl_take isl_space
*dim
)
2187 wrap
= isl_space_set_alloc(dim
->ctx
,
2188 dim
->nparam
, dim
->n_in
+ dim
->n_out
);
2190 wrap
= copy_ids(wrap
, isl_dim_param
, 0, dim
, isl_dim_param
);
2191 wrap
= copy_ids(wrap
, isl_dim_set
, 0, dim
, isl_dim_in
);
2192 wrap
= copy_ids(wrap
, isl_dim_set
, dim
->n_in
, dim
, isl_dim_out
);
2197 wrap
->nested
[1] = dim
;
2201 isl_space_free(dim
);
2205 __isl_give isl_space
*isl_space_unwrap(__isl_take isl_space
*dim
)
2212 if (!isl_space_is_wrapping(dim
))
2213 isl_die(dim
->ctx
, isl_error_invalid
, "not a wrapping space",
2216 unwrap
= isl_space_copy(dim
->nested
[1]);
2217 isl_space_free(dim
);
2221 isl_space_free(dim
);
2225 isl_bool
isl_space_is_named_or_nested(__isl_keep isl_space
*space
,
2226 enum isl_dim_type type
)
2228 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
2229 return isl_bool_false
;
2231 return isl_bool_error
;
2232 if (space
->tuple_id
[type
- isl_dim_in
])
2233 return isl_bool_true
;
2234 if (space
->nested
[type
- isl_dim_in
])
2235 return isl_bool_true
;
2236 return isl_bool_false
;
2239 isl_bool
isl_space_may_be_set(__isl_keep isl_space
*space
)
2244 return isl_bool_error
;
2245 if (isl_space_is_set(space
))
2246 return isl_bool_true
;
2247 if (isl_space_dim(space
, isl_dim_in
) != 0)
2248 return isl_bool_false
;
2249 nested
= isl_space_is_named_or_nested(space
, isl_dim_in
);
2250 if (nested
< 0 || nested
)
2251 return isl_bool_not(nested
);
2252 return isl_bool_true
;
2255 __isl_give isl_space
*isl_space_reset(__isl_take isl_space
*dim
,
2256 enum isl_dim_type type
)
2258 if (!isl_space_is_named_or_nested(dim
, type
))
2261 dim
= isl_space_cow(dim
);
2265 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
2266 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
2267 isl_space_free(dim
->nested
[type
- isl_dim_in
]);
2268 dim
->nested
[type
- isl_dim_in
] = NULL
;
2273 __isl_give isl_space
*isl_space_flatten(__isl_take isl_space
*dim
)
2277 if (!dim
->nested
[0] && !dim
->nested
[1])
2281 dim
= isl_space_reset(dim
, isl_dim_in
);
2282 if (dim
&& dim
->nested
[1])
2283 dim
= isl_space_reset(dim
, isl_dim_out
);
2288 __isl_give isl_space
*isl_space_flatten_domain(__isl_take isl_space
*space
)
2292 if (!space
->nested
[0])
2295 return isl_space_reset(space
, isl_dim_in
);
2298 __isl_give isl_space
*isl_space_flatten_range(__isl_take isl_space
*space
)
2302 if (!space
->nested
[1])
2305 return isl_space_reset(space
, isl_dim_out
);
2308 /* Replace the parameters of dst by those of src.
2310 __isl_give isl_space
*isl_space_replace_params(__isl_take isl_space
*dst
,
2311 __isl_keep isl_space
*src
)
2313 isl_bool equal_params
;
2314 enum isl_dim_type type
= isl_dim_param
;
2316 equal_params
= isl_space_has_equal_params(dst
, src
);
2317 if (equal_params
< 0)
2318 return isl_space_free(dst
);
2322 dst
= isl_space_cow(dst
);
2327 dst
= isl_space_drop_dims(dst
, type
, 0, isl_space_dim(dst
, type
));
2328 dst
= isl_space_add_dims(dst
, type
, isl_space_dim(src
, type
));
2329 dst
= copy_ids(dst
, type
, 0, src
, type
);
2333 for (i
= 0; i
<= 1; ++i
) {
2334 if (!dst
->nested
[i
])
2336 dst
->nested
[i
] = isl_space_replace_params(
2337 dst
->nested
[i
], src
);
2338 if (!dst
->nested
[i
])
2345 isl_space_free(dst
);
2349 /* Given a dimension specification "dim" of a set, create a dimension
2350 * specification for the lift of the set. In particular, the result
2351 * is of the form [dim -> local[..]], with n_local variables in the
2352 * range of the wrapped map.
2354 __isl_give isl_space
*isl_space_lift(__isl_take isl_space
*dim
, unsigned n_local
)
2356 isl_space
*local_dim
;
2361 local_dim
= isl_space_dup(dim
);
2362 local_dim
= isl_space_drop_dims(local_dim
, isl_dim_set
, 0, dim
->n_out
);
2363 local_dim
= isl_space_add_dims(local_dim
, isl_dim_set
, n_local
);
2364 local_dim
= isl_space_set_tuple_name(local_dim
, isl_dim_set
, "local");
2365 dim
= isl_space_join(isl_space_from_domain(dim
),
2366 isl_space_from_range(local_dim
));
2367 dim
= isl_space_wrap(dim
);
2368 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, "lifted");
2373 isl_bool
isl_space_can_zip(__isl_keep isl_space
*space
)
2377 is_set
= isl_space_is_set(space
);
2379 return isl_bool_error
;
2381 return isl_bool_false
;
2382 return isl_space_is_product(space
);
2385 __isl_give isl_space
*isl_space_zip(__isl_take isl_space
*dim
)
2387 isl_space
*dom
, *ran
;
2388 isl_space
*dom_dom
, *dom_ran
, *ran_dom
, *ran_ran
;
2390 if (!isl_space_can_zip(dim
))
2391 isl_die(dim
->ctx
, isl_error_invalid
, "dim cannot be zipped",
2396 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(dim
)));
2397 ran
= isl_space_unwrap(isl_space_range(dim
));
2398 dom_dom
= isl_space_domain(isl_space_copy(dom
));
2399 dom_ran
= isl_space_range(dom
);
2400 ran_dom
= isl_space_domain(isl_space_copy(ran
));
2401 ran_ran
= isl_space_range(ran
);
2402 dom
= isl_space_join(isl_space_from_domain(dom_dom
),
2403 isl_space_from_range(ran_dom
));
2404 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
2405 isl_space_from_range(ran_ran
));
2406 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
2407 isl_space_from_range(isl_space_wrap(ran
)));
2409 isl_space_free(dim
);
2413 /* Can we apply isl_space_curry to "space"?
2414 * That is, does it have a nested relation in its domain?
2416 isl_bool
isl_space_can_curry(__isl_keep isl_space
*space
)
2419 return isl_bool_error
;
2421 return !!space
->nested
[0];
2424 /* Given a space (A -> B) -> C, return the corresponding space
2427 __isl_give isl_space
*isl_space_curry(__isl_take isl_space
*space
)
2429 isl_space
*dom
, *ran
;
2430 isl_space
*dom_dom
, *dom_ran
;
2435 if (!isl_space_can_curry(space
))
2436 isl_die(space
->ctx
, isl_error_invalid
,
2437 "space cannot be curried", goto error
);
2439 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(space
)));
2440 ran
= isl_space_range(space
);
2441 dom_dom
= isl_space_domain(isl_space_copy(dom
));
2442 dom_ran
= isl_space_range(dom
);
2443 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
2444 isl_space_from_range(ran
));
2445 return isl_space_join(isl_space_from_domain(dom_dom
),
2446 isl_space_from_range(isl_space_wrap(ran
)));
2448 isl_space_free(space
);
2452 /* Can isl_space_range_curry be applied to "space"?
2453 * That is, does it have a nested relation in its range,
2454 * the domain of which is itself a nested relation?
2456 isl_bool
isl_space_can_range_curry(__isl_keep isl_space
*space
)
2461 return isl_bool_error
;
2462 can
= isl_space_range_is_wrapping(space
);
2463 if (can
< 0 || !can
)
2465 return isl_space_can_curry(space
->nested
[1]);
2468 /* Given a space A -> ((B -> C) -> D), return the corresponding space
2469 * A -> (B -> (C -> D)).
2471 __isl_give isl_space
*isl_space_range_curry(__isl_take isl_space
*space
)
2476 if (!isl_space_can_range_curry(space
))
2477 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2478 "space range cannot be curried",
2479 return isl_space_free(space
));
2481 space
= isl_space_cow(space
);
2484 space
->nested
[1] = isl_space_curry(space
->nested
[1]);
2485 if (!space
->nested
[1])
2486 return isl_space_free(space
);
2491 /* Can we apply isl_space_uncurry to "space"?
2492 * That is, does it have a nested relation in its range?
2494 isl_bool
isl_space_can_uncurry(__isl_keep isl_space
*space
)
2497 return isl_bool_error
;
2499 return !!space
->nested
[1];
2502 /* Given a space A -> (B -> C), return the corresponding space
2505 __isl_give isl_space
*isl_space_uncurry(__isl_take isl_space
*space
)
2507 isl_space
*dom
, *ran
;
2508 isl_space
*ran_dom
, *ran_ran
;
2513 if (!isl_space_can_uncurry(space
))
2514 isl_die(space
->ctx
, isl_error_invalid
,
2515 "space cannot be uncurried",
2516 return isl_space_free(space
));
2518 dom
= isl_space_domain(isl_space_copy(space
));
2519 ran
= isl_space_unwrap(isl_space_range(space
));
2520 ran_dom
= isl_space_domain(isl_space_copy(ran
));
2521 ran_ran
= isl_space_range(ran
);
2522 dom
= isl_space_join(isl_space_from_domain(dom
),
2523 isl_space_from_range(ran_dom
));
2524 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
2525 isl_space_from_range(ran_ran
));
2528 isl_bool
isl_space_has_named_params(__isl_keep isl_space
*space
)
2534 return isl_bool_error
;
2535 if (space
->nparam
== 0)
2536 return isl_bool_true
;
2537 off
= isl_space_offset(space
, isl_dim_param
);
2538 if (off
+ space
->nparam
> space
->n_id
)
2539 return isl_bool_false
;
2540 for (i
= 0; i
< space
->nparam
; ++i
)
2541 if (!space
->ids
[off
+ i
])
2542 return isl_bool_false
;
2543 return isl_bool_true
;
2546 /* Check that "space" has only named parameters, reporting an error
2549 isl_stat
isl_space_check_named_params(__isl_keep isl_space
*space
)
2553 named
= isl_space_has_named_params(space
);
2555 return isl_stat_error
;
2557 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2558 "unexpected unnamed parameters", return isl_stat_error
);
2563 /* Align the initial parameters of space1 to match the order in space2.
2565 __isl_give isl_space
*isl_space_align_params(__isl_take isl_space
*space1
,
2566 __isl_take isl_space
*space2
)
2568 isl_reordering
*exp
;
2570 if (isl_space_check_named_params(space1
) < 0 ||
2571 isl_space_check_named_params(space2
) < 0)
2574 exp
= isl_parameter_alignment_reordering(space1
, space2
);
2575 exp
= isl_reordering_extend_space(exp
, space1
);
2576 isl_space_free(space2
);
2577 space1
= isl_reordering_get_space(exp
);
2578 isl_reordering_free(exp
);
2581 isl_space_free(space1
);
2582 isl_space_free(space2
);
2586 /* Given the space of set (domain), construct a space for a map
2587 * with as domain the given space and as range the range of "model".
2589 __isl_give isl_space
*isl_space_extend_domain_with_range(
2590 __isl_take isl_space
*space
, __isl_take isl_space
*model
)
2595 space
= isl_space_from_domain(space
);
2596 space
= isl_space_add_dims(space
, isl_dim_out
,
2597 isl_space_dim(model
, isl_dim_out
));
2598 if (isl_space_has_tuple_id(model
, isl_dim_out
))
2599 space
= isl_space_set_tuple_id(space
, isl_dim_out
,
2600 isl_space_get_tuple_id(model
, isl_dim_out
));
2603 if (model
->nested
[1]) {
2604 isl_space
*nested
= isl_space_copy(model
->nested
[1]);
2605 int n_nested
, n_space
;
2606 nested
= isl_space_align_params(nested
, isl_space_copy(space
));
2607 n_nested
= isl_space_dim(nested
, isl_dim_param
);
2608 n_space
= isl_space_dim(space
, isl_dim_param
);
2609 if (n_nested
> n_space
)
2610 nested
= isl_space_drop_dims(nested
, isl_dim_param
,
2611 n_space
, n_nested
- n_space
);
2614 space
->nested
[1] = nested
;
2616 isl_space_free(model
);
2619 isl_space_free(model
);
2620 isl_space_free(space
);
2624 /* Compare the "type" dimensions of two isl_spaces.
2626 * The order is fairly arbitrary.
2628 static int isl_space_cmp_type(__isl_keep isl_space
*space1
,
2629 __isl_keep isl_space
*space2
, enum isl_dim_type type
)
2632 isl_space
*nested1
, *nested2
;
2634 if (isl_space_dim(space1
, type
) != isl_space_dim(space2
, type
))
2635 return isl_space_dim(space1
, type
) -
2636 isl_space_dim(space2
, type
);
2638 cmp
= isl_id_cmp(tuple_id(space1
, type
), tuple_id(space2
, type
));
2642 nested1
= nested(space1
, type
);
2643 nested2
= nested(space2
, type
);
2644 if (!nested1
!= !nested2
)
2645 return !nested1
- !nested2
;
2648 return isl_space_cmp(nested1
, nested2
);
2653 /* Compare two isl_spaces.
2655 * The order is fairly arbitrary.
2657 int isl_space_cmp(__isl_keep isl_space
*space1
, __isl_keep isl_space
*space2
)
2662 if (space1
== space2
)
2669 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_param
);
2672 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_in
);
2675 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_out
);
2679 if (!space1
->ids
&& !space2
->ids
)
2682 for (i
= 0; i
< n(space1
, isl_dim_param
); ++i
) {
2683 cmp
= isl_id_cmp(get_id(space1
, isl_dim_param
, i
),
2684 get_id(space2
, isl_dim_param
, i
));