Commit bf7de32863 for golang

commit bf7de328632880fdaaf611188aaaafb87b93ba99
Author: Keith Randall <khr@golang.org>
Date:   Wed Nov 11 17:29:53 2026 -0800

    cmd/compile: stop dropping zero-extension behavior on 32-bit amd64 ops

    Most of the amd64 32-bit operations zero the upper 32 bits of the
    64-bit destination. So an operation like x+0 on 32 bits is not a
    no-op, if all 64 bits of the destination are being used.

    In the test file, only "and" currently fails. That's because we have
    the rule

        (MOVLQZX (ANDLconst [c] x)) => (ANDLconst [c] x)

    This rule is correct, but it is needed to cause the real incorrect
    rules to trigger in the test.  Adding similar rules for the other
    operations causes the corresponding tests to fail.

    Fixes #36897

    Change-Id: I2f50d222ab441b1e9303455f3522c1c6b3ecb13c
    Reviewed-on: https://go-review.googlesource.com/c/go/+/862781
    Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
    Reviewed-by: Hongxiang Jiang <hxjiang@golang.org>
    LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
    Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
    Auto-Submit: Keith Randall <khr@golang.org>

diff --git a/src/cmd/compile/internal/ssa/_gen/AMD64.rules b/src/cmd/compile/internal/ssa/_gen/AMD64.rules
index 40f229fb93..cf1a52e2e1 100644
--- a/src/cmd/compile/internal/ssa/_gen/AMD64.rules
+++ b/src/cmd/compile/internal/ssa/_gen/AMD64.rules
@@ -1336,13 +1336,25 @@
 (SETAEstore [off] {sym} ptr (FlagGT_UGT) mem) => (MOVBstore [off] {sym} ptr (MOVLconst <typ.UInt8> [1]) mem)

 // Remove redundant *const ops
-(ADD(Q|L)const [0] x) => x
-(SUB(Q|L)const [0] x) => x
+// Note L versions zero-extend, so if we ask for all 8 bytes of the result
+// we still need the zero extension.
+(ADDQconst [0] x) => x
+(ADDLconst [0] x) && v.Type.Size() <= 4 => x
+(ADDLconst [0] x) && v.Type.Size() > 4 => (MOVLQZX x)
+(SUBQconst [0] x) => x
+(SUBLconst [0] x) && v.Type.Size() <= 4 => x
+(SUBLconst [0] x) && v.Type.Size() > 4 => (MOVLQZX x)
 (AND(Q|L)const [0] _) => (MOVLconst [0])
-(AND(Q|L)const [-1] x) => x
-(OR(Q|L)const [0] x) => x
+(ANDQconst [-1] x) => x
+(ANDLconst [-1] x) && v.Type.Size() <= 4 => x
+(ANDLconst [-1] x) && v.Type.Size() > 4 => (MOVLQZX x)
+(ORQconst [0] x) => x
+(ORLconst [0] x) && v.Type.Size() <= 4 => x
+(ORLconst [0] x) && v.Type.Size() > 4 => (MOVLQZX x)
 (OR(Q|L)const [-1] _) => (MOV(Q|L)const [-1])
-(XOR(Q|L)const [0] x) => x
+(XORQconst [0] x) => x
+(XORLconst [0] x) && v.Type.Size() <= 4 => x
+(XORLconst [0] x) && v.Type.Size() > 4 => (MOVLQZX x)
 // TODO: since we got rid of the W/B versions, we might miss
 // things like (ANDLconst [0x100] x) which were formerly
 // (ANDBconst [0] x).  Probably doesn't happen very often.
@@ -1351,7 +1363,9 @@

 // Remove redundant ops
 // Not in generic rules, because they may appear after lowering e. g. Slicemask
-(NEG(Q|L) (NEG(Q|L) x)) => x
+(NEGQ (NEGQ x)) => x
+(NEGL (NEGL x)) && v.Type.Size() <= 4 => x
+(NEGL (NEGL x)) && v.Type.Size() > 4 => (MOVLQZX x)
 (NEG(Q|L) s:(SUB(Q|L) x y)) && s.Uses == 1 => (SUB(Q|L) y x)

 // Convert constant subtracts to constant adds
