| 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" | |
| 11 | 10 |
| 12 namespace v8 { | 11 namespace v8 { |
| 13 namespace internal { | 12 namespace internal { |
| 14 | 13 |
| 15 class BytecodeArray; | 14 class BytecodeArray; |
| 16 | 15 |
| 17 namespace compiler { | 16 namespace compiler { |
| 18 | 17 |
| 19 class BytecodeBranchInfo; | 18 // A class for identifying branch targets within a bytecode array. |
| 20 | 19 // This information can be used to construct the local control flow |
| 21 // A class for identifying the branch targets and their branch sites | 20 // logic for high-level IR graphs built from bytecode. |
| 22 // within a bytecode array. This information can be used to construct | |
| 23 // the local control flow logic for high-level IR graphs built from | |
| 24 // bytecode. | |
| 25 // | 21 // |
| 26 // NB This class relies on the only backwards branches in bytecode | 22 // N.B. If this class is used to determine loop headers, then such a |
| 27 // being jumps back to loop headers. | 23 // usage relies on the only backwards branches in bytecode being jumps |
| 24 // back to loop headers. |
| 28 class BytecodeBranchAnalysis BASE_EMBEDDED { | 25 class BytecodeBranchAnalysis BASE_EMBEDDED { |
| 29 public: | 26 public: |
| 30 BytecodeBranchAnalysis(Handle<BytecodeArray> bytecode_array, Zone* zone); | 27 BytecodeBranchAnalysis(Handle<BytecodeArray> bytecode_array, Zone* zone); |
| 31 | 28 |
| 32 // Analyze the bytecodes to find the branch sites and their | 29 // Analyze the bytecodes to find the branch sites and their |
| 33 // targets. No other methods in this class return valid information | 30 // targets. No other methods in this class return valid information |
| 34 // until this has been called. | 31 // until this has been called. |
| 35 void Analyze(); | 32 void Analyze(); |
| 36 | 33 |
| 37 // Returns true if there are any forward branches to the bytecode at | 34 // Returns true if there are any forward branches to the bytecode at |
| 38 // |offset|. | 35 // |offset|. |
| 39 bool forward_branches_target(int offset) const { | 36 bool forward_branches_target(int offset) const { |
| 40 const ZoneVector<int>* sites = ForwardBranchesTargetting(offset); | 37 return is_forward_target_.Contains(offset); |
| 41 return sites != nullptr && sites->size() > 0; | |
| 42 } | 38 } |
| 43 | 39 |
| 44 // Returns true if there are any backward branches to the bytecode | 40 // Returns true if there are any backward branches to the bytecode |
| 45 // at |offset|. | 41 // at |offset|. |
| 46 bool backward_branches_target(int offset) const { | 42 bool backward_branches_target(int offset) const { |
| 47 const ZoneVector<int>* sites = BackwardBranchesTargetting(offset); | 43 return is_backward_target_.Contains(offset); |
| 48 return sites != nullptr && sites->size() > 0; | |
| 49 } | 44 } |
| 50 | 45 |
| 51 private: | 46 private: |
| 52 void AddBranch(int origin_offset, int target_offset); | 47 void AddBranch(int origin_offset, int target_offset); |
| 53 | 48 |
| 54 // Offsets of bytecodes having a backward branch to the bytecode at |offset|. | |
| 55 const ZoneVector<int>* BackwardBranchesTargetting(int offset) const; | |
| 56 | |
| 57 // Offsets of bytecodes having a forward branch to the bytecode at |offset|. | |
| 58 const ZoneVector<int>* ForwardBranchesTargetting(int offset) const; | |
| 59 | |
| 60 Zone* zone() const { return zone_; } | 49 Zone* zone() const { return zone_; } |
| 61 Handle<BytecodeArray> bytecode_array() const { return bytecode_array_; } | 50 Handle<BytecodeArray> bytecode_array() const { return bytecode_array_; } |
| 62 | 51 |
| 63 ZoneMap<int, BytecodeBranchInfo*> branch_infos_; | |
| 64 Handle<BytecodeArray> bytecode_array_; | 52 Handle<BytecodeArray> bytecode_array_; |
| 53 BitVector is_backward_target_; |
| 54 BitVector is_forward_target_; |
| 65 Zone* zone_; | 55 Zone* zone_; |
| 66 | 56 |
| 67 DISALLOW_COPY_AND_ASSIGN(BytecodeBranchAnalysis); | 57 DISALLOW_COPY_AND_ASSIGN(BytecodeBranchAnalysis); |
| 68 }; | 58 }; |
| 69 | 59 |
| 70 | 60 |
| 71 } // namespace compiler | 61 } // namespace compiler |
| 72 } // namespace internal | 62 } // namespace internal |
| 73 } // namespace v8 | 63 } // namespace v8 |
| 74 | 64 |
| 75 #endif // V8_COMPILER_BYTECODE_BRANCH_ANALYSIS_H_ | 65 #endif // V8_COMPILER_BYTECODE_BRANCH_ANALYSIS_H_ |
| OLD | NEW |