| |
| |
| |
|
|
| package escape |
|
|
| import ( |
| "cmd/compile/internal/base" |
| "cmd/compile/internal/ir" |
| "cmd/compile/internal/logopt" |
| "cmd/compile/internal/types" |
| "fmt" |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| type location struct { |
| n ir.Node |
| curfn *ir.Func |
| edges []edge |
| loopDepth int |
|
|
| |
| |
| |
| resultIndex int |
|
|
| |
| |
| derefs int |
| walkgen uint32 |
|
|
| |
| |
| |
| dst *location |
| dstEdgeIdx int |
|
|
| |
| |
| queuedWalkAll bool |
|
|
| |
| |
| |
| queuedWalkOne uint32 |
|
|
| |
| attrs locAttr |
|
|
| |
| paramEsc leaks |
|
|
| captured bool |
| reassigned bool |
| addrtaken bool |
| param bool |
| paramOut bool |
| } |
|
|
| type locAttr uint8 |
|
|
| const ( |
| |
| |
| attrEscapes locAttr = 1 << iota |
|
|
| |
| |
| |
| attrPersists |
|
|
| |
| |
| |
| |
| attrMutates |
|
|
| |
| |
| |
| attrCalls |
| ) |
|
|
| func (l *location) hasAttr(attr locAttr) bool { return l.attrs&attr != 0 } |
|
|
| |
| type edge struct { |
| src *location |
| derefs int |
| notes *note |
| } |
|
|
| func (l *location) asHole() hole { |
| return hole{dst: l} |
| } |
|
|
| |
| func (l *location) leakTo(sink *location, derefs int) { |
| |
| |
| |
| if !sink.hasAttr(attrEscapes) && sink.isName(ir.PPARAMOUT) && sink.curfn == l.curfn { |
| ri := sink.resultIndex - 1 |
| if ri < numEscResults { |
| |
| l.paramEsc.AddResult(ri, derefs) |
| return |
| } |
| } |
|
|
| |
| l.paramEsc.AddHeap(derefs) |
| } |
|
|
| func (l *location) isName(c ir.Class) bool { |
| return l.n != nil && l.n.Op() == ir.ONAME && l.n.(*ir.Name).Class == c |
| } |
|
|
| |
| |
| |
| type hole struct { |
| dst *location |
| derefs int |
| notes *note |
|
|
| |
| |
| |
| addrtaken bool |
| } |
|
|
| type note struct { |
| next *note |
| where ir.Node |
| why string |
| } |
|
|
| func (k hole) note(where ir.Node, why string) hole { |
| if where == nil || why == "" { |
| base.Fatalf("note: missing where/why") |
| } |
| if base.Flag.LowerM >= 2 || logopt.Enabled() { |
| k.notes = ¬e{ |
| next: k.notes, |
| where: where, |
| why: why, |
| } |
| } |
| return k |
| } |
|
|
| func (k hole) shift(delta int) hole { |
| k.derefs += delta |
| if k.derefs < -1 { |
| base.Fatalf("derefs underflow: %v", k.derefs) |
| } |
| k.addrtaken = delta < 0 |
| return k |
| } |
|
|
| func (k hole) deref(where ir.Node, why string) hole { return k.shift(1).note(where, why) } |
| func (k hole) addr(where ir.Node, why string) hole { return k.shift(-1).note(where, why) } |
|
|
| func (k hole) dotType(t *types.Type, where ir.Node, why string) hole { |
| if !t.IsInterface() && !types.IsDirectIface(t) { |
| k = k.shift(1) |
| } |
| return k.note(where, why) |
| } |
|
|
| func (b *batch) flow(k hole, src *location) { |
| if k.addrtaken { |
| src.addrtaken = true |
| } |
|
|
| dst := k.dst |
| if dst == &b.blankLoc { |
| return |
| } |
| if dst == src && k.derefs >= 0 { |
| return |
| } |
| if dst.hasAttr(attrEscapes) && k.derefs < 0 { |
| if base.Flag.LowerM >= 2 || logopt.Enabled() { |
| pos := base.FmtPos(src.n.Pos()) |
| if base.Flag.LowerM >= 2 { |
| fmt.Printf("%s: %v escapes to heap in %v:\n", pos, src.n, ir.FuncName(src.curfn)) |
| } |
| explanation := b.explainFlow(pos, dst, src, k.derefs, k.notes, []*logopt.LoggedOpt{}) |
| if logopt.Enabled() { |
| var e_curfn *ir.Func |
| logopt.LogOpt(src.n.Pos(), "escapes", "escape", ir.FuncName(e_curfn), fmt.Sprintf("%v escapes to heap", src.n), explanation) |
| } |
|
|
| } |
| src.attrs |= attrEscapes | attrPersists | attrMutates | attrCalls |
| return |
| } |
|
|
| |
| dst.edges = append(dst.edges, edge{src: src, derefs: k.derefs, notes: k.notes}) |
| } |
|
|
| func (b *batch) heapHole() hole { return b.heapLoc.asHole() } |
| func (b *batch) mutatorHole() hole { return b.mutatorLoc.asHole() } |
| func (b *batch) calleeHole() hole { return b.calleeLoc.asHole() } |
| func (b *batch) discardHole() hole { return b.blankLoc.asHole() } |
|
|
| func (b *batch) oldLoc(n *ir.Name) *location { |
| if n.Canonical().Opt == nil { |
| base.FatalfAt(n.Pos(), "%v has no location", n) |
| } |
| return n.Canonical().Opt.(*location) |
| } |
|
|
| func (e *escape) newLoc(n ir.Node, persists bool) *location { |
| if e.curfn == nil { |
| base.Fatalf("e.curfn isn't set") |
| } |
| if n != nil && n.Type() != nil && n.Type().NotInHeap() { |
| base.ErrorfAt(n.Pos(), 0, "%v is incomplete (or unallocatable); stack allocation disallowed", n.Type()) |
| } |
|
|
| if n != nil && n.Op() == ir.ONAME { |
| if canon := n.(*ir.Name).Canonical(); n != canon { |
| base.FatalfAt(n.Pos(), "newLoc on non-canonical %v (canonical is %v)", n, canon) |
| } |
| } |
| loc := &location{ |
| n: n, |
| curfn: e.curfn, |
| loopDepth: e.loopDepth, |
| } |
| if loc.isName(ir.PPARAM) { |
| loc.param = true |
| } else if loc.isName(ir.PPARAMOUT) { |
| loc.paramOut = true |
| } |
|
|
| if persists { |
| loc.attrs |= attrPersists |
| } |
| e.allLocs = append(e.allLocs, loc) |
| if n != nil { |
| if n.Op() == ir.ONAME { |
| n := n.(*ir.Name) |
| if n.Class == ir.PPARAM && n.Curfn == nil { |
| |
| } else if n.Curfn != e.curfn { |
| base.FatalfAt(n.Pos(), "curfn mismatch: %v != %v for %v", n.Curfn, e.curfn, n) |
| } |
|
|
| if n.Opt != nil { |
| base.FatalfAt(n.Pos(), "%v already has a location", n) |
| } |
| n.Opt = loc |
| } |
| } |
| return loc |
| } |
|
|
| |
| |
| func (e *escape) teeHole(ks ...hole) hole { |
| if len(ks) == 0 { |
| return e.discardHole() |
| } |
| if len(ks) == 1 { |
| return ks[0] |
| } |
| |
|
|
| |
| |
| |
| loc := e.newLoc(nil, false) |
| for _, k := range ks { |
| |
| |
| |
| |
| |
| if k.derefs < 0 { |
| base.Fatalf("teeHole: negative derefs") |
| } |
|
|
| e.flow(k, loc) |
| } |
| return loc.asHole() |
| } |
|
|
| |
| |
| |
| func (e *escape) later(k hole) hole { |
| loc := e.newLoc(nil, true) |
| e.flow(k, loc) |
| return loc.asHole() |
| } |
|
|
| |
| func Fmt(n ir.Node) string { |
| text := "" |
| switch n.Esc() { |
| case ir.EscUnknown: |
| break |
|
|
| case ir.EscHeap: |
| text = "esc(h)" |
|
|
| case ir.EscNone: |
| text = "esc(no)" |
|
|
| case ir.EscNever: |
| text = "esc(N)" |
|
|
| default: |
| text = fmt.Sprintf("esc(%d)", n.Esc()) |
| } |
|
|
| if n.Op() == ir.ONAME { |
| n := n.(*ir.Name) |
| if loc, ok := n.Opt.(*location); ok && loc.loopDepth != 0 { |
| if text != "" { |
| text += " " |
| } |
| text += fmt.Sprintf("ld(%d)", loc.loopDepth) |
| } |
| } |
|
|
| return text |
| } |
|
|