[go] cmd/compile: teach deadstore about moves

0 views
Skip to first unread message

Keith Randall (Gerrit)

unread,
Apr 28, 2026, 5:49:00 PM (11 hours ago) Apr 28
to David Chase, Cuong Manh Le, goph...@pubsubhelper.golang.org, Milind Chabbi, Keith Randall, golang-co...@googlegroups.com
Attention needed from Cuong Manh Le and David Chase

Keith Randall has uploaded the change for review

Keith Randall would like David Chase and Cuong Manh Le to review this change.

Commit message

cmd/compile: teach deadstore about moves

Moves that read from read-only memory can't be reading the results
of a previous store. These are often generated by constant struct literals.
Moves whose results aren't needed because that memory is immediately
overwritten, are not needed.

Saves a few bytes of generated code (~<0.1%).
Change-Id: I8dab6d1b9c066d6b623eae8b8fe31a51dd3de006

Change diff

diff --git a/src/cmd/compile/internal/ssa/deadstore.go b/src/cmd/compile/internal/ssa/deadstore.go
index 17a0809..fc796d1 100644
--- a/src/cmd/compile/internal/ssa/deadstore.go
+++ b/src/cmd/compile/internal/ssa/deadstore.go
@@ -50,7 +50,18 @@
for _, a := range v.Args {
if a.Block == b && a.Type.IsMemory() {
storeUse.add(a.ID)
- if v.Op != OpStore && v.Op != OpZero && v.Op != OpVarDef {
+ switch v.Op {
+ case OpStore, OpZero, OpVarDef:
+ // These ops never read from their memory input.
+ case OpMove:
+ // This op reads from its memory argument, but
+ // we can treat it as not doing so if we know
+ // the read is from read-only memory.
+ if v.Args[1].Op == OpAddr && symIsRO(auxToSym(v.Args[1].Aux)) {
+ break
+ }
+ fallthrough
+ default:
// CALL, DUFFCOPY, etc. are both
// reads and writes.
loadUse.add(a.ID)
@@ -111,7 +122,7 @@
shadowed.clear()
shadowedRanges = shadowedRanges[:0]
}
- if v.Op == OpStore || v.Op == OpZero {
+ if v.Op == OpStore || v.Op == OpZero || v.Op == OpMove {
ptr := v.Args[0]
var off int64
for ptr.Op == OpOffPtr { // Walk to base pointer
@@ -119,7 +130,7 @@
ptr = ptr.Args[0]
}
var sz int64
- if v.Op == OpStore {
+ if v.Op == OpStore || v.Op == OpMove {
sz = v.Aux.(*types.Type).Size()
} else { // OpZero
sz = v.AuxInt
@@ -137,13 +148,14 @@
}

if si != nil && si.contains(off, off+sz) {
- // Modify the store/zero into a copy of the memory state,
+ // Modify the store/zero/move into a copy of the memory state,
// effectively eliding the store operation.
- if v.Op == OpStore {
- // store addr value mem
+ if v.Op == OpStore || v.Op == OpMove {
+ // Store addr value mem
+ // or Move dst src mem
v.SetArgs1(v.Args[2])
} else {
- // zero addr mem
+ // Zero addr mem
v.SetArgs1(v.Args[1])
}
v.Aux = nil
diff --git a/test/codegen/deadstore.go b/test/codegen/deadstore.go
new file mode 100644
index 0000000..fcea34f
--- /dev/null
+++ b/test/codegen/deadstore.go
@@ -0,0 +1,29 @@
+// asmcheck
+
+// 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 codegen
+
+type S struct {
+ a, b, c, d, e int
+}
+
+func f1(s *S) {
+ // amd64:-`MOVUPS`
+ // arm64:-`STP` - `MOVD`
+ *s = S{}
+ *s = S{a: 3, b: 4, c: 5, d: 6, e: 7}
+}
+
+func f2(s *S) {
+ // amd64:-`MOVUPS`
+ // arm64:-`MOVD` -`FSTPQ`
+ *s = S{a: 1, b: 2, c: 3, d: 4, e: 5}
+ s.a = 3
+ s.b = 4
+ s.c = 5
+ s.d = 6
+ s.e = 7
+}

Change information

Files:
  • M src/cmd/compile/internal/ssa/deadstore.go
  • A test/codegen/deadstore.go
Change size: M
Delta: 2 files changed, 48 insertions(+), 7 deletions(-)
Open in Gerrit

Related details

Attention is currently required from:
  • Cuong Manh Le
  • David Chase
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I8dab6d1b9c066d6b623eae8b8fe31a51dd3de006
Gerrit-Change-Number: 771780
Gerrit-PatchSet: 1
Gerrit-Owner: Keith Randall <k...@golang.org>
Gerrit-Reviewer: Cuong Manh Le <cuong.m...@gmail.com>
Gerrit-Reviewer: David Chase <drc...@google.com>
Gerrit-Reviewer: Keith Randall <k...@golang.org>
Gerrit-CC: Milind Chabbi <mil...@uber.com>
Gerrit-Attention: David Chase <drc...@google.com>
Gerrit-Attention: Cuong Manh Le <cuong.m...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Cuong Manh Le (Gerrit)

unread,
Apr 28, 2026, 8:57:25 PM (8 hours ago) Apr 28
to Keith Randall, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, David Chase, Milind Chabbi, golang-co...@googlegroups.com
Attention needed from David Chase and Keith Randall

Cuong Manh Le added 1 comment

File test/codegen/deadstore.go
Line 15, Patchset 1 (Latest): // arm64:-`STP` - `MOVD`
Cuong Manh Le . unresolved

Should be no space here, I guess.

Open in Gerrit

Related details

Attention is currently required from:
  • David Chase
  • Keith Randall
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I8dab6d1b9c066d6b623eae8b8fe31a51dd3de006
    Gerrit-Change-Number: 771780
    Gerrit-PatchSet: 1
    Gerrit-Owner: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Cuong Manh Le <cuong.m...@gmail.com>
    Gerrit-Reviewer: David Chase <drc...@google.com>
    Gerrit-Reviewer: Keith Randall <k...@golang.org>
    Gerrit-CC: Milind Chabbi <mil...@uber.com>
    Gerrit-Attention: Keith Randall <k...@golang.org>
    Gerrit-Attention: David Chase <drc...@google.com>
    Gerrit-Comment-Date: Wed, 29 Apr 2026 00:57:21 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Keith Randall (Gerrit)

    unread,
    Apr 28, 2026, 9:08:58 PM (8 hours ago) Apr 28
    to Keith Randall, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, David Chase, Cuong Manh Le, Milind Chabbi, golang-co...@googlegroups.com
    Attention needed from Cuong Manh Le and David Chase

    Keith Randall added 1 comment

    File test/codegen/deadstore.go
    Line 15, Patchset 1 (Latest): // arm64:-`STP` - `MOVD`
    Cuong Manh Le . resolved

    Should be no space here, I guess.

    Keith Randall

    Seems likely, thanks.

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Cuong Manh Le
    • David Chase
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I8dab6d1b9c066d6b623eae8b8fe31a51dd3de006
      Gerrit-Change-Number: 771780
      Gerrit-PatchSet: 1
      Gerrit-Owner: Keith Randall <k...@golang.org>
      Gerrit-Reviewer: Cuong Manh Le <cuong.m...@gmail.com>
      Gerrit-Reviewer: David Chase <drc...@google.com>
      Gerrit-Reviewer: Keith Randall <k...@golang.org>
      Gerrit-CC: Milind Chabbi <mil...@uber.com>
      Gerrit-Attention: David Chase <drc...@google.com>
      Gerrit-Attention: Cuong Manh Le <cuong.m...@gmail.com>
      Gerrit-Comment-Date: Wed, 29 Apr 2026 01:08:54 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Cuong Manh Le <cuong.m...@gmail.com>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Keith Randall (Gerrit)

      unread,
      Apr 28, 2026, 9:09:24 PM (8 hours ago) Apr 28
      to Keith Randall, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
      Attention needed from Cuong Manh Le and David Chase

      Keith Randall uploaded new patchset

      Keith Randall uploaded patch set #2 to this change.
      Following approvals got outdated and were removed:
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Cuong Manh Le
      • David Chase
      Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: newpatchset
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I8dab6d1b9c066d6b623eae8b8fe31a51dd3de006
      Gerrit-Change-Number: 771780
      Gerrit-PatchSet: 2
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Cuong Manh Le (Gerrit)

      unread,
      Apr 28, 2026, 9:10:01 PM (8 hours ago) Apr 28
      to Keith Randall, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, David Chase, Milind Chabbi, golang-co...@googlegroups.com
      Attention needed from David Chase and Keith Randall

      Cuong Manh Le voted Code-Review+2

      Code-Review+2
      Open in Gerrit

      Related details

      Attention is currently required from:
      • David Chase
      • Keith Randall
      Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I8dab6d1b9c066d6b623eae8b8fe31a51dd3de006
      Gerrit-Change-Number: 771780
      Gerrit-PatchSet: 2
      Gerrit-Owner: Keith Randall <k...@golang.org>
      Gerrit-Reviewer: Cuong Manh Le <cuong.m...@gmail.com>
      Gerrit-Reviewer: David Chase <drc...@google.com>
      Gerrit-Reviewer: Keith Randall <k...@golang.org>
      Gerrit-CC: Milind Chabbi <mil...@uber.com>
      Gerrit-Attention: Keith Randall <k...@golang.org>
      Gerrit-Attention: David Chase <drc...@google.com>
      Gerrit-Comment-Date: Wed, 29 Apr 2026 01:09:53 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy

      Keith Randall (Gerrit)

      unread,
      Apr 28, 2026, 9:20:35 PM (8 hours ago) Apr 28
      to Keith Randall, goph...@pubsubhelper.golang.org, Cuong Manh Le, golang...@luci-project-accounts.iam.gserviceaccount.com, David Chase, Milind Chabbi, golang-co...@googlegroups.com
      Attention needed from David Chase and Keith Randall

      Keith Randall voted Code-Review+1

      Code-Review+1
      Open in Gerrit

      Related details

      Attention is currently required from:
      • David Chase
      • Keith Randall
      Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I8dab6d1b9c066d6b623eae8b8fe31a51dd3de006
      Gerrit-Change-Number: 771780
      Gerrit-PatchSet: 2
      Gerrit-Owner: Keith Randall <k...@golang.org>
      Gerrit-Reviewer: Cuong Manh Le <cuong.m...@gmail.com>
      Gerrit-Reviewer: David Chase <drc...@google.com>
      Gerrit-Reviewer: Keith Randall <k...@golang.org>
      Gerrit-Reviewer: Keith Randall <k...@google.com>
      Gerrit-CC: Milind Chabbi <mil...@uber.com>
      Gerrit-Attention: Keith Randall <k...@golang.org>
      Gerrit-Attention: David Chase <drc...@google.com>
      Gerrit-Comment-Date: Wed, 29 Apr 2026 01:20:31 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages