add pet_inliner
[pet.git] / scop_plus.cc
blob80f361c784ea94bf3c99f9998b93270414ab035e
1 /*
2 * Copyright 2011 Leiden University. All rights reserved.
3 * Copyright 2013 Ecole Normale Superieure. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY LEIDEN UNIVERSITY ''AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LEIDEN UNIVERSITY OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and documentation
30 * are those of the authors and should not be interpreted as
31 * representing official policies, either expressed or implied, of
32 * Leiden University.
33 */
35 #include <set>
36 #include <vector>
38 #include "clang.h"
39 #include "expr.h"
40 #include "id.h"
41 #include "scop_plus.h"
42 #include "tree.h"
44 using namespace std;
45 using namespace clang;
47 /* Add the sequence of nested arrays of structures "ancestors"
48 * to "arrays". The final element in the sequence may be a leaf
49 * and may therefore refer to a primitive type rather than a record type.
51 * Futhermore, if the innermost array in the sequence is an array of structures
52 * then recursively call collect_sub_arrays for all subfields of this
53 * structure.
55 static void collect_sub_arrays(ValueDecl *decl, vector<ValueDecl *> ancestors,
56 array_desc_set &arrays)
58 QualType type = decl->getType();
59 RecordDecl *record;
60 RecordDecl::field_iterator it;
62 arrays.insert(ancestors);
64 type = pet_clang_base_type(type);
66 if (!type->isRecordType())
67 return;
69 record = pet_clang_record_decl(type);
71 for (it = record->field_begin(); it != record->field_end(); ++it) {
72 FieldDecl *field = *it;
73 bool anonymous = field->isAnonymousStructOrUnion();
75 if (!anonymous)
76 ancestors.push_back(field);
77 collect_sub_arrays(field, ancestors, arrays);
78 if (!anonymous)
79 ancestors.pop_back();
83 /* Extract one or more sequences of declarations from the access expression
84 * "expr" and them to "arrays".
86 * If "expr" represents an array access, then the extracted sequence
87 * contains a single element corresponding to the array declaration.
88 * Otherwise, if "expr" represents a member access, then the extracted
89 * sequences contain an element for the outer array of structures and
90 * for each nested array or scalar. One such sequence is (recursively)
91 * added for each member of the accessed outer array.
93 * If the array being accessed has a NULL ValueDecl, then it
94 * is a virtual scalar. We don't need to collect such scalars
95 * because they are added to the scop of the statement writing
96 * to the scalar.
98 static void access_collect_arrays(__isl_keep pet_expr *expr,
99 array_desc_set &arrays)
101 isl_id *id;
102 isl_space *space;
103 ValueDecl *decl;
104 vector<ValueDecl *> ancestors;
106 if (pet_expr_is_affine(expr))
107 return;
109 space = pet_expr_access_get_data_space(expr);
111 while (space && isl_space_is_wrapping(space))
112 space = isl_space_domain(isl_space_unwrap(space));
114 id = isl_space_get_tuple_id(space, isl_dim_set);
115 isl_space_free(space);
116 if (!id)
117 return;
119 decl = pet_id_get_decl(id);
120 isl_id_free(id);
122 if (!decl)
123 return;
125 ancestors.push_back(decl);
126 collect_sub_arrays(decl, ancestors, arrays);
129 static void expr_collect_arrays(__isl_keep pet_expr *expr,
130 array_desc_set &arrays)
132 int n;
134 if (!expr)
135 return;
137 n = pet_expr_get_n_arg(expr);
138 for (int i = 0; i < n; ++i) {
139 pet_expr *arg;
141 arg = pet_expr_get_arg(expr, i);
142 expr_collect_arrays(arg, arrays);
143 pet_expr_free(arg);
146 if (pet_expr_get_type(expr) == pet_expr_access)
147 access_collect_arrays(expr, arrays);
150 /* Wrapper around access_collect_arrays for use as a callback function
151 * to pet_tree_foreach_access_expr.
153 static int access_collect_wrap(__isl_keep pet_expr *expr, void *user)
155 array_desc_set *arrays = (array_desc_set *) user;
157 access_collect_arrays(expr, *arrays);
159 return 0;
162 static void stmt_collect_arrays(struct pet_stmt *stmt,
163 array_desc_set &arrays)
165 if (!stmt)
166 return;
168 for (int i = 0; i < stmt->n_arg; ++i)
169 expr_collect_arrays(stmt->args[i], arrays);
171 pet_tree_foreach_access_expr(stmt->body, &access_collect_wrap, &arrays);
174 /* Collect the set of all accessed arrays (or scalars) in "scop",
175 * except those that already appear in scop->arrays,
176 * and put them in "arrays".
178 * Each accessed array is represented by a sequence of nested
179 * array declarations, one for the outer array of structures
180 * and one for each member access.
182 * The arrays that already appear in scop->arrays are assumed
183 * to be simple arrays, represented by a sequence of a single element.
185 void pet_scop_collect_arrays(struct pet_scop *scop,
186 array_desc_set &arrays)
188 if (!scop)
189 return;
191 for (int i = 0; i < scop->n_stmt; ++i)
192 stmt_collect_arrays(scop->stmts[i], arrays);
194 for (int i = 0; i < scop->n_array; ++i) {
195 ValueDecl *decl;
196 vector<ValueDecl *> ancestors;
198 isl_id *id = isl_set_get_tuple_id(scop->arrays[i]->extent);
199 decl = pet_id_get_decl(id);
200 isl_id_free(id);
202 if (!decl)
203 continue;
205 ancestors.push_back(decl);
207 arrays.erase(ancestors);