From 455d4f41fb1c2bf48645b4904a8ee293272c90db Mon Sep 17 00:00:00 2001 From: Jorropo Date: Tue, 7 Apr 2026 11:57:56 +0200 Subject: cmd/compile: run CondSelect into math rules on all arches Fixes #78558 I've also added tests to make sure PPC still generate ISEL when the constant isn't 1. This is to make sure we aren't generating a sequence that wouldn't work right now. But it does not mean we couldn't try to optimize other constants on PPC64 if a fast sequence exists; for example like arm64's inline register shifts. Change-Id: Ic241d593149b7a11533948f5d4c52db357cc134f Reviewed-on: https://go-review.googlesource.com/c/go/+/763340 Reviewed-by: Keith Randall Auto-Submit: Jorropo Reviewed-by: David Chase LUCI-TryBot-Result: Go LUCI Reviewed-by: Jayanth Krishnamurthy Reviewed-by: Paul Murphy --- src/cmd/compile/internal/ssa/rewrite.go | 3 +++ test/codegen/condmove.go | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/cmd/compile/internal/ssa/rewrite.go b/src/cmd/compile/internal/ssa/rewrite.go index 008767f19c..eefabfba2f 100644 --- a/src/cmd/compile/internal/ssa/rewrite.go +++ b/src/cmd/compile/internal/ssa/rewrite.go @@ -2834,6 +2834,9 @@ func rewriteCondSelectIntoMath(config *Config, op Op, constant int64) bool { return true } } + default: + // TODO: fine tune for other architectures. + return constant == 1 } return false } diff --git a/test/codegen/condmove.go b/test/codegen/condmove.go index 43ca9a961f..8527ff2896 100644 --- a/test/codegen/condmove.go +++ b/test/codegen/condmove.go @@ -481,6 +481,7 @@ func cmovmathadd2(a uint, b bool) uint { } // amd64:"LEAQ" -"CMOV" -"MUL" // arm64:"ADD R[0-9]+<<1" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } func cmovmathadd2else(a uint, b bool) uint { @@ -489,6 +490,7 @@ func cmovmathadd2else(a uint, b bool) uint { } // amd64:"LEAQ" -"CMOV" -"MUL" // arm64:"ADD R[0-9]+<<1" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } @@ -498,6 +500,7 @@ func cmovmathadd4(a uint, b bool) uint { } // amd64:"LEAQ" -"CMOV" -"MUL" // arm64:"ADD R[0-9]+<<2" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } func cmovmathadd4else(a uint, b bool) uint { @@ -506,6 +509,7 @@ func cmovmathadd4else(a uint, b bool) uint { } // amd64:"LEAQ" -"CMOV" -"MUL" // arm64:"ADD R[0-9]+<<2" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } @@ -515,6 +519,7 @@ func cmovmathadd8(a uint, b bool) uint { } // amd64:"LEAQ" -"CMOV" -"MUL" // arm64:"ADD R[0-9]+<<3" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } func cmovmathadd8else(a uint, b bool) uint { @@ -523,6 +528,7 @@ func cmovmathadd8else(a uint, b bool) uint { } // amd64:"LEAQ" -"CMOV" -"MUL" // arm64:"ADD R[0-9]+<<3" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } @@ -531,6 +537,7 @@ func cmovmathadd9223372036854775808(a uint, b bool) uint { a += 1 << 63 } // arm64:"ADD R[0-9]+<<63" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } func cmovmathadd9223372036854775808else(a uint, b bool) uint { @@ -538,6 +545,7 @@ func cmovmathadd9223372036854775808else(a uint, b bool) uint { a += 1 << 63 } // arm64:"ADD R[0-9]+<<63" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } @@ -567,6 +575,7 @@ func cmovmathsub2(a uint, b bool) uint { a -= 2 } // arm64 :"SUB R[0-9]+<<1" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } func cmovmathsub2else(a uint, b bool) uint { @@ -574,6 +583,7 @@ func cmovmathsub2else(a uint, b bool) uint { a -= 2 } // arm64 :"SUB R[0-9]+<<1" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } @@ -584,6 +594,7 @@ func cmovmathsub9223372036854775808(a uint, b bool) uint { a -= 1 << 63 } // arm64:"(SUB|ADD) R[0-9]+<<63" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } func cmovmathsub9223372036854775808else(a uint, b bool) uint { @@ -591,6 +602,7 @@ func cmovmathsub9223372036854775808else(a uint, b bool) uint { a -= 1 << 63 } // arm64:"(SUB|ADD) R[0-9]+<<63" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } @@ -685,6 +697,7 @@ func cmovmathor2(a uint, b bool) uint { a |= 2 } // arm64:"ORR R[0-9]+<<1" -"CSEL" -"MUL" + // ppc64x:"ISEL" -"MUL" return a } func cmovmathor2else(a uint, b bool) uint { @@ -692,6 +705,7 @@ func cmovmathor2else(a uint, b bool) uint { a |= 2 } // arm64:"ORR R[0-9]+<<1" -"CSEL" -"MUL" + // ppc64x:"ISEL" -"MUL" return a } @@ -700,6 +714,7 @@ func cmovmathor9223372036854775808(a uint, b bool) uint { a |= 1 << 63 } // arm64:"ORR R[0-9]+<<63" -"CSEL" -"MUL" + // ppc64x:"ISEL" -"MUL" return a } func cmovmathor9223372036854775808else(a uint, b bool) uint { @@ -707,6 +722,7 @@ func cmovmathor9223372036854775808else(a uint, b bool) uint { a |= 1 << 63 } // arm64:"ORR R[0-9]+<<63" -"CSEL" -"MUL" + // ppc64x:"ISEL" -"MUL" return a } @@ -736,6 +752,7 @@ func cmovmathxor2(a uint, b bool) uint { a ^= 2 } // arm64:"EOR R[0-9]+<<1" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } func cmovmathxor2else(a uint, b bool) uint { @@ -743,6 +760,7 @@ func cmovmathxor2else(a uint, b bool) uint { a ^= 2 } // arm64:"EOR R[0-9]+<<1" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } @@ -751,6 +769,7 @@ func cmovmathxor9223372036854775808(a uint, b bool) uint { a ^= 1 << 63 } // arm64:"EOR R[0-9]+<<63" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } func cmovmathxor9223372036854775808else(a uint, b bool) uint { @@ -758,6 +777,7 @@ func cmovmathxor9223372036854775808else(a uint, b bool) uint { a ^= 1 << 63 } // arm64:"EOR R[0-9]+<<63" -"CSEL" -"MUL" + // ppc64x: "ISEL" -"MUL" return a } -- cgit v1.3