From ce05ad448fe6ea3b9b33c0eab1143dcb40e3bbc3 Mon Sep 17 00:00:00 2001 From: Jorropo Date: Thu, 3 Jul 2025 02:57:25 +0200 Subject: 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 Reviewed-by: Keith Randall Reviewed-by: Michael Knyszek Reviewed-by: Keith Randall --- test/codegen/condmove.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'test/codegen') diff --git a/test/codegen/condmove.go b/test/codegen/condmove.go index 95a9d2cd23..5659972eed 100644 --- a/test/codegen/condmove.go +++ b/test/codegen/condmove.go @@ -473,3 +473,36 @@ func cmovmathsub(a uint, b bool) uint { // wasm:"Sub", "-Select" return a } + +func cmovmathdouble(a uint, b bool) uint { + if b { + a *= 2 + } + // amd64:"SHL", -"CMOV" + // amd64/v3:"SHL", -"CMOV", -"MOV" + // arm64:"LSL", -"CSEL" + // wasm:"Shl", "-Select" + return a +} + +func cmovmathhalvei(a int, b bool) int { + if b { + // For some reason on arm64 it attributes the ASR to inside this block rather than where the Phi node is. + // arm64:"ASR", -"CSEL" + a /= 2 + } + // arm64:-"CSEL" + // wasm:"Shr", "-Select" + return a +} + +func cmovmathhalveu(a uint, b bool) uint { + if b { + a /= 2 + } + // amd64:"SHR", -"CMOV" + // amd64/v3:"SHR", -"CMOV", -"MOV" + // arm64:"LSR", -"CSEL" + // wasm:"Shr", "-Select" + return a +} -- cgit v1.3-5-g9baa