Skip to content

Commit 9e82095

Browse files
committed
cmd/compile: use map instead of sparseSet
1 parent e15c9f7 commit 9e82095

File tree

1 file changed

+14
-19
lines changed

1 file changed

+14
-19
lines changed

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

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@ func dse(f *Func) {
2121
defer f.retSparseSet(storeUse)
2222
shadowed := f.newSparseMap(f.NumValues())
2323
defer f.retSparseMap(shadowed)
24-
localAddrs := f.newSparseSet(f.NumValues())
25-
defer f.retSparseSet(localAddrs)
24+
// localAddrs maps from a local variable (the Aux field of a LocalAddr value) to an instance of a LocalAddr value for that variable in the current block.
25+
localAddrs := map[any]*Value{}
2626
for _, b := range f.Blocks {
2727
// Find all the stores in this block. Categorize their uses:
2828
// loadUse contains stores which are used by a subsequent load.
2929
// storeUse contains stores which are used by a subsequent store.
3030
// localAddrs contains indexes into b.Values for each unique LocalAddr.
3131
loadUse.clear()
3232
storeUse.clear()
33-
localAddrs.clear()
33+
// TODO(deparker): use the 'clear' builtin once compiler bootstrap minimum version is raised to 1.21.
34+
for k := range localAddrs {
35+
delete(localAddrs, k)
36+
}
3437
stores = stores[:0]
35-
for i, v := range b.Values {
38+
for _, v := range b.Values {
3639
if v.Op == OpPhi {
3740
// Ignore phis - they will always be first and can't be eliminated
3841
continue
@@ -51,10 +54,11 @@ func dse(f *Func) {
5154
}
5255
} else {
5356
if v.Op == OpLocalAddr {
54-
if findSameLocalAddr(b, v, localAddrs) >= 0 {
57+
if _, ok := localAddrs[v.Aux]; !ok {
58+
localAddrs[v.Aux] = v
59+
} else {
5560
continue
5661
}
57-
localAddrs.add(ID(i))
5862
}
5963
for _, a := range v.Args {
6064
if a.Block == b && a.Type.IsMemory() {
@@ -110,9 +114,10 @@ func dse(f *Func) {
110114
} else { // OpZero
111115
sz = v.AuxInt
112116
}
113-
idx := findSameLocalAddr(b, ptr, localAddrs)
114-
if idx != -1 {
115-
ptr = b.Values[idx]
117+
if ptr.Op == OpLocalAddr {
118+
if la, ok := localAddrs[ptr.Aux]; ok {
119+
ptr = la
120+
}
116121
}
117122
sr := shadowRange(shadowed.get(ptr.ID))
118123
if sr.contains(off, off+sz) {
@@ -150,16 +155,6 @@ func dse(f *Func) {
150155
}
151156
}
152157

153-
func findSameLocalAddr(b *Block, vv *Value, localAddrs *sparseSet) int {
154-
for _, idx := range localAddrs.contents() {
155-
la := b.Values[idx]
156-
if isSamePtr(la, vv) {
157-
return int(idx)
158-
}
159-
}
160-
return -1
161-
}
162-
163158
// A shadowRange encodes a set of byte offsets [lo():hi()] from
164159
// a given pointer that will be written to later in the block.
165160
// A zero shadowRange encodes an empty shadowed range (and so

0 commit comments

Comments
 (0)