Line data Source code
1 : /*
2 : * Copyright 2010 INRIA Saclay
3 : *
4 : * Use of this software is governed by the MIT license
5 : *
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 : */
10 :
11 : #include <isl_ctx_private.h>
12 : #include <isl/id.h>
13 : #include <isl_space_private.h>
14 : #include <isl_reordering.h>
15 :
16 0 : __isl_give isl_reordering *isl_reordering_alloc(isl_ctx *ctx, int len)
17 : {
18 : isl_reordering *exp;
19 :
20 0 : exp = isl_alloc(ctx, struct isl_reordering,
21 : sizeof(struct isl_reordering) + (len - 1) * sizeof(int));
22 0 : if (!exp)
23 0 : return NULL;
24 :
25 0 : exp->ref = 1;
26 0 : exp->len = len;
27 0 : exp->space = NULL;
28 :
29 0 : return exp;
30 : }
31 :
32 0 : __isl_give isl_reordering *isl_reordering_copy(__isl_keep isl_reordering *exp)
33 : {
34 0 : if (!exp)
35 0 : return NULL;
36 :
37 0 : exp->ref++;
38 0 : return exp;
39 : }
40 :
41 0 : __isl_give isl_reordering *isl_reordering_dup(__isl_keep isl_reordering *r)
42 : {
43 : int i;
44 : isl_reordering *dup;
45 :
46 0 : if (!r)
47 0 : return NULL;
48 :
49 0 : dup = isl_reordering_alloc(isl_reordering_get_ctx(r), r->len);
50 0 : if (!dup)
51 0 : return NULL;
52 :
53 0 : dup->space = isl_reordering_get_space(r);
54 0 : if (!dup->space)
55 0 : return isl_reordering_free(dup);
56 0 : for (i = 0; i < dup->len; ++i)
57 0 : dup->pos[i] = r->pos[i];
58 :
59 0 : return dup;
60 : }
61 :
62 0 : __isl_give isl_reordering *isl_reordering_cow(__isl_take isl_reordering *r)
63 : {
64 0 : if (!r)
65 0 : return NULL;
66 :
67 0 : if (r->ref == 1)
68 0 : return r;
69 0 : r->ref--;
70 0 : return isl_reordering_dup(r);
71 : }
72 :
73 0 : __isl_null isl_reordering *isl_reordering_free(__isl_take isl_reordering *exp)
74 : {
75 0 : if (!exp)
76 0 : return NULL;
77 :
78 0 : if (--exp->ref > 0)
79 0 : return NULL;
80 :
81 0 : isl_space_free(exp->space);
82 0 : free(exp);
83 0 : return NULL;
84 : }
85 :
86 : /* Return the isl_ctx to which "r" belongs.
87 : */
88 0 : isl_ctx *isl_reordering_get_ctx(__isl_keep isl_reordering *r)
89 : {
90 0 : return isl_space_get_ctx(isl_reordering_peek_space(r));
91 : }
92 :
93 : /* Return the space of "r".
94 : */
95 0 : __isl_keep isl_space *isl_reordering_peek_space(__isl_keep isl_reordering *r)
96 : {
97 0 : if (!r)
98 0 : return NULL;
99 0 : return r->space;
100 : }
101 :
102 : /* Return a copy of the space of "r".
103 : */
104 0 : __isl_give isl_space *isl_reordering_get_space(__isl_keep isl_reordering *r)
105 : {
106 0 : return isl_space_copy(isl_reordering_peek_space(r));
107 : }
108 :
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".
113 : */
114 0 : __isl_give isl_reordering *isl_parameter_alignment_reordering(
115 : __isl_keep isl_space *alignee, __isl_keep isl_space *aligner)
116 : {
117 : int i, j;
118 : isl_reordering *exp;
119 :
120 0 : if (!alignee || !aligner)
121 0 : return NULL;
122 :
123 0 : exp = isl_reordering_alloc(alignee->ctx, alignee->nparam);
124 0 : if (!exp)
125 0 : return NULL;
126 :
127 0 : exp->space = isl_space_params(isl_space_copy(aligner));
128 :
129 0 : for (i = 0; i < alignee->nparam; ++i) {
130 : isl_id *id_i;
131 0 : id_i = isl_space_get_dim_id(alignee, isl_dim_param, i);
132 0 : if (!id_i)
133 0 : isl_die(alignee->ctx, isl_error_invalid,
134 : "cannot align unnamed parameters", goto error);
135 0 : for (j = 0; j < aligner->nparam; ++j) {
136 : isl_id *id_j;
137 0 : id_j = isl_space_get_dim_id(aligner, isl_dim_param, j);
138 0 : isl_id_free(id_j);
139 0 : if (id_i == id_j)
140 0 : break;
141 : }
142 0 : if (j < aligner->nparam) {
143 0 : exp->pos[i] = j;
144 0 : isl_id_free(id_i);
145 : } else {
146 : int pos;
147 0 : pos = isl_space_dim(exp->space, isl_dim_param);
148 0 : exp->space = isl_space_add_dims(exp->space,
149 : isl_dim_param, 1);
150 0 : exp->space = isl_space_set_dim_id(exp->space,
151 : isl_dim_param, pos, id_i);
152 0 : exp->pos[i] = pos;
153 : }
154 : }
155 :
156 0 : if (!exp->space)
157 0 : return isl_reordering_free(exp);
158 0 : return exp;
159 : error:
160 0 : isl_reordering_free(exp);
161 0 : return NULL;
162 : }
163 :
164 0 : __isl_give isl_reordering *isl_reordering_extend(__isl_take isl_reordering *exp,
165 : unsigned extra)
166 : {
167 : int i;
168 : isl_ctx *ctx;
169 : isl_space *space;
170 : isl_reordering *res;
171 : int offset;
172 :
173 0 : if (!exp)
174 0 : return NULL;
175 0 : if (extra == 0)
176 0 : return exp;
177 :
178 0 : ctx = isl_reordering_get_ctx(exp);
179 0 : space = isl_reordering_peek_space(exp);
180 0 : offset = isl_space_dim(space, isl_dim_all) - exp->len;
181 0 : res = isl_reordering_alloc(ctx, exp->len + extra);
182 0 : if (!res)
183 0 : goto error;
184 0 : res->space = isl_reordering_get_space(exp);
185 0 : for (i = 0; i < exp->len; ++i)
186 0 : res->pos[i] = exp->pos[i];
187 0 : for (i = exp->len; i < res->len; ++i)
188 0 : res->pos[i] = offset + i;
189 :
190 0 : isl_reordering_free(exp);
191 :
192 0 : return res;
193 : error:
194 0 : isl_reordering_free(exp);
195 0 : return NULL;
196 : }
197 :
198 0 : __isl_give isl_reordering *isl_reordering_extend_space(
199 : __isl_take isl_reordering *exp, __isl_take isl_space *space)
200 : {
201 : isl_space *exp_space;
202 : isl_reordering *res;
203 :
204 0 : if (!exp || !space)
205 : goto error;
206 :
207 0 : res = isl_reordering_extend(isl_reordering_copy(exp),
208 0 : isl_space_dim(space, isl_dim_all) - exp->len);
209 0 : res = isl_reordering_cow(res);
210 0 : if (!res)
211 0 : goto error;
212 0 : isl_space_free(res->space);
213 0 : exp_space = isl_reordering_peek_space(exp);
214 0 : res->space = isl_space_replace_params(space, exp_space);
215 :
216 0 : isl_reordering_free(exp);
217 :
218 0 : if (!res->space)
219 0 : return isl_reordering_free(res);
220 :
221 0 : return res;
222 : error:
223 0 : isl_reordering_free(exp);
224 0 : isl_space_free(space);
225 0 : return NULL;
226 : }
227 :
228 0 : void isl_reordering_dump(__isl_keep isl_reordering *exp)
229 : {
230 : int i;
231 :
232 0 : isl_space_dump(exp->space);
233 0 : for (i = 0; i < exp->len; ++i)
234 0 : fprintf(stderr, "%d -> %d; ", i, exp->pos[i]);
235 0 : fprintf(stderr, "\n");
236 0 : }
|