From f2872bfa9990c483fddc72757ee764cbc79d126c Mon Sep 17 00:00:00 2001 From: chriseth Date: Tue, 22 Nov 2016 15:43:51 +0100 Subject: Peephole optimizer for unreacheable code. --- libevmasm/PeepholeOptimiser.cpp | 110 ++++++++++++++++++++++++++-------------- 1 file changed, 73 insertions(+), 37 deletions(-) (limited to 'libevmasm/PeepholeOptimiser.cpp') diff --git a/libevmasm/PeepholeOptimiser.cpp b/libevmasm/PeepholeOptimiser.cpp index 901e310e..16ca9535 100644 --- a/libevmasm/PeepholeOptimiser.cpp +++ b/libevmasm/PeepholeOptimiser.cpp @@ -30,36 +30,55 @@ using namespace dev; // TODO: Extend this to use the tools from ExpressionClasses.cpp -struct Identity +struct OptimiserState { - static size_t windowSize() { return 1; } - static bool apply(AssemblyItems::const_iterator _in, std::back_insert_iterator _out) + AssemblyItems const& items; + size_t i; + std::back_insert_iterator out; +}; + +template +struct SimplePeepholeOptimizerMethod +{ + static bool apply(OptimiserState& _state) + { + if ( + _state.i + WindowSize <= _state.items.size() && + Method::applySimple(_state.items.begin() + _state.i, _state.out) + ) + { + _state.i += WindowSize; + return true; + } + else + return false; + } +}; + +struct Identity: SimplePeepholeOptimizerMethod +{ + static bool applySimple(AssemblyItems::const_iterator _in, std::back_insert_iterator _out) { *_out = *_in; return true; } }; -struct PushPop +struct PushPop: SimplePeepholeOptimizerMethod { - static size_t windowSize() { return 2; } - static bool apply(AssemblyItems::const_iterator _in, std::back_insert_iterator) + static size_t applySimple(AssemblyItems::const_iterator _in, std::back_insert_iterator) { auto t = _in[0].type(); - if (_in[1] == Instruction::POP && ( + return _in[1] == Instruction::POP && ( SemanticInformation::isDupInstruction(_in[0]) || t == Push || t == PushString || t == PushTag || t == PushSub || t == PushSubSize || t == PushProgramSize || t == PushData || t == PushLibraryAddress - )) - return true; - else - return false; + ); } }; -struct AddPop +struct AddPop: SimplePeepholeOptimizerMethod { - static size_t windowSize() { return 2; } static bool apply(AssemblyItems::const_iterator _in, std::back_insert_iterator _out) { if (_in[1] == Instruction::POP && @@ -83,22 +102,17 @@ struct AddPop } }; -struct DoubleSwap +struct DoubleSwap: SimplePeepholeOptimizerMethod { - static size_t windowSize() { return 2; } - static bool apply(AssemblyItems::const_iterator _in, std::back_insert_iterator) + static size_t applySimple(AssemblyItems::const_iterator _in, std::back_insert_iterator) { - if (_in[0] == _in[1] && SemanticInformation::isSwapInstruction(_in[0])) - return true; - else - return false; + return _in[0] == _in[1] && SemanticInformation::isSwapInstruction(_in[0]); } }; -struct JumpToNext +struct JumpToNext: SimplePeepholeOptimizerMethod { - static size_t windowSize() { return 3; } - static bool apply(AssemblyItems::const_iterator _in, std::back_insert_iterator _out) + static size_t applySimple(AssemblyItems::const_iterator _in, std::back_insert_iterator _out) { if ( _in[0].type() == PushTag && @@ -117,10 +131,41 @@ struct JumpToNext } }; -struct TagConjunctions +/// Removes everything after a JUMP (or similar) until the next JUMPDEST. +struct UnreachableCode { - static size_t windowSize() { return 3; } - static bool apply(AssemblyItems::const_iterator _in, std::back_insert_iterator _out) + static bool apply(OptimiserState& _state) + { + auto it = _state.items.begin() + _state.i; + auto end = _state.items.end(); + if (it == end) + return false; + if ( + it[0] != Instruction::JUMP && + it[0] != Instruction::RETURN && + it[0] != Instruction::STOP && + it[0] != Instruction::SUICIDE + ) + return false; + + size_t i = 1; + while (it + i != end && it[i].type() != Tag) + i++; + if (i > 1) + { + *_state.out = it[0]; + _state.i += i; + return true; + } + else + return false; + } +}; + + +struct TagConjunctions: SimplePeepholeOptimizerMethod +{ + static bool applySimple(AssemblyItems::const_iterator _in, std::back_insert_iterator _out) { if ( _in[0].type() == PushTag && @@ -137,13 +182,6 @@ struct TagConjunctions } }; -struct OptimiserState -{ - AssemblyItems const& items; - size_t i; - std::back_insert_iterator out; -}; - void applyMethods(OptimiserState&) { assertThrow(false, OptimizerException, "Peephole optimizer failed to apply identity."); @@ -152,9 +190,7 @@ void applyMethods(OptimiserState&) template void applyMethods(OptimiserState& _state, Method, OtherMethods... _other) { - if (_state.i + Method::windowSize() <= _state.items.size() && Method::apply(_state.items.begin() + _state.i, _state.out)) - _state.i += Method::windowSize(); - else + if (!Method::apply(_state)) applyMethods(_state, _other...); } @@ -162,7 +198,7 @@ bool PeepholeOptimiser::optimise() { OptimiserState state {m_items, 0, std::back_inserter(m_optimisedItems)}; while (state.i < m_items.size()) - applyMethods(state, PushPop(), AddPop(), DoubleSwap(), JumpToNext(), TagConjunctions(), Identity()); + applyMethods(state, PushPop(), AddPop(), DoubleSwap(), JumpToNext(), UnreachableCode(), TagConjunctions(), Identity()); if (m_optimisedItems.size() < m_items.size()) { m_items = std::move(m_optimisedItems); -- cgit v1.2.3 From 612c1726d9b0dd1cb5bccd9b93ace69f6516f9ea Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 23 Nov 2016 14:50:20 +0100 Subject: Templatize. --- libevmasm/PeepholeOptimiser.cpp | 112 +++++++++++++++++++++++++++------------- 1 file changed, 75 insertions(+), 37 deletions(-) (limited to 'libevmasm/PeepholeOptimiser.cpp') diff --git a/libevmasm/PeepholeOptimiser.cpp b/libevmasm/PeepholeOptimiser.cpp index 16ca9535..c8a28afc 100644 --- a/libevmasm/PeepholeOptimiser.cpp +++ b/libevmasm/PeepholeOptimiser.cpp @@ -37,6 +37,35 @@ struct OptimiserState std::back_insert_iterator out; }; +template +struct ApplyRule +{ +}; +template +struct ApplyRule +{ + static bool applyRule(AssemblyItems::const_iterator _in, std::back_insert_iterator _out) + { + return Method::applySimple(_in[0], _in[1], _in[2], _out); + } +}; +template +struct ApplyRule +{ + static bool applyRule(AssemblyItems::const_iterator _in, std::back_insert_iterator _out) + { + return Method::applySimple(_in[0], _in[1], _out); + } +}; +template +struct ApplyRule +{ + static bool applyRule(AssemblyItems::const_iterator _in, std::back_insert_iterator _out) + { + return Method::applySimple(_in[0], _out); + } +}; + template struct SimplePeepholeOptimizerMethod { @@ -44,7 +73,7 @@ struct SimplePeepholeOptimizerMethod { if ( _state.i + WindowSize <= _state.items.size() && - Method::applySimple(_state.items.begin() + _state.i, _state.out) + ApplyRule::applyRule(_state.items.begin() + _state.i, _state.out) ) { _state.i += WindowSize; @@ -57,20 +86,20 @@ struct SimplePeepholeOptimizerMethod struct Identity: SimplePeepholeOptimizerMethod { - static bool applySimple(AssemblyItems::const_iterator _in, std::back_insert_iterator _out) + static bool applySimple(AssemblyItem const& _item, std::back_insert_iterator _out) { - *_out = *_in; + *_out = _item; return true; } }; struct PushPop: SimplePeepholeOptimizerMethod { - static size_t applySimple(AssemblyItems::const_iterator _in, std::back_insert_iterator) + static size_t applySimple(AssemblyItem const& _push, AssemblyItem const& _pop, std::back_insert_iterator) { - auto t = _in[0].type(); - return _in[1] == Instruction::POP && ( - SemanticInformation::isDupInstruction(_in[0]) || + auto t = _push.type(); + return _pop == Instruction::POP && ( + SemanticInformation::isDupInstruction(_push) || t == Push || t == PushString || t == PushTag || t == PushSub || t == PushSubSize || t == PushProgramSize || t == PushData || t == PushLibraryAddress ); @@ -104,26 +133,55 @@ struct AddPop: SimplePeepholeOptimizerMethod struct DoubleSwap: SimplePeepholeOptimizerMethod { - static size_t applySimple(AssemblyItems::const_iterator _in, std::back_insert_iterator) + static size_t applySimple(AssemblyItem const& _s1, AssemblyItem const& _s2, std::back_insert_iterator) { - return _in[0] == _in[1] && SemanticInformation::isSwapInstruction(_in[0]); + return _s1 == _s2 && SemanticInformation::isSwapInstruction(_s1); } }; struct JumpToNext: SimplePeepholeOptimizerMethod { - static size_t applySimple(AssemblyItems::const_iterator _in, std::back_insert_iterator _out) + static size_t applySimple( + AssemblyItem const& _pushTag, + AssemblyItem const& _jump, + AssemblyItem const& _tag, + std::back_insert_iterator _out + ) + { + if ( + _pushTag.type() == PushTag && + (_jump == Instruction::JUMP || _jump == Instruction::JUMPI) && + _tag.type() == Tag && + _pushTag.data() == _tag.data() + ) + { + if (_jump == Instruction::JUMPI) + *_out = AssemblyItem(Instruction::POP, _jump.location()); + *_out = _tag; + return true; + } + else + return false; + } +}; + +struct TagConjunctions: SimplePeepholeOptimizerMethod +{ + static bool applySimple( + AssemblyItem const& _pushTag, + AssemblyItem const& _pushConstant, + AssemblyItem const& _and, + std::back_insert_iterator _out + ) { if ( - _in[0].type() == PushTag && - (_in[1] == Instruction::JUMP || _in[1] == Instruction::JUMPI) && - _in[2].type() == Tag && - _in[0].data() == _in[2].data() + _pushTag.type() == PushTag && + _and == Instruction::AND && + _pushConstant.type() == Push && + (_pushConstant.data() & u256(0xFFFFFFFF)) == u256(0xFFFFFFFF) ) { - if (_in[1] == Instruction::JUMPI) - *_out = AssemblyItem(Instruction::POP, _in[1].location()); - *_out = _in[2]; + *_out = _pushTag; return true; } else @@ -162,26 +220,6 @@ struct UnreachableCode } }; - -struct TagConjunctions: SimplePeepholeOptimizerMethod -{ - static bool applySimple(AssemblyItems::const_iterator _in, std::back_insert_iterator _out) - { - if ( - _in[0].type() == PushTag && - _in[2] == Instruction::AND && - _in[1].type() == Push && - (_in[1].data() & u256(0xFFFFFFFF)) == u256(0xFFFFFFFF) - ) - { - *_out = _in[0]; - return true; - } - else - return false; - } -}; - void applyMethods(OptimiserState&) { assertThrow(false, OptimizerException, "Peephole optimizer failed to apply identity."); -- cgit v1.2.3 From f5216249527a82834162d66f93f15a50346e38d4 Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 24 Nov 2016 21:08:30 +0100 Subject: Integrate AddPop. --- libevmasm/PeepholeOptimiser.cpp | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'libevmasm/PeepholeOptimiser.cpp') diff --git a/libevmasm/PeepholeOptimiser.cpp b/libevmasm/PeepholeOptimiser.cpp index c8a28afc..b96b0295 100644 --- a/libevmasm/PeepholeOptimiser.cpp +++ b/libevmasm/PeepholeOptimiser.cpp @@ -95,7 +95,7 @@ struct Identity: SimplePeepholeOptimizerMethod struct PushPop: SimplePeepholeOptimizerMethod { - static size_t applySimple(AssemblyItem const& _push, AssemblyItem const& _pop, std::back_insert_iterator) + static bool applySimple(AssemblyItem const& _push, AssemblyItem const& _pop, std::back_insert_iterator) { auto t = _push.type(); return _pop == Instruction::POP && ( @@ -106,23 +106,20 @@ struct PushPop: SimplePeepholeOptimizerMethod } }; -struct AddPop: SimplePeepholeOptimizerMethod +struct OpPop: SimplePeepholeOptimizerMethod { - static bool apply(AssemblyItems::const_iterator _in, std::back_insert_iterator _out) + static bool applySimple( + AssemblyItem const& _op, + AssemblyItem const& _pop, + std::back_insert_iterator _out + ) { - if (_in[1] == Instruction::POP && - _in[0].type() == Operation - ) + if (_pop == Instruction::POP && _op.type() == Operation) { - Instruction i0 = _in[0].instruction(); - if (instructionInfo(i0).ret == 1 && - !SemanticInformation::invalidatesMemory(i0) && - !SemanticInformation::invalidatesStorage(i0) && - !SemanticInformation::altersControlFlow(i0) && - !instructionInfo(i0).sideEffects - ) + Instruction instr = _op.instruction(); + if (instructionInfo(instr).ret == 1 && !instructionInfo(instr).sideEffects) { - for (int j = 0; j < instructionInfo(i0).args; j++) + for (int j = 0; j < instructionInfo(instr).args; j++) *_out = Instruction::POP; return true; } @@ -236,7 +233,7 @@ bool PeepholeOptimiser::optimise() { OptimiserState state {m_items, 0, std::back_inserter(m_optimisedItems)}; while (state.i < m_items.size()) - applyMethods(state, PushPop(), AddPop(), DoubleSwap(), JumpToNext(), UnreachableCode(), TagConjunctions(), Identity()); + applyMethods(state, PushPop(), OpPop(), DoubleSwap(), JumpToNext(), UnreachableCode(), TagConjunctions(), Identity()); if (m_optimisedItems.size() < m_items.size()) { m_items = std::move(m_optimisedItems); -- cgit v1.2.3