Skip to content

Commit 84b070b

Browse files
Michael Mundaygopherbot
authored andcommitted
cmd/compile/internal/ssa: make oneBit function generic
Allows rewrite rules using oneBit to be made more compact. Change-Id: I986715f77db5b548759d809fe668e1893048f25c Reviewed-on: https://go-review.googlesource.com/c/go/+/699295 Reviewed-by: Youlin Feng <[email protected]> Commit-Queue: Keith Randall <[email protected]> Auto-Submit: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Cherry Mui <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent fe42628 commit 84b070b

File tree

3 files changed

+24
-37
lines changed

3 files changed

+24
-37
lines changed

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

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,22 +1896,10 @@
18961896
(Neq(8|16|32|64) s:(Sub(8|16|32|64) x y) (Const(8|16|32|64) [0])) && s.Uses == 1 => (Neq(8|16|32|64) x y)
18971897

18981898
// Optimize bitsets
1899-
(Eq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [y])) && oneBit8(y)
1900-
=> (Neq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [0]))
1901-
(Eq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [y])) && oneBit16(y)
1902-
=> (Neq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [0]))
1903-
(Eq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [y])) && oneBit32(y)
1904-
=> (Neq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [0]))
1905-
(Eq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [y])) && oneBit64(y)
1906-
=> (Neq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [0]))
1907-
(Neq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [y])) && oneBit8(y)
1908-
=> (Eq8 (And8 <t> x (Const8 <t> [y])) (Const8 <t> [0]))
1909-
(Neq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [y])) && oneBit16(y)
1910-
=> (Eq16 (And16 <t> x (Const16 <t> [y])) (Const16 <t> [0]))
1911-
(Neq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [y])) && oneBit32(y)
1912-
=> (Eq32 (And32 <t> x (Const32 <t> [y])) (Const32 <t> [0]))
1913-
(Neq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [y])) && oneBit64(y)
1914-
=> (Eq64 (And64 <t> x (Const64 <t> [y])) (Const64 <t> [0]))
1899+
(Eq(8|16|32|64) (And(8|16|32|64) <t> x (Const(8|16|32|64) <t> [y])) (Const(8|16|32|64) <t> [y])) && oneBit(y)
1900+
=> (Neq(8|16|32|64) (And(8|16|32|64) <t> x (Const(8|16|32|64) <t> [y])) (Const(8|16|32|64) <t> [0]))
1901+
(Neq(8|16|32|64) (And(8|16|32|64) <t> x (Const(8|16|32|64) <t> [y])) (Const(8|16|32|64) <t> [y])) && oneBit(y)
1902+
=> (Eq(8|16|32|64) (And(8|16|32|64) <t> x (Const(8|16|32|64) <t> [y])) (Const(8|16|32|64) <t> [0]))
19151903

19161904
// Reassociate expressions involving
19171905
// constants such that constants come first,

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -470,11 +470,10 @@ func ntz32(x int32) int { return bits.TrailingZeros32(uint32(x)) }
470470
func ntz16(x int16) int { return bits.TrailingZeros16(uint16(x)) }
471471
func ntz8(x int8) int { return bits.TrailingZeros8(uint8(x)) }
472472

473-
func oneBit(x int64) bool { return x&(x-1) == 0 && x != 0 }
474-
func oneBit8(x int8) bool { return x&(x-1) == 0 && x != 0 }
475-
func oneBit16(x int16) bool { return x&(x-1) == 0 && x != 0 }
476-
func oneBit32(x int32) bool { return x&(x-1) == 0 && x != 0 }
477-
func oneBit64(x int64) bool { return x&(x-1) == 0 && x != 0 }
473+
// oneBit reports whether x contains exactly one set bit.
474+
func oneBit[T int8 | int16 | int32 | int64](x T) bool {
475+
return x&(x-1) == 0 && x != 0
476+
}
478477

479478
// nto returns the number of trailing ones.
480479
func nto(x int64) int64 {

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

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)