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 int isl_space_is_set(__isl_keep isl_space
*space
)
72 if (space
->n_in
!= 0 || space
->nested
[0])
74 if (space
->tuple_id
[0] != &isl_id_none
)
79 /* Is the given space that of a map?
81 int isl_space_is_map(__isl_keep isl_space
*space
)
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 int isl_space_is_params(__isl_keep isl_space
*space
)
116 if (space
->n_in
!= 0 || space
->nested
[0] ||
117 space
->n_out
!= 0 || space
->nested
[1])
119 if (space
->tuple_id
[0] != &isl_id_none
)
121 if (space
->tuple_id
[1] != &isl_id_none
)
126 /* Create a space for a parameter domain.
128 __isl_give isl_space
*isl_space_params_alloc(isl_ctx
*ctx
, unsigned nparam
)
131 space
= isl_space_alloc(ctx
, nparam
, 0, 0);
132 space
= mark_as_params(space
);
136 static unsigned global_pos(__isl_keep isl_space
*dim
,
137 enum isl_dim_type type
, unsigned pos
)
139 struct isl_ctx
*ctx
= dim
->ctx
;
143 isl_assert(ctx
, pos
< dim
->nparam
,
144 return isl_space_dim(dim
, isl_dim_all
));
147 isl_assert(ctx
, pos
< dim
->n_in
,
148 return isl_space_dim(dim
, isl_dim_all
));
149 return pos
+ dim
->nparam
;
151 isl_assert(ctx
, pos
< dim
->n_out
,
152 return isl_space_dim(dim
, isl_dim_all
));
153 return pos
+ dim
->nparam
+ dim
->n_in
;
155 isl_assert(ctx
, 0, return isl_space_dim(dim
, isl_dim_all
));
157 return isl_space_dim(dim
, isl_dim_all
);
160 /* Extend length of ids array to the total number of dimensions.
162 static __isl_give isl_space
*extend_ids(__isl_take isl_space
*dim
)
167 if (isl_space_dim(dim
, isl_dim_all
) <= dim
->n_id
)
171 dim
->ids
= isl_calloc_array(dim
->ctx
,
172 isl_id
*, isl_space_dim(dim
, isl_dim_all
));
176 ids
= isl_realloc_array(dim
->ctx
, dim
->ids
,
177 isl_id
*, isl_space_dim(dim
, isl_dim_all
));
181 for (i
= dim
->n_id
; i
< isl_space_dim(dim
, isl_dim_all
); ++i
)
185 dim
->n_id
= isl_space_dim(dim
, isl_dim_all
);
193 static __isl_give isl_space
*set_id(__isl_take isl_space
*dim
,
194 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
196 dim
= isl_space_cow(dim
);
201 pos
= global_pos(dim
, type
, pos
);
202 if (pos
== isl_space_dim(dim
, isl_dim_all
))
205 if (pos
>= dim
->n_id
) {
208 dim
= extend_ids(dim
);
222 static __isl_keep isl_id
*get_id(__isl_keep isl_space
*dim
,
223 enum isl_dim_type type
, unsigned pos
)
228 pos
= global_pos(dim
, type
, pos
);
229 if (pos
== isl_space_dim(dim
, isl_dim_all
))
231 if (pos
>= dim
->n_id
)
233 return dim
->ids
[pos
];
236 static unsigned offset(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
239 case isl_dim_param
: return 0;
240 case isl_dim_in
: return dim
->nparam
;
241 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
246 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
249 case isl_dim_param
: return dim
->nparam
;
250 case isl_dim_in
: return dim
->n_in
;
251 case isl_dim_out
: return dim
->n_out
;
252 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
257 unsigned isl_space_dim(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
264 unsigned isl_space_offset(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
268 return offset(dim
, type
);
271 static __isl_give isl_space
*copy_ids(__isl_take isl_space
*dst
,
272 enum isl_dim_type dst_type
, unsigned offset
, __isl_keep isl_space
*src
,
273 enum isl_dim_type src_type
)
281 for (i
= 0; i
< n(src
, src_type
); ++i
) {
282 id
= get_id(src
, src_type
, i
);
285 dst
= set_id(dst
, dst_type
, offset
+ i
, isl_id_copy(id
));
292 __isl_take isl_space
*isl_space_dup(__isl_keep isl_space
*dim
)
297 dup
= isl_space_alloc(dim
->ctx
, dim
->nparam
, dim
->n_in
, dim
->n_out
);
300 if (dim
->tuple_id
[0] &&
301 !(dup
->tuple_id
[0] = isl_id_copy(dim
->tuple_id
[0])))
303 if (dim
->tuple_id
[1] &&
304 !(dup
->tuple_id
[1] = isl_id_copy(dim
->tuple_id
[1])))
306 if (dim
->nested
[0] && !(dup
->nested
[0] = isl_space_copy(dim
->nested
[0])))
308 if (dim
->nested
[1] && !(dup
->nested
[1] = isl_space_copy(dim
->nested
[1])))
312 dup
= copy_ids(dup
, isl_dim_param
, 0, dim
, isl_dim_param
);
313 dup
= copy_ids(dup
, isl_dim_in
, 0, dim
, isl_dim_in
);
314 dup
= copy_ids(dup
, isl_dim_out
, 0, dim
, isl_dim_out
);
321 __isl_give isl_space
*isl_space_cow(__isl_take isl_space
*dim
)
329 return isl_space_dup(dim
);
332 __isl_give isl_space
*isl_space_copy(__isl_keep isl_space
*dim
)
341 __isl_null isl_space
*isl_space_free(__isl_take isl_space
*space
)
348 if (--space
->ref
> 0)
351 isl_id_free(space
->tuple_id
[0]);
352 isl_id_free(space
->tuple_id
[1]);
354 isl_space_free(space
->nested
[0]);
355 isl_space_free(space
->nested
[1]);
357 for (i
= 0; i
< space
->n_id
; ++i
)
358 isl_id_free(space
->ids
[i
]);
360 isl_ctx_deref(space
->ctx
);
367 /* Check if "s" is a valid dimension or tuple name.
368 * We currently only forbid names that look like a number.
370 * s is assumed to be non-NULL.
372 static int name_ok(isl_ctx
*ctx
, const char *s
)
377 dummy
= strtol(s
, &p
, 0);
379 isl_die(ctx
, isl_error_invalid
, "name looks like a number",
385 /* Is it possible for the given dimension type to have a tuple id?
387 static int space_can_have_id(__isl_keep isl_space
*space
,
388 enum isl_dim_type type
)
392 if (isl_space_is_params(space
))
393 isl_die(space
->ctx
, isl_error_invalid
,
394 "parameter spaces don't have tuple ids", return 0);
395 if (isl_space_is_set(space
) && type
!= isl_dim_set
)
396 isl_die(space
->ctx
, isl_error_invalid
,
397 "set spaces can only have a set id", return 0);
398 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
399 isl_die(space
->ctx
, isl_error_invalid
,
400 "only input, output and set tuples can have ids",
406 /* Does the tuple have an id?
408 int isl_space_has_tuple_id(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
410 if (!space_can_have_id(dim
, type
))
412 return dim
->tuple_id
[type
- isl_dim_in
] != NULL
;
415 __isl_give isl_id
*isl_space_get_tuple_id(__isl_keep isl_space
*dim
,
416 enum isl_dim_type type
)
422 has_id
= isl_space_has_tuple_id(dim
, type
);
426 isl_die(dim
->ctx
, isl_error_invalid
,
427 "tuple has no id", return NULL
);
428 return isl_id_copy(dim
->tuple_id
[type
- isl_dim_in
]);
431 __isl_give isl_space
*isl_space_set_tuple_id(__isl_take isl_space
*dim
,
432 enum isl_dim_type type
, __isl_take isl_id
*id
)
434 dim
= isl_space_cow(dim
);
437 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
438 isl_die(dim
->ctx
, isl_error_invalid
,
439 "only input, output and set tuples can have names",
442 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
443 dim
->tuple_id
[type
- isl_dim_in
] = id
;
452 __isl_give isl_space
*isl_space_reset_tuple_id(__isl_take isl_space
*dim
,
453 enum isl_dim_type type
)
455 dim
= isl_space_cow(dim
);
458 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
459 isl_die(dim
->ctx
, isl_error_invalid
,
460 "only input, output and set tuples can have names",
463 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
464 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
472 /* Set the id of the given dimension of "space" to "id".
473 * If the dimension already has an id, then it is replaced.
474 * If the dimension is a parameter, then we need to change it
475 * in the nested spaces (if any) as well.
477 __isl_give isl_space
*isl_space_set_dim_id(__isl_take isl_space
*space
,
478 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
480 space
= isl_space_cow(space
);
484 if (type
== isl_dim_param
) {
487 for (i
= 0; i
< 2; ++i
) {
488 if (!space
->nested
[i
])
491 isl_space_set_dim_id(space
->nested
[i
],
492 type
, pos
, isl_id_copy(id
));
493 if (!space
->nested
[i
])
498 isl_id_free(get_id(space
, type
, pos
));
499 return set_id(space
, type
, pos
, id
);
502 isl_space_free(space
);
506 /* Reset the id of the given dimension of "space".
507 * If the dimension already has an id, then it is removed.
508 * If the dimension is a parameter, then we need to reset it
509 * in the nested spaces (if any) as well.
511 __isl_give isl_space
*isl_space_reset_dim_id(__isl_take isl_space
*space
,
512 enum isl_dim_type type
, unsigned pos
)
514 space
= isl_space_cow(space
);
518 if (type
== isl_dim_param
) {
521 for (i
= 0; i
< 2; ++i
) {
522 if (!space
->nested
[i
])
525 isl_space_reset_dim_id(space
->nested
[i
],
527 if (!space
->nested
[i
])
532 isl_id_free(get_id(space
, type
, pos
));
533 return set_id(space
, type
, pos
, NULL
);
535 isl_space_free(space
);
539 int isl_space_has_dim_id(__isl_keep isl_space
*dim
,
540 enum isl_dim_type type
, unsigned pos
)
544 return get_id(dim
, type
, pos
) != NULL
;
547 __isl_give isl_id
*isl_space_get_dim_id(__isl_keep isl_space
*dim
,
548 enum isl_dim_type type
, unsigned pos
)
552 if (!get_id(dim
, type
, pos
))
553 isl_die(dim
->ctx
, isl_error_invalid
,
554 "dim has no id", return NULL
);
555 return isl_id_copy(get_id(dim
, type
, pos
));
558 __isl_give isl_space
*isl_space_set_tuple_name(__isl_take isl_space
*dim
,
559 enum isl_dim_type type
, const char *s
)
567 return isl_space_reset_tuple_id(dim
, type
);
569 if (!name_ok(dim
->ctx
, s
))
572 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
573 return isl_space_set_tuple_id(dim
, type
, id
);
579 /* Does the tuple have a name?
581 int isl_space_has_tuple_name(__isl_keep isl_space
*space
,
582 enum isl_dim_type type
)
586 if (!space_can_have_id(space
, type
))
588 id
= space
->tuple_id
[type
- isl_dim_in
];
589 return id
&& id
->name
;
592 const char *isl_space_get_tuple_name(__isl_keep isl_space
*dim
,
593 enum isl_dim_type type
)
598 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
600 id
= dim
->tuple_id
[type
- isl_dim_in
];
601 return id
? id
->name
: NULL
;
604 __isl_give isl_space
*isl_space_set_dim_name(__isl_take isl_space
*dim
,
605 enum isl_dim_type type
, unsigned pos
,
613 return isl_space_reset_dim_id(dim
, type
, pos
);
614 if (!name_ok(dim
->ctx
, s
))
616 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
617 return isl_space_set_dim_id(dim
, type
, pos
, id
);
623 /* Does the given dimension have a name?
625 int isl_space_has_dim_name(__isl_keep isl_space
*space
,
626 enum isl_dim_type type
, unsigned pos
)
632 id
= get_id(space
, type
, pos
);
633 return id
&& id
->name
;
636 __isl_keep
const char *isl_space_get_dim_name(__isl_keep isl_space
*dim
,
637 enum isl_dim_type type
, unsigned pos
)
639 isl_id
*id
= get_id(dim
, type
, pos
);
640 return id
? id
->name
: NULL
;
643 int isl_space_find_dim_by_id(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
644 __isl_keep isl_id
*id
)
653 offset
= isl_space_offset(dim
, type
);
654 n
= isl_space_dim(dim
, type
);
655 for (i
= 0; i
< n
&& offset
+ i
< dim
->n_id
; ++i
)
656 if (dim
->ids
[offset
+ i
] == id
)
662 int isl_space_find_dim_by_name(__isl_keep isl_space
*space
,
663 enum isl_dim_type type
, const char *name
)
672 offset
= isl_space_offset(space
, type
);
673 n
= isl_space_dim(space
, type
);
674 for (i
= 0; i
< n
&& offset
+ i
< space
->n_id
; ++i
)
675 if (space
->ids
[offset
+ i
]->name
&&
676 !strcmp(space
->ids
[offset
+ i
]->name
, name
))
682 /* Reset the user pointer on all identifiers of parameters and tuples
685 __isl_give isl_space
*isl_space_reset_user(__isl_take isl_space
*space
)
695 ctx
= isl_space_get_ctx(space
);
697 for (i
= 0; i
< space
->nparam
&& i
< space
->n_id
; ++i
) {
698 if (!isl_id_get_user(space
->ids
[i
]))
700 space
= isl_space_cow(space
);
703 name
= isl_id_get_name(space
->ids
[i
]);
704 id
= isl_id_alloc(ctx
, name
, NULL
);
705 isl_id_free(space
->ids
[i
]);
708 return isl_space_free(space
);
711 for (i
= 0; i
< 2; ++i
) {
712 if (!space
->tuple_id
[i
])
714 if (!isl_id_get_user(space
->tuple_id
[i
]))
716 space
= isl_space_cow(space
);
719 name
= isl_id_get_name(space
->tuple_id
[i
]);
720 id
= isl_id_alloc(ctx
, name
, NULL
);
721 isl_id_free(space
->tuple_id
[i
]);
722 space
->tuple_id
[i
] = id
;
724 return isl_space_free(space
);
727 for (i
= 0; i
< 2; ++i
) {
728 if (!space
->nested
[i
])
730 space
= isl_space_cow(space
);
733 space
->nested
[i
] = isl_space_reset_user(space
->nested
[i
]);
734 if (!space
->nested
[i
])
735 return isl_space_free(space
);
741 static __isl_keep isl_id
*tuple_id(__isl_keep isl_space
*dim
,
742 enum isl_dim_type type
)
746 if (type
== isl_dim_in
)
747 return dim
->tuple_id
[0];
748 if (type
== isl_dim_out
)
749 return dim
->tuple_id
[1];
753 static __isl_keep isl_space
*nested(__isl_keep isl_space
*dim
,
754 enum isl_dim_type type
)
758 if (type
== isl_dim_in
)
759 return dim
->nested
[0];
760 if (type
== isl_dim_out
)
761 return dim
->nested
[1];
765 int isl_space_tuple_match(__isl_keep isl_space
*dim1
, enum isl_dim_type dim1_type
,
766 __isl_keep isl_space
*dim2
, enum isl_dim_type dim2_type
)
769 isl_space
*nested1
, *nested2
;
774 if (dim1
== dim2
&& dim1_type
== dim2_type
)
777 if (n(dim1
, dim1_type
) != n(dim2
, dim2_type
))
779 id1
= tuple_id(dim1
, dim1_type
);
780 id2
= tuple_id(dim2
, dim2_type
);
783 if (id1
&& id1
!= id2
)
785 nested1
= nested(dim1
, dim1_type
);
786 nested2
= nested(dim2
, dim2_type
);
787 if (!nested1
^ !nested2
)
789 if (nested1
&& !isl_space_is_equal(nested1
, nested2
))
794 static int match(__isl_keep isl_space
*dim1
, enum isl_dim_type dim1_type
,
795 __isl_keep isl_space
*dim2
, enum isl_dim_type dim2_type
)
799 if (dim1
== dim2
&& dim1_type
== dim2_type
)
802 if (!isl_space_tuple_match(dim1
, dim1_type
, dim2
, dim2_type
))
805 if (!dim1
->ids
&& !dim2
->ids
)
808 for (i
= 0; i
< n(dim1
, dim1_type
); ++i
) {
809 if (get_id(dim1
, dim1_type
, i
) != get_id(dim2
, dim2_type
, i
))
815 int isl_space_match(__isl_keep isl_space
*dim1
, enum isl_dim_type dim1_type
,
816 __isl_keep isl_space
*dim2
, enum isl_dim_type dim2_type
)
821 return match(dim1
, dim1_type
, dim2
, dim2_type
);
824 static void get_ids(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
825 unsigned first
, unsigned n
, __isl_keep isl_id
**ids
)
829 for (i
= 0; i
< n
; ++i
)
830 ids
[i
] = get_id(dim
, type
, first
+ i
);
833 __isl_give isl_space
*isl_space_extend(__isl_take isl_space
*dim
,
834 unsigned nparam
, unsigned n_in
, unsigned n_out
)
840 if (dim
->nparam
== nparam
&& dim
->n_in
== n_in
&& dim
->n_out
== n_out
)
843 isl_assert(dim
->ctx
, dim
->nparam
<= nparam
, goto error
);
844 isl_assert(dim
->ctx
, dim
->n_in
<= n_in
, goto error
);
845 isl_assert(dim
->ctx
, dim
->n_out
<= n_out
, goto error
);
847 dim
= isl_space_cow(dim
);
852 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
853 nparam
+ n_in
+ n_out
);
856 get_ids(dim
, isl_dim_param
, 0, dim
->nparam
, ids
);
857 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
+ nparam
);
858 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ nparam
+ n_in
);
861 dim
->n_id
= nparam
+ n_in
+ n_out
;
863 dim
->nparam
= nparam
;
874 __isl_give isl_space
*isl_space_add_dims(__isl_take isl_space
*dim
,
875 enum isl_dim_type type
, unsigned n
)
879 dim
= isl_space_reset(dim
, type
);
882 dim
= isl_space_extend(dim
,
883 dim
->nparam
+ n
, dim
->n_in
, dim
->n_out
);
884 if (dim
&& dim
->nested
[0] &&
885 !(dim
->nested
[0] = isl_space_add_dims(dim
->nested
[0],
888 if (dim
&& dim
->nested
[1] &&
889 !(dim
->nested
[1] = isl_space_add_dims(dim
->nested
[1],
894 return isl_space_extend(dim
,
895 dim
->nparam
, dim
->n_in
+ n
, dim
->n_out
);
897 return isl_space_extend(dim
,
898 dim
->nparam
, dim
->n_in
, dim
->n_out
+ n
);
900 isl_die(dim
->ctx
, isl_error_invalid
,
901 "cannot add dimensions of specified type", goto error
);
908 static int valid_dim_type(enum isl_dim_type type
)
920 /* Insert "n" dimensions of type "type" at position "pos".
921 * If we are inserting parameters, then they are also inserted in
924 __isl_give isl_space
*isl_space_insert_dims(__isl_take isl_space
*dim
,
925 enum isl_dim_type type
, unsigned pos
, unsigned n
)
932 return isl_space_reset(dim
, type
);
934 if (!valid_dim_type(type
))
935 isl_die(dim
->ctx
, isl_error_invalid
,
936 "cannot insert dimensions of specified type",
939 isl_assert(dim
->ctx
, pos
<= isl_space_dim(dim
, type
), goto error
);
941 dim
= isl_space_cow(dim
);
946 enum isl_dim_type t
, o
= isl_dim_param
;
949 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
950 dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
);
954 s
[isl_dim_param
- o
] = dim
->nparam
;
955 s
[isl_dim_in
- o
] = dim
->n_in
;
956 s
[isl_dim_out
- o
] = dim
->n_out
;
957 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
959 get_ids(dim
, t
, 0, s
[t
- o
], ids
+ off
);
962 get_ids(dim
, t
, 0, pos
, ids
+ off
);
964 get_ids(dim
, t
, pos
, s
[t
- o
] - pos
, ids
+ off
);
965 off
+= s
[t
- o
] - pos
;
970 dim
->n_id
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
;
973 case isl_dim_param
: dim
->nparam
+= n
; break;
974 case isl_dim_in
: dim
->n_in
+= n
; break;
975 case isl_dim_out
: dim
->n_out
+= n
; break;
978 dim
= isl_space_reset(dim
, type
);
980 if (type
== isl_dim_param
) {
981 if (dim
&& dim
->nested
[0] &&
982 !(dim
->nested
[0] = isl_space_insert_dims(dim
->nested
[0],
983 isl_dim_param
, pos
, n
)))
985 if (dim
&& dim
->nested
[1] &&
986 !(dim
->nested
[1] = isl_space_insert_dims(dim
->nested
[1],
987 isl_dim_param
, pos
, n
)))
997 __isl_give isl_space
*isl_space_move_dims(__isl_take isl_space
*dim
,
998 enum isl_dim_type dst_type
, unsigned dst_pos
,
999 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
1006 dim
= isl_space_reset(dim
, src_type
);
1007 return isl_space_reset(dim
, dst_type
);
1010 isl_assert(dim
->ctx
, src_pos
+ n
<= isl_space_dim(dim
, src_type
),
1013 if (dst_type
== src_type
&& dst_pos
== src_pos
)
1016 isl_assert(dim
->ctx
, dst_type
!= src_type
, goto error
);
1018 dim
= isl_space_reset(dim
, src_type
);
1019 dim
= isl_space_reset(dim
, dst_type
);
1021 dim
= isl_space_cow(dim
);
1027 enum isl_dim_type t
, o
= isl_dim_param
;
1030 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
1031 dim
->nparam
+ dim
->n_in
+ dim
->n_out
);
1035 s
[isl_dim_param
- o
] = dim
->nparam
;
1036 s
[isl_dim_in
- o
] = dim
->n_in
;
1037 s
[isl_dim_out
- o
] = dim
->n_out
;
1038 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
1039 if (t
== dst_type
) {
1040 get_ids(dim
, t
, 0, dst_pos
, ids
+ off
);
1042 get_ids(dim
, src_type
, src_pos
, n
, ids
+ off
);
1044 get_ids(dim
, t
, dst_pos
, s
[t
- o
] - dst_pos
,
1046 off
+= s
[t
- o
] - dst_pos
;
1047 } else if (t
== src_type
) {
1048 get_ids(dim
, t
, 0, src_pos
, ids
+ off
);
1050 get_ids(dim
, t
, src_pos
+ n
,
1051 s
[t
- o
] - src_pos
- n
, ids
+ off
);
1052 off
+= s
[t
- o
] - src_pos
- n
;
1054 get_ids(dim
, t
, 0, s
[t
- o
], ids
+ off
);
1060 dim
->n_id
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
1064 case isl_dim_param
: dim
->nparam
+= n
; break;
1065 case isl_dim_in
: dim
->n_in
+= n
; break;
1066 case isl_dim_out
: dim
->n_out
+= n
; break;
1071 case isl_dim_param
: dim
->nparam
-= n
; break;
1072 case isl_dim_in
: dim
->n_in
-= n
; break;
1073 case isl_dim_out
: dim
->n_out
-= n
; break;
1077 if (dst_type
!= isl_dim_param
&& src_type
!= isl_dim_param
)
1080 for (i
= 0; i
< 2; ++i
) {
1081 if (!dim
->nested
[i
])
1083 dim
->nested
[i
] = isl_space_replace(dim
->nested
[i
],
1084 isl_dim_param
, dim
);
1085 if (!dim
->nested
[i
])
1091 isl_space_free(dim
);
1095 __isl_give isl_space
*isl_space_join(__isl_take isl_space
*left
,
1096 __isl_take isl_space
*right
)
1100 if (!left
|| !right
)
1103 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
1105 isl_assert(left
->ctx
,
1106 isl_space_tuple_match(left
, isl_dim_out
, right
, isl_dim_in
),
1109 dim
= isl_space_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
1113 dim
= copy_ids(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
1114 dim
= copy_ids(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
1115 dim
= copy_ids(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
1117 if (dim
&& left
->tuple_id
[0] &&
1118 !(dim
->tuple_id
[0] = isl_id_copy(left
->tuple_id
[0])))
1120 if (dim
&& right
->tuple_id
[1] &&
1121 !(dim
->tuple_id
[1] = isl_id_copy(right
->tuple_id
[1])))
1123 if (dim
&& left
->nested
[0] &&
1124 !(dim
->nested
[0] = isl_space_copy(left
->nested
[0])))
1126 if (dim
&& right
->nested
[1] &&
1127 !(dim
->nested
[1] = isl_space_copy(right
->nested
[1])))
1130 isl_space_free(left
);
1131 isl_space_free(right
);
1135 isl_space_free(left
);
1136 isl_space_free(right
);
1140 /* Given two map spaces { A -> C } and { B -> D }, construct the space
1141 * { [A -> B] -> [C -> D] }.
1142 * Given two set spaces { A } and { B }, construct the space { [A -> B] }.
1144 __isl_give isl_space
*isl_space_product(__isl_take isl_space
*left
,
1145 __isl_take isl_space
*right
)
1147 isl_space
*dom1
, *dom2
, *nest1
, *nest2
;
1150 if (!left
|| !right
)
1153 is_set
= isl_space_is_set(left
);
1154 if (is_set
!= isl_space_is_set(right
))
1155 isl_die(isl_space_get_ctx(left
), isl_error_invalid
,
1156 "expecting either two set spaces or two map spaces",
1159 return isl_space_range_product(left
, right
);
1161 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
1164 dom1
= isl_space_domain(isl_space_copy(left
));
1165 dom2
= isl_space_domain(isl_space_copy(right
));
1166 nest1
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1168 dom1
= isl_space_range(left
);
1169 dom2
= isl_space_range(right
);
1170 nest2
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1172 return isl_space_join(isl_space_reverse(nest1
), nest2
);
1174 isl_space_free(left
);
1175 isl_space_free(right
);
1179 /* Given two spaces { A -> C } and { B -> C }, construct the space
1182 __isl_give isl_space
*isl_space_domain_product(__isl_take isl_space
*left
,
1183 __isl_take isl_space
*right
)
1185 isl_space
*ran
, *dom1
, *dom2
, *nest
;
1187 if (!left
|| !right
)
1190 if (!match(left
, isl_dim_param
, right
, isl_dim_param
))
1191 isl_die(left
->ctx
, isl_error_invalid
,
1192 "parameters need to match", goto error
);
1193 if (!isl_space_tuple_match(left
, isl_dim_out
, right
, isl_dim_out
))
1194 isl_die(left
->ctx
, isl_error_invalid
,
1195 "ranges need to match", goto error
);
1197 ran
= isl_space_range(isl_space_copy(left
));
1199 dom1
= isl_space_domain(left
);
1200 dom2
= isl_space_domain(right
);
1201 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1203 return isl_space_join(isl_space_reverse(nest
), ran
);
1205 isl_space_free(left
);
1206 isl_space_free(right
);
1210 __isl_give isl_space
*isl_space_range_product(__isl_take isl_space
*left
,
1211 __isl_take isl_space
*right
)
1213 isl_space
*dom
, *ran1
, *ran2
, *nest
;
1215 if (!left
|| !right
)
1218 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
1220 if (!isl_space_tuple_match(left
, isl_dim_in
, right
, isl_dim_in
))
1221 isl_die(left
->ctx
, isl_error_invalid
,
1222 "domains need to match", goto error
);
1224 dom
= isl_space_domain(isl_space_copy(left
));
1226 ran1
= isl_space_range(left
);
1227 ran2
= isl_space_range(right
);
1228 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(ran1
), ran2
));
1230 return isl_space_join(isl_space_reverse(dom
), nest
);
1232 isl_space_free(left
);
1233 isl_space_free(right
);
1237 /* Given a space of the form [A -> B] -> C, return the space A -> C.
1239 __isl_give isl_space
*isl_space_domain_factor_domain(
1240 __isl_take isl_space
*space
)
1247 if (!isl_space_domain_is_wrapping(space
))
1248 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1249 "domain not a product", return isl_space_free(space
));
1251 nested
= space
->nested
[0];
1252 domain
= isl_space_copy(space
);
1253 domain
= isl_space_drop_dims(space
, isl_dim_in
,
1254 nested
->n_in
, nested
->n_out
);
1256 return isl_space_free(space
);
1257 if (nested
->tuple_id
[0]) {
1258 domain
->tuple_id
[0] = isl_id_copy(nested
->tuple_id
[0]);
1259 if (!domain
->tuple_id
[0])
1262 if (nested
->nested
[0]) {
1263 domain
->nested
[0] = isl_space_copy(nested
->nested
[0]);
1264 if (!domain
->nested
[0])
1268 isl_space_free(space
);
1271 isl_space_free(space
);
1272 isl_space_free(domain
);
1276 /* Given a space of the form A -> [B -> C], return the space A -> B.
1278 __isl_give isl_space
*isl_space_range_factor_domain(
1279 __isl_take isl_space
*space
)
1286 if (!isl_space_range_is_wrapping(space
))
1287 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1288 "range not a product", return isl_space_free(space
));
1290 nested
= space
->nested
[1];
1291 domain
= isl_space_copy(space
);
1292 domain
= isl_space_drop_dims(space
, isl_dim_out
,
1293 nested
->n_in
, nested
->n_out
);
1295 return isl_space_free(space
);
1296 if (nested
->tuple_id
[0]) {
1297 domain
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[0]);
1298 if (!domain
->tuple_id
[1])
1301 if (nested
->nested
[0]) {
1302 domain
->nested
[1] = isl_space_copy(nested
->nested
[0]);
1303 if (!domain
->nested
[1])
1307 isl_space_free(space
);
1310 isl_space_free(space
);
1311 isl_space_free(domain
);
1315 /* Given a space of the form A -> [B -> C], return the space A -> C.
1317 __isl_give isl_space
*isl_space_range_factor_range(
1318 __isl_take isl_space
*space
)
1325 if (!isl_space_range_is_wrapping(space
))
1326 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1327 "range not a product", return isl_space_free(space
));
1329 nested
= space
->nested
[1];
1330 range
= isl_space_copy(space
);
1331 range
= isl_space_drop_dims(space
, isl_dim_out
, 0, nested
->n_in
);
1333 return isl_space_free(space
);
1334 if (nested
->tuple_id
[1]) {
1335 range
->tuple_id
[1] = isl_id_copy(nested
->tuple_id
[1]);
1336 if (!range
->tuple_id
[1])
1339 if (nested
->nested
[1]) {
1340 range
->nested
[1] = isl_space_copy(nested
->nested
[1]);
1341 if (!range
->nested
[1])
1345 isl_space_free(space
);
1348 isl_space_free(space
);
1349 isl_space_free(range
);
1353 __isl_give isl_space
*isl_space_map_from_set(__isl_take isl_space
*dim
)
1356 isl_id
**ids
= NULL
;
1360 ctx
= isl_space_get_ctx(dim
);
1361 if (!isl_space_is_set(dim
))
1362 isl_die(ctx
, isl_error_invalid
, "not a set space", goto error
);
1363 dim
= isl_space_cow(dim
);
1367 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
1368 dim
->nparam
+ dim
->n_out
+ dim
->n_out
);
1371 get_ids(dim
, isl_dim_param
, 0, dim
->nparam
, ids
);
1372 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->nparam
);
1374 dim
->n_in
= dim
->n_out
;
1378 dim
->n_id
= dim
->nparam
+ dim
->n_out
+ dim
->n_out
;
1379 dim
= copy_ids(dim
, isl_dim_out
, 0, dim
, isl_dim_in
);
1381 isl_id_free(dim
->tuple_id
[0]);
1382 dim
->tuple_id
[0] = isl_id_copy(dim
->tuple_id
[1]);
1383 isl_space_free(dim
->nested
[0]);
1384 dim
->nested
[0] = isl_space_copy(dim
->nested
[1]);
1387 isl_space_free(dim
);
1391 __isl_give isl_space
*isl_space_map_from_domain_and_range(
1392 __isl_take isl_space
*domain
, __isl_take isl_space
*range
)
1394 if (!domain
|| !range
)
1396 if (!isl_space_is_set(domain
))
1397 isl_die(isl_space_get_ctx(domain
), isl_error_invalid
,
1398 "domain is not a set space", goto error
);
1399 if (!isl_space_is_set(range
))
1400 isl_die(isl_space_get_ctx(range
), isl_error_invalid
,
1401 "range is not a set space", goto error
);
1402 return isl_space_join(isl_space_reverse(domain
), range
);
1404 isl_space_free(domain
);
1405 isl_space_free(range
);
1409 static __isl_give isl_space
*set_ids(__isl_take isl_space
*dim
,
1410 enum isl_dim_type type
,
1411 unsigned first
, unsigned n
, __isl_take isl_id
**ids
)
1415 for (i
= 0; i
< n
; ++i
)
1416 dim
= set_id(dim
, type
, first
+ i
, ids
[i
]);
1421 __isl_give isl_space
*isl_space_reverse(__isl_take isl_space
*dim
)
1425 isl_id
**ids
= NULL
;
1430 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
1433 dim
= isl_space_cow(dim
);
1437 id
= dim
->tuple_id
[0];
1438 dim
->tuple_id
[0] = dim
->tuple_id
[1];
1439 dim
->tuple_id
[1] = id
;
1441 nested
= dim
->nested
[0];
1442 dim
->nested
[0] = dim
->nested
[1];
1443 dim
->nested
[1] = nested
;
1446 int n_id
= dim
->n_in
+ dim
->n_out
;
1447 ids
= isl_alloc_array(dim
->ctx
, isl_id
*, n_id
);
1450 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
);
1451 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->n_in
);
1455 dim
->n_in
= dim
->n_out
;
1459 dim
= set_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
);
1460 dim
= set_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
+ dim
->n_out
);
1467 isl_space_free(dim
);
1471 __isl_give isl_space
*isl_space_drop_dims(__isl_take isl_space
*dim
,
1472 enum isl_dim_type type
, unsigned first
, unsigned num
)
1480 return isl_space_reset(dim
, type
);
1482 if (!valid_dim_type(type
))
1483 isl_die(dim
->ctx
, isl_error_invalid
,
1484 "cannot drop dimensions of specified type", goto error
);
1486 if (first
+ num
> n(dim
, type
) || first
+ num
< first
)
1487 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1488 "index out of bounds", return isl_space_free(dim
));
1489 dim
= isl_space_cow(dim
);
1493 dim
= extend_ids(dim
);
1496 for (i
= 0; i
< num
; ++i
)
1497 isl_id_free(get_id(dim
, type
, first
+ i
));
1498 for (i
= first
+num
; i
< n(dim
, type
); ++i
)
1499 set_id(dim
, type
, i
- num
, get_id(dim
, type
, i
));
1502 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
,
1503 dim
->ids
+ offset(dim
, isl_dim_in
) - num
);
1505 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
,
1506 dim
->ids
+ offset(dim
, isl_dim_out
) - num
);
1513 case isl_dim_param
: dim
->nparam
-= num
; break;
1514 case isl_dim_in
: dim
->n_in
-= num
; break;
1515 case isl_dim_out
: dim
->n_out
-= num
; break;
1518 dim
= isl_space_reset(dim
, type
);
1519 if (type
== isl_dim_param
) {
1520 if (dim
&& dim
->nested
[0] &&
1521 !(dim
->nested
[0] = isl_space_drop_dims(dim
->nested
[0],
1522 isl_dim_param
, first
, num
)))
1524 if (dim
&& dim
->nested
[1] &&
1525 !(dim
->nested
[1] = isl_space_drop_dims(dim
->nested
[1],
1526 isl_dim_param
, first
, num
)))
1531 isl_space_free(dim
);
1535 __isl_give isl_space
*isl_space_drop_inputs(__isl_take isl_space
*dim
,
1536 unsigned first
, unsigned n
)
1540 return isl_space_drop_dims(dim
, isl_dim_in
, first
, n
);
1543 __isl_give isl_space
*isl_space_drop_outputs(__isl_take isl_space
*dim
,
1544 unsigned first
, unsigned n
)
1548 return isl_space_drop_dims(dim
, isl_dim_out
, first
, n
);
1551 __isl_give isl_space
*isl_space_domain(__isl_take isl_space
*dim
)
1555 dim
= isl_space_drop_outputs(dim
, 0, dim
->n_out
);
1556 dim
= isl_space_reverse(dim
);
1557 dim
= mark_as_set(dim
);
1561 __isl_give isl_space
*isl_space_from_domain(__isl_take isl_space
*dim
)
1565 if (!isl_space_is_set(dim
))
1566 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1567 "not a set space", goto error
);
1568 dim
= isl_space_reverse(dim
);
1569 dim
= isl_space_reset(dim
, isl_dim_out
);
1572 isl_space_free(dim
);
1576 __isl_give isl_space
*isl_space_range(__isl_take isl_space
*dim
)
1580 dim
= isl_space_drop_inputs(dim
, 0, dim
->n_in
);
1581 dim
= mark_as_set(dim
);
1585 __isl_give isl_space
*isl_space_from_range(__isl_take isl_space
*dim
)
1589 if (!isl_space_is_set(dim
))
1590 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1591 "not a set space", goto error
);
1592 return isl_space_reset(dim
, isl_dim_in
);
1594 isl_space_free(dim
);
1598 /* Given a map space A -> B, return the map space [A -> B] -> A.
1600 __isl_give isl_space
*isl_space_domain_map(__isl_take isl_space
*space
)
1604 domain
= isl_space_from_range(isl_space_domain(isl_space_copy(space
)));
1605 space
= isl_space_from_domain(isl_space_wrap(space
));
1606 space
= isl_space_join(space
, domain
);
1611 /* Given a map space A -> B, return the map space [A -> B] -> B.
1613 __isl_give isl_space
*isl_space_range_map(__isl_take isl_space
*space
)
1617 range
= isl_space_from_range(isl_space_range(isl_space_copy(space
)));
1618 space
= isl_space_from_domain(isl_space_wrap(space
));
1619 space
= isl_space_join(space
, range
);
1624 __isl_give isl_space
*isl_space_params(__isl_take isl_space
*space
)
1626 if (isl_space_is_params(space
))
1628 space
= isl_space_drop_dims(space
,
1629 isl_dim_in
, 0, isl_space_dim(space
, isl_dim_in
));
1630 space
= isl_space_drop_dims(space
,
1631 isl_dim_out
, 0, isl_space_dim(space
, isl_dim_out
));
1632 space
= mark_as_params(space
);
1636 __isl_give isl_space
*isl_space_set_from_params(__isl_take isl_space
*space
)
1640 if (!isl_space_is_params(space
))
1641 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1642 "not a parameter space", goto error
);
1643 return isl_space_reset(space
, isl_dim_set
);
1645 isl_space_free(space
);
1649 __isl_give isl_space
*isl_space_as_set_space(__isl_take isl_space
*dim
)
1651 dim
= isl_space_cow(dim
);
1655 dim
->n_out
+= dim
->n_in
;
1657 dim
= isl_space_reset(dim
, isl_dim_in
);
1658 dim
= isl_space_reset(dim
, isl_dim_out
);
1663 __isl_give isl_space
*isl_space_underlying(__isl_take isl_space
*dim
,
1671 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_id
== 0)
1672 return isl_space_reset(isl_space_reset(dim
, isl_dim_in
), isl_dim_out
);
1673 dim
= isl_space_cow(dim
);
1676 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
1680 for (i
= 0; i
< dim
->n_id
; ++i
)
1681 isl_id_free(get_id(dim
, isl_dim_out
, i
));
1683 dim
= isl_space_reset(dim
, isl_dim_in
);
1684 dim
= isl_space_reset(dim
, isl_dim_out
);
1689 /* Are the two spaces the same, including positions and names of parameters?
1691 int isl_space_is_equal(__isl_keep isl_space
*dim1
, __isl_keep isl_space
*dim2
)
1697 return match(dim1
, isl_dim_param
, dim2
, isl_dim_param
) &&
1698 isl_space_tuple_match(dim1
, isl_dim_in
, dim2
, isl_dim_in
) &&
1699 isl_space_tuple_match(dim1
, isl_dim_out
, dim2
, isl_dim_out
);
1702 /* Is space1 equal to the domain of space2?
1704 * In the internal version we also allow space2 to be the space of a set,
1705 * provided space1 is a parameter space.
1707 int isl_space_is_domain_internal(__isl_keep isl_space
*space1
,
1708 __isl_keep isl_space
*space2
)
1710 if (!space1
|| !space2
)
1712 if (!isl_space_is_set(space1
))
1714 return match(space1
, isl_dim_param
, space2
, isl_dim_param
) &&
1715 isl_space_tuple_match(space1
, isl_dim_set
, space2
, isl_dim_in
);
1718 /* Is space1 equal to the domain of space2?
1720 int isl_space_is_domain(__isl_keep isl_space
*space1
,
1721 __isl_keep isl_space
*space2
)
1725 if (!isl_space_is_map(space2
))
1727 return isl_space_is_domain_internal(space1
, space2
);
1730 /* Is space1 equal to the range of space2?
1732 * In the internal version, space2 is allowed to be the space of a set,
1733 * in which case it should be equal to space1.
1735 int isl_space_is_range_internal(__isl_keep isl_space
*space1
,
1736 __isl_keep isl_space
*space2
)
1738 if (!space1
|| !space2
)
1740 if (!isl_space_is_set(space1
))
1742 return match(space1
, isl_dim_param
, space2
, isl_dim_param
) &&
1743 isl_space_tuple_match(space1
, isl_dim_set
, space2
, isl_dim_out
);
1746 /* Is space1 equal to the range of space2?
1748 int isl_space_is_range(__isl_keep isl_space
*space1
,
1749 __isl_keep isl_space
*space2
)
1753 if (!isl_space_is_map(space2
))
1755 return isl_space_is_range_internal(space1
, space2
);
1758 int isl_space_compatible(__isl_keep isl_space
*dim1
,
1759 __isl_keep isl_space
*dim2
)
1761 return dim1
->nparam
== dim2
->nparam
&&
1762 dim1
->n_in
+ dim1
->n_out
== dim2
->n_in
+ dim2
->n_out
;
1765 static uint32_t isl_hash_dim(uint32_t hash
, __isl_keep isl_space
*dim
)
1773 isl_hash_byte(hash
, dim
->nparam
% 256);
1774 isl_hash_byte(hash
, dim
->n_in
% 256);
1775 isl_hash_byte(hash
, dim
->n_out
% 256);
1777 for (i
= 0; i
< dim
->nparam
; ++i
) {
1778 id
= get_id(dim
, isl_dim_param
, i
);
1779 hash
= isl_hash_id(hash
, id
);
1782 id
= tuple_id(dim
, isl_dim_in
);
1783 hash
= isl_hash_id(hash
, id
);
1784 id
= tuple_id(dim
, isl_dim_out
);
1785 hash
= isl_hash_id(hash
, id
);
1787 hash
= isl_hash_dim(hash
, dim
->nested
[0]);
1788 hash
= isl_hash_dim(hash
, dim
->nested
[1]);
1793 uint32_t isl_space_get_hash(__isl_keep isl_space
*dim
)
1800 hash
= isl_hash_init();
1801 hash
= isl_hash_dim(hash
, dim
);
1806 int isl_space_is_wrapping(__isl_keep isl_space
*dim
)
1811 if (!isl_space_is_set(dim
))
1814 return dim
->nested
[1] != NULL
;
1817 /* Is "space" the space of a map where the domain is a wrapped map space?
1819 int isl_space_domain_is_wrapping(__isl_keep isl_space
*space
)
1824 if (isl_space_is_set(space
))
1827 return space
->nested
[0] != NULL
;
1830 /* Is "space" the space of a map where the range is a wrapped map space?
1832 int isl_space_range_is_wrapping(__isl_keep isl_space
*space
)
1837 if (isl_space_is_set(space
))
1840 return space
->nested
[1] != NULL
;
1843 __isl_give isl_space
*isl_space_wrap(__isl_take isl_space
*dim
)
1850 wrap
= isl_space_set_alloc(dim
->ctx
,
1851 dim
->nparam
, dim
->n_in
+ dim
->n_out
);
1853 wrap
= copy_ids(wrap
, isl_dim_param
, 0, dim
, isl_dim_param
);
1854 wrap
= copy_ids(wrap
, isl_dim_set
, 0, dim
, isl_dim_in
);
1855 wrap
= copy_ids(wrap
, isl_dim_set
, dim
->n_in
, dim
, isl_dim_out
);
1860 wrap
->nested
[1] = dim
;
1864 isl_space_free(dim
);
1868 __isl_give isl_space
*isl_space_unwrap(__isl_take isl_space
*dim
)
1875 if (!isl_space_is_wrapping(dim
))
1876 isl_die(dim
->ctx
, isl_error_invalid
, "not a wrapping space",
1879 unwrap
= isl_space_copy(dim
->nested
[1]);
1880 isl_space_free(dim
);
1884 isl_space_free(dim
);
1888 int isl_space_is_named_or_nested(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
1890 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
1894 if (dim
->tuple_id
[type
- isl_dim_in
])
1896 if (dim
->nested
[type
- isl_dim_in
])
1901 int isl_space_may_be_set(__isl_keep isl_space
*dim
)
1905 if (isl_space_is_set(dim
))
1907 if (isl_space_dim(dim
, isl_dim_in
) != 0)
1909 if (isl_space_is_named_or_nested(dim
, isl_dim_in
))
1914 __isl_give isl_space
*isl_space_reset(__isl_take isl_space
*dim
,
1915 enum isl_dim_type type
)
1917 if (!isl_space_is_named_or_nested(dim
, type
))
1920 dim
= isl_space_cow(dim
);
1924 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
1925 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
1926 isl_space_free(dim
->nested
[type
- isl_dim_in
]);
1927 dim
->nested
[type
- isl_dim_in
] = NULL
;
1932 __isl_give isl_space
*isl_space_flatten(__isl_take isl_space
*dim
)
1936 if (!dim
->nested
[0] && !dim
->nested
[1])
1940 dim
= isl_space_reset(dim
, isl_dim_in
);
1941 if (dim
&& dim
->nested
[1])
1942 dim
= isl_space_reset(dim
, isl_dim_out
);
1947 __isl_give isl_space
*isl_space_flatten_domain(__isl_take isl_space
*dim
)
1951 if (!dim
->nested
[0])
1954 return isl_space_reset(dim
, isl_dim_in
);
1957 __isl_give isl_space
*isl_space_flatten_range(__isl_take isl_space
*dim
)
1961 if (!dim
->nested
[1])
1964 return isl_space_reset(dim
, isl_dim_out
);
1967 /* Replace the dimensions of the given type of dst by those of src.
1969 __isl_give isl_space
*isl_space_replace(__isl_take isl_space
*dst
,
1970 enum isl_dim_type type
, __isl_keep isl_space
*src
)
1972 dst
= isl_space_cow(dst
);
1977 dst
= isl_space_drop_dims(dst
, type
, 0, isl_space_dim(dst
, type
));
1978 dst
= isl_space_add_dims(dst
, type
, isl_space_dim(src
, type
));
1979 dst
= copy_ids(dst
, type
, 0, src
, type
);
1981 if (dst
&& type
== isl_dim_param
) {
1983 for (i
= 0; i
<= 1; ++i
) {
1984 if (!dst
->nested
[i
])
1986 dst
->nested
[i
] = isl_space_replace(dst
->nested
[i
],
1988 if (!dst
->nested
[i
])
1995 isl_space_free(dst
);
1999 /* Given a dimension specification "dim" of a set, create a dimension
2000 * specification for the lift of the set. In particular, the result
2001 * is of the form [dim -> local[..]], with n_local variables in the
2002 * range of the wrapped map.
2004 __isl_give isl_space
*isl_space_lift(__isl_take isl_space
*dim
, unsigned n_local
)
2006 isl_space
*local_dim
;
2011 local_dim
= isl_space_dup(dim
);
2012 local_dim
= isl_space_drop_dims(local_dim
, isl_dim_set
, 0, dim
->n_out
);
2013 local_dim
= isl_space_add_dims(local_dim
, isl_dim_set
, n_local
);
2014 local_dim
= isl_space_set_tuple_name(local_dim
, isl_dim_set
, "local");
2015 dim
= isl_space_join(isl_space_from_domain(dim
),
2016 isl_space_from_range(local_dim
));
2017 dim
= isl_space_wrap(dim
);
2018 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, "lifted");
2023 int isl_space_can_zip(__isl_keep isl_space
*dim
)
2028 return dim
->nested
[0] && dim
->nested
[1];
2031 __isl_give isl_space
*isl_space_zip(__isl_take isl_space
*dim
)
2033 isl_space
*dom
, *ran
;
2034 isl_space
*dom_dom
, *dom_ran
, *ran_dom
, *ran_ran
;
2036 if (!isl_space_can_zip(dim
))
2037 isl_die(dim
->ctx
, isl_error_invalid
, "dim cannot be zipped",
2042 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(dim
)));
2043 ran
= isl_space_unwrap(isl_space_range(dim
));
2044 dom_dom
= isl_space_domain(isl_space_copy(dom
));
2045 dom_ran
= isl_space_range(dom
);
2046 ran_dom
= isl_space_domain(isl_space_copy(ran
));
2047 ran_ran
= isl_space_range(ran
);
2048 dom
= isl_space_join(isl_space_from_domain(dom_dom
),
2049 isl_space_from_range(ran_dom
));
2050 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
2051 isl_space_from_range(ran_ran
));
2052 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
2053 isl_space_from_range(isl_space_wrap(ran
)));
2055 isl_space_free(dim
);
2059 /* Can we apply isl_space_curry to "space"?
2060 * That is, does it have a nested relation in its domain?
2062 int isl_space_can_curry(__isl_keep isl_space
*space
)
2067 return !!space
->nested
[0];
2070 /* Given a space (A -> B) -> C, return the corresponding space
2073 __isl_give isl_space
*isl_space_curry(__isl_take isl_space
*space
)
2075 isl_space
*dom
, *ran
;
2076 isl_space
*dom_dom
, *dom_ran
;
2081 if (!isl_space_can_curry(space
))
2082 isl_die(space
->ctx
, isl_error_invalid
,
2083 "space cannot be curried", goto error
);
2085 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(space
)));
2086 ran
= isl_space_range(space
);
2087 dom_dom
= isl_space_domain(isl_space_copy(dom
));
2088 dom_ran
= isl_space_range(dom
);
2089 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
2090 isl_space_from_range(ran
));
2091 return isl_space_join(isl_space_from_domain(dom_dom
),
2092 isl_space_from_range(isl_space_wrap(ran
)));
2094 isl_space_free(space
);
2098 /* Can we apply isl_space_uncurry to "space"?
2099 * That is, does it have a nested relation in its range?
2101 int isl_space_can_uncurry(__isl_keep isl_space
*space
)
2106 return !!space
->nested
[1];
2109 /* Given a space A -> (B -> C), return the corresponding space
2112 __isl_give isl_space
*isl_space_uncurry(__isl_take isl_space
*space
)
2114 isl_space
*dom
, *ran
;
2115 isl_space
*ran_dom
, *ran_ran
;
2120 if (!isl_space_can_uncurry(space
))
2121 isl_die(space
->ctx
, isl_error_invalid
,
2122 "space cannot be uncurried",
2123 return isl_space_free(space
));
2125 dom
= isl_space_domain(isl_space_copy(space
));
2126 ran
= isl_space_unwrap(isl_space_range(space
));
2127 ran_dom
= isl_space_domain(isl_space_copy(ran
));
2128 ran_ran
= isl_space_range(ran
);
2129 dom
= isl_space_join(isl_space_from_domain(dom
),
2130 isl_space_from_range(ran_dom
));
2131 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
2132 isl_space_from_range(ran_ran
));
2135 int isl_space_has_named_params(__isl_keep isl_space
*dim
)
2142 if (dim
->nparam
== 0)
2144 off
= isl_space_offset(dim
, isl_dim_param
);
2145 if (off
+ dim
->nparam
> dim
->n_id
)
2147 for (i
= 0; i
< dim
->nparam
; ++i
)
2148 if (!dim
->ids
[off
+ i
])
2153 /* Align the initial parameters of dim1 to match the order in dim2.
2155 __isl_give isl_space
*isl_space_align_params(__isl_take isl_space
*dim1
,
2156 __isl_take isl_space
*dim2
)
2158 isl_reordering
*exp
;
2160 if (!isl_space_has_named_params(dim1
) || !isl_space_has_named_params(dim2
))
2161 isl_die(isl_space_get_ctx(dim1
), isl_error_invalid
,
2162 "parameter alignment requires named parameters",
2165 dim2
= isl_space_params(dim2
);
2166 exp
= isl_parameter_alignment_reordering(dim1
, dim2
);
2167 exp
= isl_reordering_extend_space(exp
, dim1
);
2168 isl_space_free(dim2
);
2171 dim1
= isl_space_copy(exp
->dim
);
2172 isl_reordering_free(exp
);
2175 isl_space_free(dim1
);
2176 isl_space_free(dim2
);
2180 /* Given the space of set (domain), construct a space for a map
2181 * with as domain the given space and as range the range of "model".
2183 __isl_give isl_space
*isl_space_extend_domain_with_range(
2184 __isl_take isl_space
*space
, __isl_take isl_space
*model
)
2189 space
= isl_space_from_domain(space
);
2190 space
= isl_space_add_dims(space
, isl_dim_out
,
2191 isl_space_dim(model
, isl_dim_out
));
2192 if (isl_space_has_tuple_id(model
, isl_dim_out
))
2193 space
= isl_space_set_tuple_id(space
, isl_dim_out
,
2194 isl_space_get_tuple_id(model
, isl_dim_out
));
2197 if (model
->nested
[1]) {
2198 isl_space
*nested
= isl_space_copy(model
->nested
[1]);
2199 int n_nested
, n_space
;
2200 nested
= isl_space_align_params(nested
, isl_space_copy(space
));
2201 n_nested
= isl_space_dim(nested
, isl_dim_param
);
2202 n_space
= isl_space_dim(space
, isl_dim_param
);
2203 if (n_nested
> n_space
)
2204 nested
= isl_space_drop_dims(nested
, isl_dim_param
,
2205 n_space
, n_nested
- n_space
);
2208 space
->nested
[1] = nested
;
2210 isl_space_free(model
);
2213 isl_space_free(model
);
2214 isl_space_free(space
);
2218 /* Compare the "type" dimensions of two isl_spaces.
2220 * The order is fairly arbitrary.
2222 static int isl_space_cmp_type(__isl_keep isl_space
*space1
,
2223 __isl_keep isl_space
*space2
, enum isl_dim_type type
)
2226 isl_space
*nested1
, *nested2
;
2228 if (isl_space_dim(space1
, type
) != isl_space_dim(space2
, type
))
2229 return isl_space_dim(space1
, type
) -
2230 isl_space_dim(space2
, type
);
2232 cmp
= isl_id_cmp(tuple_id(space1
, type
), tuple_id(space2
, type
));
2236 nested1
= nested(space1
, type
);
2237 nested2
= nested(space2
, type
);
2238 if (!nested1
!= !nested2
)
2239 return !nested1
- !nested2
;
2242 return isl_space_cmp(nested1
, nested2
);
2247 /* Compare two isl_spaces.
2249 * The order is fairly arbitrary.
2251 int isl_space_cmp(__isl_keep isl_space
*space1
, __isl_keep isl_space
*space2
)
2256 if (space1
== space2
)
2263 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_param
);
2266 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_in
);
2269 cmp
= isl_space_cmp_type(space1
, space2
, isl_dim_out
);
2273 if (!space1
->ids
&& !space2
->ids
)
2276 for (i
= 0; i
< n(space1
, isl_dim_param
); ++i
) {
2277 cmp
= isl_id_cmp(get_id(space1
, isl_dim_param
, i
),
2278 get_id(space2
, isl_dim_param
, i
));