cmd/compile: debug rewrite

If -d=ssa/PASS/debug=N is specified (N >= 2) for a rewrite pass
(e.g. lower), when a Value (or Block) is rewritten, print the
Value (or Block) before and after.

For #31915.
Updates #19013.

Change-Id: I80eadd44302ae736bc7daed0ef68529ab7a16776
Reviewed-on: https://go-review.googlesource.com/c/go/+/176718
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
Cherry Zhang 2020-01-24 10:43:09 -05:00
parent 6d63a74f8e
commit 1b15c7f102
2 changed files with 35 additions and 4 deletions

View file

@ -23,9 +23,19 @@ func applyRewrite(f *Func, rb blockRewriter, rv valueRewriter) {
// repeat rewrites until we find no more rewrites
pendingLines := f.cachedLineStarts // Holds statement boundaries that need to be moved to a new value/block
pendingLines.clear()
debug := f.pass.debug
if debug > 1 {
fmt.Printf("%s: rewriting for %s\n", f.pass.name, f.Name)
}
for {
change := false
for _, b := range f.Blocks {
var b0 *Block
if debug > 1 {
b0 = new(Block)
*b0 = *b
b0.Succs = append([]Edge{}, b.Succs...) // make a new copy, not aliasing
}
for i, c := range b.ControlValues() {
for c.Op == OpCopy {
c = c.Args[0]
@ -34,9 +44,22 @@ func applyRewrite(f *Func, rb blockRewriter, rv valueRewriter) {
}
if rb(b) {
change = true
if debug > 1 {
fmt.Printf("rewriting %s -> %s\n", b0.LongString(), b.LongString())
}
}
for j, v := range b.Values {
change = phielimValue(v) || change
var v0 *Value
if debug > 1 {
v0 = new(Value)
*v0 = *v
v0.Args = append([]*Value{}, v.Args...) // make a new copy, not aliasing
}
vchange := phielimValue(v)
if vchange && debug > 1 {
fmt.Printf("rewriting %s -> %s\n", v0.LongString(), v.LongString())
}
// Eliminate copy inputs.
// If any copy input becomes unused, mark it
@ -70,17 +93,20 @@ func applyRewrite(f *Func, rb blockRewriter, rv valueRewriter) {
}
a.Pos = a.Pos.WithNotStmt()
}
change = true
vchange = true
for a.Uses == 0 {
b := a.Args[0]
a.reset(OpInvalid)
a = b
}
}
if vchange && debug > 1 {
fmt.Printf("rewriting %s -> %s\n", v0.LongString(), v.LongString())
}
// apply rewrite function
if rv(v) {
change = true
vchange = true
// If value changed to a poor choice for a statement boundary, move the boundary
if v.Pos.IsStmt() == src.PosIsStmt {
if k := nextGoodStatementIndex(v, j, b); k != j {
@ -89,6 +115,11 @@ func applyRewrite(f *Func, rb blockRewriter, rv valueRewriter) {
}
}
}
change = change || vchange
if vchange && debug > 1 {
fmt.Printf("rewriting %s -> %s\n", v0.LongString(), v.LongString())
}
}
}
if !change {

View file

@ -1,4 +1,4 @@
// errorcheck -0 -d=ssa/opt/debug=3
// errorcheck -0 -d=ssa/opt/debug=1
package main