]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: use ,ok return idiom for sparsemap.get
authorKeith Randall <khr@golang.org>
Fri, 13 Jun 2025 18:08:53 +0000 (11:08 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 24 Jul 2025 16:04:29 +0000 (09:04 -0700)
Change-Id: I89719b94de74a32402d02309515dffc4989484db
Reviewed-on: https://go-review.googlesource.com/c/go/+/681575
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Keith Randall <khr@google.com>
Auto-Submit: Keith Randall <khr@google.com>

src/cmd/compile/internal/ssa/biasedsparsemap.go
src/cmd/compile/internal/ssa/deadcode.go
src/cmd/compile/internal/ssa/deadstore.go
src/cmd/compile/internal/ssa/nilcheck.go
src/cmd/compile/internal/ssa/rewrite.go
src/cmd/compile/internal/ssa/sparsemap.go
src/cmd/compile/internal/ssa/xposmap.go

index c08efbf1621394a5f6c15ee53d043a5c6f29d02e..3032309b7a2f1b3d3217a4e58330f34e09504d77 100644 (file)
@@ -56,21 +56,21 @@ func (s *biasedSparseMap) contains(x uint) bool {
        return s.s.contains(ID(int(x) - s.first))
 }
 
-// get returns the value s maps for key x, or -1 if
-// x is not mapped or is out of range for s.
-func (s *biasedSparseMap) get(x uint) int32 {
+// get returns the value s maps for key x and true, or
+// 0/false if x is not mapped or is out of range for s.
+func (s *biasedSparseMap) get(x uint) (int32, bool) {
        if s == nil || s.s == nil {
-               return -1
+               return 0, false
        }
        if int(x) < s.first {
-               return -1
+               return 0, false
        }
        if int(x) >= s.cap() {
-               return -1
+               return 0, false
        }
        k := ID(int(x) - s.first)
        if !s.s.contains(k) {
-               return -1 // TODO: push presence check to callers?
+               return 0, false
        }
        return s.s.get(k)
 }
index 1aa0f9e29603e9ead2dbb93109316eafc9640ada..aa85097c29d63e1e81e27f3bfdeb1efdf3f2e40c 100644 (file)
@@ -257,7 +257,7 @@ func deadcode(f *Func) {
        // Find new homes for lost lines -- require earliest in data flow with same line that is also in same block
        for i := len(order) - 1; i >= 0; i-- {
                w := order[i]
-               if j := pendingLines.get(w.Pos); j > -1 && f.Blocks[j] == w.Block {
+               if j, ok := pendingLines.get(w.Pos); ok && f.Blocks[j] == w.Block {
                        w.Pos = w.Pos.WithIsStmt()
                        pendingLines.remove(w.Pos)
                }
index 40905bedcf19cf959085d4ce77bacb4c54724f16..9e67e8339992c7f2da958cde70bbd97db6d007ce 100644 (file)
@@ -118,7 +118,8 @@ func dse(f *Func) {
                                        ptr = la
                                }
                        }
-                       sr := shadowRange(shadowed.get(ptr.ID))
+                       srNum, _ := shadowed.get(ptr.ID)
+                       sr := shadowRange(srNum)
                        if sr.contains(off, off+sz) {
                                // Modify the store/zero into a copy of the memory state,
                                // effectively eliding the store operation.
index 9d43ec19919825b61ee041dd649eadc901ba632a..467c7514eee5f2065d9a46ef9eb503e30147afdf 100644 (file)
@@ -221,7 +221,8 @@ func nilcheckelim2(f *Func) {
 
                                // Iteration order means that first nilcheck in the chain wins, others
                                // are bumped into the ordinary statement preservation algorithm.
-                               u := b.Values[unnecessary.get(v.Args[0].ID)]
+                               uid, _ := unnecessary.get(v.Args[0].ID)
+                               u := b.Values[uid]
                                if !u.Type.IsMemory() && !u.Pos.SameFileAndLine(v.Pos) {
                                        if u.Pos.IsStmt() == src.PosIsStmt {
                                                pendingLines.add(u.Pos)
index eb2c3b31b8c99827afcb77752afdafd85a52b333..0e7d354d6bd7440b7a5c9d77089f7c490b843f8f 100644 (file)
@@ -199,16 +199,18 @@ func applyRewrite(f *Func, rb blockRewriter, rv valueRewriter, deadcode deadValu
                                f.freeValue(v)
                                continue
                        }
-                       if v.Pos.IsStmt() != src.PosNotStmt && !notStmtBoundary(v.Op) && pendingLines.get(vl) == int32(b.ID) {
-                               pendingLines.remove(vl)
-                               v.Pos = v.Pos.WithIsStmt()
+                       if v.Pos.IsStmt() != src.PosNotStmt && !notStmtBoundary(v.Op) {
+                               if pl, ok := pendingLines.get(vl); ok && pl == int32(b.ID) {
+                                       pendingLines.remove(vl)
+                                       v.Pos = v.Pos.WithIsStmt()
+                               }
                        }
                        if i != j {
                                b.Values[j] = v
                        }
                        j++
                }
-               if pendingLines.get(b.Pos) == int32(b.ID) {
+               if pl, ok := pendingLines.get(b.Pos); ok && pl == int32(b.ID) {
                        b.Pos = b.Pos.WithIsStmt()
                        pendingLines.remove(b.Pos)
                }
index b7363b36ebeecb00a2d1e855e814b108afdf8384..255a346d37f01d5523e0f68f48ba4f1caab40219 100644 (file)
@@ -41,13 +41,13 @@ func (s *genericSparseMap[K, V]) contains(k K) bool {
 
 // get returns the value for key k, or the zero V
 // if k does not appear in the map.
-func (s *genericSparseMap[K, V]) get(k K) V {
+func (s *genericSparseMap[K, V]) get(k K) (V, bool) {
        i := s.sparse[k]
        if i < int32(len(s.dense)) && s.dense[i].key == k {
-               return s.dense[i].val
+               return s.dense[i].val, true
        }
        var v V
-       return v
+       return v, false
 }
 
 func (s *genericSparseMap[K, V]) set(k K, v V) {
index 93582e1373e27223b472b0b96f81e39120dfbccb..382f916571c0a23b560c4bae3d1a1e51d41fcd30 100644 (file)
@@ -69,10 +69,10 @@ func (m *xposmap) set(p src.XPos, v int32) {
 }
 
 // get returns the int32 associated with the file index and line of p.
-func (m *xposmap) get(p src.XPos) int32 {
+func (m *xposmap) get(p src.XPos) (int32, bool) {
        s := m.mapFor(p.FileIndex())
        if s == nil {
-               return -1
+               return 0, false
        }
        return s.get(p.Line())
 }