From 974e35d09c1c737b10d916121bbb6a5efe1a9560 Mon Sep 17 00:00:00 2001 From: Harikrishnan Mulackal <webmail.hari@gmail.com> Date: Thu, 20 Aug 2020 16:56:25 +0200 Subject: [PATCH] Tests and changelog --- Changelog.md | 7 ++ test/cmdlineTests/evm_to_wasm_break/output | 99 ++++----------- test/cmdlineTests/yul_stack_opt/input.sol | 15 +++ test/cmdlineTests/yul_stack_opt/output | 119 +++++++++++++++--- test/libsolidity/StandardCompiler.cpp | 10 +- test/libsolidity/gasTests/abiv2_optimised.sol | 4 +- .../fullSuite/stack_compressor_msize.yul | 4 +- .../unusedFunctionParameterPruner.yul | 31 +++++ .../unusedFunctionParameterPruner_loop.yul | 33 +++++ ...nusedFunctionParameterPruner_recursion.yul | 29 +++++ .../unusedFunctionParameterPruner_return.yul | 34 +++++ .../unusedFunctionParameterPruner_simple.yul | 36 ++++++ .../LiteralRematerialiser.yul | 27 ++++ .../multiple_param.yul | 24 ++++ .../multiple_return.yul | 24 ++++ .../nested_function.yul | 56 +++++++++ .../nested_function_name_collision.yul | 52 ++++++++ .../no_return.yul | 21 ++++ .../no_unused.yul | 16 +++ .../recursion.yul | 21 ++++ .../unusedFunctionParameterPruner/simple.yul | 21 ++++ .../unusedFunctionParameterPruner/smoke.yul | 5 + .../too_many_arguments.yul | 23 ++++ 23 files changed, 615 insertions(+), 96 deletions(-) create mode 100644 test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner.yul create mode 100644 test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_loop.yul create mode 100644 test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_recursion.yul create mode 100644 test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_return.yul create mode 100644 test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_simple.yul create mode 100644 test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/LiteralRematerialiser.yul create mode 100644 test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/multiple_param.yul create mode 100644 test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/multiple_return.yul create mode 100644 test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/nested_function.yul create mode 100644 test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/nested_function_name_collision.yul create mode 100644 test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/no_return.yul create mode 100644 test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/no_unused.yul create mode 100644 test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/recursion.yul create mode 100644 test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/simple.yul create mode 100644 test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/smoke.yul create mode 100644 test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/too_many_arguments.yul diff --git a/Changelog.md b/Changelog.md index 62b100ec0..446eb2b4e 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,12 @@ ### 0.7.2 (unreleased) +Language Features: + + +Compiler Features: + * Yul Optimizer: Prune unused parameters in functions. + +Bugfixes: ### 0.7.1 (2020-09-02) diff --git a/test/cmdlineTests/evm_to_wasm_break/output b/test/cmdlineTests/evm_to_wasm_break/output index ddeb04529..ecb9a9ce3 100644 --- a/test/cmdlineTests/evm_to_wasm_break/output +++ b/test/cmdlineTests/evm_to_wasm_break/output @@ -31,9 +31,10 @@ object "object" { let x_6 := x_2 let x_7 := x_3 let _2 := 1 - let _3:i32 := i32.eqz(i32.eqz(i64.eqz(i64.or(i64.or(_1, _1), i64.or(_1, _2))))) + let _3 := i64.or(_1, _1) + let _4:i32 := i32.eqz(i32.eqz(i64.eqz(i64.or(_3, i64.or(_1, _2))))) for { } - i32.eqz(_3) + i32.eqz(_4) { let x_8, x_9, x_10, x_11 := add(x_4, x_5, x_6, x_7, _1, _1, _1, _2) x_4 := x_8 @@ -42,13 +43,10 @@ object "object" { x_7 := x_11 } { - let _4, _5, _6, _7 := lt(x_4, x_5, x_6, x_7, _1, _1, _1, 10) - let _8, _9, _10, _11 := iszero(_4, _5, _6, _7) - if i32.eqz(i64.eqz(i64.or(i64.or(_8, _9), i64.or(_10, _11)))) { break } - let _12, _13, _14, _15 := eq(x_4, x_5, x_6, x_7, _1, _1, _1, 2) - if i32.eqz(i64.eqz(i64.or(i64.or(_12, _13), i64.or(_14, _15)))) { break } - let _16, _17, _18, _19 := eq(x_4, x_5, x_6, x_7, _1, _1, _1, 4) - if i32.eqz(i64.eqz(i64.or(i64.or(_16, _17), i64.or(_18, _19)))) { continue } + let _5, _6, _7, _8 := iszero_170_789(_1, _1, _1, lt_172(x_4, x_5, x_6, x_7, _1, _1, _1, 10)) + if i32.eqz(i64.eqz(i64.or(i64.or(_5, _6), i64.or(_7, _8)))) { break } + if i32.eqz(i64.eqz(i64.or(_3, i64.or(_1, eq(x_4, x_5, x_6, x_7, _1, _1, _1, 2))))) { break } + if i32.eqz(i64.eqz(i64.or(_3, i64.or(_1, eq(x_4, x_5, x_6, x_7, _1, _1, _1, 4))))) { continue } } sstore(_1, _1, _1, _1, x_4, x_5, x_6, x_7) } @@ -69,11 +67,11 @@ object "object" { let r1_1, carry_2 := add_carry(x1, y1, carry_1) r1 := r1_1 } - function iszero(x1, x2, x3, x4) -> r1, r2, r3, r4 + function iszero_170_789(x1, x2, x3, x4) -> r1, r2, r3, r4 { r4 := i64.extend_i32_u(i64.eqz(i64.or(i64.or(x1, x2), i64.or(x3, x4)))) } - function eq(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4 + function eq(x1, x2, x3, x4, y1, y2, y3, y4) -> r4 { if i64.eq(x1, y1) { @@ -89,7 +87,7 @@ object "object" { case 1:i32 { r := 0xffffffff:i32 } default { r := i64.ne(a, b) } } - function lt(x1, x2, x3, x4, y1, y2, y3, y4) -> z1, z2, z3, z4 + function lt_172(x1, x2, x3, x4, y1, y2, y3, y4) -> z4 { let z:i32 := false switch cmp(x1, y1) @@ -154,7 +152,7 @@ object "object" { Binary representation: -0061736d0100000001480a60000060017e017e60027e7e017f60037e7e7e017e60047e7e7e7e017e60087e7e7e7e7e7e7e7e0060087e7e7e7e7e7e7e7e017e60057f7e7e7e7e0060027f7f0060037f7f7f0002310208657468657265756d0c73746f7261676553746f7265000808657468657265756d0c63616c6c44617461436f70790009030e0d0003060406020604010101070505030100010610037e0142000b7e0142000b7e0142000b071102066d656d6f72790200046d61696e00020ac3080dde02030a7e017f147e02404200210002402000200020002000100921012300210223012103230221040b20012105200221062003210720042108420121092000200084200020098484504545210a02400340200a45450d01024002402005200620072008200020002000420a1008210b2300210c2301210d2302210e0b0240200b200c200d200e1005210f2300211023012111230221120b200f201084201120128484504504400c030b024020052006200720082000200020004202100621132300211423012115230221160b2013201484201520168484504504400c030b0240200520062007200820002000200042041006211723002118230121192302211a0b20172018842019201a8484504504400c010b0b0240200520062007200820002000200020091004211b2300211c2301211d2302211e0b201b2105201c2106201d2107201e21080c000b0b20002000200020002005200620072008100e0b0b2901037e0240200020017c2105200520027c21032005200054200320055472ad21040b2004240020030b6c010b7e0240200320077c210c200c42007c210b024020022006200c200354200b200c5472ad1003210d2300210e0b200d210a024020012005200e1003210f230021100b200f2109024020002004201010032111230021120b201121080b20092400200a2401200b240220080b2401047e0240200020018420022003848450ad21070b20052400200624012007240220040b3901047e0240200020045104402001200551044020022006510440200320075104404201210b0b0b0b0b0b20092400200a2401200b240220080b2701027f024002402000200154210320034101460440417f210205200020015221020b0b0b20020b960102047e047f02404100210c0240200020041007210d200d41004604400240200120051007210e200e41004604400240200220061007210f200f41004604402003200754210c05200f41014604404100210c054101210c0b0b0b05200e41014604404100210c054101210c0b0b0b05200d41014604404100210c054101210c0b0b0b200cad210b0b20092400200a2401200b240220080b7601087e024042002000200184200284520440000b42002003422088520440000b41002003a7412010014100290000100c2108410041086a290000100c2109410041106a290000100c210a410041186a290000100c210b2008210420092105200a2106200b21070b20052400200624012007240220040b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100a421086210220022000421088100a8421010b20010b1e01027e02402000100b422086210220022000422088100b8421010b20010b3200024020002001100c370000200041086a2002100c370000200041106a2003100c370000200041186a2004100c3700000b0b2300024041002000200120022003100d41202004200520062007100d4100412010000b0b +0061736d0100000001480a60000060017e017e60027e7e017f60037e7e7e017e60047e7e7e7e017e60087e7e7e7e7e7e7e7e0060087e7e7e7e7e7e7e7e017e60057f7e7e7e7e0060027f7f0060037f7f7f0002310208657468657265756d0c73746f7261676553746f7265000808657468657265756d0c63616c6c44617461436f70790009030e0d0003060406020604010101070505030100010610037e0142000b7e0142000b7e0142000b071102066d656d6f72790200046d61696e00020af0070da302030b7e017f087e02404200210002402000200020002000100921012300210223012103230221040b20012105200221062003210720042108420121092000200084210a200a200020098484504545210b02400340200b45450d01024002402000200020002005200620072008200020002000420a10081005210c2300210d2301210e2302210f0b200c200d84200e200f8484504504400c030b200a20002005200620072008200020002000420210068484504504400c030b200a20002005200620072008200020002000420410068484504504400c010b0b024020052006200720082000200020002009100421102300211123012112230221130b201021052011210620122107201321080c000b0b20002000200020002005200620072008100e0b0b2901037e0240200020017c2105200520027c21032005200054200320055472ad21040b2004240020030b6c010b7e0240200320077c210c200c42007c210b024020022006200c200354200b200c5472ad1003210d2300210e0b200d210a024020012005200e1003210f230021100b200f2109024020002004201010032111230021120b201121080b20092400200a2401200b240220080b2401047e0240200020018420022003848450ad21070b20052400200624012007240220040b2d01017e024020002004510440200120055104402002200651044020032007510440420121080b0b0b0b0b20080b2701027f024002402000200154210320034101460440417f210205200020015221020b0b0b20020b8a0102017e047f0240410021090240200020041007210a200a41004604400240200120051007210b200b41004604400240200220061007210c200c41004604402003200754210905200c41014604404100210905410121090b0b0b05200b41014604404100210905410121090b0b0b05200a41014604404100210905410121090b0b0b2009ad21080b20080b7601087e024042002000200184200284520440000b42002003422088520440000b41002003a7412010014100290000100c2108410041086a290000100c2109410041106a290000100c210a410041186a290000100c210b2008210420092105200a2106200b21070b20052400200624012007240220040b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100a421086210220022000421088100a8421010b20010b1e01027e02402000100b422086210220022000422088100b8421010b20010b3200024020002001100c370000200041086a2002100c370000200041106a2003100c370000200041186a2004100c3700000b0b2300024041002000200120022003100d41202004200520062007100d4100412010000b0b Text representation: (module @@ -177,23 +175,12 @@ Text representation: (local $x_6 i64) (local $x_7 i64) (local $_2 i64) - (local $_3 i32) - (local $_4 i64) + (local $_3 i64) + (local $_4 i32) (local $_5 i64) (local $_6 i64) (local $_7 i64) (local $_8 i64) - (local $_9 i64) - (local $_10 i64) - (local $_11 i64) - (local $_12 i64) - (local $_13 i64) - (local $_14 i64) - (local $_15 i64) - (local $_16 i64) - (local $_17 i64) - (local $_18 i64) - (local $_19 i64) (local $x_8 i64) (local $x_9 i64) (local $x_10 i64) @@ -212,46 +199,26 @@ Text representation: (local.set $x_6 (local.get $x_2)) (local.set $x_7 (local.get $x_3)) (local.set $_2 (i64.const 1)) - (local.set $_3 (i32.eqz (i32.eqz (i64.eqz (i64.or (i64.or (local.get $_1) (local.get $_1)) (i64.or (local.get $_1) (local.get $_2))))))) + (local.set $_3 (i64.or (local.get $_1) (local.get $_1))) + (local.set $_4 (i32.eqz (i32.eqz (i64.eqz (i64.or (local.get $_3) (i64.or (local.get $_1) (local.get $_2))))))) (block $label__3 (loop $label__5 - (br_if $label__3 (i32.eqz (i32.eqz (local.get $_3)))) + (br_if $label__3 (i32.eqz (i32.eqz (local.get $_4)))) (block $label__4 (block - (local.set $_4 (call $lt (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 10))) - (local.set $_5 (global.get $global_)) - (local.set $_6 (global.get $global__1)) - (local.set $_7 (global.get $global__2)) + (local.set $_5 (call $iszero_170_789 (local.get $_1) (local.get $_1) (local.get $_1) (call $lt_172 (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 10)))) + (local.set $_6 (global.get $global_)) + (local.set $_7 (global.get $global__1)) + (local.set $_8 (global.get $global__2)) ) - (block - (local.set $_8 (call $iszero (local.get $_4) (local.get $_5) (local.get $_6) (local.get $_7))) - (local.set $_9 (global.get $global_)) - (local.set $_10 (global.get $global__1)) - (local.set $_11 (global.get $global__2)) - - ) - (if (i32.eqz (i64.eqz (i64.or (i64.or (local.get $_8) (local.get $_9)) (i64.or (local.get $_10) (local.get $_11))))) (then + (if (i32.eqz (i64.eqz (i64.or (i64.or (local.get $_5) (local.get $_6)) (i64.or (local.get $_7) (local.get $_8))))) (then (br $label__3) )) - (block - (local.set $_12 (call $eq (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 2))) - (local.set $_13 (global.get $global_)) - (local.set $_14 (global.get $global__1)) - (local.set $_15 (global.get $global__2)) - - ) - (if (i32.eqz (i64.eqz (i64.or (i64.or (local.get $_12) (local.get $_13)) (i64.or (local.get $_14) (local.get $_15))))) (then + (if (i32.eqz (i64.eqz (i64.or (local.get $_3) (i64.or (local.get $_1) (call $eq (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 2)))))) (then (br $label__3) )) - (block - (local.set $_16 (call $eq (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 4))) - (local.set $_17 (global.get $global_)) - (local.set $_18 (global.get $global__1)) - (local.set $_19 (global.get $global__2)) - - ) - (if (i32.eqz (i64.eqz (i64.or (i64.or (local.get $_16) (local.get $_17)) (i64.or (local.get $_18) (local.get $_19))))) (then + (if (i32.eqz (i64.eqz (i64.or (local.get $_3) (i64.or (local.get $_1) (call $eq (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 4)))))) (then (br $label__4) )) @@ -343,7 +310,7 @@ Text representation: (local.get $r1) ) -(func $iszero +(func $iszero_170_789 (param $x1 i64) (param $x2 i64) (param $x3 i64) @@ -373,9 +340,6 @@ Text representation: (param $y3 i64) (param $y4 i64) (result i64) - (local $r1 i64) - (local $r2 i64) - (local $r3 i64) (local $r4 i64) (block $label__9 (if (i64.eq (local.get $x1) (local.get $y1)) (then @@ -389,10 +353,7 @@ Text representation: )) ) - (global.set $global_ (local.get $r2)) - (global.set $global__1 (local.get $r3)) - (global.set $global__2 (local.get $r4)) - (local.get $r1) + (local.get $r4) ) (func $cmp @@ -416,7 +377,7 @@ Text representation: (local.get $r) ) -(func $lt +(func $lt_172 (param $x1 i64) (param $x2 i64) (param $x3 i64) @@ -426,9 +387,6 @@ Text representation: (param $y3 i64) (param $y4 i64) (result i64) - (local $z1 i64) - (local $z2 i64) - (local $z3 i64) (local $z4 i64) (local $z i32) (local $condition_12 i32) @@ -476,10 +434,7 @@ Text representation: (local.set $z4 (i64.extend_i32_u (local.get $z))) ) - (global.set $global_ (local.get $z2)) - (global.set $global__1 (local.get $z3)) - (global.set $global__2 (local.get $z4)) - (local.get $z1) + (local.get $z4) ) (func $calldataload diff --git a/test/cmdlineTests/yul_stack_opt/input.sol b/test/cmdlineTests/yul_stack_opt/input.sol index 772a6d4df..d60620ecf 100644 --- a/test/cmdlineTests/yul_stack_opt/input.sol +++ b/test/cmdlineTests/yul_stack_opt/input.sol @@ -17,6 +17,21 @@ sstore(add(a, 10), b) sstore(add(a, 11), b) sstore(add(a, 12), b) + a3 := 1 + b3 := 1 + c3 := 1 + d3 := 1 + e3 := 1 + f3 := 1 + g3 := 1 + h3 := 1 + i3 := 1 + j3 := 1 + k3 := 1 + l3 := 1 + m3 := 1 + o3 := 1 + p3 := 1 } let a1, b1, c1, d1, e1, f1, g1, h1, i1, j1, k1, l1, m1, n1, o1, p1 := fun() let a2, b2, c2, d2, e2, f2, g2, h2, i2, j2, k2, l2, m2, n2, o2, p2 := fun() diff --git a/test/cmdlineTests/yul_stack_opt/output b/test/cmdlineTests/yul_stack_opt/output index e5731f1e1..a02859f1e 100644 --- a/test/cmdlineTests/yul_stack_opt/output +++ b/test/cmdlineTests/yul_stack_opt/output @@ -5,11 +5,11 @@ Pretty printed source: object "object" { code { { - let a1, b1, c1, d1, e1, f1, g1, h1, i1, j1, k1, l1, m1, n1, o1, p1 := fun() - let a2, b2, c2, d2, e2, f2, g2, h2, i2, j2, k2, l2, m2, n2, o2, p2 := fun() - sstore(a1, a2) + let a3, b3, c3, d3, e3, f3, g3, h3, i3, j3, k3, l3, m3, o3, p3 := fun() + let a3_1, b3_1, c3_1, d3_1, e3_1, f3_1, g3_1, h3_1, i3_1, j3_1, k3_1, l3_1, m3_1, o3_1, p3_1 := fun() + sstore(a3, a3_1) } - function fun() -> a3, b3, c3, d3, e3, f3, g3, h3, i3, j3, k3, l3, m3, n3, o3, p3 + function fun() -> a3, b3, c3, d3, e3, f3, g3, h3, i3, j3, k3, l3, m3, o3, p3 { let _1 := 1 sstore(_1, _1) @@ -25,21 +25,35 @@ object "object" { sstore(11, _1) sstore(12, _1) sstore(13, _1) + a3 := _1 + b3 := _1 + c3 := _1 + d3 := _1 + e3 := _1 + f3 := _1 + g3 := _1 + h3 := _1 + i3 := _1 + j3 := _1 + k3 := _1 + l3 := _1 + m3 := _1 + o3 := _1 + p3 := _1 } } } Binary representation: -60056032565b505050505050505050505050505050601a6032565b5050505050505050505050505050508082555050609b565b60006000600060006000600060006000600060006000600060006000600060006001808155806002558060035580600455806005558060065580600755806008558060095580600a5580600b5580600c5580600d55505b909192939495969798999a9b9c9d9e9f565b +60056030565b505050505050505050505050505060196030565b5050505050505050505050505050808255505060c3565b6000600060006000600060006000600060006000600060006000600060006001808155806002558060035580600455806005558060065580600755806008558060095580600a5580600b5580600c5580600d55809f50809e50809d50809c50809b50809a50809950809850809750809650809550809450809350809250809150505b909192939495969798999a9b9c9d9e565b Text representation: - /* "yul_stack_opt/input.sol":495:500 */ + /* "yul_stack_opt/input.sol":3:573 */ tag_1 tag_2 jump // in tag_1: - /* "yul_stack_opt/input.sol":425:500 */ pop pop pop @@ -54,13 +68,10 @@ tag_1: pop pop pop - pop - /* "yul_stack_opt/input.sol":572:577 */ tag_3 tag_2 jump // in tag_3: - /* "yul_stack_opt/input.sol":502:577 */ pop pop pop @@ -75,16 +86,15 @@ tag_3: pop pop pop - pop - /* "yul_stack_opt/input.sol":590:592 */ + /* "yul_stack_opt/input.sol":740:742 */ dup1 - /* "yul_stack_opt/input.sol":586:588 */ + /* "yul_stack_opt/input.sol":736:738 */ dup3 - /* "yul_stack_opt/input.sol":579:593 */ + /* "yul_stack_opt/input.sol":729:743 */ sstore pop pop - /* "yul_stack_opt/input.sol":3:423 */ + /* "yul_stack_opt/input.sol":3:573 */ jump(tag_4) tag_2: 0x00 @@ -101,7 +111,6 @@ tag_2: 0x00 0x00 0x00 - 0x00 0x00 /* "yul_stack_opt/input.sol":98:99 */ 0x01 @@ -181,8 +190,83 @@ tag_2: 0x0d /* "yul_stack_opt/input.sol":399:420 */ sstore + /* "yul_stack_opt/input.sol":98:99 */ + dup1 + /* "yul_stack_opt/input.sol":423:430 */ + swap16 pop - /* "yul_stack_opt/input.sol":85:423 */ + /* "yul_stack_opt/input.sol":98:99 */ + dup1 + /* "yul_stack_opt/input.sol":433:440 */ + swap15 + pop + /* "yul_stack_opt/input.sol":98:99 */ + dup1 + /* "yul_stack_opt/input.sol":443:450 */ + swap14 + pop + /* "yul_stack_opt/input.sol":98:99 */ + dup1 + /* "yul_stack_opt/input.sol":453:460 */ + swap13 + pop + /* "yul_stack_opt/input.sol":98:99 */ + dup1 + /* "yul_stack_opt/input.sol":463:470 */ + swap12 + pop + /* "yul_stack_opt/input.sol":98:99 */ + dup1 + /* "yul_stack_opt/input.sol":473:480 */ + swap11 + pop + /* "yul_stack_opt/input.sol":98:99 */ + dup1 + /* "yul_stack_opt/input.sol":483:490 */ + swap10 + pop + /* "yul_stack_opt/input.sol":98:99 */ + dup1 + /* "yul_stack_opt/input.sol":493:500 */ + swap9 + pop + /* "yul_stack_opt/input.sol":98:99 */ + dup1 + /* "yul_stack_opt/input.sol":503:510 */ + swap8 + pop + /* "yul_stack_opt/input.sol":98:99 */ + dup1 + /* "yul_stack_opt/input.sol":513:520 */ + swap7 + pop + /* "yul_stack_opt/input.sol":98:99 */ + dup1 + /* "yul_stack_opt/input.sol":523:530 */ + swap6 + pop + /* "yul_stack_opt/input.sol":98:99 */ + dup1 + /* "yul_stack_opt/input.sol":533:540 */ + swap5 + pop + /* "yul_stack_opt/input.sol":98:99 */ + dup1 + /* "yul_stack_opt/input.sol":543:550 */ + swap4 + pop + /* "yul_stack_opt/input.sol":98:99 */ + dup1 + /* "yul_stack_opt/input.sol":553:560 */ + swap3 + pop + /* "yul_stack_opt/input.sol":98:99 */ + dup1 + /* "yul_stack_opt/input.sol":563:570 */ + swap2 + pop + pop + /* "yul_stack_opt/input.sol":85:573 */ tag_5: swap1 swap2 @@ -199,6 +283,5 @@ tag_5: swap13 swap14 swap15 - swap16 jump // out tag_4: diff --git a/test/libsolidity/StandardCompiler.cpp b/test/libsolidity/StandardCompiler.cpp index d71cf10f1..b02ebbe2e 100644 --- a/test/libsolidity/StandardCompiler.cpp +++ b/test/libsolidity/StandardCompiler.cpp @@ -29,6 +29,7 @@ #include <libsolutil/CommonData.h> #include <test/Metadata.h> +#include <algorithm> #include <set> using namespace std; @@ -1235,9 +1236,16 @@ BOOST_AUTO_TEST_CASE(use_stack_optimization) BOOST_REQUIRE(contract["evm"]["bytecode"]["object"].isString()); BOOST_CHECK(contract["evm"]["bytecode"]["object"].asString().length() > 20); - // Now disable stack optimizations + // Now disable stack optimizations and UnusedFunctionParameterPruner (p) // results in "stack too deep" + string optimiserSteps = OptimiserSettings::DefaultYulOptimiserSteps; + optimiserSteps.erase( + remove_if(optimiserSteps.begin(), optimiserSteps.end(), [](char ch) { return ch == 'p'; }), + optimiserSteps.end() + ); parsedInput["settings"]["optimizer"]["details"]["yulDetails"]["stackAllocation"] = false; + parsedInput["settings"]["optimizer"]["details"]["yulDetails"]["optimizerSteps"] = optimiserSteps; + result = compiler.compile(parsedInput); BOOST_REQUIRE(result["errors"].isArray()); BOOST_CHECK(result["errors"][0]["severity"] == "error"); diff --git a/test/libsolidity/gasTests/abiv2_optimised.sol b/test/libsolidity/gasTests/abiv2_optimised.sol index 74cd89a42..47eaeaceb 100644 --- a/test/libsolidity/gasTests/abiv2_optimised.sol +++ b/test/libsolidity/gasTests/abiv2_optimised.sol @@ -17,9 +17,9 @@ contract C { // optimize-yul: true // ---- // creation: -// codeDepositCost: 616600 +// codeDepositCost: 614600 // executionCost: 651 -// totalCost: 617251 +// totalCost: 615251 // external: // a(): 1029 // b(uint256): 2084 diff --git a/test/libyul/yulOptimizerTests/fullSuite/stack_compressor_msize.yul b/test/libyul/yulOptimizerTests/fullSuite/stack_compressor_msize.yul index 56a2d6cfe..b417991c2 100644 --- a/test/libyul/yulOptimizerTests/fullSuite/stack_compressor_msize.yul +++ b/test/libyul/yulOptimizerTests/fullSuite/stack_compressor_msize.yul @@ -49,7 +49,7 @@ // sstore(not(gcd(10, 15)), 1) // sstore(0, 0) // sstore(2, 1) -// pop(foo_singlereturn_1(calldataload(0), calldataload(3))) +// extcodecopy(1, msize(), 1, 1) // sstore(0, 0) // sstore(3, 1) // } @@ -59,6 +59,4 @@ // case 0 { out := _a } // default { out := gcd(_b, mod(_a, _b)) } // } -// function foo_singlereturn_1(in, in_1) -> out -// { extcodecopy(1, msize(), 1, 1) } // } diff --git a/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner.yul b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner.yul new file mode 100644 index 000000000..8fb84fb49 --- /dev/null +++ b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner.yul @@ -0,0 +1,31 @@ +{ + let x, y := foo(sload(0),sload(32)) + sstore(0, x) + sstore(0, y) + x, y := foo(sload(32), sload(8)) + + function foo(a, b) -> out1, out2 + { + out1 := mload(32) + out1 := sload(out1) + out2 := add(out1, 1) + extcodecopy(out1, out2, 1, b) + } +} +// ---- +// step: fullSuite +// +// { +// { +// let out1, out2 := foo(sload(32)) +// sstore(0, out1) +// sstore(0, out2) +// let out1_1, out2_1 := foo(sload(8)) +// } +// function foo(b) -> out1, out2 +// { +// out1 := sload(mload(32)) +// out2 := add(out1, 1) +// extcodecopy(out1, out2, 1, b) +// } +// } diff --git a/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_loop.yul b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_loop.yul new file mode 100644 index 000000000..47635e415 --- /dev/null +++ b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_loop.yul @@ -0,0 +1,33 @@ +{ + sstore(f(1), 1) + sstore(f(2), 1) + sstore(f(3), 1) + function f(a) -> x { + for {let b := 10} iszero(b) { b := sub(b, 1) } + { + a := calldataload(0) + mstore(a, x) + } + } +} +// ---- +// step: fullSuite +// +// { +// { +// f() +// sstore(0, 1) +// f() +// sstore(0, 1) +// f() +// sstore(0, 1) +// } +// function f() +// { +// let b := 10 +// let _1 := 0 +// let a := calldataload(_1) +// for { } iszero(b) { b := add(b, not(0)) } +// { mstore(a, _1) } +// } +// } diff --git a/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_recursion.yul b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_recursion.yul new file mode 100644 index 000000000..69b4517eb --- /dev/null +++ b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_recursion.yul @@ -0,0 +1,29 @@ +{ + let j, k, l := f(1, 2, 3) + sstore(0, j) + sstore(1, k) + sstore(1, l) + function f(a, b, c) -> x, y, z + { + x, y, z := f(1, 2, 3) + x := add(x, 1) + } +} +// ---- +// step: fullSuite +// +// { +// { +// let x, y, z := f() +// sstore(0, x) +// sstore(1, y) +// sstore(1, z) +// } +// function f() -> x, y, z +// { +// let x_1, y_1, z_1 := f() +// y := y_1 +// z := z_1 +// x := add(x_1, 1) +// } +// } diff --git a/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_return.yul b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_return.yul new file mode 100644 index 000000000..aed7406be --- /dev/null +++ b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_return.yul @@ -0,0 +1,34 @@ +{ + let x, y, z := foo(sload(0),sload(32)) + sstore(0, x) + sstore(0, y) + sstore(0, z) + x, y, z := foo(sload(32), sload(8)) + + // out3 is unassigned. + function foo(a, b) -> out1, out2, out3 + { + out1 := mload(32) + out1 := sload(out1) + out2 := add(out1, 1) + extcodecopy(out1, out1, 1, b) + } +} +// ---- +// step: fullSuite +// +// { +// { +// let out1, out2 := foo(sload(32)) +// sstore(0, out1) +// sstore(0, out2) +// sstore(0, 0) +// let out1_1, out2_1 := foo(sload(8)) +// } +// function foo(b) -> out1, out2 +// { +// out1 := sload(mload(32)) +// out2 := add(out1, 1) +// extcodecopy(out1, out1, 1, b) +// } +// } diff --git a/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_simple.yul b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_simple.yul new file mode 100644 index 000000000..a50257b2b --- /dev/null +++ b/test/libyul/yulOptimizerTests/fullSuite/unusedFunctionParameterPruner_simple.yul @@ -0,0 +1,36 @@ +{ + sstore(f(1), 1) + sstore(f(2), 1) + sstore(f(3), 1) + function f(a) -> x { + // The usage of a is redundant + a := calldataload(0) + mstore(a, x) + // to prevent getting fully inlined + sstore(1, 1) + sstore(2, 2) + sstore(3, 3) + sstore(3, 3) + } +} +// ---- +// step: fullSuite +// +// { +// { +// f() +// sstore(0, 1) +// f() +// sstore(0, 1) +// f() +// sstore(0, 1) +// } +// function f() +// { +// mstore(calldataload(0), 0) +// sstore(1, 1) +// sstore(2, 2) +// sstore(3, 3) +// sstore(3, 3) +// } +// } diff --git a/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/LiteralRematerialiser.yul b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/LiteralRematerialiser.yul new file mode 100644 index 000000000..e11ea2a96 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/LiteralRematerialiser.yul @@ -0,0 +1,27 @@ +{ + let a := f(mload(1)) + let b := f(a) + sstore(a, b) + function f(x) -> y + { + // Test if LiteralRematerializer can convert `y` to `0` and therefore allowing us to + // rewrite the function f + if iszero(x) { revert(y, y)} + if iszero(add(x, 1)) { revert(y, y)} + } +} +// ---- +// step: unusedFunctionParameterPruner +// +// { +// let a := f_1(mload(1)) +// let b := f_1(a) +// sstore(a, b) +// function f(x) +// { +// if iszero(x) { revert(0, 0) } +// if iszero(add(x, 1)) { revert(0, 0) } +// } +// function f_1(x_2) -> y_3 +// { f(x_2) } +// } diff --git a/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/multiple_param.yul b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/multiple_param.yul new file mode 100644 index 000000000..f2a036459 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/multiple_param.yul @@ -0,0 +1,24 @@ +{ + let d, e, i := f(1, 2, 3) + sstore(d, 0) + // b and x are unused + function f(a, b, c) -> x, y, z + { + y := mload(a) + z := mload(c) + } +} +// ---- +// step: unusedFunctionParameterPruner +// +// { +// let d, e, i := f_1(1, 2, 3) +// sstore(d, 0) +// function f(a, c) -> y, z +// { +// y := mload(a) +// z := mload(c) +// } +// function f_1(a_2, b_3, c_4) -> x_5, y_6, z_7 +// { y_6, z_7 := f(a_2, c_4) } +// } diff --git a/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/multiple_return.yul b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/multiple_return.yul new file mode 100644 index 000000000..51612a699 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/multiple_return.yul @@ -0,0 +1,24 @@ +{ + let a, b, c := f(sload(0)) + sstore(a, 0) + // d is unused, x is unassigned + function f(d) -> x, y, z + { + y := mload(1) + z := mload(2) + } +} +// ---- +// step: unusedFunctionParameterPruner +// +// { +// let a, b, c := f_1(sload(0)) +// sstore(a, 0) +// function f() -> y, z +// { +// y := mload(1) +// z := mload(2) +// } +// function f_1(d_2) -> x_3, y_4, z_5 +// { y_4, z_5 := f() } +// } diff --git a/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/nested_function.yul b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/nested_function.yul new file mode 100644 index 000000000..d09ba44a6 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/nested_function.yul @@ -0,0 +1,56 @@ +// Test case to see if the step applies for nested functions. The function `j` has an unused argument. +{ + sstore(f(1), 0) + sstore(h(1), 0) + + function f(a) -> x + { + x := g(1) + x := add(x, 1) + function g(b) -> y + { + b := add(b, 1) + y := mload(b) + } + } + + function h(c) -> u + { + u := j(c) + // d is unused + function j(d) -> w + { + w := 13 + w := add(w, 1) + } + } + +} +// ---- +// step: unusedFunctionParameterPruner +// +// { +// sstore(f_1(1), 0) +// sstore(h(1), 0) +// function g(b) -> y +// { +// b := add(b, 1) +// y := mload(b) +// } +// function f() -> x +// { +// x := g(1) +// x := add(x, 1) +// } +// function f_1(a_3) -> x_4 +// { x_4 := f() } +// function j() -> w +// { +// w := 13 +// w := add(13, 1) +// } +// function j_2(d_5) -> w_6 +// { w_6 := j() } +// function h(c) -> u +// { u := j_2(c) } +// } diff --git a/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/nested_function_name_collision.yul b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/nested_function_name_collision.yul new file mode 100644 index 000000000..8ce80f140 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/nested_function_name_collision.yul @@ -0,0 +1,52 @@ +// Test case where the name `g` occurs at two different places. Test to see if name-collision can +// cause issues. +{ + sstore(h(1), 0) + sstore(f(1), 0) + + function f(c) -> u + { + u := g(c) + function g(d) -> w + { + w := 13 + sstore(0, w) + } + } + + function h(c) -> u + { + u := g(c) + function g(d) -> w + { + w := 13 + sstore(0, w) + } + } + +} +// ---- +// step: unusedFunctionParameterPruner +// +// { +// sstore(h(1), 0) +// sstore(f(1), 0) +// function g() -> w +// { +// w := 13 +// sstore(0, 13) +// } +// function g_1(d_3) -> w_4 +// { w_4 := g() } +// function f(c) -> u +// { u := g_1(c) } +// function g_3() -> w_5 +// { +// w_5 := 13 +// sstore(0, 13) +// } +// function g_3_2(d_4_5) -> w_5_6 +// { w_5_6 := g_3() } +// function h(c_1) -> u_2 +// { u_2 := g_3_2(c_1) } +// } diff --git a/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/no_return.yul b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/no_return.yul new file mode 100644 index 000000000..1cf059da2 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/no_return.yul @@ -0,0 +1,21 @@ +{ + f(1, 2) + function f(a, b) + { + sstore(a, 0) + a := add(a, 1) + } +} +// ---- +// step: unusedFunctionParameterPruner +// +// { +// f_1(1, 2) +// function f(a) +// { +// sstore(a, 0) +// a := add(a, 1) +// } +// function f_1(a_2, b_3) +// { f(a_2) } +// } diff --git a/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/no_unused.yul b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/no_unused.yul new file mode 100644 index 000000000..ebbb70536 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/no_unused.yul @@ -0,0 +1,16 @@ +// A test where all parameters are used +{ + sstore(f(1), 0) + function f(a) -> x { x := a } + function g(b) -> y { y := g(b) } +} +// ---- +// step: unusedFunctionParameterPruner +// +// { +// sstore(f(1), 0) +// function f(a) -> x +// { x := a } +// function g(b) -> y +// { y := g(b) } +// } diff --git a/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/recursion.yul b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/recursion.yul new file mode 100644 index 000000000..4a2c5b6cc --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/recursion.yul @@ -0,0 +1,21 @@ +{ + let a1, b1, c1 := f(1, 2, 3) + function f(a, b, c) -> x, y, z + { + x, y, z := f(1, 2, 3) + x := add(x, 1) + } +} +// ---- +// step: unusedFunctionParameterPruner +// +// { +// let a1, b1, c1 := f_1(1, 2, 3) +// function f() -> x, y, z +// { +// x, y, z := f_1(1, 2, 3) +// x := add(x, 1) +// } +// function f_1(a_2, b_3, c_4) -> x_5, y_6, z_7 +// { x_5, y_6, z_7 := f() } +// } diff --git a/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/simple.yul b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/simple.yul new file mode 100644 index 000000000..873eef977 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/simple.yul @@ -0,0 +1,21 @@ +{ + sstore(f(1), 0) + function f(x) -> y + { + let w := mload(1) + y := mload(w) + } +} +// ---- +// step: unusedFunctionParameterPruner +// +// { +// sstore(f_1(1), 0) +// function f() -> y +// { +// let w := mload(1) +// y := mload(w) +// } +// function f_1(x_2) -> y_3 +// { y_3 := f() } +// } diff --git a/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/smoke.yul b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/smoke.yul new file mode 100644 index 000000000..1e0f9eb61 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/smoke.yul @@ -0,0 +1,5 @@ +{} +// ---- +// step: unusedFunctionParameterPruner +// +// { } diff --git a/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/too_many_arguments.yul b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/too_many_arguments.yul new file mode 100644 index 000000000..720489d57 --- /dev/null +++ b/test/libyul/yulOptimizerTests/unusedFunctionParameterPruner/too_many_arguments.yul @@ -0,0 +1,23 @@ +{ + let a, b := f(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) + sstore(a, b) + function f(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18) -> y, z + { + y := mload(x3) + z := mload(x7) + } +} +// ---- +// step: unusedFunctionParameterPruner +// +// { +// let a, b := f_1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18) +// sstore(a, b) +// function f(x3, x7) -> y, z +// { +// y := mload(x3) +// z := mload(x7) +// } +// function f_1(x1_2, x2_3, x3_4, x4_5, x5_6, x6_7, x7_8, x8_9, x9_10, x10_11, x11_12, x12_13, x13_14, x14_15, x15_16, x16_17, x17_18, x18_19) -> y_20, z_21 +// { y_20, z_21 := f(x3_4, x7_8) } +// }