src/cmd/compile/internal/ssa/_gen/AMD64.rules | 3 ++- src/cmd/compile/internal/ssa/rewriteAMD64.go | 6 ++++-- test/fixedbugs/issue79182.go | 32 ++++++++++++++++++++++++++++++++ diff --git a/src/cmd/compile/internal/ssa/_gen/AMD64.rules b/src/cmd/compile/internal/ssa/_gen/AMD64.rules index 956077d3924011b64df9653e508920a6dcdbf7f2..f4e18337382c4007678072ee37b1bafbd462ba4b 100644 --- a/src/cmd/compile/internal/ssa/_gen/AMD64.rules +++ b/src/cmd/compile/internal/ssa/_gen/AMD64.rules @@ -909,7 +909,8 @@ // (x + x) << 1 -> x << 2 (LEA(Q|L)2 [0] {s} (ADD(Q|L) x x) x) && s == nil => (SHL(Q|L)const [2] x) // (x + x) << 2 -> x << 3 and similar -(SHL(Q|L)const [c] (ADD(Q|L) x x)) => (SHL(Q|L)const [c+1] x) +(SHLQconst [c] (ADDQ x x)) && c < 63 => (SHLQconst [c+1] x) +(SHLLconst [c] (ADDL x x)) && c < 31 => (SHLLconst [c+1] x) // reverse ordering of compare instruction (SETL (InvertFlags x)) => (SETG x) diff --git a/src/cmd/compile/internal/ssa/rewriteAMD64.go b/src/cmd/compile/internal/ssa/rewriteAMD64.go index d005b15a5752640cd919512a742e076236be04d1..f32d46407533ff4a084e4d4abee05b5d7aa469bf 100644 --- a/src/cmd/compile/internal/ssa/rewriteAMD64.go +++ b/src/cmd/compile/internal/ssa/rewriteAMD64.go @@ -26242,6 +26242,7 @@ v.AddArg2(x, x) return true } // match: (SHLLconst [c] (ADDL x x)) + // cond: c < 31 // result: (SHLLconst [c+1] x) for { c := auxIntToInt8(v.AuxInt) @@ -26249,7 +26250,7 @@ if v_0.Op != OpAMD64ADDL { break } x := v_0.Args[1] - if x != v_0.Args[0] { + if x != v_0.Args[0] || !(c < 31) { break } v.reset(OpAMD64SHLLconst) @@ -26513,6 +26514,7 @@ v.AddArg2(x, x) return true } // match: (SHLQconst [c] (ADDQ x x)) + // cond: c < 63 // result: (SHLQconst [c+1] x) for { c := auxIntToInt8(v.AuxInt) @@ -26520,7 +26522,7 @@ if v_0.Op != OpAMD64ADDQ { break } x := v_0.Args[1] - if x != v_0.Args[0] { + if x != v_0.Args[0] || !(c < 63) { break } v.reset(OpAMD64SHLQconst) diff --git a/test/fixedbugs/issue79182.go b/test/fixedbugs/issue79182.go new file mode 100644 index 0000000000000000000000000000000000000000..9b2694a9359cc6e2aa9dee058ee2530c0e4b9cb3 --- /dev/null +++ b/test/fixedbugs/issue79182.go @@ -0,0 +1,32 @@ +// run + +// Copyright 2026 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Issue 79182: SHLQconst/SHLLconst rewrite rule for (x+x)<