]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: add ssa.Block.truncateValues
authorJosh Bleecher Snyder <josharian@gmail.com>
Thu, 23 Apr 2020 04:35:31 +0000 (21:35 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Thu, 23 Apr 2020 14:59:55 +0000 (14:59 +0000)
It is a common operation.

Passes toolstash-check.

Change-Id: Icc34600b0f79d0ecb19f257e3c7f23b6f01a26ab
Reviewed-on: https://go-review.googlesource.com/c/go/+/229599
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/compile/internal/ssa/block.go
src/cmd/compile/internal/ssa/deadcode.go
src/cmd/compile/internal/ssa/nilcheck.go
src/cmd/compile/internal/ssa/rewrite.go

index 56babf418ff1672c184582d38d7a1dfe4116e0a3..519ac214caef220ddae40f6cd7b49048fba4fc3e 100644 (file)
@@ -256,6 +256,17 @@ func (b *Block) resetWithControl2(kind BlockKind, v, w *Value) {
        w.Uses++
 }
 
+// truncateValues truncates b.Values at the ith element, zeroing subsequent elements.
+// The values in b.Values after i must already have had their args reset,
+// to maintain correct value uses counts.
+func (b *Block) truncateValues(i int) {
+       tail := b.Values[i:]
+       for j := range tail {
+               tail[j] = nil
+       }
+       b.Values = b.Values[:i]
+}
+
 // AddEdgeTo adds an edge from block b to block c. Used during building of the
 // SSA graph; do not use on an already-completed SSA graph.
 func (b *Block) AddEdgeTo(c *Block) {
index 395c1617e5748ebdc7e8c6068bc8c74956a0c9f4..96b552ecf3bfa4d3f361aca2ccf01389921c7622 100644 (file)
@@ -296,12 +296,7 @@ func deadcode(f *Func) {
                                f.freeValue(v)
                        }
                }
-               // aid GC
-               tail := b.Values[i:]
-               for j := range tail {
-                       tail[j] = nil
-               }
-               b.Values = b.Values[:i]
+               b.truncateValues(i)
        }
 
        // Remove dead blocks from WBLoads list.
index 9e1473b3b8101f547949d55523894abb6c43b71e..6b24371ac7a95ebe8f839fcc5a2e70593b5e9d29 100644 (file)
@@ -171,10 +171,7 @@ func nilcheckelim(f *Func) {
                                b.Pos = b.Pos.WithIsStmt()
                                pendingLines.remove(b.Pos)
                        }
-                       for j := i; j < len(b.Values); j++ {
-                               b.Values[j] = nil
-                       }
-                       b.Values = b.Values[:i]
+                       b.truncateValues(i)
 
                        // Add all dominated blocks to the work list.
                        for w := sdom[node.block.ID].child; w != nil; w = sdom[w.ID].sibling {
@@ -331,10 +328,7 @@ func nilcheckelim2(f *Func) {
                        b.Pos = b.Pos.WithIsStmt()
                }
 
-               for j := i; j < len(b.Values); j++ {
-                       b.Values[j] = nil
-               }
-               b.Values = b.Values[:i]
+               b.truncateValues(i)
 
                // TODO: if b.Kind == BlockPlain, start the analysis in the subsequent block to find
                // more unnecessary nil checks.  Would fix test/nilptr3.go:159.
index adda7fae93492e8775273fa617bf960583535d14..ed9b7bd4a164d61fe63710852a9e65a6ad4fc6a1 100644 (file)
@@ -152,13 +152,7 @@ func applyRewrite(f *Func, rb blockRewriter, rv valueRewriter) {
                        b.Pos = b.Pos.WithIsStmt()
                        pendingLines.remove(b.Pos)
                }
-               if j != len(b.Values) {
-                       tail := b.Values[j:]
-                       for j := range tail {
-                               tail[j] = nil
-                       }
-                       b.Values = b.Values[:j]
-               }
+               b.truncateValues(j)
        }
 }