diff --git a/src/cmd/compile/internal/ssa/rewrite/amd64/rewriteAMD64.go b/src/cmd/compile/internal/ssa/rewrite/amd64/rewriteAMD64.go
index a90d8c625d..2cf4e230d5 100644
--- a/src/cmd/compile/internal/ssa/rewrite/amd64/rewriteAMD64.go
+++ b/src/cmd/compile/internal/ssa/rewrite/amd64/rewriteAMD64.go
@@ -8417,15 +8417,34 @@ func rewriteValue_OpAMD64ADDLconst(v *ssa.Value) bool {
 		return true
 	}
 	// match: (ADDLconst [0] x)
+	// cond: v.Type.Size() <= 4
 	// result: x
 	for {
 		if ssa.AuxIntToInt32(v.AuxInt) != 0 {
 			break
 		}
 		x := v_0
+		if !(v.Type.Size() <= 4) {
+			break
+		}
 		v.CopyOf(x)
 		return true
 	}
+	// match: (ADDLconst [0] x)
+	// cond: v.Type.Size() > 4
+	// result: (MOVLQZX x)
+	for {
+		if ssa.AuxIntToInt32(v.AuxInt) != 0 {
+			break
+		}
+		x := v_0
+		if !(v.Type.Size() > 4) {
+			break
+		}
+		v.Reset(ssaop.OpAMD64MOVLQZX)
+		v.AddArg(x)
+		return true
+	}
 	// match: (ADDLconst [c] (MOVLconst [d]))
 	// result: (MOVLconst [c+d])
 	for {
@@ -9904,15 +9923,34 @@ func rewriteValue_OpAMD64ANDLconst(v *ssa.Value) bool {
 		return true
 	}
 	// match: (ANDLconst [-1] x)
+	// cond: v.Type.Size() <= 4
 	// result: x
 	for {
 		if ssa.AuxIntToInt32(v.AuxInt) != -1 {
 			break
 		}
 		x := v_0
+		if !(v.Type.Size() <= 4) {
+			break
+		}
 		v.CopyOf(x)
 		return true
 	}
+	// match: (ANDLconst [-1] x)
+	// cond: v.Type.Size() > 4
+	// result: (MOVLQZX x)
+	for {
+		if ssa.AuxIntToInt32(v.AuxInt) != -1 {
+			break
+		}
+		x := v_0
+		if !(v.Type.Size() > 4) {
+			break
+		}
+		v.Reset(ssaop.OpAMD64MOVLQZX)
+		v.AddArg(x)
+		return true
+	}
 	// match: (ANDLconst [c] (MOVLconst [d]))
 	// result: (MOVLconst [c&d])
 	for {
@@ -30727,15 +30765,34 @@ func rewriteValue_OpAMD64MULSSload(v *ssa.Value) bool {
 func rewriteValue_OpAMD64NEGL(v *ssa.Value) bool {
 	v_0 := v.Args[0]
 	// match: (NEGL (NEGL x))
+	// cond: v.Type.Size() <= 4
 	// result: x
 	for {
 		if v_0.Op != ssaop.OpAMD64NEGL {
 			break
 		}
 		x := v_0.Args[0]
+		if !(v.Type.Size() <= 4) {
+			break
+		}
 		v.CopyOf(x)
 		return true
 	}
+	// match: (NEGL (NEGL x))
+	// cond: v.Type.Size() > 4
+	// result: (MOVLQZX x)
+	for {
+		if v_0.Op != ssaop.OpAMD64NEGL {
+			break
+		}
+		x := v_0.Args[0]
+		if !(v.Type.Size() > 4) {
+			break
+		}
+		v.Reset(ssaop.OpAMD64MOVLQZX)
+		v.AddArg(x)
+		return true
+	}
 	// match: (NEGL s:(SUBL x y))
 	// cond: s.Uses == 1
 	// result: (SUBL y x)
@@ -30999,15 +31056,34 @@ func rewriteValue_OpAMD64ORLconst(v *ssa.Value) bool {
 		return true
 	}
 	// match: (ORLconst [0] x)
+	// cond: v.Type.Size() <= 4
 	// result: x
 	for {
 		if ssa.AuxIntToInt32(v.AuxInt) != 0 {
 			break
 		}
 		x := v_0
+		if !(v.Type.Size() <= 4) {
+			break
+		}
 		v.CopyOf(x)
 		return true
 	}
+	// match: (ORLconst [0] x)
+	// cond: v.Type.Size() > 4
+	// result: (MOVLQZX x)
+	for {
+		if ssa.AuxIntToInt32(v.AuxInt) != 0 {
+			break
+		}
+		x := v_0
+		if !(v.Type.Size() > 4) {
+			break
+		}
+		v.Reset(ssaop.OpAMD64MOVLQZX)
+		v.AddArg(x)
+		return true
+	}
 	// match: (ORLconst [-1] _)
 	// result: (MOVLconst [-1])
 	for {
@@ -41643,15 +41719,34 @@ func rewriteValue_OpAMD64SUBL(v *ssa.Value) bool {
 func rewriteValue_OpAMD64SUBLconst(v *ssa.Value) bool {
 	v_0 := v.Args[0]
 	// match: (SUBLconst [0] x)
+	// cond: v.Type.Size() <= 4
 	// result: x
 	for {
 		if ssa.AuxIntToInt32(v.AuxInt) != 0 {
 			break
 		}
 		x := v_0
+		if !(v.Type.Size() <= 4) {
+			break
+		}
 		v.CopyOf(x)
 		return true
 	}
+	// match: (SUBLconst [0] x)
+	// cond: v.Type.Size() > 4
+	// result: (MOVLQZX x)
+	for {
+		if ssa.AuxIntToInt32(v.AuxInt) != 0 {
+			break
+		}
+		x := v_0
+		if !(v.Type.Size() > 4) {
+			break
+		}
+		v.Reset(ssaop.OpAMD64MOVLQZX)
+		v.AddArg(x)
+		return true
+	}
 	// match: (SUBLconst [c] x)
 	// result: (ADDLconst [-c] x)
 	for {
@@ -98738,15 +98833,34 @@ func rewriteValue_OpAMD64XORLconst(v *ssa.Value) bool {
 		return true
 	}
 	// match: (XORLconst [0] x)
+	// cond: v.Type.Size() <= 4
 	// result: x
 	for {
 		if ssa.AuxIntToInt32(v.AuxInt) != 0 {
 			break
 		}
 		x := v_0
+		if !(v.Type.Size() <= 4) {
+			break
+		}
 		v.CopyOf(x)
 		return true
 	}
+	// match: (XORLconst [0] x)
+	// cond: v.Type.Size() > 4
+	// result: (MOVLQZX x)
+	for {
+		if ssa.AuxIntToInt32(v.AuxInt) != 0 {
+			break
+		}
+		x := v_0
+		if !(v.Type.Size() > 4) {
+			break
+		}
+		v.Reset(ssaop.OpAMD64MOVLQZX)
+		v.AddArg(x)
+		return true
+	}
 	// match: (XORLconst [c] (MOVLconst [d]))
 	// result: (MOVLconst [c^d])
 	for {
diff --git a/test/fixedbugs/issue36897.go b/test/fixedbugs/issue36897.go
new file mode 100644
index 0000000000..49b4558a54
--- /dev/null
+++ b/test/fixedbugs/issue36897.go
@@ -0,0 +1,63 @@
+// run -gcflags=-N
+
+// 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 add(x uint64) uint64 {
+	zero := uint32(x) ^ uint32(x)
+	return uint64(uint32(x) + zero)
+}
+
+//go:noinline
+func sub(x uint64) uint64 {
+	zero := uint32(x) ^ uint32(x)
+	return uint64(uint32(x) - zero)
+}
+
+//go:noinline
+func and(x uint64) uint64 {
+	zero := uint32(x) ^ uint32(x)
+	return uint64(uint32(x) &^ zero)
+}
+
+//go:noinline
+func or(x uint64) uint64 {
+	zero := uint32(x) ^ uint32(x)
+	return uint64(uint32(x) | zero)
+}
+
+//go:noinline
+func xor(x uint64) uint64 {
+	zero := uint32(x) ^ uint32(x)
+	return uint64(uint32(x) ^ zero)
+}
+
+//go:noinline
+func negneg(x uint64) uint64 {
+	return uint64(-(-uint32(x)))
+}
+
+func main() {
+	if got := add(1<<60 | 5); got != 5 {
+		println("add got", got)
+	}
+	if got := sub(1<<60 | 5); got != 5 {
+		println("sub got", got)
+	}
+	if got := and(1<<60 | 5); got != 5 {
+		println("and got", got)
+	}
+	if got := or(1<<60 | 5); got != 5 {
+		println("or got", got)
+	}
+	if got := xor(1<<60 | 5); got != 5 {
+		println("xor got", got)
+	}
+	if got := negneg(1<<60 | 5); got != 5 {
+		println("negneg got", got)
+	}
+}