Home | History | Annotate | Download | only in compiler
      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-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:
     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(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.
     43 class 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   Zone* zone_;
     58   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) {
     67     if (node != nullptr) Visit(node);
     68   }
     69 
     70   void AnalyzeAssignment(Variable* var);
     71 
     72   DEFINE_AST_VISITOR_SUBCLASS_MEMBERS();
     73   DISALLOW_COPY_AND_ASSIGN(AstLoopAssignmentAnalyzer);
     74 };
     75 }  // namespace compiler
     76 }  // namespace internal
     77 }  // namespace v8
     78 
     79 #endif  // V8_COMPILER_AST_LOOP_ASSIGNMENT_ANALYZER_H_
     80