1 // Copyright 2016 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_BYTECODE_LOOP_ANALYSIS_H_
6 #define V8_COMPILER_BYTECODE_LOOP_ANALYSIS_H_
7 
8 #include "src/handles.h"
9 #include "src/zone/zone-containers.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 class BytecodeArray;
15 
16 namespace compiler {
17 
18 class BytecodeBranchAnalysis;
19 
20 class BytecodeLoopAnalysis BASE_EMBEDDED {
21  public:
22   BytecodeLoopAnalysis(Handle<BytecodeArray> bytecode_array,
23                        const BytecodeBranchAnalysis* branch_analysis,
24                        Zone* zone);
25 
26   // Analyze the bytecodes to find the branch sites and their
27   // targets. No other methods in this class return valid information
28   // until this has been called.
29   void Analyze();
30 
31   // Get the loop header offset of the containing loop for arbitrary
32   // {offset}, or -1 if the {offset} is not inside any loop.
33   int GetLoopOffsetFor(int offset) const;
34   // Gets the loop header offset of the parent loop of the loop header
35   // at {header_offset}, or -1 for outer-most loops.
36   int GetParentLoopFor(int header_offset) const;
37 
38  private:
39   void AddLoopEntry(int entry_offset);
40   void AddBranch(int origin_offset, int target_offset);
41 
zone()42   Zone* zone() const { return zone_; }
bytecode_array()43   Handle<BytecodeArray> bytecode_array() const { return bytecode_array_; }
44 
45   Handle<BytecodeArray> bytecode_array_;
46   const BytecodeBranchAnalysis* branch_analysis_;
47   Zone* zone_;
48 
49   int current_loop_offset_;
50   bool found_current_backedge_;
51 
52   // Map from the offset of a backedge jump to the offset of the corresponding
53   // loop header. There might be multiple backedges for do-while loops.
54   ZoneMap<int, int> backedge_to_header_;
55   // Map from the offset of a loop header to the offset of its parent's loop
56   // header. This map will have as many entries as there are loops in the
57   // function.
58   ZoneMap<int, int> loop_header_to_parent_;
59 
60   DISALLOW_COPY_AND_ASSIGN(BytecodeLoopAnalysis);
61 };
62 
63 }  // namespace compiler
64 }  // namespace internal
65 }  // namespace v8
66 
67 #endif  // V8_COMPILER_BYTECODE_LOOP_ANALYSIS_H_
68