diff options
author | Ben Shi <powerman1st@163.com> | 2019-08-24 12:39:26 +0000 |
---|---|---|
committer | Cherry Zhang <cherryyz@google.com> | 2019-08-30 21:20:03 +0000 |
commit | 1786ecd502364ac92493acbdee5ddca50c80afde (patch) | |
tree | 47a18a70bf0c14c4610146ce32892de45d5bef53 /src/cmd/compile/internal/wasm | |
parent | cd03fd05b55b3b22d31a56a9df388c72a96e62c2 (diff) | |
download | go-1786ecd502364ac92493acbdee5ddca50c80afde.tar.gz go-1786ecd502364ac92493acbdee5ddca50c80afde.zip |
cmd/compile: eliminate WASM's redundant extension & wrapping
This CL eliminates unnecessary pairs of I32WrapI64 and
I64ExtendI32U generated by the WASM backend for IF
statements. And it makes the total size of pkg/js_wasm/
decreases about 490KB.
Change-Id: I16b0abb686c4e30d5624323166ec2d0ec57dbe2d
Reviewed-on: https://go-review.googlesource.com/c/go/+/191758
Run-TryBot: Ben Shi <powerman1st@163.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Richard Musiol <neelance@gmail.com>
Diffstat (limited to 'src/cmd/compile/internal/wasm')
-rw-r--r-- | src/cmd/compile/internal/wasm/ssa.go | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/src/cmd/compile/internal/wasm/ssa.go b/src/cmd/compile/internal/wasm/ssa.go index 7fdd335ee9..b50f662a8b 100644 --- a/src/cmd/compile/internal/wasm/ssa.go +++ b/src/cmd/compile/internal/wasm/ssa.go @@ -201,7 +201,7 @@ func ssaGenValue(s *gc.SSAGenState, v *ssa.Value) { // Instead, we delay the generation to when the value is used and then directly generate it on the WebAssembly stack. return } - ssaGenValueOnStack(s, v) + ssaGenValueOnStack(s, v, true) if s.OnWasmStackSkipped != 0 { panic("wasm: bad stack") } @@ -209,7 +209,7 @@ func ssaGenValue(s *gc.SSAGenState, v *ssa.Value) { } } -func ssaGenValueOnStack(s *gc.SSAGenState, v *ssa.Value) { +func ssaGenValueOnStack(s *gc.SSAGenState, v *ssa.Value, extend bool) { switch v.Op { case ssa.OpWasmLoweredGetClosurePtr: getReg(s, wasm.REG_CTXT) @@ -257,8 +257,7 @@ func ssaGenValueOnStack(s *gc.SSAGenState, v *ssa.Value) { case ssa.OpWasmSelect: getValue64(s, v.Args[0]) getValue64(s, v.Args[1]) - getValue64(s, v.Args[2]) - s.Prog(wasm.AI32WrapI64) + getValue32(s, v.Args[2]) s.Prog(v.Op.Asm()) case ssa.OpWasmI64AddConst: @@ -283,13 +282,17 @@ func ssaGenValueOnStack(s *gc.SSAGenState, v *ssa.Value) { case ssa.OpWasmI64Eqz: getValue64(s, v.Args[0]) s.Prog(v.Op.Asm()) - s.Prog(wasm.AI64ExtendI32U) + if extend { + s.Prog(wasm.AI64ExtendI32U) + } case ssa.OpWasmI64Eq, ssa.OpWasmI64Ne, ssa.OpWasmI64LtS, ssa.OpWasmI64LtU, ssa.OpWasmI64GtS, ssa.OpWasmI64GtU, ssa.OpWasmI64LeS, ssa.OpWasmI64LeU, ssa.OpWasmI64GeS, ssa.OpWasmI64GeU, ssa.OpWasmF64Eq, ssa.OpWasmF64Ne, ssa.OpWasmF64Lt, ssa.OpWasmF64Gt, ssa.OpWasmF64Le, ssa.OpWasmF64Ge: getValue64(s, v.Args[0]) getValue64(s, v.Args[1]) s.Prog(v.Op.Asm()) - s.Prog(wasm.AI64ExtendI32U) + if extend { + s.Prog(wasm.AI64ExtendI32U) + } case ssa.OpWasmI64Add, ssa.OpWasmI64Sub, ssa.OpWasmI64Mul, ssa.OpWasmI64DivU, ssa.OpWasmI64RemS, ssa.OpWasmI64RemU, ssa.OpWasmI64And, ssa.OpWasmI64Or, ssa.OpWasmI64Xor, ssa.OpWasmI64Shl, ssa.OpWasmI64ShrS, ssa.OpWasmI64ShrU, ssa.OpWasmF64Add, ssa.OpWasmF64Sub, ssa.OpWasmF64Mul, ssa.OpWasmF64Div, ssa.OpWasmF64Copysign, ssa.OpWasmI64Rotl: getValue64(s, v.Args[0]) @@ -348,11 +351,22 @@ func ssaGenValueOnStack(s *gc.SSAGenState, v *ssa.Value) { } } +func isCmp(v *ssa.Value) bool { + switch v.Op { + case ssa.OpWasmI64Eqz, ssa.OpWasmI64Eq, ssa.OpWasmI64Ne, ssa.OpWasmI64LtS, ssa.OpWasmI64LtU, ssa.OpWasmI64GtS, ssa.OpWasmI64GtU, ssa.OpWasmI64LeS, ssa.OpWasmI64LeU, ssa.OpWasmI64GeS, ssa.OpWasmI64GeU, ssa.OpWasmF64Eq, ssa.OpWasmF64Ne, ssa.OpWasmF64Lt, ssa.OpWasmF64Gt, ssa.OpWasmF64Le, ssa.OpWasmF64Ge: + return true + default: + return false + } +} + func getValue32(s *gc.SSAGenState, v *ssa.Value) { if v.OnWasmStack { s.OnWasmStackSkipped-- - ssaGenValueOnStack(s, v) - s.Prog(wasm.AI32WrapI64) + ssaGenValueOnStack(s, v, false) + if !isCmp(v) { + s.Prog(wasm.AI32WrapI64) + } return } @@ -366,7 +380,7 @@ func getValue32(s *gc.SSAGenState, v *ssa.Value) { func getValue64(s *gc.SSAGenState, v *ssa.Value) { if v.OnWasmStack { s.OnWasmStackSkipped-- - ssaGenValueOnStack(s, v) + ssaGenValueOnStack(s, v, true) return } |