Index: src/interpreter/bytecode-peephole-optimizer.cc |
diff --git a/src/interpreter/bytecode-peephole-optimizer.cc b/src/interpreter/bytecode-peephole-optimizer.cc |
index 803fc23089927faf47fe893deb178dd479265d6f..7c30da9e7e03d95a89a5c22c4045f0f7ef3c32fd 100644 |
--- a/src/interpreter/bytecode-peephole-optimizer.cc |
+++ b/src/interpreter/bytecode-peephole-optimizer.cc |
@@ -94,25 +94,6 @@ bool BytecodePeepholeOptimizer::LastBytecodePutsNameInAccumulator() const { |
GetConstantForIndexOperand(&last_, 0)->IsName())); |
} |
-void BytecodePeepholeOptimizer::UpdateCurrentBytecode(BytecodeNode* current) { |
- if (Bytecodes::IsJumpIfToBoolean(current->bytecode()) && |
- Bytecodes::WritesBooleanToAccumulator(last_.bytecode())) { |
- // Conditional jumps with boolean conditions are emitted in |
- // ToBoolean form by the bytecode array builder, |
- // i.e. JumpIfToBooleanTrue rather JumpIfTrue. The ToBoolean element |
- // can be removed if the previous bytecode put a boolean value in |
- // the accumulator. |
- Bytecode jump = Bytecodes::GetJumpWithoutToBoolean(current->bytecode()); |
- current->set_bytecode(jump, current->operand(0), current->operand_scale()); |
- } else if (current->bytecode() == Bytecode::kToBooleanLogicalNot && |
- Bytecodes::WritesBooleanToAccumulator(last_.bytecode())) { |
- // Logical-nots are emitted in ToBoolean form by the bytecode array |
- // builder, The ToBoolean element can be removed if the previous bytecode |
- // put a boolean value in the accumulator. |
- current->set_bytecode(Bytecode::kLogicalNot); |
- } |
-} |
- |
bool BytecodePeepholeOptimizer::CanElideCurrent( |
const BytecodeNode* const current) const { |
if (Bytecodes::IsLdarOrStar(last_.bytecode()) && |
@@ -134,6 +115,77 @@ bool BytecodePeepholeOptimizer::CanElideCurrent( |
} |
} |
+void BytecodePeepholeOptimizer::ChangeLoadStarToLdrLdar( |
+ Bytecode new_bytecode, BytecodeNode* const current) { |
+ last_.Transform(new_bytecode, current->operand(0), current->operand_scale()); |
+ current->set_bytecode(Bytecode::kLdar, current->operand(0), |
+ current->operand_scale()); |
+ if (current->source_info().is_valid()) { |
+ last_.source_info().Update(current->source_info()); |
+ current->source_info().set_invalid(); |
+ } |
+} |
+ |
+bool BytecodePeepholeOptimizer::ChangeLoadToLdr(BytecodeNode* const current) { |
+ if (current->bytecode() == Bytecode::kStar) { |
+ switch (last_.bytecode()) { |
+ case Bytecode::kLoadIC: |
+ ChangeLoadStarToLdrLdar(Bytecode::kLdrNamedProperty, current); |
+ return true; |
+ case Bytecode::kKeyedLoadIC: |
+ ChangeLoadStarToLdrLdar(Bytecode::kLdrKeyedProperty, current); |
+ return true; |
+ case Bytecode::kLdaGlobal: |
+ ChangeLoadStarToLdrLdar(Bytecode::kLdrGlobal, current); |
+ return true; |
+ case Bytecode::kLdaContextSlot: |
+ ChangeLoadStarToLdrLdar(Bytecode::kLdrContextSlot, current); |
+ return true; |
+ case Bytecode::kLdaUndefined: |
+ ChangeLoadStarToLdrLdar(Bytecode::kLdrUndefined, current); |
+ return true; |
+ default: |
+ break; |
+ } |
+ } |
+ return false; |
+} |
+ |
+bool BytecodePeepholeOptimizer::RemoveToBooleanFromJump( |
+ BytecodeNode* const current) { |
+ bool can_remove = Bytecodes::IsJumpIfToBoolean(current->bytecode()) && |
+ Bytecodes::WritesBooleanToAccumulator(last_.bytecode()); |
+ if (can_remove) { |
+ // Conditional jumps with boolean conditions are emiitted in |
+ // ToBoolean form by the bytecode array builder, |
+ // i.e. JumpIfToBooleanTrue rather JumpIfTrue. The ToBoolean |
+ // element can be removed if the previous bytecode put a boolean |
+ // value in the accumulator. |
+ Bytecode jump = Bytecodes::GetJumpWithoutToBoolean(current->bytecode()); |
+ current->set_bytecode(jump, current->operand(0), current->operand_scale()); |
+ } |
+ return can_remove; |
+} |
+ |
+bool BytecodePeepholeOptimizer::RemoveToBooleanFromLogicalNot( |
+ BytecodeNode* const current) { |
+ bool can_remove = current->bytecode() == Bytecode::kToBooleanLogicalNot && |
+ Bytecodes::WritesBooleanToAccumulator(last_.bytecode()); |
+ if (can_remove) { |
+ // Logical-nots are emitted in ToBoolean form by the bytecode array |
+ // builder, The ToBoolean element can be removed if the previous bytecode |
+ // put a boolean value in the accumulator. |
+ current->set_bytecode(Bytecode::kLogicalNot); |
+ } |
+ return can_remove; |
+} |
+ |
+bool BytecodePeepholeOptimizer::SimpleSubstitution( |
+ BytecodeNode* const current) { |
+ return RemoveToBooleanFromJump(current) || ChangeLoadToLdr(current) || |
+ RemoveToBooleanFromLogicalNot(current); |
rmcilroy
2016/05/19 10:01:02
nit - RemoveTo calls together.
oth
2016/05/19 10:35:22
Done.
|
+} |
+ |
bool BytecodePeepholeOptimizer::CanElideLast( |
const BytecodeNode* const current) const { |
if (!last_is_discardable_) { |
@@ -150,25 +202,35 @@ bool BytecodePeepholeOptimizer::CanElideLast( |
// consecutive accumulator loads (that don't have side effects) then only |
// the final load is potentially visible. |
return true; |
+ } else if (Bytecodes::GetAccumulatorUse(current->bytecode()) == |
+ AccumulatorUse::kWrite && |
+ Bytecodes::IsAccumulatorLoadWithoutEffects(last_.bytecode())) { |
+ // The current instruction clobbers the accumulator without reading it. The |
+ // load in the last instruction can be elided as it has no effect. |
+ return true; |
} else { |
return false; |
} |
} |
BytecodeNode* BytecodePeepholeOptimizer::Optimize(BytecodeNode* current) { |
- UpdateCurrentBytecode(current); |
- |
- if (CanElideCurrent(current)) { |
- if (current->source_info().is_valid()) { |
- current->set_bytecode(Bytecode::kNop); |
- } else { |
- current = nullptr; |
+ if (!SimpleSubstitution(current)) { |
+ if (CanElideCurrent(current)) { |
+ if (current->source_info().is_valid()) { |
rmcilroy
2016/05/19 10:01:02
As discussed offline, I think this should be if (c
oth
2016/05/19 10:35:22
The is_statement() was a daft mistake. Gone.
|
+ if (current->source_info().is_statement()) { |
+ current->set_bytecode(Bytecode::kNop); |
+ } |
+ } else { |
+ return nullptr; |
+ } |
} |
- } else if (CanElideLast(current)) { |
- if (last_.source_info().is_valid()) { |
- current->source_info().Update(last_.source_info()); |
+ |
+ if (CanElideLast(current)) { |
+ if (last_.source_info().is_valid()) { |
+ current->source_info().Update(last_.source_info()); |
+ } |
+ InvalidateLast(); |
} |
- InvalidateLast(); |
} |
return current; |
} |