Chromium Code Reviews| 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 const ZoneVector<int>* back_edge_offsets() { return &back_edge_offsets_; } | |
| 23 const ZoneVector<int>* fore_edge_offsets() { return &fore_edge_offsets_; } | |
| 24 | |
| 25 private: | |
| 26 // The offsets of bytecodes that refer to this bytecode as | |
| 27 // a back-edge predecessor. | |
|
rmcilroy
2015/12/15 17:35:43
nit - these comments would be better on the public
oth
2015/12/15 22:42:17
Done.
| |
| 28 ZoneVector<int> back_edge_offsets_; | |
| 29 | |
| 30 // The offsets of bytecodes that refer to this bytecode as | |
| 31 // a forwards-edge predecessor. | |
| 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 } | |
|
rmcilroy
2015/12/15 17:35:43
nit - pull the repeated code out into a shared hel
oth
2015/12/15 22:42:17
Nit - ignoring this one, the delta is back_edge_of
| |
| 98 } | |
| 99 | |
| 100 | |
| 101 bool BytecodeBranchAnalysis::IsLastBackwardBranchTo(int branch_target, | |
| 102 int offset) const { | |
| 103 return BackwardBranchesTargetting(branch_target)->back() == offset; | |
| 104 } | |
| 105 | |
| 106 | |
| 107 void BytecodeBranchAnalysis::AddBranch(int source_offset, int target_offset) { | |
| 108 BytecodeBranchInfo* branch_info = nullptr; | |
| 109 auto iterator = branch_infos_.find(target_offset); | |
| 110 if (branch_infos_.end() == iterator) { | |
| 111 branch_info = new (zone()) BytecodeBranchInfo(zone()); | |
| 112 branch_infos_.insert(std::make_pair(target_offset, branch_info)); | |
| 113 } else { | |
| 114 branch_info = iterator->second; | |
| 115 } | |
| 116 branch_info->AddBranch(source_offset, target_offset); | |
| 117 } | |
| 118 | |
| 119 | |
| 120 } // namespace compiler | |
| 121 } // namespace internal | |
| 122 } // namespace v8 | |
| OLD | NEW |