Optimizing various single operations.

This commit is contained in:
chriseth 2015-03-20 22:39:42 +01:00
parent 671254626a
commit 782e69fbe4

View File

@ -74,6 +74,14 @@ public:
"\nOptimized: " + toHex(optimizedOutput)); "\nOptimized: " + toHex(optimizedOutput));
} }
void checkCSE(AssemblyItems const& _input, AssemblyItems const& _expectation)
{
eth::CommonSubexpressionEliminator cse;
BOOST_REQUIRE(cse.feedItems(_input.begin(), _input.end()) == _input.end());
AssemblyItems output = cse.getOptimizedItems();
BOOST_CHECK_EQUAL_COLLECTIONS(_expectation.begin(), _expectation.end(), output.begin(), output.end());
}
protected: protected:
Address m_optimizedContract; Address m_optimizedContract;
Address m_nonOptimizedContract; Address m_nonOptimizedContract;
@ -199,61 +207,59 @@ BOOST_AUTO_TEST_CASE(cse_intermediate_swap)
BOOST_AUTO_TEST_CASE(cse_negative_stack_access) BOOST_AUTO_TEST_CASE(cse_negative_stack_access)
{ {
eth::CommonSubexpressionEliminator cse; AssemblyItems input{Instruction::DUP2, u256(0)};
AssemblyItems input{AssemblyItem(Instruction::DUP2), AssemblyItem(u256(0))}; checkCSE(input, input);
BOOST_REQUIRE(cse.feedItems(input.begin(), input.end()) == input.end());
AssemblyItems output = cse.getOptimizedItems();
BOOST_CHECK_EQUAL_COLLECTIONS(input.begin(), input.end(), output.begin(), output.end());
} }
BOOST_AUTO_TEST_CASE(cse_negative_stack_end) BOOST_AUTO_TEST_CASE(cse_negative_stack_end)
{ {
eth::CommonSubexpressionEliminator cse; AssemblyItems input{Instruction::ADD};
AssemblyItems input{ checkCSE(input, input);
AssemblyItem(Instruction::ADD)
};
BOOST_REQUIRE(cse.feedItems(input.begin(), input.end()) == input.end());
AssemblyItems output = cse.getOptimizedItems();
BOOST_CHECK_EQUAL_COLLECTIONS(input.begin(), input.end(), output.begin(), output.end());
} }
BOOST_AUTO_TEST_CASE(cse_intermediate_negative_stack) BOOST_AUTO_TEST_CASE(cse_intermediate_negative_stack)
{ {
eth::CommonSubexpressionEliminator cse; AssemblyItems input{Instruction::ADD, u256(1), Instruction::DUP1};
AssemblyItems input{ checkCSE(input, input);
AssemblyItem(Instruction::ADD),
AssemblyItem(u256(1)),
AssemblyItem(Instruction::DUP2)
};
BOOST_REQUIRE(cse.feedItems(input.begin(), input.end()) == input.end());
AssemblyItems output = cse.getOptimizedItems();
BOOST_CHECK_EQUAL_COLLECTIONS(input.begin(), input.end(), output.begin(), output.end());
} }
BOOST_AUTO_TEST_CASE(cse_pop) BOOST_AUTO_TEST_CASE(cse_pop)
{ {
eth::CommonSubexpressionEliminator cse; checkCSE({Instruction::POP}, {Instruction::POP});
AssemblyItems input{
AssemblyItem(Instruction::POP)
};
BOOST_REQUIRE(cse.feedItems(input.begin(), input.end()) == input.end());
AssemblyItems output = cse.getOptimizedItems();
BOOST_CHECK_EQUAL_COLLECTIONS(input.begin(), input.end(), output.begin(), output.end());
} }
BOOST_AUTO_TEST_CASE(cse_unneeded_items) BOOST_AUTO_TEST_CASE(cse_unneeded_items)
{ {
eth::CommonSubexpressionEliminator cse;
AssemblyItems input{ AssemblyItems input{
AssemblyItem(Instruction::ADD), Instruction::ADD,
AssemblyItem(Instruction::SWAP1), Instruction::SWAP1,
AssemblyItem(Instruction::POP), Instruction::POP,
AssemblyItem(u256(7)), u256(7),
AssemblyItem(u256(8)), u256(8),
}; };
BOOST_REQUIRE(cse.feedItems(input.begin(), input.end()) == input.end()); checkCSE(input, input);
AssemblyItems output = cse.getOptimizedItems(); }
BOOST_CHECK_EQUAL_COLLECTIONS(input.begin(), input.end(), output.begin(), output.end());
BOOST_AUTO_TEST_CASE(cse_invariants)
{
AssemblyItems input{
Instruction::DUP1,
Instruction::DUP1,
u256(0),
Instruction::OR,
Instruction::OR
};
checkCSE(input, {Instruction::DUP1});
}
BOOST_AUTO_TEST_CASE(cse_subself)
{
checkCSE({Instruction::DUP1, Instruction::SUB}, {Instruction::POP, u256(0)});
}
BOOST_AUTO_TEST_CASE(cse_subother)
{
checkCSE({Instruction::SUB}, {Instruction::SUB});
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()