Skip to content

Commit 00db0cb

Browse files
committed
cmd/compile: add minor bit twiddling optimizations
Noticed while adding to the bitset implementation in cmd/compile/internal/gc. The (Com (Const)) optimizations were already present in the AMD64 lowered optimizations. They trigger 118, 44, 262, and 108 times respectively for int sizes 8, 16, 32, and 64 in a run of make.bash. The (Or (And)) optimization is new. It triggers 3 times for int size 8 and once for int size 64 during make.bash, in packages internal/poll, reflect, encoding/asn1, and go/types, so there is a bit of natural test coverage. Change-Id: I44072864ff88831d5ec7dce37c516d29df056e98 Reviewed-on: https://go-review.googlesource.com/41758 Run-TryBot: Josh Bleecher Snyder <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent c095e92 commit 00db0cb

File tree

2 files changed

+572
-4
lines changed

2 files changed

+572
-4
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,10 @@
586586
(Com16 (Com16 x)) -> x
587587
(Com32 (Com32 x)) -> x
588588
(Com64 (Com64 x)) -> x
589+
(Com8 (Const8 [c])) -> (Const8 [^c])
590+
(Com16 (Const16 [c])) -> (Const16 [^c])
591+
(Com32 (Const32 [c])) -> (Const32 [^c])
592+
(Com64 (Const64 [c])) -> (Const64 [^c])
589593
(Neg8 (Sub8 x y)) -> (Sub8 y x)
590594
(Neg16 (Sub16 x y)) -> (Sub16 y x)
591595
(Neg32 (Sub32 x y)) -> (Sub32 y x)
@@ -608,6 +612,18 @@
608612
(Xor16 x (Xor16 x y)) -> y
609613
(Xor8 x (Xor8 x y)) -> y
610614

615+
// Ands clear bits. Ors set bits.
616+
// If a subsequent Or will set all the bits
617+
// that an And cleared, we can skip the And.
618+
// This happens in bitmasking code like:
619+
// x &^= 3 << shift // clear two old bits
620+
// x |= v << shift // set two new bits
621+
// when shift is a small constant and v ends up a constant 3.
622+
(Or8 (And8 x (Const8 [c2])) (Const8 <t> [c1])) && ^(c1 | c2) == 0 -> (Or8 (Const8 <t> [c1]) x)
623+
(Or16 (And16 x (Const16 [c2])) (Const16 <t> [c1])) && ^(c1 | c2) == 0 -> (Or16 (Const16 <t> [c1]) x)
624+
(Or32 (And32 x (Const32 [c2])) (Const32 <t> [c1])) && ^(c1 | c2) == 0 -> (Or32 (Const32 <t> [c1]) x)
625+
(Or64 (And64 x (Const64 [c2])) (Const64 <t> [c1])) && ^(c1 | c2) == 0 -> (Or64 (Const64 <t> [c1]) x)
626+
611627
(Trunc64to8 (And64 (Const64 [y]) x)) && y&0xFF == 0xFF -> (Trunc64to8 x)
612628
(Trunc64to16 (And64 (Const64 [y]) x)) && y&0xFFFF == 0xFFFF -> (Trunc64to16 x)
613629
(Trunc64to32 (And64 (Const64 [y]) x)) && y&0xFFFFFFFF == 0xFFFFFFFF -> (Trunc64to32 x)

0 commit comments

Comments
 (0)