Skip to content

Commit

Permalink
Inline tiny functions first.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Sep 10, 2020
1 parent f102fea commit 42c26e0
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 315 deletions.
12 changes: 10 additions & 2 deletions libyul/optimiser/FullInliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ using namespace solidity::yul;

void FullInliner::run(OptimiserStepContext& _context, Block& _ast)
{
FullInliner{_ast, _context.dispenser, _context.dialect}.run();
FullInliner inliner{_ast, _context.dispenser, _context.dialect};
inliner.run(Pass::InlineTiny);
inliner.run(Pass::InlineRest);
}

FullInliner::FullInliner(Block& _ast, NameDispenser& _dispenser, Dialect const& _dialect):
Expand Down Expand Up @@ -73,8 +75,10 @@ FullInliner::FullInliner(Block& _ast, NameDispenser& _dispenser, Dialect const&
}
}

void FullInliner::run()
void FullInliner::run(Pass _pass)
{
m_pass = _pass;

// Note that the order of inlining can result in very different code.
// Since AST IDs and thus function names depend on whether or not a contract
// is compiled together with other source files, a change in AST IDs
Expand Down Expand Up @@ -171,6 +175,10 @@ bool FullInliner::shallInline(FunctionCall const& _funCall, YulString _callSite)
if (size <= 1)
return true;

// In the first pass, only inline tiny functions.
if (m_pass == Pass::InlineTiny)
return false;

// Do not inline into already big functions.
if (m_functionSizes.at(_callSite) > 45)
return false;
Expand Down
5 changes: 4 additions & 1 deletion libyul/optimiser/FullInliner.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,10 @@ class FullInliner: public ASTModifier
void tentativelyUpdateCodeSize(YulString _function, YulString _callSite);

private:
enum Pass { InlineTiny, InlineRest };

FullInliner(Block& _ast, NameDispenser& _dispenser, Dialect const& _dialect);
void run();
void run(Pass _pass);

/// @returns a map containing the maximum depths of a call chain starting at each
/// function. For recursive functions, the value is one larger than for all others.
Expand All @@ -102,6 +104,7 @@ class FullInliner: public ASTModifier
void handleBlock(YulString _currentFunctionName, Block& _block);
bool recursive(FunctionDefinition const& _fun) const;

Pass m_pass;
/// The AST to be modified. The root block itself will not be modified, because
/// we store pointers to functions.
Block& m_ast;
Expand Down
113 changes: 30 additions & 83 deletions test/cmdlineTests/evm_to_wasm_break/output
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,13 +43,10 @@ object "object" {
x_7 := x_11
}
{
let _4, _5, _6, _7 := lt_172_762(x_4, x_5, x_6, x_7, _1, _1, _1, 10)
let _8, _9, _10, _11 := iszero_170_760(_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_171_761(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_171_761(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)
}
Expand All @@ -69,32 +67,27 @@ object "object" {
let r1_1, carry_2 := add_carry(x1, y1, carry_1)
r1 := r1_1
}
function iszero_170_760(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_171_761(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4
function eq(x1, x2, x3, x4, y1, y2, y3, y4) -> r4
{
let r4_1 := 0
if i64.eq(x1, y1)
{
if i64.eq(x2, y2)
{
if i64.eq(x3, y3)
{
if i64.eq(x4, y4) { r4_1 := 1 }
}
if i64.eq(x3, y3) { if i64.eq(x4, y4) { r4 := 1 } }
}
}
r4 := r4_1
}
function cmp(a, b) -> r:i32
{
switch i64.lt_u(a, b)
case 1:i32 { r := 0xffffffff:i32 }
default { r := i64.ne(a, b) }
}
function lt_172_762(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)
Expand Down Expand Up @@ -159,7 +152,7 @@ object "object" {


Binary representation:
0061736d0100000001480a60000060017e017e60027e7e017f60037e7e7e017e60047e7e7e7e017e60087e7e7e7e7e7e7e7e0060087e7e7e7e7e7e7e7e017e60057f7e7e7e7e0060027f7f0060037f7f7f0002310208657468657265756d0c73746f7261676553746f7265000808657468657265756d0c63616c6c44617461436f70790009030e0d0003060406020604010101070505030100010610037e0142000b7e0142000b7e0142000b071102066d656d6f72790200046d61696e00020acb080dde02030a7e017f147e02404200210002402000200020002000100921012300210223012103230221040b20012105200221062003210720042108420121092000200084200020098484504545210a02400340200a45450d01024002402005200620072008200020002000420a1008210b2300210c2301210d2302210e0b0240200b200c200d200e1005210f2300211023012111230221120b200f201084201120128484504504400c030b024020052006200720082000200020004202100621132300211423012115230221160b2013201484201520168484504504400c030b0240200520062007200820002000200042041006211723002118230121192302211a0b20172018842019201a8484504504400c010b0b0240200520062007200820002000200020091004211b2300211c2301211d2302211e0b201b2105201c2106201d2107201e21080c000b0b20002000200020002005200620072008100e0b0b2901037e0240200020017c2105200520027c21032005200054200320055472ad21040b2004240020030b6c010b7e0240200320077c210c200c42007c210b024020022006200c200354200b200c5472ad1003210d2300210e0b200d210a024020012005200e1003210f230021100b200f2109024020002004201010032111230021120b201121080b20092400200a2401200b240220080b2401047e0240200020018420022003848450ad21070b20052400200624012007240220040b4101057e02404200210c200020045104402001200551044020022006510440200320075104404201210c0b0b0b0b200c210b0b20092400200a2401200b240220080b2701027f024002402000200154210320034101460440417f210205200020015221020b0b0b20020b960102047e047f02404100210c0240200020041007210d200d41004604400240200120051007210e200e41004604400240200220061007210f200f41004604402003200754210c05200f41014604404100210c054101210c0b0b0b05200e41014604404100210c054101210c0b0b0b05200d41014604404100210c054101210c0b0b0b200cad210b0b20092400200a2401200b240220080b7601087e024042002000200184200284520440000b42002003422088520440000b41002003a7412010014100290000100c2108410041086a290000100c2109410041106a290000100c210a410041186a290000100c210b2008210420092105200a2106200b21070b20052400200624012007240220040b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100a421086210220022000421088100a8421010b20010b1e01027e02402000100b422086210220022000422088100b8421010b20010b3200024020002001100c370000200041086a2002100c370000200041106a2003100c370000200041186a2004100c3700000b0b2300024041002000200120022003100d41202004200520062007100d4100412010000b0b
0061736d0100000001480a60000060017e017e60027e7e017f60037e7e7e017e60047e7e7e7e017e60087e7e7e7e7e7e7e7e0060087e7e7e7e7e7e7e7e017e60057f7e7e7e7e0060027f7f0060037f7f7f0002310208657468657265756d0c73746f7261676553746f7265000808657468657265756d0c63616c6c44617461436f70790009030e0d0003060406020604010101070505030100010610037e0142000b7e0142000b7e0142000b071102066d656d6f72790200046d61696e00020af0070da302030b7e017f087e02404200210002402000200020002000100921012300210223012103230221040b20012105200221062003210720042108420121092000200084210a200a200020098484504545210b02400340200b45450d01024002402000200020002005200620072008200020002000420a10081005210c2300210d2301210e2302210f0b200c200d84200e200f8484504504400c030b200a20002005200620072008200020002000420210068484504504400c030b200a20002005200620072008200020002000420410068484504504400c010b0b024020052006200720082000200020002009100421102300211123012112230221130b201021052011210620122107201321080c000b0b20002000200020002005200620072008100e0b0b2901037e0240200020017c2105200520027c21032005200054200320055472ad21040b2004240020030b6c010b7e0240200320077c210c200c42007c210b024020022006200c200354200b200c5472ad1003210d2300210e0b200d210a024020012005200e1003210f230021100b200f2109024020002004201010032111230021120b201121080b20092400200a2401200b240220080b2401047e0240200020018420022003848450ad21070b20052400200624012007240220040b2d01017e024020002004510440200120055104402002200651044020032007510440420121080b0b0b0b0b20080b2701027f024002402000200154210320034101460440417f210205200020015221020b0b0b20020b8a0102017e047f0240410021090240200020041007210a200a41004604400240200120051007210b200b41004604400240200220061007210c200c41004604402003200754210905200c41014604404100210905410121090b0b0b05200b41014604404100210905410121090b0b0b05200a41014604404100210905410121090b0b0b2009ad21080b20080b7601087e024042002000200184200284520440000b42002003422088520440000b41002003a7412010014100290000100c2108410041086a290000100c2109410041106a290000100c210a410041186a290000100c210b2008210420092105200a2106200b21070b20052400200624012007240220040b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100a421086210220022000421088100a8421010b20010b1e01027e02402000100b422086210220022000422088100b8421010b20010b3200024020002001100c370000200041086a2002100c370000200041106a2003100c370000200041186a2004100c3700000b0b2300024041002000200120022003100d41202004200520062007100d4100412010000b0b

Text representation:
(module
Expand All @@ -182,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)
Expand All @@ -217,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_172_762 (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_170_760 (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_171_761 (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_171_761 (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)
))

Expand Down Expand Up @@ -348,7 +310,7 @@ Text representation:
(local.get $r1)
)

(func $iszero_170_760
(func $iszero_170_789
(param $x1 i64)
(param $x2 i64)
(param $x3 i64)
Expand All @@ -368,7 +330,7 @@ Text representation:
(local.get $r1)
)

(func $eq_171_761
(func $eq
(param $x1 i64)
(param $x2 i64)
(param $x3 i64)
Expand All @@ -378,29 +340,20 @@ Text representation:
(param $y3 i64)
(param $y4 i64)
(result i64)
(local $r1 i64)
(local $r2 i64)
(local $r3 i64)
(local $r4 i64)
(local $r4_1 i64)
(block $label__9
(local.set $r4_1 (i64.const 0))
(if (i64.eq (local.get $x1) (local.get $y1)) (then
(if (i64.eq (local.get $x2) (local.get $y2)) (then
(if (i64.eq (local.get $x3) (local.get $y3)) (then
(if (i64.eq (local.get $x4) (local.get $y4)) (then
(local.set $r4_1 (i64.const 1))
(local.set $r4 (i64.const 1))
))
))
))
))
(local.set $r4 (local.get $r4_1))

)
(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
Expand All @@ -424,7 +377,7 @@ Text representation:
(local.get $r)
)

(func $lt_172_762
(func $lt_172
(param $x1 i64)
(param $x2 i64)
(param $x3 i64)
Expand All @@ -434,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)
Expand Down Expand Up @@ -484,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
Expand Down
Loading

0 comments on commit 42c26e0

Please sign in to comment.