blob: a4a4609757b080f89fad8e94a5d0324c59247a0f [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// 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#ifndef V8_COMPILER_AST_LOOP_ASSIGNMENT_ANALYZER_H_
6#define V8_COMPILER_AST_LOOP_ASSIGNMENT_ANALYZER_H_
7
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include "src/ast/ast.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009#include "src/bit-vector.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040010#include "src/zone-containers.h"
11
12namespace v8 {
13namespace internal {
14
Ben Murdochc5610432016-08-08 18:44:38 +010015class CompilationInfo;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040016class Scope;
Ben Murdochc5610432016-08-08 18:44:38 +010017class Variable;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018
19namespace compiler {
20
21// The result of analyzing loop assignments.
22class LoopAssignmentAnalysis : public ZoneObject {
23 public:
24 BitVector* GetVariablesAssignedInLoop(IterationStatement* loop) {
25 for (size_t i = 0; i < list_.size(); i++) {
26 // TODO(turbofan): hashmap or binary search for loop assignments.
27 if (list_[i].first == loop) return list_[i].second;
28 }
29 UNREACHABLE(); // should never ask for loops that aren't here!
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000030 return nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040031 }
32
33 int GetAssignmentCountForTesting(Scope* scope, Variable* var);
34
35 private:
36 friend class AstLoopAssignmentAnalyzer;
37 explicit LoopAssignmentAnalysis(Zone* zone) : list_(zone) {}
38 ZoneVector<std::pair<IterationStatement*, BitVector*>> list_;
39};
40
41
42// The class that performs loop assignment analysis by walking the AST.
43class AstLoopAssignmentAnalyzer : public AstVisitor {
44 public:
45 AstLoopAssignmentAnalyzer(Zone* zone, CompilationInfo* info);
46
47 LoopAssignmentAnalysis* Analyze();
48
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000049#define DECLARE_VISIT(type) void Visit##type(type* node) override;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040050 AST_NODE_LIST(DECLARE_VISIT)
51#undef DECLARE_VISIT
52
53 static int GetVariableIndex(Scope* scope, Variable* var);
54
55 private:
56 CompilationInfo* info_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000057 Zone* zone_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040058 ZoneDeque<BitVector*> loop_stack_;
59 LoopAssignmentAnalysis* result_;
60
61 CompilationInfo* info() { return info_; }
62
63 void Enter(IterationStatement* loop);
64 void Exit(IterationStatement* loop);
65
66 void VisitIfNotNull(AstNode* node) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000067 if (node != nullptr) Visit(node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040068 }
69
70 void AnalyzeAssignment(Variable* var);
71
72 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
73 DISALLOW_COPY_AND_ASSIGN(AstLoopAssignmentAnalyzer);
74};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075} // namespace compiler
76} // namespace internal
77} // namespace v8
Emily Bernierd0a1eb72015-03-24 16:35:39 -040078
79#endif // V8_COMPILER_AST_LOOP_ASSIGNMENT_ANALYZER_H_