Stricter tests for constant optimization.

This commit is contained in:
chriseth 2017-04-03 14:41:09 +02:00
parent f77f2123f0
commit e2f00c96d5

View File

@ -74,16 +74,17 @@ public:
void compileBothVersions( void compileBothVersions(
std::string const& _sourceCode, std::string const& _sourceCode,
u256 const& _value = 0, u256 const& _value = 0,
std::string const& _contractName = "" std::string const& _contractName = "",
unsigned const _optimizeRuns = 200
) )
{ {
bytes nonOptimizedBytecode = compileAndRunWithOptimizer(_sourceCode, _value, _contractName, false); bytes nonOptimizedBytecode = compileAndRunWithOptimizer(_sourceCode, _value, _contractName, false, _optimizeRuns);
m_nonOptimizedContract = m_contractAddress; m_nonOptimizedContract = m_contractAddress;
bytes optimizedBytecode = compileAndRunWithOptimizer(_sourceCode, _value, _contractName, true); bytes optimizedBytecode = compileAndRunWithOptimizer(_sourceCode, _value, _contractName, true, _optimizeRuns);
size_t nonOptimizedSize = numInstructions(nonOptimizedBytecode); size_t nonOptimizedSize = numInstructions(nonOptimizedBytecode);
size_t optimizedSize = numInstructions(optimizedBytecode); size_t optimizedSize = numInstructions(optimizedBytecode);
BOOST_CHECK_MESSAGE( BOOST_CHECK_MESSAGE(
optimizedSize < nonOptimizedSize, _optimizeRuns < 50 || optimizedSize < nonOptimizedSize,
string("Optimizer did not reduce bytecode size. Non-optimized size: ") + string("Optimizer did not reduce bytecode size. Non-optimized size: ") +
std::to_string(nonOptimizedSize) + " - optimized size: " + std::to_string(nonOptimizedSize) + " - optimized size: " +
std::to_string(optimizedSize) std::to_string(optimizedSize)
@ -1191,31 +1192,42 @@ BOOST_AUTO_TEST_CASE(clear_unreachable_code)
BOOST_AUTO_TEST_CASE(computing_constants) BOOST_AUTO_TEST_CASE(computing_constants)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(
contract c { contract C {
uint a; uint m_a;
uint b; uint m_b;
uint c; uint m_c;
function set() returns (uint a, uint b, uint c) { uint m_d;
a = 0x77abc0000000000000000000000000000000000000000000000000000000001; function C() {
b = 0x817416927846239487123469187231298734162934871263941234127518276; set();
}
function set() returns (uint) {
m_a = 0x77abc0000000000000000000000000000000000000000000000000000000001;
m_b = 0x817416927846239487123469187231298734162934871263941234127518276;
g(); g();
return 1;
} }
function g() { function g() {
b = 0x817416927846239487123469187231298734162934871263941234127518276; m_b = 0x817416927846239487123469187231298734162934871263941234127518276;
c = 0x817416927846239487123469187231298734162934871263941234127518276; m_c = 0x817416927846239487123469187231298734162934871263941234127518276;
h();
} }
function get() returns (uint ra, uint rb, uint rc) { function h() {
ra = a; m_d = 0xff05694900000000000000000000000000000000000000000000000000000000;
rb = b; }
rc = c ; function get() returns (uint ra, uint rb, uint rc, uint rd) {
ra = m_a;
rb = m_b;
rc = m_c;
rd = m_d;
} }
} }
)"; )";
compileBothVersions(sourceCode); compileBothVersions(sourceCode, 0, "C", 1);
compareVersions("get()");
compareVersions("set()"); compareVersions("set()");
compareVersions("get()"); compareVersions("get()");
bytes optimizedBytecode = compileAndRunWithOptimizer(sourceCode, 0, "c", true, 1); bytes optimizedBytecode = compileAndRunWithOptimizer(sourceCode, 0, "C", true, 1);
bytes complicatedConstant = toBigEndian(u256("0x817416927846239487123469187231298734162934871263941234127518276")); bytes complicatedConstant = toBigEndian(u256("0x817416927846239487123469187231298734162934871263941234127518276"));
unsigned occurrences = 0; unsigned occurrences = 0;
for (auto iter = optimizedBytecode.cbegin(); iter < optimizedBytecode.cend(); ++occurrences) for (auto iter = optimizedBytecode.cbegin(); iter < optimizedBytecode.cend(); ++occurrences)