cmd/compile: use ,ok return idiom for sparsemap.get

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>
This commit is contained in:
Keith Randall
2025-06-13 11:08:53 -07:00
committed by Gopher Robot
parent 6505fcbd0a
commit 2ddf542e4c
7 changed files with 23 additions and 19 deletions

View 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)
}

View 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)
}

View 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.

View 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)

View 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)
}

View 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) {

View 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())
}