blob: 7d32da828132fb10f4d100b0d7c736aca1ad4cdd [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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 Murdoch4a90d5f2016-03-22 12:00:34 +000010
11namespace v8 {
12namespace internal {
13
14class BytecodeArray;
15
16namespace compiler {
17
Ben Murdoch097c5b22016-05-18 11:27:45 +010018// 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 Murdoch4a90d5f2016-03-22 12:00:34 +000021//
Ben Murdoch097c5b22016-05-18 11:27:45 +010022// 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 Murdoch4a90d5f2016-03-22 12:00:34 +000025class 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 Murdoch4a90d5f2016-03-22 12:00:34 +000034 // Returns true if there are any forward branches to the bytecode at
35 // |offset|.
36 bool forward_branches_target(int offset) const {
Ben Murdoch097c5b22016-05-18 11:27:45 +010037 return is_forward_target_.Contains(offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038 }
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 Murdoch097c5b22016-05-18 11:27:45 +010043 return is_backward_target_.Contains(offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044 }
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 Murdoch4a90d5f2016-03-22 12:00:34 +000052 Handle<BytecodeArray> bytecode_array_;
Ben Murdoch097c5b22016-05-18 11:27:45 +010053 BitVector is_backward_target_;
54 BitVector is_forward_target_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000055 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_