Commit 01079ab4a2 for golang
commit 01079ab4a2af37e2c653f2bbec6de8f9204a3bd1
Author: Michael Haddon <michael@politepixels.io>
Date: Mon Nov 30 18:34:18 2026 +0000
cmd/compile: guard riscv64 and loong64 shift rules for a shift of 0
The 32-bit shifts on riscv64 and loong64 sign extend their result, so
a 32-bit shift by 0 is a sign extension, not a no-op. Four rules
assume a non-zero shift. (SRLI [x] (MOVWUreg y)) => (SRLIW [x] y)
turns a zero extension into a sign extension when x is 0, and
(MOVWUreg x:(SRLIW [c] y)) => x drops a zero extension that is still
needed when c is 0. loong64 has the same two rules for SRLconst.
A shift count that only becomes 0 after the generic rules have run,
such as one that only sccp proves to be 0, reaches these rules. On
riscv64, uint64(uint32(x)) >> s and uint64(uint32(x) >> (s & 31)) then
keep the sign of bit 31 in the upper half.
Require a non-zero shift in all four rules. On loong64 the SRLVconst
rule is currently shadowed by an earlier BSTRPICKV rule, but guard it
anyway so that it does not depend on rule order.
The amd64 part of the issue is #36897, which CL 862781 fixes.
Fixes #83404
Change-Id: I1a9a59e554baa964f47fe4bfe491bf80c1e19598
GitHub-Last-Rev: dd812d3d229e4354faff096b69f3302a3c224377
GitHub-Pull-Request: golang/go#83517
Reviewed-on: https://go-review.googlesource.com/c/go/+/868185
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
diff --git a/src/cmd/compile/internal/ssa/_gen/LOONG64.rules b/src/cmd/compile/internal/ssa/_gen/LOONG64.rules
index aa93227d9b..435f5c949c 100644
--- a/src/cmd/compile/internal/ssa/_gen/LOONG64.rules
+++ b/src/cmd/compile/internal/ssa/_gen/LOONG64.rules
@@ -677,7 +677,7 @@
(SRAV x (ANDconst [63] y)) => (SRAV x y)
// Avoid unnecessary zero and sign extension when right shifting.
-(SRLVconst [rc] (MOVWUreg y)) && rc >= 0 && rc <= 31 => (SRLconst [int64(rc)] y)
+(SRLVconst [rc] (MOVWUreg y)) && rc > 0 && rc <= 31 => (SRLconst [int64(rc)] y)
(SRAVconst [rc] (MOVWreg y)) && rc >= 0 && rc <= 31 => (SRAconst [int64(rc)] y)
// Replace right shifts that exceed size of signed type.
@@ -813,7 +813,7 @@
// Avoid extending when already sufficiently shifted.
(MOVBUreg x:(SRLconst [c] y)) && c >= 24 => x
(MOVHUreg x:(SRLconst [c] y)) && c >= 16 => x
-(MOVWUreg x:(SRLconst [c] y)) => x
+(MOVWUreg x:(SRLconst [c] y)) && c > 0 => x
// Avoid extending when already sufficiently masked.
(MOVBreg x:(ANDconst [c] y)) && c >= 0 && int64(int8(c)) == c => x
diff --git a/src/cmd/compile/internal/ssa/_gen/RISCV64.rules b/src/cmd/compile/internal/ssa/_gen/RISCV64.rules
index c0f91f1e88..3476b969c4 100644
--- a/src/cmd/compile/internal/ssa/_gen/RISCV64.rules
+++ b/src/cmd/compile/internal/ssa/_gen/RISCV64.rules
@@ -609,7 +609,7 @@
(MOVWUreg x:(SRLI [c] y)) && c >= 32 => x
(MOVBUreg x:(SRLIW [c] y)) && c >= 24 => x
(MOVHUreg x:(SRLIW [c] y)) && c >= 16 => x
-(MOVWUreg x:(SRLIW [c] y)) => x
+(MOVWUreg x:(SRLIW [c] y)) && c > 0 => x
// Avoid sign/zero extension for consts.
(MOVBreg (MOVDconst [c])) => (MOVDconst [int64(int8(c))])
@@ -710,7 +710,7 @@
// Avoid unnecessary zero and sign extension when right shifting.
(SRAI [x] (MOVWreg y)) && x >= 0 && x <= 31 => (SRAIW [x] y)
-(SRLI [x] (MOVWUreg y)) && x >= 0 && x <= 31 => (SRLIW [x] y)
+(SRLI [x] (MOVWUreg y)) && x > 0 && x <= 31 => (SRLIW [x] y)
// Replace right shifts that exceed size of signed type.
(SRAI <t> [x] (MOVBreg y)) && x >= 8 => (SRAI [63] (SLLI <typ.Int64> [56] y))
diff --git a/src/cmd/compile/internal/ssa/rewrite/loong64/rewriteLOONG64.go b/src/cmd/compile/internal/ssa/rewrite/loong64/rewriteLOONG64.go
index b08d57d777..341f40cb31 100644
--- a/src/cmd/compile/internal/ssa/rewrite/loong64/rewriteLOONG64.go
+++ b/src/cmd/compile/internal/ssa/rewrite/loong64/rewriteLOONG64.go
@@ -5295,12 +5295,17 @@ func rewriteValue_OpLOONG64MOVWUreg(v *ssa.Value) bool {
return true
}
// match: (MOVWUreg x:(SRLconst [c] y))
+ // cond: c > 0
// result: x
for {
x := v_0
if x.Op != ssaop.OpLOONG64SRLconst {
break
}
+ c := ssa.AuxIntToInt64(x.AuxInt)
+ if !(c > 0) {
+ break
+ }
v.CopyOf(x)
return true
}
@@ -7338,7 +7343,7 @@ func rewriteValue_OpLOONG64SRLVconst(v *ssa.Value) bool {
return true
}
// match: (SRLVconst [rc] (MOVWUreg y))
- // cond: rc >= 0 && rc <= 31
+ // cond: rc > 0 && rc <= 31
// result: (SRLconst [int64(rc)] y)
for {
rc := ssa.AuxIntToInt64(v.AuxInt)
@@ -7346,7 +7351,7 @@ func rewriteValue_OpLOONG64SRLVconst(v *ssa.Value) bool {
break
}
y := v_0.Args[0]
- if !(rc >= 0 && rc <= 31) {
+ if !(rc > 0 && rc <= 31) {
break
}
v.Reset(ssaop.OpLOONG64SRLconst)
diff --git a/src/cmd/compile/internal/ssa/rewrite/riscv64/rewriteRISCV64.go b/src/cmd/compile/internal/ssa/rewrite/riscv64/rewriteRISCV64.go
index efca45c4b8..d8badeb6e7 100644
--- a/src/cmd/compile/internal/ssa/rewrite/riscv64/rewriteRISCV64.go
+++ b/src/cmd/compile/internal/ssa/rewrite/riscv64/rewriteRISCV64.go
@@ -6465,12 +6465,17 @@ func rewriteValue_OpRISCV64MOVWUreg(v *ssa.Value) bool {
return true
}
// match: (MOVWUreg x:(SRLIW [c] y))
+ // cond: c > 0
// result: x
for {
x := v_0
if x.Op != ssaop.OpRISCV64SRLIW {
break
}
+ c := ssa.AuxIntToInt64(x.AuxInt)
+ if !(c > 0) {
+ break
+ }
v.CopyOf(x)
return true
}
@@ -9148,7 +9153,7 @@ func rewriteValue_OpRISCV64SRL(v *ssa.Value) bool {
func rewriteValue_OpRISCV64SRLI(v *ssa.Value) bool {
v_0 := v.Args[0]
// match: (SRLI [x] (MOVWUreg y))
- // cond: x >= 0 && x <= 31
+ // cond: x > 0 && x <= 31
// result: (SRLIW [x] y)
for {
x := ssa.AuxIntToInt64(v.AuxInt)
@@ -9156,7 +9161,7 @@ func rewriteValue_OpRISCV64SRLI(v *ssa.Value) bool {
break
}
y := v_0.Args[0]
- if !(x >= 0 && x <= 31) {
+ if !(x > 0 && x <= 31) {
break
}
v.Reset(ssaop.OpRISCV64SRLIW)
diff --git a/test/fixedbugs/issue83404.go b/test/fixedbugs/issue83404.go
new file mode 100644
index 0000000000..821ae1d886
--- /dev/null
+++ b/test/fixedbugs/issue83404.go
@@ -0,0 +1,41 @@
+// 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.
+
+package main
+
+import "fmt"
+
+// zero returns 0, but only sccp can prove it, so a shift by zero(n)
+// reaches lowering as a shift by 0.
+func zero(n int) uint {
+ s := uint(0)
+ for i := 0; i < n; i++ {
+ s *= 3
+ }
+ return s
+}
+
+//go:noinline
+func shr64(x uint64, n int) uint64 { return uint64(uint32(x)) >> zero(n) }
+
+//go:noinline
+func shr64Masked(x uint64, n int) uint64 { return uint64(uint32(x)) >> (zero(n) & 31) }
+
+//go:noinline
+func shrMasked(x uint64, n int) uint64 { return uint64(uint32(x) >> (zero(n) & 31)) }
+
+func main() {
+ const x, want uint64 = 0x1234567880000001, 0x80000001
+ if got := shr64(x, 3); got != want {
+ panic(fmt.Sprintf("shr64: got %#x, want %#x", got, want))
+ }
+ if got := shr64Masked(x, 3); got != want {
+ panic(fmt.Sprintf("shr64Masked: got %#x, want %#x", got, want))
+ }
+ if got := shrMasked(x, 3); got != want {
+ panic(fmt.Sprintf("shrMasked: got %#x, want %#x", got, want))
+ }
+}