Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_COMPILER_BYTECODE_BRANCH_ANALYSIS_H_ |
| 6 | #define V8_COMPILER_BYTECODE_BRANCH_ANALYSIS_H_ |
| 7 | |
| 8 | #include "src/bit-vector.h" |
| 9 | #include "src/handles.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 10 | |
| 11 | namespace v8 { |
| 12 | namespace internal { |
| 13 | |
| 14 | class BytecodeArray; |
| 15 | |
| 16 | namespace compiler { |
| 17 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 18 | // A class for identifying branch targets within a bytecode array. |
| 19 | // This information can be used to construct the local control flow |
| 20 | // logic for high-level IR graphs built from bytecode. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 21 | // |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 22 | // N.B. If this class is used to determine loop headers, then such a |
| 23 | // usage relies on the only backwards branches in bytecode being jumps |
| 24 | // back to loop headers. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 25 | class BytecodeBranchAnalysis BASE_EMBEDDED { |
| 26 | public: |
| 27 | BytecodeBranchAnalysis(Handle<BytecodeArray> bytecode_array, Zone* zone); |
| 28 | |
| 29 | // Analyze the bytecodes to find the branch sites and their |
| 30 | // targets. No other methods in this class return valid information |
| 31 | // until this has been called. |
| 32 | void Analyze(); |
| 33 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 34 | // Returns true if there are any forward branches to the bytecode at |
| 35 | // |offset|. |
| 36 | bool forward_branches_target(int offset) const { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 37 | return is_forward_target_.Contains(offset); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | // Returns true if there are any backward branches to the bytecode |
| 41 | // at |offset|. |
| 42 | bool backward_branches_target(int offset) const { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 43 | return is_backward_target_.Contains(offset); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | private: |
| 47 | void AddBranch(int origin_offset, int target_offset); |
| 48 | |
| 49 | Zone* zone() const { return zone_; } |
| 50 | Handle<BytecodeArray> bytecode_array() const { return bytecode_array_; } |
| 51 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 52 | Handle<BytecodeArray> bytecode_array_; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 53 | BitVector is_backward_target_; |
| 54 | BitVector is_forward_target_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 55 | Zone* zone_; |
| 56 | |
| 57 | DISALLOW_COPY_AND_ASSIGN(BytecodeBranchAnalysis); |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | } // namespace compiler |
| 62 | } // namespace internal |
| 63 | } // namespace v8 |
| 64 | |
| 65 | #endif // V8_COMPILER_BYTECODE_BRANCH_ANALYSIS_H_ |