blob: 00a7f2d79d4387a98d85a004b57a1829e407ac96 [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
8#include "src/ast.h"
9#include "src/bit-vector.h"
10#include "src/v8.h"
11#include "src/zone-containers.h"
12
13namespace v8 {
14namespace internal {
15
16class Variable;
17class Scope;
18
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!
30 return NULL;
31 }
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
49#define DECLARE_VISIT(type) void Visit##type(type* node) OVERRIDE;
50 AST_NODE_LIST(DECLARE_VISIT)
51#undef DECLARE_VISIT
52
53 static int GetVariableIndex(Scope* scope, Variable* var);
54
55 private:
56 CompilationInfo* info_;
57 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) {
66 if (node != NULL) Visit(node);
67 }
68
69 void AnalyzeAssignment(Variable* var);
70
71 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
72 DISALLOW_COPY_AND_ASSIGN(AstLoopAssignmentAnalyzer);
73};
74}
75}
76} // namespace v8::internal::compiler
77
78#endif // V8_COMPILER_AST_LOOP_ASSIGNMENT_ANALYZER_H_