OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/compiler/bytecode-loop-analysis.h" |
| 6 |
| 7 #include "src/compiler/bytecode-branch-analysis.h" |
| 8 #include "src/interpreter/bytecode-array-iterator.h" |
| 9 #include "src/objects-inl.h" |
| 10 |
| 11 namespace v8 { |
| 12 namespace internal { |
| 13 namespace compiler { |
| 14 |
| 15 BytecodeLoopAnalysis::BytecodeLoopAnalysis( |
| 16 Handle<BytecodeArray> bytecode_array, |
| 17 const BytecodeBranchAnalysis* branch_analysis, Zone* zone) |
| 18 : bytecode_array_(bytecode_array), |
| 19 branch_analysis_(branch_analysis), |
| 20 zone_(zone), |
| 21 backedge_to_header_(zone), |
| 22 loop_header_to_parent_(zone) {} |
| 23 |
| 24 void BytecodeLoopAnalysis::Analyze() { |
| 25 current_loop_offset_ = -1; |
| 26 interpreter::BytecodeArrayIterator iterator(bytecode_array()); |
| 27 while (!iterator.done()) { |
| 28 interpreter::Bytecode bytecode = iterator.current_bytecode(); |
| 29 int current_offset = iterator.current_offset(); |
| 30 if (branch_analysis_->backward_branches_target(current_offset)) { |
| 31 AddLoopEntry(current_offset); |
| 32 } else if (interpreter::Bytecodes::IsJump(bytecode)) { |
| 33 AddBranch(current_offset, iterator.GetJumpTargetOffset()); |
| 34 } |
| 35 iterator.Advance(); |
| 36 } |
| 37 } |
| 38 |
| 39 void BytecodeLoopAnalysis::AddLoopEntry(int entry_offset) { |
| 40 loop_header_to_parent_[entry_offset] = current_loop_offset_; |
| 41 current_loop_offset_ = entry_offset; |
| 42 } |
| 43 |
| 44 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. |
| 46 if (target_offset < origin_offset) { |
| 47 backedge_to_header_[origin_offset] = target_offset; |
| 48 // Check that we are finishing the current loop. This assumes that |
| 49 // there is one backedge for each loop. |
| 50 DCHECK_EQ(target_offset, current_loop_offset_); |
| 51 current_loop_offset_ = loop_header_to_parent_[target_offset]; |
| 52 } |
| 53 } |
| 54 |
| 55 int BytecodeLoopAnalysis::GetLoopOffsetFor(int offset) const { |
| 56 auto next_backedge = backedge_to_header_.lower_bound(offset); |
| 57 // If there is no next backedge => offset is not in a loop. |
| 58 if (next_backedge == backedge_to_header_.end()) { |
| 59 return -1; |
| 60 } |
| 61 // If the header preceeds the offset, it is the backedge of the containing |
| 62 // loop. |
| 63 if (next_backedge->second <= offset) { |
| 64 return next_backedge->second; |
| 65 } |
| 66 // Otherwise there is a nested loop after this offset. We just return the |
| 67 // parent of the next nested loop. |
| 68 return loop_header_to_parent_.upper_bound(offset)->second; |
| 69 } |
| 70 |
| 71 int BytecodeLoopAnalysis::GetParentLoopFor(int header_offset) const { |
| 72 auto parent = loop_header_to_parent_.find(header_offset); |
| 73 DCHECK(parent != loop_header_to_parent_.end()); |
| 74 return parent->second; |
| 75 } |
| 76 |
| 77 } // namespace compiler |
| 78 } // namespace internal |
| 79 } // namespace v8 |
OLD | NEW |