OLD | NEW |
(Empty) | |
| 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 |
| 9 namespace v8 { |
| 10 namespace internal { |
| 11 namespace compiler { |
| 12 |
| 13 // The class contains all of the sites that contain |
| 14 // branches to a particular target (bytecode offset). |
| 15 class BytecodeBranchInfo final : public ZoneObject { |
| 16 public: |
| 17 explicit BytecodeBranchInfo(Zone* zone) |
| 18 : back_edge_offsets_(zone), fore_edge_offsets_(zone) {} |
| 19 |
| 20 void AddBranch(int source_offset, int target_offset); |
| 21 |
| 22 // The offsets of bytecodes that refer to this bytecode as |
| 23 // a back-edge predecessor. |
| 24 const ZoneVector<int>* back_edge_offsets() { return &back_edge_offsets_; } |
| 25 |
| 26 // The offsets of bytecodes that refer to this bytecode as |
| 27 // a forwards-edge predecessor. |
| 28 const ZoneVector<int>* fore_edge_offsets() { return &fore_edge_offsets_; } |
| 29 |
| 30 private: |
| 31 ZoneVector<int> back_edge_offsets_; |
| 32 ZoneVector<int> fore_edge_offsets_; |
| 33 |
| 34 DISALLOW_COPY_AND_ASSIGN(BytecodeBranchInfo); |
| 35 }; |
| 36 |
| 37 |
| 38 void BytecodeBranchInfo::AddBranch(int source_offset, int target_offset) { |
| 39 if (source_offset < target_offset) { |
| 40 fore_edge_offsets_.push_back(source_offset); |
| 41 } else { |
| 42 back_edge_offsets_.push_back(source_offset); |
| 43 } |
| 44 } |
| 45 |
| 46 |
| 47 void BytecodeBranchAnalysis::Analyze() { |
| 48 interpreter::BytecodeArrayIterator iterator(bytecode_array()); |
| 49 bool reachable = true; |
| 50 while (!iterator.done()) { |
| 51 interpreter::Bytecode bytecode = iterator.current_bytecode(); |
| 52 int current_offset = iterator.current_offset(); |
| 53 // All bytecode basic blocks are generated to be forward reachable |
| 54 // and may also be backward reachable. Hence if there's a forward |
| 55 // branch targetting here the code becomes reachable. |
| 56 reachable = reachable || forward_branches_target(current_offset); |
| 57 if (reachable) { |
| 58 reachable_.Add(current_offset); |
| 59 if (interpreter::Bytecodes::IsConditionalJump(bytecode)) { |
| 60 // Only the branch is recorded, the forward path falls through |
| 61 // and is handled as normal bytecode data flow. |
| 62 AddBranch(current_offset, iterator.GetJumpTargetOffset()); |
| 63 } else if (interpreter::Bytecodes::IsJump(bytecode)) { |
| 64 // Unless the branch targets the next bytecode it's not |
| 65 // reachable. If it targets the next bytecode the check at the |
| 66 // start of the loop will set the reachable flag. |
| 67 AddBranch(current_offset, iterator.GetJumpTargetOffset()); |
| 68 reachable = false; |
| 69 } else if (interpreter::Bytecodes::IsLocalControlFlow(bytecode)) { |
| 70 DCHECK_EQ(bytecode, interpreter::Bytecode::kReturn); |
| 71 reachable = false; |
| 72 } |
| 73 } |
| 74 iterator.Advance(); |
| 75 } |
| 76 } |
| 77 |
| 78 |
| 79 const ZoneVector<int>* BytecodeBranchAnalysis::BackwardBranchesTargetting( |
| 80 int offset) const { |
| 81 auto iterator = branch_infos_.find(offset); |
| 82 if (branch_infos_.end() != iterator) { |
| 83 return iterator->second->back_edge_offsets(); |
| 84 } else { |
| 85 return nullptr; |
| 86 } |
| 87 } |
| 88 |
| 89 |
| 90 const ZoneVector<int>* BytecodeBranchAnalysis::ForwardBranchesTargetting( |
| 91 int offset) const { |
| 92 auto iterator = branch_infos_.find(offset); |
| 93 if (branch_infos_.end() != iterator) { |
| 94 return iterator->second->fore_edge_offsets(); |
| 95 } else { |
| 96 return nullptr; |
| 97 } |
| 98 } |
| 99 |
| 100 |
| 101 void BytecodeBranchAnalysis::AddBranch(int source_offset, int target_offset) { |
| 102 BytecodeBranchInfo* branch_info = nullptr; |
| 103 auto iterator = branch_infos_.find(target_offset); |
| 104 if (branch_infos_.end() == iterator) { |
| 105 branch_info = new (zone()) BytecodeBranchInfo(zone()); |
| 106 branch_infos_.insert(std::make_pair(target_offset, branch_info)); |
| 107 } else { |
| 108 branch_info = iterator->second; |
| 109 } |
| 110 branch_info->AddBranch(source_offset, target_offset); |
| 111 } |
| 112 |
| 113 |
| 114 } // namespace compiler |
| 115 } // namespace internal |
| 116 } // namespace v8 |
OLD | NEW |