Skip to content

Commit 5bb59b6

Browse files
committed
Revert "compile: prefer an AND instead of SHR+SHL instructions"
This reverts commit 9ec7074. Reason for revert: broke s390x (copysign, abs) and arm64 (bitfield) tests. Change-Id: I16c1b389c062e8c4aa5de079f1d46c9b25b0db52 Reviewed-on: https://go-review.googlesource.com/c/go/+/193850 Run-TryBot: Martin Möhrmann <[email protected]> Reviewed-by: Agniva De Sarker <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 9ec7074 commit 5bb59b6

File tree

4 files changed

+123
-8
lines changed

4 files changed

+123
-8
lines changed

src/cmd/compile/internal/ssa/gen/ARM64.rules

+3-2
Original file line numberDiff line numberDiff line change
@@ -1863,8 +1863,9 @@
18631863
(XORshiftLL <t> [c] (UBFX [bfc] x) x2) && c < 32 && t.Size() == 4 && bfc == armBFAuxInt(32-c, c)
18641864
-> (EXTRWconst [32-c] x2 x)
18651865

1866-
// Rewrite special pairs of shifts to AND.
1867-
// On ARM64 the bitmask can fit into an instruction.
1866+
// Generic rules rewrite certain AND to a pair of shifts.
1867+
// However, on ARM64 the bitmask can fit into an instruction.
1868+
// Rewrite it back to AND.
18681869
(SRLconst [c] (SLLconst [c] x)) && 0 < c && c < 64 -> (ANDconst [1<<uint(64-c)-1] x) // mask out high bits
18691870
(SLLconst [c] (SRLconst [c] x)) && 0 < c && c < 64 -> (ANDconst [^(1<<uint(c)-1)] x) // mask out low bits
18701871

src/cmd/compile/internal/ssa/gen/generic.rules

+8
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,14 @@
542542
(Slicemask (Const64 [x])) && x > 0 -> (Const64 [-1])
543543
(Slicemask (Const64 [0])) -> (Const64 [0])
544544

545+
// Rewrite AND of consts as shifts if possible, slightly faster for 64 bit operands
546+
// leading zeros can be shifted left, then right
547+
(And64 <t> (Const64 [y]) x) && nlz(y) + nto(y) == 64 && nto(y) >= 32
548+
-> (Rsh64Ux64 (Lsh64x64 <t> x (Const64 <t> [nlz(y)])) (Const64 <t> [nlz(y)]))
549+
// trailing zeros can be shifted right, then left
550+
(And64 <t> (Const64 [y]) x) && nlo(y) + ntz(y) == 64 && ntz(y) >= 32
551+
-> (Lsh64x64 (Rsh64Ux64 <t> x (Const64 <t> [ntz(y)])) (Const64 <t> [ntz(y)]))
552+
545553
// simplifications often used for lengths. e.g. len(s[i:i+5])==5
546554
(Sub(64|32|16|8) (Add(64|32|16|8) x y) x) -> y
547555
(Sub(64|32|16|8) (Add(64|32|16|8) x y) y) -> x

src/cmd/compile/internal/ssa/rewritegeneric.go

+110-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/codegen/math.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func abs32(x float32) float32 {
8181

8282
// Check that it's using integer registers
8383
func copysign(a, b, c float64) {
84-
// amd64:"BTRQ\t[$]63","ANDQ","ORQ"
84+
// amd64:"BTRQ\t[$]63","SHRQ\t[$]63","SHLQ\t[$]63","ORQ"
8585
// s390x:"CPSDR",-"MOVD" (no integer load/store)
8686
// ppc64:"FCPSGN"
8787
// ppc64le:"FCPSGN"
@@ -100,7 +100,7 @@ func copysign(a, b, c float64) {
100100
// s390x:"LNDFR\t",-"MOVD\t" (no integer load/store)
101101
sink64[2] = math.Float64frombits(math.Float64bits(a) | 1<<63)
102102

103-
// amd64:"ANDQ","ORQ"
103+
// amd64:-"SHLQ\t[$]1",-"SHRQ\t[$]1","SHRQ\t[$]63","SHLQ\t[$]63","ORQ"
104104
// s390x:"CPSDR\t",-"MOVD\t" (no integer load/store)
105105
// ppc64:"FCPSGN"
106106
// ppc64le:"FCPSGN"

0 commit comments

Comments
 (0)