PetScan::extract_affine(Expr *): stop taking into account nesting_enabled
[pet.git] / scop_plus.cc
bloba67f8aea4e1c922e9efff4b181bce20bad5c6960
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 "scop_plus.h"
42 using namespace std;
43 using namespace clang;
45 /* And the sequence of nested arrays of structures "ancestors"
46 * to "arrays". The final element in the sequence may be a leaf
47 * and may therefore refer to a primitive type rather than a record type.
49 * Futhermore, if the innermost array in the sequence is an array of structures
50 * then recursively call collect_sub_arrays for all subfields of this
51 * structure.
53 static void collect_sub_arrays(ValueDecl *decl, vector<ValueDecl *> ancestors,
54 array_desc_set &arrays)
56 QualType type = decl->getType();
57 RecordDecl *record;
58 RecordDecl::field_iterator it;
60 arrays.insert(ancestors);
62 type = pet_clang_base_type(type);
64 if (!type->isRecordType())
65 return;
67 record = pet_clang_record_decl(type);
69 for (it = record->field_begin(); it != record->field_end(); ++it) {
70 FieldDecl *field = *it;
71 bool anonymous = field->isAnonymousStructOrUnion();
73 if (!anonymous)
74 ancestors.push_back(field);
75 collect_sub_arrays(field, ancestors, arrays);
76 if (!anonymous)
77 ancestors.pop_back();
81 /* Extract one or more sequences of declarations from the access expression
82 * "expr" and them to "arrays".
84 * If "expr" represents an array access, then the extracted sequence
85 * contains a single element corresponding to the array declaration.
86 * Otherwise, if "expr" represents a member access, then the extracted
87 * sequences contain an element for the outer array of structures and
88 * for each nested array or scalar. One such sequence is (recursively)
89 * added for each member of the accessed outer array.
91 * If the array being accessed has a NULL ValueDecl, then it
92 * is a virtual scalar. We don't need to collect such scalars
93 * because they are added to the scop of the statement writing
94 * to the scalar.
96 static void access_collect_arrays(__isl_keep pet_expr *expr,
97 array_desc_set &arrays)
99 isl_id *id;
100 isl_space *space;
101 ValueDecl *decl;
102 vector<ValueDecl *> ancestors;
104 if (pet_expr_is_affine(expr))
105 return;
107 space = pet_expr_access_get_data_space(expr);
109 while (space && isl_space_is_wrapping(space))
110 space = isl_space_domain(isl_space_unwrap(space));
112 id = isl_space_get_tuple_id(space, isl_dim_set);
113 isl_space_free(space);
114 if (!id)
115 return;
117 decl = (ValueDecl *)isl_id_get_user(id);
118 isl_id_free(id);
120 if (!decl)
121 return;
123 ancestors.push_back(decl);
124 collect_sub_arrays(decl, ancestors, arrays);
127 static void expr_collect_arrays(__isl_keep pet_expr *expr,
128 array_desc_set &arrays)
130 int n;
132 if (!expr)
133 return;
135 n = pet_expr_get_n_arg(expr);
136 for (int i = 0; i < n; ++i) {
137 pet_expr *arg;
139 arg = pet_expr_get_arg(expr, i);
140 expr_collect_arrays(arg, arrays);
141 pet_expr_free(arg);
144 if (pet_expr_get_type(expr) == pet_expr_access)
145 access_collect_arrays(expr, arrays);
148 static void stmt_collect_arrays(struct pet_stmt *stmt,
149 array_desc_set &arrays)
151 if (!stmt)
152 return;
154 for (int i = 0; i < stmt->n_arg; ++i)
155 expr_collect_arrays(stmt->args[i], arrays);
157 expr_collect_arrays(stmt->body, arrays);
160 /* Collect the set of all accessed arrays (or scalars) in "scop",
161 * except those that already appear in scop->arrays,
162 * and put them in "arrays".
164 * Each accessed array is represented by a sequence of nested
165 * array declarations, one for the outer array of structures
166 * and one for each member access.
168 * The arrays that already appear in scop->arrays are assumed
169 * to be simple arrays, represented by a sequence of a single element.
171 void pet_scop_collect_arrays(struct pet_scop *scop,
172 array_desc_set &arrays)
174 if (!scop)
175 return;
177 for (int i = 0; i < scop->n_stmt; ++i)
178 stmt_collect_arrays(scop->stmts[i], arrays);
180 for (int i = 0; i < scop->n_array; ++i) {
181 ValueDecl *decl;
182 vector<ValueDecl *> ancestors;
184 isl_id *id = isl_set_get_tuple_id(scop->arrays[i]->extent);
185 decl = (ValueDecl *)isl_id_get_user(id);
186 isl_id_free(id);
188 if (!decl)
189 continue;
191 ancestors.push_back(decl);
193 arrays.erase(ancestors);