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
*space
)
23 return space
? space
->ctx
: NULL
;
26 __isl_give isl_space
*isl_space_alloc(isl_ctx
*ctx
,
27 unsigned nparam
, unsigned n_in
, unsigned n_out
)
31 space
= isl_alloc_type(ctx
, struct isl_space
);
38 space
->nparam
= nparam
;
42 space
->tuple_id
[0] = NULL
;
43 space
->tuple_id
[1] = NULL
;
45 space
->nested
[0] = NULL
;
46 space
->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 /* Check that "space" is a set space.
81 isl_stat
isl_space_check_is_set(__isl_keep isl_space
*space
)
85 is_set
= isl_space_is_set(space
);
87 return isl_stat_error
;
89 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
90 "space is not a set", return isl_stat_error
);
94 /* Is the given space that of a map?
96 isl_bool
isl_space_is_map(__isl_keep isl_space
*space
)
99 return isl_bool_error
;
100 return space
->tuple_id
[0] != &isl_id_none
&&
101 space
->tuple_id
[1] != &isl_id_none
;
104 __isl_give isl_space
*isl_space_set_alloc(isl_ctx
*ctx
,
105 unsigned nparam
, unsigned dim
)
108 space
= isl_space_alloc(ctx
, nparam
, 0, dim
);
109 space
= mark_as_set(space
);
113 /* Mark the space as being that of a parameter domain, by setting
114 * both tuples to isl_id_none.
116 static __isl_give isl_space
*mark_as_params(isl_space
*space
)
120 space
= isl_space_set_tuple_id(space
, isl_dim_in
, &isl_id_none
);
121 space
= isl_space_set_tuple_id(space
, isl_dim_out
, &isl_id_none
);
125 /* Is the space that of a parameter domain?
127 isl_bool
isl_space_is_params(__isl_keep isl_space
*space
)
130 return isl_bool_error
;
131 if (space
->n_in
!= 0 || space
->nested
[0] ||
132 space
->n_out
!= 0 || space
->nested
[1])
133 return isl_bool_false
;
134 if (space
->tuple_id
[0] != &isl_id_none
)
135 return isl_bool_false
;
136 if (space
->tuple_id
[1] != &isl_id_none
)
137 return isl_bool_false
;
138 return isl_bool_true
;
141 /* Create a space for a parameter domain.
143 __isl_give isl_space
*isl_space_params_alloc(isl_ctx
*ctx
, unsigned nparam
)
146 space
= isl_space_alloc(ctx
, nparam
, 0, 0);
147 space
= mark_as_params(space
);
151 static isl_size
global_pos(__isl_keep isl_space
*space
,
152 enum isl_dim_type type
, unsigned pos
)
154 if (isl_space_check_range(space
, type
, pos
, 1) < 0)
155 return isl_size_error
;
161 return pos
+ space
->nparam
;
163 return pos
+ space
->nparam
+ space
->n_in
;
165 isl_assert(isl_space_get_ctx(space
), 0, return isl_size_error
);
167 return isl_size_error
;
170 /* Extend length of ids array to the total number of dimensions.
172 static __isl_give isl_space
*extend_ids(__isl_take isl_space
*space
)
178 dim
= isl_space_dim(space
, isl_dim_all
);
180 return isl_space_free(space
);
181 if (dim
<= space
->n_id
)
185 space
->ids
= isl_calloc_array(space
->ctx
, isl_id
*, dim
);
189 ids
= isl_realloc_array(space
->ctx
, space
->ids
, isl_id
*, dim
);
193 for (i
= space
->n_id
; i
< dim
; ++i
)
194 space
->ids
[i
] = NULL
;
201 isl_space_free(space
);
205 static __isl_give isl_space
*set_id(__isl_take isl_space
*space
,
206 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
210 space
= isl_space_cow(space
);
212 gpos
= global_pos(space
, type
, pos
);
216 if (gpos
>= space
->n_id
) {
219 space
= extend_ids(space
);
224 space
->ids
[gpos
] = id
;
229 isl_space_free(space
);
233 static __isl_keep isl_id
*get_id(__isl_keep isl_space
*space
,
234 enum isl_dim_type type
, unsigned pos
)
238 gpos
= global_pos(space
, type
, pos
);
241 if (gpos
>= space
->n_id
)
243 return space
->ids
[gpos
];
246 static unsigned offset(__isl_keep isl_space
*space
, enum isl_dim_type type
)
249 case isl_dim_param
: return 0;
250 case isl_dim_in
: return space
->nparam
;
251 case isl_dim_out
: return space
->nparam
+ space
->n_in
;
256 static unsigned n(__isl_keep isl_space
*space
, enum isl_dim_type type
)
259 case isl_dim_param
: return space
->nparam
;
260 case isl_dim_in
: return space
->n_in
;
261 case isl_dim_out
: return space
->n_out
;
263 return space
->nparam
+ space
->n_in
+ space
->n_out
;
268 isl_size
isl_space_dim(__isl_keep isl_space
*space
, enum isl_dim_type type
)
271 return isl_size_error
;
272 return n(space
, type
);
275 unsigned isl_space_offset(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
279 return offset(dim
, type
);
282 static __isl_give isl_space
*copy_ids(__isl_take isl_space
*dst
,
283 enum isl_dim_type dst_type
, unsigned offset
, __isl_keep isl_space
*src
,
284 enum isl_dim_type src_type
)
292 for (i
= 0; i
< n(src
, src_type
); ++i
) {
293 id
= get_id(src
, src_type
, i
);
296 dst
= set_id(dst
, dst_type
, offset
+ i
, isl_id_copy(id
));
303 __isl_take isl_space
*isl_space_dup(__isl_keep isl_space
*space
)
308 dup
= isl_space_alloc(space
->ctx
,
309 space
->nparam
, space
->n_in
, space
->n_out
);
312 if (space
->tuple_id
[0] &&
313 !(dup
->tuple_id
[0] = isl_id_copy(space
->tuple_id
[0])))
315 if (space
->tuple_id
[1] &&
316 !(dup
->tuple_id
[1] = isl_id_copy(space
->tuple_id
[1])))
318 if (space
->nested
[0] &&
319 !(dup
->nested
[0] = isl_space_copy(space
->nested
[0])))
321 if (space
->nested
[1] &&
322 !(dup
->nested
[1] = isl_space_copy(space
->nested
[1])))
326 dup
= copy_ids(dup
, isl_dim_param
, 0, space
, isl_dim_param
);
327 dup
= copy_ids(dup
, isl_dim_in
, 0, space
, isl_dim_in
);
328 dup
= copy_ids(dup
, isl_dim_out
, 0, space
, isl_dim_out
);
335 __isl_give isl_space
*isl_space_cow(__isl_take isl_space
*space
)
343 return isl_space_dup(space
);
346 __isl_give isl_space
*isl_space_copy(__isl_keep isl_space
*dim
)
355 __isl_null isl_space
*isl_space_free(__isl_take isl_space
*space
)
362 if (--space
->ref
> 0)
365 isl_id_free(space
->tuple_id
[0]);
366 isl_id_free(space
->tuple_id
[1]);
368 isl_space_free(space
->nested
[0]);
369 isl_space_free(space
->nested
[1]);
371 for (i
= 0; i
< space
->n_id
; ++i
)
372 isl_id_free(space
->ids
[i
]);
374 isl_ctx_deref(space
->ctx
);
381 /* Check if "s" is a valid dimension or tuple name.
382 * We currently only forbid names that look like a number.
384 * s is assumed to be non-NULL.
386 static int name_ok(isl_ctx
*ctx
, const char *s
)
391 dummy
= strtol(s
, &p
, 0);
393 isl_die(ctx
, isl_error_invalid
, "name looks like a number",
399 /* Is it possible for the given dimension type to have a tuple id?
401 static int space_can_have_id(__isl_keep isl_space
*space
,
402 enum isl_dim_type type
)
406 if (isl_space_is_params(space
))
407 isl_die(space
->ctx
, isl_error_invalid
,
408 "parameter spaces don't have tuple ids", return 0);
409 if (isl_space_is_set(space
) && type
!= isl_dim_set
)
410 isl_die(space
->ctx
, isl_error_invalid
,
411 "set spaces can only have a set id", return 0);
412 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
413 isl_die(space
->ctx
, isl_error_invalid
,
414 "only input, output and set tuples can have ids",
420 /* Does the tuple have an id?
422 isl_bool
isl_space_has_tuple_id(__isl_keep isl_space
*space
,
423 enum isl_dim_type type
)
425 if (!space_can_have_id(space
, type
))
426 return isl_bool_error
;
427 return isl_bool_ok(space
->tuple_id
[type
- isl_dim_in
] != NULL
);
430 __isl_give isl_id
*isl_space_get_tuple_id(__isl_keep isl_space
*space
,
431 enum isl_dim_type type
)
437 has_id
= isl_space_has_tuple_id(space
, type
);
441 isl_die(space
->ctx
, isl_error_invalid
,
442 "tuple has no id", return NULL
);
443 return isl_id_copy(space
->tuple_id
[type
- isl_dim_in
]);
446 __isl_give isl_space
*isl_space_set_tuple_id(__isl_take isl_space
*space
,
447 enum isl_dim_type type
, __isl_take isl_id
*id
)
449 space
= isl_space_cow(space
);
452 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
453 isl_die(space
->ctx
, isl_error_invalid
,
454 "only input, output and set tuples can have names",
457 isl_id_free(space
->tuple_id
[type
- isl_dim_in
]);
458 space
->tuple_id
[type
- isl_dim_in
] = id
;
463 isl_space_free(space
);
467 __isl_give isl_space
*isl_space_reset_tuple_id(__isl_take isl_space
*space
,
468 enum isl_dim_type type
)
470 space
= isl_space_cow(space
);
473 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
474 isl_die(space
->ctx
, isl_error_invalid
,
475 "only input, output and set tuples can have names",
478 isl_id_free(space
->tuple_id
[type
- isl_dim_in
]);
479 space
->tuple_id
[type
- isl_dim_in
] = NULL
;
483 isl_space_free(space
);
487 /* Set the id of the given dimension of "space" to "id".
488 * If the dimension already has an id, then it is replaced.
489 * If the dimension is a parameter, then we need to change it
490 * in the nested spaces (if any) as well.
492 __isl_give isl_space
*isl_space_set_dim_id(__isl_take isl_space
*space
,
493 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
495 space
= isl_space_cow(space
);
499 if (type
== isl_dim_param
) {
502 for (i
= 0; i
< 2; ++i
) {
503 if (!space
->nested
[i
])
506 isl_space_set_dim_id(space
->nested
[i
],
507 type
, pos
, isl_id_copy(id
));
508 if (!space
->nested
[i
])
513 isl_id_free(get_id(space
, type
, pos
));
514 return set_id(space
, type
, pos
, id
);
517 isl_space_free(space
);
521 /* Reset the id of the given dimension of "space".
522 * If the dimension already has an id, then it is removed.
523 * If the dimension is a parameter, then we need to reset it
524 * in the nested spaces (if any) as well.
526 __isl_give isl_space
*isl_space_reset_dim_id(__isl_take isl_space
*space
,
527 enum isl_dim_type type
, unsigned pos
)
529 space
= isl_space_cow(space
);
533 if (type
== isl_dim_param
) {
536 for (i
= 0; i
< 2; ++i
) {
537 if (!space
->nested
[i
])
540 isl_space_reset_dim_id(space
->nested
[i
],
542 if (!space
->nested
[i
])
547 isl_id_free(get_id(space
, type
, pos
));
548 return set_id(space
, type
, pos
, NULL
);
550 isl_space_free(space
);
554 isl_bool
isl_space_has_dim_id(__isl_keep isl_space
*space
,
555 enum isl_dim_type type
, unsigned pos
)
558 return isl_bool_error
;
559 return isl_bool_ok(get_id(space
, type
, pos
) != NULL
);
562 __isl_give isl_id
*isl_space_get_dim_id(__isl_keep isl_space
*space
,
563 enum isl_dim_type type
, unsigned pos
)
567 if (!get_id(space
, type
, pos
))
568 isl_die(space
->ctx
, isl_error_invalid
,
569 "dim has no id", return NULL
);
570 return isl_id_copy(get_id(space
, type
, pos
));
573 __isl_give isl_space
*isl_space_set_tuple_name(__isl_take isl_space
*space
,
574 enum isl_dim_type type
, const char *s
)
582 return isl_space_reset_tuple_id(space
, type
);
584 if (!name_ok(space
->ctx
, s
))
587 id
= isl_id_alloc(space
->ctx
, s
, NULL
);
588 return isl_space_set_tuple_id(space
, type
, id
);
590 isl_space_free(space
);
594 /* Does the tuple have a name?
596 isl_bool
isl_space_has_tuple_name(__isl_keep isl_space
*space
,
597 enum isl_dim_type type
)
601 if (!space_can_have_id(space
, type
))
602 return isl_bool_error
;
603 id
= space
->tuple_id
[type
- isl_dim_in
];
604 return isl_bool_ok(id
&& id
->name
);
607 __isl_keep
const char *isl_space_get_tuple_name(__isl_keep isl_space
*space
,
608 enum isl_dim_type type
)
613 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
615 id
= space
->tuple_id
[type
- isl_dim_in
];
616 return id
? id
->name
: NULL
;
619 __isl_give isl_space
*isl_space_set_dim_name(__isl_take isl_space
*space
,
620 enum isl_dim_type type
, unsigned pos
,
628 return isl_space_reset_dim_id(space
, type
, pos
);
629 if (!name_ok(space
->ctx
, s
))
631 id
= isl_id_alloc(space
->ctx
, s
, NULL
);
632 return isl_space_set_dim_id(space
, type
, pos
, id
);
634 isl_space_free(space
);
638 /* Does the given dimension have a name?
640 isl_bool
isl_space_has_dim_name(__isl_keep isl_space
*space
,
641 enum isl_dim_type type
, unsigned pos
)
646 return isl_bool_error
;
647 id
= get_id(space
, type
, pos
);
648 return isl_bool_ok(id
&& id
->name
);
651 __isl_keep
const char *isl_space_get_dim_name(__isl_keep isl_space
*space
,
652 enum isl_dim_type type
, unsigned pos
)
654 isl_id
*id
= get_id(space
, type
, pos
);
655 return id
? id
->name
: NULL
;
658 int isl_space_find_dim_by_id(__isl_keep isl_space
*space
,
659 enum isl_dim_type type
, __isl_keep isl_id
*id
)
665 n
= isl_space_dim(space
, type
);
669 offset
= isl_space_offset(space
, type
);
670 for (i
= 0; i
< n
&& offset
+ i
< space
->n_id
; ++i
)
671 if (space
->ids
[offset
+ i
] == id
)
677 int isl_space_find_dim_by_name(__isl_keep isl_space
*space
,
678 enum isl_dim_type type
, const char *name
)
684 n
= isl_space_dim(space
, type
);
688 offset
= isl_space_offset(space
, type
);
689 for (i
= 0; i
< n
&& offset
+ i
< space
->n_id
; ++i
) {
690 isl_id
*id
= get_id(space
, type
, i
);
691 if (id
&& id
->name
&& !strcmp(id
->name
, name
))
698 /* Reset the user pointer on all identifiers of parameters and tuples
701 __isl_give isl_space
*isl_space_reset_user(__isl_take isl_space
*space
)
711 ctx
= isl_space_get_ctx(space
);
713 for (i
= 0; i
< space
->nparam
&& i
< space
->n_id
; ++i
) {
714 if (!isl_id_get_user(space
->ids
[i
]))
716 space
= isl_space_cow(space
);
719 name
= isl_id_get_name(space
->ids
[i
]);
720 id
= isl_id_alloc(ctx
, name
, NULL
);
721 isl_id_free(space
->ids
[i
]);
724 return isl_space_free(space
);
727 for (i
= 0; i
< 2; ++i
) {
728 if (!space
->tuple_id
[i
])
730 if (!isl_id_get_user(space
->tuple_id
[i
]))
732 space
= isl_space_cow(space
);
735 name
= isl_id_get_name(space
->tuple_id
[i
]);
736 id
= isl_id_alloc(ctx
, name
, NULL
);
737 isl_id_free(space
->tuple_id
[i
]);
738 space
->tuple_id
[i
] = id
;
740 return isl_space_free(space
);
743 for (i
= 0; i
< 2; ++i
) {
744 if (!space
->nested
[i
])
746 space
= isl_space_cow(space
);
749 space
->nested
[i
] = isl_space_reset_user(space
->nested
[i
]);
750 if (!space
->nested
[i
])
751 return isl_space_free(space
);
757 static __isl_keep isl_id
*tuple_id(__isl_keep isl_space
*dim
,
758 enum isl_dim_type type
)
762 if (type
== isl_dim_in
)
763 return dim
->tuple_id
[0];
764 if (type
== isl_dim_out
)
765 return dim
->tuple_id
[1];
769 static __isl_keep isl_space
*nested(__isl_keep isl_space
*dim
,
770 enum isl_dim_type type
)
774 if (type
== isl_dim_in
)
775 return dim
->nested
[0];
776 if (type
== isl_dim_out
)
777 return dim
->nested
[1];
781 /* Are the two spaces the same, apart from positions and names of parameters?
783 isl_bool
isl_space_has_equal_tuples(__isl_keep isl_space
*space1
,
784 __isl_keep isl_space
*space2
)
786 if (!space1
|| !space2
)
787 return isl_bool_error
;
788 if (space1
== space2
)
789 return isl_bool_true
;
790 return isl_space_tuple_is_equal(space1
, isl_dim_in
,
791 space2
, isl_dim_in
) &&
792 isl_space_tuple_is_equal(space1
, isl_dim_out
,
793 space2
, isl_dim_out
);
796 /* Check if the tuple of type "type1" of "space1" is the same as
797 * the tuple of type "type2" of "space2".
799 * That is, check if the tuples have the same identifier, the same dimension
800 * and the same internal structure.
801 * The identifiers of the dimensions inside the tuples do not affect the result.
803 * Note that this function only checks the tuples themselves.
804 * If nested tuples are involved, then we need to be careful not
805 * to have result affected by possibly differing parameters
806 * in those nested tuples.
808 isl_bool
isl_space_tuple_is_equal(__isl_keep isl_space
*space1
,
809 enum isl_dim_type type1
, __isl_keep isl_space
*space2
,
810 enum isl_dim_type type2
)
813 isl_space
*nested1
, *nested2
;
815 if (!space1
|| !space2
)
816 return isl_bool_error
;
818 if (space1
== space2
&& type1
== type2
)
819 return isl_bool_true
;
821 if (n(space1
, type1
) != n(space2
, type2
))
822 return isl_bool_false
;
823 id1
= tuple_id(space1
, type1
);
824 id2
= tuple_id(space2
, type2
);
826 return isl_bool_false
;
827 if (id1
&& id1
!= id2
)
828 return isl_bool_false
;
829 nested1
= nested(space1
, type1
);
830 nested2
= nested(space2
, type2
);
831 if (!nested1
^ !nested2
)
832 return isl_bool_false
;
833 if (nested1
&& !isl_space_has_equal_tuples(nested1
, nested2
))
834 return isl_bool_false
;
835 return isl_bool_true
;
838 static isl_bool
match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
839 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
843 if (space1
== space2
&& type1
== type2
)
844 return isl_bool_true
;
846 if (!isl_space_tuple_is_equal(space1
, type1
, space2
, type2
))
847 return isl_bool_false
;
849 if (!space1
->ids
&& !space2
->ids
)
850 return isl_bool_true
;
852 for (i
= 0; i
< n(space1
, type1
); ++i
) {
853 if (get_id(space1
, type1
, i
) != get_id(space2
, type2
, i
))
854 return isl_bool_false
;
856 return isl_bool_true
;
859 /* Do "space1" and "space2" have the same parameters?
861 isl_bool
isl_space_has_equal_params(__isl_keep isl_space
*space1
,
862 __isl_keep isl_space
*space2
)
864 if (!space1
|| !space2
)
865 return isl_bool_error
;
867 return match(space1
, isl_dim_param
, space2
, isl_dim_param
);
870 /* Do "space1" and "space2" have the same identifiers for all
871 * the tuple variables?
873 isl_bool
isl_space_has_equal_ids(__isl_keep isl_space
*space1
,
874 __isl_keep isl_space
*space2
)
878 if (!space1
|| !space2
)
879 return isl_bool_error
;
881 equal
= match(space1
, isl_dim_in
, space2
, isl_dim_in
);
882 if (equal
< 0 || !equal
)
884 return match(space1
, isl_dim_out
, space2
, isl_dim_out
);
887 isl_bool
isl_space_match(__isl_keep isl_space
*space1
, enum isl_dim_type type1
,
888 __isl_keep isl_space
*space2
, enum isl_dim_type type2
)
890 if (!space1
|| !space2
)
891 return isl_bool_error
;
893 return match(space1
, type1
, space2
, type2
);
896 static void get_ids(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
897 unsigned first
, unsigned n
, __isl_keep isl_id
**ids
)
901 for (i
= 0; i
< n
; ++i
)
902 ids
[i
] = get_id(dim
, type
, first
+ i
);
905 static __isl_give isl_space
*space_extend(__isl_take isl_space
*space
,
906 unsigned nparam
, unsigned n_in
, unsigned n_out
)
912 if (space
->nparam
== nparam
&&
913 space
->n_in
== n_in
&& space
->n_out
== n_out
)
916 isl_assert(space
->ctx
, space
->nparam
<= nparam
, goto error
);
917 isl_assert(space
->ctx
, space
->n_in
<= n_in
, goto error
);
918 isl_assert(space
->ctx
, space
->n_out
<= n_out
, goto error
);
920 space
= isl_space_cow(space
);
926 n
= nparam
+ n_in
+ n_out
;
927 if (n
< nparam
|| n
< n_in
|| n
< n_out
)
928 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
929 "overflow in total number of dimensions",
931 ids
= isl_calloc_array(space
->ctx
, isl_id
*, n
);
934 get_ids(space
, isl_dim_param
, 0, space
->nparam
, ids
);
935 get_ids(space
, isl_dim_in
, 0, space
->n_in
, ids
+ nparam
);
936 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
937 ids
+ nparam
+ n_in
);
940 space
->n_id
= nparam
+ n_in
+ n_out
;
942 space
->nparam
= nparam
;
944 space
->n_out
= n_out
;
949 isl_space_free(space
);
953 __isl_give isl_space
*isl_space_extend(__isl_take isl_space
*space
,
954 unsigned nparam
, unsigned n_in
, unsigned n_out
)
956 return space_extend(space
, nparam
, n_in
, n_out
);
959 __isl_give isl_space
*isl_space_add_dims(__isl_take isl_space
*space
,
960 enum isl_dim_type type
, unsigned n
)
962 space
= isl_space_reset(space
, type
);
967 space
= space_extend(space
,
968 space
->nparam
+ n
, space
->n_in
, space
->n_out
);
969 if (space
&& space
->nested
[0] &&
970 !(space
->nested
[0] = isl_space_add_dims(space
->nested
[0],
973 if (space
&& space
->nested
[1] &&
974 !(space
->nested
[1] = isl_space_add_dims(space
->nested
[1],
979 return space_extend(space
,
980 space
->nparam
, space
->n_in
+ n
, space
->n_out
);
982 return space_extend(space
,
983 space
->nparam
, space
->n_in
, space
->n_out
+ n
);
985 isl_die(space
->ctx
, isl_error_invalid
,
986 "cannot add dimensions of specified type", goto error
);
989 isl_space_free(space
);
993 /* Add a parameter with identifier "id" to "space", provided
994 * it does not already appear in "space".
996 __isl_give isl_space
*isl_space_add_param_id(__isl_take isl_space
*space
,
997 __isl_take isl_id
*id
)
1004 if (isl_space_find_dim_by_id(space
, isl_dim_param
, id
) >= 0) {
1009 pos
= isl_space_dim(space
, isl_dim_param
);
1012 space
= isl_space_add_dims(space
, isl_dim_param
, 1);
1013 space
= isl_space_set_dim_id(space
, isl_dim_param
, pos
, id
);
1017 isl_space_free(space
);
1022 static int valid_dim_type(enum isl_dim_type type
)
1035 #define TYPE isl_space
1036 #include "check_type_range_templ.c"
1038 /* Insert "n" dimensions of type "type" at position "pos".
1039 * If we are inserting parameters, then they are also inserted in
1040 * any nested spaces.
1042 __isl_give isl_space
*isl_space_insert_dims(__isl_take isl_space
*space
,
1043 enum isl_dim_type type
, unsigned pos
, unsigned n
)
1046 isl_id
**ids
= NULL
;
1051 return isl_space_reset(space
, type
);
1053 ctx
= isl_space_get_ctx(space
);
1054 if (!valid_dim_type(type
))
1055 isl_die(ctx
, isl_error_invalid
,
1056 "cannot insert dimensions of specified type",
1059 if (isl_space_check_range(space
, type
, pos
, 0) < 0)
1060 return isl_space_free(space
);
1062 space
= isl_space_cow(space
);
1067 enum isl_dim_type t
, o
= isl_dim_param
;
1070 ids
= isl_calloc_array(ctx
, isl_id
*,
1071 space
->nparam
+ space
->n_in
+ space
->n_out
+ n
);
1075 s
[isl_dim_param
- o
] = space
->nparam
;
1076 s
[isl_dim_in
- o
] = space
->n_in
;
1077 s
[isl_dim_out
- o
] = space
->n_out
;
1078 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1080 get_ids(space
, t
, 0, s
[t
- o
], ids
+ off
);
1083 get_ids(space
, t
, 0, pos
, ids
+ off
);
1085 get_ids(space
, t
, pos
, s
[t
- o
] - pos
,
1087 off
+= s
[t
- o
] - pos
;
1092 space
->n_id
= space
->nparam
+ space
->n_in
+ space
->n_out
+ n
;
1095 case isl_dim_param
: space
->nparam
+= n
; break;
1096 case isl_dim_in
: space
->n_in
+= n
; break;
1097 case isl_dim_out
: space
->n_out
+= n
; break;
1100 space
= isl_space_reset(space
, type
);
1102 if (type
== isl_dim_param
) {
1103 if (space
&& space
->nested
[0] &&
1104 !(space
->nested
[0] = isl_space_insert_dims(space
->nested
[0],
1105 isl_dim_param
, pos
, n
)))
1107 if (space
&& space
->nested
[1] &&
1108 !(space
->nested
[1] = isl_space_insert_dims(space
->nested
[1],
1109 isl_dim_param
, pos
, n
)))
1115 isl_space_free(space
);
1119 __isl_give isl_space
*isl_space_move_dims(__isl_take isl_space
*space
,
1120 enum isl_dim_type dst_type
, unsigned dst_pos
,
1121 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1125 space
= isl_space_reset(space
, src_type
);
1126 space
= isl_space_reset(space
, dst_type
);
1132 if (isl_space_check_range(space
, src_type
, src_pos
, n
) < 0)
1133 return isl_space_free(space
);
1135 if (dst_type
== src_type
&& dst_pos
== src_pos
)
1138 isl_assert(space
->ctx
, dst_type
!= src_type
, goto error
);
1140 space
= isl_space_cow(space
);
1146 enum isl_dim_type t
, o
= isl_dim_param
;
1149 ids
= isl_calloc_array(space
->ctx
, isl_id
*,
1150 space
->nparam
+ space
->n_in
+ space
->n_out
);
1154 s
[isl_dim_param
- o
] = space
->nparam
;
1155 s
[isl_dim_in
- o
] = space
->n_in
;
1156 s
[isl_dim_out
- o
] = space
->n_out
;
1157 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1158 if (t
== dst_type
) {
1159 get_ids(space
, t
, 0, dst_pos
, ids
+ off
);
1161 get_ids(space
, src_type
, src_pos
, n
, ids
+ off
);
1163 get_ids(space
, t
, dst_pos
, s
[t
- o
] - dst_pos
,
1165 off
+= s
[t
- o
] - dst_pos
;
1166 } else if (t
== src_type
) {
1167 get_ids(space
, t
, 0, src_pos
, ids
+ off
);
1169 get_ids(space
, t
, src_pos
+ n
,
1170 s
[t
- o
] - src_pos
- n
, ids
+ off
);
1171 off
+= s
[t
- o
] - src_pos
- n
;
1173 get_ids(space
, t
, 0, s
[t
- o
], ids
+ off
);
1179 space
->n_id
= space
->nparam
+ space
->n_in
+ space
->n_out
;
1183 case isl_dim_param
: space
->nparam
+= n
; break;
1184 case isl_dim_in
: space
->n_in
+= n
; break;
1185 case isl_dim_out
: space
->n_out
+= n
; break;
1190 case isl_dim_param
: space
->nparam
-= n
; break;
1191 case isl_dim_in
: space
->n_in
-= n
; break;
1192 case isl_dim_out
: space
->n_out
-= n
; break;
1196 if (dst_type
!= isl_dim_param
&& src_type
!= isl_dim_param
)
1199 for (i
= 0; i
< 2; ++i
) {
1200 if (!space
->nested
[i
])
1202 space
->nested
[i
] = isl_space_replace_params(space
->nested
[i
],
1204 if (!space
->nested
[i
])
1210 isl_space_free(space
);
1214 /* Check that "space1" and "space2" have the same parameters,
1215 * reporting an error if they do not.
1217 isl_stat
isl_space_check_equal_params(__isl_keep isl_space
*space1
,
1218 __isl_keep isl_space
*space2
)
1222 equal
= isl_space_has_equal_params(space1
, space2
);
1224 return isl_stat_error
;
1226 isl_die(isl_space_get_ctx(space1
), isl_error_invalid
,
1227 "parameters need to match", return isl_stat_error
);
1231 __isl_give isl_space
*isl_space_join(__isl_take isl_space
*left
,
1232 __isl_take isl_space
*right
)
1236 if (isl_space_check_equal_params(left
, right
) < 0)
1239 isl_assert(left
->ctx
,
1240 isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_in
),
1243 space
= isl_space_alloc(left
->ctx
,
1244 left
->nparam
, left
->n_in
, right
->n_out
);
1248 space
= copy_ids(space
, isl_dim_param
, 0, left
, isl_dim_param
);
1249 space
= copy_ids(space
, isl_dim_in
, 0, left
, isl_dim_in
);
1250 space
= copy_ids(space
, isl_dim_out
, 0, right
, isl_dim_out
);
1252 if (space
&& left
->tuple_id
[0] &&
1253 !(space
->tuple_id
[0] = isl_id_copy(left
->tuple_id
[0])))
1255 if (space
&& right
->tuple_id
[1] &&
1256 !(space
->tuple_id
[1] = isl_id_copy(right
->tuple_id
[1])))
1258 if (space
&& left
->nested
[0] &&
1259 !(space
->nested
[0] = isl_space_copy(left
->nested
[0])))
1261 if (space
&& right
->nested
[1] &&
1262 !(space
->nested
[1] = isl_space_copy(right
->nested
[1])))
1265 isl_space_free(left
);
1266 isl_space_free(right
);
1270 isl_space_free(left
);
1271 isl_space_free(right
);
1275 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1276 * { [A -> B] -> [C -> D] }.
1277 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1279 __isl_give isl_space
*isl_space_product(__isl_take isl_space
*left
,
1280 __isl_take isl_space
*right
)
1282 isl_space
*dom1
, *dom2
, *nest1
, *nest2
;
1285 if (!left
|| !right
)
1288 is_set
= isl_space_is_set(left
);
1289 if (is_set
!= isl_space_is_set(right
))
1290 isl_die(isl_space_get_ctx(left
), isl_error_invalid
,
1291 "expecting either two set spaces or two map spaces",
1294 return isl_space_range_product(left
, right
);
1296 if (isl_space_check_equal_params(left
, right
) < 0)
1299 dom1
= isl_space_domain(isl_space_copy(left
));
1300 dom2
= isl_space_domain(isl_space_copy(right
));
1301 nest1
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1303 dom1
= isl_space_range(left
);
1304 dom2
= isl_space_range(right
);
1305 nest2
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1307 return isl_space_join(isl_space_reverse(nest1
), nest2
);
1309 isl_space_free(left
);
1310 isl_space_free(right
);
1314 /* Given two spaces { A -> C } and { B -> C }, construct the space
1317 __isl_give isl_space
*isl_space_domain_product(__isl_take isl_space
*left
,
1318 __isl_take isl_space
*right
)
1320 isl_space
*ran
, *dom1
, *dom2
, *nest
;
1322 if (isl_space_check_equal_params(left
, right
) < 0)
1325 if (!isl_space_tuple_is_equal(left
, isl_dim_out
, right
, isl_dim_out
))
1326 isl_die(left
->ctx
, isl_error_invalid
,
1327 "ranges need to match", goto error
);
1329 ran
= isl_space_range(isl_space_copy(left
));
1331 dom1
= isl_space_domain(left
);
1332 dom2
= isl_space_domain(right
);
1333 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1335 return isl_space_join(isl_space_reverse(nest
), ran
);
1337 isl_space_free(left
);
1338 isl_space_free(right
);
1342 __isl_give isl_space
*isl_space_range_product(__isl_take isl_space
*left
,
1343 __isl_take isl_space
*right
)
1345 isl_space
*dom
, *ran1
, *ran2
, *nest
;
1347 if (isl_space_check_equal_params(left
, right
) < 0)
1350 if (!isl_space_tuple_is_equal(left
, isl_dim_in
, right
, isl_dim_in
))
1351 isl_die(left
->ctx
, isl_error_invalid
,
1352 "domains need to match", goto error
);
1354 dom
= isl_space_domain(isl_space_copy(left
));
1356 ran1
= isl_space_range(left
);
1357 ran2
= isl_space_range(right
);
1358 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(ran1
), ran2
));
1360 return isl_space_join(isl_space_reverse(dom
), nest
);
1362 isl_space_free(left
);
1363 isl_space_free(right
);
1367 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1369 __isl_give isl_space
*isl_space_domain_factor_domain(
1370 __isl_take isl_space
*space
)
1377 if (!isl_space_domain_is_wrapping(space
))
1378 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1379 "domain not a product", return isl_space_free(space
));
1381 nested
= space
->nested
[0];
1382 domain
= isl_space_copy(space
);
1383 domain
= isl_space_drop_dims(domain
, isl_dim_in
,
1384 nested
->n_in
, nested
->n_out
);
1386 return isl_space_free(space
);
1387 if (nested
->tuple_id
[0]) {
1388 domain
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[0]);
1389 if (!domain
->tuple_id
[0])
1392 if (nested
->nested
[0]) {
1393 domain
->nested
[0] = isl_space_copy(nested
->nested
[0]);
1394 if (!domain
->nested
[0])
1398 isl_space_free(space
);
1401 isl_space_free(space
);
1402 isl_space_free(domain
);
1406 /* Given a space of the form [A -> B] -> C, return the space B -> C.
1408 __isl_give isl_space
*isl_space_domain_factor_range(
1409 __isl_take isl_space
*space
)
1416 if (!isl_space_domain_is_wrapping(space
))
1417 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1418 "domain not a product", return isl_space_free(space
));
1420 nested
= space
->nested
[0];
1421 range
= isl_space_copy(space
);
1422 range
= isl_space_drop_dims(range
, isl_dim_in
, 0, nested
->n_in
);
1424 return isl_space_free(space
);
1425 if (nested
->tuple_id
[1]) {
1426 range
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[1]);
1427 if (!range
->tuple_id
[0])
1430 if (nested
->nested
[1]) {
1431 range
->nested
[0] = isl_space_copy(nested
->nested
[1]);
1432 if (!range
->nested
[0])
1436 isl_space_free(space
);
1439 isl_space_free(space
);
1440 isl_space_free(range
);
1444 /* Internal function that selects the domain of the map that is
1445 * embedded in either a set space or the range of a map space.
1446 * In particular, given a space of the form [A -> B], return the space A.
1447 * Given a space of the form A -> [B -> C], return the space A -> B.
1449 static __isl_give isl_space
*range_factor_domain(__isl_take isl_space
*space
)
1457 nested
= space
->nested
[1];
1458 domain
= isl_space_copy(space
);
1459 domain
= isl_space_drop_dims(domain
, isl_dim_out
,
1460 nested
->n_in
, nested
->n_out
);
1462 return isl_space_free(space
);
1463 if (nested
->tuple_id
[0]) {
1464 domain
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[0]);
1465 if (!domain
->tuple_id
[1])
1468 if (nested
->nested
[0]) {
1469 domain
->nested
[1] = isl_space_copy(nested
->nested
[0]);
1470 if (!domain
->nested
[1])
1474 isl_space_free(space
);
1477 isl_space_free(space
);
1478 isl_space_free(domain
);
1482 /* Given a space of the form A -> [B -> C], return the space A -> B.
1484 __isl_give isl_space
*isl_space_range_factor_domain(
1485 __isl_take isl_space
*space
)
1489 if (!isl_space_range_is_wrapping(space
))
1490 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1491 "range not a product", return isl_space_free(space
));
1493 return range_factor_domain(space
);
1496 /* Given a space of the form [A -> B], return the space A.
1498 static __isl_give isl_space
*set_factor_domain(__isl_take isl_space
*space
)
1502 if (!isl_space_is_wrapping(space
))
1503 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1504 "not a product", return isl_space_free(space
));
1506 return range_factor_domain(space
);
1509 /* Given a space of the form [A -> B] -> [C -> D], return the space A -> C.
1510 * Given a space of the form [A -> B], return the space A.
1512 __isl_give isl_space
*isl_space_factor_domain(__isl_take isl_space
*space
)
1516 if (isl_space_is_set(space
))
1517 return set_factor_domain(space
);
1518 space
= isl_space_domain_factor_domain(space
);
1519 space
= isl_space_range_factor_domain(space
);
1523 /* Internal function that selects the range of the map that is
1524 * embedded in either a set space or the range of a map space.
1525 * In particular, given a space of the form [A -> B], return the space B.
1526 * Given a space of the form A -> [B -> C], return the space A -> C.
1528 static __isl_give isl_space
*range_factor_range(__isl_take isl_space
*space
)
1536 nested
= space
->nested
[1];
1537 range
= isl_space_copy(space
);
1538 range
= isl_space_drop_dims(range
, isl_dim_out
, 0, nested
->n_in
);
1540 return isl_space_free(space
);
1541 if (nested
->tuple_id
[1]) {
1542 range
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[1]);
1543 if (!range
->tuple_id
[1])
1546 if (nested
->nested
[1]) {
1547 range
->nested
[1] = isl_space_copy(nested
->nested
[1]);
1548 if (!range
->nested
[1])
1552 isl_space_free(space
);
1555 isl_space_free(space
);
1556 isl_space_free(range
);
1560 /* Given a space of the form A -> [B -> C], return the space A -> C.
1562 __isl_give isl_space
*isl_space_range_factor_range(
1563 __isl_take isl_space
*space
)
1567 if (!isl_space_range_is_wrapping(space
))
1568 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1569 "range not a product", return isl_space_free(space
));
1571 return range_factor_range(space
);
1574 /* Given a space of the form [A -> B], return the space B.
1576 static __isl_give isl_space
*set_factor_range(__isl_take isl_space
*space
)
1580 if (!isl_space_is_wrapping(space
))
1581 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1582 "not a product", return isl_space_free(space
));
1584 return range_factor_range(space
);
1587 /* Given a space of the form [A -> B] -> [C -> D], return the space B -> D.
1588 * Given a space of the form [A -> B], return the space B.
1590 __isl_give isl_space
*isl_space_factor_range(__isl_take isl_space
*space
)
1594 if (isl_space_is_set(space
))
1595 return set_factor_range(space
);
1596 space
= isl_space_domain_factor_range(space
);
1597 space
= isl_space_range_factor_range(space
);
1601 __isl_give isl_space
*isl_space_map_from_set(__isl_take isl_space
*space
)
1604 isl_id
**ids
= NULL
;
1609 ctx
= isl_space_get_ctx(space
);
1610 if (!isl_space_is_set(space
))
1611 isl_die(ctx
, isl_error_invalid
, "not a set space", goto error
);
1612 space
= isl_space_cow(space
);
1615 n_id
= space
->nparam
+ space
->n_out
+ space
->n_out
;
1616 if (n_id
> 0 && space
->ids
) {
1617 ids
= isl_calloc_array(space
->ctx
, isl_id
*, n_id
);
1620 get_ids(space
, isl_dim_param
, 0, space
->nparam
, ids
);
1621 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
1622 ids
+ space
->nparam
);
1624 space
->n_in
= space
->n_out
;
1629 space
= copy_ids(space
, isl_dim_out
, 0, space
, isl_dim_in
);
1631 isl_id_free(space
->tuple_id
[0]);
1632 space
->tuple_id
[0] = isl_id_copy(space
->tuple_id
[1]);
1633 isl_space_free(space
->nested
[0]);
1634 space
->nested
[0] = isl_space_copy(space
->nested
[1]);
1637 isl_space_free(space
);
1641 __isl_give isl_space
*isl_space_map_from_domain_and_range(
1642 __isl_take isl_space
*domain
, __isl_take isl_space
*range
)
1644 if (!domain
|| !range
)
1646 if (!isl_space_is_set(domain
))
1647 isl_die(isl_space_get_ctx(domain
), isl_error_invalid
,
1648 "domain is not a set space", goto error
);
1649 if (!isl_space_is_set(range
))
1650 isl_die(isl_space_get_ctx(range
), isl_error_invalid
,
1651 "range is not a set space", goto error
);
1652 return isl_space_join(isl_space_reverse(domain
), range
);
1654 isl_space_free(domain
);
1655 isl_space_free(range
);
1659 static __isl_give isl_space
*set_ids(__isl_take isl_space
*dim
,
1660 enum isl_dim_type type
,
1661 unsigned first
, unsigned n
, __isl_take isl_id
**ids
)
1665 for (i
= 0; i
< n
; ++i
)
1666 dim
= set_id(dim
, type
, first
+ i
, ids
[i
]);
1671 __isl_give isl_space
*isl_space_reverse(__isl_take isl_space
*space
)
1675 isl_id
**ids
= NULL
;
1680 if (match(space
, isl_dim_in
, space
, isl_dim_out
))
1683 space
= isl_space_cow(space
);
1687 id
= space
->tuple_id
[0];
1688 space
->tuple_id
[0] = space
->tuple_id
[1];
1689 space
->tuple_id
[1] = id
;
1691 nested
= space
->nested
[0];
1692 space
->nested
[0] = space
->nested
[1];
1693 space
->nested
[1] = nested
;
1696 int n_id
= space
->n_in
+ space
->n_out
;
1697 ids
= isl_alloc_array(space
->ctx
, isl_id
*, n_id
);
1700 get_ids(space
, isl_dim_in
, 0, space
->n_in
, ids
);
1701 get_ids(space
, isl_dim_out
, 0, space
->n_out
, ids
+ space
->n_in
);
1705 space
->n_in
= space
->n_out
;
1709 space
= set_ids(space
, isl_dim_out
, 0, space
->n_out
, ids
);
1710 space
= set_ids(space
, isl_dim_in
, 0, space
->n_in
,
1711 ids
+ space
->n_out
);
1718 isl_space_free(space
);
1722 __isl_give isl_space
*isl_space_drop_dims(__isl_take isl_space
*space
,
1723 enum isl_dim_type type
, unsigned first
, unsigned num
)
1731 return isl_space_reset(space
, type
);
1733 if (!valid_dim_type(type
))
1734 isl_die(space
->ctx
, isl_error_invalid
,
1735 "cannot drop dimensions of specified type", goto error
);
1737 if (isl_space_check_range(space
, type
, first
, num
) < 0)
1738 return isl_space_free(space
);
1739 space
= isl_space_cow(space
);
1743 space
= extend_ids(space
);
1746 for (i
= 0; i
< num
; ++i
)
1747 isl_id_free(get_id(space
, type
, first
+ i
));
1748 for (i
= first
+num
; i
< n(space
, type
); ++i
)
1749 set_id(space
, type
, i
- num
, get_id(space
, type
, i
));
1752 get_ids(space
, isl_dim_in
, 0, space
->n_in
,
1753 space
->ids
+ offset(space
, isl_dim_in
) - num
);
1755 get_ids(space
, isl_dim_out
, 0, space
->n_out
,
1756 space
->ids
+ offset(space
, isl_dim_out
) - num
);
1763 case isl_dim_param
: space
->nparam
-= num
; break;
1764 case isl_dim_in
: space
->n_in
-= num
; break;
1765 case isl_dim_out
: space
->n_out
-= num
; break;
1768 space
= isl_space_reset(space
, type
);
1769 if (type
== isl_dim_param
) {
1770 if (space
&& space
->nested
[0] &&
1771 !(space
->nested
[0] = isl_space_drop_dims(space
->nested
[0],
1772 isl_dim_param
, first
, num
)))
1774 if (space
&& space
->nested
[1] &&
1775 !(space
->nested
[1] = isl_space_drop_dims(space
->nested
[1],
1776 isl_dim_param
, first
, num
)))
1781 isl_space_free(space
);
1785 __isl_give isl_space
*isl_space_drop_inputs(__isl_take isl_space
*dim
,
1786 unsigned first
, unsigned n
)
1790 return isl_space_drop_dims(dim
, isl_dim_in
, first
, n
);
1793 __isl_give isl_space
*isl_space_drop_outputs(__isl_take isl_space
*dim
,
1794 unsigned first
, unsigned n
)
1798 return isl_space_drop_dims(dim
, isl_dim_out
, first
, n
);
1801 /* Remove all parameters from "space".
1803 __isl_give isl_space
*isl_space_drop_all_params(__isl_take isl_space
*space
)
1807 nparam
= isl_space_dim(space
, isl_dim_param
);
1809 return isl_space_free(space
);
1810 return isl_space_drop_dims(space
, isl_dim_param
, 0, nparam
);
1813 __isl_give isl_space
*isl_space_domain(__isl_take isl_space
*space
)
1817 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, space
->n_out
);
1818 space
= isl_space_reverse(space
);
1819 space
= mark_as_set(space
);
1823 __isl_give isl_space
*isl_space_from_domain(__isl_take isl_space
*space
)
1827 if (!isl_space_is_set(space
))
1828 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1829 "not a set space", goto error
);
1830 space
= isl_space_reverse(space
);
1831 space
= isl_space_reset(space
, isl_dim_out
);
1834 isl_space_free(space
);
1838 __isl_give isl_space
*isl_space_range(__isl_take isl_space
*space
)
1842 space
= isl_space_drop_dims(space
, isl_dim_in
, 0, space
->n_in
);
1843 space
= mark_as_set(space
);
1847 __isl_give isl_space
*isl_space_from_range(__isl_take isl_space
*space
)
1851 if (!isl_space_is_set(space
))
1852 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1853 "not a set space", goto error
);
1854 return isl_space_reset(space
, isl_dim_in
);
1856 isl_space_free(space
);
1860 /* Given a map space A -> B, return the map space [A -> B] -> A.
1862 __isl_give isl_space
*isl_space_domain_map(__isl_take isl_space
*space
)
1866 domain
= isl_space_from_range(isl_space_domain(isl_space_copy(space
)));
1867 space
= isl_space_from_domain(isl_space_wrap(space
));
1868 space
= isl_space_join(space
, domain
);
1873 /* Given a map space A -> B, return the map space [A -> B] -> B.
1875 __isl_give isl_space
*isl_space_range_map(__isl_take isl_space
*space
)
1879 range
= isl_space_from_range(isl_space_range(isl_space_copy(space
)));
1880 space
= isl_space_from_domain(isl_space_wrap(space
));
1881 space
= isl_space_join(space
, range
);
1886 __isl_give isl_space
*isl_space_params(__isl_take isl_space
*space
)
1888 isl_size n_in
, n_out
;
1890 if (isl_space_is_params(space
))
1892 n_in
= isl_space_dim(space
, isl_dim_in
);
1893 n_out
= isl_space_dim(space
, isl_dim_out
);
1894 if (n_in
< 0 || n_out
< 0)
1895 return isl_space_free(space
);
1896 space
= isl_space_drop_dims(space
, isl_dim_in
, 0, n_in
);
1897 space
= isl_space_drop_dims(space
, isl_dim_out
, 0, n_out
);
1898 space
= mark_as_params(space
);
1902 __isl_give isl_space
*isl_space_set_from_params(__isl_take isl_space
*space
)
1906 if (!isl_space_is_params(space
))
1907 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1908 "not a parameter space", goto error
);
1909 return isl_space_reset(space
, isl_dim_set
);
1911 isl_space_free(space
);
1915 __isl_give isl_space
*isl_space_underlying(__isl_take isl_space
*space
,
1921 is_set
= isl_space_is_set(space
);
1923 return isl_space_free(space
);
1924 if (n_div
== 0 && is_set
&&
1925 space
->nparam
== 0 && space
->n_in
== 0 && space
->n_id
== 0)
1926 return isl_space_reset(space
, isl_dim_out
);
1927 space
= isl_space_cow(space
);
1930 space
->n_out
+= space
->nparam
+ space
->n_in
+ n_div
;
1934 for (i
= 0; i
< space
->n_id
; ++i
)
1935 isl_id_free(get_id(space
, isl_dim_out
, i
));
1937 space
= isl_space_reset(space
, isl_dim_in
);
1938 space
= isl_space_reset(space
, isl_dim_out
);
1939 space
= mark_as_set(space
);
1944 /* Are the two spaces the same, including positions and names of parameters?
1946 isl_bool
isl_space_is_equal(__isl_keep isl_space
*space1
,
1947 __isl_keep isl_space
*space2
)
1951 if (!space1
|| !space2
)
1952 return isl_bool_error
;
1953 if (space1
== space2
)
1954 return isl_bool_true
;
1955 equal
= isl_space_has_equal_params(space1
, space2
);
1956 if (equal
< 0 || !equal
)
1958 return isl_space_has_equal_tuples(space1
, space2
);
1961 /* Do the tuples of "space1" correspond to those of the domain of "space2"?
1962 * That is, is "space1" eqaul to the domain of "space2", ignoring parameters.
1964 * "space2" is allowed to be a set space, in which case "space1"
1965 * should be a parameter space.
1967 isl_bool
isl_space_has_domain_tuples(__isl_keep isl_space
*space1
,
1968 __isl_keep isl_space
*space2
)
1972 is_set
= isl_space_is_set(space1
);
1973 if (is_set
< 0 || !is_set
)
1975 return isl_space_tuple_is_equal(space1
, isl_dim_set
,
1976 space2
, isl_dim_in
);
1979 /* Is space1 equal to the domain of space2?
1981 * In the internal version we also allow space2 to be the space of a set,
1982 * provided space1 is a parameter space.
1984 isl_bool
isl_space_is_domain_internal(__isl_keep isl_space
*space1
,
1985 __isl_keep isl_space
*space2
)
1987 isl_bool equal_params
;
1989 if (!space1
|| !space2
)
1990 return isl_bool_error
;
1991 equal_params
= isl_space_has_equal_params(space1
, space2
);
1992 if (equal_params
< 0 || !equal_params
)
1993 return equal_params
;
1994 return isl_space_has_domain_tuples(space1
, space2
);
1997 /* Is space1 equal to the domain of space2?
1999 isl_bool
isl_space_is_domain(__isl_keep isl_space
*space1
,
2000 __isl_keep isl_space
*space2
)
2003 return isl_bool_error
;
2004 if (!isl_space_is_map(space2
))
2005 return isl_bool_false
;
2006 return isl_space_is_domain_internal(space1
, space2
);
2009 /* Is space1 equal to the range of space2?
2011 * In the internal version, space2 is allowed to be the space of a set,
2012 * in which case it should be equal to space1.
2014 isl_bool
isl_space_is_range_internal(__isl_keep isl_space
*space1
,
2015 __isl_keep isl_space
*space2
)
2017 isl_bool equal_params
;
2019 if (!space1
|| !space2
)
2020 return isl_bool_error
;
2021 if (!isl_space_is_set(space1
))
2022 return isl_bool_false
;
2023 equal_params
= isl_space_has_equal_params(space1
, space2
);
2024 if (equal_params
< 0 || !equal_params
)
2025 return equal_params
;
2026 return isl_space_tuple_is_equal(space1
, isl_dim_set
,
2027 space2
, isl_dim_out
);
2030 /* Is space1 equal to the range of space2?
2032 isl_bool
isl_space_is_range(__isl_keep isl_space
*space1
,
2033 __isl_keep isl_space
*space2
)
2036 return isl_bool_error
;
2037 if (!isl_space_is_map(space2
))
2038 return isl_bool_false
;
2039 return isl_space_is_range_internal(space1
, space2
);
2042 /* Update "hash" by hashing in the parameters of "space".
2044 static uint32_t isl_hash_params(uint32_t hash
, __isl_keep isl_space
*space
)
2052 isl_hash_byte(hash
, space
->nparam
% 256);
2054 for (i
= 0; i
< space
->nparam
; ++i
) {
2055 id
= get_id(space
, isl_dim_param
, i
);
2056 hash
= isl_hash_id(hash
, id
);
2062 /* Update "hash" by hashing in the tuples of "space".
2063 * Changes in this function should be reflected in isl_hash_tuples_domain.
2065 static uint32_t isl_hash_tuples(uint32_t hash
, __isl_keep isl_space
*space
)
2072 isl_hash_byte(hash
, space
->n_in
% 256);
2073 isl_hash_byte(hash
, space
->n_out
% 256);
2075 id
= tuple_id(space
, isl_dim_in
);
2076 hash
= isl_hash_id(hash
, id
);
2077 id
= tuple_id(space
, isl_dim_out
);
2078 hash
= isl_hash_id(hash
, id
);
2080 hash
= isl_hash_tuples(hash
, space
->nested
[0]);
2081 hash
= isl_hash_tuples(hash
, space
->nested
[1]);
2086 /* Update "hash" by hashing in the domain tuple of "space".
2087 * The result of this function is equal to the result of applying
2088 * isl_hash_tuples to the domain of "space".
2090 static uint32_t isl_hash_tuples_domain(uint32_t hash
,
2091 __isl_keep isl_space
*space
)
2098 isl_hash_byte(hash
, 0);
2099 isl_hash_byte(hash
, space
->n_in
% 256);
2101 hash
= isl_hash_id(hash
, &isl_id_none
);
2102 id
= tuple_id(space
, isl_dim_in
);
2103 hash
= isl_hash_id(hash
, id
);
2105 hash
= isl_hash_tuples(hash
, space
->nested
[0]);
2110 /* Return a hash value that digests the tuples of "space",
2111 * i.e., that ignores the parameters.
2113 uint32_t isl_space_get_tuple_hash(__isl_keep isl_space
*space
)
2120 hash
= isl_hash_init();
2121 hash
= isl_hash_tuples(hash
, space
);
2126 uint32_t isl_space_get_hash(__isl_keep isl_space
*space
)
2133 hash
= isl_hash_init();
2134 hash
= isl_hash_params(hash
, space
);
2135 hash
= isl_hash_tuples(hash
, space
);
2140 /* Return the hash value of the domain of "space".
2141 * That is, isl_space_get_domain_hash(space) is equal to
2142 * isl_space_get_hash(isl_space_domain(space)).
2144 uint32_t isl_space_get_domain_hash(__isl_keep isl_space
*space
)
2151 hash
= isl_hash_init();
2152 hash
= isl_hash_params(hash
, space
);
2153 hash
= isl_hash_tuples_domain(hash
, space
);
2158 /* Is "space" the space of a set wrapping a map space?
2160 isl_bool
isl_space_is_wrapping(__isl_keep isl_space
*space
)
2163 return isl_bool_error
;
2165 if (!isl_space_is_set(space
))
2166 return isl_bool_false
;
2168 return isl_bool_ok(space
->nested
[1] != NULL
);
2171 /* Is "space" the space of a map where the domain is a wrapped map space?
2173 isl_bool
isl_space_domain_is_wrapping(__isl_keep isl_space
*space
)
2176 return isl_bool_error
;
2178 if (isl_space_is_set(space
))
2179 return isl_bool_false
;
2181 return isl_bool_ok(space
->nested
[0] != NULL
);
2184 /* Is "space" the space of a map where the range is a wrapped map space?
2186 isl_bool
isl_space_range_is_wrapping(__isl_keep isl_space
*space
)
2189 return isl_bool_error
;
2191 if (isl_space_is_set(space
))
2192 return isl_bool_false
;
2194 return isl_bool_ok(space
->nested
[1] != NULL
);
2197 /* Is "space" a product of two spaces?
2198 * That is, is it a wrapping set space or a map space
2199 * with wrapping domain and range?
2201 isl_bool
isl_space_is_product(__isl_keep isl_space
*space
)
2204 isl_bool is_product
;
2206 is_set
= isl_space_is_set(space
);
2208 return isl_bool_error
;
2210 return isl_space_is_wrapping(space
);
2211 is_product
= isl_space_domain_is_wrapping(space
);
2212 if (is_product
< 0 || !is_product
)
2214 return isl_space_range_is_wrapping(space
);
2217 __isl_give isl_space
*isl_space_wrap(__isl_take isl_space
*space
)
2224 wrap
= isl_space_set_alloc(space
->ctx
,
2225 space
->nparam
, space
->n_in
+ space
->n_out
);
2227 wrap
= copy_ids(wrap
, isl_dim_param
, 0, space
, isl_dim_param
);
2228 wrap
= copy_ids(wrap
, isl_dim_set
, 0, space
, isl_dim_in
);
2229 wrap
= copy_ids(wrap
, isl_dim_set
, space
->n_in
, space
, isl_dim_out
);
2234 wrap
->nested
[1] = space
;
2238 isl_space_free(space
);
2242 __isl_give isl_space
*isl_space_unwrap(__isl_take isl_space
*space
)
2249 if (!isl_space_is_wrapping(space
))
2250 isl_die(space
->ctx
, isl_error_invalid
, "not a wrapping space",
2253 unwrap
= isl_space_copy(space
->nested
[1]);
2254 isl_space_free(space
);
2258 isl_space_free(space
);
2262 isl_bool
isl_space_is_named_or_nested(__isl_keep isl_space
*space
,
2263 enum isl_dim_type type
)
2265 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
2266 return isl_bool_false
;
2268 return isl_bool_error
;
2269 if (space
->tuple_id
[type
- isl_dim_in
])
2270 return isl_bool_true
;
2271 if (space
->nested
[type
- isl_dim_in
])
2272 return isl_bool_true
;
2273 return isl_bool_false
;
2276 isl_bool
isl_space_may_be_set(__isl_keep isl_space
*space
)
2282 return isl_bool_error
;
2283 if (isl_space_is_set(space
))
2284 return isl_bool_true
;
2285 n_in
= isl_space_dim(space
, isl_dim_in
);
2287 return isl_bool_error
;
2289 return isl_bool_false
;
2290 nested
= isl_space_is_named_or_nested(space
, isl_dim_in
);
2291 if (nested
< 0 || nested
)
2292 return isl_bool_not(nested
);
2293 return isl_bool_true
;
2296 __isl_give isl_space
*isl_space_reset(__isl_take isl_space
*space
,
2297 enum isl_dim_type type
)
2299 if (!isl_space_is_named_or_nested(space
, type
))
2302 space
= isl_space_cow(space
);
2306 isl_id_free(space
->tuple_id
[type
- isl_dim_in
]);
2307 space
->tuple_id
[type
- isl_dim_in
] = NULL
;
2308 isl_space_free(space
->nested
[type
- isl_dim_in
]);
2309 space
->nested
[type
- isl_dim_in
] = NULL
;
2314 __isl_give isl_space
*isl_space_flatten(__isl_take isl_space
*space
)
2318 if (!space
->nested
[0] && !space
->nested
[1])
2321 if (space
->nested
[0])
2322 space
= isl_space_reset(space
, isl_dim_in
);
2323 if (space
&& space
->nested
[1])
2324 space
= isl_space_reset(space
, isl_dim_out
);
2329 __isl_give isl_space
*isl_space_flatten_domain(__isl_take isl_space
*space
)
2333 if (!space
->nested
[0])
2336 return isl_space_reset(space
, isl_dim_in
);
2339 __isl_give isl_space
*isl_space_flatten_range(__isl_take isl_space
*space
)
2343 if (!space
->nested
[1])
2346 return isl_space_reset(space
, isl_dim_out
);
2349 /* Replace the parameters of dst by those of src.
2351 __isl_give isl_space
*isl_space_replace_params(__isl_take isl_space
*dst
,
2352 __isl_keep isl_space
*src
)
2354 isl_size dst_dim
, src_dim
;
2355 isl_bool equal_params
;
2356 enum isl_dim_type type
= isl_dim_param
;
2358 equal_params
= isl_space_has_equal_params(dst
, src
);
2359 if (equal_params
< 0)
2360 return isl_space_free(dst
);
2364 dst
= isl_space_cow(dst
);
2366 dst_dim
= isl_space_dim(dst
, type
);
2367 src_dim
= isl_space_dim(src
, type
);
2368 if (dst_dim
< 0 || src_dim
< 0)
2371 dst
= isl_space_drop_dims(dst
, type
, 0, dst_dim
);
2372 dst
= isl_space_add_dims(dst
, type
, src_dim
);
2373 dst
= copy_ids(dst
, type
, 0, src
, type
);
2377 for (i
= 0; i
<= 1; ++i
) {
2378 if (!dst
->nested
[i
])
2380 dst
->nested
[i
] = isl_space_replace_params(
2381 dst
->nested
[i
], src
);
2382 if (!dst
->nested
[i
])
2389 isl_space_free(dst
);
2393 /* Given a dimension specification "dim" of a set, create a dimension
2394 * specification for the lift of the set. In particular, the result
2395 * is of the form [dim -> local[..]], with n_local variables in the
2396 * range of the wrapped map.
2398 __isl_give isl_space
*isl_space_lift(__isl_take isl_space
*space
,
2401 isl_space
*local_space
;
2406 local_space
= isl_space_dup(space
);
2407 local_space
= isl_space_drop_dims(local_space
, isl_dim_set
, 0,
2409 local_space
= isl_space_add_dims(local_space
, isl_dim_set
, n_local
);
2410 local_space
= isl_space_set_tuple_name(local_space
,
2411 isl_dim_set
, "local");
2412 space
= isl_space_join(isl_space_from_domain(space
),
2413 isl_space_from_range(local_space
));
2414 space
= isl_space_wrap(space
);
2415 space
= isl_space_set_tuple_name(space
, isl_dim_set
, "lifted");
2420 isl_bool
isl_space_can_zip(__isl_keep isl_space
*space
)
2424 is_set
= isl_space_is_set(space
);
2426 return isl_bool_error
;
2428 return isl_bool_false
;
2429 return isl_space_is_product(space
);
2432 __isl_give isl_space
*isl_space_zip(__isl_take isl_space
*space
)
2434 isl_space
*dom
, *ran
;
2435 isl_space
*dom_dom
, *dom_ran
, *ran_dom
, *ran_ran
;
2437 if (!isl_space_can_zip(space
))
2438 isl_die(space
->ctx
, isl_error_invalid
, "dim cannot be zipped",
2443 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(space
)));
2444 ran
= isl_space_unwrap(isl_space_range(space
));
2445 dom_dom
= isl_space_domain(isl_space_copy(dom
));
2446 dom_ran
= isl_space_range(dom
);
2447 ran_dom
= isl_space_domain(isl_space_copy(ran
));
2448 ran_ran
= isl_space_range(ran
);
2449 dom
= isl_space_join(isl_space_from_domain(dom_dom
),
2450 isl_space_from_range(ran_dom
));
2451 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
2452 isl_space_from_range(ran_ran
));
2453 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
2454 isl_space_from_range(isl_space_wrap(ran
)));
2456 isl_space_free(space
);
2460 /* Can we apply isl_space_curry to "space"?
2461 * That is, does is it have a map space with a nested relation in its domain?
2463 isl_bool
isl_space_can_curry(__isl_keep isl_space
*space
)
2465 return isl_space_domain_is_wrapping(space
);
2468 /* Given a space (A -> B) -> C, return the corresponding space
2471 __isl_give isl_space
*isl_space_curry(__isl_take isl_space
*space
)
2473 isl_space
*dom
, *ran
;
2474 isl_space
*dom_dom
, *dom_ran
;
2479 if (!isl_space_can_curry(space
))
2480 isl_die(space
->ctx
, isl_error_invalid
,
2481 "space cannot be curried", goto error
);
2483 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(space
)));
2484 ran
= isl_space_range(space
);
2485 dom_dom
= isl_space_domain(isl_space_copy(dom
));
2486 dom_ran
= isl_space_range(dom
);
2487 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
2488 isl_space_from_range(ran
));
2489 return isl_space_join(isl_space_from_domain(dom_dom
),
2490 isl_space_from_range(isl_space_wrap(ran
)));
2492 isl_space_free(space
);
2496 /* Can isl_space_range_curry be applied to "space"?
2497 * That is, does it have a nested relation in its range,
2498 * the domain of which is itself a nested relation?
2500 isl_bool
isl_space_can_range_curry(__isl_keep isl_space
*space
)
2505 return isl_bool_error
;
2506 can
= isl_space_range_is_wrapping(space
);
2507 if (can
< 0 || !can
)
2509 return isl_space_can_curry(space
->nested
[1]);
2512 /* Given a space A -> ((B -> C) -> D), return the corresponding space
2513 * A -> (B -> (C -> D)).
2515 __isl_give isl_space
*isl_space_range_curry(__isl_take isl_space
*space
)
2520 if (!isl_space_can_range_curry(space
))
2521 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2522 "space range cannot be curried",
2523 return isl_space_free(space
));
2525 space
= isl_space_cow(space
);
2528 space
->nested
[1] = isl_space_curry(space
->nested
[1]);
2529 if (!space
->nested
[1])
2530 return isl_space_free(space
);
2535 /* Can we apply isl_space_uncurry to "space"?
2536 * That is, does it have a map space with a nested relation in its range?
2538 isl_bool
isl_space_can_uncurry(__isl_keep isl_space
*space
)
2540 return isl_space_range_is_wrapping(space
);
2543 /* Given a space A -> (B -> C), return the corresponding space
2546 __isl_give isl_space
*isl_space_uncurry(__isl_take isl_space
*space
)
2548 isl_space
*dom
, *ran
;
2549 isl_space
*ran_dom
, *ran_ran
;
2554 if (!isl_space_can_uncurry(space
))
2555 isl_die(space
->ctx
, isl_error_invalid
,
2556 "space cannot be uncurried",
2557 return isl_space_free(space
));
2559 dom
= isl_space_domain(isl_space_copy(space
));
2560 ran
= isl_space_unwrap(isl_space_range(space
));
2561 ran_dom
= isl_space_domain(isl_space_copy(ran
));
2562 ran_ran
= isl_space_range(ran
);
2563 dom
= isl_space_join(isl_space_from_domain(dom
),
2564 isl_space_from_range(ran_dom
));
2565 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
2566 isl_space_from_range(ran_ran
));
2569 isl_bool
isl_space_has_named_params(__isl_keep isl_space
*space
)
2575 return isl_bool_error
;
2576 if (space
->nparam
== 0)
2577 return isl_bool_true
;
2578 off
= isl_space_offset(space
, isl_dim_param
);
2579 if (off
+ space
->nparam
> space
->n_id
)
2580 return isl_bool_false
;
2581 for (i
= 0; i
< space
->nparam
; ++i
)
2582 if (!space
->ids
[off
+ i
])
2583 return isl_bool_false
;
2584 return isl_bool_true
;
2587 /* Check that "space" has only named parameters, reporting an error
2590 isl_stat
isl_space_check_named_params(__isl_keep isl_space
*space
)
2594 named
= isl_space_has_named_params(space
);
2596 return isl_stat_error
;
2598 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
2599 "unexpected unnamed parameters", return isl_stat_error
);
2604 /* Align the initial parameters of space1 to match the order in space2.
2606 __isl_give isl_space
*isl_space_align_params(__isl_take isl_space
*space1
,
2607 __isl_take isl_space
*space2
)
2609 isl_reordering
*exp
;
2611 if (isl_space_check_named_params(space1
) < 0 ||
2612 isl_space_check_named_params(space2
) < 0)
2615 exp
= isl_parameter_alignment_reordering(space1
, space2
);
2616 exp
= isl_reordering_extend_space(exp
, space1
);
2617 isl_space_free(space2
);
2618 space1
= isl_reordering_get_space(exp
);
2619 isl_reordering_free(exp
);
2622 isl_space_free(space1
);
2623 isl_space_free(space2
);
2627 /* Given the space of set (domain), construct a space for a map
2628 * with as domain the given space and as range the range of "model".
2630 __isl_give isl_space
*isl_space_extend_domain_with_range(
2631 __isl_take isl_space
*space
, __isl_take isl_space
*model
)
2638 space
= isl_space_from_domain(space
);
2639 n_out
= isl_space_dim(model
, isl_dim_out
);
2642 space
= isl_space_add_dims(space
, isl_dim_out
, n_out
);
2643 if (isl_space_has_tuple_id(model
, isl_dim_out
))
2644 space
= isl_space_set_tuple_id(space
, isl_dim_out
,
2645 isl_space_get_tuple_id(model
, isl_dim_out
));
2648 if (model
->nested
[1]) {
2649 isl_space
*nested
= isl_space_copy(model
->nested
[1]);
2650 isl_size n_nested
, n_space
;
2651 nested
= isl_space_align_params(nested
, isl_space_copy(space
));
2652 n_nested
= isl_space_dim(nested
, isl_dim_param
);
2653 n_space
= isl_space_dim(space
, isl_dim_param
);
2654 if (n_nested
< 0 || n_space
< 0)
2656 if (n_nested
> n_space
)
2657 nested
= isl_space_drop_dims(nested
, isl_dim_param
,
2658 n_space
, n_nested
- n_space
);
2661 space
->nested
[1] = nested
;
2663 isl_space_free(model
);
2666 isl_space_free(model
);
2667 isl_space_free(space
);
2671 /* Compare the "type" dimensions of two isl_spaces.
2673 * The order is fairly arbitrary.
2675 static int isl_space_cmp_type(__isl_keep isl_space
*space1
,
2676 __isl_keep isl_space
*space2
, enum isl_dim_type type
)
2679 isl_size dim1
, dim2
;
2680 isl_space
*nested1
, *nested2
;
2682 dim1
= isl_space_dim(space1
, type
);
2683 dim2
= isl_space_dim(space2
, type
);
2684 if (dim1
< 0 || dim2
< 0)
2689 cmp
= isl_id_cmp(tuple_id(space1
, type
), tuple_id(space2
, type
));
2693 nested1
= nested(space1
, type
);
2694 nested2
= nested(space2
, type
);
2695 if (!nested1
!= !nested2
)
2696 return !nested1
- !nested2
;
2699 return isl_space_cmp(nested1
, nested2
);
2704 /* Compare two isl_spaces.
2706 * The order is fairly arbitrary.
2708 int isl_space_cmp(__isl_keep isl_space
*space1
, __isl_keep isl_space
*space2
)
2713 if (space1
== space2
)
2720 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_param
);
2723 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_in
);
2726 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_out
);
2730 if (!space1
->ids
&& !space2
->ids
)
2733 for (i
= 0; i
< n(space1
, isl_dim_param
); ++i
) {
2734 cmp
= isl_id_cmp(get_id(space1
, isl_dim_param
, i
),
2735 get_id(space2
, isl_dim_param
, i
));