diff --git a/libevmasm/PeepholeOptimiser.cpp b/libevmasm/PeepholeOptimiser.cpp index 8e4a09fcc..b04c2158c 100644 --- a/libevmasm/PeepholeOptimiser.cpp +++ b/libevmasm/PeepholeOptimiser.cpp @@ -41,51 +41,28 @@ struct OptimiserState std::back_insert_iterator out; }; -template -struct ApplyRule +template +struct FunctionParameterCount; +template +struct FunctionParameterCount { -}; -template -struct ApplyRule -{ - static bool applyRule(AssemblyItems::const_iterator _in, std::back_insert_iterator _out) - { - return Method::applySimple(_in[0], _in[1], _in[2], _in[3], _out); - } -}; -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); - } + static constexpr auto value = sizeof...(Args); }; -template +template struct SimplePeepholeOptimizerMethod { + template + static bool applyRule(AssemblyItems::const_iterator _in, back_insert_iterator _out, index_sequence) + { + return Method::applySimple(_in[Indices]..., _out); + } static bool apply(OptimiserState& _state) { + static constexpr size_t WindowSize = FunctionParameterCount::value - 1; if ( _state.i + WindowSize <= _state.items.size() && - ApplyRule::applyRule(_state.items.begin() + static_cast(_state.i), _state.out) + applyRule(_state.items.begin() + static_cast(_state.i), _state.out, make_index_sequence{}) ) { _state.i += WindowSize; @@ -96,7 +73,7 @@ struct SimplePeepholeOptimizerMethod } }; -struct Identity: SimplePeepholeOptimizerMethod +struct Identity: SimplePeepholeOptimizerMethod { static bool applySimple(AssemblyItem const& _item, std::back_insert_iterator _out) { @@ -105,7 +82,7 @@ struct Identity: SimplePeepholeOptimizerMethod } }; -struct PushPop: SimplePeepholeOptimizerMethod +struct PushPop: SimplePeepholeOptimizerMethod { static bool applySimple(AssemblyItem const& _push, AssemblyItem const& _pop, std::back_insert_iterator) { @@ -118,7 +95,7 @@ struct PushPop: SimplePeepholeOptimizerMethod } }; -struct OpPop: SimplePeepholeOptimizerMethod +struct OpPop: SimplePeepholeOptimizerMethod { static bool applySimple( AssemblyItem const& _op, @@ -140,7 +117,7 @@ struct OpPop: SimplePeepholeOptimizerMethod } }; -struct DoubleSwap: SimplePeepholeOptimizerMethod +struct DoubleSwap: SimplePeepholeOptimizerMethod { static size_t applySimple(AssemblyItem const& _s1, AssemblyItem const& _s2, std::back_insert_iterator) { @@ -148,7 +125,7 @@ struct DoubleSwap: SimplePeepholeOptimizerMethod } }; -struct DoublePush: SimplePeepholeOptimizerMethod +struct DoublePush: SimplePeepholeOptimizerMethod { static bool applySimple(AssemblyItem const& _push1, AssemblyItem const& _push2, std::back_insert_iterator _out) { @@ -163,7 +140,7 @@ struct DoublePush: SimplePeepholeOptimizerMethod } }; -struct CommutativeSwap: SimplePeepholeOptimizerMethod +struct CommutativeSwap: SimplePeepholeOptimizerMethod { static bool applySimple(AssemblyItem const& _swap, AssemblyItem const& _op, std::back_insert_iterator _out) { @@ -181,7 +158,7 @@ struct CommutativeSwap: SimplePeepholeOptimizerMethod } }; -struct SwapComparison: SimplePeepholeOptimizerMethod +struct SwapComparison: SimplePeepholeOptimizerMethod { static bool applySimple(AssemblyItem const& _swap, AssemblyItem const& _op, std::back_insert_iterator _out) { @@ -207,7 +184,7 @@ struct SwapComparison: SimplePeepholeOptimizerMethod }; /// Remove swapN after dupN -struct DupSwap: SimplePeepholeOptimizerMethod +struct DupSwap: SimplePeepholeOptimizerMethod { static size_t applySimple( AssemblyItem const& _dupN, @@ -230,7 +207,7 @@ struct DupSwap: SimplePeepholeOptimizerMethod }; -struct IsZeroIsZeroJumpI: SimplePeepholeOptimizerMethod +struct IsZeroIsZeroJumpI: SimplePeepholeOptimizerMethod { static size_t applySimple( AssemblyItem const& _iszero1, @@ -256,7 +233,7 @@ struct IsZeroIsZeroJumpI: SimplePeepholeOptimizerMethod } }; -struct JumpToNext: SimplePeepholeOptimizerMethod +struct JumpToNext: SimplePeepholeOptimizerMethod { static size_t applySimple( AssemblyItem const& _pushTag, @@ -282,7 +259,7 @@ struct JumpToNext: SimplePeepholeOptimizerMethod } }; -struct TagConjunctions: SimplePeepholeOptimizerMethod +struct TagConjunctions: SimplePeepholeOptimizerMethod { static bool applySimple( AssemblyItem const& _pushTag, @@ -317,7 +294,7 @@ struct TagConjunctions: SimplePeepholeOptimizerMethod } }; -struct TruthyAnd: SimplePeepholeOptimizerMethod +struct TruthyAnd: SimplePeepholeOptimizerMethod { static bool applySimple( AssemblyItem const& _push,