From c6ae1754b955d50e893bb4f2e453ffd5c6c930e2 Mon Sep 17 00:00:00 2001 From: Christian Date: Wed, 7 Jan 2015 15:41:11 +0100 Subject: [PATCH] Some changes to the optimizer. --- libevmcore/Assembly.cpp | 9 +++------ test/SolidityOptimizer.cpp | 14 +++++++------- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/libevmcore/Assembly.cpp b/libevmcore/Assembly.cpp index 5efbb9487..fd256d688 100644 --- a/libevmcore/Assembly.cpp +++ b/libevmcore/Assembly.cpp @@ -393,7 +393,6 @@ Assembly& Assembly::optimise(bool _enable) auto rw = r.second(vr); unsigned const vrSize = bytesRequiredBySlice(vr.begin(), vr.end()); unsigned const rwSize = bytesRequiredBySlice(rw.begin(), rw.end()); - //@todo check the actual size (including constant sizes) if (rwSize < vrSize || (rwSize == vrSize && popCountIncreased(vr, rw))) { copt << vr << "matches" << AssemblyItemsConstRef(&r.first) << "becomes..."; @@ -405,12 +404,10 @@ Assembly& Assembly::optimise(bool _enable) m_items.resize(m_items.size() + sizeIncrease, AssemblyItem(UndefinedItem)); move_backward(m_items.begin() + i, m_items.end() - sizeIncrease, m_items.end()); } + else + m_items.erase(m_items.begin() + i + rw.size(), m_items.begin() + i + vr.size()); - for (unsigned j = 0; j < max(rw.size(), vr.size()); ++j) - if (j < rw.size()) - m_items[i + j] = rw[j]; - else - m_items.erase(m_items.begin() + i + rw.size()); + copy(rw.begin(), rw.end(), m_items.begin() + i); count++; copt << "Now:\n" << m_items; diff --git a/test/SolidityOptimizer.cpp b/test/SolidityOptimizer.cpp index ef5c6f9b5..7e06c2e30 100644 --- a/test/SolidityOptimizer.cpp +++ b/test/SolidityOptimizer.cpp @@ -48,7 +48,7 @@ public: m_optimize = true; bytes optimizedBytecode = compileAndRun(_sourceCode, _value, _contractName); int sizeDiff = nonOptimizedBytecode.size() - optimizedBytecode.size(); - BOOST_CHECK_MESSAGE(sizeDiff == int(_expectedSizeDecrease), "Bytecode did only shrink by " + BOOST_CHECK_MESSAGE(sizeDiff == int(_expectedSizeDecrease), "Bytecode shrank by " + boost::lexical_cast(sizeDiff) + " bytes, expected: " + boost::lexical_cast(_expectedSizeDecrease)); m_optimizedContract = m_contractAddress; @@ -91,10 +91,10 @@ BOOST_AUTO_TEST_CASE(large_integers) contract test { function f() returns (uint a, uint b) { a = 0x234234872642837426347000000; - b = 0x110000000000000000000000002; + b = 0x10000000000000000000000002; } })"; - compileBothVersions(28, sourceCode); + compileBothVersions(33, sourceCode); compareVersions(0); } @@ -102,8 +102,8 @@ BOOST_AUTO_TEST_CASE(invariants) { char const* sourceCode = R"( contract test { - function f(uint a) returns (uint b) { - return (((a + (1 - 1)) ^ 0) | 0) & (uint(0) - 1); + function f(int a) returns (int b) { + return int(0) | (int(1) * (int(0) ^ (0 + a))); } })"; compileBothVersions(28, sourceCode); @@ -127,8 +127,8 @@ BOOST_AUTO_TEST_CASE(unused_expressions) BOOST_AUTO_TEST_CASE(constant_folding_both_sides) { // if constants involving the same associative and commutative operator are applied from both - // sides, the operator should be applied only once, because the expression compiler - // (even in non-optimized mode) pushes literals as late as possible + // sides, the operator should be applied only once, because the expression compiler pushes + // literals as late as possible char const* sourceCode = R"( contract test { function f(uint x) returns (uint y) {