Skip to content

Commit

Permalink
cmd/compile: optimize bounded shifts on wasm
Browse files Browse the repository at this point in the history
Use the shiftIsBounded function to generate more efficient
Shift instructions.

Updates golang#25167

Change-Id: Id350f8462dc3a7ed3bfed0bcbea2860b8f40048a
Reviewed-on: https://go-review.googlesource.com/c/go/+/182558
Run-TryBot: Agniva De Sarker <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Cherry Zhang <[email protected]>
Reviewed-by: Richard Musiol <[email protected]>
  • Loading branch information
agnivade committed Aug 28, 2019
1 parent b9ef4c0 commit 8fedb2d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/cmd/compile/internal/ssa/gen/Wasm.rules
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
// Lowering shifts
// Unsigned shifts need to return 0 if shift amount is >= width of shifted value.

(Lsh64x64 x y) && shiftIsBounded(v) -> (I64Shl x y)
(Lsh64x64 x y) -> (Select (I64Shl x y) (I64Const [0]) (I64LtU y (I64Const [64])))
(Lsh64x(32|16|8) x y) -> (Lsh64x64 x (ZeroExt(32|16|8)to64 y))

Expand All @@ -114,6 +115,7 @@
(Lsh8x64 x y) -> (Lsh64x64 x y)
(Lsh8x(32|16|8) x y) -> (Lsh64x64 x (ZeroExt(32|16|8)to64 y))

(Rsh64Ux64 x y) && shiftIsBounded(v) -> (I64ShrU x y)
(Rsh64Ux64 x y) -> (Select (I64ShrU x y) (I64Const [0]) (I64LtU y (I64Const [64])))
(Rsh64Ux(32|16|8) x y) -> (Rsh64Ux64 x (ZeroExt(32|16|8)to64 y))

Expand All @@ -129,6 +131,7 @@
// Signed right shift needs to return 0/-1 if shift amount is >= width of shifted value.
// We implement this by setting the shift value to (width - 1) if the shift value is >= width.

(Rsh64x64 x y) && shiftIsBounded(v) -> (I64ShrS x y)
(Rsh64x64 x y) -> (I64ShrS x (Select <typ.Int64> y (I64Const [63]) (I64LtU y (I64Const [64]))))
(Rsh64x(32|16|8) x y) -> (Rsh64x64 x (ZeroExt(32|16|8)to64 y))

Expand Down
42 changes: 42 additions & 0 deletions src/cmd/compile/internal/ssa/rewriteWasm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions test/codegen/shift.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,25 @@ func lshSignedMasked(v8 int8, v16 int16, v32 int32, v64 int64, x int) {
// bounded shifts //
// ------------------ //

func lshGuarded64(v int64, s uint) int64 {
func rshGuarded64(v int64, s uint) int64 {
if s < 64 {
// s390x:-".*AND",-".*MOVDGE"
// s390x:-".*AND",-".*MOVDGE" wasm:-"Select",-".*LtU"
return v >> s
}
panic("shift too large")
}

func rshGuarded64U(v uint64, s uint) uint64 {
if s < 64 {
// s390x:-".*AND",-".*MOVDGE"
// s390x:-".*AND",-".*MOVDGE" wasm:-"Select",-".*LtU"
return v >> s
}
panic("shift too large")
}

func rshGuarded64(v int64, s uint) int64 {
func lshGuarded64(v int64, s uint) int64 {
if s < 64 {
// s390x:-".*AND",-".*MOVDGE"
// s390x:-".*AND",-".*MOVDGE" wasm:-"Select",-".*LtU"
return v << s
}
panic("shift too large")
Expand Down

0 comments on commit 8fedb2d

Please sign in to comment.