2 * Copyright 2010 INRIA Saclay
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, INRIA Saclay - Ile-de-France,
7 * Parc Club Orsay Universite, ZAC des vignes, 4 rue Jacques Monod,
11 #include <isl_ctx_private.h>
13 #include <isl_space_private.h>
14 #include <isl_reordering.h>
16 __isl_give isl_reordering
*isl_reordering_alloc(isl_ctx
*ctx
, int len
)
20 exp
= isl_alloc(ctx
, struct isl_reordering
,
21 sizeof(struct isl_reordering
) + (len
- 1) * sizeof(int));
32 __isl_give isl_reordering
*isl_reordering_copy(__isl_keep isl_reordering
*exp
)
41 __isl_give isl_reordering
*isl_reordering_dup(__isl_keep isl_reordering
*r
)
49 dup
= isl_reordering_alloc(isl_reordering_get_ctx(r
), r
->len
);
53 dup
->space
= isl_reordering_get_space(r
);
55 return isl_reordering_free(dup
);
56 for (i
= 0; i
< dup
->len
; ++i
)
57 dup
->pos
[i
] = r
->pos
[i
];
62 __isl_give isl_reordering
*isl_reordering_cow(__isl_take isl_reordering
*r
)
70 return isl_reordering_dup(r
);
73 __isl_null isl_reordering
*isl_reordering_free(__isl_take isl_reordering
*exp
)
81 isl_space_free(exp
->space
);
86 /* Return the isl_ctx to which "r" belongs.
88 isl_ctx
*isl_reordering_get_ctx(__isl_keep isl_reordering
*r
)
90 return isl_space_get_ctx(isl_reordering_peek_space(r
));
93 /* Return the space of "r".
95 __isl_keep isl_space
*isl_reordering_peek_space(__isl_keep isl_reordering
*r
)
102 /* Return a copy of the space of "r".
104 __isl_give isl_space
*isl_reordering_get_space(__isl_keep isl_reordering
*r
)
106 return isl_space_copy(isl_reordering_peek_space(r
));
109 /* Construct a reordering that maps the parameters of "alignee"
110 * to the corresponding parameters in a new dimension specification
111 * that has the parameters of "aligner" first, followed by
112 * any remaining parameters of "alignee" that do not occur in "aligner".
114 __isl_give isl_reordering
*isl_parameter_alignment_reordering(
115 __isl_keep isl_space
*alignee
, __isl_keep isl_space
*aligner
)
120 if (!alignee
|| !aligner
)
123 exp
= isl_reordering_alloc(alignee
->ctx
, alignee
->nparam
);
127 exp
->space
= isl_space_params(isl_space_copy(aligner
));
129 for (i
= 0; i
< alignee
->nparam
; ++i
) {
131 id_i
= isl_space_get_dim_id(alignee
, isl_dim_param
, i
);
133 isl_die(alignee
->ctx
, isl_error_invalid
,
134 "cannot align unnamed parameters", goto error
);
135 for (j
= 0; j
< aligner
->nparam
; ++j
) {
137 id_j
= isl_space_get_dim_id(aligner
, isl_dim_param
, j
);
142 if (j
< aligner
->nparam
) {
147 pos
= isl_space_dim(exp
->space
, isl_dim_param
);
149 exp
->space
= isl_space_free(exp
->space
);
150 exp
->space
= isl_space_add_dims(exp
->space
,
152 exp
->space
= isl_space_set_dim_id(exp
->space
,
153 isl_dim_param
, pos
, id_i
);
159 return isl_reordering_free(exp
);
162 isl_reordering_free(exp
);
166 /* Return a reordering that moves the parameters identified by
167 * the elements of "tuple" to a domain tuple inserted into "space".
168 * The parameters that remain, are moved from their original positions
169 * in the list of parameters to their new positions in this list.
170 * The parameters that get removed, are moved to the corresponding
171 * positions in the new domain. Note that these set dimensions
172 * do not necessarily need to appear as parameters in "space".
173 * Any other dimensions are shifted by the number of extra dimensions
174 * introduced, i.e., the number of dimensions in the new domain
175 * that did not appear as parameters in "space".
177 __isl_give isl_reordering
*isl_reordering_unbind_params_insert_domain(
178 __isl_keep isl_space
*space
, __isl_keep isl_multi_id
*tuple
)
185 if (!space
|| !tuple
)
188 ctx
= isl_space_get_ctx(space
);
189 r
= isl_reordering_alloc(ctx
, isl_space_dim(space
, isl_dim_all
));
193 r
->space
= isl_space_copy(space
);
194 r
->space
= isl_space_unbind_params_insert_domain(r
->space
, tuple
);
196 return isl_reordering_free(r
);
198 n
= isl_space_dim(r
->space
, isl_dim_param
);
199 for (i
= 0; i
< n
; ++i
) {
203 id
= isl_space_get_dim_id(r
->space
, isl_dim_param
, i
);
205 return isl_reordering_free(r
);
206 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
211 offset
= isl_space_dim(r
->space
, isl_dim_param
);
212 n
= isl_multi_id_size(tuple
);
213 for (i
= 0; i
< n
; ++i
) {
217 id
= isl_multi_id_get_id(tuple
, i
);
219 return isl_reordering_free(r
);
220 pos
= isl_space_find_dim_by_id(space
, isl_dim_param
, id
);
224 r
->pos
[pos
] = offset
+ i
;
227 offset
= isl_space_dim(r
->space
, isl_dim_all
) - r
->len
;
228 first
= isl_space_dim(space
, isl_dim_param
);
230 for (i
= 0; i
< n
; ++i
)
231 r
->pos
[first
+ i
] = first
+ offset
+ i
;
236 __isl_give isl_reordering
*isl_reordering_extend(__isl_take isl_reordering
*exp
,
251 ctx
= isl_reordering_get_ctx(exp
);
252 space
= isl_reordering_peek_space(exp
);
253 dim
= isl_space_dim(space
, isl_dim_all
);
255 return isl_reordering_free(exp
);
256 offset
= dim
- exp
->len
;
257 res
= isl_reordering_alloc(ctx
, exp
->len
+ extra
);
260 res
->space
= isl_reordering_get_space(exp
);
261 for (i
= 0; i
< exp
->len
; ++i
)
262 res
->pos
[i
] = exp
->pos
[i
];
263 for (i
= exp
->len
; i
< res
->len
; ++i
)
264 res
->pos
[i
] = offset
+ i
;
266 isl_reordering_free(exp
);
270 isl_reordering_free(exp
);
274 __isl_give isl_reordering
*isl_reordering_extend_space(
275 __isl_take isl_reordering
*exp
, __isl_take isl_space
*space
)
277 isl_space
*exp_space
;
281 dim
= isl_space_dim(space
, isl_dim_all
);
285 res
= isl_reordering_extend(isl_reordering_copy(exp
), dim
- exp
->len
);
286 res
= isl_reordering_cow(res
);
289 isl_space_free(res
->space
);
290 exp_space
= isl_reordering_peek_space(exp
);
291 res
->space
= isl_space_replace_params(space
, exp_space
);
293 isl_reordering_free(exp
);
296 return isl_reordering_free(res
);
300 isl_reordering_free(exp
);
301 isl_space_free(space
);
305 void isl_reordering_dump(__isl_keep isl_reordering
*exp
)
309 isl_space_dump(exp
->space
);
310 for (i
= 0; i
< exp
->len
; ++i
)
311 fprintf(stderr
, "%d -> %d; ", i
, exp
->pos
[i
]);
312 fprintf(stderr
, "\n");