[ewasm] Use select instruction where appropriate

This commit is contained in:
Alex Beregszaszi 2020-11-27 22:26:51 +00:00
parent 9640cfd77e
commit a7ee1355bd
2 changed files with 42 additions and 70 deletions

View File

@ -51,11 +51,7 @@ function eq(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4 {
// returns 0 if a == b, -1 if a < b and 1 if a > b
function cmp(a, b) -> r:i32 {
switch i64.lt_u(a, b)
case 1:i32 { r := 0xffffffff:i32 }
default {
r := i64.ne(a, b)
}
r := i32.select(0xffffffff:i32, i64.ne(a, b), i64.lt_u(a, b))
}
function lt_320x320_64(x1, x2, x3, x4, x5, y1, y2, y3, y4, y5) -> z:i32 {

View File

@ -42,11 +42,11 @@ object "object" {
x_7 := x_11
}
{
let _4, _5, _6, _7 := iszero_200_868_1493(_1, _1, _1, lt_202(x_4, x_5, x_6, x_7, _1, _1, _1, 10))
let _4, _5, _6, _7 := iszero_200_872_1499(_1, _1, _1, lt_202(x_4, x_5, x_6, x_7, _1, _1, _1, 10))
if i32.eqz(i64.eqz(i64.or(i64.or(_4, _5), i64.or(_6, _7)))) { break }
let _8, _9, _10, _11 := eq_201_869_1494(x_4, x_5, x_6, x_7, _1, _1, _1, 2)
let _8, _9, _10, _11 := eq_201_873_1500(x_4, x_5, x_6, x_7, _1, _1, _1, 2)
if i32.eqz(i64.eqz(i64.or(i64.or(_8, _9), i64.or(_10, _11)))) { break }
let _12, _13, _14, _15 := eq_201_869_1494(x_4, x_5, x_6, x_7, _1, _1, _1, 4)
let _12, _13, _14, _15 := eq_201_873_1500(x_4, x_5, x_6, x_7, _1, _1, _1, 4)
if i32.eqz(i64.eqz(i64.or(i64.or(_12, _13), i64.or(_14, _15)))) { continue }
}
sstore(_1, _1, _1, _1, x_4, x_5, x_6, x_7)
@ -68,28 +68,23 @@ object "object" {
let r1_1, carry_2 := add_carry(x1, y1, carry_1)
r1 := r1_1
}
function iszero_200_868_1493(x1, x2, x3, x4) -> r1, r2, r3, r4
function iszero_200_872_1499(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_201_869_1494(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4
function eq_201_873_1500(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4
{
r4 := i64.extend_i32_u(i32.and(i64.eq(x1, y1), i32.and(i64.eq(x2, y2), i32.and(i64.eq(x3, y3), i64.eq(x4, y4)))))
}
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_202(x1, x2, x3, x4, y1, y2, y3, y4) -> z4
{
let z:i32 := false
switch cmp(x1, y1)
let _1:i32 := 0xffffffff:i32
switch i32.select(_1, i64.ne(x1, y1), i64.lt_u(x1, y1))
case 0:i32 {
switch cmp(x2, y2)
switch i32.select(_1, i64.ne(x2, y2), i64.lt_u(x2, y2))
case 0:i32 {
switch cmp(x3, y3)
switch i32.select(_1, i64.ne(x3, y3), i64.lt_u(x3, y3))
case 0:i32 { z := i64.lt_u(x4, y4) }
case 1:i32 { z := 0:i32 }
default { z := 1:i32 }
@ -173,7 +168,7 @@ object "object" {
Binary representation:
0061736d0100000001540c6000006000017f60017e017e60027e7e017f60037e7e7e017e60047e7e7e7e017e60047e7e7e7e017f60087e7e7e7e7e7e7e7e0060087e7e7e7e7e7e7e7e017e60057f7e7e7e7e0060027f7f0060037f7f7f00025e0408657468657265756d0c73746f7261676553746f7265000a08657468657265756d06726576657274000a08657468657265756d0f67657443616c6c4461746153697a65000108657468657265756d0c63616c6c44617461436f7079000b030f0e000408050803080602020205090705030100010610037e0142000b7e0142000b7e0142000b071102066d656d6f72790200046d61696e00040acb090ecb02030a7e017f107e02404200210002402000200020002000100f21012300210223012103230221040b20012105200221062003210720042108420121092000200084200020098484504545210a02400340200a45450d01024002402000200020002005200620072008200020002000420a100a1007210b2300210c2301210d2302210e0b200b200c84200d200e8484504504400c030b0240200520062007200820002000200042021008210f2300211023012111230221120b200f201084201120128484504504400c030b024020052006200720082000200020004204100821132300211423012115230221160b2013201484201520168484504504400c010b0b0240200520062007200820002000200020091006211723002118230121192302211a0b201721052018210620192107201a21080c000b0b2000200020002000200520062007200810110b0b2901037e0240200020017c2105200520027c21032005200054200320055472ad21040b2004240020030b6c010b7e0240200320077c210c200c42007c210b024020022006200c200354200b200c5472ad1005210d2300210e0b200d210a024020012005200e1005210f230021100b200f2109024020002004201010052111230021120b201121080b20092400200a2401200b240220080b2401047e0240200020018420022003848450ad21070b20052400200624012007240220040b2f01047e02402000200451200120055120022006512003200751717171ad210b0b20092400200a2401200b240220080b2701027f024002402000200154210320034101460440417f210205200020015221020b0b0b20020b8a0102017e047f0240410021090240200020041009210a200a41004604400240200120051009210b200b41004604400240200220061009210c200c41004604402003200754210905200c41014604404100210905410121090b0b0b05200b41014604404100210905410121090b0b0b05200a41014604404100210905410121090b0b0b2009ad21080b20080b2901017f024042002000200184200284520440000b42002003422088520440000b2003a721040b20040b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100c421086210220022000421088100c8421010b20010b1e01027e02402000100d422086210220022000422088100d8421010b20010bfc0105047e017f017e087f047e024010022108420021092009200920092009100b210a2000200120022003100b210b2009200920094220100b210c200b417f200c6b4b04404100410010010b2008200b6b210d200b20084b04404100210d0b4100210e200d200e4b0440200a200b200d10030b200c200d4b0440200c200d6b210f200a200d6a2110200e2111024003402011200f49450d010240201020116a200e3a00000b201141016a21110c000b0b0b200e290000100e2112200e41086a290000100e2113200e41106a290000100e2114200e41186a290000100e2115201221042013210520142106201521070b20052400200624012007240220040b3200024020002001100e370000200041086a2002100e370000200041106a2003100e370000200041186a2004100e3700000b0b230002404100200020012002200310104120200420052006200710104100412010000b0b
0061736d01000000014e0b6000006000017f60017e017e60037e7e7e017e60047e7e7e7e017e60047e7e7e7e017f60087e7e7e7e7e7e7e7e0060087e7e7e7e7e7e7e7e017e60057f7e7e7e7e0060027f7f0060037f7f7f00025e0408657468657265756d0c73746f7261676553746f7265000908657468657265756d06726576657274000908657468657265756d0f67657443616c6c4461746153697a65000108657468657265756d0c63616c6c44617461436f7079000a030e0d0003070407070502020204080605030100010610037e0142000b7e0142000b7e0142000b071102066d656d6f72790200046d61696e00040abc090dcb02030a7e017f107e02404200210002402000200020002000100e21012300210223012103230221040b20012105200221062003210720042108420121092000200084200020098484504545210a02400340200a45450d01024002402000200020002005200620072008200020002000420a10091007210b2300210c2301210d2302210e0b200b200c84200d200e8484504504400c030b0240200520062007200820002000200042021008210f2300211023012111230221120b200f201084201120128484504504400c030b024020052006200720082000200020004204100821132300211423012115230221160b2013201484201520168484504504400c010b0b0240200520062007200820002000200020091006211723002118230121192302211a0b201721052018210620192107201a21080c000b0b2000200020002000200520062007200810100b0b2901037e0240200020017c2105200520027c21032005200054200320055472ad21040b2004240020030b6c010b7e0240200320077c210c200c42007c210b024020022006200c200354200b200c5472ad1005210d2300210e0b200d210a024020012005200e1005210f230021100b200f2109024020002004201010052111230021120b201121080b20092400200a2401200b240220080b2401047e0240200020018420022003848450ad21070b20052400200624012007240220040b2f01047e02402000200451200120055120022006512003200751717171ad210b0b20092400200a2401200b240220080ba30102017e057f024041002109417f210a0240200a200020045220002004541b210b200b41004604400240200a200120055220012005541b210c200c41004604400240200a200220065220022006541b210d200d41004604402003200754210905200d41014604404100210905410121090b0b0b05200c41014604404100210905410121090b0b0b05200b41014604404100210905410121090b0b0b2009ad21080b20080b2901017f024042002000200184200284520440000b42002003422088520440000b2003a721040b20040b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100b421086210220022000421088100b8421010b20010b1e01027e02402000100c422086210220022000422088100c8421010b20010bfc0105047e017f017e087f047e024010022108420021092009200920092009100a210a2000200120022003100a210b2009200920094220100a210c200b417f200c6b4b04404100410010010b2008200b6b210d200b20084b04404100210d0b4100210e200d200e4b0440200a200b200d10030b200c200d4b0440200c200d6b210f200a200d6a2110200e2111024003402011200f49450d010240201020116a200e3a00000b201141016a21110c000b0b0b200e290000100d2112200e41086a290000100d2113200e41106a290000100d2114200e41186a290000100d2115201221042013210520142106201521070b20052400200624012007240220040b3200024020002001100d370000200041086a2002100d370000200041106a2003100d370000200041186a2004100d3700000b0b2300024041002000200120022003100f41202004200520062007100f4100412010000b0b
Text representation:
(module
@ -235,7 +230,7 @@ Text representation:
(br_if $label__3 (i32.eqz (i32.eqz (local.get $_3))))
(block $label__4
(block
(local.set $_4 (call $iszero_200_868_1493 (local.get $_1) (local.get $_1) (local.get $_1) (call $lt_202 (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 $_4 (call $iszero_200_872_1499 (local.get $_1) (local.get $_1) (local.get $_1) (call $lt_202 (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))
@ -245,7 +240,7 @@ Text representation:
(br $label__3)
))
(block
(local.set $_8 (call $eq_201_869_1494 (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 $_8 (call $eq_201_873_1500 (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 $_9 (global.get $global_))
(local.set $_10 (global.get $global__1))
(local.set $_11 (global.get $global__2))
@ -255,7 +250,7 @@ Text representation:
(br $label__3)
))
(block
(local.set $_12 (call $eq_201_869_1494 (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 $_12 (call $eq_201_873_1500 (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 $_13 (global.get $global_))
(local.set $_14 (global.get $global__1))
(local.set $_15 (global.get $global__2))
@ -353,7 +348,7 @@ Text representation:
(local.get $r1)
)
(func $iszero_200_868_1493
(func $iszero_200_872_1499
(param $x1 i64)
(param $x2 i64)
(param $x3 i64)
@ -373,7 +368,7 @@ Text representation:
(local.get $r1)
)
(func $eq_201_869_1494
(func $eq_201_873_1500
(param $x1 i64)
(param $x2 i64)
(param $x3 i64)
@ -397,27 +392,6 @@ Text representation:
(local.get $r1)
)
(func $cmp
(param $a i64)
(param $b i64)
(result i32)
(local $r i32)
(local $condition i32)
(block $label__10
(block
(local.set $condition (i64.lt_u (local.get $a) (local.get $b)))
(if (i32.eq (local.get $condition) (i32.const 1)) (then
(local.set $r (i32.const 4294967295))
)(else
(local.set $r (i64.ne (local.get $a) (local.get $b)))
))
)
)
(local.get $r)
)
(func $lt_202
(param $x1 i64)
(param $x2 i64)
@ -430,23 +404,25 @@ Text representation:
(result i64)
(local $z4 i64)
(local $z i32)
(local $_1 i32)
(local $condition i32)
(local $condition_11 i32)
(local $condition_12 i32)
(local $condition_13 i32)
(local $condition_14 i32)
(block $label__11
(block $label__10
(local.set $z (i32.const 0))
(local.set $_1 (i32.const 4294967295))
(block
(local.set $condition_12 (call $cmp (local.get $x1) (local.get $y1)))
(if (i32.eq (local.get $condition_12) (i32.const 0)) (then
(local.set $condition (i32.select (local.get $_1) (i64.ne (local.get $x1) (local.get $y1)) (i64.lt_u (local.get $x1) (local.get $y1))))
(if (i32.eq (local.get $condition) (i32.const 0)) (then
(block
(local.set $condition_13 (call $cmp (local.get $x2) (local.get $y2)))
(if (i32.eq (local.get $condition_13) (i32.const 0)) (then
(local.set $condition_11 (i32.select (local.get $_1) (i64.ne (local.get $x2) (local.get $y2)) (i64.lt_u (local.get $x2) (local.get $y2))))
(if (i32.eq (local.get $condition_11) (i32.const 0)) (then
(block
(local.set $condition_14 (call $cmp (local.get $x3) (local.get $y3)))
(if (i32.eq (local.get $condition_14) (i32.const 0)) (then
(local.set $condition_12 (i32.select (local.get $_1) (i64.ne (local.get $x3) (local.get $y3)) (i64.lt_u (local.get $x3) (local.get $y3))))
(if (i32.eq (local.get $condition_12) (i32.const 0)) (then
(local.set $z (i64.lt_u (local.get $x4) (local.get $y4)))
)(else
(if (i32.eq (local.get $condition_14) (i32.const 1)) (then
(if (i32.eq (local.get $condition_12) (i32.const 1)) (then
(local.set $z (i32.const 0))
)(else
(local.set $z (i32.const 1))
@ -455,7 +431,7 @@ Text representation:
)
)(else
(if (i32.eq (local.get $condition_13) (i32.const 1)) (then
(if (i32.eq (local.get $condition_11) (i32.const 1)) (then
(local.set $z (i32.const 0))
)(else
(local.set $z (i32.const 1))
@ -464,7 +440,7 @@ Text representation:
)
)(else
(if (i32.eq (local.get $condition_12) (i32.const 1)) (then
(if (i32.eq (local.get $condition) (i32.const 1)) (then
(local.set $z (i32.const 0))
)(else
(local.set $z (i32.const 1))
@ -485,7 +461,7 @@ Text representation:
(param $x4 i64)
(result i32)
(local $v i32)
(block $label__15
(block $label__13
(if (i64.ne (i64.const 0) (i64.or (i64.or (local.get $x1) (local.get $x2)) (local.get $x3))) (then
(unreachable)))
(if (i64.ne (i64.const 0) (i64.shr_u (local.get $x4) (i64.const 32))) (then
@ -500,7 +476,7 @@ Text representation:
(param $x i64)
(result i64)
(local $y i64)
(block $label__16
(block $label__14
(local.set $y (i64.or (i64.and (i64.shl (local.get $x) (i64.const 8)) (i64.const 65280)) (i64.and (i64.shr_u (local.get $x) (i64.const 8)) (i64.const 255))))
)
@ -512,7 +488,7 @@ Text representation:
(result i64)
(local $y i64)
(local $hi i64)
(block $label__17
(block $label__15
(local.set $hi (i64.shl (call $bswap16 (local.get $x)) (i64.const 16)))
(local.set $y (i64.or (local.get $hi) (call $bswap16 (i64.shr_u (local.get $x) (i64.const 16)))))
@ -525,7 +501,7 @@ Text representation:
(result i64)
(local $y i64)
(local $hi i64)
(block $label__18
(block $label__16
(local.set $hi (i64.shl (call $bswap32 (local.get $x)) (i64.const 32)))
(local.set $y (i64.or (local.get $hi) (call $bswap32 (i64.shr_u (local.get $x) (i64.const 32)))))
@ -557,7 +533,7 @@ Text representation:
(local $z2_1 i64)
(local $z3_1 i64)
(local $z4_1 i64)
(block $label__19
(block $label__17
(local.set $cds (call $eth.getCallDataSize))
(local.set $_1 (i64.const 0))
(local.set $destination (call $u256_to_i32 (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_1)))
@ -576,14 +552,14 @@ Text representation:
(local.set $_3 (i32.sub (local.get $requested_size) (local.get $available_size)))
(local.set $_4 (i32.add (local.get $destination) (local.get $available_size)))
(local.set $i (local.get $_2))
(block $label__20
(loop $label__22
(br_if $label__20 (i32.eqz (i32.lt_u (local.get $i) (local.get $_3))))
(block $label__21
(block $label__18
(loop $label__20
(br_if $label__18 (i32.eqz (i32.lt_u (local.get $i) (local.get $_3))))
(block $label__19
(i32.store8 (i32.add (local.get $_4) (local.get $i)) (local.get $_2))
)
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br $label__22)
(br $label__20)
)
)
@ -610,7 +586,7 @@ Text representation:
(param $y2 i64)
(param $y3 i64)
(param $y4 i64)
(block $label__23
(block $label__21
(i64.store (local.get $pos) (call $bswap64 (local.get $y1)))
(i64.store (i32.add (local.get $pos) (i32.const 8)) (call $bswap64 (local.get $y2)))
(i64.store (i32.add (local.get $pos) (i32.const 16)) (call $bswap64 (local.get $y3)))
@ -627,7 +603,7 @@ Text representation:
(param $y2 i64)
(param $y3 i64)
(param $y4 i64)
(block $label__24
(block $label__22
(call $mstore_internal (i32.const 0) (local.get $x1) (local.get $x2) (local.get $x3) (local.get $x4))
(call $mstore_internal (i32.const 32) (local.get $y1) (local.get $y2) (local.get $y3) (local.get $y4))
(call $eth.storageStore (i32.const 0) (i32.const 32))