diff options
author | chriseth <chris@ethereum.org> | 2018-04-20 14:57:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-20 14:57:00 +0800 |
commit | 0f3284316d7b6ac3e30180811a0fd1677ad2850a (patch) | |
tree | d191fc2f3c6b2481950beba79a486d587d2a697c | |
parent | 20289609853e30e62969def1702de2a7795244ba (diff) | |
parent | 38460d8b473052bd9d4871ed7f10e72f8fb65615 (diff) | |
download | dexon-solidity-0f3284316d7b6ac3e30180811a0fd1677ad2850a.tar.gz dexon-solidity-0f3284316d7b6ac3e30180811a0fd1677ad2850a.tar.zst dexon-solidity-0f3284316d7b6ac3e30180811a0fd1677ad2850a.zip |
Merge pull request #3924 from ethereum/optim-address-op
Remove unnecessary masking of the result of known short instructions
-rw-r--r-- | Changelog.md | 1 | ||||
-rw-r--r-- | libevmasm/RuleList.h | 20 | ||||
-rw-r--r-- | test/libevmasm/Optimiser.cpp | 69 | ||||
-rw-r--r-- | test/libsolidity/SolidityEndToEndTest.cpp | 20 |
4 files changed, 110 insertions, 0 deletions
diff --git a/Changelog.md b/Changelog.md index faeb628b..207e2c21 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,6 +1,7 @@ ### 0.4.24 (unreleased) Features: + * Optimizer: Remove unnecessary masking of the result of known short instructions (``ADDRESS``, ``CALLER``, ``ORIGIN`` and ``COINBASE``). Bugfixes: diff --git a/libevmasm/RuleList.h b/libevmasm/RuleList.h index abcf170c..2b7da01b 100644 --- a/libevmasm/RuleList.h +++ b/libevmasm/RuleList.h @@ -174,6 +174,26 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleList( }); } + for (auto const& op: std::vector<Instruction>{ + Instruction::ADDRESS, + Instruction::CALLER, + Instruction::ORIGIN, + Instruction::COINBASE + }) + { + u256 const mask = (u256(1) << 160) - 1; + rules.push_back({ + {Instruction::AND, {{op, mask}}}, + [=]() -> Pattern { return op; }, + false + }); + rules.push_back({ + {Instruction::AND, {{mask, op}}}, + [=]() -> Pattern { return op; }, + false + }); + } + // Double negation of opcodes with boolean result for (auto const& op: std::vector<Instruction>{ Instruction::EQ, diff --git a/test/libevmasm/Optimiser.cpp b/test/libevmasm/Optimiser.cpp index 089be45d..4b399a14 100644 --- a/test/libevmasm/Optimiser.cpp +++ b/test/libevmasm/Optimiser.cpp @@ -1072,6 +1072,75 @@ BOOST_AUTO_TEST_CASE(cse_sub_zero) }); } +BOOST_AUTO_TEST_CASE(cse_remove_unwanted_masking_of_address) +{ + vector<Instruction> ops{ + Instruction::ADDRESS, + Instruction::CALLER, + Instruction::ORIGIN, + Instruction::COINBASE + }; + for (auto const& op: ops) + { + checkCSE({ + u256("0xffffffffffffffffffffffffffffffffffffffff"), + op, + Instruction::AND + }, { + op + }); + + checkCSE({ + op, + u256("0xffffffffffffffffffffffffffffffffffffffff"), + Instruction::AND + }, { + op + }); + + // do not remove mask for other masking + checkCSE({ + u256(1234), + op, + Instruction::AND + }, { + op, + u256(1234), + Instruction::AND + }); + + checkCSE({ + op, + u256(1234), + Instruction::AND + }, { + u256(1234), + op, + Instruction::AND + }); + } + + // leave other opcodes untouched + checkCSE({ + u256("0xffffffffffffffffffffffffffffffffffffffff"), + Instruction::CALLVALUE, + Instruction::AND + }, { + Instruction::CALLVALUE, + u256("0xffffffffffffffffffffffffffffffffffffffff"), + Instruction::AND + }); + + checkCSE({ + Instruction::CALLVALUE, + u256("0xffffffffffffffffffffffffffffffffffffffff"), + Instruction::AND + }, { + u256("0xffffffffffffffffffffffffffffffffffffffff"), + Instruction::CALLVALUE, + Instruction::AND + }); +} BOOST_AUTO_TEST_SUITE_END() diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 789b89d1..71386010 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -11881,6 +11881,26 @@ BOOST_AUTO_TEST_CASE(bitwise_shifting_constants_constantinople) BOOST_CHECK(callContractFunction("shr_3()") == encodeArgs(u256(1))); } +BOOST_AUTO_TEST_CASE(senders_balance) +{ + char const* sourceCode = R"( + contract C { + function f() public view returns (uint) { + return msg.sender.balance; + } + } + contract D { + C c = new C(); + constructor() payable { } + function f() public view returns (uint) { + return c.f(); + } + } + )"; + compileAndRun(sourceCode, 27, "D"); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(27))); +} + BOOST_AUTO_TEST_SUITE_END() } |