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 #include "src/compiler/bytecode-branch-analysis.h"
6 
7 #include "src/interpreter/bytecode-array-iterator.h"
8 #include "src/objects-inl.h"
9 
10 namespace v8 {
11 namespace internal {
12 namespace compiler {
13 
BytecodeBranchAnalysis(Handle<BytecodeArray> bytecode_array,Zone * zone)14 BytecodeBranchAnalysis::BytecodeBranchAnalysis(
15     Handle<BytecodeArray> bytecode_array, Zone* zone)
16     : bytecode_array_(bytecode_array),
17       is_backward_target_(bytecode_array->length(), zone),
18       is_forward_target_(bytecode_array->length(), zone),
19       zone_(zone) {}
20 
Analyze()21 void BytecodeBranchAnalysis::Analyze() {
22   interpreter::BytecodeArrayIterator iterator(bytecode_array());
23   while (!iterator.done()) {
24     interpreter::Bytecode bytecode = iterator.current_bytecode();
25     int current_offset = iterator.current_offset();
26     if (interpreter::Bytecodes::IsJump(bytecode)) {
27       AddBranch(current_offset, iterator.GetJumpTargetOffset());
28     }
29     iterator.Advance();
30   }
31 }
32 
AddBranch(int source_offset,int target_offset)33 void BytecodeBranchAnalysis::AddBranch(int source_offset, int target_offset) {
34   if (source_offset < target_offset) {
35     is_forward_target_.Add(target_offset);
36   } else {
37     is_backward_target_.Add(target_offset);
38   }
39 }
40 
41 }  // namespace compiler
42 }  // namespace internal
43 }  // namespace v8
44