1 // Copyright 2023 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.
8 "cmd/compile/internal/ir"
9 "cmd/compile/internal/pgo"
15 type callSiteAnalyzer struct {
18 ptab map[ir.Node]pstate
24 func makeCallSiteAnalyzer(fn *ir.Func, cstab CallSiteTab, ptab map[ir.Node]pstate, loopNestingLevel int) *callSiteAnalyzer {
25 isInit := fn.IsPackageInit() || strings.HasPrefix(fn.Sym().Name, "init.")
26 return &callSiteAnalyzer{
31 loopNest: loopNestingLevel,
32 nstack: []ir.Node{fn},
36 // computeCallSiteTable builds and returns a table of call sites for
37 // the specified region in function fn. A region here corresponds to a
38 // specific subtree within the AST for a function. The main intended
39 // use cases are for 'region' to be either A) an entire function body,
40 // or B) an inlined call expression.
41 func computeCallSiteTable(fn *ir.Func, region ir.Nodes, cstab CallSiteTab, ptab map[ir.Node]pstate, loopNestingLevel int) CallSiteTab {
42 csa := makeCallSiteAnalyzer(fn, cstab, ptab, loopNestingLevel)
43 var doNode func(ir.Node) bool
44 doNode = func(n ir.Node) bool {
46 ir.DoChildren(n, doNode)
50 for _, n := range region {
56 func (csa *callSiteAnalyzer) flagsForNode(call *ir.CallExpr) CSPropBits {
59 if debugTrace&debugTraceCalls != 0 {
60 fmt.Fprintf(os.Stderr, "=-= analyzing call at %s\n",
61 fmtFullPos(call.Pos()))
64 // Set a bit if this call is within a loop.
69 // Set a bit if the call is within an init function (either
70 // compiler-generated or user-written).
72 r |= CallSiteInInitFunc
75 // Decide whether to apply the panic path heuristic. Hack: don't
76 // apply this heuristic in the function "main.main" (mostly just
77 // to avoid annoying users).
78 if !isMainMain(csa.fn) {
79 r = csa.determinePanicPathBits(call, r)
85 // determinePanicPathBits updates the CallSiteOnPanicPath bit within
86 // "r" if we think this call is on an unconditional path to
87 // panic/exit. Do this by walking back up the node stack to see if we
88 // can find either A) an enclosing panic, or B) a statement node that
89 // we've determined leads to a panic/exit.
90 func (csa *callSiteAnalyzer) determinePanicPathBits(call ir.Node, r CSPropBits) CSPropBits {
91 csa.nstack = append(csa.nstack, call)
93 csa.nstack = csa.nstack[:len(csa.nstack)-1]
96 for ri := range csa.nstack[:len(csa.nstack)-1] {
97 i := len(csa.nstack) - ri - 1
99 _, isCallExpr := n.(*ir.CallExpr)
100 _, isStmt := n.(ir.Stmt)
105 if debugTrace&debugTraceCalls != 0 {
106 ps, inps := csa.ptab[n]
107 fmt.Fprintf(os.Stderr, "=-= callpar %d op=%s ps=%s inptab=%v stmt=%v\n", i, n.Op().String(), ps.String(), inps, isStmt)
110 if n.Op() == ir.OPANIC {
111 r |= CallSiteOnPanicPath
114 if v, ok := csa.ptab[n]; ok {
115 if v == psCallsPanic {
116 r |= CallSiteOnPanicPath
127 func (csa *callSiteAnalyzer) addCallSite(callee *ir.Func, call *ir.CallExpr) {
128 flags := csa.flagsForNode(call)
129 // FIXME: maybe bulk-allocate these?
133 Assign: csa.containingAssignment(call),
135 ID: uint(len(csa.cstab)),
137 if _, ok := csa.cstab[call]; ok {
138 fmt.Fprintf(os.Stderr, "*** cstab duplicate entry at: %s\n",
139 fmtFullPos(call.Pos()))
140 fmt.Fprintf(os.Stderr, "*** call: %+v\n", call)
143 if callee.Inl != nil {
144 // Set initial score for callsite to the cost computed
145 // by CanInline; this score will be refined later based
147 cs.Score = int(callee.Inl.Cost)
150 if csa.cstab == nil {
151 csa.cstab = make(CallSiteTab)
154 if debugTrace&debugTraceCalls != 0 {
155 fmt.Fprintf(os.Stderr, "=-= added callsite at %s: callee=%s call[%p]=%v\n", fmtFullPos(call.Pos()), callee.Sym().Name, call, call)
159 func (csa *callSiteAnalyzer) nodeVisitPre(n ir.Node) {
161 case ir.ORANGE, ir.OFOR:
162 if !hasTopLevelLoopBodyReturnOrBreak(loopBody(n)) {
166 ce := n.(*ir.CallExpr)
167 callee := pgo.DirectCallee(ce.Fun)
168 if callee != nil && callee.Inl != nil {
169 csa.addCallSite(callee, ce)
172 csa.nstack = append(csa.nstack, n)
175 func (csa *callSiteAnalyzer) nodeVisitPost(n ir.Node) {
176 csa.nstack = csa.nstack[:len(csa.nstack)-1]
178 case ir.ORANGE, ir.OFOR:
179 if !hasTopLevelLoopBodyReturnOrBreak(loopBody(n)) {
185 func loopBody(n ir.Node) ir.Nodes {
186 if forst, ok := n.(*ir.ForStmt); ok {
189 if rst, ok := n.(*ir.RangeStmt); ok {
195 // hasTopLevelLoopBodyReturnOrBreak examines the body of a "for" or
196 // "range" loop to try to verify that it is a real loop, as opposed to
197 // a construct that is syntactically loopy but doesn't actually iterate
198 // multiple times, like:
205 // [Remark: the pattern above crops up quite a bit in the source code
206 // for the compiler itself, e.g. the auto-generated rewrite code]
208 // Note that we don't look for GOTO statements here, so it's possible
209 // we'll get the wrong result for a loop with complicated control
211 func hasTopLevelLoopBodyReturnOrBreak(loopBody ir.Nodes) bool {
212 for _, n := range loopBody {
213 if n.Op() == ir.ORETURN || n.Op() == ir.OBREAK {
220 // containingAssignment returns the top-level assignment statement
221 // for a statement level function call "n". Examples:
224 // x, y := bar(z, baz())
227 // Here the top-level assignment statement for the foo() call is the
228 // statement assigning to "x"; the top-level assignment for "bar()"
229 // call is the assignment to x,y. For the baz() and blah() calls,
230 // there is no top level assignment statement.
232 // The unstated goal here is that we want to use the containing
233 // assignment to establish a connection between a given call and the
234 // variables to which its results/returns are being assigned.
236 // Note that for the "bar" command above, the front end sometimes
237 // decomposes this into two assignments, the first one assigning the
238 // call to a pair of auto-temps, then the second one assigning the
239 // auto-temps to the user-visible vars. This helper will return the
240 // second (outer) of these two.
241 func (csa *callSiteAnalyzer) containingAssignment(n ir.Node) ir.Node {
242 parent := csa.nstack[len(csa.nstack)-1]
244 // assignsOnlyAutoTemps returns TRUE of the specified OAS2FUNC
245 // node assigns only auto-temps.
246 assignsOnlyAutoTemps := func(x ir.Node) bool {
247 alst := x.(*ir.AssignListStmt)
248 oa2init := alst.Init()
249 if len(oa2init) == 0 {
252 for _, v := range oa2init {
254 if !ir.IsAutoTmp(d.X) {
261 // Simple case: x := foo()
262 if parent.Op() == ir.OAS {
266 // Multi-return case: x, y := bar()
267 if parent.Op() == ir.OAS2FUNC {
268 // Hack city: if the result vars are auto-temps, try looking
269 // for an outer assignment in the tree. The code shape we're
270 // looking for here is:
272 // OAS1({x,y},OCONVNOP(OAS2FUNC({auto1,auto2},OCALLFUNC(bar))))
274 if assignsOnlyAutoTemps(parent) {
275 par2 := csa.nstack[len(csa.nstack)-2]
276 if par2.Op() == ir.OAS2 {
279 if par2.Op() == ir.OCONVNOP {
280 par3 := csa.nstack[len(csa.nstack)-3]
281 if par3.Op() == ir.OAS2 {