Skip to content

Commit ce05ad4

Browse files
committed
cmd/compile: rewrite condselects into doublings and halvings
For performance see CL 685676. This allows something like: if y { x *= 2 } To be compiled to: SHLXQ BX, AX, AX Instead of: MOVQ AX, CX SHLQ $1, CX MOVBLZX BL, DX TESTQ DX, DX CMOVQNE CX, AX While ./make.bash uniqued per LOC, there is 2 doublings and 4 halvings. Change-Id: Ic0727cbf429528a2dbf17cbfc3b0121db8387444 Reviewed-on: https://go-review.googlesource.com/c/go/+/685695 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent fcd2807 commit ce05ad4

File tree

3 files changed

+280
-0
lines changed

3 files changed

+280
-0
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,3 +2855,10 @@
28552855
// if b { x-- } => x -= b
28562856
(CondSelect (Add8 <t> x (Const8 [-1])) x bool) => (Sub8 x (CvtBoolToUint8 <t> bool))
28572857
(CondSelect (Add(64|32|16) <t> x (Const(64|32|16) [-1])) x bool) => (Sub(64|32|16) x (ZeroExt8to(64|32|16) <t> (CvtBoolToUint8 <types.Types[types.TUINT8]> bool)))
2858+
2859+
// if b { x <<= 1 } => x <<= b
2860+
(CondSelect (Lsh(64|32|16|8)x64 x (Const64 [1])) x bool) => (Lsh(64|32|16|8)x8 [true] x (CvtBoolToUint8 <types.Types[types.TUINT8]> bool))
2861+
2862+
// if b { x >>= 1 } => x >>= b
2863+
(CondSelect (Rsh(64|32|16|8)x64 x (Const64 [1])) x bool) => (Rsh(64|32|16|8)x8 [true] x (CvtBoolToUint8 <types.Types[types.TUINT8]> bool))
2864+
(CondSelect (Rsh(64|32|16|8)Ux64 x (Const64 [1])) x bool) => (Rsh(64|32|16|8)Ux8 [true] x (CvtBoolToUint8 <types.Types[types.TUINT8]> bool))

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

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

test/codegen/condmove.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,3 +473,36 @@ func cmovmathsub(a uint, b bool) uint {
473473
// wasm:"Sub", "-Select"
474474
return a
475475
}
476+
477+
func cmovmathdouble(a uint, b bool) uint {
478+
if b {
479+
a *= 2
480+
}
481+
// amd64:"SHL", -"CMOV"
482+
// amd64/v3:"SHL", -"CMOV", -"MOV"
483+
// arm64:"LSL", -"CSEL"
484+
// wasm:"Shl", "-Select"
485+
return a
486+
}
487+
488+
func cmovmathhalvei(a int, b bool) int {
489+
if b {
490+
// For some reason on arm64 it attributes the ASR to inside this block rather than where the Phi node is.
491+
// arm64:"ASR", -"CSEL"
492+
a /= 2
493+
}
494+
// arm64:-"CSEL"
495+
// wasm:"Shr", "-Select"
496+
return a
497+
}
498+
499+
func cmovmathhalveu(a uint, b bool) uint {
500+
if b {
501+
a /= 2
502+
}
503+
// amd64:"SHR", -"CMOV"
504+
// amd64/v3:"SHR", -"CMOV", -"MOV"
505+
// arm64:"LSR", -"CSEL"
506+
// wasm:"Shr", "-Select"
507+
return a
508+
}

0 commit comments

Comments
 (0)