// nilcheckelim2 eliminates unnecessary nil checks.
// Runs after lowering and scheduling.
func nilcheckelim2(f *Func) {
- unnecessary := f.newSparseSet(f.NumValues())
- defer f.retSparseSet(unnecessary)
+ unnecessary := f.newSparseMap(f.NumValues()) // map from pointer that will be dereferenced to index of dereferencing value in b.Values[]
+ defer f.retSparseMap(unnecessary)
pendingLines := f.cachedLineStarts // Holds statement boundaries that need to be moved to a new value/block
if f.fe.Debug_checknil() && v.Pos.Line() > 1 {
f.Warnl(v.Pos, "removed nil check")
}
- if v.Pos.IsStmt() == src.PosIsStmt {
+ // For bug 33724, policy is that we might choose to bump an existing position
+ // off the faulting load/store in favor of the one from the nil check.
+
+ // Iteration order means that first nilcheck in the chain wins, others
+ // are bumped into the ordinary statement preservation algorithm.
+ u := b.Values[unnecessary.get(v.Args[0].ID)]
+ if !u.Pos.SameFileAndLine(v.Pos) {
+ if u.Pos.IsStmt() == src.PosIsStmt {
+ pendingLines.add(u.Pos)
+ }
+ u.Pos = v.Pos
+ } else if v.Pos.IsStmt() == src.PosIsStmt {
pendingLines.add(v.Pos)
}
+
v.reset(OpUnknown)
firstToRemove = i
continue
}
// This instruction is guaranteed to fault if ptr is nil.
// Any previous nil check op is unnecessary.
- unnecessary.add(ptr.ID)
+ unnecessary.set(ptr.ID, int32(i), src.NoXPos)
}
}
// Remove values we've clobbered with OpUnknown.
for j := i; j < len(b.Values); j++ {
v := b.Values[j]
if v.Op != OpUnknown {
- if v.Pos.IsStmt() != src.PosNotStmt && pendingLines.contains(v.Pos) {
+ if !notStmtBoundary(v.Op) && pendingLines.contains(v.Pos) { // Late in compilation, so any remaining NotStmt values are probably okay now.
v.Pos = v.Pos.WithIsStmt()
pendingLines.remove(v.Pos)
}
// rewrite.
func notStmtBoundary(op Op) bool {
switch op {
- case OpCopy, OpPhi, OpVarKill, OpVarDef, OpUnknown, OpFwdRef, OpArg:
+ case OpCopy, OpPhi, OpVarKill, OpVarDef, OpVarLive, OpUnknown, OpFwdRef, OpArg:
return true
}
return false
// We want to merge load+op in the first function, but not in the
// second. See Issue 19595.
func load_op_merge(p, q *int) {
- x := *p
- *q += x // amd64:`ADDQ\t\(`
+ x := *p // amd64:`ADDQ\t\(`
+ *q += x // The combined nilcheck and load would normally have this line number, but we want that combined operation to have the line number of the nil check instead (see #33724).
}
func load_op_no_merge(p, q *int) {
x := *p
--- /dev/null
+// run
+package main
+
+import (
+ "fmt"
+ "runtime/debug"
+ "strings"
+)
+
+type Inner struct {
+ Err int
+}
+
+func (i *Inner) NotExpectedInStackTrace() int {
+ if i == nil {
+ return 86
+ }
+ return 17 + i.Err
+}
+
+type Outer struct {
+ Inner
+}
+
+func ExpectedInStackTrace() {
+ var o *Outer
+ println(o.NotExpectedInStackTrace())
+}
+
+func main() {
+ defer func() {
+ if r := recover(); r != nil {
+ stacktrace := string(debug.Stack())
+ if strings.Contains(stacktrace, "NotExpectedInStackTrace") {
+ fmt.Println("FAIL, stacktrace contains NotExpectedInStackTrace")
+ }
+ if !strings.Contains(stacktrace, "ExpectedInStackTrace") {
+ fmt.Println("FAIL, stacktrace does not contain ExpectedInStackTrace")
+ }
+ } else {
+ fmt.Println("FAIL, should have panicked but did not")
+ }
+ }()
+ ExpectedInStackTrace()
+}