Skip to content

Commit

Permalink
cmd/compile: refine statement marking in numberlines
Browse files Browse the repository at this point in the history
1) An empty block is treated as not-a-statement unless its line differs
from at least one of its predecessors (it might make sense to
rearrange branches in predecessors, but that is a different issue).

2) When iterating forward to choose a "good" place for a statement,
actually check that the chosen place is in fact good.

3) Refactor same line and same file into methods on XPos and Pos.

This reduces the failure rate of ssa/stmtlines_test by 7-ish lines.
(And interacts favorably with later debugging CLs.)

Change-Id: Idb7cca7068f6fc9fbfdbe25bc0da15bcfc7b9d4a
Reviewed-on: https://go-review.googlesource.com/c/go/+/188217
Run-TryBot: David Chase <[email protected]>
Reviewed-by: Jeremy Faller <[email protected]>
  • Loading branch information
dr2chase committed Oct 3, 2019
1 parent 3ad3508 commit f7f85bd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
41 changes: 25 additions & 16 deletions src/cmd/compile/internal/ssa/numberlines.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,20 @@ func nextGoodStatementIndex(v *Value, i int, b *Block) int {
if i >= len(b.Values)-1 {
return i
}
// Only consider the likely-ephemeral/fragile opcodes expected to vanish in a rewrite.
// Skip the likely-ephemeral/fragile opcodes expected to vanish in a rewrite.
if !isPoorStatementOp(v.Op) {
return i
}
// Look ahead to see what the line number is on the next thing that could be a boundary.
for j := i + 1; j < len(b.Values); j++ {
if b.Values[j].Pos.IsStmt() == src.PosNotStmt { // ignore non-statements
u := b.Values[j]
if u.Pos.IsStmt() == src.PosNotStmt { // ignore non-statements
continue
}
if b.Values[j].Pos.Line() == v.Pos.Line() && v.Pos.SameFile(b.Values[j].Pos) {
if u.Pos.SameFileAndLine(v.Pos) {
if isPoorStatementOp(u.Op) {
continue // Keep looking, this is also not a good statement op
}
return j
}
return i
Expand Down Expand Up @@ -156,18 +160,10 @@ func numberLines(f *Func) {
}

if firstPosIndex == -1 { // Effectively empty block, check block's own Pos, consider preds.
if b.Pos.IsStmt() != src.PosNotStmt {
b.Pos = b.Pos.WithIsStmt()
endlines[b.ID] = b.Pos
if f.pass.debug > 0 {
fmt.Printf("Mark stmt effectively-empty-block %s %s %s\n", f.Name, b, flc(b.Pos))
}
continue
}
line := src.NoXPos
for _, p := range b.Preds {
pbi := p.Block().ID
if endlines[pbi] != line {
if !endlines[pbi].SameFileAndLine(line) {
if line == src.NoXPos {
line = endlines[pbi]
continue
Expand All @@ -178,7 +174,20 @@ func numberLines(f *Func) {

}
}
endlines[b.ID] = line
// If the block has no statement itself and is effectively empty, tag it w/ predecessor(s) but not as a statement
if b.Pos.IsStmt() == src.PosNotStmt {
b.Pos = line
endlines[b.ID] = line
continue
}
// If the block differs from its predecessors, mark it as a statement
if line == src.NoXPos || !line.SameFileAndLine(b.Pos) {
b.Pos = b.Pos.WithIsStmt()
if f.pass.debug > 0 {
fmt.Printf("Mark stmt effectively-empty-block %s %s %s\n", f.Name, b, flc(b.Pos))
}
}
endlines[b.ID] = b.Pos
continue
}
// check predecessors for any difference; if firstPos differs, then it is a boundary.
Expand All @@ -190,7 +199,7 @@ func numberLines(f *Func) {
} else { // differing pred
for _, p := range b.Preds {
pbi := p.Block().ID
if endlines[pbi].Line() != firstPos.Line() || !endlines[pbi].SameFile(firstPos) {
if !endlines[pbi].SameFileAndLine(firstPos) {
b.Values[firstPosIndex].Pos = firstPos.WithIsStmt()
if f.pass.debug > 0 {
fmt.Printf("Mark stmt differing-pred %s %s %s %s, different=%s ending %s\n",
Expand All @@ -210,7 +219,7 @@ func numberLines(f *Func) {
// skip ahead if possible
i = nextGoodStatementIndex(v, i, b)
v = b.Values[i]
if v.Pos.Line() != firstPos.Line() || !v.Pos.SameFile(firstPos) {
if !v.Pos.SameFileAndLine(firstPos) {
if f.pass.debug > 0 {
fmt.Printf("Mark stmt new line %s %s %s %s prev pos = %s\n", f.Name, b, v, flc(v.Pos), flc(firstPos))
}
Expand All @@ -220,7 +229,7 @@ func numberLines(f *Func) {
v.Pos = v.Pos.WithDefaultStmt()
}
}
if b.Pos.IsStmt() != src.PosNotStmt && (b.Pos.Line() != firstPos.Line() || !b.Pos.SameFile(firstPos)) {
if b.Pos.IsStmt() != src.PosNotStmt && !b.Pos.SameFileAndLine(firstPos) {
if f.pass.debug > 0 {
fmt.Printf("Mark stmt end of block differs %s %s %s prev pos = %s\n", f.Name, b, flc(b.Pos), flc(firstPos))
}
Expand Down
5 changes: 3 additions & 2 deletions src/cmd/internal/src/pos.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,9 @@ func makeLico(line, col uint) lico {
return makeLicoRaw(line, col)
}

func (x lico) Line() uint { return uint(x) >> lineShift }
func (x lico) Col() uint { return uint(x) >> colShift & colMax }
func (x lico) Line() uint { return uint(x) >> lineShift }
func (x lico) SameLine(y lico) bool { return 0 == (x^y)&^lico(1 << lineShift-1) }
func (x lico) Col() uint { return uint(x) >> colShift & colMax }
func (x lico) IsStmt() uint {
if x == 0 {
return PosNotStmt
Expand Down
5 changes: 5 additions & 0 deletions src/cmd/internal/src/xpos.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ func (p XPos) SameFile(q XPos) bool {
return p.index == q.index
}

// SameFileAndLine reports whether p and q are positions on the same line in the same file.
func (p XPos) SameFileAndLine(q XPos) bool {
return p.index == q.index && p.lico.SameLine(q.lico)
}

// After reports whether the position p comes after q in the source.
// For positions with different bases, ordering is by base index.
func (p XPos) After(q XPos) bool {
Expand Down

0 comments on commit f7f85bd

Please sign in to comment.