Line data Source code
1 : // Copyright 2014 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #include "src/compiler/ast-loop-assignment-analyzer.h"
6 : #include "src/ast/scopes.h"
7 : #include "src/compilation-info.h"
8 : #include "src/objects-inl.h"
9 :
10 : namespace v8 {
11 : namespace internal {
12 : namespace compiler {
13 :
14 : typedef class AstLoopAssignmentAnalyzer ALAA; // for code shortitude.
15 :
16 6078 : ALAA::AstLoopAssignmentAnalyzer(Zone* zone, CompilationInfo* info)
17 3039 : : info_(info), zone_(zone), loop_stack_(zone) {
18 : InitializeAstVisitor(info->isolate());
19 3039 : }
20 :
21 :
22 6078 : LoopAssignmentAnalysis* ALAA::Analyze() {
23 6078 : LoopAssignmentAnalysis* a = new (zone_) LoopAssignmentAnalysis(zone_);
24 3039 : result_ = a;
25 3039 : VisitStatements(info()->literal()->body());
26 3039 : result_ = nullptr;
27 3039 : return a;
28 : }
29 :
30 :
31 4329 : void ALAA::Enter(IterationStatement* loop) {
32 2886 : int num_variables = 1 + info()->scope()->num_parameters() +
33 1443 : info()->scope()->num_stack_slots();
34 2886 : BitVector* bits = new (zone_) BitVector(num_variables, zone_);
35 1646 : if (info()->is_osr() && info()->osr_ast_id() == loop->OsrEntryId())
36 68 : bits->AddAll();
37 1443 : loop_stack_.push_back(bits);
38 1443 : }
39 :
40 :
41 1443 : void ALAA::Exit(IterationStatement* loop) {
42 : DCHECK(loop_stack_.size() > 0);
43 1443 : BitVector* bits = loop_stack_.back();
44 1443 : loop_stack_.pop_back();
45 1443 : if (!loop_stack_.empty()) {
46 517 : loop_stack_.back()->Union(*bits);
47 : }
48 : result_->list_.push_back(
49 2886 : std::pair<IterationStatement*, BitVector*>(loop, bits));
50 1443 : }
51 :
52 :
53 : // ---------------------------------------------------------------------------
54 : // -- Leaf nodes -------------------------------------------------------------
55 : // ---------------------------------------------------------------------------
56 :
57 0 : void ALAA::VisitVariableDeclaration(VariableDeclaration* leaf) {}
58 0 : void ALAA::VisitFunctionDeclaration(FunctionDeclaration* leaf) {}
59 0 : void ALAA::VisitEmptyStatement(EmptyStatement* leaf) {}
60 0 : void ALAA::VisitContinueStatement(ContinueStatement* leaf) {}
61 0 : void ALAA::VisitBreakStatement(BreakStatement* leaf) {}
62 0 : void ALAA::VisitDebuggerStatement(DebuggerStatement* leaf) {}
63 0 : void ALAA::VisitFunctionLiteral(FunctionLiteral* leaf) {}
64 0 : void ALAA::VisitNativeFunctionLiteral(NativeFunctionLiteral* leaf) {}
65 0 : void ALAA::VisitVariableProxy(VariableProxy* leaf) {}
66 0 : void ALAA::VisitLiteral(Literal* leaf) {}
67 0 : void ALAA::VisitRegExpLiteral(RegExpLiteral* leaf) {}
68 0 : void ALAA::VisitThisFunction(ThisFunction* leaf) {}
69 0 : void ALAA::VisitSuperPropertyReference(SuperPropertyReference* leaf) {}
70 0 : void ALAA::VisitSuperCallReference(SuperCallReference* leaf) {}
71 :
72 :
73 : // ---------------------------------------------------------------------------
74 : // -- Pass-through nodes------------------------------------------------------
75 : // ---------------------------------------------------------------------------
76 6003 : void ALAA::VisitBlock(Block* stmt) { VisitStatements(stmt->statements()); }
77 :
78 :
79 0 : void ALAA::VisitDoExpression(DoExpression* expr) {
80 0 : Visit(expr->block());
81 0 : Visit(expr->result());
82 0 : }
83 :
84 :
85 22929 : void ALAA::VisitExpressionStatement(ExpressionStatement* stmt) {
86 22929 : Visit(stmt->expression());
87 22929 : }
88 :
89 :
90 8925 : void ALAA::VisitIfStatement(IfStatement* stmt) {
91 2975 : Visit(stmt->condition());
92 2975 : Visit(stmt->then_statement());
93 2975 : Visit(stmt->else_statement());
94 2975 : }
95 :
96 :
97 2670 : void ALAA::VisitReturnStatement(ReturnStatement* stmt) {
98 2670 : Visit(stmt->expression());
99 2670 : }
100 :
101 :
102 0 : void ALAA::VisitWithStatement(WithStatement* stmt) {
103 0 : Visit(stmt->expression());
104 0 : Visit(stmt->statement());
105 0 : }
106 :
107 :
108 236 : void ALAA::VisitSwitchStatement(SwitchStatement* stmt) {
109 118 : Visit(stmt->tag());
110 : ZoneList<CaseClause*>* clauses = stmt->cases();
111 1194 : for (int i = 0; i < clauses->length(); i++) {
112 1076 : Visit(clauses->at(i));
113 : }
114 118 : }
115 :
116 :
117 56 : void ALAA::VisitTryFinallyStatement(TryFinallyStatement* stmt) {
118 28 : Visit(stmt->try_block());
119 28 : Visit(stmt->finally_block());
120 28 : }
121 :
122 :
123 0 : void ALAA::VisitClassLiteral(ClassLiteral* e) {
124 : VisitIfNotNull(e->extends());
125 : VisitIfNotNull(e->constructor());
126 : ZoneList<ClassLiteralProperty*>* properties = e->properties();
127 0 : for (int i = 0; i < properties->length(); i++) {
128 0 : Visit(properties->at(i)->key());
129 0 : Visit(properties->at(i)->value());
130 : }
131 0 : }
132 :
133 :
134 1773 : void ALAA::VisitConditional(Conditional* e) {
135 591 : Visit(e->condition());
136 591 : Visit(e->then_expression());
137 591 : Visit(e->else_expression());
138 591 : }
139 :
140 :
141 22 : void ALAA::VisitObjectLiteral(ObjectLiteral* e) {
142 : ZoneList<ObjectLiteralProperty*>* properties = e->properties();
143 144 : for (int i = 0; i < properties->length(); i++) {
144 122 : Visit(properties->at(i)->key());
145 50 : Visit(properties->at(i)->value());
146 : }
147 22 : }
148 :
149 :
150 44 : void ALAA::VisitArrayLiteral(ArrayLiteral* e) { VisitExpressions(e->values()); }
151 :
152 0 : void ALAA::VisitSuspend(Suspend* stmt) {
153 0 : Visit(stmt->generator_object());
154 0 : Visit(stmt->expression());
155 0 : }
156 :
157 :
158 30 : void ALAA::VisitThrow(Throw* stmt) { Visit(stmt->exception()); }
159 :
160 :
161 21158 : void ALAA::VisitProperty(Property* e) {
162 10579 : Visit(e->obj());
163 10579 : Visit(e->key());
164 10579 : }
165 :
166 :
167 3178 : void ALAA::VisitCall(Call* e) {
168 1589 : Visit(e->expression());
169 1589 : VisitExpressions(e->arguments());
170 1589 : }
171 :
172 :
173 36 : void ALAA::VisitCallNew(CallNew* e) {
174 18 : Visit(e->expression());
175 18 : VisitExpressions(e->arguments());
176 18 : }
177 :
178 :
179 45 : void ALAA::VisitCallRuntime(CallRuntime* e) {
180 45 : VisitExpressions(e->arguments());
181 45 : }
182 :
183 :
184 965 : void ALAA::VisitUnaryOperation(UnaryOperation* e) { Visit(e->expression()); }
185 :
186 :
187 95660 : void ALAA::VisitBinaryOperation(BinaryOperation* e) {
188 47830 : Visit(e->left());
189 47830 : Visit(e->right());
190 47830 : }
191 :
192 :
193 7688 : void ALAA::VisitCompareOperation(CompareOperation* e) {
194 3844 : Visit(e->left());
195 3844 : Visit(e->right());
196 3844 : }
197 :
198 :
199 0 : void ALAA::VisitSpread(Spread* e) { UNREACHABLE(); }
200 :
201 :
202 0 : void ALAA::VisitEmptyParentheses(EmptyParentheses* e) { UNREACHABLE(); }
203 :
204 0 : void ALAA::VisitGetIterator(GetIterator* e) { UNREACHABLE(); }
205 :
206 0 : void ALAA::VisitImportCallExpression(ImportCallExpression* e) { UNREACHABLE(); }
207 :
208 958 : void ALAA::VisitCaseClause(CaseClause* cc) {
209 479 : if (!cc->is_default()) Visit(cc->label());
210 479 : VisitStatements(cc->statements());
211 479 : }
212 :
213 :
214 0 : void ALAA::VisitSloppyBlockFunctionStatement(
215 0 : SloppyBlockFunctionStatement* stmt) {
216 0 : Visit(stmt->statement());
217 0 : }
218 :
219 :
220 : // ---------------------------------------------------------------------------
221 : // -- Interesting nodes-------------------------------------------------------
222 : // ---------------------------------------------------------------------------
223 84 : void ALAA::VisitTryCatchStatement(TryCatchStatement* stmt) {
224 28 : Visit(stmt->try_block());
225 28 : Visit(stmt->catch_block());
226 28 : AnalyzeAssignment(stmt->scope()->catch_variable());
227 28 : }
228 :
229 :
230 884 : void ALAA::VisitDoWhileStatement(DoWhileStatement* loop) {
231 442 : Enter(loop);
232 442 : Visit(loop->body());
233 442 : Visit(loop->cond());
234 442 : Exit(loop);
235 442 : }
236 :
237 :
238 1826 : void ALAA::VisitWhileStatement(WhileStatement* loop) {
239 913 : Enter(loop);
240 913 : Visit(loop->cond());
241 913 : Visit(loop->body());
242 913 : Exit(loop);
243 913 : }
244 :
245 :
246 201 : void ALAA::VisitForStatement(ForStatement* loop) {
247 : VisitIfNotNull(loop->init());
248 67 : Enter(loop);
249 : VisitIfNotNull(loop->cond());
250 67 : Visit(loop->body());
251 : VisitIfNotNull(loop->next());
252 67 : Exit(loop);
253 67 : }
254 :
255 :
256 42 : void ALAA::VisitForInStatement(ForInStatement* loop) {
257 : Expression* l = loop->each();
258 21 : Enter(loop);
259 21 : Visit(l);
260 21 : Visit(loop->subject());
261 21 : Visit(loop->body());
262 42 : if (l->IsVariableProxy()) AnalyzeAssignment(l->AsVariableProxy()->var());
263 21 : Exit(loop);
264 21 : }
265 :
266 :
267 0 : void ALAA::VisitForOfStatement(ForOfStatement* loop) {
268 0 : Visit(loop->assign_iterator());
269 0 : Enter(loop);
270 0 : Visit(loop->next_result());
271 0 : Visit(loop->result_done());
272 0 : Visit(loop->assign_each());
273 0 : Visit(loop->body());
274 0 : Exit(loop);
275 0 : }
276 :
277 :
278 43548 : void ALAA::VisitAssignment(Assignment* stmt) {
279 : Expression* l = stmt->target();
280 21774 : Visit(l);
281 21774 : Visit(stmt->value());
282 39368 : if (l->IsVariableProxy()) AnalyzeAssignment(l->AsVariableProxy()->var());
283 21774 : }
284 :
285 :
286 333 : void ALAA::VisitCountOperation(CountOperation* e) {
287 : Expression* l = e->expression();
288 333 : Visit(l);
289 666 : if (l->IsVariableProxy()) AnalyzeAssignment(l->AsVariableProxy()->var());
290 333 : }
291 :
292 :
293 0 : void ALAA::VisitRewritableExpression(RewritableExpression* expr) {
294 0 : Visit(expr->expression());
295 0 : }
296 :
297 :
298 26140 : void ALAA::AnalyzeAssignment(Variable* var) {
299 26229 : if (!loop_stack_.empty() && var->IsStackAllocated()) {
300 16328 : loop_stack_.back()->Add(GetVariableIndex(info()->scope(), var));
301 : }
302 17976 : }
303 :
304 17658 : int ALAA::GetVariableIndex(DeclarationScope* scope, Variable* var) {
305 8829 : CHECK(var->IsStackAllocated());
306 8829 : if (var->is_this()) return 0;
307 10136 : if (var->IsParameter()) return 1 + var->index();
308 15044 : return 1 + scope->num_parameters() + var->index();
309 : }
310 :
311 665 : int LoopAssignmentAnalysis::GetAssignmentCountForTesting(
312 : DeclarationScope* scope, Variable* var) {
313 : int count = 0;
314 665 : int var_index = AstLoopAssignmentAnalyzer::GetVariableIndex(scope, var);
315 3164 : for (size_t i = 0; i < list_.size(); i++) {
316 3416 : if (list_[i].second->Contains(var_index)) count++;
317 : }
318 665 : return count;
319 : }
320 : } // namespace compiler
321 : } // namespace internal
322 : } // namespace v8
|