diff --git a/Changelog.md b/Changelog.md index f0cfcb147..882f77b60 100644 --- a/Changelog.md +++ b/Changelog.md @@ -13,6 +13,7 @@ Compiler Features: Bugfixes: + * Yul Optimizer: Hash hex and decimal literals according to their value instead of their representation, improving the detection of equivalent functions. * Solidity Upgrade Tool ``solidity-upgrade``: Fix the tool returning success code on uncaught exceptions. diff --git a/libyul/optimiser/BlockHasher.cpp b/libyul/optimiser/BlockHasher.cpp index 182e11dda..53a357739 100644 --- a/libyul/optimiser/BlockHasher.cpp +++ b/libyul/optimiser/BlockHasher.cpp @@ -54,7 +54,10 @@ std::map BlockHasher::run(Block const& _block) void BlockHasher::operator()(Literal const& _literal) { hash64(compileTimeLiteralHash("Literal")); - hash64(_literal.value.hash()); + if (_literal.kind == LiteralKind::Number) + hash64(std::hash{}(valueOfNumberLiteral(_literal))); + else + hash64(_literal.value.hash()); hash64(_literal.type.hash()); hash8(static_cast(_literal.kind)); } diff --git a/test/libsolidity/semanticTests/array/copying/elements_of_nested_array_of_structs_memory_to_storage.sol b/test/libsolidity/semanticTests/array/copying/elements_of_nested_array_of_structs_memory_to_storage.sol index 9c0bb74ca..8bd1eb18a 100644 --- a/test/libsolidity/semanticTests/array/copying/elements_of_nested_array_of_structs_memory_to_storage.sol +++ b/test/libsolidity/semanticTests/array/copying/elements_of_nested_array_of_structs_memory_to_storage.sol @@ -33,8 +33,8 @@ contract C { // compileViaYul: true // ---- // test1((uint8[],uint8[2])[][][]): 0x20, 1, 0x20, 2, 0x40, 0x0140, 1, 0x20, 0x60, 3, 7, 2, 1, 2, 2, 0x40, 0x0100, 0x60, 17, 19, 2, 11, 13, 0x60, 31, 37, 2, 23, 29 -> 0x20, 2, 0x40, 0x0140, 1, 0x20, 0x60, 3, 7, 2, 1, 2, 2, 0x40, 0x0100, 0x60, 17, 19, 2, 11, 13, 0x60, 31, 37, 2, 23, 29 -// gas irOptimized: 332334 +// gas irOptimized: 332586 // test2((uint8[],uint8[2])[][1][]): 0x20, 2, 0x40, 0x0160, 0x20, 1, 0x20, 0x60, 17, 19, 2, 11, 13, 0x20, 1, 0x20, 0x60, 31, 37, 2, 23, 29 -> 0x20, 0x20, 1, 0x20, 0x60, 17, 19, 2, 11, 13 -// gas irOptimized: 145029 +// gas irOptimized: 145182 // test3((uint8[],uint8[2])[1][][2]): 0x20, 0x40, 0x60, 0, 2, 0x40, 288, 0x20, 0x60, 3, 7, 2, 1, 2, 0x20, 0x60, 17, 19, 2, 11, 13 -> 0x20, 2, 0x40, 288, 0x20, 0x60, 3, 7, 2, 1, 2, 0x20, 0x60, 17, 19, 2, 11, 13 -// gas irOptimized: 192344 +// gas irOptimized: 192512 diff --git a/test/libsolidity/semanticTests/array/copying/nested_array_of_structs_memory_to_storage.sol b/test/libsolidity/semanticTests/array/copying/nested_array_of_structs_memory_to_storage.sol index 688946aae..218e8f1c7 100644 --- a/test/libsolidity/semanticTests/array/copying/nested_array_of_structs_memory_to_storage.sol +++ b/test/libsolidity/semanticTests/array/copying/nested_array_of_structs_memory_to_storage.sol @@ -31,8 +31,8 @@ contract C { // compileViaYul: true // ---- // test1((uint8[],uint8[2])[][]): 0x20, 2, 0x40, 0x0140, 1, 0x20, 0x60, 3, 7, 2, 1, 2, 2, 0x40, 0x0100, 0x60, 17, 19, 2, 11, 13, 0x60, 31, 37, 2, 23, 29 -> 0x20, 2, 0x40, 0x0140, 1, 0x20, 0x60, 3, 7, 2, 1, 2, 2, 0x40, 0x0100, 0x60, 17, 19, 2, 11, 13, 0x60, 31, 37, 2, 23, 29 -// gas irOptimized: 308790 +// gas irOptimized: 309123 // test2((uint8[],uint8[2])[][1]): 0x20, 0x20, 1, 0x20, 0x60, 17, 19, 2, 11, 13 -> 0x20, 0x20, 1, 0x20, 0x60, 17, 19, 2, 11, 13 -// gas irOptimized: 118077 +// gas irOptimized: 118188 // test3((uint8[],uint8[2])[1][]): 0x20, 2, 0x40, 0x0120, 0x20, 0x60, 3, 7, 2, 1, 2, 0x20, 0x60, 17, 19, 2, 11, 13 -> 0x20, 2, 0x40, 0x0120, 0x20, 0x60, 3, 7, 2, 1, 2, 0x20, 0x60, 17, 19, 2, 11, 13 -// gas irOptimized: 190775 +// gas irOptimized: 190997 diff --git a/test/libsolidity/semanticTests/byte_array_to_storage_cleanup.sol b/test/libsolidity/semanticTests/byte_array_to_storage_cleanup.sol index 745b8ee12..d7a5c8bf3 100644 --- a/test/libsolidity/semanticTests/byte_array_to_storage_cleanup.sol +++ b/test/libsolidity/semanticTests/byte_array_to_storage_cleanup.sol @@ -28,7 +28,7 @@ contract C { // compileViaYul: also // ---- // constructor() -> -// gas irOptimized: 518935 +// gas irOptimized: 464753 // gas legacy: 729908 // gas legacyOptimized: 493347 // h() -> 0x20, 0x40, 0x00, 0 diff --git a/test/libsolidity/semanticTests/externalContracts/snark.sol b/test/libsolidity/semanticTests/externalContracts/snark.sol index 4c3cb78f9..7a341898a 100644 --- a/test/libsolidity/semanticTests/externalContracts/snark.sol +++ b/test/libsolidity/semanticTests/externalContracts/snark.sol @@ -299,6 +299,6 @@ contract Test { // gas legacyOptimized: 267239 // verifyTx() -> true // ~ emit Verified(string): 0x20, 0x16, "Successfully verified." -// gas irOptimized: 783949 +// gas irOptimized: 784027 // gas legacy: 805423 // gas legacyOptimized: 772571 diff --git a/test/libsolidity/semanticTests/userDefinedValueType/calldata.sol b/test/libsolidity/semanticTests/userDefinedValueType/calldata.sol index f395621d3..b2acbb01f 100644 --- a/test/libsolidity/semanticTests/userDefinedValueType/calldata.sol +++ b/test/libsolidity/semanticTests/userDefinedValueType/calldata.sol @@ -49,11 +49,11 @@ contract C { } // ---- // test_f() -> true -// gas irOptimized: 122053 +// gas irOptimized: 122510 // gas legacy: 126150 // gas legacyOptimized: 123163 // test_g() -> true -// gas irOptimized: 106138 +// gas irOptimized: 106903 // gas legacy: 112481 // gas legacyOptimized: 107706 // addresses(uint256): 0 -> 0x18 diff --git a/test/libyul/yulOptimizerTests/equivalentFunctionCombiner/constant_representation.yul b/test/libyul/yulOptimizerTests/equivalentFunctionCombiner/constant_representation.yul new file mode 100644 index 000000000..b7a815dd9 --- /dev/null +++ b/test/libyul/yulOptimizerTests/equivalentFunctionCombiner/constant_representation.yul @@ -0,0 +1,17 @@ +{ + f() + g() + function f() { mstore(0x01, mload(0x00)) } + function g() { mstore(1, mload(0)) } +} +// ---- +// step: equivalentFunctionCombiner +// +// { +// f() +// f() +// function f() +// { mstore(0x01, mload(0x00)) } +// function g() +// { mstore(1, mload(0)) } +// }