-
Notifications
You must be signed in to change notification settings - Fork 9
/
scop_plus.cc
302 lines (265 loc) · 9.07 KB
/
scop_plus.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright 2011 Leiden University. All rights reserved.
* Copyright 2013 Ecole Normale Superieure. All rights reserved.
* Copyright 2017 Sven Verdoolaege. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation
* are those of the authors and should not be interpreted as
* representing official policies, either expressed or implied, of
* Leiden University.
*/
#include <set>
#include <vector>
#include "clang.h"
#include "expr.h"
#include "id.h"
#include "scop_plus.h"
#include "tree.h"
using namespace std;
using namespace clang;
/* Add the sequence of nested arrays of structures of the direct
* subfields of the record type represented by "ancestors"
* to "arrays". The final element in the sequence is guaranteed
* to refer to a record type.
*
* If any of the subfields is anonymous, then add its subfields as well.
*/
static void collect_direct_sub_arrays(ValueDecl *decl,
__isl_keep isl_id_list *ancestors, array_desc_set &arrays)
{
isl_ctx *ctx;
QualType type = decl->getType();
RecordDecl *record;
RecordDecl::field_iterator it;
type = pet_clang_base_type(type);
record = pet_clang_record_decl(type);
ctx = isl_id_list_get_ctx(ancestors);
for (it = record->field_begin(); it != record->field_end(); ++it) {
FieldDecl *field = *it;
bool anonymous = field->isAnonymousStructOrUnion();
isl_id *id;
isl_id_list *extended;
if (anonymous) {
collect_direct_sub_arrays(field, ancestors, arrays);
continue;
}
extended = isl_id_list_copy(ancestors);
id = pet_id_from_decl(ctx, field);
extended = isl_id_list_add(extended, id);
arrays.insert(extended);
}
}
/* Add the sequence of nested array declarations "list" to "arrays".
*
* If "list" represents a member access (i.e., the list has at least
* two elements), then also add the other members in each of its
* outer arrays.
*/
static void add_with_siblings(__isl_take isl_id_list *list,
array_desc_set &arrays)
{
int n;
arrays.insert(isl_id_list_copy(list));
n = isl_id_list_n_id(list);
while (n > 1) {
isl_id *id;
ValueDecl *decl;
list = isl_id_list_drop(list, --n, 1);
arrays.insert(isl_id_list_copy(list));
id = isl_id_list_get_id(list, n - 1);
decl = pet_id_get_decl(id);
isl_id_free(id);
collect_direct_sub_arrays(decl, list, arrays);
}
isl_id_list_free(list);
}
/* Construct a sequence of nested array declarations containing
* a single element corresponding to the tuple identifier
* of the set space "space".
*
* If the array being accessed has a NULL ValueDecl, then it
* is a virtual scalar. These do not need to be collected
* because they are added to the scop of the statement writing
* to the scalar. Return an empty list instead.
*/
static __isl_give isl_id_list *extract_list_from_tuple_id(
__isl_keep isl_space *space)
{
isl_ctx *ctx;
isl_id *id;
id = isl_space_get_tuple_id(space, isl_dim_set);
if (pet_id_get_decl(id))
return isl_id_list_from_id(id);
isl_id_free(id);
ctx = isl_space_get_ctx(space);
return isl_id_list_alloc(ctx, 0);
}
/* Construct a sequence of nested array declarations corresponding
* to the accessed data space "space".
*
* If "space" represents an array access, then the extracted sequence
* contains a single element corresponding to the array declaration.
* Otherwise, if "space" represents a member access, then the extracted
* sequence contains an element for the outer array of structures and
* for each nested array or scalar.
*
* If the array being accessed has a NULL ValueDecl, then it
* is a virtual scalar. These do not need to be collected
* because they are added to the scop of the statement writing
* to the scalar. Return an empty list instead.
*/
static __isl_give isl_id_list *extract_list(__isl_keep isl_space *space)
{
isl_bool is_wrapping;
isl_space *range;
isl_id_list *list;
is_wrapping = isl_space_is_wrapping(space);
if (is_wrapping < 0)
return NULL;
if (!is_wrapping)
return extract_list_from_tuple_id(space);
space = isl_space_unwrap(isl_space_copy(space));
range = isl_space_range(isl_space_copy(space));
list = extract_list(range);
isl_space_free(range);
space = isl_space_domain(space);
list = isl_id_list_concat(extract_list(space), list);
isl_space_free(space);
return list;
}
/* Extract one or more sequences of declarations from the accessed
* data space "space" and add them to "arrays".
*
* If "space" represents an array access, then the extracted sequence
* contains a single element corresponding to the array declaration.
* Otherwise, if "space" represents a member access, then the extracted
* sequences contain an element for the outer array of structures and
* for each nested array or scalar. If such a sequence is constructed
* corresponding to a given member access, then such sequences are
* also constructed for the other members in the same structure.
*
* If the array being accessed has a NULL ValueDecl, then it
* is a virtual scalar. We don't need to collect such scalars
* because they are added to the scop of the statement writing
* to the scalar. extract_list returns an empty list for
* such arrays.
*
* If the sequence corresponding to "space" already appears
* in "arrays", then its siblings have already been added as well,
* so there is nothing left to do.
*/
static isl_stat space_collect_arrays(__isl_take isl_space *space, void *user)
{
array_desc_set *arrays = (array_desc_set *) user;
int n;
isl_id_list *list;
list = extract_list(space);
n = isl_id_list_n_id(list);
if (n > 0 && arrays->find(list) == arrays->end())
add_with_siblings(list, *arrays);
else
isl_id_list_free(list);
isl_space_free(space);
return isl_stat_ok;
}
/* Extract one or more sequences of declarations from the access expression
* "expr" and add them to "arrays".
*/
static void access_collect_arrays(__isl_keep pet_expr *expr,
array_desc_set &arrays)
{
if (pet_expr_is_affine(expr))
return;
pet_expr_access_foreach_data_space(expr,
&space_collect_arrays, &arrays);
}
static void expr_collect_arrays(__isl_keep pet_expr *expr,
array_desc_set &arrays)
{
int n;
if (!expr)
return;
n = pet_expr_get_n_arg(expr);
for (int i = 0; i < n; ++i) {
pet_expr *arg;
arg = pet_expr_get_arg(expr, i);
expr_collect_arrays(arg, arrays);
pet_expr_free(arg);
}
if (pet_expr_get_type(expr) == pet_expr_access)
access_collect_arrays(expr, arrays);
}
/* Wrapper around access_collect_arrays for use as a callback function
* to pet_tree_foreach_access_expr.
*/
static int access_collect_wrap(__isl_keep pet_expr *expr, void *user)
{
array_desc_set *arrays = (array_desc_set *) user;
access_collect_arrays(expr, *arrays);
return 0;
}
static void stmt_collect_arrays(struct pet_stmt *stmt,
array_desc_set &arrays)
{
if (!stmt)
return;
for (unsigned i = 0; i < stmt->n_arg; ++i)
expr_collect_arrays(stmt->args[i], arrays);
pet_tree_foreach_access_expr(stmt->body, &access_collect_wrap, &arrays);
}
/* Collect the set of all accessed arrays (or scalars) in "scop",
* except those that already appear in scop->arrays,
* and put them in "arrays".
*
* Each accessed array is represented by a sequence of nested
* array declarations, one for the outer array of structures
* and one for each member access.
*
* The arrays that already appear in scop->arrays are assumed
* to be simple arrays, represented by a sequence of a single element.
*/
void pet_scop_collect_arrays(struct pet_scop *scop,
array_desc_set &arrays)
{
if (!scop)
return;
for (int i = 0; i < scop->n_stmt; ++i)
stmt_collect_arrays(scop->stmts[i], arrays);
for (int i = 0; i < scop->n_array; ++i) {
ValueDecl *decl;
isl_id_list *ancestors;
isl_id *id = isl_set_get_tuple_id(scop->arrays[i]->extent);
decl = pet_id_get_decl(id);
if (!decl) {
isl_id_free(id);
continue;
}
ancestors = isl_id_list_from_id(id);
arrays.erase(ancestors);
isl_id_list_free(ancestors);
}
}