Remove useless SWAP1 in front of commutative operations

This commit is contained in:
Alex Beregszaszi 2018-03-29 12:10:39 +01:00
parent 0edce4b570
commit 17bcabb6cf
3 changed files with 72 additions and 1 deletions

View File

@ -5,6 +5,7 @@ Features:
* Commandline interface: Error when missing or inaccessible file detected. Suppress it with the ``--ignore-missing`` flag. * Commandline interface: Error when missing or inaccessible file detected. Suppress it with the ``--ignore-missing`` flag.
* General: Support accessing dynamic return data in post-byzantium EVMs. * General: Support accessing dynamic return data in post-byzantium EVMs.
* Interfaces: Allow overriding external functions in interfaces with public in an implementing contract. * Interfaces: Allow overriding external functions in interfaces with public in an implementing contract.
* Optimizer: Remove useless ``SWAP1`` instruction preceding a commutative instruction (such as ``ADD``, ``MUL``, etc).
* Optimizer: Optimize across ``mload`` if ``msize()`` is not used. * Optimizer: Optimize across ``mload`` if ``msize()`` is not used.
* Syntax Checker: Issue warning for empty structs (or error as experimental 0.5.0 feature). * Syntax Checker: Issue warning for empty structs (or error as experimental 0.5.0 feature).

View File

@ -154,6 +154,25 @@ struct DoublePush: SimplePeepholeOptimizerMethod<DoublePush, 2>
} }
}; };
struct CommutativeSwap: SimplePeepholeOptimizerMethod<CommutativeSwap, 2>
{
static bool applySimple(AssemblyItem const& _swap, AssemblyItem const& _op, std::back_insert_iterator<AssemblyItems> _out)
{
// Remove SWAP1 if following instruction is commutative
if (
_swap.type() == Operation &&
_swap.instruction() == Instruction::SWAP1 &&
SemanticInformation::isCommutativeOperation(_op)
)
{
*_out = _op;
return true;
}
else
return false;
}
};
struct JumpToNext: SimplePeepholeOptimizerMethod<JumpToNext, 3> struct JumpToNext: SimplePeepholeOptimizerMethod<JumpToNext, 3>
{ {
static size_t applySimple( static size_t applySimple(
@ -260,7 +279,7 @@ bool PeepholeOptimiser::optimise()
{ {
OptimiserState state {m_items, 0, std::back_inserter(m_optimisedItems)}; OptimiserState state {m_items, 0, std::back_inserter(m_optimisedItems)};
while (state.i < m_items.size()) while (state.i < m_items.size())
applyMethods(state, PushPop(), OpPop(), DoublePush(), DoubleSwap(), JumpToNext(), UnreachableCode(), TagConjunctions(), Identity()); applyMethods(state, PushPop(), OpPop(), DoublePush(), DoubleSwap(), CommutativeSwap(), JumpToNext(), UnreachableCode(), TagConjunctions(), Identity());
if (m_optimisedItems.size() < m_items.size() || ( if (m_optimisedItems.size() < m_items.size() || (
m_optimisedItems.size() == m_items.size() && ( m_optimisedItems.size() == m_items.size() && (
eth::bytesRequired(m_optimisedItems, 3) < eth::bytesRequired(m_items, 3) || eth::bytesRequired(m_optimisedItems, 3) < eth::bytesRequired(m_items, 3) ||

View File

@ -858,6 +858,57 @@ BOOST_AUTO_TEST_CASE(peephole_pop_calldatasize)
BOOST_CHECK(items.empty()); BOOST_CHECK(items.empty());
} }
BOOST_AUTO_TEST_CASE(peephole_commutative_swap1)
{
AssemblyItems items{
u256(1),
u256(2),
Instruction::SWAP1,
Instruction::ADD,
u256(4),
u256(5)
};
AssemblyItems expectation{
u256(1),
u256(2),
Instruction::ADD,
u256(4),
u256(5)
};
PeepholeOptimiser peepOpt(items);
BOOST_REQUIRE(peepOpt.optimise());
BOOST_CHECK_EQUAL_COLLECTIONS(
items.begin(), items.end(),
expectation.begin(), expectation.end()
);
}
BOOST_AUTO_TEST_CASE(peephole_noncommutative_swap1)
{
AssemblyItems items{
u256(1),
u256(2),
Instruction::SWAP1,
Instruction::SUB,
u256(4),
u256(5)
};
AssemblyItems expectation{
u256(1),
u256(2),
Instruction::SWAP1,
Instruction::SUB,
u256(4),
u256(5)
};
PeepholeOptimiser peepOpt(items);
BOOST_REQUIRE(!peepOpt.optimise());
BOOST_CHECK_EQUAL_COLLECTIONS(
items.begin(), items.end(),
expectation.begin(), expectation.end()
);
}
BOOST_AUTO_TEST_CASE(jumpdest_removal) BOOST_AUTO_TEST_CASE(jumpdest_removal)
{ {
AssemblyItems items{ AssemblyItems items{