]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: optimize BSRQ with nonzero input
authorKeith Randall <khr@golang.org>
Sat, 26 Mar 2022 17:03:06 +0000 (10:03 -0700)
committerKeith Randall <khr@golang.org>
Sun, 27 Mar 2022 17:02:31 +0000 (17:02 +0000)
Same as BSFQ, if the input is known to be nonzero we don't need a CMOV
to correct for the weird all-zero behavior of the BSRQ instruction.

Fixes #51964

Change-Id: Ic1d14fdf3e7e698ff2f47efecfd761d8a036012a
Reviewed-on: https://go-review.googlesource.com/c/go/+/396034
Trust: Keith Randall <khr@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
src/cmd/compile/internal/ssa/gen/AMD64.rules
src/cmd/compile/internal/ssa/rewriteAMD64.go

index 47a6af003c9ecd9b6d9f376d608e646cdc8d0296..87fe0fbee13d1a6b8c6c88a21227171d2dff5351 100644 (file)
        (CMPXCHGLlock [off1+off2] {sym} ptr old new_ mem)
 
 // We don't need the conditional move if we know the arg of BSF is not zero.
-(CMOVQEQ x _ (Select1 (BSFQ (ORQconst [c] _)))) && c != 0 => x
+(CMOVQEQ x _ (Select1 (BS(F|R)Q (ORQconst [c] _)))) && c != 0 => x
 // Extension is unnecessary for trailing zeros.
 (BSFQ (ORQconst <t> [1<<8] (MOVBQZX x))) => (BSFQ (ORQconst <t> [1<<8] x))
 (BSFQ (ORQconst <t> [1<<16] (MOVWQZX x))) => (BSFQ (ORQconst <t> [1<<16] x))
index 0c789d6b496477c66c4213b220d1cd9d4a2e6708..c17d8b03e20dd169318668d4a0ff9dd691c97868 100644 (file)
@@ -5191,6 +5191,29 @@ func rewriteValueAMD64_OpAMD64CMOVQEQ(v *Value) bool {
                v.copyOf(x)
                return true
        }
+       // match: (CMOVQEQ x _ (Select1 (BSRQ (ORQconst [c] _))))
+       // cond: c != 0
+       // result: x
+       for {
+               x := v_0
+               if v_2.Op != OpSelect1 {
+                       break
+               }
+               v_2_0 := v_2.Args[0]
+               if v_2_0.Op != OpAMD64BSRQ {
+                       break
+               }
+               v_2_0_0 := v_2_0.Args[0]
+               if v_2_0_0.Op != OpAMD64ORQconst {
+                       break
+               }
+               c := auxIntToInt32(v_2_0_0.AuxInt)
+               if !(c != 0) {
+                       break
+               }
+               v.copyOf(x)
+               return true
+       }
        return false
 }
 func rewriteValueAMD64_OpAMD64CMOVQGE(v *Value) bool {