Commit 5f0cfed796 for golang

commit 5f0cfed796c4b15fd56e3f12dff8fa52178c66f4
Author: Michael Haddon <michael@politepixels.io>
Date:   Thu Nov 26 05:17:01 2026 +0000

    cmd/compile: fold noov comparisons of constant flags by the sign alone

    On arm and arm64, x+y < 0 and the other comparisons of a sum with 0
    become noov comparisons of CMN x y, which test MI, PL and EQ and so
    compare the sum as Go wraps it. When lowering then finds that x and y
    are constants, LtNoov and the others fold the flags. They also require
    V to be clear, so they are all false when the sum overflows. For
    example, x+y < 0 was false for x == math.MaxInt32 and y == 1 loaded
    from a constant string.

    Test only N and Z, as the generated code does.

    Add a test.

    Fixes #83409

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

diff --git a/src/cmd/compile/internal/ssa/rewrite.go b/src/cmd/compile/internal/ssa/rewrite.go
index 17512cbcd4..721f4c9a78 100644
--- a/src/cmd/compile/internal/ssa/rewrite.go
+++ b/src/cmd/compile/internal/ssa/rewrite.go
@@ -489,20 +489,23 @@ func (fc FlagConstant) Uge() bool {
 	return fc.C()
 }

+// The noov conditions compare the result of an operation with 0,
+// ignoring V, as the MI, PL and EQ conditions that implement them do.
+
 func (fc FlagConstant) LtNoov() bool {
-	return fc.Lt() && !fc.V()
+	return fc.N()
 }

 func (fc FlagConstant) LeNoov() bool {
-	return fc.Le() && !fc.V()
+	return fc.Z() || fc.LtNoov()
 }

 func (fc FlagConstant) GtNoov() bool {
-	return fc.Gt() && !fc.V()
+	return !fc.Z() && fc.GeNoov()
 }

 func (fc FlagConstant) GeNoov() bool {
-	return fc.Ge() && !fc.V()
+	return !fc.N()
 }

 func (fc FlagConstant) String() string {
diff --git a/src/cmd/compile/internal/ssacompile/rewrite_test.go b/src/cmd/compile/internal/ssacompile/rewrite_test.go
index b5ffc1f253..78600a5008 100644
--- a/src/cmd/compile/internal/ssacompile/rewrite_test.go
+++ b/src/cmd/compile/internal/ssacompile/rewrite_test.go
@@ -46,6 +46,20 @@ func TestSubFlags(t *testing.T) {
 	}
 }

+func TestNoovFlags(t *testing.T) {
+	numbers := []int64{0, 1, -1, math.MaxInt64, math.MinInt64}
+	for _, x := range numbers {
+		for _, y := range numbers {
+			fc, z := ssa.AddFlags64(x, y), x+y
+			got := [4]bool{fc.LtNoov(), fc.LeNoov(), fc.GtNoov(), fc.GeNoov()}
+			want := [4]bool{z < 0, z <= 0, z > 0, z >= 0}
+			if got != want {
+				t.Errorf("AddFlags64(%d, %d) LtNoov, LeNoov, GtNoov, GeNoov = %v, want %v", x, y, got, want)
+			}
+		}
+	}
+}
+
 //go:noinline
 func unopt(f func(float64) float64, x float32) float32 {
 	return float32(f(float64(x)))
diff --git a/test/fixedbugs/issue83409.go b/test/fixedbugs/issue83409.go
new file mode 100644
index 0000000000..798e0cece6
--- /dev/null
+++ b/test/fixedbugs/issue83409.go
@@ -0,0 +1,48 @@
+// 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
+
+// The loads from s become constants only during lowering.
+const s = "\xff\xff\xff\x7f\x01\x00\x00\x00\x00\x00\x00\x80\xff\xff\xff\xff"
+
+func load(i int) int32 {
+	return int32(uint32(s[i]) | uint32(s[i+1])<<8 | uint32(s[i+2])<<16 | uint32(s[i+3])<<24)
+}
+
+//go:noinline
+func lt() bool {
+	return load(0)+load(4) < 0 // math.MaxInt32 + 1 wraps to math.MinInt32
+}
+
+//go:noinline
+func ge() bool {
+	return load(8)+load(12) >= 0 // math.MinInt32 - 1 wraps to math.MaxInt32
+}
+
+//go:noinline
+func le() bool {
+	if load(0)+load(4) <= 0 {
+		return true
+	}
+	return false
+}
+
+//go:noinline
+func gt() bool {
+	if load(8)+load(12) > 0 {
+		return true
+	}
+	return false
+}
+
+func main() {
+	for i, f := range []func() bool{lt, ge, le, gt} {
+		if !f() {
+			panic(i)
+		}
+	}
+}