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 #ifndef V8_COMPILER_AST_LOOP_ASSIGNMENT_ANALYZER_H_ 6 #define V8_COMPILER_AST_LOOP_ASSIGNMENT_ANALYZER_H_ 7 8 #include "src/ast/ast.h" 9 #include "src/bit-vector.h" 10 #include "src/zone/zone-containers.h" 11 12 namespace v8 { 13 namespace internal { 14 15 class CompilationInfo; 16 class Scope; 17 class Variable; 18 19 namespace compiler { 20 21 // The result of analyzing loop assignments. 22 class LoopAssignmentAnalysis : public ZoneObject { 23 public: GetVariablesAssignedInLoop(IterationStatement * loop)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 nullptr; 31 } 32 33 int GetAssignmentCountForTesting(DeclarationScope* scope, Variable* var); 34 35 private: 36 friend class AstLoopAssignmentAnalyzer; LoopAssignmentAnalysis(Zone * zone)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. 43 class AstLoopAssignmentAnalyzer final 44 : public AstVisitor<AstLoopAssignmentAnalyzer> { 45 public: 46 AstLoopAssignmentAnalyzer(Zone* zone, CompilationInfo* info); 47 48 LoopAssignmentAnalysis* Analyze(); 49 50 #define DECLARE_VISIT(type) void Visit##type(type* node); 51 AST_NODE_LIST(DECLARE_VISIT) 52 #undef DECLARE_VISIT 53 54 static int GetVariableIndex(DeclarationScope* scope, Variable* var); 55 56 private: 57 CompilationInfo* info_; 58 Zone* zone_; 59 ZoneDeque<BitVector*> loop_stack_; 60 LoopAssignmentAnalysis* result_; 61 info()62 CompilationInfo* info() { return info_; } 63 64 void Enter(IterationStatement* loop); 65 void Exit(IterationStatement* loop); 66 VisitIfNotNull(AstNode * node)67 void VisitIfNotNull(AstNode* node) { 68 if (node != nullptr) Visit(node); 69 } 70 71 void AnalyzeAssignment(Variable* var); 72 73 DEFINE_AST_VISITOR_SUBCLASS_MEMBERS(); 74 DISALLOW_COPY_AND_ASSIGN(AstLoopAssignmentAnalyzer); 75 }; 76 } // namespace compiler 77 } // namespace internal 78 } // namespace v8 79 80 #endif // V8_COMPILER_AST_LOOP_ASSIGNMENT_ANALYZER_H_ 81