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>
12 #include <isl_space_private.h>
13 #include <isl_reordering.h>
15 __isl_give isl_reordering
*isl_reordering_alloc(isl_ctx
*ctx
, int len
)
19 exp
= isl_alloc(ctx
, struct isl_reordering
,
20 sizeof(struct isl_reordering
) + (len
- 1) * sizeof(int));
31 __isl_give isl_reordering
*isl_reordering_copy(__isl_keep isl_reordering
*exp
)
40 __isl_give isl_reordering
*isl_reordering_dup(__isl_keep isl_reordering
*r
)
48 dup
= isl_reordering_alloc(r
->dim
->ctx
, r
->len
);
52 dup
->dim
= isl_space_copy(r
->dim
);
54 return isl_reordering_free(dup
);
55 for (i
= 0; i
< dup
->len
; ++i
)
56 dup
->pos
[i
] = r
->pos
[i
];
61 __isl_give isl_reordering
*isl_reordering_cow(__isl_take isl_reordering
*r
)
69 return isl_reordering_dup(r
);
72 void *isl_reordering_free(__isl_take isl_reordering
*exp
)
80 isl_space_free(exp
->dim
);
85 /* Construct a reordering that maps the parameters of "alignee"
86 * to the corresponding parameters in a new dimension specification
87 * that has the parameters of "aligner" first, followed by
88 * any remaining parameters of "alignee" that do not occur in "aligner".
90 __isl_give isl_reordering
*isl_parameter_alignment_reordering(
91 __isl_keep isl_space
*alignee
, __isl_keep isl_space
*aligner
)
96 if (!alignee
|| !aligner
)
99 exp
= isl_reordering_alloc(alignee
->ctx
, alignee
->nparam
);
103 exp
->dim
= isl_space_copy(aligner
);
105 for (i
= 0; i
< alignee
->nparam
; ++i
) {
107 id_i
= isl_space_get_dim_id(alignee
, isl_dim_param
, i
);
109 isl_die(alignee
->ctx
, isl_error_invalid
,
110 "cannot align unnamed parameters", goto error
);
111 for (j
= 0; j
< aligner
->nparam
; ++j
) {
113 id_j
= isl_space_get_dim_id(aligner
, isl_dim_param
, j
);
118 if (j
< aligner
->nparam
) {
123 pos
= isl_space_dim(exp
->dim
, isl_dim_param
);
124 exp
->dim
= isl_space_add_dims(exp
->dim
, isl_dim_param
, 1);
125 exp
->dim
= isl_space_set_dim_id(exp
->dim
,
126 isl_dim_param
, pos
, id_i
);
132 return isl_reordering_free(exp
);
135 isl_reordering_free(exp
);
139 __isl_give isl_reordering
*isl_reordering_extend(__isl_take isl_reordering
*exp
,
151 offset
= isl_space_dim(exp
->dim
, isl_dim_all
) - exp
->len
;
152 res
= isl_reordering_alloc(exp
->dim
->ctx
, exp
->len
+ extra
);
155 res
->dim
= isl_space_copy(exp
->dim
);
156 for (i
= 0; i
< exp
->len
; ++i
)
157 res
->pos
[i
] = exp
->pos
[i
];
158 for (i
= exp
->len
; i
< res
->len
; ++i
)
159 res
->pos
[i
] = offset
+ i
;
161 isl_reordering_free(exp
);
165 isl_reordering_free(exp
);
169 __isl_give isl_reordering
*isl_reordering_extend_space(
170 __isl_take isl_reordering
*exp
, __isl_take isl_space
*dim
)
177 res
= isl_reordering_extend(isl_reordering_copy(exp
),
178 isl_space_dim(dim
, isl_dim_all
) - exp
->len
);
179 res
= isl_reordering_cow(res
);
182 isl_space_free(res
->dim
);
183 res
->dim
= isl_space_replace(dim
, isl_dim_param
, exp
->dim
);
185 isl_reordering_free(exp
);
188 return isl_reordering_free(res
);
192 isl_reordering_free(exp
);
197 void isl_reordering_dump(__isl_keep isl_reordering
*exp
)
201 isl_space_dump(exp
->dim
);
202 for (i
= 0; i
< exp
->len
; ++i
)
203 fprintf(stderr
, "%d -> %d; ", i
, exp
->pos
[i
]);
204 fprintf(stderr
, "\n");