| |
| |
| |
|
|
| package inlheur |
|
|
| import ( |
| "cmd/compile/internal/base" |
| "cmd/compile/internal/ir" |
| "cmd/internal/src" |
| "fmt" |
| "io" |
| "path/filepath" |
| "sort" |
| "strings" |
| ) |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| type CallSite struct { |
| Callee *ir.Func |
| Call *ir.CallExpr |
| parent *CallSite |
| Assign ir.Node |
| Flags CSPropBits |
|
|
| ArgProps []ActualExprPropBits |
| Score int |
| ScoreMask scoreAdjustTyp |
| ID uint |
| aux uint8 |
| } |
|
|
| |
| |
| |
| |
| |
| type CallSiteTab map[*ir.CallExpr]*CallSite |
|
|
| |
| |
| type ActualExprPropBits uint8 |
|
|
| const ( |
| ActualExprConstant ActualExprPropBits = 1 << iota |
| ActualExprIsConcreteConvIface |
| ActualExprIsFunc |
| ActualExprIsInlinableFunc |
| ) |
|
|
| type CSPropBits uint32 |
|
|
| const ( |
| CallSiteInLoop CSPropBits = 1 << iota |
| CallSiteOnPanicPath |
| CallSiteInInitFunc |
| ) |
|
|
| type csAuxBits uint8 |
|
|
| const ( |
| csAuxInlined = 1 << iota |
| ) |
|
|
| |
| |
| |
| type encodedCallSiteTab map[string]propsAndScore |
|
|
| type propsAndScore struct { |
| props CSPropBits |
| score int |
| mask scoreAdjustTyp |
| } |
|
|
| func (pas propsAndScore) String() string { |
| return fmt.Sprintf("P=%s|S=%d|M=%s", pas.props.String(), |
| pas.score, pas.mask.String()) |
| } |
|
|
| func (cst CallSiteTab) merge(other CallSiteTab) error { |
| for k, v := range other { |
| if prev, ok := cst[k]; ok { |
| return fmt.Errorf("internal error: collision during call site table merge, fn=%s callsite=%s", prev.Callee.Sym().Name, fmtFullPos(prev.Call.Pos())) |
| } |
| cst[k] = v |
| } |
| return nil |
| } |
|
|
| func fmtFullPos(p src.XPos) string { |
| var sb strings.Builder |
| sep := "" |
| base.Ctxt.AllPos(p, func(pos src.Pos) { |
| sb.WriteString(sep) |
| sep = "|" |
| file := filepath.Base(pos.Filename()) |
| fmt.Fprintf(&sb, "%s:%d:%d", file, pos.Line(), pos.Col()) |
| }) |
| return sb.String() |
| } |
|
|
| func EncodeCallSiteKey(cs *CallSite) string { |
| var sb strings.Builder |
| |
| sb.WriteString(fmtFullPos(cs.Call.Pos())) |
| fmt.Fprintf(&sb, "|%d", cs.ID) |
| return sb.String() |
| } |
|
|
| func buildEncodedCallSiteTab(tab CallSiteTab) encodedCallSiteTab { |
| r := make(encodedCallSiteTab) |
| for _, cs := range tab { |
| k := EncodeCallSiteKey(cs) |
| r[k] = propsAndScore{ |
| props: cs.Flags, |
| score: cs.Score, |
| mask: cs.ScoreMask, |
| } |
| } |
| return r |
| } |
|
|
| |
| |
| |
| func dumpCallSiteComments(w io.Writer, tab CallSiteTab, ecst encodedCallSiteTab) { |
| if ecst == nil { |
| ecst = buildEncodedCallSiteTab(tab) |
| } |
| tags := make([]string, 0, len(ecst)) |
| for k := range ecst { |
| tags = append(tags, k) |
| } |
| sort.Strings(tags) |
| for _, s := range tags { |
| v := ecst[s] |
| 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()) |
| } |
| fmt.Fprintf(w, "// %s\n", csDelimiter) |
| } |
|
|