2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 * Copyright 2010 INRIA Saclay
5 * Use of this software is governed by the GNU LGPLv2.1 license
7 * Written by Sven Verdoolaege, K.U.Leuven, Departement
8 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
9 * and INRIA Saclay - Ile-de-France, Parc Club Orsay Universite,
10 * ZAC des vignes, 4 rue Jacques Monod, 91893 Orsay, France
14 #include <isl_space_private.h>
15 #include <isl_id_private.h>
16 #include <isl_reordering.h>
18 isl_ctx
*isl_space_get_ctx(__isl_keep isl_space
*dim
)
20 return dim
? dim
->ctx
: NULL
;
23 __isl_give isl_space
*isl_space_alloc(isl_ctx
*ctx
,
24 unsigned nparam
, unsigned n_in
, unsigned n_out
)
28 dim
= isl_alloc_type(ctx
, struct isl_space
);
39 dim
->tuple_id
[0] = NULL
;
40 dim
->tuple_id
[1] = NULL
;
42 dim
->nested
[0] = NULL
;
43 dim
->nested
[1] = NULL
;
51 /* Mark the space as being that of a set, by setting the domain tuple
54 static __isl_give isl_space
*mark_as_set(__isl_take isl_space
*space
)
56 space
= isl_space_cow(space
);
59 space
= isl_space_set_tuple_id(space
, isl_dim_in
, &isl_id_none
);
63 /* Is the space that of a set?
65 int isl_space_is_set(__isl_keep isl_space
*space
)
69 if (space
->n_in
!= 0 || space
->nested
[0])
71 if (space
->tuple_id
[0] != &isl_id_none
)
76 __isl_give isl_space
*isl_space_set_alloc(isl_ctx
*ctx
,
77 unsigned nparam
, unsigned dim
)
80 space
= isl_space_alloc(ctx
, nparam
, 0, dim
);
81 space
= mark_as_set(space
);
85 /* Mark the space as being that of a parameter domain, by setting
86 * both tuples to isl_id_none.
88 static __isl_give isl_space
*mark_as_params(isl_space
*space
)
92 space
= isl_space_set_tuple_id(space
, isl_dim_in
, &isl_id_none
);
93 space
= isl_space_set_tuple_id(space
, isl_dim_out
, &isl_id_none
);
97 /* Is the space that of a parameter domain?
99 int isl_space_is_params(__isl_keep isl_space
*space
)
103 if (space
->n_in
!= 0 || space
->nested
[0] ||
104 space
->n_out
!= 0 || space
->nested
[1])
106 if (space
->tuple_id
[0] != &isl_id_none
)
108 if (space
->tuple_id
[1] != &isl_id_none
)
113 /* Create a space for a parameter domain.
115 __isl_give isl_space
*isl_space_params_alloc(isl_ctx
*ctx
, unsigned nparam
)
118 space
= isl_space_alloc(ctx
, nparam
, 0, 0);
119 space
= mark_as_params(space
);
123 static unsigned global_pos(__isl_keep isl_space
*dim
,
124 enum isl_dim_type type
, unsigned pos
)
126 struct isl_ctx
*ctx
= dim
->ctx
;
130 isl_assert(ctx
, pos
< dim
->nparam
,
131 return isl_space_dim(dim
, isl_dim_all
));
134 isl_assert(ctx
, pos
< dim
->n_in
,
135 return isl_space_dim(dim
, isl_dim_all
));
136 return pos
+ dim
->nparam
;
138 isl_assert(ctx
, pos
< dim
->n_out
,
139 return isl_space_dim(dim
, isl_dim_all
));
140 return pos
+ dim
->nparam
+ dim
->n_in
;
142 isl_assert(ctx
, 0, return isl_space_dim(dim
, isl_dim_all
));
144 return isl_space_dim(dim
, isl_dim_all
);
147 /* Extend length of ids array to the total number of dimensions.
149 static __isl_give isl_space
*extend_ids(__isl_take isl_space
*dim
)
154 if (isl_space_dim(dim
, isl_dim_all
) <= dim
->n_id
)
158 dim
->ids
= isl_calloc_array(dim
->ctx
,
159 isl_id
*, isl_space_dim(dim
, isl_dim_all
));
163 ids
= isl_realloc_array(dim
->ctx
, dim
->ids
,
164 isl_id
*, isl_space_dim(dim
, isl_dim_all
));
168 for (i
= dim
->n_id
; i
< isl_space_dim(dim
, isl_dim_all
); ++i
)
172 dim
->n_id
= isl_space_dim(dim
, isl_dim_all
);
180 static __isl_give isl_space
*set_id(__isl_take isl_space
*dim
,
181 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
183 dim
= isl_space_cow(dim
);
188 pos
= global_pos(dim
, type
, pos
);
189 if (pos
== isl_space_dim(dim
, isl_dim_all
))
192 if (pos
>= dim
->n_id
) {
195 dim
= extend_ids(dim
);
209 static __isl_keep isl_id
*get_id(__isl_keep isl_space
*dim
,
210 enum isl_dim_type type
, unsigned pos
)
215 pos
= global_pos(dim
, type
, pos
);
216 if (pos
== isl_space_dim(dim
, isl_dim_all
))
218 if (pos
>= dim
->n_id
)
220 return dim
->ids
[pos
];
223 static unsigned offset(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
226 case isl_dim_param
: return 0;
227 case isl_dim_in
: return dim
->nparam
;
228 case isl_dim_out
: return dim
->nparam
+ dim
->n_in
;
233 static unsigned n(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
236 case isl_dim_param
: return dim
->nparam
;
237 case isl_dim_in
: return dim
->n_in
;
238 case isl_dim_out
: return dim
->n_out
;
239 case isl_dim_all
: return dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
244 unsigned isl_space_dim(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
251 unsigned isl_space_offset(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
255 return offset(dim
, type
);
258 static __isl_give isl_space
*copy_ids(__isl_take isl_space
*dst
,
259 enum isl_dim_type dst_type
, unsigned offset
, __isl_keep isl_space
*src
,
260 enum isl_dim_type src_type
)
268 for (i
= 0; i
< n(src
, src_type
); ++i
) {
269 id
= get_id(src
, src_type
, i
);
272 dst
= set_id(dst
, dst_type
, offset
+ i
, isl_id_copy(id
));
279 __isl_take isl_space
*isl_space_dup(__isl_keep isl_space
*dim
)
284 dup
= isl_space_alloc(dim
->ctx
, dim
->nparam
, dim
->n_in
, dim
->n_out
);
285 if (dim
->tuple_id
[0] &&
286 !(dup
->tuple_id
[0] = isl_id_copy(dim
->tuple_id
[0])))
288 if (dim
->tuple_id
[1] &&
289 !(dup
->tuple_id
[1] = isl_id_copy(dim
->tuple_id
[1])))
291 if (dim
->nested
[0] && !(dup
->nested
[0] = isl_space_copy(dim
->nested
[0])))
293 if (dim
->nested
[1] && !(dup
->nested
[1] = isl_space_copy(dim
->nested
[1])))
297 dup
= copy_ids(dup
, isl_dim_param
, 0, dim
, isl_dim_param
);
298 dup
= copy_ids(dup
, isl_dim_in
, 0, dim
, isl_dim_in
);
299 dup
= copy_ids(dup
, isl_dim_out
, 0, dim
, isl_dim_out
);
306 __isl_give isl_space
*isl_space_cow(__isl_take isl_space
*dim
)
314 return isl_space_dup(dim
);
317 __isl_give isl_space
*isl_space_copy(__isl_keep isl_space
*dim
)
326 void isl_space_free(__isl_take isl_space
*dim
)
336 isl_id_free(dim
->tuple_id
[0]);
337 isl_id_free(dim
->tuple_id
[1]);
339 isl_space_free(dim
->nested
[0]);
340 isl_space_free(dim
->nested
[1]);
342 for (i
= 0; i
< dim
->n_id
; ++i
)
343 isl_id_free(dim
->ids
[i
]);
345 isl_ctx_deref(dim
->ctx
);
350 static int name_ok(isl_ctx
*ctx
, const char *s
)
355 dummy
= strtol(s
, &p
, 0);
357 isl_die(ctx
, isl_error_invalid
, "name looks like a number",
363 int isl_space_has_tuple_id(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
367 if (isl_space_is_params(dim
))
368 isl_die(dim
->ctx
, isl_error_invalid
,
369 "parameter spaces don't have tuple ids", return -1);
370 if (isl_space_is_set(dim
) && type
!= isl_dim_set
)
371 isl_die(dim
->ctx
, isl_error_invalid
,
372 "set spaces can only have a set id", return -1);
373 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
374 isl_die(dim
->ctx
, isl_error_invalid
,
375 "only input, output and set tuples can have ids",
377 return dim
->tuple_id
[type
- isl_dim_in
] != NULL
;
380 __isl_give isl_id
*isl_space_get_tuple_id(__isl_keep isl_space
*dim
,
381 enum isl_dim_type type
)
387 has_id
= isl_space_has_tuple_id(dim
, type
);
391 isl_die(dim
->ctx
, isl_error_invalid
,
392 "tuple has no id", return NULL
);
393 return isl_id_copy(dim
->tuple_id
[type
- isl_dim_in
]);
396 __isl_give isl_space
*isl_space_set_tuple_id(__isl_take isl_space
*dim
,
397 enum isl_dim_type type
, __isl_take isl_id
*id
)
399 dim
= isl_space_cow(dim
);
402 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
403 isl_die(dim
->ctx
, isl_error_invalid
,
404 "only input, output and set tuples can have names",
407 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
408 dim
->tuple_id
[type
- isl_dim_in
] = id
;
417 __isl_give isl_space
*isl_space_reset_tuple_id(__isl_take isl_space
*dim
,
418 enum isl_dim_type type
)
420 dim
= isl_space_cow(dim
);
423 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
424 isl_die(dim
->ctx
, isl_error_invalid
,
425 "only input, output and set tuples can have names",
428 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
429 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
437 /* Set the id of the given dimension of "space" to "id".
438 * If the dimension already has an id, then it is replaced.
439 * If the dimension is a parameter, then we need to change it
440 * in the nested spaces (if any) as well.
442 __isl_give isl_space
*isl_space_set_dim_id(__isl_take isl_space
*space
,
443 enum isl_dim_type type
, unsigned pos
, __isl_take isl_id
*id
)
445 space
= isl_space_cow(space
);
449 if (type
== isl_dim_param
) {
452 for (i
= 0; i
< 2; ++i
) {
453 if (!space
->nested
[i
])
456 isl_space_set_dim_id(space
->nested
[i
],
457 type
, pos
, isl_id_copy(id
));
458 if (!space
->nested
[i
])
463 isl_id_free(get_id(space
, type
, pos
));
464 return set_id(space
, type
, pos
, id
);
467 isl_space_free(space
);
471 int isl_space_has_dim_id(__isl_keep isl_space
*dim
,
472 enum isl_dim_type type
, unsigned pos
)
476 return get_id(dim
, type
, pos
) != NULL
;
479 __isl_give isl_id
*isl_space_get_dim_id(__isl_keep isl_space
*dim
,
480 enum isl_dim_type type
, unsigned pos
)
484 if (!get_id(dim
, type
, pos
))
485 isl_die(dim
->ctx
, isl_error_invalid
,
486 "dim has no id", return NULL
);
487 return isl_id_copy(get_id(dim
, type
, pos
));
490 __isl_give isl_space
*isl_space_set_tuple_name(__isl_take isl_space
*dim
,
491 enum isl_dim_type type
, const char *s
)
499 return isl_space_reset_tuple_id(dim
, type
);
501 if (!name_ok(dim
->ctx
, s
))
504 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
505 return isl_space_set_tuple_id(dim
, type
, id
);
511 const char *isl_space_get_tuple_name(__isl_keep isl_space
*dim
,
512 enum isl_dim_type type
)
517 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
519 id
= dim
->tuple_id
[type
- isl_dim_in
];
520 return id
? id
->name
: NULL
;
523 __isl_give isl_space
*isl_space_set_dim_name(__isl_take isl_space
*dim
,
524 enum isl_dim_type type
, unsigned pos
,
531 if (!name_ok(dim
->ctx
, s
))
533 id
= isl_id_alloc(dim
->ctx
, s
, NULL
);
534 return isl_space_set_dim_id(dim
, type
, pos
, id
);
540 __isl_keep
const char *isl_space_get_dim_name(__isl_keep isl_space
*dim
,
541 enum isl_dim_type type
, unsigned pos
)
543 isl_id
*id
= get_id(dim
, type
, pos
);
544 return id
? id
->name
: NULL
;
547 int isl_space_find_dim_by_id(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
548 __isl_keep isl_id
*id
)
557 offset
= isl_space_offset(dim
, type
);
558 n
= isl_space_dim(dim
, type
);
559 for (i
= 0; i
< n
&& offset
+ i
< dim
->n_id
; ++i
)
560 if (dim
->ids
[offset
+ i
] == id
)
566 int isl_space_find_dim_by_name(__isl_keep isl_space
*space
,
567 enum isl_dim_type type
, const char *name
)
576 offset
= isl_space_offset(space
, type
);
577 n
= isl_space_dim(space
, type
);
578 for (i
= 0; i
< n
&& offset
+ i
< space
->n_id
; ++i
)
579 if (space
->ids
[offset
+ i
]->name
&&
580 !strcmp(space
->ids
[offset
+ i
]->name
, name
))
586 static __isl_keep isl_id
*tuple_id(__isl_keep isl_space
*dim
,
587 enum isl_dim_type type
)
591 if (type
== isl_dim_in
)
592 return dim
->tuple_id
[0];
593 if (type
== isl_dim_out
)
594 return dim
->tuple_id
[1];
598 static __isl_keep isl_space
*nested(__isl_keep isl_space
*dim
,
599 enum isl_dim_type type
)
603 if (type
== isl_dim_in
)
604 return dim
->nested
[0];
605 if (type
== isl_dim_out
)
606 return dim
->nested
[1];
610 int isl_space_tuple_match(__isl_keep isl_space
*dim1
, enum isl_dim_type dim1_type
,
611 __isl_keep isl_space
*dim2
, enum isl_dim_type dim2_type
)
614 isl_space
*nested1
, *nested2
;
619 if (dim1
== dim2
&& dim1_type
== dim2_type
)
622 if (n(dim1
, dim1_type
) != n(dim2
, dim2_type
))
624 id1
= tuple_id(dim1
, dim1_type
);
625 id2
= tuple_id(dim2
, dim2_type
);
628 if (id1
&& id1
!= id2
)
630 nested1
= nested(dim1
, dim1_type
);
631 nested2
= nested(dim2
, dim2_type
);
632 if (!nested1
^ !nested2
)
634 if (nested1
&& !isl_space_is_equal(nested1
, nested2
))
639 static int match(__isl_keep isl_space
*dim1
, enum isl_dim_type dim1_type
,
640 __isl_keep isl_space
*dim2
, enum isl_dim_type dim2_type
)
644 if (dim1
== dim2
&& dim1_type
== dim2_type
)
647 if (!isl_space_tuple_match(dim1
, dim1_type
, dim2
, dim2_type
))
650 if (!dim1
->ids
&& !dim2
->ids
)
653 for (i
= 0; i
< n(dim1
, dim1_type
); ++i
) {
654 if (get_id(dim1
, dim1_type
, i
) != get_id(dim2
, dim2_type
, i
))
660 int isl_space_match(__isl_keep isl_space
*dim1
, enum isl_dim_type dim1_type
,
661 __isl_keep isl_space
*dim2
, enum isl_dim_type dim2_type
)
666 return match(dim1
, dim1_type
, dim2
, dim2_type
);
669 static void get_ids(__isl_keep isl_space
*dim
, enum isl_dim_type type
,
670 unsigned first
, unsigned n
, __isl_keep isl_id
**ids
)
674 for (i
= 0; i
< n
; ++i
)
675 ids
[i
] = get_id(dim
, type
, first
+ i
);
678 __isl_give isl_space
*isl_space_extend(__isl_take isl_space
*dim
,
679 unsigned nparam
, unsigned n_in
, unsigned n_out
)
685 if (dim
->nparam
== nparam
&& dim
->n_in
== n_in
&& dim
->n_out
== n_out
)
688 isl_assert(dim
->ctx
, dim
->nparam
<= nparam
, goto error
);
689 isl_assert(dim
->ctx
, dim
->n_in
<= n_in
, goto error
);
690 isl_assert(dim
->ctx
, dim
->n_out
<= n_out
, goto error
);
692 dim
= isl_space_cow(dim
);
695 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
696 nparam
+ n_in
+ n_out
);
699 get_ids(dim
, isl_dim_param
, 0, dim
->nparam
, ids
);
700 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
+ nparam
);
701 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ nparam
+ n_in
);
704 dim
->n_id
= nparam
+ n_in
+ n_out
;
706 dim
->nparam
= nparam
;
717 __isl_give isl_space
*isl_space_add_dims(__isl_take isl_space
*dim
,
718 enum isl_dim_type type
, unsigned n
)
722 dim
= isl_space_reset(dim
, type
);
725 dim
= isl_space_extend(dim
,
726 dim
->nparam
+ n
, dim
->n_in
, dim
->n_out
);
727 if (dim
&& dim
->nested
[0] &&
728 !(dim
->nested
[0] = isl_space_add_dims(dim
->nested
[0],
731 if (dim
&& dim
->nested
[1] &&
732 !(dim
->nested
[1] = isl_space_add_dims(dim
->nested
[1],
737 return isl_space_extend(dim
,
738 dim
->nparam
, dim
->n_in
+ n
, dim
->n_out
);
740 return isl_space_extend(dim
,
741 dim
->nparam
, dim
->n_in
, dim
->n_out
+ n
);
743 isl_die(dim
->ctx
, isl_error_invalid
,
744 "cannot add dimensions of specified type", goto error
);
751 static int valid_dim_type(enum isl_dim_type type
)
763 __isl_give isl_space
*isl_space_insert_dims(__isl_take isl_space
*dim
,
764 enum isl_dim_type type
, unsigned pos
, unsigned n
)
771 return isl_space_reset(dim
, type
);
773 if (!valid_dim_type(type
))
774 isl_die(dim
->ctx
, isl_error_invalid
,
775 "cannot insert dimensions of specified type",
778 isl_assert(dim
->ctx
, pos
<= isl_space_dim(dim
, type
), goto error
);
780 dim
= isl_space_cow(dim
);
788 int *size
= s
- isl_dim_param
;
789 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
790 dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
);
794 size
[isl_dim_param
] = dim
->nparam
;
795 size
[isl_dim_in
] = dim
->n_in
;
796 size
[isl_dim_out
] = dim
->n_out
;
797 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
799 get_ids(dim
, t
, 0, size
[t
], ids
+ off
);
802 get_ids(dim
, t
, 0, pos
, ids
+ off
);
804 get_ids(dim
, t
, pos
, size
[t
] - pos
, ids
+ off
);
805 off
+= size
[t
] - pos
;
810 dim
->n_id
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
+ n
;
813 case isl_dim_param
: dim
->nparam
+= n
; break;
814 case isl_dim_in
: dim
->n_in
+= n
; break;
815 case isl_dim_out
: dim
->n_out
+= n
; break;
818 dim
= isl_space_reset(dim
, type
);
826 __isl_give isl_space
*isl_space_move_dims(__isl_take isl_space
*dim
,
827 enum isl_dim_type dst_type
, unsigned dst_pos
,
828 enum isl_dim_type src_type
, unsigned src_pos
, unsigned n
)
837 isl_assert(dim
->ctx
, src_pos
+ n
<= isl_space_dim(dim
, src_type
),
840 if (dst_type
== src_type
&& dst_pos
== src_pos
)
843 isl_assert(dim
->ctx
, dst_type
!= src_type
, goto error
);
845 dim
= isl_space_reset(dim
, src_type
);
846 dim
= isl_space_reset(dim
, dst_type
);
848 dim
= isl_space_cow(dim
);
857 int *size
= s
- isl_dim_param
;
858 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
859 dim
->nparam
+ dim
->n_in
+ dim
->n_out
);
863 size
[isl_dim_param
] = dim
->nparam
;
864 size
[isl_dim_in
] = dim
->n_in
;
865 size
[isl_dim_out
] = dim
->n_out
;
866 for (t
= isl_dim_param
; t
<= isl_dim_out
; ++t
) {
868 get_ids(dim
, t
, 0, dst_pos
, ids
+ off
);
870 get_ids(dim
, src_type
, src_pos
, n
, ids
+ off
);
872 get_ids(dim
, t
, dst_pos
, size
[t
] - dst_pos
,
874 off
+= size
[t
] - dst_pos
;
875 } else if (t
== src_type
) {
876 get_ids(dim
, t
, 0, src_pos
, ids
+ off
);
878 get_ids(dim
, t
, src_pos
+ n
,
879 size
[t
] - src_pos
- n
, ids
+ off
);
880 off
+= size
[t
] - src_pos
- n
;
882 get_ids(dim
, t
, 0, size
[t
], ids
+ off
);
888 dim
->n_id
= dim
->nparam
+ dim
->n_in
+ dim
->n_out
;
892 case isl_dim_param
: dim
->nparam
+= n
; break;
893 case isl_dim_in
: dim
->n_in
+= n
; break;
894 case isl_dim_out
: dim
->n_out
+= n
; break;
899 case isl_dim_param
: dim
->nparam
-= n
; break;
900 case isl_dim_in
: dim
->n_in
-= n
; break;
901 case isl_dim_out
: dim
->n_out
-= n
; break;
905 if (dst_type
!= isl_dim_param
&& src_type
!= isl_dim_param
)
908 for (i
= 0; i
< 2; ++i
) {
911 dim
->nested
[i
] = isl_space_replace(dim
->nested
[i
],
923 __isl_give isl_space
*isl_space_join(__isl_take isl_space
*left
,
924 __isl_take isl_space
*right
)
931 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
933 isl_assert(left
->ctx
,
934 isl_space_tuple_match(left
, isl_dim_out
, right
, isl_dim_in
),
937 dim
= isl_space_alloc(left
->ctx
, left
->nparam
, left
->n_in
, right
->n_out
);
941 dim
= copy_ids(dim
, isl_dim_param
, 0, left
, isl_dim_param
);
942 dim
= copy_ids(dim
, isl_dim_in
, 0, left
, isl_dim_in
);
943 dim
= copy_ids(dim
, isl_dim_out
, 0, right
, isl_dim_out
);
945 if (dim
&& left
->tuple_id
[0] &&
946 !(dim
->tuple_id
[0] = isl_id_copy(left
->tuple_id
[0])))
948 if (dim
&& right
->tuple_id
[1] &&
949 !(dim
->tuple_id
[1] = isl_id_copy(right
->tuple_id
[1])))
951 if (dim
&& left
->nested
[0] &&
952 !(dim
->nested
[0] = isl_space_copy(left
->nested
[0])))
954 if (dim
&& right
->nested
[1] &&
955 !(dim
->nested
[1] = isl_space_copy(right
->nested
[1])))
958 isl_space_free(left
);
959 isl_space_free(right
);
963 isl_space_free(left
);
964 isl_space_free(right
);
968 __isl_give isl_space
*isl_space_product(__isl_take isl_space
*left
,
969 __isl_take isl_space
*right
)
971 isl_space
*dom1
, *dom2
, *nest1
, *nest2
;
976 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
979 dom1
= isl_space_domain(isl_space_copy(left
));
980 dom2
= isl_space_domain(isl_space_copy(right
));
981 nest1
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
983 dom1
= isl_space_range(left
);
984 dom2
= isl_space_range(right
);
985 nest2
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
987 return isl_space_join(isl_space_reverse(nest1
), nest2
);
989 isl_space_free(left
);
990 isl_space_free(right
);
994 /* Given two spaces { A -> C } and { B -> C }, construct the space
997 __isl_give isl_space
*isl_space_domain_product(__isl_take isl_space
*left
,
998 __isl_take isl_space
*right
)
1000 isl_space
*ran
, *dom1
, *dom2
, *nest
;
1002 if (!left
|| !right
)
1005 if (!match(left
, isl_dim_param
, right
, isl_dim_param
))
1006 isl_die(left
->ctx
, isl_error_invalid
,
1007 "parameters need to match", goto error
);
1008 if (!isl_space_tuple_match(left
, isl_dim_out
, right
, isl_dim_out
))
1009 isl_die(left
->ctx
, isl_error_invalid
,
1010 "ranges need to match", goto error
);
1012 ran
= isl_space_range(isl_space_copy(left
));
1014 dom1
= isl_space_domain(left
);
1015 dom2
= isl_space_domain(right
);
1016 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(dom1
), dom2
));
1018 return isl_space_join(isl_space_reverse(nest
), ran
);
1020 isl_space_free(left
);
1021 isl_space_free(right
);
1025 __isl_give isl_space
*isl_space_range_product(__isl_take isl_space
*left
,
1026 __isl_take isl_space
*right
)
1028 isl_space
*dom
, *ran1
, *ran2
, *nest
;
1030 if (!left
|| !right
)
1033 isl_assert(left
->ctx
, match(left
, isl_dim_param
, right
, isl_dim_param
),
1035 if (!isl_space_tuple_match(left
, isl_dim_in
, right
, isl_dim_in
))
1036 isl_die(left
->ctx
, isl_error_invalid
,
1037 "domains need to match", goto error
);
1039 dom
= isl_space_domain(isl_space_copy(left
));
1041 ran1
= isl_space_range(left
);
1042 ran2
= isl_space_range(right
);
1043 nest
= isl_space_wrap(isl_space_join(isl_space_reverse(ran1
), ran2
));
1045 return isl_space_join(isl_space_reverse(dom
), nest
);
1047 isl_space_free(left
);
1048 isl_space_free(right
);
1052 __isl_give isl_space
*isl_space_map_from_set(__isl_take isl_space
*dim
)
1055 isl_id
**ids
= NULL
;
1059 ctx
= isl_space_get_ctx(dim
);
1060 if (!isl_space_is_set(dim
))
1061 isl_die(ctx
, isl_error_invalid
, "not a set space", goto error
);
1062 dim
= isl_space_cow(dim
);
1066 ids
= isl_calloc_array(dim
->ctx
, isl_id
*,
1067 dim
->nparam
+ dim
->n_out
+ dim
->n_out
);
1070 get_ids(dim
, isl_dim_param
, 0, dim
->nparam
, ids
);
1071 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->nparam
);
1073 dim
->n_in
= dim
->n_out
;
1077 dim
->n_id
= dim
->nparam
+ dim
->n_out
+ dim
->n_out
;
1078 dim
= copy_ids(dim
, isl_dim_out
, 0, dim
, isl_dim_in
);
1080 isl_id_free(dim
->tuple_id
[0]);
1081 dim
->tuple_id
[0] = isl_id_copy(dim
->tuple_id
[1]);
1082 isl_space_free(dim
->nested
[0]);
1083 dim
->nested
[0] = isl_space_copy(dim
->nested
[1]);
1086 isl_space_free(dim
);
1090 __isl_give isl_space
*isl_space_map_from_domain_and_range(
1091 __isl_take isl_space
*domain
, __isl_take isl_space
*range
)
1093 if (!domain
|| !range
)
1095 if (!isl_space_is_set(domain
))
1096 isl_die(isl_space_get_ctx(domain
), isl_error_invalid
,
1097 "domain is not a set space", goto error
);
1098 if (!isl_space_is_set(range
))
1099 isl_die(isl_space_get_ctx(range
), isl_error_invalid
,
1100 "range is not a set space", goto error
);
1101 return isl_space_join(isl_space_reverse(domain
), range
);
1103 isl_space_free(domain
);
1104 isl_space_free(range
);
1108 static __isl_give isl_space
*set_ids(__isl_take isl_space
*dim
,
1109 enum isl_dim_type type
,
1110 unsigned first
, unsigned n
, __isl_take isl_id
**ids
)
1114 for (i
= 0; i
< n
; ++i
)
1115 dim
= set_id(dim
, type
, first
+ i
, ids
[i
]);
1120 __isl_give isl_space
*isl_space_reverse(__isl_take isl_space
*dim
)
1124 isl_id
**ids
= NULL
;
1129 if (match(dim
, isl_dim_in
, dim
, isl_dim_out
))
1132 dim
= isl_space_cow(dim
);
1136 id
= dim
->tuple_id
[0];
1137 dim
->tuple_id
[0] = dim
->tuple_id
[1];
1138 dim
->tuple_id
[1] = id
;
1140 nested
= dim
->nested
[0];
1141 dim
->nested
[0] = dim
->nested
[1];
1142 dim
->nested
[1] = nested
;
1145 ids
= isl_alloc_array(dim
->ctx
, isl_id
*,
1146 dim
->n_in
+ dim
->n_out
);
1149 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
);
1150 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
+ dim
->n_in
);
1154 dim
->n_in
= dim
->n_out
;
1158 dim
= set_ids(dim
, isl_dim_out
, 0, dim
->n_out
, ids
);
1159 dim
= set_ids(dim
, isl_dim_in
, 0, dim
->n_in
, ids
+ dim
->n_out
);
1166 isl_space_free(dim
);
1170 __isl_give isl_space
*isl_space_drop_dims(__isl_take isl_space
*dim
,
1171 enum isl_dim_type type
, unsigned first
, unsigned num
)
1179 return isl_space_reset(dim
, type
);
1181 if (!valid_dim_type(type
))
1182 isl_die(dim
->ctx
, isl_error_invalid
,
1183 "cannot drop dimensions of specified type", goto error
);
1185 isl_assert(dim
->ctx
, first
+ num
<= n(dim
, type
), goto error
);
1186 dim
= isl_space_cow(dim
);
1190 dim
= extend_ids(dim
);
1193 for (i
= 0; i
< num
; ++i
)
1194 isl_id_free(get_id(dim
, type
, first
+ i
));
1195 for (i
= first
+num
; i
< n(dim
, type
); ++i
)
1196 set_id(dim
, type
, i
- num
, get_id(dim
, type
, i
));
1199 get_ids(dim
, isl_dim_in
, 0, dim
->n_in
,
1200 dim
->ids
+ offset(dim
, isl_dim_in
) - num
);
1202 get_ids(dim
, isl_dim_out
, 0, dim
->n_out
,
1203 dim
->ids
+ offset(dim
, isl_dim_out
) - num
);
1210 case isl_dim_param
: dim
->nparam
-= num
; break;
1211 case isl_dim_in
: dim
->n_in
-= num
; break;
1212 case isl_dim_out
: dim
->n_out
-= num
; break;
1215 dim
= isl_space_reset(dim
, type
);
1216 if (type
== isl_dim_param
) {
1217 if (dim
&& dim
->nested
[0] &&
1218 !(dim
->nested
[0] = isl_space_drop_dims(dim
->nested
[0],
1219 isl_dim_param
, first
, num
)))
1221 if (dim
&& dim
->nested
[1] &&
1222 !(dim
->nested
[1] = isl_space_drop_dims(dim
->nested
[1],
1223 isl_dim_param
, first
, num
)))
1228 isl_space_free(dim
);
1232 __isl_give isl_space
*isl_space_drop_inputs(__isl_take isl_space
*dim
,
1233 unsigned first
, unsigned n
)
1237 return isl_space_drop_dims(dim
, isl_dim_in
, first
, n
);
1240 __isl_give isl_space
*isl_space_drop_outputs(__isl_take isl_space
*dim
,
1241 unsigned first
, unsigned n
)
1245 return isl_space_drop_dims(dim
, isl_dim_out
, first
, n
);
1248 __isl_give isl_space
*isl_space_domain(__isl_take isl_space
*dim
)
1252 dim
= isl_space_drop_outputs(dim
, 0, dim
->n_out
);
1253 dim
= isl_space_reverse(dim
);
1254 dim
= mark_as_set(dim
);
1258 __isl_give isl_space
*isl_space_from_domain(__isl_take isl_space
*dim
)
1262 if (!isl_space_is_set(dim
))
1263 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1264 "not a set space", goto error
);
1265 dim
= isl_space_reverse(dim
);
1266 dim
= isl_space_reset(dim
, isl_dim_out
);
1269 isl_space_free(dim
);
1273 __isl_give isl_space
*isl_space_range(__isl_take isl_space
*dim
)
1277 dim
= isl_space_drop_inputs(dim
, 0, dim
->n_in
);
1278 dim
= mark_as_set(dim
);
1282 __isl_give isl_space
*isl_space_from_range(__isl_take isl_space
*dim
)
1286 if (!isl_space_is_set(dim
))
1287 isl_die(isl_space_get_ctx(dim
), isl_error_invalid
,
1288 "not a set space", goto error
);
1289 return isl_space_reset(dim
, isl_dim_in
);
1291 isl_space_free(dim
);
1295 __isl_give isl_space
*isl_space_params(__isl_take isl_space
*space
)
1297 if (isl_space_is_params(space
))
1299 space
= isl_space_drop_dims(space
,
1300 isl_dim_in
, 0, isl_space_dim(space
, isl_dim_in
));
1301 space
= isl_space_drop_dims(space
,
1302 isl_dim_out
, 0, isl_space_dim(space
, isl_dim_out
));
1303 space
= mark_as_params(space
);
1307 __isl_give isl_space
*isl_space_set_from_params(__isl_take isl_space
*space
)
1311 if (!isl_space_is_params(space
))
1312 isl_die(isl_space_get_ctx(space
), isl_error_invalid
,
1313 "not a parameter space", goto error
);
1314 return isl_space_reset(space
, isl_dim_set
);
1316 isl_space_free(space
);
1320 __isl_give isl_space
*isl_space_as_set_space(__isl_take isl_space
*dim
)
1322 dim
= isl_space_cow(dim
);
1326 dim
->n_out
+= dim
->n_in
;
1328 dim
= isl_space_reset(dim
, isl_dim_in
);
1329 dim
= isl_space_reset(dim
, isl_dim_out
);
1334 __isl_give isl_space
*isl_space_underlying(__isl_take isl_space
*dim
,
1342 dim
->nparam
== 0 && dim
->n_in
== 0 && dim
->n_id
== 0)
1343 return isl_space_reset(isl_space_reset(dim
, isl_dim_in
), isl_dim_out
);
1344 dim
= isl_space_cow(dim
);
1347 dim
->n_out
+= dim
->nparam
+ dim
->n_in
+ n_div
;
1351 for (i
= 0; i
< dim
->n_id
; ++i
)
1352 isl_id_free(get_id(dim
, isl_dim_out
, i
));
1354 dim
= isl_space_reset(dim
, isl_dim_in
);
1355 dim
= isl_space_reset(dim
, isl_dim_out
);
1360 int isl_space_is_equal(__isl_keep isl_space
*dim1
, __isl_keep isl_space
*dim2
)
1366 return match(dim1
, isl_dim_param
, dim2
, isl_dim_param
) &&
1367 isl_space_tuple_match(dim1
, isl_dim_in
, dim2
, isl_dim_in
) &&
1368 isl_space_tuple_match(dim1
, isl_dim_out
, dim2
, isl_dim_out
);
1371 /* Is space1 equal to the domain of space2?
1373 int isl_space_is_domain(__isl_keep isl_space
*space1
,
1374 __isl_keep isl_space
*space2
)
1376 if (!space1
|| !space2
)
1378 if (!isl_space_is_set(space1
))
1380 return match(space1
, isl_dim_param
, space2
, isl_dim_param
) &&
1381 isl_space_tuple_match(space1
, isl_dim_set
, space2
, isl_dim_in
);
1384 int isl_space_compatible(__isl_keep isl_space
*dim1
,
1385 __isl_keep isl_space
*dim2
)
1387 return dim1
->nparam
== dim2
->nparam
&&
1388 dim1
->n_in
+ dim1
->n_out
== dim2
->n_in
+ dim2
->n_out
;
1391 static uint32_t isl_hash_dim(uint32_t hash
, __isl_keep isl_space
*dim
)
1399 hash
= isl_hash_builtin(hash
, dim
->nparam
);
1400 hash
= isl_hash_builtin(hash
, dim
->n_in
);
1401 hash
= isl_hash_builtin(hash
, dim
->n_out
);
1403 for (i
= 0; i
< dim
->nparam
; ++i
) {
1404 id
= get_id(dim
, isl_dim_param
, i
);
1405 hash
= isl_hash_id(hash
, id
);
1408 id
= tuple_id(dim
, isl_dim_in
);
1409 hash
= isl_hash_id(hash
, id
);
1410 id
= tuple_id(dim
, isl_dim_out
);
1411 hash
= isl_hash_id(hash
, id
);
1413 hash
= isl_hash_dim(hash
, dim
->nested
[0]);
1414 hash
= isl_hash_dim(hash
, dim
->nested
[1]);
1419 uint32_t isl_space_get_hash(__isl_keep isl_space
*dim
)
1426 hash
= isl_hash_init();
1427 hash
= isl_hash_dim(hash
, dim
);
1432 int isl_space_is_wrapping(__isl_keep isl_space
*dim
)
1437 if (!isl_space_is_set(dim
))
1440 return dim
->nested
[1] != NULL
;
1443 __isl_give isl_space
*isl_space_wrap(__isl_take isl_space
*dim
)
1450 wrap
= isl_space_set_alloc(dim
->ctx
,
1451 dim
->nparam
, dim
->n_in
+ dim
->n_out
);
1453 wrap
= copy_ids(wrap
, isl_dim_param
, 0, dim
, isl_dim_param
);
1454 wrap
= copy_ids(wrap
, isl_dim_set
, 0, dim
, isl_dim_in
);
1455 wrap
= copy_ids(wrap
, isl_dim_set
, dim
->n_in
, dim
, isl_dim_out
);
1460 wrap
->nested
[1] = dim
;
1464 isl_space_free(dim
);
1468 __isl_give isl_space
*isl_space_unwrap(__isl_take isl_space
*dim
)
1475 if (!isl_space_is_wrapping(dim
))
1476 isl_die(dim
->ctx
, isl_error_invalid
, "not a wrapping dim",
1479 unwrap
= isl_space_copy(dim
->nested
[1]);
1480 isl_space_free(dim
);
1484 isl_space_free(dim
);
1488 int isl_space_is_named_or_nested(__isl_keep isl_space
*dim
, enum isl_dim_type type
)
1490 if (type
!= isl_dim_in
&& type
!= isl_dim_out
)
1494 if (dim
->tuple_id
[type
- isl_dim_in
])
1496 if (dim
->nested
[type
- isl_dim_in
])
1501 int isl_space_may_be_set(__isl_keep isl_space
*dim
)
1505 if (isl_space_is_set(dim
))
1507 if (isl_space_dim(dim
, isl_dim_in
) != 0)
1509 if (isl_space_is_named_or_nested(dim
, isl_dim_in
))
1514 __isl_give isl_space
*isl_space_reset(__isl_take isl_space
*dim
,
1515 enum isl_dim_type type
)
1517 if (!isl_space_is_named_or_nested(dim
, type
))
1520 dim
= isl_space_cow(dim
);
1524 isl_id_free(dim
->tuple_id
[type
- isl_dim_in
]);
1525 dim
->tuple_id
[type
- isl_dim_in
] = NULL
;
1526 isl_space_free(dim
->nested
[type
- isl_dim_in
]);
1527 dim
->nested
[type
- isl_dim_in
] = NULL
;
1532 __isl_give isl_space
*isl_space_flatten(__isl_take isl_space
*dim
)
1536 if (!dim
->nested
[0] && !dim
->nested
[1])
1540 dim
= isl_space_reset(dim
, isl_dim_in
);
1541 if (dim
&& dim
->nested
[1])
1542 dim
= isl_space_reset(dim
, isl_dim_out
);
1547 __isl_give isl_space
*isl_space_flatten_domain(__isl_take isl_space
*dim
)
1551 if (!dim
->nested
[0])
1554 return isl_space_reset(dim
, isl_dim_in
);
1557 __isl_give isl_space
*isl_space_flatten_range(__isl_take isl_space
*dim
)
1561 if (!dim
->nested
[1])
1564 return isl_space_reset(dim
, isl_dim_out
);
1567 /* Replace the dimensions of the given type of dst by those of src.
1569 __isl_give isl_space
*isl_space_replace(__isl_take isl_space
*dst
,
1570 enum isl_dim_type type
, __isl_keep isl_space
*src
)
1572 dst
= isl_space_cow(dst
);
1577 dst
= isl_space_drop_dims(dst
, type
, 0, isl_space_dim(dst
, type
));
1578 dst
= isl_space_add_dims(dst
, type
, isl_space_dim(src
, type
));
1579 dst
= copy_ids(dst
, type
, 0, src
, type
);
1581 if (dst
&& type
== isl_dim_param
) {
1583 for (i
= 0; i
<= 1; ++i
) {
1584 if (!dst
->nested
[i
])
1586 dst
->nested
[i
] = isl_space_replace(dst
->nested
[i
],
1588 if (!dst
->nested
[i
])
1595 isl_space_free(dst
);
1599 /* Given a dimension specification "dim" of a set, create a dimension
1600 * specification for the lift of the set. In particular, the result
1601 * is of the form [dim -> local[..]], with n_local variables in the
1602 * range of the wrapped map.
1604 __isl_give isl_space
*isl_space_lift(__isl_take isl_space
*dim
, unsigned n_local
)
1606 isl_space
*local_dim
;
1611 local_dim
= isl_space_dup(dim
);
1612 local_dim
= isl_space_drop_dims(local_dim
, isl_dim_set
, 0, dim
->n_out
);
1613 local_dim
= isl_space_add_dims(local_dim
, isl_dim_set
, n_local
);
1614 local_dim
= isl_space_set_tuple_name(local_dim
, isl_dim_set
, "local");
1615 dim
= isl_space_join(isl_space_from_domain(dim
),
1616 isl_space_from_range(local_dim
));
1617 dim
= isl_space_wrap(dim
);
1618 dim
= isl_space_set_tuple_name(dim
, isl_dim_set
, "lifted");
1623 int isl_space_can_zip(__isl_keep isl_space
*dim
)
1628 return dim
->nested
[0] && dim
->nested
[1];
1631 __isl_give isl_space
*isl_space_zip(__isl_take isl_space
*dim
)
1633 isl_space
*dom
, *ran
;
1634 isl_space
*dom_dom
, *dom_ran
, *ran_dom
, *ran_ran
;
1636 if (!isl_space_can_zip(dim
))
1637 isl_die(dim
->ctx
, isl_error_invalid
, "dim cannot be zipped",
1642 dom
= isl_space_unwrap(isl_space_domain(isl_space_copy(dim
)));
1643 ran
= isl_space_unwrap(isl_space_range(dim
));
1644 dom_dom
= isl_space_domain(isl_space_copy(dom
));
1645 dom_ran
= isl_space_range(dom
);
1646 ran_dom
= isl_space_domain(isl_space_copy(ran
));
1647 ran_ran
= isl_space_range(ran
);
1648 dom
= isl_space_join(isl_space_from_domain(dom_dom
),
1649 isl_space_from_range(ran_dom
));
1650 ran
= isl_space_join(isl_space_from_domain(dom_ran
),
1651 isl_space_from_range(ran_ran
));
1652 return isl_space_join(isl_space_from_domain(isl_space_wrap(dom
)),
1653 isl_space_from_range(isl_space_wrap(ran
)));
1655 isl_space_free(dim
);
1659 int isl_space_has_named_params(__isl_keep isl_space
*dim
)
1666 if (dim
->nparam
== 0)
1668 off
= isl_space_offset(dim
, isl_dim_param
);
1669 if (off
+ dim
->nparam
> dim
->n_id
)
1671 for (i
= 0; i
< dim
->nparam
; ++i
)
1672 if (!dim
->ids
[off
+ i
])
1677 /* Align the initial parameters of dim1 to match the order in dim2.
1679 __isl_give isl_space
*isl_space_align_params(__isl_take isl_space
*dim1
,
1680 __isl_take isl_space
*dim2
)
1682 isl_reordering
*exp
;
1684 if (!isl_space_has_named_params(dim1
) || !isl_space_has_named_params(dim2
))
1685 isl_die(isl_space_get_ctx(dim1
), isl_error_invalid
,
1686 "parameter alignment requires named parameters",
1689 dim2
= isl_space_params(dim2
);
1690 exp
= isl_parameter_alignment_reordering(dim1
, dim2
);
1691 exp
= isl_reordering_extend_space(exp
, dim1
);
1692 isl_space_free(dim2
);
1695 dim1
= isl_space_copy(exp
->dim
);
1696 isl_reordering_free(exp
);
1699 isl_space_free(dim1
);
1700 isl_space_free(dim2
);
1704 /* Given the space of set (domain), construct a space for a map
1705 * with as domain the given space and as range the range of "model".
1707 __isl_give isl_space
*isl_space_extend_domain_with_range(
1708 __isl_take isl_space
*domain
, __isl_take isl_space
*model
)
1712 space
= isl_space_from_domain(domain
);
1713 space
= isl_space_add_dims(space
, isl_dim_out
,
1714 isl_space_dim(model
, isl_dim_out
));
1715 if (isl_space_has_tuple_id(model
, isl_dim_out
))
1716 space
= isl_space_set_tuple_id(space
, isl_dim_out
,
1717 isl_space_get_tuple_id(model
, isl_dim_out
));
1718 isl_space_free(model
);