Commit 74a35ac7e0 for golang

commit 74a35ac7e001db57003d231e84b90fed4c80752e
Author: Michael Haddon <michael@politepixels.io>
Date:   Thu Nov 26 04:03:52 2026 +0000

    cmd/compile: fix constant folding of narrow right shifts on 386

    The 386 rules fold an SARBconst or SARWconst of a MOVLconst by
    shifting the whole 32-bit constant, but the bits above a byte or a
    16-bit value are junk. A byte loaded from a constant string can be
    held as MOVLconst [254], so int8(s[0]) >> 7 was 1 instead of -1 for
    s[0] == 0xfe.

    Sign-extend the constant from the width of the shift first, as
    CL 93716 did for amd64.

    Add a test.

    Fixes #83406

    Change-Id: I2ea8ec7ffbdf9c1032e083623c0cece9dd30630c
    GitHub-Last-Rev: 99f2f318dca21524a1f88e5e256ee755b11dd823
    GitHub-Pull-Request: golang/go#83407
    Reviewed-on: https://go-review.googlesource.com/c/go/+/866042
    LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
    Reviewed-by: Keith Randall <khr@golang.org>
    Auto-Submit: Keith Randall <khr@golang.org>
    Reviewed-by: Robert Griesemer <gri@google.com>
    Reviewed-by: Keith Randall <khr@google.com>

diff --git a/src/cmd/compile/internal/ssa/_gen/386.rules b/src/cmd/compile/internal/ssa/_gen/386.rules
index 8a0637b01a..a9c98fbef5 100644
--- a/src/cmd/compile/internal/ssa/_gen/386.rules
+++ b/src/cmd/compile/internal/ssa/_gen/386.rules
@@ -895,8 +895,8 @@
 (ADDLconst [c] (MOVLconst [d])) => (MOVLconst [c+d])
 (ADDLconst [c] (ADDLconst [d] x)) => (ADDLconst [c+d] x)
 (SARLconst [c] (MOVLconst [d])) => (MOVLconst [d>>uint64(c)])
-(SARWconst [c] (MOVLconst [d])) => (MOVLconst [d>>uint64(c)])
-(SARBconst [c] (MOVLconst [d])) => (MOVLconst [d>>uint64(c)])
+(SARWconst [c] (MOVLconst [d])) => (MOVLconst [int32(int16(d))>>uint64(c)])
+(SARBconst [c] (MOVLconst [d])) => (MOVLconst [int32(int8(d))>>uint64(c)])
 (NEGL (MOVLconst [c])) => (MOVLconst [-c])
 (MULLconst [c] (MOVLconst [d])) => (MOVLconst [c*d])
 (ANDLconst [c] (MOVLconst [d])) => (MOVLconst [c&d])
diff --git a/src/cmd/compile/internal/ssa/rewrite/i386/rewrite386.go b/src/cmd/compile/internal/ssa/rewrite/i386/rewrite386.go
index e408f0f3a6..61b72e715e 100644
--- a/src/cmd/compile/internal/ssa/rewrite/i386/rewrite386.go
+++ b/src/cmd/compile/internal/ssa/rewrite/i386/rewrite386.go
@@ -6219,7 +6219,7 @@ func rewriteValue_Op386SARBconst(v *ssa.Value) bool {
 		return true
 	}
 	// match: (SARBconst [c] (MOVLconst [d]))
-	// result: (MOVLconst [d>>uint64(c)])
+	// result: (MOVLconst [int32(int8(d))>>uint64(c)])
 	for {
 		c := ssa.AuxIntToInt8(v.AuxInt)
 		if v_0.Op != ssaop.Op386MOVLconst {
@@ -6227,7 +6227,7 @@ func rewriteValue_Op386SARBconst(v *ssa.Value) bool {
 		}
 		d := ssa.AuxIntToInt32(v_0.AuxInt)
 		v.Reset(ssaop.Op386MOVLconst)
-		v.AuxInt = ssa.Int32ToAuxInt(d >> uint64(c))
+		v.AuxInt = ssa.Int32ToAuxInt(int32(int8(d)) >> uint64(c))
 		return true
 	}
 	return false
@@ -6319,7 +6319,7 @@ func rewriteValue_Op386SARWconst(v *ssa.Value) bool {
 		return true
 	}
 	// match: (SARWconst [c] (MOVLconst [d]))
-	// result: (MOVLconst [d>>uint64(c)])
+	// result: (MOVLconst [int32(int16(d))>>uint64(c)])
 	for {
 		c := ssa.AuxIntToInt16(v.AuxInt)
 		if v_0.Op != ssaop.Op386MOVLconst {
@@ -6327,7 +6327,7 @@ func rewriteValue_Op386SARWconst(v *ssa.Value) bool {
 		}
 		d := ssa.AuxIntToInt32(v_0.AuxInt)
 		v.Reset(ssaop.Op386MOVLconst)
-		v.AuxInt = ssa.Int32ToAuxInt(d >> uint64(c))
+		v.AuxInt = ssa.Int32ToAuxInt(int32(int16(d)) >> uint64(c))
 		return true
 	}
 	return false
diff --git a/test/fixedbugs/issue83406.go b/test/fixedbugs/issue83406.go
new file mode 100644
index 0000000000..6de59266ff
--- /dev/null
+++ b/test/fixedbugs/issue83406.go
@@ -0,0 +1,28 @@
+// 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
+
+//go:noinline
+func sarb() int8 {
+	s := "\xfe"
+	return int8(s[0]) >> 7
+}
+
+//go:noinline
+func sarw() int16 {
+	s := "\xfe\xff"
+	return int16(uint16(s[0])|uint16(s[1])<<8) >> 15
+}
+
+func main() {
+	if got := sarb(); got != -1 {
+		panic(got)
+	}
+	if got := sarw(); got != -1 {
+		panic(got)
+	}
+}