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/base"
9 "cmd/compile/internal/ir"
18 // CallSite records useful information about a potentially inlinable
19 // (direct) function call. "Callee" is the target of the call, "Call"
20 // is the ir node corresponding to the call itself, "Assign" is
21 // the top-level assignment statement containing the call (if the call
22 // appears in the form of a top-level statement, e.g. "x := foo()"),
23 // "Flags" contains properties of the call that might be useful for
24 // making inlining decisions, "Score" is the final score assigned to
25 // the site, and "ID" is a numeric ID for the site within its
26 // containing function.
27 type CallSite struct {
33 ScoreMask scoreAdjustTyp
37 // CallSiteTab is a table of call sites, keyed by call expr.
38 // Ideally it would be nice to key the table by src.XPos, but
39 // this results in collisions for calls on very long lines (the
40 // front end saturates column numbers at 255). We also wind up
41 // with many calls that share the same auto-generated pos.
42 type CallSiteTab map[*ir.CallExpr]*CallSite
44 type CSPropBits uint32
47 CallSiteInLoop CSPropBits = 1 << iota
52 // encodedCallSiteTab is a table keyed by "encoded" callsite
53 // (stringified src.XPos plus call site ID) mapping to a value of call
54 // property bits and score.
55 type encodedCallSiteTab map[string]propsAndScore
57 type propsAndScore struct {
63 func (pas propsAndScore) String() string {
64 return fmt.Sprintf("P=%s|S=%d|M=%s", pas.props.String(),
65 pas.score, pas.mask.String())
68 func (cst CallSiteTab) merge(other CallSiteTab) error {
69 for k, v := range other {
70 if prev, ok := cst[k]; ok {
71 return fmt.Errorf("internal error: collision during call site table merge, fn=%s callsite=%s", prev.Callee.Sym().Name, fmtFullPos(prev.Call.Pos()))
78 func fmtFullPos(p src.XPos) string {
79 var sb strings.Builder
81 base.Ctxt.AllPos(p, func(pos src.Pos) {
84 file := filepath.Base(pos.Filename())
85 fmt.Fprintf(&sb, "%s:%d:%d", file, pos.Line(), pos.Col())
90 func EncodeCallSiteKey(cs *CallSite) string {
91 var sb strings.Builder
92 // FIXME: maybe rewrite line offsets relative to function start?
93 sb.WriteString(fmtFullPos(cs.Call.Pos()))
94 fmt.Fprintf(&sb, "|%d", cs.ID)
98 func buildEncodedCallSiteTab(tab CallSiteTab) encodedCallSiteTab {
99 r := make(encodedCallSiteTab)
100 for _, cs := range tab {
101 k := EncodeCallSiteKey(cs)
102 r[k] = propsAndScore{
111 // dumpCallSiteComments emits comments into the dump file for the
112 // callsites in the function of interest. If "ecst" is non-nil, we use
113 // that, otherwise generated a fresh encodedCallSiteTab from "tab".
114 func dumpCallSiteComments(w io.Writer, tab CallSiteTab, ecst encodedCallSiteTab) {
116 ecst = buildEncodedCallSiteTab(tab)
118 tags := make([]string, 0, len(ecst))
119 for k := range ecst {
120 tags = append(tags, k)
123 for _, s := range tags {
125 fmt.Fprintf(w, "// callsite: %s flagstr %q flagval %d score %d mask %d maskstr %q\n", s, v.props.String(), v.props, v.score, v.mask, v.mask.String())
127 fmt.Fprintf(w, "// %s\n", csDelimiter)