diff options
author | chriseth <c@ethdev.com> | 2015-05-12 22:17:31 +0800 |
---|---|---|
committer | chriseth <c@ethdev.com> | 2015-05-12 22:17:31 +0800 |
commit | b6bd56bccd836a1f97afe18468029504e525bba6 (patch) | |
tree | 2e492a69ac0125af4b470e1af7f0c82fde26ca4a /libsolidity | |
parent | fba1cc6e980beb61214b9320e5feee00d756cfc2 (diff) | |
parent | 54bc7dda5310b165d0dc2ecbbe5f770cfb290fe5 (diff) | |
download | dexon-solidity-b6bd56bccd836a1f97afe18468029504e525bba6.tar.gz dexon-solidity-b6bd56bccd836a1f97afe18468029504e525bba6.tar.zst dexon-solidity-b6bd56bccd836a1f97afe18468029504e525bba6.zip |
Merge pull request #1864 from chriseth/sol_knowledgeEngine2
Transfer knowledge about the state across jumps.
Diffstat (limited to 'libsolidity')
-rw-r--r-- | libsolidity/SolidityOptimizer.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/libsolidity/SolidityOptimizer.cpp b/libsolidity/SolidityOptimizer.cpp index 3cb6a536..e50469dd 100644 --- a/libsolidity/SolidityOptimizer.cpp +++ b/libsolidity/SolidityOptimizer.cpp @@ -251,6 +251,63 @@ BOOST_AUTO_TEST_CASE(function_calls) compareVersions("f(uint256)", 36); } +BOOST_AUTO_TEST_CASE(storage_write_in_loops) +{ + char const* sourceCode = R"( + contract test { + uint d; + function f(uint a) returns (uint r) { + var x = d; + for (uint i = 1; i < a * a; i++) { + r = d; + d = i; + } + + } + } + )"; + compileBothVersions(sourceCode); + compareVersions("f(uint256)", 0); + compareVersions("f(uint256)", 10); + compareVersions("f(uint256)", 36); +} + +BOOST_AUTO_TEST_CASE(retain_information_in_branches) +{ + // This tests that the optimizer knows that we already have "z == sha3(y)" inside both branches. + char const* sourceCode = R"( + contract c { + bytes32 d; + uint a; + function f(uint x, bytes32 y) returns (uint r_a, bytes32 r_d) { + bytes32 z = sha3(y); + if (x > 8) { + z = sha3(y); + a = x; + } else { + z = sha3(y); + a = x; + } + r_a = a; + r_d = d; + } + } + )"; + compileBothVersions(sourceCode); + compareVersions("f(uint256,bytes32)", 0, "abc"); + compareVersions("f(uint256,bytes32)", 8, "def"); + compareVersions("f(uint256,bytes32)", 10, "ghi"); + + m_optimize = true; + bytes optimizedBytecode = compileAndRun(sourceCode, 0, "c"); + size_t numSHA3s = 0; + eth::eachInstruction(optimizedBytecode, [&](Instruction _instr, u256 const&) { + if (_instr == eth::Instruction::SHA3) + numSHA3s++; + }); + BOOST_CHECK_EQUAL(1, numSHA3s); +} + BOOST_AUTO_TEST_CASE(cse_intermediate_swap) { eth::KnownState state; |