isl_term_free: return NULL
[isl.git] / isl_multi_move_dims_templ.c
blob1b6e6ca97aa5ea1db5cbbf71e29eea76f9040f26
1 /*
2 * Copyright 2013 Ecole Normale Superieure
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege,
7 * Ecole Normale Superieure, 45 rue d'Ulm, 75230 Paris, France
8 */
10 #include <isl/space.h>
12 #include <isl_multi_macro.h>
14 /* Move the "n" dimensions of "src_type" starting at "src_pos" of "multi"
15 * to dimensions of "dst_type" at "dst_pos".
17 * We only support moving input dimensions to parameters and vice versa.
19 __isl_give MULTI(BASE) *FN(MULTI(BASE),move_dims)(__isl_take MULTI(BASE) *multi,
20 enum isl_dim_type dst_type, unsigned dst_pos,
21 enum isl_dim_type src_type, unsigned src_pos, unsigned n)
23 int i;
25 if (!multi)
26 return NULL;
28 if (n == 0 &&
29 !isl_space_is_named_or_nested(multi->space, src_type) &&
30 !isl_space_is_named_or_nested(multi->space, dst_type))
31 return multi;
33 if (dst_type == isl_dim_out || src_type == isl_dim_out)
34 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
35 "cannot move output/set dimension",
36 return FN(MULTI(BASE),free)(multi));
37 if (dst_type == isl_dim_div || src_type == isl_dim_div)
38 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
39 "cannot move divs",
40 return FN(MULTI(BASE),free)(multi));
41 if (src_pos + n > isl_space_dim(multi->space, src_type))
42 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_invalid,
43 "range out of bounds",
44 return FN(MULTI(BASE),free)(multi));
45 if (dst_type == src_type)
46 isl_die(FN(MULTI(BASE),get_ctx)(multi), isl_error_unsupported,
47 "moving dims within the same type not supported",
48 return FN(MULTI(BASE),free)(multi));
50 multi = FN(MULTI(BASE),cow)(multi);
51 if (!multi)
52 return NULL;
54 multi->space = isl_space_move_dims(multi->space, dst_type, dst_pos,
55 src_type, src_pos, n);
56 if (!multi->space)
57 return FN(MULTI(BASE),free)(multi);
58 if (FN(MULTI(BASE),has_explicit_domain)(multi))
59 multi = FN(MULTI(BASE),move_explicit_domain_dims)(multi,
60 dst_type, dst_pos, src_type, src_pos, n);
61 if (!multi)
62 return NULL;
64 for (i = 0; i < multi->n; ++i) {
65 multi->u.p[i] = FN(EL,move_dims)(multi->u.p[i],
66 dst_type, dst_pos,
67 src_type, src_pos, n);
68 if (!multi->u.p[i])
69 return FN(MULTI(BASE),free)(multi);
72 return multi;