mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #3693 from ethereum/optimizeMLOAD
Optimize across MLOAD if MSIZE is not used.
This commit is contained in:
@@ -8756,6 +8756,32 @@ BOOST_AUTO_TEST_CASE(create_dynamic_array_with_zero_length)
|
||||
ABI_CHECK(callContractFunction("f()"), encodeArgs(u256(7)));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(correctly_initialize_memory_array_in_constructor)
|
||||
{
|
||||
// Memory arrays are initialized using codecopy past the size of the code.
|
||||
// This test checks that it also works in the constructor context.
|
||||
char const* sourceCode = R"(
|
||||
contract C {
|
||||
bool public success;
|
||||
function C() public {
|
||||
// Make memory dirty.
|
||||
assembly {
|
||||
for { let i := 0 } lt(i, 64) { i := add(i, 1) } {
|
||||
mstore(msize, not(0))
|
||||
}
|
||||
}
|
||||
uint16[3] memory c;
|
||||
require(c[0] == 0 && c[1] == 0 && c[2] == 0);
|
||||
uint16[] memory x = new uint16[](3);
|
||||
require(x[0] == 0 && x[1] == 0 && x[2] == 0);
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileAndRun(sourceCode, 0, "C");
|
||||
ABI_CHECK(callContractFunction("success()"), encodeArgs(u256(1)));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(return_does_not_skip_modifier)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
@@ -9119,7 +9145,7 @@ BOOST_AUTO_TEST_CASE(calling_uninitialized_function_in_detail)
|
||||
int mutex;
|
||||
function t() returns (uint) {
|
||||
if (mutex > 0)
|
||||
return 7;
|
||||
{ assembly { mstore(0, 7) return(0, 0x20) } }
|
||||
mutex = 1;
|
||||
// Avoid re-executing this function if we jump somewhere.
|
||||
x();
|
||||
@@ -9132,6 +9158,27 @@ BOOST_AUTO_TEST_CASE(calling_uninitialized_function_in_detail)
|
||||
ABI_CHECK(callContractFunction("t()"), encodeArgs());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(calling_uninitialized_function_through_array)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract C {
|
||||
int mutex;
|
||||
function t() returns (uint) {
|
||||
if (mutex > 0)
|
||||
{ assembly { mstore(0, 7) return(0, 0x20) } }
|
||||
mutex = 1;
|
||||
// Avoid re-executing this function if we jump somewhere.
|
||||
function() internal returns (uint)[200] x;
|
||||
x[0]();
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
)";
|
||||
|
||||
compileAndRun(sourceCode, 0, "C");
|
||||
ABI_CHECK(callContractFunction("t()"), encodeArgs());
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(pass_function_types_internally)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
|
||||
@@ -74,11 +74,11 @@ public:
|
||||
unsigned const _optimizeRuns = 200
|
||||
)
|
||||
{
|
||||
bytes nonOptimizedBytecode = compileAndRunWithOptimizer(_sourceCode, _value, _contractName, false, _optimizeRuns);
|
||||
m_nonOptimizedBytecode = compileAndRunWithOptimizer(_sourceCode, _value, _contractName, false, _optimizeRuns);
|
||||
m_nonOptimizedContract = m_contractAddress;
|
||||
bytes optimizedBytecode = compileAndRunWithOptimizer(_sourceCode, _value, _contractName, true, _optimizeRuns);
|
||||
size_t nonOptimizedSize = numInstructions(nonOptimizedBytecode);
|
||||
size_t optimizedSize = numInstructions(optimizedBytecode);
|
||||
m_optimizedBytecode = compileAndRunWithOptimizer(_sourceCode, _value, _contractName, true, _optimizeRuns);
|
||||
size_t nonOptimizedSize = numInstructions(m_nonOptimizedBytecode);
|
||||
size_t optimizedSize = numInstructions(m_optimizedBytecode);
|
||||
BOOST_CHECK_MESSAGE(
|
||||
_optimizeRuns < 50 || optimizedSize < nonOptimizedSize,
|
||||
string("Optimizer did not reduce bytecode size. Non-optimized size: ") +
|
||||
@@ -104,7 +104,7 @@ public:
|
||||
|
||||
/// @returns the number of intructions in the given bytecode, not taking the metadata hash
|
||||
/// into account.
|
||||
size_t numInstructions(bytes const& _bytecode)
|
||||
size_t numInstructions(bytes const& _bytecode, boost::optional<Instruction> _which = boost::optional<Instruction>{})
|
||||
{
|
||||
BOOST_REQUIRE(_bytecode.size() > 5);
|
||||
size_t metadataSize = (_bytecode[_bytecode.size() - 2] << 8) + _bytecode[_bytecode.size() - 1];
|
||||
@@ -112,13 +112,16 @@ public:
|
||||
BOOST_REQUIRE(_bytecode.size() >= metadataSize + 2);
|
||||
bytes realCode = bytes(_bytecode.begin(), _bytecode.end() - metadataSize - 2);
|
||||
size_t instructions = 0;
|
||||
solidity::eachInstruction(realCode, [&](Instruction, u256 const&) {
|
||||
instructions++;
|
||||
solidity::eachInstruction(realCode, [&](Instruction _instr, u256 const&) {
|
||||
if (!_which || *_which == _instr)
|
||||
instructions++;
|
||||
});
|
||||
return instructions;
|
||||
}
|
||||
|
||||
protected:
|
||||
bytes m_nonOptimizedBytecode;
|
||||
bytes m_optimizedBytecode;
|
||||
Address m_optimizedContract;
|
||||
Address m_nonOptimizedContract;
|
||||
};
|
||||
@@ -581,6 +584,29 @@ BOOST_AUTO_TEST_CASE(invalid_state_at_control_flow_join)
|
||||
compareVersions("test()");
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(optimise_multi_stores)
|
||||
{
|
||||
char const* sourceCode = R"(
|
||||
contract Test {
|
||||
struct S { uint16 a; uint16 b; uint16[3] c; uint[] dyn; }
|
||||
uint padding;
|
||||
S[] s;
|
||||
function f() public returns (uint16, uint16, uint16[3], uint) {
|
||||
uint16[3] memory c;
|
||||
c[0] = 7;
|
||||
c[1] = 8;
|
||||
c[2] = 9;
|
||||
s.push(S(1, 2, c, new uint[](4)));
|
||||
return (s[0].a, s[0].b, s[0].c, s[0].dyn[2]);
|
||||
}
|
||||
}
|
||||
)";
|
||||
compileBothVersions(sourceCode);
|
||||
compareVersions("f()");
|
||||
BOOST_CHECK_EQUAL(numInstructions(m_nonOptimizedBytecode, Instruction::SSTORE), 13);
|
||||
BOOST_CHECK_EQUAL(numInstructions(m_optimizedBytecode, Instruction::SSTORE), 11);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user