OLD | NEW |
---|---|
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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 #include "src/compiler/bytecode-loop-analysis.h" | 5 #include "src/compiler/bytecode-loop-analysis.h" |
6 | 6 |
7 #include "src/compiler/bytecode-branch-analysis.h" | 7 #include "src/compiler/bytecode-branch-analysis.h" |
8 #include "src/interpreter/bytecode-array-iterator.h" | 8 #include "src/interpreter/bytecode-array-iterator.h" |
9 #include "src/objects-inl.h" | 9 #include "src/objects-inl.h" |
10 | 10 |
11 namespace v8 { | 11 namespace v8 { |
12 namespace internal { | 12 namespace internal { |
13 namespace compiler { | 13 namespace compiler { |
14 | 14 |
15 BytecodeLoopAnalysis::BytecodeLoopAnalysis( | 15 BytecodeLoopAnalysis::BytecodeLoopAnalysis( |
16 Handle<BytecodeArray> bytecode_array, | 16 Handle<BytecodeArray> bytecode_array, |
17 const BytecodeBranchAnalysis* branch_analysis, Zone* zone) | 17 const BytecodeBranchAnalysis* branch_analysis, Zone* zone) |
18 : bytecode_array_(bytecode_array), | 18 : bytecode_array_(bytecode_array), |
19 branch_analysis_(branch_analysis), | 19 branch_analysis_(branch_analysis), |
20 zone_(zone), | 20 zone_(zone), |
Michael Starzinger
2016/08/18 13:24:28
nit: Could we add sensible initializations of both
klaasb
2016/08/18 13:50:11
Done.
| |
21 backedge_to_header_(zone), | 21 backedge_to_header_(zone), |
22 loop_header_to_parent_(zone) {} | 22 loop_header_to_parent_(zone) {} |
23 | 23 |
24 void BytecodeLoopAnalysis::Analyze() { | 24 void BytecodeLoopAnalysis::Analyze() { |
25 current_loop_offset_ = -1; | 25 current_loop_offset_ = -1; |
26 interpreter::BytecodeArrayIterator iterator(bytecode_array()); | 26 interpreter::BytecodeArrayIterator iterator(bytecode_array()); |
27 while (!iterator.done()) { | 27 while (!iterator.done()) { |
28 interpreter::Bytecode bytecode = iterator.current_bytecode(); | 28 interpreter::Bytecode bytecode = iterator.current_bytecode(); |
29 int current_offset = iterator.current_offset(); | 29 int current_offset = iterator.current_offset(); |
30 if (branch_analysis_->backward_branches_target(current_offset)) { | 30 if (branch_analysis_->backward_branches_target(current_offset)) { |
31 AddLoopEntry(current_offset); | 31 AddLoopEntry(current_offset); |
32 } else if (interpreter::Bytecodes::IsJump(bytecode)) { | 32 } else if (interpreter::Bytecodes::IsJump(bytecode)) { |
33 AddBranch(current_offset, iterator.GetJumpTargetOffset()); | 33 AddBranch(current_offset, iterator.GetJumpTargetOffset()); |
34 } | 34 } |
35 iterator.Advance(); | 35 iterator.Advance(); |
36 } | 36 } |
37 } | 37 } |
38 | 38 |
39 void BytecodeLoopAnalysis::AddLoopEntry(int entry_offset) { | 39 void BytecodeLoopAnalysis::AddLoopEntry(int entry_offset) { |
40 if (found_current_backedge_) { | |
41 // We assume that all backedges of a loop must occur together and before | |
42 // another loop entry or an outer loop backedge. | |
Jarin
2016/08/18 12:49:50
Could you explain how you guard against the assump
klaasb
2016/08/18 13:38:01
The combination of the DCHECKs on 58, 60 and 63 do
| |
43 // Thus here, the current loop actually ended before. | |
44 current_loop_offset_ = loop_header_to_parent_[current_loop_offset_]; | |
45 } | |
40 loop_header_to_parent_[entry_offset] = current_loop_offset_; | 46 loop_header_to_parent_[entry_offset] = current_loop_offset_; |
41 current_loop_offset_ = entry_offset; | 47 current_loop_offset_ = entry_offset; |
48 found_current_backedge_ = false; | |
42 } | 49 } |
43 | 50 |
44 void BytecodeLoopAnalysis::AddBranch(int origin_offset, int target_offset) { | 51 void BytecodeLoopAnalysis::AddBranch(int origin_offset, int target_offset) { |
45 // If this is a backedge, record it and update the current loop to the parent. | 52 // If this is a backedge, record it. |
46 if (target_offset < origin_offset) { | 53 if (target_offset < origin_offset) { |
47 backedge_to_header_[origin_offset] = target_offset; | 54 backedge_to_header_[origin_offset] = target_offset; |
48 // Check that we are finishing the current loop. This assumes that | 55 // Check whether this is actually a backedge of the outer loop and we have |
49 // there is one backedge for each loop. | 56 // already finished the current loop. |
50 DCHECK_EQ(target_offset, current_loop_offset_); | 57 if (target_offset < current_loop_offset_) { |
51 current_loop_offset_ = loop_header_to_parent_[target_offset]; | 58 DCHECK(found_current_backedge_); |
59 int parent_offset = loop_header_to_parent_[current_loop_offset_]; | |
60 DCHECK_EQ(target_offset, parent_offset); | |
61 current_loop_offset_ = parent_offset; | |
62 } else { | |
63 DCHECK_EQ(target_offset, current_loop_offset_); | |
64 found_current_backedge_ = true; | |
65 } | |
52 } | 66 } |
53 } | 67 } |
54 | 68 |
55 int BytecodeLoopAnalysis::GetLoopOffsetFor(int offset) const { | 69 int BytecodeLoopAnalysis::GetLoopOffsetFor(int offset) const { |
56 auto next_backedge = backedge_to_header_.lower_bound(offset); | 70 auto next_backedge = backedge_to_header_.lower_bound(offset); |
57 // If there is no next backedge => offset is not in a loop. | 71 // If there is no next backedge => offset is not in a loop. |
58 if (next_backedge == backedge_to_header_.end()) { | 72 if (next_backedge == backedge_to_header_.end()) { |
59 return -1; | 73 return -1; |
60 } | 74 } |
61 // If the header preceeds the offset, it is the backedge of the containing | 75 // If the header preceeds the offset, it is the backedge of the containing |
62 // loop. | 76 // loop. |
63 if (next_backedge->second <= offset) { | 77 if (next_backedge->second <= offset) { |
64 return next_backedge->second; | 78 return next_backedge->second; |
65 } | 79 } |
66 // Otherwise there is a nested loop after this offset. We just return the | 80 // Otherwise there is a nested loop after this offset. We just return the |
67 // parent of the next nested loop. | 81 // parent of the next nested loop. |
68 return loop_header_to_parent_.upper_bound(offset)->second; | 82 return loop_header_to_parent_.upper_bound(offset)->second; |
69 } | 83 } |
70 | 84 |
71 int BytecodeLoopAnalysis::GetParentLoopFor(int header_offset) const { | 85 int BytecodeLoopAnalysis::GetParentLoopFor(int header_offset) const { |
72 auto parent = loop_header_to_parent_.find(header_offset); | 86 auto parent = loop_header_to_parent_.find(header_offset); |
73 DCHECK(parent != loop_header_to_parent_.end()); | 87 DCHECK(parent != loop_header_to_parent_.end()); |
74 return parent->second; | 88 return parent->second; |
75 } | 89 } |
76 | 90 |
77 } // namespace compiler | 91 } // namespace compiler |
78 } // namespace internal | 92 } // namespace internal |
79 } // namespace v8 | 93 } // namespace v8 |
OLD | NEW |