|
| 1 | +// Copyright 2011 The Go Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package gc |
| 6 | + |
| 7 | +// Strongly connected components. |
| 8 | +// |
| 9 | +// Run analysis on minimal sets of mutually recursive functions |
| 10 | +// or single non-recursive functions, bottom up. |
| 11 | +// |
| 12 | +// Finding these sets is finding strongly connected components |
| 13 | +// by reverse topological order in the static call graph. |
| 14 | +// The algorithm (known as Tarjan's algorithm) for doing that is taken from |
| 15 | +// Sedgewick, Algorithms, Second Edition, p. 482, with two adaptations. |
| 16 | +// |
| 17 | +// First, a hidden closure function (n.Func.IsHiddenClosure()) cannot be the |
| 18 | +// root of a connected component. Refusing to use it as a root |
| 19 | +// forces it into the component of the function in which it appears. |
| 20 | +// This is more convenient for escape analysis. |
| 21 | +// |
| 22 | +// Second, each function becomes two virtual nodes in the graph, |
| 23 | +// with numbers n and n+1. We record the function's node number as n |
| 24 | +// but search from node n+1. If the search tells us that the component |
| 25 | +// number (min) is n+1, we know that this is a trivial component: one function |
| 26 | +// plus its closures. If the search tells us that the component number is |
| 27 | +// n, then there was a path from node n+1 back to node n, meaning that |
| 28 | +// the function set is mutually recursive. The escape analysis can be |
| 29 | +// more precise when analyzing a single non-recursive function than |
| 30 | +// when analyzing a set of mutually recursive functions. |
| 31 | + |
| 32 | +type bottomUpVisitor struct { |
| 33 | + analyze func([]*Node, bool) |
| 34 | + visitgen uint32 |
| 35 | + nodeID map[*Node]uint32 |
| 36 | + stack []*Node |
| 37 | +} |
| 38 | + |
| 39 | +// visitBottomUp invokes analyze on the ODCLFUNC nodes listed in list. |
| 40 | +// It calls analyze with successive groups of functions, working from |
| 41 | +// the bottom of the call graph upward. Each time analyze is called with |
| 42 | +// a list of functions, every function on that list only calls other functions |
| 43 | +// on the list or functions that have been passed in previous invocations of |
| 44 | +// analyze. Closures appear in the same list as their outer functions. |
| 45 | +// The lists are as short as possible while preserving those requirements. |
| 46 | +// (In a typical program, many invocations of analyze will be passed just |
| 47 | +// a single function.) The boolean argument 'recursive' passed to analyze |
| 48 | +// specifies whether the functions on the list are mutually recursive. |
| 49 | +// If recursive is false, the list consists of only a single function and its closures. |
| 50 | +// If recursive is true, the list may still contain only a single function, |
| 51 | +// if that function is itself recursive. |
| 52 | +func visitBottomUp(list []*Node, analyze func(list []*Node, recursive bool)) { |
| 53 | + var v bottomUpVisitor |
| 54 | + v.analyze = analyze |
| 55 | + v.nodeID = make(map[*Node]uint32) |
| 56 | + for _, n := range list { |
| 57 | + if n.Op == ODCLFUNC && !n.Func.IsHiddenClosure() { |
| 58 | + v.visit(n) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func (v *bottomUpVisitor) visit(n *Node) uint32 { |
| 64 | + if id := v.nodeID[n]; id > 0 { |
| 65 | + // already visited |
| 66 | + return id |
| 67 | + } |
| 68 | + |
| 69 | + v.visitgen++ |
| 70 | + id := v.visitgen |
| 71 | + v.nodeID[n] = id |
| 72 | + v.visitgen++ |
| 73 | + min := v.visitgen |
| 74 | + |
| 75 | + v.stack = append(v.stack, n) |
| 76 | + min = v.visitcodelist(n.Nbody, min) |
| 77 | + if (min == id || min == id+1) && !n.Func.IsHiddenClosure() { |
| 78 | + // This node is the root of a strongly connected component. |
| 79 | + |
| 80 | + // The original min passed to visitcodelist was v.nodeID[n]+1. |
| 81 | + // If visitcodelist found its way back to v.nodeID[n], then this |
| 82 | + // block is a set of mutually recursive functions. |
| 83 | + // Otherwise it's just a lone function that does not recurse. |
| 84 | + recursive := min == id |
| 85 | + |
| 86 | + // Remove connected component from stack. |
| 87 | + // Mark walkgen so that future visits return a large number |
| 88 | + // so as not to affect the caller's min. |
| 89 | + |
| 90 | + var i int |
| 91 | + for i = len(v.stack) - 1; i >= 0; i-- { |
| 92 | + x := v.stack[i] |
| 93 | + if x == n { |
| 94 | + break |
| 95 | + } |
| 96 | + v.nodeID[x] = ^uint32(0) |
| 97 | + } |
| 98 | + v.nodeID[n] = ^uint32(0) |
| 99 | + block := v.stack[i:] |
| 100 | + // Run escape analysis on this set of functions. |
| 101 | + v.stack = v.stack[:i] |
| 102 | + v.analyze(block, recursive) |
| 103 | + } |
| 104 | + |
| 105 | + return min |
| 106 | +} |
| 107 | + |
| 108 | +func (v *bottomUpVisitor) visitcodelist(l Nodes, min uint32) uint32 { |
| 109 | + for _, n := range l.Slice() { |
| 110 | + min = v.visitcode(n, min) |
| 111 | + } |
| 112 | + return min |
| 113 | +} |
| 114 | + |
| 115 | +func (v *bottomUpVisitor) visitcode(n *Node, min uint32) uint32 { |
| 116 | + if n == nil { |
| 117 | + return min |
| 118 | + } |
| 119 | + |
| 120 | + min = v.visitcodelist(n.Ninit, min) |
| 121 | + min = v.visitcode(n.Left, min) |
| 122 | + min = v.visitcode(n.Right, min) |
| 123 | + min = v.visitcodelist(n.List, min) |
| 124 | + min = v.visitcodelist(n.Nbody, min) |
| 125 | + min = v.visitcodelist(n.Rlist, min) |
| 126 | + |
| 127 | + switch n.Op { |
| 128 | + case OCALLFUNC, OCALLMETH: |
| 129 | + fn := asNode(n.Left.Type.Nname()) |
| 130 | + if fn != nil && fn.Op == ONAME && fn.Class() == PFUNC && fn.Name.Defn != nil { |
| 131 | + m := v.visit(fn.Name.Defn) |
| 132 | + if m < min { |
| 133 | + min = m |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + case OCLOSURE: |
| 138 | + m := v.visit(n.Func.Closure) |
| 139 | + if m < min { |
| 140 | + min = m |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return min |
| 145 | +} |
0 commit comments