Skip to content

Commit 2ddf542

Browse files
randall77gopherbot
authored andcommitted
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 <[email protected]> Reviewed-by: David Chase <[email protected]> Reviewed-by: Keith Randall <[email protected]> Auto-Submit: Keith Randall <[email protected]>
1 parent 6505fcb commit 2ddf542

File tree

7 files changed

+23
-19
lines changed

7 files changed

+23
-19
lines changed

src/cmd/compile/internal/ssa/biasedsparsemap.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,21 @@ func (s *biasedSparseMap) contains(x uint) bool {
5656
return s.s.contains(ID(int(x) - s.first))
5757
}
5858

59-
// get returns the value s maps for key x, or -1 if
60-
// x is not mapped or is out of range for s.
61-
func (s *biasedSparseMap) get(x uint) int32 {
59+
// get returns the value s maps for key x and true, or
60+
// 0/false if x is not mapped or is out of range for s.
61+
func (s *biasedSparseMap) get(x uint) (int32, bool) {
6262
if s == nil || s.s == nil {
63-
return -1
63+
return 0, false
6464
}
6565
if int(x) < s.first {
66-
return -1
66+
return 0, false
6767
}
6868
if int(x) >= s.cap() {
69-
return -1
69+
return 0, false
7070
}
7171
k := ID(int(x) - s.first)
7272
if !s.s.contains(k) {
73-
return -1 // TODO: push presence check to callers?
73+
return 0, false
7474
}
7575
return s.s.get(k)
7676
}

src/cmd/compile/internal/ssa/deadcode.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func deadcode(f *Func) {
257257
// Find new homes for lost lines -- require earliest in data flow with same line that is also in same block
258258
for i := len(order) - 1; i >= 0; i-- {
259259
w := order[i]
260-
if j := pendingLines.get(w.Pos); j > -1 && f.Blocks[j] == w.Block {
260+
if j, ok := pendingLines.get(w.Pos); ok && f.Blocks[j] == w.Block {
261261
w.Pos = w.Pos.WithIsStmt()
262262
pendingLines.remove(w.Pos)
263263
}

src/cmd/compile/internal/ssa/deadstore.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ func dse(f *Func) {
118118
ptr = la
119119
}
120120
}
121-
sr := shadowRange(shadowed.get(ptr.ID))
121+
srNum, _ := shadowed.get(ptr.ID)
122+
sr := shadowRange(srNum)
122123
if sr.contains(off, off+sz) {
123124
// Modify the store/zero into a copy of the memory state,
124125
// effectively eliding the store operation.

src/cmd/compile/internal/ssa/nilcheck.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ func nilcheckelim2(f *Func) {
221221

222222
// Iteration order means that first nilcheck in the chain wins, others
223223
// are bumped into the ordinary statement preservation algorithm.
224-
u := b.Values[unnecessary.get(v.Args[0].ID)]
224+
uid, _ := unnecessary.get(v.Args[0].ID)
225+
u := b.Values[uid]
225226
if !u.Type.IsMemory() && !u.Pos.SameFileAndLine(v.Pos) {
226227
if u.Pos.IsStmt() == src.PosIsStmt {
227228
pendingLines.add(u.Pos)

src/cmd/compile/internal/ssa/rewrite.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,18 @@ func applyRewrite(f *Func, rb blockRewriter, rv valueRewriter, deadcode deadValu
199199
f.freeValue(v)
200200
continue
201201
}
202-
if v.Pos.IsStmt() != src.PosNotStmt && !notStmtBoundary(v.Op) && pendingLines.get(vl) == int32(b.ID) {
203-
pendingLines.remove(vl)
204-
v.Pos = v.Pos.WithIsStmt()
202+
if v.Pos.IsStmt() != src.PosNotStmt && !notStmtBoundary(v.Op) {
203+
if pl, ok := pendingLines.get(vl); ok && pl == int32(b.ID) {
204+
pendingLines.remove(vl)
205+
v.Pos = v.Pos.WithIsStmt()
206+
}
205207
}
206208
if i != j {
207209
b.Values[j] = v
208210
}
209211
j++
210212
}
211-
if pendingLines.get(b.Pos) == int32(b.ID) {
213+
if pl, ok := pendingLines.get(b.Pos); ok && pl == int32(b.ID) {
212214
b.Pos = b.Pos.WithIsStmt()
213215
pendingLines.remove(b.Pos)
214216
}

src/cmd/compile/internal/ssa/sparsemap.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ func (s *genericSparseMap[K, V]) contains(k K) bool {
4141

4242
// get returns the value for key k, or the zero V
4343
// if k does not appear in the map.
44-
func (s *genericSparseMap[K, V]) get(k K) V {
44+
func (s *genericSparseMap[K, V]) get(k K) (V, bool) {
4545
i := s.sparse[k]
4646
if i < int32(len(s.dense)) && s.dense[i].key == k {
47-
return s.dense[i].val
47+
return s.dense[i].val, true
4848
}
4949
var v V
50-
return v
50+
return v, false
5151
}
5252

5353
func (s *genericSparseMap[K, V]) set(k K, v V) {

src/cmd/compile/internal/ssa/xposmap.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ func (m *xposmap) set(p src.XPos, v int32) {
6969
}
7070

7171
// get returns the int32 associated with the file index and line of p.
72-
func (m *xposmap) get(p src.XPos) int32 {
72+
func (m *xposmap) get(p src.XPos) (int32, bool) {
7373
s := m.mapFor(p.FileIndex())
7474
if s == nil {
75-
return -1
75+
return 0, false
7676
}
7777
return s.get(p.Line())
7878
}

0 commit comments

Comments
 (0)