blob: 169691135a97bce8ee00ae2f64c9590bb48160eb [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
15class Variable;
16class Scope;
17
18namespace compiler {
19
20// The result of analyzing loop assignments.
21class LoopAssignmentAnalysis : public ZoneObject {
22 public:
23 BitVector* GetVariablesAssignedInLoop(IterationStatement* loop) {
24 for (size_t i = 0; i < list_.size(); i++) {
25 // TODO(turbofan): hashmap or binary search for loop assignments.
26 if (list_[i].first == loop) return list_[i].second;
27 }
28 UNREACHABLE(); // should never ask for loops that aren't here!
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000029 return nullptr;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040030 }
31
32 int GetAssignmentCountForTesting(Scope* scope, Variable* var);
33
34 private:
35 friend class AstLoopAssignmentAnalyzer;
36 explicit LoopAssignmentAnalysis(Zone* zone) : list_(zone) {}
37 ZoneVector<std::pair<IterationStatement*, BitVector*>> list_;
38};
39
40
41// The class that performs loop assignment analysis by walking the AST.
42class AstLoopAssignmentAnalyzer : public AstVisitor {
43 public:
44 AstLoopAssignmentAnalyzer(Zone* zone, CompilationInfo* info);
45
46 LoopAssignmentAnalysis* Analyze();
47
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000048#define DECLARE_VISIT(type) void Visit##type(type* node) override;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049 AST_NODE_LIST(DECLARE_VISIT)
50#undef DECLARE_VISIT
51
52 static int GetVariableIndex(Scope* scope, Variable* var);
53
54 private:
55 CompilationInfo* info_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056 Zone* zone_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040057 ZoneDeque<BitVector*> loop_stack_;
58 LoopAssignmentAnalysis* result_;
59
60 CompilationInfo* info() { return info_; }
61
62 void Enter(IterationStatement* loop);
63 void Exit(IterationStatement* loop);
64
65 void VisitIfNotNull(AstNode* node) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000066 if (node != nullptr) Visit(node);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040067 }
68
69 void AnalyzeAssignment(Variable* var);
70
71 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
72 DISALLOW_COPY_AND_ASSIGN(AstLoopAssignmentAnalyzer);
73};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074} // namespace compiler
75} // namespace internal
76} // namespace v8
Emily Bernierd0a1eb72015-03-24 16:35:39 -040077
78#endif // V8_COMPILER_AST_LOOP_ASSIGNMENT_ANALYZER_H_