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/interpreter/bytecode-peephole-optimizer.h" | |
| 6 | |
| 7 #include "src/interpreter/constant-array-builder.h" | |
| 8 #include "src/objects-inl.h" | |
| 9 #include "src/objects.h" | |
| 10 | |
| 11 namespace v8 { | |
| 12 namespace internal { | |
| 13 namespace interpreter { | |
| 14 | |
| 15 BytecodePeepholeOptimizer::BytecodePeepholeOptimizer( | |
| 16 ConstantArrayBuilder* constant_array_builder, BytecodeWriter* output_writer) | |
| 17 : constant_array_builder_(constant_array_builder), | |
| 18 output_writer_(output_writer), | |
| 19 last_(nullptr), | |
| 20 last_is_discardable_(true) {} | |
| 21 | |
| 22 // override | |
| 23 size_t BytecodePeepholeOptimizer::FlushForOffset() { | |
| 24 size_t buffered_size = output_writer_->FlushForOffset(); | |
| 25 if (last_ != nullptr) { | |
| 26 if (last_->bytecode() == Bytecode::kNop && | |
| 27 !last_->source_info().is_statement()) { | |
| 28 last_->Release(); | |
| 29 last_ = nullptr; | |
| 30 } else { | |
| 31 buffered_size += last_->Size(); | |
| 32 last_is_discardable_ = false; | |
| 33 } | |
| 34 } | |
| 35 return buffered_size; | |
| 36 } | |
| 37 | |
| 38 // override | |
| 39 void BytecodePeepholeOptimizer::LeaveBasicBlock() { | |
| 40 if (last_ != nullptr) { | |
| 41 output_writer_->Write(last_); | |
| 42 last_ = nullptr; | |
| 43 } | |
| 44 last_is_discardable_ = false; | |
| 45 output_writer_->LeaveBasicBlock(); | |
| 46 } | |
| 47 | |
| 48 // override | |
| 49 void BytecodePeepholeOptimizer::Write(BytecodeNode* node) { | |
| 50 // Attempt optimization if there is an earlier node to optimize with. | |
| 51 if (last_ != nullptr) { | |
| 52 node = Optimize(node); | |
| 53 } | |
| 54 | |
| 55 // Only output if optimization did not remove earlier node. | |
| 56 if (last_ != nullptr) { | |
| 57 output_writer_->Write(last_); | |
| 58 } | |
| 59 | |
| 60 last_ = node; | |
| 61 last_is_discardable_ = true; | |
| 62 } | |
| 63 | |
| 64 Handle<Object> BytecodePeepholeOptimizer::GetConstantForIndexOperand( | |
| 65 const BytecodeNode* const node, int index) const { | |
| 66 DCHECK_LE(index, node->operand_count()); | |
| 67 DCHECK_EQ(Bytecodes::GetOperandType(node->bytecode(), 0), OperandType::kIdx); | |
| 68 uint32_t index_operand = node->operands()[0]; | |
| 69 return constant_array_builder_->At(index_operand); | |
| 70 } | |
| 71 | |
| 72 BytecodeNode* BytecodePeepholeOptimizer::Optimize(BytecodeNode* current) { | |
|
rmcilroy
2016/05/06 15:25:48
This function is a bit hard for me to reason about
oth
2016/05/09 11:02:17
Done.
| |
| 73 DCHECK_NE(current, last_); | |
| 74 | |
| 75 // The current node can always be discarded here. The last node | |
| 76 // can only be discarded if last_is_discardable_ == true. | |
|
rmcilroy
2016/05/06 15:25:48
This comment isn't really true given the source po
oth
2016/05/09 11:02:17
Done.
| |
| 77 if (Bytecodes::IsJumpIfToBoolean(current->bytecode()) && | |
| 78 Bytecodes::WritesAccumulatorWithBoolean(last_->bytecode())) { | |
| 79 current->set_bytecode( | |
|
rmcilroy
2016/05/06 15:25:48
nit - could we have a replace_bytecode() in Byteco
oth
2016/05/09 11:02:17
Done.
| |
| 80 Bytecodes::GetJumpWithoutToBoolean(current->bytecode()), | |
| 81 current->operands()[0], current->operand_scale()); | |
| 82 return current; | |
| 83 } else if (Bytecodes::IsAccumulatorLoadWithoutEffects(current->bytecode()) && | |
| 84 Bytecodes::IsAccumulatorLoadWithoutEffects(last_->bytecode()) && | |
| 85 last_is_discardable_) { | |
| 86 last_->Release(); | |
| 87 last_ = nullptr; | |
| 88 } else if ((last_->bytecode() == Bytecode::kLdar || | |
| 89 last_->bytecode() == Bytecode::kStar) && | |
| 90 (current->bytecode() == Bytecode::kLdar || | |
| 91 current->bytecode() == Bytecode::kStar) && | |
| 92 current->operands()[0] == last_->operands()[0]) { | |
| 93 // The current operation is moot as the last has performed desired effect. | |
| 94 // If current bytecode has a source position, change it to a nop, else it | |
| 95 // can be discarded. The next call to Optimize will try to remove the nop. | |
| 96 if (current->source_info().is_valid()) { | |
| 97 current->set_bytecode(Bytecode::kNop); | |
| 98 } else { | |
| 99 current->Release(); | |
| 100 current = nullptr; | |
| 101 } | |
| 102 } else if (last_->bytecode() == Bytecode::kNop && last_is_discardable_) { | |
| 103 if (last_->source_info().is_valid()) { | |
| 104 current->source_info().Update(last_->source_info()); | |
| 105 } | |
| 106 last_->Release(); | |
| 107 last_ = nullptr; | |
| 108 } else if (current->bytecode() == Bytecode::kToName && | |
| 109 !current->source_info().is_statement()) { | |
|
rmcilroy
2016/05/06 15:25:48
It's a bit weird that this is the only is_statemen
oth
2016/05/09 11:02:17
Cleaned-up in later CL. This is more oversight of
| |
| 110 if (last_->bytecode() == Bytecode::kToName || | |
| 111 last_->bytecode() == Bytecode::kTypeOf || | |
| 112 (last_->bytecode() == Bytecode::kLdaConstant && | |
| 113 GetConstantForIndexOperand(last_, 0)->IsName())) { | |
| 114 current->Release(); | |
| 115 current = nullptr; | |
| 116 } | |
| 117 } | |
| 118 return current; | |
| 119 } | |
| 120 | |
| 121 } // namespace interpreter | |
| 122 } // namespace internal | |
| 123 } // namespace v8 | |
| OLD | NEW |