OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_COMPILER_BYTECODE_BRANCH_ANALYSIS_H_ | 5 #ifndef V8_COMPILER_BYTECODE_BRANCH_ANALYSIS_H_ |
6 #define V8_COMPILER_BYTECODE_BRANCH_ANALYSIS_H_ | 6 #define V8_COMPILER_BYTECODE_BRANCH_ANALYSIS_H_ |
7 | 7 |
8 #include "src/bit-vector.h" | 8 #include "src/bit-vector.h" |
9 #include "src/handles.h" | 9 #include "src/handles.h" |
10 #include "src/zone-containers.h" | 10 #include "src/zone-containers.h" |
11 | 11 |
12 namespace v8 { | 12 namespace v8 { |
13 namespace internal { | 13 namespace internal { |
14 | 14 |
15 class BytecodeArray; | 15 class BytecodeArray; |
16 | 16 |
17 namespace compiler { | 17 namespace compiler { |
18 | 18 |
19 class BytecodeBranchInfo; | 19 class BytecodeBranchInfo; |
20 | 20 |
21 // A class for identifying the branch targets and their branch sites | 21 // A class for identifying the branch targets and their branch sites |
22 // within a bytecode array and also identifying which bytecodes are | 22 // within a bytecode array. This information can be used to construct |
23 // reachable. This information can be used to construct the local | 23 // the local control flow logic for high-level IR graphs built from |
24 // control flow logic for high-level IR graphs built from bytecode. | 24 // bytecode. |
25 // | 25 // |
26 // NB This class relies on the only backwards branches in bytecode | 26 // NB This class relies on the only backwards branches in bytecode |
27 // being jumps back to loop headers. | 27 // being jumps back to loop headers. |
28 class BytecodeBranchAnalysis BASE_EMBEDDED { | 28 class BytecodeBranchAnalysis BASE_EMBEDDED { |
29 public: | 29 public: |
30 BytecodeBranchAnalysis(Handle<BytecodeArray> bytecode_array, Zone* zone); | 30 BytecodeBranchAnalysis(Handle<BytecodeArray> bytecode_array, Zone* zone); |
31 | 31 |
32 // Analyze the bytecodes to find the branch sites and their | 32 // Analyze the bytecodes to find the branch sites and their |
33 // targets. No other methods in this class return valid information | 33 // targets. No other methods in this class return valid information |
34 // until this has been called. | 34 // until this has been called. |
35 void Analyze(); | 35 void Analyze(); |
36 | 36 |
37 // Returns true if the bytecode at |offset| is reachable. | |
38 bool is_reachable(int offset) const { return reachable_.Contains(offset); } | |
39 | |
40 // Returns true if there are any forward branches to the bytecode at | 37 // Returns true if there are any forward branches to the bytecode at |
41 // |offset|. | 38 // |offset|. |
42 bool forward_branches_target(int offset) const { | 39 bool forward_branches_target(int offset) const { |
43 const ZoneVector<int>* sites = ForwardBranchesTargetting(offset); | 40 const ZoneVector<int>* sites = ForwardBranchesTargetting(offset); |
44 return sites != nullptr && sites->size() > 0; | 41 return sites != nullptr && sites->size() > 0; |
45 } | 42 } |
46 | 43 |
47 // Returns true if there are any backward branches to the bytecode | 44 // Returns true if there are any backward branches to the bytecode |
48 // at |offset|. | 45 // at |offset|. |
49 bool backward_branches_target(int offset) const { | 46 bool backward_branches_target(int offset) const { |
50 const ZoneVector<int>* sites = BackwardBranchesTargetting(offset); | 47 const ZoneVector<int>* sites = BackwardBranchesTargetting(offset); |
51 return sites != nullptr && sites->size() > 0; | 48 return sites != nullptr && sites->size() > 0; |
52 } | 49 } |
53 | 50 |
54 private: | 51 private: |
55 void AddBranch(int origin_offset, int target_offset); | 52 void AddBranch(int origin_offset, int target_offset); |
56 void AnalyzeExceptionHandlers(); | |
57 | 53 |
58 // Offsets of bytecodes having a backward branch to the bytecode at |offset|. | 54 // Offsets of bytecodes having a backward branch to the bytecode at |offset|. |
59 const ZoneVector<int>* BackwardBranchesTargetting(int offset) const; | 55 const ZoneVector<int>* BackwardBranchesTargetting(int offset) const; |
60 | 56 |
61 // Offsets of bytecodes having a forward branch to the bytecode at |offset|. | 57 // Offsets of bytecodes having a forward branch to the bytecode at |offset|. |
62 const ZoneVector<int>* ForwardBranchesTargetting(int offset) const; | 58 const ZoneVector<int>* ForwardBranchesTargetting(int offset) const; |
63 | 59 |
64 Zone* zone() const { return zone_; } | 60 Zone* zone() const { return zone_; } |
65 Handle<BytecodeArray> bytecode_array() const { return bytecode_array_; } | 61 Handle<BytecodeArray> bytecode_array() const { return bytecode_array_; } |
66 | 62 |
67 ZoneMap<int, BytecodeBranchInfo*> branch_infos_; | 63 ZoneMap<int, BytecodeBranchInfo*> branch_infos_; |
68 Handle<BytecodeArray> bytecode_array_; | 64 Handle<BytecodeArray> bytecode_array_; |
69 BitVector reachable_; | |
70 Zone* zone_; | 65 Zone* zone_; |
71 | 66 |
72 DISALLOW_COPY_AND_ASSIGN(BytecodeBranchAnalysis); | 67 DISALLOW_COPY_AND_ASSIGN(BytecodeBranchAnalysis); |
73 }; | 68 }; |
74 | 69 |
75 | 70 |
76 } // namespace compiler | 71 } // namespace compiler |
77 } // namespace internal | 72 } // namespace internal |
78 } // namespace v8 | 73 } // namespace v8 |
79 | 74 |
80 #endif // V8_COMPILER_BYTECODE_BRANCH_ANALYSIS_H_ | 75 #endif // V8_COMPILER_BYTECODE_BRANCH_ANALYSIS_H_ |
OLD | NEW |