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.
4
5package inlheur
6
7// This file defines a set of Go function "properties" intended to
8// guide inlining heuristics; these properties may apply to the
9// function as a whole, or to one or more function return values or
10// parameters.
11//
12// IMPORTANT: function properties are produced on a "best effort"
13// basis, meaning that the code that computes them doesn't verify that
14// the properties are guaranteed to be true in 100% of cases. For this
15// reason, properties should only be used to drive always-safe
16// optimization decisions (e.g. "should I inline this call", or
17// "should I unroll this loop") as opposed to potentially unsafe IR
18// alterations that could change program semantics (e.g. "can I delete
19// this variable" or "can I move this statement to a new location").
20//
21//----------------------------------------------------------------
22
23// FuncProps describes a set of function or method properties that may
24// be useful for inlining heuristics. Here 'Flags' are properties that
25// we think apply to the entire function; 'RecvrParamFlags' are
26// properties of specific function params (or the receiver), and
27// 'ResultFlags' are things properties we think will apply to values
28// of specific results. Note that 'ParamFlags' includes and entry for
29// the receiver if applicable, and does include etries for blank
30// params; for a function such as "func foo(_ int, b byte, _ float32)"
31// the length of ParamFlags will be 3.
32type FuncProps struct {
33	Flags       FuncPropBits
34	ParamFlags  []ParamPropBits // slot 0 receiver if applicable
35	ResultFlags []ResultPropBits
36}
37
38type FuncPropBits uint32
39
40const (
41	// Function always panics or invokes os.Exit() or a func that does
42	// likewise.
43	FuncPropNeverReturns FuncPropBits = 1 << iota
44)
45
46type ParamPropBits uint32
47
48const (
49	// No info about this param
50	ParamNoInfo ParamPropBits = 0
51
52	// Parameter value feeds unmodified into a top-level interface
53	// call (this assumes the parameter is of interface type).
54	ParamFeedsInterfaceMethodCall ParamPropBits = 1 << iota
55
56	// Parameter value feeds unmodified into an interface call that
57	// may be conditional/nested and not always executed (this assumes
58	// the parameter is of interface type).
59	ParamMayFeedInterfaceMethodCall ParamPropBits = 1 << iota
60
61	// Parameter value feeds unmodified into a top level indirect
62	// function call (assumes parameter is of function type).
63	ParamFeedsIndirectCall
64
65	// Parameter value feeds unmodified into an indirect function call
66	// that is conditional/nested (not guaranteed to execute). Assumes
67	// parameter is of function type.
68	ParamMayFeedIndirectCall
69
70	// Parameter value feeds unmodified into a top level "switch"
71	// statement or "if" statement simple expressions (see more on
72	// "simple" expression classification below).
73	ParamFeedsIfOrSwitch
74
75	// Parameter value feeds unmodified into a "switch" or "if"
76	// statement simple expressions (see more on "simple" expression
77	// classification below), where the if/switch is
78	// conditional/nested.
79	ParamMayFeedIfOrSwitch
80)
81
82type ResultPropBits uint32
83
84const (
85	// No info about this result
86	ResultNoInfo ResultPropBits = 0
87	// This result always contains allocated memory.
88	ResultIsAllocatedMem ResultPropBits = 1 << iota
89	// This result is always a single concrete type that is
90	// implicitly converted to interface.
91	ResultIsConcreteTypeConvertedToInterface
92	// Result is always the same non-composite compile time constant.
93	ResultAlwaysSameConstant
94	// Result is always the same function or closure.
95	ResultAlwaysSameFunc
96	// Result is always the same (potentially) inlinable function or closure.
97	ResultAlwaysSameInlinableFunc
98)
99