From 72d24a7484063d1ca1113badb481f725382e39b8 Mon Sep 17 00:00:00 2001 From: Richard Musiol Date: Thu, 28 Feb 2019 15:20:59 +0100 Subject: [PATCH] cmd/compile: simplify zero ext operations on wasm On wasm every integer is stored with 64 bits. We can do zero extension by simply zeroing the upper bits. Change-Id: I02c54a38b3b2b7654fff96055edab1b92d48ff32 Reviewed-on: https://go-review.googlesource.com/c/164461 Run-TryBot: Richard Musiol TryBot-Result: Gobot Gobot Reviewed-by: Cherry Zhang --- src/cmd/compile/internal/ssa/gen/Wasm.rules | 6 +- src/cmd/compile/internal/ssa/rewriteWasm.go | 90 +++++++-------------- 2 files changed, 33 insertions(+), 63 deletions(-) diff --git a/src/cmd/compile/internal/ssa/gen/Wasm.rules b/src/cmd/compile/internal/ssa/gen/Wasm.rules index 64198839d0..41d8d1122d 100644 --- a/src/cmd/compile/internal/ssa/gen/Wasm.rules +++ b/src/cmd/compile/internal/ssa/gen/Wasm.rules @@ -59,9 +59,9 @@ (SignExt32to64 x) -> (I64ShrS (I64Shl x (I64Const [32])) (I64Const [32])) (SignExt16to(64|32) x) -> (I64ShrS (I64Shl x (I64Const [48])) (I64Const [48])) (SignExt8to(64|32|16) x) -> (I64ShrS (I64Shl x (I64Const [56])) (I64Const [56])) -(ZeroExt32to64 x) -> (I64ShrU (I64Shl x (I64Const [32])) (I64Const [32])) -(ZeroExt16to(64|32) x) -> (I64ShrU (I64Shl x (I64Const [48])) (I64Const [48])) -(ZeroExt8to(64|32|16) x) -> (I64ShrU (I64Shl x (I64Const [56])) (I64Const [56])) +(ZeroExt32to64 x) -> (I64And x (I64Const [0xffffffff])) +(ZeroExt16to(64|32) x) -> (I64And x (I64Const [0xffff])) +(ZeroExt8to(64|32|16) x) -> (I64And x (I64Const [0xff])) (Slicemask x) -> (I64ShrS (I64Sub (I64Const [0]) x) (I64Const [63])) diff --git a/src/cmd/compile/internal/ssa/rewriteWasm.go b/src/cmd/compile/internal/ssa/rewriteWasm.go index c17ed54b3c..e14d6251be 100644 --- a/src/cmd/compile/internal/ssa/rewriteWasm.go +++ b/src/cmd/compile/internal/ssa/rewriteWasm.go @@ -6386,19 +6386,14 @@ func rewriteValueWasm_OpZeroExt16to32_0(v *Value) bool { } // match: (ZeroExt16to32 x) // cond: - // result: (I64ShrU (I64Shl x (I64Const [48])) (I64Const [48])) + // result: (I64And x (I64Const [0xffff])) for { x := v.Args[0] - v.reset(OpWasmI64ShrU) - v0 := b.NewValue0(v.Pos, OpWasmI64Shl, typ.Int64) - v0.AddArg(x) - v1 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) - v1.AuxInt = 48 - v0.AddArg(v1) + v.reset(OpWasmI64And) + v.AddArg(x) + v0 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) + v0.AuxInt = 0xffff v.AddArg(v0) - v2 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) - v2.AuxInt = 48 - v.AddArg(v2) return true } } @@ -6423,19 +6418,14 @@ func rewriteValueWasm_OpZeroExt16to64_0(v *Value) bool { } // match: (ZeroExt16to64 x) // cond: - // result: (I64ShrU (I64Shl x (I64Const [48])) (I64Const [48])) + // result: (I64And x (I64Const [0xffff])) for { x := v.Args[0] - v.reset(OpWasmI64ShrU) - v0 := b.NewValue0(v.Pos, OpWasmI64Shl, typ.Int64) - v0.AddArg(x) - v1 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) - v1.AuxInt = 48 - v0.AddArg(v1) + v.reset(OpWasmI64And) + v.AddArg(x) + v0 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) + v0.AuxInt = 0xffff v.AddArg(v0) - v2 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) - v2.AuxInt = 48 - v.AddArg(v2) return true } } @@ -6460,19 +6450,14 @@ func rewriteValueWasm_OpZeroExt32to64_0(v *Value) bool { } // match: (ZeroExt32to64 x) // cond: - // result: (I64ShrU (I64Shl x (I64Const [32])) (I64Const [32])) + // result: (I64And x (I64Const [0xffffffff])) for { x := v.Args[0] - v.reset(OpWasmI64ShrU) - v0 := b.NewValue0(v.Pos, OpWasmI64Shl, typ.Int64) - v0.AddArg(x) - v1 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) - v1.AuxInt = 32 - v0.AddArg(v1) + v.reset(OpWasmI64And) + v.AddArg(x) + v0 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) + v0.AuxInt = 0xffffffff v.AddArg(v0) - v2 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) - v2.AuxInt = 32 - v.AddArg(v2) return true } } @@ -6497,19 +6482,14 @@ func rewriteValueWasm_OpZeroExt8to16_0(v *Value) bool { } // match: (ZeroExt8to16 x) // cond: - // result: (I64ShrU (I64Shl x (I64Const [56])) (I64Const [56])) + // result: (I64And x (I64Const [0xff])) for { x := v.Args[0] - v.reset(OpWasmI64ShrU) - v0 := b.NewValue0(v.Pos, OpWasmI64Shl, typ.Int64) - v0.AddArg(x) - v1 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) - v1.AuxInt = 56 - v0.AddArg(v1) + v.reset(OpWasmI64And) + v.AddArg(x) + v0 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) + v0.AuxInt = 0xff v.AddArg(v0) - v2 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) - v2.AuxInt = 56 - v.AddArg(v2) return true } } @@ -6534,19 +6514,14 @@ func rewriteValueWasm_OpZeroExt8to32_0(v *Value) bool { } // match: (ZeroExt8to32 x) // cond: - // result: (I64ShrU (I64Shl x (I64Const [56])) (I64Const [56])) + // result: (I64And x (I64Const [0xff])) for { x := v.Args[0] - v.reset(OpWasmI64ShrU) - v0 := b.NewValue0(v.Pos, OpWasmI64Shl, typ.Int64) - v0.AddArg(x) - v1 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) - v1.AuxInt = 56 - v0.AddArg(v1) + v.reset(OpWasmI64And) + v.AddArg(x) + v0 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) + v0.AuxInt = 0xff v.AddArg(v0) - v2 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) - v2.AuxInt = 56 - v.AddArg(v2) return true } } @@ -6571,19 +6546,14 @@ func rewriteValueWasm_OpZeroExt8to64_0(v *Value) bool { } // match: (ZeroExt8to64 x) // cond: - // result: (I64ShrU (I64Shl x (I64Const [56])) (I64Const [56])) + // result: (I64And x (I64Const [0xff])) for { x := v.Args[0] - v.reset(OpWasmI64ShrU) - v0 := b.NewValue0(v.Pos, OpWasmI64Shl, typ.Int64) - v0.AddArg(x) - v1 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) - v1.AuxInt = 56 - v0.AddArg(v1) + v.reset(OpWasmI64And) + v.AddArg(x) + v0 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) + v0.AuxInt = 0xff v.AddArg(v0) - v2 := b.NewValue0(v.Pos, OpWasmI64Const, typ.Int64) - v2.AuxInt = 56 - v.AddArg(v2) return true } } -- 2.50.0