| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 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-branch-analysis.h" | 5 #include "src/compiler/bytecode-branch-analysis.h" |
| 6 | 6 |
| 7 #include "src/interpreter/bytecode-array-iterator.h" | 7 #include "src/interpreter/bytecode-array-iterator.h" |
| 8 #include "src/objects-inl.h" | 8 #include "src/objects-inl.h" |
| 9 | 9 |
| 10 namespace v8 { | 10 namespace v8 { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 } else { | 42 } else { |
| 43 back_edge_offsets_.push_back(source_offset); | 43 back_edge_offsets_.push_back(source_offset); |
| 44 } | 44 } |
| 45 } | 45 } |
| 46 | 46 |
| 47 | 47 |
| 48 BytecodeBranchAnalysis::BytecodeBranchAnalysis( | 48 BytecodeBranchAnalysis::BytecodeBranchAnalysis( |
| 49 Handle<BytecodeArray> bytecode_array, Zone* zone) | 49 Handle<BytecodeArray> bytecode_array, Zone* zone) |
| 50 : branch_infos_(zone), | 50 : branch_infos_(zone), |
| 51 bytecode_array_(bytecode_array), | 51 bytecode_array_(bytecode_array), |
| 52 reachable_(bytecode_array->length(), zone), | |
| 53 zone_(zone) {} | 52 zone_(zone) {} |
| 54 | 53 |
| 55 | 54 |
| 56 void BytecodeBranchAnalysis::Analyze() { | 55 void BytecodeBranchAnalysis::Analyze() { |
| 57 interpreter::BytecodeArrayIterator iterator(bytecode_array()); | 56 interpreter::BytecodeArrayIterator iterator(bytecode_array()); |
| 58 AnalyzeExceptionHandlers(); | |
| 59 bool reachable = true; | |
| 60 while (!iterator.done()) { | 57 while (!iterator.done()) { |
| 61 interpreter::Bytecode bytecode = iterator.current_bytecode(); | 58 interpreter::Bytecode bytecode = iterator.current_bytecode(); |
| 62 int current_offset = iterator.current_offset(); | 59 int current_offset = iterator.current_offset(); |
| 63 // All bytecode basic blocks are generated to be forward reachable | 60 if (interpreter::Bytecodes::IsJump(bytecode)) { |
| 64 // and may also be backward reachable. Hence if there's a forward | 61 AddBranch(current_offset, iterator.GetJumpTargetOffset()); |
| 65 // branch targetting here the code becomes reachable. | |
| 66 reachable = reachable || forward_branches_target(current_offset); | |
| 67 // Some bytecode basic blocks are reachable through a side-entry | |
| 68 // (e.g. exception handler), which has been represented in the | |
| 69 // bit-vector by a corresponding pre-pass. | |
| 70 reachable = reachable || reachable_.Contains(current_offset); | |
| 71 if (reachable) { | |
| 72 reachable_.Add(current_offset); | |
| 73 if (interpreter::Bytecodes::IsConditionalJump(bytecode)) { | |
| 74 // Only the branch is recorded, the forward path falls through | |
| 75 // and is handled as normal bytecode data flow. | |
| 76 AddBranch(current_offset, iterator.GetJumpTargetOffset()); | |
| 77 } else if (interpreter::Bytecodes::IsJump(bytecode)) { | |
| 78 // Unless the branch targets the next bytecode it's not | |
| 79 // reachable. If it targets the next bytecode the check at the | |
| 80 // start of the loop will set the reachable flag. | |
| 81 AddBranch(current_offset, iterator.GetJumpTargetOffset()); | |
| 82 reachable = false; | |
| 83 } else if (interpreter::Bytecodes::IsJumpOrReturn(bytecode)) { | |
| 84 DCHECK_EQ(bytecode, interpreter::Bytecode::kReturn); | |
| 85 reachable = false; | |
| 86 } | |
| 87 } | 62 } |
| 88 iterator.Advance(); | 63 iterator.Advance(); |
| 89 } | 64 } |
| 90 } | 65 } |
| 91 | 66 |
| 92 | 67 |
| 93 const ZoneVector<int>* BytecodeBranchAnalysis::BackwardBranchesTargetting( | 68 const ZoneVector<int>* BytecodeBranchAnalysis::BackwardBranchesTargetting( |
| 94 int offset) const { | 69 int offset) const { |
| 95 auto iterator = branch_infos_.find(offset); | 70 auto iterator = branch_infos_.find(offset); |
| 96 if (branch_infos_.end() != iterator) { | 71 if (branch_infos_.end() != iterator) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 116 auto iterator = branch_infos_.find(target_offset); | 91 auto iterator = branch_infos_.find(target_offset); |
| 117 if (branch_infos_.end() == iterator) { | 92 if (branch_infos_.end() == iterator) { |
| 118 branch_info = new (zone()) BytecodeBranchInfo(zone()); | 93 branch_info = new (zone()) BytecodeBranchInfo(zone()); |
| 119 branch_infos_.insert(std::make_pair(target_offset, branch_info)); | 94 branch_infos_.insert(std::make_pair(target_offset, branch_info)); |
| 120 } else { | 95 } else { |
| 121 branch_info = iterator->second; | 96 branch_info = iterator->second; |
| 122 } | 97 } |
| 123 branch_info->AddBranch(source_offset, target_offset); | 98 branch_info->AddBranch(source_offset, target_offset); |
| 124 } | 99 } |
| 125 | 100 |
| 126 void BytecodeBranchAnalysis::AnalyzeExceptionHandlers() { | |
| 127 HandlerTable* table = HandlerTable::cast(bytecode_array()->handler_table()); | |
| 128 for (int i = 0; i < table->NumberOfRangeEntries(); ++i) { | |
| 129 reachable_.Add(table->GetRangeHandler(i)); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 } // namespace compiler | 101 } // namespace compiler |
| 134 } // namespace internal | 102 } // namespace internal |
| 135 } // namespace v8 | 103 } // namespace v8 |
| OLD | NEW |