Skip to content

Commit

Permalink
cmd/compile: use shift boundedness when lowering shifts on 386
Browse files Browse the repository at this point in the history
Minor improvements to generated code.

file                                          before   after    Δ       %
runtime.s                                     451117   450977   -140    -0.031%
compress/bzip2.s                              10202    10194    -8      -0.078%
compress/lzw.s                                5924     5904     -20     -0.338%
compress/flate.s                              45053    45032    -21     -0.047%
net.s                                         236980   236970   -10     -0.004%
vendor/golang.org/x/crypto/cryptobyte.s       29450    29439    -11     -0.037%
crypto/x509.s                                 107854   107840   -14     -0.013%
cmd/vendor/golang.org/x/arch/arm64/arm64asm.s 102448   102434   -14     -0.014%
cmd/internal/obj/arm.s                        60536    60528    -8      -0.013%
cmd/vendor/golang.org/x/mod/sumdb/tlog.s      38273    38276    +3      +0.008%
net/http.s                                    462215   462201   -14     -0.003%
cmd/compile/internal/ssa.s                    3951732  3954683  +2951   +0.075%
total                                         16946051 16948745 +2694   +0.016%

Change-Id: I9f6df1a90a295dce6fe86c8eb7576a8c96f8bb0a
Reviewed-on: https://go-review.googlesource.com/c/go/+/217000
Run-TryBot: Josh Bleecher Snyder <[email protected]>
Reviewed-by: Cherry Zhang <[email protected]>
  • Loading branch information
josharian committed Feb 21, 2020
1 parent 3330876 commit bcdd5d0
Show file tree
Hide file tree
Showing 2 changed files with 570 additions and 9 deletions.
30 changes: 21 additions & 9 deletions src/cmd/compile/internal/ssa/gen/386.rules
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,32 @@
// Lowering shifts
// Unsigned shifts need to return 0 if shift amount is >= width of shifted value.
// result = (arg << shift) & (shift >= argbits ? 0 : 0xffffffffffffffff)
(Lsh32x(32|16|8) <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMP(L|W|B)const y [32])))
(Lsh16x(32|16|8) <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMP(L|W|B)const y [32])))
(Lsh8x(32|16|8) <t> x y) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMP(L|W|B)const y [32])))
(Lsh32x(32|16|8) <t> x y) && !shiftIsBounded(v) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMP(L|W|B)const y [32])))
(Lsh16x(32|16|8) <t> x y) && !shiftIsBounded(v) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMP(L|W|B)const y [32])))
(Lsh8x(32|16|8) <t> x y) && !shiftIsBounded(v) -> (ANDL (SHLL <t> x y) (SBBLcarrymask <t> (CMP(L|W|B)const y [32])))

(Rsh32Ux(32|16|8) <t> x y) -> (ANDL (SHRL <t> x y) (SBBLcarrymask <t> (CMP(L|W|B)const y [32])))
(Rsh16Ux(32|16|8) <t> x y) -> (ANDL (SHRW <t> x y) (SBBLcarrymask <t> (CMP(L|W|B)const y [16])))
(Rsh8Ux(32|16|8) <t> x y) -> (ANDL (SHRB <t> x y) (SBBLcarrymask <t> (CMP(L|W|B)const y [8])))
(Lsh32x(32|16|8) <t> x y) && shiftIsBounded(v) -> (SHLL <t> x y)
(Lsh16x(32|16|8) <t> x y) && shiftIsBounded(v) -> (SHLL <t> x y)
(Lsh8x(32|16|8) <t> x y) && shiftIsBounded(v) -> (SHLL <t> x y)

(Rsh32Ux(32|16|8) <t> x y) && !shiftIsBounded(v) -> (ANDL (SHRL <t> x y) (SBBLcarrymask <t> (CMP(L|W|B)const y [32])))
(Rsh16Ux(32|16|8) <t> x y) && !shiftIsBounded(v) -> (ANDL (SHRW <t> x y) (SBBLcarrymask <t> (CMP(L|W|B)const y [16])))
(Rsh8Ux(32|16|8) <t> x y) && !shiftIsBounded(v) -> (ANDL (SHRB <t> x y) (SBBLcarrymask <t> (CMP(L|W|B)const y [8])))

(Rsh32Ux(32|16|8) <t> x y) && shiftIsBounded(v) -> (SHRL <t> x y)
(Rsh16Ux(32|16|8) <t> x y) && shiftIsBounded(v) -> (SHRW <t> x y)
(Rsh8Ux(32|16|8) <t> x y) && shiftIsBounded(v) -> (SHRB <t> x y)

// 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 -1 (all ones) if the shift value is >= width.

(Rsh32x(32|16|8) <t> x y) -> (SARL <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMP(L|W|B)const y [32])))))
(Rsh16x(32|16|8) <t> x y) -> (SARW <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMP(L|W|B)const y [16])))))
(Rsh8x(32|16|8) <t> x y) -> (SARB <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMP(L|W|B)const y [8])))))
(Rsh32x(32|16|8) <t> x y) && !shiftIsBounded(v) -> (SARL <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMP(L|W|B)const y [32])))))
(Rsh16x(32|16|8) <t> x y) && !shiftIsBounded(v) -> (SARW <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMP(L|W|B)const y [16])))))
(Rsh8x(32|16|8) <t> x y) && !shiftIsBounded(v) -> (SARB <t> x (ORL <y.Type> y (NOTL <y.Type> (SBBLcarrymask <y.Type> (CMP(L|W|B)const y [8])))))

(Rsh32x(32|16|8) <t> x y) && shiftIsBounded(v) -> (SARL x y)
(Rsh16x(32|16|8) <t> x y) && shiftIsBounded(v) -> (SARW x y)
(Rsh8x(32|16|8) <t> x y) && shiftIsBounded(v) -> (SARB x y)

// constant shifts
// generic opt rewrites all constant shifts to shift by Const64
Expand Down
Loading

0 comments on commit bcdd5d0

Please sign in to comment.