isl_scheduler.c: add missing include
[isl.git] / isl_reordering.c
blob2b247c7116c1a8d2f214a4777b4fceb6f70ca8b6
1 /*
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,
8 * 91893 Orsay, France
9 */
11 #include <isl_ctx_private.h>
12 #include <isl/id.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)
18 isl_reordering *exp;
20 exp = isl_alloc(ctx, struct isl_reordering,
21 sizeof(struct isl_reordering) + (len - 1) * sizeof(int));
22 if (!exp)
23 return NULL;
25 exp->ref = 1;
26 exp->len = len;
27 exp->dim = NULL;
29 return exp;
32 __isl_give isl_reordering *isl_reordering_copy(__isl_keep isl_reordering *exp)
34 if (!exp)
35 return NULL;
37 exp->ref++;
38 return exp;
41 __isl_give isl_reordering *isl_reordering_dup(__isl_keep isl_reordering *r)
43 int i;
44 isl_reordering *dup;
46 if (!r)
47 return NULL;
49 dup = isl_reordering_alloc(r->dim->ctx, r->len);
50 if (!dup)
51 return NULL;
53 dup->dim = isl_space_copy(r->dim);
54 if (!dup->dim)
55 return isl_reordering_free(dup);
56 for (i = 0; i < dup->len; ++i)
57 dup->pos[i] = r->pos[i];
59 return dup;
62 __isl_give isl_reordering *isl_reordering_cow(__isl_take isl_reordering *r)
64 if (!r)
65 return NULL;
67 if (r->ref == 1)
68 return r;
69 r->ref--;
70 return isl_reordering_dup(r);
73 void *isl_reordering_free(__isl_take isl_reordering *exp)
75 if (!exp)
76 return NULL;
78 if (--exp->ref > 0)
79 return NULL;
81 isl_space_free(exp->dim);
82 free(exp);
83 return NULL;
86 /* Construct a reordering that maps the parameters of "alignee"
87 * to the corresponding parameters in a new dimension specification
88 * that has the parameters of "aligner" first, followed by
89 * any remaining parameters of "alignee" that do not occur in "aligner".
91 __isl_give isl_reordering *isl_parameter_alignment_reordering(
92 __isl_keep isl_space *alignee, __isl_keep isl_space *aligner)
94 int i, j;
95 isl_reordering *exp;
97 if (!alignee || !aligner)
98 return NULL;
100 exp = isl_reordering_alloc(alignee->ctx, alignee->nparam);
101 if (!exp)
102 return NULL;
104 exp->dim = isl_space_copy(aligner);
106 for (i = 0; i < alignee->nparam; ++i) {
107 isl_id *id_i;
108 id_i = isl_space_get_dim_id(alignee, isl_dim_param, i);
109 if (!id_i)
110 isl_die(alignee->ctx, isl_error_invalid,
111 "cannot align unnamed parameters", goto error);
112 for (j = 0; j < aligner->nparam; ++j) {
113 isl_id *id_j;
114 id_j = isl_space_get_dim_id(aligner, isl_dim_param, j);
115 isl_id_free(id_j);
116 if (id_i == id_j)
117 break;
119 if (j < aligner->nparam) {
120 exp->pos[i] = j;
121 isl_id_free(id_i);
122 } else {
123 int pos;
124 pos = isl_space_dim(exp->dim, isl_dim_param);
125 exp->dim = isl_space_add_dims(exp->dim, isl_dim_param, 1);
126 exp->dim = isl_space_set_dim_id(exp->dim,
127 isl_dim_param, pos, id_i);
128 exp->pos[i] = pos;
132 if (!exp->dim)
133 return isl_reordering_free(exp);
134 return exp;
135 error:
136 isl_reordering_free(exp);
137 return NULL;
140 __isl_give isl_reordering *isl_reordering_extend(__isl_take isl_reordering *exp,
141 unsigned extra)
143 int i;
144 isl_reordering *res;
145 int offset;
147 if (!exp)
148 return NULL;
149 if (extra == 0)
150 return exp;
152 offset = isl_space_dim(exp->dim, isl_dim_all) - exp->len;
153 res = isl_reordering_alloc(exp->dim->ctx, exp->len + extra);
154 if (!res)
155 goto error;
156 res->dim = isl_space_copy(exp->dim);
157 for (i = 0; i < exp->len; ++i)
158 res->pos[i] = exp->pos[i];
159 for (i = exp->len; i < res->len; ++i)
160 res->pos[i] = offset + i;
162 isl_reordering_free(exp);
164 return res;
165 error:
166 isl_reordering_free(exp);
167 return NULL;
170 __isl_give isl_reordering *isl_reordering_extend_space(
171 __isl_take isl_reordering *exp, __isl_take isl_space *space)
173 isl_reordering *res;
175 if (!exp || !space)
176 goto error;
178 res = isl_reordering_extend(isl_reordering_copy(exp),
179 isl_space_dim(space, isl_dim_all) - exp->len);
180 res = isl_reordering_cow(res);
181 if (!res)
182 goto error;
183 isl_space_free(res->dim);
184 res->dim = isl_space_replace_params(space, exp->dim);
186 isl_reordering_free(exp);
188 if (!res->dim)
189 return isl_reordering_free(res);
191 return res;
192 error:
193 isl_reordering_free(exp);
194 isl_space_free(space);
195 return NULL;
198 void isl_reordering_dump(__isl_keep isl_reordering *exp)
200 int i;
202 isl_space_dump(exp->dim);
203 for (i = 0; i < exp->len; ++i)
204 fprintf(stderr, "%d -> %d; ", i, exp->pos[i]);
205 fprintf(stderr, "\n");