ADAPT-Chase commited on
Commit
6a16f9d
·
verified ·
1 Parent(s): 47feb9b

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/a.go +14 -0
  2. platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/b.go +11 -0
  3. platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/exports.go +91 -0
  4. platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/generics.go +29 -0
  5. platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/issue15920.go +11 -0
  6. platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/issue20046.go +9 -0
  7. platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/issue25301.go +17 -0
  8. platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/issue25596.go +13 -0
  9. platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/p.go +13 -0
  10. platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/versions/test.go +28 -0
  11. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/actualexprpropbits_string.go +58 -0
  12. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/analyze.go +370 -0
  13. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/analyze_func_callsites.go +413 -0
  14. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/analyze_func_flags.go +356 -0
  15. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/analyze_func_params.go +355 -0
  16. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/analyze_func_returns.go +277 -0
  17. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/callsite.go +149 -0
  18. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/cspropbits_string.go +56 -0
  19. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/debugflags_test.go +65 -0
  20. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/dumpscores_test.go +109 -0
  21. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/eclassify.go +247 -0
  22. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/funcprop_string.go +44 -0
  23. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/funcpropbits_string.go +58 -0
  24. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/funcprops_test.go +530 -0
  25. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/function_properties.go +98 -0
  26. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/names.go +129 -0
  27. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/parampropbits_string.go +70 -0
  28. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/pstate_string.go +30 -0
  29. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/resultpropbits_string.go +68 -0
  30. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/score_callresult_uses.go +413 -0
  31. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/scoreadjusttyp_string.go +80 -0
  32. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/scoring.go +751 -0
  33. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/serialize.go +80 -0
  34. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/dumpscores.go +45 -0
  35. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/props/README.txt +77 -0
  36. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/props/acrosscall.go +214 -0
  37. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/props/calls.go +240 -0
  38. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/props/funcflags.go +341 -0
  39. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/props/params.go +367 -0
  40. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/props/returns.go +370 -0
  41. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/props/returns2.go +231 -0
  42. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/texpr_classify_test.go +217 -0
  43. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/trace_off.go +18 -0
  44. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/trace_on.go +40 -0
  45. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/tserial_test.go +65 -0
  46. platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/interleaved/interleaved.go +132 -0
  47. platform/dbops/binaries/go/go/src/cmd/compile/internal/loopvar/testdata/for_complicated_esc_address.go +115 -0
  48. platform/dbops/binaries/go/go/src/cmd/compile/internal/loopvar/testdata/for_esc_address.go +45 -0
  49. platform/dbops/binaries/go/go/src/cmd/compile/internal/loopvar/testdata/for_esc_closure.go +51 -0
  50. platform/dbops/binaries/go/go/src/cmd/compile/internal/loopvar/testdata/for_esc_method.go +51 -0
platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/a.go ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2016 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
+ // Input for TestIssue13566
6
+
7
+ package a
8
+
9
+ import "encoding/json"
10
+
11
+ type A struct {
12
+ a *A
13
+ json json.RawMessage
14
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/b.go ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2016 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
+ // Input for TestIssue13566
6
+
7
+ package b
8
+
9
+ import "./a"
10
+
11
+ type A a.A
platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/exports.go ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ // This file is used to generate an object file which
6
+ // serves as test file for gcimporter_test.go.
7
+
8
+ package exports
9
+
10
+ import "go/ast"
11
+
12
+ // Issue 3682: Correctly read dotted identifiers from export data.
13
+ const init1 = 0
14
+
15
+ func init() {}
16
+
17
+ const (
18
+ C0 int = 0
19
+ C1 = 3.14159265
20
+ C2 = 2.718281828i
21
+ C3 = -123.456e-789
22
+ C4 = +123.456e+789
23
+ C5 = 1234i
24
+ C6 = "foo\n"
25
+ C7 = `bar\n`
26
+ C8 = 42
27
+ C9 int = 42
28
+ C10 float64 = 42
29
+ )
30
+
31
+ type (
32
+ T1 int
33
+ T2 [10]int
34
+ T3 []int
35
+ T4 *int
36
+ T5 chan int
37
+ T6a chan<- int
38
+ T6b chan (<-chan int)
39
+ T6c chan<- (chan int)
40
+ T7 <-chan *ast.File
41
+ T8 struct{}
42
+ T9 struct {
43
+ a int
44
+ b, c float32
45
+ d []string `go:"tag"`
46
+ }
47
+ T10 struct {
48
+ T8
49
+ T9
50
+ _ *T10
51
+ }
52
+ T11 map[int]string
53
+ T12 interface{}
54
+ T13 interface {
55
+ m1()
56
+ m2(int) float32
57
+ }
58
+ T14 interface {
59
+ T12
60
+ T13
61
+ m3(x ...struct{}) []T9
62
+ }
63
+ T15 func()
64
+ T16 func(int)
65
+ T17 func(x int)
66
+ T18 func() float32
67
+ T19 func() (x float32)
68
+ T20 func(...interface{})
69
+ T21 struct{ next *T21 }
70
+ T22 struct{ link *T23 }
71
+ T23 struct{ link *T22 }
72
+ T24 *T24
73
+ T25 *T26
74
+ T26 *T27
75
+ T27 *T25
76
+ T28 func(T28) T28
77
+ )
78
+
79
+ var (
80
+ V0 int
81
+ V1 = -991.0
82
+ V2 float32 = 1.2
83
+ )
84
+
85
+ func F1() {}
86
+ func F2(x int) {}
87
+ func F3() int { return 0 }
88
+ func F4() float32 { return 0 }
89
+ func F5(a, b, c int, u, v, w struct{ x, y T1 }, more ...interface{}) (p, q, r chan<- T10)
90
+
91
+ func (p *T1) M1()
platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/generics.go ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2021 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
+ // This file is used to generate an object file which
6
+ // serves as test file for gcimporter_test.go.
7
+
8
+ package generics
9
+
10
+ type Any any
11
+
12
+ var x any
13
+
14
+ type T[A, B any] struct {
15
+ Left A
16
+ Right B
17
+ }
18
+
19
+ var X T[int, string] = T[int, string]{1, "hi"}
20
+
21
+ func ToInt[P interface{ ~int }](p P) int { return int(p) }
22
+
23
+ var IntID = ToInt[int]
24
+
25
+ type G[C comparable] int
26
+
27
+ func ImplicitFunc[T ~int]() {}
28
+
29
+ type ImplicitType[T ~int] int
platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/issue15920.go ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2016 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 p
6
+
7
+ // The underlying type of Error is the underlying type of error.
8
+ // Make sure we can import this again without problems.
9
+ type Error error
10
+
11
+ func F() Error { return nil }
platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/issue20046.go ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2017 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 p
6
+
7
+ var V interface {
8
+ M()
9
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/issue25301.go ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 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 issue25301
6
+
7
+ type (
8
+ A = interface {
9
+ M()
10
+ }
11
+ T interface {
12
+ A
13
+ }
14
+ S struct{}
15
+ )
16
+
17
+ func (S) M() { println("m") }
platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/issue25596.go ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2018 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 issue25596
6
+
7
+ type E interface {
8
+ M() T
9
+ }
10
+
11
+ type T interface {
12
+ E
13
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/p.go ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2016 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
+ // Input for TestIssue15517
6
+
7
+ package p
8
+
9
+ const C = 0
10
+
11
+ var V int
12
+
13
+ func F() {}
platform/dbops/binaries/go/go/src/cmd/compile/internal/importer/testdata/versions/test.go ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright 2016 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
+ // To create a test case for a new export format version,
6
+ // build this package with the latest compiler and store
7
+ // the resulting .a file appropriately named in the versions
8
+ // directory. The VersionHandling test will pick it up.
9
+ //
10
+ // In the testdata/versions:
11
+ //
12
+ // go build -o test_go1.$X_$Y.a test.go
13
+ //
14
+ // with $X = Go version and $Y = export format version
15
+ // (add 'b' or 'i' to distinguish between binary and
16
+ // indexed format starting with 1.11 as long as both
17
+ // formats are supported).
18
+ //
19
+ // Make sure this source is extended such that it exercises
20
+ // whatever export format change has taken place.
21
+
22
+ package test
23
+
24
+ // Any release before and including Go 1.7 didn't encode
25
+ // the package for a blank struct field.
26
+ type BlankField struct {
27
+ _ int
28
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/actualexprpropbits_string.go ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Code generated by "stringer -bitset -type ActualExprPropBits"; DO NOT EDIT.
2
+
3
+ package inlheur
4
+
5
+ import "strconv"
6
+ import "bytes"
7
+
8
+ func _() {
9
+ // An "invalid array index" compiler error signifies that the constant values have changed.
10
+ // Re-run the stringer command to generate them again.
11
+ var x [1]struct{}
12
+ _ = x[ActualExprConstant-1]
13
+ _ = x[ActualExprIsConcreteConvIface-2]
14
+ _ = x[ActualExprIsFunc-4]
15
+ _ = x[ActualExprIsInlinableFunc-8]
16
+ }
17
+
18
+ var _ActualExprPropBits_value = [...]uint64{
19
+ 0x1, /* ActualExprConstant */
20
+ 0x2, /* ActualExprIsConcreteConvIface */
21
+ 0x4, /* ActualExprIsFunc */
22
+ 0x8, /* ActualExprIsInlinableFunc */
23
+ }
24
+
25
+ const _ActualExprPropBits_name = "ActualExprConstantActualExprIsConcreteConvIfaceActualExprIsFuncActualExprIsInlinableFunc"
26
+
27
+ var _ActualExprPropBits_index = [...]uint8{0, 18, 47, 63, 88}
28
+
29
+ func (i ActualExprPropBits) String() string {
30
+ var b bytes.Buffer
31
+
32
+ remain := uint64(i)
33
+ seen := false
34
+
35
+ for k, v := range _ActualExprPropBits_value {
36
+ x := _ActualExprPropBits_name[_ActualExprPropBits_index[k]:_ActualExprPropBits_index[k+1]]
37
+ if v == 0 {
38
+ if i == 0 {
39
+ b.WriteString(x)
40
+ return b.String()
41
+ }
42
+ continue
43
+ }
44
+ if (v & remain) == v {
45
+ remain &^= v
46
+ x := _ActualExprPropBits_name[_ActualExprPropBits_index[k]:_ActualExprPropBits_index[k+1]]
47
+ if seen {
48
+ b.WriteString("|")
49
+ }
50
+ seen = true
51
+ b.WriteString(x)
52
+ }
53
+ }
54
+ if remain == 0 {
55
+ return b.String()
56
+ }
57
+ return "ActualExprPropBits(0x" + strconv.FormatInt(int64(i), 16) + ")"
58
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/analyze.go ADDED
@@ -0,0 +1,370 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import (
8
+ "cmd/compile/internal/base"
9
+ "cmd/compile/internal/ir"
10
+ "cmd/compile/internal/types"
11
+ "encoding/json"
12
+ "fmt"
13
+ "internal/buildcfg"
14
+ "io"
15
+ "os"
16
+ "path/filepath"
17
+ "sort"
18
+ "strings"
19
+ )
20
+
21
+ const (
22
+ debugTraceFuncs = 1 << iota
23
+ debugTraceFuncFlags
24
+ debugTraceResults
25
+ debugTraceParams
26
+ debugTraceExprClassify
27
+ debugTraceCalls
28
+ debugTraceScoring
29
+ )
30
+
31
+ // propAnalyzer interface is used for defining one or more analyzer
32
+ // helper objects, each tasked with computing some specific subset of
33
+ // the properties we're interested in. The assumption is that
34
+ // properties are independent, so each new analyzer that implements
35
+ // this interface can operate entirely on its own. For a given analyzer
36
+ // there will be a sequence of calls to nodeVisitPre and nodeVisitPost
37
+ // as the nodes within a function are visited, then a followup call to
38
+ // setResults so that the analyzer can transfer its results into the
39
+ // final properties object.
40
+ type propAnalyzer interface {
41
+ nodeVisitPre(n ir.Node)
42
+ nodeVisitPost(n ir.Node)
43
+ setResults(funcProps *FuncProps)
44
+ }
45
+
46
+ // fnInlHeur contains inline heuristics state information about a
47
+ // specific Go function being analyzed/considered by the inliner. Note
48
+ // that in addition to constructing a fnInlHeur object by analyzing a
49
+ // specific *ir.Func, there is also code in the test harness
50
+ // (funcprops_test.go) that builds up fnInlHeur's by reading in and
51
+ // parsing a dump. This is the reason why we have file/fname/line
52
+ // fields below instead of just an *ir.Func field.
53
+ type fnInlHeur struct {
54
+ props *FuncProps
55
+ cstab CallSiteTab
56
+ fname string
57
+ file string
58
+ line uint
59
+ }
60
+
61
+ var fpmap = map[*ir.Func]fnInlHeur{}
62
+
63
+ // AnalyzeFunc computes function properties for fn and its contained
64
+ // closures, updating the global 'fpmap' table. It is assumed that
65
+ // "CanInline" has been run on fn and on the closures that feed
66
+ // directly into calls; other closures not directly called will also
67
+ // be checked inlinability for inlinability here in case they are
68
+ // returned as a result.
69
+ func AnalyzeFunc(fn *ir.Func, canInline func(*ir.Func), budgetForFunc func(*ir.Func) int32, inlineMaxBudget int) {
70
+ if fpmap == nil {
71
+ // If fpmap is nil this indicates that the main inliner pass is
72
+ // complete and we're doing inlining of wrappers (no heuristics
73
+ // used here).
74
+ return
75
+ }
76
+ if fn.OClosure != nil {
77
+ // closures will be processed along with their outer enclosing func.
78
+ return
79
+ }
80
+ enableDebugTraceIfEnv()
81
+ if debugTrace&debugTraceFuncs != 0 {
82
+ fmt.Fprintf(os.Stderr, "=-= AnalyzeFunc(%v)\n", fn)
83
+ }
84
+ // Build up a list containing 'fn' and any closures it contains. Along
85
+ // the way, test to see whether each closure is inlinable in case
86
+ // we might be returning it.
87
+ funcs := []*ir.Func{fn}
88
+ ir.VisitFuncAndClosures(fn, func(n ir.Node) {
89
+ if clo, ok := n.(*ir.ClosureExpr); ok {
90
+ funcs = append(funcs, clo.Func)
91
+ }
92
+ })
93
+
94
+ // Analyze the list of functions. We want to visit a given func
95
+ // only after the closures it contains have been processed, so
96
+ // iterate through the list in reverse order. Once a function has
97
+ // been analyzed, revisit the question of whether it should be
98
+ // inlinable; if it is over the default hairyness limit and it
99
+ // doesn't have any interesting properties, then we don't want
100
+ // the overhead of writing out its inline body.
101
+ nameFinder := newNameFinder(fn)
102
+ for i := len(funcs) - 1; i >= 0; i-- {
103
+ f := funcs[i]
104
+ if f.OClosure != nil && !f.InlinabilityChecked() {
105
+ canInline(f)
106
+ }
107
+ funcProps := analyzeFunc(f, inlineMaxBudget, nameFinder)
108
+ revisitInlinability(f, funcProps, budgetForFunc)
109
+ if f.Inl != nil {
110
+ f.Inl.Properties = funcProps.SerializeToString()
111
+ }
112
+ }
113
+ disableDebugTrace()
114
+ }
115
+
116
+ // TearDown is invoked at the end of the main inlining pass; doing
117
+ // function analysis and call site scoring is unlikely to help a lot
118
+ // after this point, so nil out fpmap and other globals to reclaim
119
+ // storage.
120
+ func TearDown() {
121
+ fpmap = nil
122
+ scoreCallsCache.tab = nil
123
+ scoreCallsCache.csl = nil
124
+ }
125
+
126
+ func analyzeFunc(fn *ir.Func, inlineMaxBudget int, nf *nameFinder) *FuncProps {
127
+ if funcInlHeur, ok := fpmap[fn]; ok {
128
+ return funcInlHeur.props
129
+ }
130
+ funcProps, fcstab := computeFuncProps(fn, inlineMaxBudget, nf)
131
+ file, line := fnFileLine(fn)
132
+ entry := fnInlHeur{
133
+ fname: fn.Sym().Name,
134
+ file: file,
135
+ line: line,
136
+ props: funcProps,
137
+ cstab: fcstab,
138
+ }
139
+ fn.SetNeverReturns(entry.props.Flags&FuncPropNeverReturns != 0)
140
+ fpmap[fn] = entry
141
+ if fn.Inl != nil && fn.Inl.Properties == "" {
142
+ fn.Inl.Properties = entry.props.SerializeToString()
143
+ }
144
+ return funcProps
145
+ }
146
+
147
+ // revisitInlinability revisits the question of whether to continue to
148
+ // treat function 'fn' as an inline candidate based on the set of
149
+ // properties we've computed for it. If (for example) it has an
150
+ // initial size score of 150 and no interesting properties to speak
151
+ // of, then there isn't really any point to moving ahead with it as an
152
+ // inline candidate.
153
+ func revisitInlinability(fn *ir.Func, funcProps *FuncProps, budgetForFunc func(*ir.Func) int32) {
154
+ if fn.Inl == nil {
155
+ return
156
+ }
157
+ maxAdj := int32(LargestNegativeScoreAdjustment(fn, funcProps))
158
+ budget := budgetForFunc(fn)
159
+ if fn.Inl.Cost+maxAdj > budget {
160
+ fn.Inl = nil
161
+ }
162
+ }
163
+
164
+ // computeFuncProps examines the Go function 'fn' and computes for it
165
+ // a function "properties" object, to be used to drive inlining
166
+ // heuristics. See comments on the FuncProps type for more info.
167
+ func computeFuncProps(fn *ir.Func, inlineMaxBudget int, nf *nameFinder) (*FuncProps, CallSiteTab) {
168
+ if debugTrace&debugTraceFuncs != 0 {
169
+ fmt.Fprintf(os.Stderr, "=-= starting analysis of func %v:\n%+v\n",
170
+ fn, fn)
171
+ }
172
+ funcProps := new(FuncProps)
173
+ ffa := makeFuncFlagsAnalyzer(fn)
174
+ analyzers := []propAnalyzer{ffa}
175
+ analyzers = addResultsAnalyzer(fn, analyzers, funcProps, inlineMaxBudget, nf)
176
+ analyzers = addParamsAnalyzer(fn, analyzers, funcProps, nf)
177
+ runAnalyzersOnFunction(fn, analyzers)
178
+ for _, a := range analyzers {
179
+ a.setResults(funcProps)
180
+ }
181
+ cstab := computeCallSiteTable(fn, fn.Body, nil, ffa.panicPathTable(), 0, nf)
182
+ return funcProps, cstab
183
+ }
184
+
185
+ func runAnalyzersOnFunction(fn *ir.Func, analyzers []propAnalyzer) {
186
+ var doNode func(ir.Node) bool
187
+ doNode = func(n ir.Node) bool {
188
+ for _, a := range analyzers {
189
+ a.nodeVisitPre(n)
190
+ }
191
+ ir.DoChildren(n, doNode)
192
+ for _, a := range analyzers {
193
+ a.nodeVisitPost(n)
194
+ }
195
+ return false
196
+ }
197
+ doNode(fn)
198
+ }
199
+
200
+ func propsForFunc(fn *ir.Func) *FuncProps {
201
+ if funcInlHeur, ok := fpmap[fn]; ok {
202
+ return funcInlHeur.props
203
+ } else if fn.Inl != nil && fn.Inl.Properties != "" {
204
+ // FIXME: considering adding some sort of cache or table
205
+ // for deserialized properties of imported functions.
206
+ return DeserializeFromString(fn.Inl.Properties)
207
+ }
208
+ return nil
209
+ }
210
+
211
+ func fnFileLine(fn *ir.Func) (string, uint) {
212
+ p := base.Ctxt.InnermostPos(fn.Pos())
213
+ return filepath.Base(p.Filename()), p.Line()
214
+ }
215
+
216
+ func Enabled() bool {
217
+ return buildcfg.Experiment.NewInliner || UnitTesting()
218
+ }
219
+
220
+ func UnitTesting() bool {
221
+ return base.Debug.DumpInlFuncProps != "" ||
222
+ base.Debug.DumpInlCallSiteScores != 0
223
+ }
224
+
225
+ // DumpFuncProps computes and caches function properties for the func
226
+ // 'fn', writing out a description of the previously computed set of
227
+ // properties to the file given in 'dumpfile'. Used for the
228
+ // "-d=dumpinlfuncprops=..." command line flag, intended for use
229
+ // primarily in unit testing.
230
+ func DumpFuncProps(fn *ir.Func, dumpfile string) {
231
+ if fn != nil {
232
+ if fn.OClosure != nil {
233
+ // closures will be processed along with their outer enclosing func.
234
+ return
235
+ }
236
+ captureFuncDumpEntry(fn)
237
+ ir.VisitFuncAndClosures(fn, func(n ir.Node) {
238
+ if clo, ok := n.(*ir.ClosureExpr); ok {
239
+ captureFuncDumpEntry(clo.Func)
240
+ }
241
+ })
242
+ } else {
243
+ emitDumpToFile(dumpfile)
244
+ }
245
+ }
246
+
247
+ // emitDumpToFile writes out the buffer function property dump entries
248
+ // to a file, for unit testing. Dump entries need to be sorted by
249
+ // definition line, and due to generics we need to account for the
250
+ // possibility that several ir.Func's will have the same def line.
251
+ func emitDumpToFile(dumpfile string) {
252
+ mode := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
253
+ if dumpfile[0] == '+' {
254
+ dumpfile = dumpfile[1:]
255
+ mode = os.O_WRONLY | os.O_APPEND | os.O_CREATE
256
+ }
257
+ if dumpfile[0] == '%' {
258
+ dumpfile = dumpfile[1:]
259
+ d, b := filepath.Dir(dumpfile), filepath.Base(dumpfile)
260
+ ptag := strings.ReplaceAll(types.LocalPkg.Path, "/", ":")
261
+ dumpfile = d + "/" + ptag + "." + b
262
+ }
263
+ outf, err := os.OpenFile(dumpfile, mode, 0644)
264
+ if err != nil {
265
+ base.Fatalf("opening function props dump file %q: %v\n", dumpfile, err)
266
+ }
267
+ defer outf.Close()
268
+ dumpFilePreamble(outf)
269
+
270
+ atline := map[uint]uint{}
271
+ sl := make([]fnInlHeur, 0, len(dumpBuffer))
272
+ for _, e := range dumpBuffer {
273
+ sl = append(sl, e)
274
+ atline[e.line] = atline[e.line] + 1
275
+ }
276
+ sl = sortFnInlHeurSlice(sl)
277
+
278
+ prevline := uint(0)
279
+ for _, entry := range sl {
280
+ idx := uint(0)
281
+ if prevline == entry.line {
282
+ idx++
283
+ }
284
+ prevline = entry.line
285
+ atl := atline[entry.line]
286
+ if err := dumpFnPreamble(outf, &entry, nil, idx, atl); err != nil {
287
+ base.Fatalf("function props dump: %v\n", err)
288
+ }
289
+ }
290
+ dumpBuffer = nil
291
+ }
292
+
293
+ // captureFuncDumpEntry grabs the function properties object for 'fn'
294
+ // and enqueues it for later dumping. Used for the
295
+ // "-d=dumpinlfuncprops=..." command line flag, intended for use
296
+ // primarily in unit testing.
297
+ func captureFuncDumpEntry(fn *ir.Func) {
298
+ // avoid capturing compiler-generated equality funcs.
299
+ if strings.HasPrefix(fn.Sym().Name, ".eq.") {
300
+ return
301
+ }
302
+ funcInlHeur, ok := fpmap[fn]
303
+ if !ok {
304
+ // Missing entry is expected for functions that are too large
305
+ // to inline. We still want to write out call site scores in
306
+ // this case however.
307
+ funcInlHeur = fnInlHeur{cstab: callSiteTab}
308
+ }
309
+ if dumpBuffer == nil {
310
+ dumpBuffer = make(map[*ir.Func]fnInlHeur)
311
+ }
312
+ if _, ok := dumpBuffer[fn]; ok {
313
+ return
314
+ }
315
+ if debugTrace&debugTraceFuncs != 0 {
316
+ fmt.Fprintf(os.Stderr, "=-= capturing dump for %v:\n", fn)
317
+ }
318
+ dumpBuffer[fn] = funcInlHeur
319
+ }
320
+
321
+ // dumpFilePreamble writes out a file-level preamble for a given
322
+ // Go function as part of a function properties dump.
323
+ func dumpFilePreamble(w io.Writer) {
324
+ fmt.Fprintf(w, "// DO NOT EDIT (use 'go test -v -update-expected' instead.)\n")
325
+ fmt.Fprintf(w, "// See cmd/compile/internal/inline/inlheur/testdata/props/README.txt\n")
326
+ fmt.Fprintf(w, "// for more information on the format of this file.\n")
327
+ fmt.Fprintf(w, "// %s\n", preambleDelimiter)
328
+ }
329
+
330
+ // dumpFnPreamble writes out a function-level preamble for a given
331
+ // Go function as part of a function properties dump. See the
332
+ // README.txt file in testdata/props for more on the format of
333
+ // this preamble.
334
+ func dumpFnPreamble(w io.Writer, funcInlHeur *fnInlHeur, ecst encodedCallSiteTab, idx, atl uint) error {
335
+ fmt.Fprintf(w, "// %s %s %d %d %d\n",
336
+ funcInlHeur.file, funcInlHeur.fname, funcInlHeur.line, idx, atl)
337
+ // emit props as comments, followed by delimiter
338
+ fmt.Fprintf(w, "%s// %s\n", funcInlHeur.props.ToString("// "), comDelimiter)
339
+ data, err := json.Marshal(funcInlHeur.props)
340
+ if err != nil {
341
+ return fmt.Errorf("marshall error %v\n", err)
342
+ }
343
+ fmt.Fprintf(w, "// %s\n", string(data))
344
+ dumpCallSiteComments(w, funcInlHeur.cstab, ecst)
345
+ fmt.Fprintf(w, "// %s\n", fnDelimiter)
346
+ return nil
347
+ }
348
+
349
+ // sortFnInlHeurSlice sorts a slice of fnInlHeur based on
350
+ // the starting line of the function definition, then by name.
351
+ func sortFnInlHeurSlice(sl []fnInlHeur) []fnInlHeur {
352
+ sort.SliceStable(sl, func(i, j int) bool {
353
+ if sl[i].line != sl[j].line {
354
+ return sl[i].line < sl[j].line
355
+ }
356
+ return sl[i].fname < sl[j].fname
357
+ })
358
+ return sl
359
+ }
360
+
361
+ // delimiters written to various preambles to make parsing of
362
+ // dumps easier.
363
+ const preambleDelimiter = "<endfilepreamble>"
364
+ const fnDelimiter = "<endfuncpreamble>"
365
+ const comDelimiter = "<endpropsdump>"
366
+ const csDelimiter = "<endcallsites>"
367
+
368
+ // dumpBuffer stores up function properties dumps when
369
+ // "-d=dumpinlfuncprops=..." is in effect.
370
+ var dumpBuffer map[*ir.Func]fnInlHeur
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/analyze_func_callsites.go ADDED
@@ -0,0 +1,413 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import (
8
+ "cmd/compile/internal/ir"
9
+ "cmd/compile/internal/pgo"
10
+ "cmd/compile/internal/typecheck"
11
+ "fmt"
12
+ "os"
13
+ "strings"
14
+ )
15
+
16
+ type callSiteAnalyzer struct {
17
+ fn *ir.Func
18
+ *nameFinder
19
+ }
20
+
21
+ type callSiteTableBuilder struct {
22
+ fn *ir.Func
23
+ *nameFinder
24
+ cstab CallSiteTab
25
+ ptab map[ir.Node]pstate
26
+ nstack []ir.Node
27
+ loopNest int
28
+ isInit bool
29
+ }
30
+
31
+ func makeCallSiteAnalyzer(fn *ir.Func) *callSiteAnalyzer {
32
+ return &callSiteAnalyzer{
33
+ fn: fn,
34
+ nameFinder: newNameFinder(fn),
35
+ }
36
+ }
37
+
38
+ func makeCallSiteTableBuilder(fn *ir.Func, cstab CallSiteTab, ptab map[ir.Node]pstate, loopNestingLevel int, nf *nameFinder) *callSiteTableBuilder {
39
+ isInit := fn.IsPackageInit() || strings.HasPrefix(fn.Sym().Name, "init.")
40
+ return &callSiteTableBuilder{
41
+ fn: fn,
42
+ cstab: cstab,
43
+ ptab: ptab,
44
+ isInit: isInit,
45
+ loopNest: loopNestingLevel,
46
+ nstack: []ir.Node{fn},
47
+ nameFinder: nf,
48
+ }
49
+ }
50
+
51
+ // computeCallSiteTable builds and returns a table of call sites for
52
+ // the specified region in function fn. A region here corresponds to a
53
+ // specific subtree within the AST for a function. The main intended
54
+ // use cases are for 'region' to be either A) an entire function body,
55
+ // or B) an inlined call expression.
56
+ func computeCallSiteTable(fn *ir.Func, region ir.Nodes, cstab CallSiteTab, ptab map[ir.Node]pstate, loopNestingLevel int, nf *nameFinder) CallSiteTab {
57
+ cstb := makeCallSiteTableBuilder(fn, cstab, ptab, loopNestingLevel, nf)
58
+ var doNode func(ir.Node) bool
59
+ doNode = func(n ir.Node) bool {
60
+ cstb.nodeVisitPre(n)
61
+ ir.DoChildren(n, doNode)
62
+ cstb.nodeVisitPost(n)
63
+ return false
64
+ }
65
+ for _, n := range region {
66
+ doNode(n)
67
+ }
68
+ return cstb.cstab
69
+ }
70
+
71
+ func (cstb *callSiteTableBuilder) flagsForNode(call *ir.CallExpr) CSPropBits {
72
+ var r CSPropBits
73
+
74
+ if debugTrace&debugTraceCalls != 0 {
75
+ fmt.Fprintf(os.Stderr, "=-= analyzing call at %s\n",
76
+ fmtFullPos(call.Pos()))
77
+ }
78
+
79
+ // Set a bit if this call is within a loop.
80
+ if cstb.loopNest > 0 {
81
+ r |= CallSiteInLoop
82
+ }
83
+
84
+ // Set a bit if the call is within an init function (either
85
+ // compiler-generated or user-written).
86
+ if cstb.isInit {
87
+ r |= CallSiteInInitFunc
88
+ }
89
+
90
+ // Decide whether to apply the panic path heuristic. Hack: don't
91
+ // apply this heuristic in the function "main.main" (mostly just
92
+ // to avoid annoying users).
93
+ if !isMainMain(cstb.fn) {
94
+ r = cstb.determinePanicPathBits(call, r)
95
+ }
96
+
97
+ return r
98
+ }
99
+
100
+ // determinePanicPathBits updates the CallSiteOnPanicPath bit within
101
+ // "r" if we think this call is on an unconditional path to
102
+ // panic/exit. Do this by walking back up the node stack to see if we
103
+ // can find either A) an enclosing panic, or B) a statement node that
104
+ // we've determined leads to a panic/exit.
105
+ func (cstb *callSiteTableBuilder) determinePanicPathBits(call ir.Node, r CSPropBits) CSPropBits {
106
+ cstb.nstack = append(cstb.nstack, call)
107
+ defer func() {
108
+ cstb.nstack = cstb.nstack[:len(cstb.nstack)-1]
109
+ }()
110
+
111
+ for ri := range cstb.nstack[:len(cstb.nstack)-1] {
112
+ i := len(cstb.nstack) - ri - 1
113
+ n := cstb.nstack[i]
114
+ _, isCallExpr := n.(*ir.CallExpr)
115
+ _, isStmt := n.(ir.Stmt)
116
+ if isCallExpr {
117
+ isStmt = false
118
+ }
119
+
120
+ if debugTrace&debugTraceCalls != 0 {
121
+ ps, inps := cstb.ptab[n]
122
+ fmt.Fprintf(os.Stderr, "=-= callpar %d op=%s ps=%s inptab=%v stmt=%v\n", i, n.Op().String(), ps.String(), inps, isStmt)
123
+ }
124
+
125
+ if n.Op() == ir.OPANIC {
126
+ r |= CallSiteOnPanicPath
127
+ break
128
+ }
129
+ if v, ok := cstb.ptab[n]; ok {
130
+ if v == psCallsPanic {
131
+ r |= CallSiteOnPanicPath
132
+ break
133
+ }
134
+ if isStmt {
135
+ break
136
+ }
137
+ }
138
+ }
139
+ return r
140
+ }
141
+
142
+ // propsForArg returns property bits for a given call argument expression arg.
143
+ func (cstb *callSiteTableBuilder) propsForArg(arg ir.Node) ActualExprPropBits {
144
+ if cval := cstb.constValue(arg); cval != nil {
145
+ return ActualExprConstant
146
+ }
147
+ if cstb.isConcreteConvIface(arg) {
148
+ return ActualExprIsConcreteConvIface
149
+ }
150
+ fname := cstb.funcName(arg)
151
+ if fname != nil {
152
+ if fn := fname.Func; fn != nil && typecheck.HaveInlineBody(fn) {
153
+ return ActualExprIsInlinableFunc
154
+ }
155
+ return ActualExprIsFunc
156
+ }
157
+ return 0
158
+ }
159
+
160
+ // argPropsForCall returns a slice of argument properties for the
161
+ // expressions being passed to the callee in the specific call
162
+ // expression; these will be stored in the CallSite object for a given
163
+ // call and then consulted when scoring. If no arg has any interesting
164
+ // properties we try to save some space and return a nil slice.
165
+ func (cstb *callSiteTableBuilder) argPropsForCall(ce *ir.CallExpr) []ActualExprPropBits {
166
+ rv := make([]ActualExprPropBits, len(ce.Args))
167
+ somethingInteresting := false
168
+ for idx := range ce.Args {
169
+ argProp := cstb.propsForArg(ce.Args[idx])
170
+ somethingInteresting = somethingInteresting || (argProp != 0)
171
+ rv[idx] = argProp
172
+ }
173
+ if !somethingInteresting {
174
+ return nil
175
+ }
176
+ return rv
177
+ }
178
+
179
+ func (cstb *callSiteTableBuilder) addCallSite(callee *ir.Func, call *ir.CallExpr) {
180
+ flags := cstb.flagsForNode(call)
181
+ argProps := cstb.argPropsForCall(call)
182
+ if debugTrace&debugTraceCalls != 0 {
183
+ fmt.Fprintf(os.Stderr, "=-= props %+v for call %v\n", argProps, call)
184
+ }
185
+ // FIXME: maybe bulk-allocate these?
186
+ cs := &CallSite{
187
+ Call: call,
188
+ Callee: callee,
189
+ Assign: cstb.containingAssignment(call),
190
+ ArgProps: argProps,
191
+ Flags: flags,
192
+ ID: uint(len(cstb.cstab)),
193
+ }
194
+ if _, ok := cstb.cstab[call]; ok {
195
+ fmt.Fprintf(os.Stderr, "*** cstab duplicate entry at: %s\n",
196
+ fmtFullPos(call.Pos()))
197
+ fmt.Fprintf(os.Stderr, "*** call: %+v\n", call)
198
+ panic("bad")
199
+ }
200
+ // Set initial score for callsite to the cost computed
201
+ // by CanInline; this score will be refined later based
202
+ // on heuristics.
203
+ cs.Score = int(callee.Inl.Cost)
204
+
205
+ if cstb.cstab == nil {
206
+ cstb.cstab = make(CallSiteTab)
207
+ }
208
+ cstb.cstab[call] = cs
209
+ if debugTrace&debugTraceCalls != 0 {
210
+ fmt.Fprintf(os.Stderr, "=-= added callsite: caller=%v callee=%v n=%s\n",
211
+ cstb.fn, callee, fmtFullPos(call.Pos()))
212
+ }
213
+ }
214
+
215
+ func (cstb *callSiteTableBuilder) nodeVisitPre(n ir.Node) {
216
+ switch n.Op() {
217
+ case ir.ORANGE, ir.OFOR:
218
+ if !hasTopLevelLoopBodyReturnOrBreak(loopBody(n)) {
219
+ cstb.loopNest++
220
+ }
221
+ case ir.OCALLFUNC:
222
+ ce := n.(*ir.CallExpr)
223
+ callee := pgo.DirectCallee(ce.Fun)
224
+ if callee != nil && callee.Inl != nil {
225
+ cstb.addCallSite(callee, ce)
226
+ }
227
+ }
228
+ cstb.nstack = append(cstb.nstack, n)
229
+ }
230
+
231
+ func (cstb *callSiteTableBuilder) nodeVisitPost(n ir.Node) {
232
+ cstb.nstack = cstb.nstack[:len(cstb.nstack)-1]
233
+ switch n.Op() {
234
+ case ir.ORANGE, ir.OFOR:
235
+ if !hasTopLevelLoopBodyReturnOrBreak(loopBody(n)) {
236
+ cstb.loopNest--
237
+ }
238
+ }
239
+ }
240
+
241
+ func loopBody(n ir.Node) ir.Nodes {
242
+ if forst, ok := n.(*ir.ForStmt); ok {
243
+ return forst.Body
244
+ }
245
+ if rst, ok := n.(*ir.RangeStmt); ok {
246
+ return rst.Body
247
+ }
248
+ return nil
249
+ }
250
+
251
+ // hasTopLevelLoopBodyReturnOrBreak examines the body of a "for" or
252
+ // "range" loop to try to verify that it is a real loop, as opposed to
253
+ // a construct that is syntactically loopy but doesn't actually iterate
254
+ // multiple times, like:
255
+ //
256
+ // for {
257
+ // blah()
258
+ // return 1
259
+ // }
260
+ //
261
+ // [Remark: the pattern above crops up quite a bit in the source code
262
+ // for the compiler itself, e.g. the auto-generated rewrite code]
263
+ //
264
+ // Note that we don't look for GOTO statements here, so it's possible
265
+ // we'll get the wrong result for a loop with complicated control
266
+ // jumps via gotos.
267
+ func hasTopLevelLoopBodyReturnOrBreak(loopBody ir.Nodes) bool {
268
+ for _, n := range loopBody {
269
+ if n.Op() == ir.ORETURN || n.Op() == ir.OBREAK {
270
+ return true
271
+ }
272
+ }
273
+ return false
274
+ }
275
+
276
+ // containingAssignment returns the top-level assignment statement
277
+ // for a statement level function call "n". Examples:
278
+ //
279
+ // x := foo()
280
+ // x, y := bar(z, baz())
281
+ // if blah() { ...
282
+ //
283
+ // Here the top-level assignment statement for the foo() call is the
284
+ // statement assigning to "x"; the top-level assignment for "bar()"
285
+ // call is the assignment to x,y. For the baz() and blah() calls,
286
+ // there is no top level assignment statement.
287
+ //
288
+ // The unstated goal here is that we want to use the containing
289
+ // assignment to establish a connection between a given call and the
290
+ // variables to which its results/returns are being assigned.
291
+ //
292
+ // Note that for the "bar" command above, the front end sometimes
293
+ // decomposes this into two assignments, the first one assigning the
294
+ // call to a pair of auto-temps, then the second one assigning the
295
+ // auto-temps to the user-visible vars. This helper will return the
296
+ // second (outer) of these two.
297
+ func (cstb *callSiteTableBuilder) containingAssignment(n ir.Node) ir.Node {
298
+ parent := cstb.nstack[len(cstb.nstack)-1]
299
+
300
+ // assignsOnlyAutoTemps returns TRUE of the specified OAS2FUNC
301
+ // node assigns only auto-temps.
302
+ assignsOnlyAutoTemps := func(x ir.Node) bool {
303
+ alst := x.(*ir.AssignListStmt)
304
+ oa2init := alst.Init()
305
+ if len(oa2init) == 0 {
306
+ return false
307
+ }
308
+ for _, v := range oa2init {
309
+ d := v.(*ir.Decl)
310
+ if !ir.IsAutoTmp(d.X) {
311
+ return false
312
+ }
313
+ }
314
+ return true
315
+ }
316
+
317
+ // Simple case: x := foo()
318
+ if parent.Op() == ir.OAS {
319
+ return parent
320
+ }
321
+
322
+ // Multi-return case: x, y := bar()
323
+ if parent.Op() == ir.OAS2FUNC {
324
+ // Hack city: if the result vars are auto-temps, try looking
325
+ // for an outer assignment in the tree. The code shape we're
326
+ // looking for here is:
327
+ //
328
+ // OAS1({x,y},OCONVNOP(OAS2FUNC({auto1,auto2},OCALLFUNC(bar))))
329
+ //
330
+ if assignsOnlyAutoTemps(parent) {
331
+ par2 := cstb.nstack[len(cstb.nstack)-2]
332
+ if par2.Op() == ir.OAS2 {
333
+ return par2
334
+ }
335
+ if par2.Op() == ir.OCONVNOP {
336
+ par3 := cstb.nstack[len(cstb.nstack)-3]
337
+ if par3.Op() == ir.OAS2 {
338
+ return par3
339
+ }
340
+ }
341
+ }
342
+ }
343
+
344
+ return nil
345
+ }
346
+
347
+ // UpdateCallsiteTable handles updating of callerfn's call site table
348
+ // after an inlined has been carried out, e.g. the call at 'n' as been
349
+ // turned into the inlined call expression 'ic' within function
350
+ // callerfn. The chief thing of interest here is to make sure that any
351
+ // call nodes within 'ic' are added to the call site table for
352
+ // 'callerfn' and scored appropriately.
353
+ func UpdateCallsiteTable(callerfn *ir.Func, n *ir.CallExpr, ic *ir.InlinedCallExpr) {
354
+ enableDebugTraceIfEnv()
355
+ defer disableDebugTrace()
356
+
357
+ funcInlHeur, ok := fpmap[callerfn]
358
+ if !ok {
359
+ // This can happen for compiler-generated wrappers.
360
+ if debugTrace&debugTraceCalls != 0 {
361
+ fmt.Fprintf(os.Stderr, "=-= early exit, no entry for caller fn %v\n", callerfn)
362
+ }
363
+ return
364
+ }
365
+
366
+ if debugTrace&debugTraceCalls != 0 {
367
+ fmt.Fprintf(os.Stderr, "=-= UpdateCallsiteTable(caller=%v, cs=%s)\n",
368
+ callerfn, fmtFullPos(n.Pos()))
369
+ }
370
+
371
+ // Mark the call in question as inlined.
372
+ oldcs, ok := funcInlHeur.cstab[n]
373
+ if !ok {
374
+ // This can happen for compiler-generated wrappers.
375
+ return
376
+ }
377
+ oldcs.aux |= csAuxInlined
378
+
379
+ if debugTrace&debugTraceCalls != 0 {
380
+ fmt.Fprintf(os.Stderr, "=-= marked as inlined: callee=%v %s\n",
381
+ oldcs.Callee, EncodeCallSiteKey(oldcs))
382
+ }
383
+
384
+ // Walk the inlined call region to collect new callsites.
385
+ var icp pstate
386
+ if oldcs.Flags&CallSiteOnPanicPath != 0 {
387
+ icp = psCallsPanic
388
+ }
389
+ var loopNestLevel int
390
+ if oldcs.Flags&CallSiteInLoop != 0 {
391
+ loopNestLevel = 1
392
+ }
393
+ ptab := map[ir.Node]pstate{ic: icp}
394
+ nf := newNameFinder(nil)
395
+ icstab := computeCallSiteTable(callerfn, ic.Body, nil, ptab, loopNestLevel, nf)
396
+
397
+ // Record parent callsite. This is primarily for debug output.
398
+ for _, cs := range icstab {
399
+ cs.parent = oldcs
400
+ }
401
+
402
+ // Score the calls in the inlined body. Note the setting of
403
+ // "doCallResults" to false here: at the moment there isn't any
404
+ // easy way to localize or region-ize the work done by
405
+ // "rescoreBasedOnCallResultUses", which currently does a walk
406
+ // over the entire function to look for uses of a given set of
407
+ // results. Similarly we're passing nil to makeCallSiteAnalyzer,
408
+ // so as to run name finding without the use of static value &
409
+ // friends.
410
+ csa := makeCallSiteAnalyzer(nil)
411
+ const doCallResults = false
412
+ csa.scoreCallsRegion(callerfn, ic.Body, icstab, doCallResults, ic)
413
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/analyze_func_flags.go ADDED
@@ -0,0 +1,356 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import (
8
+ "cmd/compile/internal/base"
9
+ "cmd/compile/internal/ir"
10
+ "cmd/compile/internal/types"
11
+ "fmt"
12
+ "os"
13
+ )
14
+
15
+ // funcFlagsAnalyzer computes the "Flags" value for the FuncProps
16
+ // object we're computing. The main item of interest here is "nstate",
17
+ // which stores the disposition of a given ir Node with respect to the
18
+ // flags/properties we're trying to compute.
19
+ type funcFlagsAnalyzer struct {
20
+ fn *ir.Func
21
+ nstate map[ir.Node]pstate
22
+ noInfo bool // set if we see something inscrutable/un-analyzable
23
+ }
24
+
25
+ // pstate keeps track of the disposition of a given node and its
26
+ // children with respect to panic/exit calls.
27
+ type pstate int
28
+
29
+ const (
30
+ psNoInfo pstate = iota // nothing interesting about this node
31
+ psCallsPanic // node causes call to panic or os.Exit
32
+ psMayReturn // executing node may trigger a "return" stmt
33
+ psTop // dataflow lattice "top" element
34
+ )
35
+
36
+ func makeFuncFlagsAnalyzer(fn *ir.Func) *funcFlagsAnalyzer {
37
+ return &funcFlagsAnalyzer{
38
+ fn: fn,
39
+ nstate: make(map[ir.Node]pstate),
40
+ }
41
+ }
42
+
43
+ // setResults transfers func flag results to 'funcProps'.
44
+ func (ffa *funcFlagsAnalyzer) setResults(funcProps *FuncProps) {
45
+ var rv FuncPropBits
46
+ if !ffa.noInfo && ffa.stateForList(ffa.fn.Body) == psCallsPanic {
47
+ rv = FuncPropNeverReturns
48
+ }
49
+ // This is slightly hacky and not at all required, but include a
50
+ // special case for main.main, which often ends in a call to
51
+ // os.Exit. People who write code like this (very common I
52
+ // imagine)
53
+ //
54
+ // func main() {
55
+ // rc = perform()
56
+ // ...
57
+ // foo()
58
+ // os.Exit(rc)
59
+ // }
60
+ //
61
+ // will be constantly surprised when foo() is inlined in many
62
+ // other spots in the program but not in main().
63
+ if isMainMain(ffa.fn) {
64
+ rv &^= FuncPropNeverReturns
65
+ }
66
+ funcProps.Flags = rv
67
+ }
68
+
69
+ func (ffa *funcFlagsAnalyzer) getState(n ir.Node) pstate {
70
+ return ffa.nstate[n]
71
+ }
72
+
73
+ func (ffa *funcFlagsAnalyzer) setState(n ir.Node, st pstate) {
74
+ if st != psNoInfo {
75
+ ffa.nstate[n] = st
76
+ }
77
+ }
78
+
79
+ func (ffa *funcFlagsAnalyzer) updateState(n ir.Node, st pstate) {
80
+ if st == psNoInfo {
81
+ delete(ffa.nstate, n)
82
+ } else {
83
+ ffa.nstate[n] = st
84
+ }
85
+ }
86
+
87
+ func (ffa *funcFlagsAnalyzer) panicPathTable() map[ir.Node]pstate {
88
+ return ffa.nstate
89
+ }
90
+
91
+ // blockCombine merges together states as part of a linear sequence of
92
+ // statements, where 'pred' and 'succ' are analysis results for a pair
93
+ // of consecutive statements. Examples:
94
+ //
95
+ // case 1: case 2:
96
+ // panic("foo") if q { return x } <-pred
97
+ // return x panic("boo") <-succ
98
+ //
99
+ // In case 1, since the pred state is "always panic" it doesn't matter
100
+ // what the succ state is, hence the state for the combination of the
101
+ // two blocks is "always panics". In case 2, because there is a path
102
+ // to return that avoids the panic in succ, the state for the
103
+ // combination of the two statements is "may return".
104
+ func blockCombine(pred, succ pstate) pstate {
105
+ switch succ {
106
+ case psTop:
107
+ return pred
108
+ case psMayReturn:
109
+ if pred == psCallsPanic {
110
+ return psCallsPanic
111
+ }
112
+ return psMayReturn
113
+ case psNoInfo:
114
+ return pred
115
+ case psCallsPanic:
116
+ if pred == psMayReturn {
117
+ return psMayReturn
118
+ }
119
+ return psCallsPanic
120
+ }
121
+ panic("should never execute")
122
+ }
123
+
124
+ // branchCombine combines two states at a control flow branch point where
125
+ // either p1 or p2 executes (as in an "if" statement).
126
+ func branchCombine(p1, p2 pstate) pstate {
127
+ if p1 == psCallsPanic && p2 == psCallsPanic {
128
+ return psCallsPanic
129
+ }
130
+ if p1 == psMayReturn || p2 == psMayReturn {
131
+ return psMayReturn
132
+ }
133
+ return psNoInfo
134
+ }
135
+
136
+ // stateForList walks through a list of statements and computes the
137
+ // state/diposition for the entire list as a whole, as well
138
+ // as updating disposition of intermediate nodes.
139
+ func (ffa *funcFlagsAnalyzer) stateForList(list ir.Nodes) pstate {
140
+ st := psTop
141
+ // Walk the list backwards so that we can update the state for
142
+ // earlier list elements based on what we find out about their
143
+ // successors. Example:
144
+ //
145
+ // if ... {
146
+ // L10: foo()
147
+ // L11: <stmt>
148
+ // L12: panic(...)
149
+ // }
150
+ //
151
+ // After combining the dispositions for line 11 and 12, we want to
152
+ // update the state for the call at line 10 based on that combined
153
+ // disposition (if L11 has no path to "return", then the call at
154
+ // line 10 will be on a panic path).
155
+ for i := len(list) - 1; i >= 0; i-- {
156
+ n := list[i]
157
+ psi := ffa.getState(n)
158
+ if debugTrace&debugTraceFuncFlags != 0 {
159
+ fmt.Fprintf(os.Stderr, "=-= %v: stateForList n=%s ps=%s\n",
160
+ ir.Line(n), n.Op().String(), psi.String())
161
+ }
162
+ st = blockCombine(psi, st)
163
+ ffa.updateState(n, st)
164
+ }
165
+ if st == psTop {
166
+ st = psNoInfo
167
+ }
168
+ return st
169
+ }
170
+
171
+ func isMainMain(fn *ir.Func) bool {
172
+ s := fn.Sym()
173
+ return (s.Pkg.Name == "main" && s.Name == "main")
174
+ }
175
+
176
+ func isWellKnownFunc(s *types.Sym, pkg, name string) bool {
177
+ return s.Pkg.Path == pkg && s.Name == name
178
+ }
179
+
180
+ // isExitCall reports TRUE if the node itself is an unconditional
181
+ // call to os.Exit(), a panic, or a function that does likewise.
182
+ func isExitCall(n ir.Node) bool {
183
+ if n.Op() != ir.OCALLFUNC {
184
+ return false
185
+ }
186
+ cx := n.(*ir.CallExpr)
187
+ name := ir.StaticCalleeName(cx.Fun)
188
+ if name == nil {
189
+ return false
190
+ }
191
+ s := name.Sym()
192
+ if isWellKnownFunc(s, "os", "Exit") ||
193
+ isWellKnownFunc(s, "runtime", "throw") {
194
+ return true
195
+ }
196
+ if funcProps := propsForFunc(name.Func); funcProps != nil {
197
+ if funcProps.Flags&FuncPropNeverReturns != 0 {
198
+ return true
199
+ }
200
+ }
201
+ return name.Func.NeverReturns()
202
+ }
203
+
204
+ // pessimize is called to record the fact that we saw something in the
205
+ // function that renders it entirely impossible to analyze.
206
+ func (ffa *funcFlagsAnalyzer) pessimize() {
207
+ ffa.noInfo = true
208
+ }
209
+
210
+ // shouldVisit reports TRUE if this is an interesting node from the
211
+ // perspective of computing function flags. NB: due to the fact that
212
+ // ir.CallExpr implements the Stmt interface, we wind up visiting
213
+ // a lot of nodes that we don't really need to, but these can
214
+ // simply be screened out as part of the visit.
215
+ func shouldVisit(n ir.Node) bool {
216
+ _, isStmt := n.(ir.Stmt)
217
+ return n.Op() != ir.ODCL &&
218
+ (isStmt || n.Op() == ir.OCALLFUNC || n.Op() == ir.OPANIC)
219
+ }
220
+
221
+ // nodeVisitPost helps implement the propAnalyzer interface; when
222
+ // called on a given node, it decides the disposition of that node
223
+ // based on the state(s) of the node's children.
224
+ func (ffa *funcFlagsAnalyzer) nodeVisitPost(n ir.Node) {
225
+ if debugTrace&debugTraceFuncFlags != 0 {
226
+ fmt.Fprintf(os.Stderr, "=+= nodevis %v %s should=%v\n",
227
+ ir.Line(n), n.Op().String(), shouldVisit(n))
228
+ }
229
+ if !shouldVisit(n) {
230
+ return
231
+ }
232
+ var st pstate
233
+ switch n.Op() {
234
+ case ir.OCALLFUNC:
235
+ if isExitCall(n) {
236
+ st = psCallsPanic
237
+ }
238
+ case ir.OPANIC:
239
+ st = psCallsPanic
240
+ case ir.ORETURN:
241
+ st = psMayReturn
242
+ case ir.OBREAK, ir.OCONTINUE:
243
+ // FIXME: this handling of break/continue is sub-optimal; we
244
+ // have them as "mayReturn" in order to help with this case:
245
+ //
246
+ // for {
247
+ // if q() { break }
248
+ // panic(...)
249
+ // }
250
+ //
251
+ // where the effect of the 'break' is to cause the subsequent
252
+ // panic to be skipped. One possible improvement would be to
253
+ // track whether the currently enclosing loop is a "for {" or
254
+ // a for/range with condition, then use mayReturn only for the
255
+ // former. Note also that "break X" or "continue X" is treated
256
+ // the same as "goto", since we don't have a good way to track
257
+ // the target of the branch.
258
+ st = psMayReturn
259
+ n := n.(*ir.BranchStmt)
260
+ if n.Label != nil {
261
+ ffa.pessimize()
262
+ }
263
+ case ir.OBLOCK:
264
+ n := n.(*ir.BlockStmt)
265
+ st = ffa.stateForList(n.List)
266
+ case ir.OCASE:
267
+ if ccst, ok := n.(*ir.CaseClause); ok {
268
+ st = ffa.stateForList(ccst.Body)
269
+ } else if ccst, ok := n.(*ir.CommClause); ok {
270
+ st = ffa.stateForList(ccst.Body)
271
+ } else {
272
+ panic("unexpected")
273
+ }
274
+ case ir.OIF:
275
+ n := n.(*ir.IfStmt)
276
+ st = branchCombine(ffa.stateForList(n.Body), ffa.stateForList(n.Else))
277
+ case ir.OFOR:
278
+ // Treat for { XXX } like a block.
279
+ // Treat for <cond> { XXX } like an if statement with no else.
280
+ n := n.(*ir.ForStmt)
281
+ bst := ffa.stateForList(n.Body)
282
+ if n.Cond == nil {
283
+ st = bst
284
+ } else {
285
+ if bst == psMayReturn {
286
+ st = psMayReturn
287
+ }
288
+ }
289
+ case ir.ORANGE:
290
+ // Treat for range { XXX } like an if statement with no else.
291
+ n := n.(*ir.RangeStmt)
292
+ if ffa.stateForList(n.Body) == psMayReturn {
293
+ st = psMayReturn
294
+ }
295
+ case ir.OGOTO:
296
+ // punt if we see even one goto. if we built a control
297
+ // flow graph we could do more, but this is just a tree walk.
298
+ ffa.pessimize()
299
+ case ir.OSELECT:
300
+ // process selects for "may return" but not "always panics",
301
+ // the latter case seems very improbable.
302
+ n := n.(*ir.SelectStmt)
303
+ if len(n.Cases) != 0 {
304
+ st = psTop
305
+ for _, c := range n.Cases {
306
+ st = branchCombine(ffa.stateForList(c.Body), st)
307
+ }
308
+ }
309
+ case ir.OSWITCH:
310
+ n := n.(*ir.SwitchStmt)
311
+ if len(n.Cases) != 0 {
312
+ st = psTop
313
+ for _, c := range n.Cases {
314
+ st = branchCombine(ffa.stateForList(c.Body), st)
315
+ }
316
+ }
317
+
318
+ st, fall := psTop, psNoInfo
319
+ for i := len(n.Cases) - 1; i >= 0; i-- {
320
+ cas := n.Cases[i]
321
+ cst := ffa.stateForList(cas.Body)
322
+ endsInFallthrough := false
323
+ if len(cas.Body) != 0 {
324
+ endsInFallthrough = cas.Body[0].Op() == ir.OFALL
325
+ }
326
+ if endsInFallthrough {
327
+ cst = blockCombine(cst, fall)
328
+ }
329
+ st = branchCombine(st, cst)
330
+ fall = cst
331
+ }
332
+ case ir.OFALL:
333
+ // Not important.
334
+ case ir.ODCLFUNC, ir.ORECOVER, ir.OAS, ir.OAS2, ir.OAS2FUNC, ir.OASOP,
335
+ ir.OPRINTLN, ir.OPRINT, ir.OLABEL, ir.OCALLINTER, ir.ODEFER,
336
+ ir.OSEND, ir.ORECV, ir.OSELRECV2, ir.OGO, ir.OAPPEND, ir.OAS2DOTTYPE,
337
+ ir.OAS2MAPR, ir.OGETG, ir.ODELETE, ir.OINLMARK, ir.OAS2RECV,
338
+ ir.OMIN, ir.OMAX, ir.OMAKE, ir.ORECOVERFP, ir.OGETCALLERSP:
339
+ // these should all be benign/uninteresting
340
+ case ir.OTAILCALL, ir.OJUMPTABLE, ir.OTYPESW:
341
+ // don't expect to see these at all.
342
+ base.Fatalf("unexpected op %s in func %s",
343
+ n.Op().String(), ir.FuncName(ffa.fn))
344
+ default:
345
+ base.Fatalf("%v: unhandled op %s in func %v",
346
+ ir.Line(n), n.Op().String(), ir.FuncName(ffa.fn))
347
+ }
348
+ if debugTrace&debugTraceFuncFlags != 0 {
349
+ fmt.Fprintf(os.Stderr, "=-= %v: visit n=%s returns %s\n",
350
+ ir.Line(n), n.Op().String(), st.String())
351
+ }
352
+ ffa.setState(n, st)
353
+ }
354
+
355
+ func (ffa *funcFlagsAnalyzer) nodeVisitPre(n ir.Node) {
356
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/analyze_func_params.go ADDED
@@ -0,0 +1,355 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import (
8
+ "cmd/compile/internal/ir"
9
+ "fmt"
10
+ "os"
11
+ )
12
+
13
+ // paramsAnalyzer holds state information for the phase that computes
14
+ // flags for a Go functions parameters, for use in inline heuristics.
15
+ // Note that the params slice below includes entries for blanks.
16
+ type paramsAnalyzer struct {
17
+ fname string
18
+ values []ParamPropBits
19
+ params []*ir.Name
20
+ top []bool
21
+ *condLevelTracker
22
+ *nameFinder
23
+ }
24
+
25
+ // getParams returns an *ir.Name slice containing all params for the
26
+ // function (plus rcvr as well if applicable).
27
+ func getParams(fn *ir.Func) []*ir.Name {
28
+ sig := fn.Type()
29
+ numParams := sig.NumRecvs() + sig.NumParams()
30
+ return fn.Dcl[:numParams]
31
+ }
32
+
33
+ // addParamsAnalyzer creates a new paramsAnalyzer helper object for
34
+ // the function fn, appends it to the analyzers list, and returns the
35
+ // new list. If the function in question doesn't have any interesting
36
+ // parameters then the analyzer list is returned unchanged, and the
37
+ // params flags in "fp" are updated accordingly.
38
+ func addParamsAnalyzer(fn *ir.Func, analyzers []propAnalyzer, fp *FuncProps, nf *nameFinder) []propAnalyzer {
39
+ pa, props := makeParamsAnalyzer(fn, nf)
40
+ if pa != nil {
41
+ analyzers = append(analyzers, pa)
42
+ } else {
43
+ fp.ParamFlags = props
44
+ }
45
+ return analyzers
46
+ }
47
+
48
+ // makeParamAnalyzer creates a new helper object to analyze parameters
49
+ // of function fn. If the function doesn't have any interesting
50
+ // params, a nil helper is returned along with a set of default param
51
+ // flags for the func.
52
+ func makeParamsAnalyzer(fn *ir.Func, nf *nameFinder) (*paramsAnalyzer, []ParamPropBits) {
53
+ params := getParams(fn) // includes receiver if applicable
54
+ if len(params) == 0 {
55
+ return nil, nil
56
+ }
57
+ vals := make([]ParamPropBits, len(params))
58
+ if fn.Inl == nil {
59
+ return nil, vals
60
+ }
61
+ top := make([]bool, len(params))
62
+ interestingToAnalyze := false
63
+ for i, pn := range params {
64
+ if pn == nil {
65
+ continue
66
+ }
67
+ pt := pn.Type()
68
+ if !pt.IsScalar() && !pt.HasNil() {
69
+ // existing properties not applicable here (for things
70
+ // like structs, arrays, slices, etc).
71
+ continue
72
+ }
73
+ // If param is reassigned, skip it.
74
+ if ir.Reassigned(pn) {
75
+ continue
76
+ }
77
+ top[i] = true
78
+ interestingToAnalyze = true
79
+ }
80
+ if !interestingToAnalyze {
81
+ return nil, vals
82
+ }
83
+
84
+ if debugTrace&debugTraceParams != 0 {
85
+ fmt.Fprintf(os.Stderr, "=-= param analysis of func %v:\n",
86
+ fn.Sym().Name)
87
+ for i := range vals {
88
+ n := "_"
89
+ if params[i] != nil {
90
+ n = params[i].Sym().String()
91
+ }
92
+ fmt.Fprintf(os.Stderr, "=-= %d: %q %s top=%v\n",
93
+ i, n, vals[i].String(), top[i])
94
+ }
95
+ }
96
+ pa := &paramsAnalyzer{
97
+ fname: fn.Sym().Name,
98
+ values: vals,
99
+ params: params,
100
+ top: top,
101
+ condLevelTracker: new(condLevelTracker),
102
+ nameFinder: nf,
103
+ }
104
+ return pa, nil
105
+ }
106
+
107
+ func (pa *paramsAnalyzer) setResults(funcProps *FuncProps) {
108
+ funcProps.ParamFlags = pa.values
109
+ }
110
+
111
+ func (pa *paramsAnalyzer) findParamIdx(n *ir.Name) int {
112
+ if n == nil {
113
+ panic("bad")
114
+ }
115
+ for i := range pa.params {
116
+ if pa.params[i] == n {
117
+ return i
118
+ }
119
+ }
120
+ return -1
121
+ }
122
+
123
+ type testfType func(x ir.Node, param *ir.Name, idx int) (bool, bool)
124
+
125
+ // paramsAnalyzer invokes function 'testf' on the specified expression
126
+ // 'x' for each parameter, and if the result is TRUE, or's 'flag' into
127
+ // the flags for that param.
128
+ func (pa *paramsAnalyzer) checkParams(x ir.Node, flag ParamPropBits, mayflag ParamPropBits, testf testfType) {
129
+ for idx, p := range pa.params {
130
+ if !pa.top[idx] && pa.values[idx] == ParamNoInfo {
131
+ continue
132
+ }
133
+ result, may := testf(x, p, idx)
134
+ if debugTrace&debugTraceParams != 0 {
135
+ fmt.Fprintf(os.Stderr, "=-= test expr %v param %s result=%v flag=%s\n", x, p.Sym().Name, result, flag.String())
136
+ }
137
+ if result {
138
+ v := flag
139
+ if pa.condLevel != 0 || may {
140
+ v = mayflag
141
+ }
142
+ pa.values[idx] |= v
143
+ pa.top[idx] = false
144
+ }
145
+ }
146
+ }
147
+
148
+ // foldCheckParams checks expression 'x' (an 'if' condition or
149
+ // 'switch' stmt expr) to see if the expr would fold away if a
150
+ // specific parameter had a constant value.
151
+ func (pa *paramsAnalyzer) foldCheckParams(x ir.Node) {
152
+ pa.checkParams(x, ParamFeedsIfOrSwitch, ParamMayFeedIfOrSwitch,
153
+ func(x ir.Node, p *ir.Name, idx int) (bool, bool) {
154
+ return ShouldFoldIfNameConstant(x, []*ir.Name{p}), false
155
+ })
156
+ }
157
+
158
+ // callCheckParams examines the target of call expression 'ce' to see
159
+ // if it is making a call to the value passed in for some parameter.
160
+ func (pa *paramsAnalyzer) callCheckParams(ce *ir.CallExpr) {
161
+ switch ce.Op() {
162
+ case ir.OCALLINTER:
163
+ if ce.Op() != ir.OCALLINTER {
164
+ return
165
+ }
166
+ sel := ce.Fun.(*ir.SelectorExpr)
167
+ r := pa.staticValue(sel.X)
168
+ if r.Op() != ir.ONAME {
169
+ return
170
+ }
171
+ name := r.(*ir.Name)
172
+ if name.Class != ir.PPARAM {
173
+ return
174
+ }
175
+ pa.checkParams(r, ParamFeedsInterfaceMethodCall,
176
+ ParamMayFeedInterfaceMethodCall,
177
+ func(x ir.Node, p *ir.Name, idx int) (bool, bool) {
178
+ name := x.(*ir.Name)
179
+ return name == p, false
180
+ })
181
+ case ir.OCALLFUNC:
182
+ if ce.Fun.Op() != ir.ONAME {
183
+ return
184
+ }
185
+ called := ir.StaticValue(ce.Fun)
186
+ if called.Op() != ir.ONAME {
187
+ return
188
+ }
189
+ name := called.(*ir.Name)
190
+ if name.Class == ir.PPARAM {
191
+ pa.checkParams(called, ParamFeedsIndirectCall,
192
+ ParamMayFeedIndirectCall,
193
+ func(x ir.Node, p *ir.Name, idx int) (bool, bool) {
194
+ name := x.(*ir.Name)
195
+ return name == p, false
196
+ })
197
+ } else {
198
+ cname := pa.funcName(called)
199
+ if cname != nil {
200
+ pa.deriveFlagsFromCallee(ce, cname.Func)
201
+ }
202
+ }
203
+ }
204
+ }
205
+
206
+ // deriveFlagsFromCallee tries to derive flags for the current
207
+ // function based on a call this function makes to some other
208
+ // function. Example:
209
+ //
210
+ // /* Simple */ /* Derived from callee */
211
+ // func foo(f func(int)) { func foo(f func(int)) {
212
+ // f(2) bar(32, f)
213
+ // } }
214
+ // func bar(x int, f func()) {
215
+ // f(x)
216
+ // }
217
+ //
218
+ // Here we can set the "param feeds indirect call" flag for
219
+ // foo's param 'f' since we know that bar has that flag set for
220
+ // its second param, and we're passing that param a function.
221
+ func (pa *paramsAnalyzer) deriveFlagsFromCallee(ce *ir.CallExpr, callee *ir.Func) {
222
+ calleeProps := propsForFunc(callee)
223
+ if calleeProps == nil {
224
+ return
225
+ }
226
+ if debugTrace&debugTraceParams != 0 {
227
+ fmt.Fprintf(os.Stderr, "=-= callee props for %v:\n%s",
228
+ callee.Sym().Name, calleeProps.String())
229
+ }
230
+
231
+ must := []ParamPropBits{ParamFeedsInterfaceMethodCall, ParamFeedsIndirectCall, ParamFeedsIfOrSwitch}
232
+ may := []ParamPropBits{ParamMayFeedInterfaceMethodCall, ParamMayFeedIndirectCall, ParamMayFeedIfOrSwitch}
233
+
234
+ for pidx, arg := range ce.Args {
235
+ // Does the callee param have any interesting properties?
236
+ // If not we can skip this one.
237
+ pflag := calleeProps.ParamFlags[pidx]
238
+ if pflag == 0 {
239
+ continue
240
+ }
241
+ // See if one of the caller's parameters is flowing unmodified
242
+ // into this actual expression.
243
+ r := pa.staticValue(arg)
244
+ if r.Op() != ir.ONAME {
245
+ return
246
+ }
247
+ name := r.(*ir.Name)
248
+ if name.Class != ir.PPARAM {
249
+ return
250
+ }
251
+ callerParamIdx := pa.findParamIdx(name)
252
+ // note that callerParamIdx may return -1 in the case where
253
+ // the param belongs not to the current closure func we're
254
+ // analyzing but to an outer enclosing func.
255
+ if callerParamIdx == -1 {
256
+ return
257
+ }
258
+ if pa.params[callerParamIdx] == nil {
259
+ panic("something went wrong")
260
+ }
261
+ if !pa.top[callerParamIdx] &&
262
+ pa.values[callerParamIdx] == ParamNoInfo {
263
+ continue
264
+ }
265
+ if debugTrace&debugTraceParams != 0 {
266
+ fmt.Fprintf(os.Stderr, "=-= pflag for arg %d is %s\n",
267
+ pidx, pflag.String())
268
+ }
269
+ for i := range must {
270
+ mayv := may[i]
271
+ mustv := must[i]
272
+ if pflag&mustv != 0 && pa.condLevel == 0 {
273
+ pa.values[callerParamIdx] |= mustv
274
+ } else if pflag&(mustv|mayv) != 0 {
275
+ pa.values[callerParamIdx] |= mayv
276
+ }
277
+ }
278
+ pa.top[callerParamIdx] = false
279
+ }
280
+ }
281
+
282
+ func (pa *paramsAnalyzer) nodeVisitPost(n ir.Node) {
283
+ if len(pa.values) == 0 {
284
+ return
285
+ }
286
+ pa.condLevelTracker.post(n)
287
+ switch n.Op() {
288
+ case ir.OCALLFUNC:
289
+ ce := n.(*ir.CallExpr)
290
+ pa.callCheckParams(ce)
291
+ case ir.OCALLINTER:
292
+ ce := n.(*ir.CallExpr)
293
+ pa.callCheckParams(ce)
294
+ case ir.OIF:
295
+ ifst := n.(*ir.IfStmt)
296
+ pa.foldCheckParams(ifst.Cond)
297
+ case ir.OSWITCH:
298
+ swst := n.(*ir.SwitchStmt)
299
+ if swst.Tag != nil {
300
+ pa.foldCheckParams(swst.Tag)
301
+ }
302
+ }
303
+ }
304
+
305
+ func (pa *paramsAnalyzer) nodeVisitPre(n ir.Node) {
306
+ if len(pa.values) == 0 {
307
+ return
308
+ }
309
+ pa.condLevelTracker.pre(n)
310
+ }
311
+
312
+ // condLevelTracker helps keeps track very roughly of "level of conditional
313
+ // nesting", e.g. how many "if" statements you have to go through to
314
+ // get to the point where a given stmt executes. Example:
315
+ //
316
+ // cond nesting level
317
+ // func foo() {
318
+ // G = 1 0
319
+ // if x < 10 { 0
320
+ // if y < 10 { 1
321
+ // G = 0 2
322
+ // }
323
+ // }
324
+ // }
325
+ //
326
+ // The intent here is to provide some sort of very abstract relative
327
+ // hotness metric, e.g. "G = 1" above is expected to be executed more
328
+ // often than "G = 0" (in the aggregate, across large numbers of
329
+ // functions).
330
+ type condLevelTracker struct {
331
+ condLevel int
332
+ }
333
+
334
+ func (c *condLevelTracker) pre(n ir.Node) {
335
+ // Increment level of "conditional testing" if we see
336
+ // an "if" or switch statement, and decrement if in
337
+ // a loop.
338
+ switch n.Op() {
339
+ case ir.OIF, ir.OSWITCH:
340
+ c.condLevel++
341
+ case ir.OFOR, ir.ORANGE:
342
+ c.condLevel--
343
+ }
344
+ }
345
+
346
+ func (c *condLevelTracker) post(n ir.Node) {
347
+ switch n.Op() {
348
+ case ir.OFOR, ir.ORANGE:
349
+ c.condLevel++
350
+ case ir.OIF:
351
+ c.condLevel--
352
+ case ir.OSWITCH:
353
+ c.condLevel--
354
+ }
355
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/analyze_func_returns.go ADDED
@@ -0,0 +1,277 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import (
8
+ "cmd/compile/internal/ir"
9
+ "fmt"
10
+ "go/constant"
11
+ "go/token"
12
+ "os"
13
+ )
14
+
15
+ // resultsAnalyzer stores state information for the process of
16
+ // computing flags/properties for the return values of a specific Go
17
+ // function, as part of inline heuristics synthesis.
18
+ type resultsAnalyzer struct {
19
+ fname string
20
+ props []ResultPropBits
21
+ values []resultVal
22
+ inlineMaxBudget int
23
+ *nameFinder
24
+ }
25
+
26
+ // resultVal captures information about a specific result returned from
27
+ // the function we're analyzing; we are interested in cases where
28
+ // the func always returns the same constant, or always returns
29
+ // the same function, etc. This container stores info on a the specific
30
+ // scenarios we're looking for.
31
+ type resultVal struct {
32
+ cval constant.Value
33
+ fn *ir.Name
34
+ fnClo bool
35
+ top bool
36
+ derived bool // see deriveReturnFlagsFromCallee below
37
+ }
38
+
39
+ // addResultsAnalyzer creates a new resultsAnalyzer helper object for
40
+ // the function fn, appends it to the analyzers list, and returns the
41
+ // new list. If the function in question doesn't have any returns (or
42
+ // any interesting returns) then the analyzer list is left as is, and
43
+ // the result flags in "fp" are updated accordingly.
44
+ func addResultsAnalyzer(fn *ir.Func, analyzers []propAnalyzer, fp *FuncProps, inlineMaxBudget int, nf *nameFinder) []propAnalyzer {
45
+ ra, props := makeResultsAnalyzer(fn, inlineMaxBudget, nf)
46
+ if ra != nil {
47
+ analyzers = append(analyzers, ra)
48
+ } else {
49
+ fp.ResultFlags = props
50
+ }
51
+ return analyzers
52
+ }
53
+
54
+ // makeResultsAnalyzer creates a new helper object to analyze results
55
+ // in function fn. If the function doesn't have any interesting
56
+ // results, a nil helper is returned along with a set of default
57
+ // result flags for the func.
58
+ func makeResultsAnalyzer(fn *ir.Func, inlineMaxBudget int, nf *nameFinder) (*resultsAnalyzer, []ResultPropBits) {
59
+ results := fn.Type().Results()
60
+ if len(results) == 0 {
61
+ return nil, nil
62
+ }
63
+ props := make([]ResultPropBits, len(results))
64
+ if fn.Inl == nil {
65
+ return nil, props
66
+ }
67
+ vals := make([]resultVal, len(results))
68
+ interestingToAnalyze := false
69
+ for i := range results {
70
+ rt := results[i].Type
71
+ if !rt.IsScalar() && !rt.HasNil() {
72
+ // existing properties not applicable here (for things
73
+ // like structs, arrays, slices, etc).
74
+ continue
75
+ }
76
+ // set the "top" flag (as in "top element of data flow lattice")
77
+ // meaning "we have no info yet, but we might later on".
78
+ vals[i].top = true
79
+ interestingToAnalyze = true
80
+ }
81
+ if !interestingToAnalyze {
82
+ return nil, props
83
+ }
84
+ ra := &resultsAnalyzer{
85
+ props: props,
86
+ values: vals,
87
+ inlineMaxBudget: inlineMaxBudget,
88
+ nameFinder: nf,
89
+ }
90
+ return ra, nil
91
+ }
92
+
93
+ // setResults transfers the calculated result properties for this
94
+ // function to 'funcProps'.
95
+ func (ra *resultsAnalyzer) setResults(funcProps *FuncProps) {
96
+ // Promote ResultAlwaysSameFunc to ResultAlwaysSameInlinableFunc
97
+ for i := range ra.values {
98
+ if ra.props[i] == ResultAlwaysSameFunc && !ra.values[i].derived {
99
+ f := ra.values[i].fn.Func
100
+ // HACK: in order to allow for call site score
101
+ // adjustments, we used a relaxed inline budget in
102
+ // determining inlinability. For the check below, however,
103
+ // we want to know is whether the func in question is
104
+ // likely to be inlined, as opposed to whether it might
105
+ // possibly be inlined if all the right score adjustments
106
+ // happened, so do a simple check based on the cost.
107
+ if f.Inl != nil && f.Inl.Cost <= int32(ra.inlineMaxBudget) {
108
+ ra.props[i] = ResultAlwaysSameInlinableFunc
109
+ }
110
+ }
111
+ }
112
+ funcProps.ResultFlags = ra.props
113
+ }
114
+
115
+ func (ra *resultsAnalyzer) pessimize() {
116
+ for i := range ra.props {
117
+ ra.props[i] = ResultNoInfo
118
+ }
119
+ }
120
+
121
+ func (ra *resultsAnalyzer) nodeVisitPre(n ir.Node) {
122
+ }
123
+
124
+ func (ra *resultsAnalyzer) nodeVisitPost(n ir.Node) {
125
+ if len(ra.values) == 0 {
126
+ return
127
+ }
128
+ if n.Op() != ir.ORETURN {
129
+ return
130
+ }
131
+ if debugTrace&debugTraceResults != 0 {
132
+ fmt.Fprintf(os.Stderr, "=+= returns nodevis %v %s\n",
133
+ ir.Line(n), n.Op().String())
134
+ }
135
+
136
+ // No support currently for named results, so if we see an empty
137
+ // "return" stmt, be conservative.
138
+ rs := n.(*ir.ReturnStmt)
139
+ if len(rs.Results) != len(ra.values) {
140
+ ra.pessimize()
141
+ return
142
+ }
143
+ for i, r := range rs.Results {
144
+ ra.analyzeResult(i, r)
145
+ }
146
+ }
147
+
148
+ // analyzeResult examines the expression 'n' being returned as the
149
+ // 'ii'th argument in some return statement to see whether has
150
+ // interesting characteristics (for example, returns a constant), then
151
+ // applies a dataflow "meet" operation to combine this result with any
152
+ // previous result (for the given return slot) that we've already
153
+ // processed.
154
+ func (ra *resultsAnalyzer) analyzeResult(ii int, n ir.Node) {
155
+ isAllocMem := ra.isAllocatedMem(n)
156
+ isConcConvItf := ra.isConcreteConvIface(n)
157
+ constVal := ra.constValue(n)
158
+ isConst := (constVal != nil)
159
+ isNil := ra.isNil(n)
160
+ rfunc := ra.funcName(n)
161
+ isFunc := (rfunc != nil)
162
+ isClo := (rfunc != nil && rfunc.Func.OClosure != nil)
163
+ curp := ra.props[ii]
164
+ dprops, isDerivedFromCall := ra.deriveReturnFlagsFromCallee(n)
165
+ newp := ResultNoInfo
166
+ var newcval constant.Value
167
+ var newfunc *ir.Name
168
+
169
+ if debugTrace&debugTraceResults != 0 {
170
+ fmt.Fprintf(os.Stderr, "=-= %v: analyzeResult n=%s ismem=%v isconcconv=%v isconst=%v isnil=%v isfunc=%v isclo=%v\n", ir.Line(n), n.Op().String(), isAllocMem, isConcConvItf, isConst, isNil, isFunc, isClo)
171
+ }
172
+
173
+ if ra.values[ii].top {
174
+ ra.values[ii].top = false
175
+ // this is the first return we've seen; record
176
+ // whatever properties it has.
177
+ switch {
178
+ case isAllocMem:
179
+ newp = ResultIsAllocatedMem
180
+ case isConcConvItf:
181
+ newp = ResultIsConcreteTypeConvertedToInterface
182
+ case isFunc:
183
+ newp = ResultAlwaysSameFunc
184
+ newfunc = rfunc
185
+ case isConst:
186
+ newp = ResultAlwaysSameConstant
187
+ newcval = constVal
188
+ case isNil:
189
+ newp = ResultAlwaysSameConstant
190
+ newcval = nil
191
+ case isDerivedFromCall:
192
+ newp = dprops
193
+ ra.values[ii].derived = true
194
+ }
195
+ } else {
196
+ if !ra.values[ii].derived {
197
+ // this is not the first return we've seen; apply
198
+ // what amounts of a "meet" operator to combine
199
+ // the properties we see here with what we saw on
200
+ // the previous returns.
201
+ switch curp {
202
+ case ResultIsAllocatedMem:
203
+ if isAllocMem {
204
+ newp = ResultIsAllocatedMem
205
+ }
206
+ case ResultIsConcreteTypeConvertedToInterface:
207
+ if isConcConvItf {
208
+ newp = ResultIsConcreteTypeConvertedToInterface
209
+ }
210
+ case ResultAlwaysSameConstant:
211
+ if isNil && ra.values[ii].cval == nil {
212
+ newp = ResultAlwaysSameConstant
213
+ newcval = nil
214
+ } else if isConst && constant.Compare(constVal, token.EQL, ra.values[ii].cval) {
215
+ newp = ResultAlwaysSameConstant
216
+ newcval = constVal
217
+ }
218
+ case ResultAlwaysSameFunc:
219
+ if isFunc && isSameFuncName(rfunc, ra.values[ii].fn) {
220
+ newp = ResultAlwaysSameFunc
221
+ newfunc = rfunc
222
+ }
223
+ }
224
+ }
225
+ }
226
+ ra.values[ii].fn = newfunc
227
+ ra.values[ii].fnClo = isClo
228
+ ra.values[ii].cval = newcval
229
+ ra.props[ii] = newp
230
+
231
+ if debugTrace&debugTraceResults != 0 {
232
+ fmt.Fprintf(os.Stderr, "=-= %v: analyzeResult newp=%s\n",
233
+ ir.Line(n), newp)
234
+ }
235
+ }
236
+
237
+ // deriveReturnFlagsFromCallee tries to set properties for a given
238
+ // return result where we're returning call expression; return value
239
+ // is a return property value and a boolean indicating whether the
240
+ // prop is valid. Examples:
241
+ //
242
+ // func foo() int { return bar() }
243
+ // func bar() int { return 42 }
244
+ // func blix() int { return 43 }
245
+ // func two(y int) int {
246
+ // if y < 0 { return bar() } else { return blix() }
247
+ // }
248
+ //
249
+ // Since "foo" always returns the result of a call to "bar", we can
250
+ // set foo's return property to that of bar. In the case of "two", however,
251
+ // even though each return path returns a constant, we don't know
252
+ // whether the constants are identical, hence we need to be conservative.
253
+ func (ra *resultsAnalyzer) deriveReturnFlagsFromCallee(n ir.Node) (ResultPropBits, bool) {
254
+ if n.Op() != ir.OCALLFUNC {
255
+ return 0, false
256
+ }
257
+ ce := n.(*ir.CallExpr)
258
+ if ce.Fun.Op() != ir.ONAME {
259
+ return 0, false
260
+ }
261
+ called := ir.StaticValue(ce.Fun)
262
+ if called.Op() != ir.ONAME {
263
+ return 0, false
264
+ }
265
+ cname := ra.funcName(called)
266
+ if cname == nil {
267
+ return 0, false
268
+ }
269
+ calleeProps := propsForFunc(cname.Func)
270
+ if calleeProps == nil {
271
+ return 0, false
272
+ }
273
+ if len(calleeProps.ResultFlags) != 1 {
274
+ return 0, false
275
+ }
276
+ return calleeProps.ResultFlags[0], true
277
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/callsite.go ADDED
@@ -0,0 +1,149 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import (
8
+ "cmd/compile/internal/base"
9
+ "cmd/compile/internal/ir"
10
+ "cmd/internal/src"
11
+ "fmt"
12
+ "io"
13
+ "path/filepath"
14
+ "sort"
15
+ "strings"
16
+ )
17
+
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 {
28
+ Callee *ir.Func
29
+ Call *ir.CallExpr
30
+ parent *CallSite
31
+ Assign ir.Node
32
+ Flags CSPropBits
33
+
34
+ ArgProps []ActualExprPropBits
35
+ Score int
36
+ ScoreMask scoreAdjustTyp
37
+ ID uint
38
+ aux uint8
39
+ }
40
+
41
+ // CallSiteTab is a table of call sites, keyed by call expr.
42
+ // Ideally it would be nice to key the table by src.XPos, but
43
+ // this results in collisions for calls on very long lines (the
44
+ // front end saturates column numbers at 255). We also wind up
45
+ // with many calls that share the same auto-generated pos.
46
+ type CallSiteTab map[*ir.CallExpr]*CallSite
47
+
48
+ // ActualExprPropBits describes a property of an actual expression (value
49
+ // passed to some specific func argument at a call site).
50
+ type ActualExprPropBits uint8
51
+
52
+ const (
53
+ ActualExprConstant ActualExprPropBits = 1 << iota
54
+ ActualExprIsConcreteConvIface
55
+ ActualExprIsFunc
56
+ ActualExprIsInlinableFunc
57
+ )
58
+
59
+ type CSPropBits uint32
60
+
61
+ const (
62
+ CallSiteInLoop CSPropBits = 1 << iota
63
+ CallSiteOnPanicPath
64
+ CallSiteInInitFunc
65
+ )
66
+
67
+ type csAuxBits uint8
68
+
69
+ const (
70
+ csAuxInlined = 1 << iota
71
+ )
72
+
73
+ // encodedCallSiteTab is a table keyed by "encoded" callsite
74
+ // (stringified src.XPos plus call site ID) mapping to a value of call
75
+ // property bits and score.
76
+ type encodedCallSiteTab map[string]propsAndScore
77
+
78
+ type propsAndScore struct {
79
+ props CSPropBits
80
+ score int
81
+ mask scoreAdjustTyp
82
+ }
83
+
84
+ func (pas propsAndScore) String() string {
85
+ return fmt.Sprintf("P=%s|S=%d|M=%s", pas.props.String(),
86
+ pas.score, pas.mask.String())
87
+ }
88
+
89
+ func (cst CallSiteTab) merge(other CallSiteTab) error {
90
+ for k, v := range other {
91
+ if prev, ok := cst[k]; ok {
92
+ return fmt.Errorf("internal error: collision during call site table merge, fn=%s callsite=%s", prev.Callee.Sym().Name, fmtFullPos(prev.Call.Pos()))
93
+ }
94
+ cst[k] = v
95
+ }
96
+ return nil
97
+ }
98
+
99
+ func fmtFullPos(p src.XPos) string {
100
+ var sb strings.Builder
101
+ sep := ""
102
+ base.Ctxt.AllPos(p, func(pos src.Pos) {
103
+ fmt.Fprintf(&sb, sep)
104
+ sep = "|"
105
+ file := filepath.Base(pos.Filename())
106
+ fmt.Fprintf(&sb, "%s:%d:%d", file, pos.Line(), pos.Col())
107
+ })
108
+ return sb.String()
109
+ }
110
+
111
+ func EncodeCallSiteKey(cs *CallSite) string {
112
+ var sb strings.Builder
113
+ // FIXME: maybe rewrite line offsets relative to function start?
114
+ sb.WriteString(fmtFullPos(cs.Call.Pos()))
115
+ fmt.Fprintf(&sb, "|%d", cs.ID)
116
+ return sb.String()
117
+ }
118
+
119
+ func buildEncodedCallSiteTab(tab CallSiteTab) encodedCallSiteTab {
120
+ r := make(encodedCallSiteTab)
121
+ for _, cs := range tab {
122
+ k := EncodeCallSiteKey(cs)
123
+ r[k] = propsAndScore{
124
+ props: cs.Flags,
125
+ score: cs.Score,
126
+ mask: cs.ScoreMask,
127
+ }
128
+ }
129
+ return r
130
+ }
131
+
132
+ // dumpCallSiteComments emits comments into the dump file for the
133
+ // callsites in the function of interest. If "ecst" is non-nil, we use
134
+ // that, otherwise generated a fresh encodedCallSiteTab from "tab".
135
+ func dumpCallSiteComments(w io.Writer, tab CallSiteTab, ecst encodedCallSiteTab) {
136
+ if ecst == nil {
137
+ ecst = buildEncodedCallSiteTab(tab)
138
+ }
139
+ tags := make([]string, 0, len(ecst))
140
+ for k := range ecst {
141
+ tags = append(tags, k)
142
+ }
143
+ sort.Strings(tags)
144
+ for _, s := range tags {
145
+ v := ecst[s]
146
+ 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())
147
+ }
148
+ fmt.Fprintf(w, "// %s\n", csDelimiter)
149
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/cspropbits_string.go ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Code generated by "stringer -bitset -type CSPropBits"; DO NOT EDIT.
2
+
3
+ package inlheur
4
+
5
+ import "strconv"
6
+ import "bytes"
7
+
8
+ func _() {
9
+ // An "invalid array index" compiler error signifies that the constant values have changed.
10
+ // Re-run the stringer command to generate them again.
11
+ var x [1]struct{}
12
+ _ = x[CallSiteInLoop-1]
13
+ _ = x[CallSiteOnPanicPath-2]
14
+ _ = x[CallSiteInInitFunc-4]
15
+ }
16
+
17
+ var _CSPropBits_value = [...]uint64{
18
+ 0x1, /* CallSiteInLoop */
19
+ 0x2, /* CallSiteOnPanicPath */
20
+ 0x4, /* CallSiteInInitFunc */
21
+ }
22
+
23
+ const _CSPropBits_name = "CallSiteInLoopCallSiteOnPanicPathCallSiteInInitFunc"
24
+
25
+ var _CSPropBits_index = [...]uint8{0, 14, 33, 51}
26
+
27
+ func (i CSPropBits) String() string {
28
+ var b bytes.Buffer
29
+
30
+ remain := uint64(i)
31
+ seen := false
32
+
33
+ for k, v := range _CSPropBits_value {
34
+ x := _CSPropBits_name[_CSPropBits_index[k]:_CSPropBits_index[k+1]]
35
+ if v == 0 {
36
+ if i == 0 {
37
+ b.WriteString(x)
38
+ return b.String()
39
+ }
40
+ continue
41
+ }
42
+ if (v & remain) == v {
43
+ remain &^= v
44
+ x := _CSPropBits_name[_CSPropBits_index[k]:_CSPropBits_index[k+1]]
45
+ if seen {
46
+ b.WriteString("|")
47
+ }
48
+ seen = true
49
+ b.WriteString(x)
50
+ }
51
+ }
52
+ if remain == 0 {
53
+ return b.String()
54
+ }
55
+ return "CSPropBits(0x" + strconv.FormatInt(int64(i), 16) + ")"
56
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/debugflags_test.go ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import (
8
+ "testing"
9
+ )
10
+
11
+ func TestInlScoreAdjFlagParse(t *testing.T) {
12
+ scenarios := []struct {
13
+ value string
14
+ expok bool
15
+ }{
16
+ {
17
+ value: "returnFeedsConcreteToInterfaceCallAdj:9",
18
+ expok: true,
19
+ },
20
+ {
21
+ value: "panicPathAdj:-1/initFuncAdj:9",
22
+ expok: true,
23
+ },
24
+ {
25
+ value: "",
26
+ expok: false,
27
+ },
28
+ {
29
+ value: "nonsenseAdj:10",
30
+ expok: false,
31
+ },
32
+ {
33
+ value: "inLoopAdj:",
34
+ expok: false,
35
+ },
36
+ {
37
+ value: "inLoopAdj:10:10",
38
+ expok: false,
39
+ },
40
+ {
41
+ value: "inLoopAdj:blah",
42
+ expok: false,
43
+ },
44
+ {
45
+ value: "/",
46
+ expok: false,
47
+ },
48
+ }
49
+
50
+ for _, scenario := range scenarios {
51
+ err := parseScoreAdj(scenario.value)
52
+ t.Logf("for value=%q err is %v\n", scenario.value, err)
53
+ if scenario.expok {
54
+ if err != nil {
55
+ t.Errorf("expected parseScoreAdj(%s) ok, got err %v",
56
+ scenario.value, err)
57
+ }
58
+ } else {
59
+ if err == nil {
60
+ t.Errorf("expected parseScoreAdj(%s) failure, got success",
61
+ scenario.value)
62
+ }
63
+ }
64
+ }
65
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/dumpscores_test.go ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import (
8
+ "internal/testenv"
9
+ "os"
10
+ "path/filepath"
11
+ "strings"
12
+ "testing"
13
+ )
14
+
15
+ func TestDumpCallSiteScoreDump(t *testing.T) {
16
+ td := t.TempDir()
17
+ testenv.MustHaveGoBuild(t)
18
+
19
+ scenarios := []struct {
20
+ name string
21
+ promoted int
22
+ indirectlyPromoted int
23
+ demoted int
24
+ unchanged int
25
+ }{
26
+ {
27
+ name: "dumpscores",
28
+ promoted: 1,
29
+ indirectlyPromoted: 1,
30
+ demoted: 1,
31
+ unchanged: 5,
32
+ },
33
+ }
34
+
35
+ for _, scen := range scenarios {
36
+ dumpfile, err := gatherInlCallSitesScoresForFile(t, scen.name, td)
37
+ if err != nil {
38
+ t.Fatalf("dumping callsite scores for %q: error %v", scen.name, err)
39
+ }
40
+ var lines []string
41
+ if content, err := os.ReadFile(dumpfile); err != nil {
42
+ t.Fatalf("reading dump %q: error %v", dumpfile, err)
43
+ } else {
44
+ lines = strings.Split(string(content), "\n")
45
+ }
46
+ prom, indprom, dem, unch := 0, 0, 0, 0
47
+ for _, line := range lines {
48
+ switch {
49
+ case strings.TrimSpace(line) == "":
50
+ case !strings.Contains(line, "|"):
51
+ case strings.HasPrefix(line, "#"):
52
+ case strings.Contains(line, "PROMOTED"):
53
+ prom++
54
+ case strings.Contains(line, "INDPROM"):
55
+ indprom++
56
+ case strings.Contains(line, "DEMOTED"):
57
+ dem++
58
+ default:
59
+ unch++
60
+ }
61
+ }
62
+ showout := false
63
+ if prom != scen.promoted {
64
+ t.Errorf("testcase %q, got %d promoted want %d promoted",
65
+ scen.name, prom, scen.promoted)
66
+ showout = true
67
+ }
68
+ if indprom != scen.indirectlyPromoted {
69
+ t.Errorf("testcase %q, got %d indirectly promoted want %d",
70
+ scen.name, indprom, scen.indirectlyPromoted)
71
+ showout = true
72
+ }
73
+ if dem != scen.demoted {
74
+ t.Errorf("testcase %q, got %d demoted want %d demoted",
75
+ scen.name, dem, scen.demoted)
76
+ showout = true
77
+ }
78
+ if unch != scen.unchanged {
79
+ t.Errorf("testcase %q, got %d unchanged want %d unchanged",
80
+ scen.name, unch, scen.unchanged)
81
+ showout = true
82
+ }
83
+ if showout {
84
+ t.Logf(">> dump output: %s", strings.Join(lines, "\n"))
85
+ }
86
+ }
87
+ }
88
+
89
+ // gatherInlCallSitesScoresForFile builds the specified testcase 'testcase'
90
+ // from testdata/props passing the "-d=dumpinlcallsitescores=1"
91
+ // compiler option, to produce a dump, then returns the path of the
92
+ // newly created file.
93
+ func gatherInlCallSitesScoresForFile(t *testing.T, testcase string, td string) (string, error) {
94
+ t.Helper()
95
+ gopath := "testdata/" + testcase + ".go"
96
+ outpath := filepath.Join(td, testcase+".a")
97
+ dumpfile := filepath.Join(td, testcase+".callsites.txt")
98
+ run := []string{testenv.GoToolPath(t), "build",
99
+ "-gcflags=-d=dumpinlcallsitescores=1", "-o", outpath, gopath}
100
+ out, err := testenv.Command(t, run[0], run[1:]...).CombinedOutput()
101
+ t.Logf("run: %+v\n", run)
102
+ if err != nil {
103
+ return "", err
104
+ }
105
+ if err := os.WriteFile(dumpfile, out, 0666); err != nil {
106
+ return "", err
107
+ }
108
+ return dumpfile, err
109
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/eclassify.go ADDED
@@ -0,0 +1,247 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import (
8
+ "cmd/compile/internal/ir"
9
+ "fmt"
10
+ "os"
11
+ )
12
+
13
+ // ShouldFoldIfNameConstant analyzes expression tree 'e' to see
14
+ // whether it contains only combinations of simple references to all
15
+ // of the names in 'names' with selected constants + operators. The
16
+ // intent is to identify expression that could be folded away to a
17
+ // constant if the value of 'n' were available. Return value is TRUE
18
+ // if 'e' does look foldable given the value of 'n', and given that
19
+ // 'e' actually makes reference to 'n'. Some examples where the type
20
+ // of "n" is int64, type of "s" is string, and type of "p" is *byte:
21
+ //
22
+ // Simple? Expr
23
+ // yes n<10
24
+ // yes n*n-100
25
+ // yes (n < 10 || n > 100) && (n >= 12 || n <= 99 || n != 101)
26
+ // yes s == "foo"
27
+ // yes p == nil
28
+ // no n<foo()
29
+ // no n<1 || n>m
30
+ // no float32(n)<1.0
31
+ // no *p == 1
32
+ // no 1 + 100
33
+ // no 1 / n
34
+ // no 1 + unsafe.Sizeof(n)
35
+ //
36
+ // To avoid complexities (e.g. nan, inf) we stay way from folding and
37
+ // floating point or complex operations (integers, bools, and strings
38
+ // only). We also try to be conservative about avoiding any operation
39
+ // that might result in a panic at runtime, e.g. for "n" with type
40
+ // int64:
41
+ //
42
+ // 1<<(n-9) < 100/(n<<9999)
43
+ //
44
+ // we would return FALSE due to the negative shift count and/or
45
+ // potential divide by zero.
46
+ func ShouldFoldIfNameConstant(n ir.Node, names []*ir.Name) bool {
47
+ cl := makeExprClassifier(names)
48
+ var doNode func(ir.Node) bool
49
+ doNode = func(n ir.Node) bool {
50
+ ir.DoChildren(n, doNode)
51
+ cl.Visit(n)
52
+ return false
53
+ }
54
+ doNode(n)
55
+ if cl.getdisp(n) != exprSimple {
56
+ return false
57
+ }
58
+ for _, v := range cl.names {
59
+ if !v {
60
+ return false
61
+ }
62
+ }
63
+ return true
64
+ }
65
+
66
+ // exprClassifier holds intermediate state about nodes within an
67
+ // expression tree being analyzed by ShouldFoldIfNameConstant. Here
68
+ // "name" is the name node passed in, and "disposition" stores the
69
+ // result of classifying a given IR node.
70
+ type exprClassifier struct {
71
+ names map[*ir.Name]bool
72
+ disposition map[ir.Node]disp
73
+ }
74
+
75
+ type disp int
76
+
77
+ const (
78
+ // no info on this expr
79
+ exprNoInfo disp = iota
80
+
81
+ // expr contains only literals
82
+ exprLiterals
83
+
84
+ // expr is legal combination of literals and specified names
85
+ exprSimple
86
+ )
87
+
88
+ func (d disp) String() string {
89
+ switch d {
90
+ case exprNoInfo:
91
+ return "noinfo"
92
+ case exprSimple:
93
+ return "simple"
94
+ case exprLiterals:
95
+ return "literals"
96
+ default:
97
+ return fmt.Sprintf("unknown<%d>", d)
98
+ }
99
+ }
100
+
101
+ func makeExprClassifier(names []*ir.Name) *exprClassifier {
102
+ m := make(map[*ir.Name]bool, len(names))
103
+ for _, n := range names {
104
+ m[n] = false
105
+ }
106
+ return &exprClassifier{
107
+ names: m,
108
+ disposition: make(map[ir.Node]disp),
109
+ }
110
+ }
111
+
112
+ // Visit sets the classification for 'n' based on the previously
113
+ // calculated classifications for n's children, as part of a bottom-up
114
+ // walk over an expression tree.
115
+ func (ec *exprClassifier) Visit(n ir.Node) {
116
+
117
+ ndisp := exprNoInfo
118
+
119
+ binparts := func(n ir.Node) (ir.Node, ir.Node) {
120
+ if lex, ok := n.(*ir.LogicalExpr); ok {
121
+ return lex.X, lex.Y
122
+ } else if bex, ok := n.(*ir.BinaryExpr); ok {
123
+ return bex.X, bex.Y
124
+ } else {
125
+ panic("bad")
126
+ }
127
+ }
128
+
129
+ t := n.Type()
130
+ if t == nil {
131
+ if debugTrace&debugTraceExprClassify != 0 {
132
+ fmt.Fprintf(os.Stderr, "=-= *** untyped op=%s\n",
133
+ n.Op().String())
134
+ }
135
+ } else if t.IsInteger() || t.IsString() || t.IsBoolean() || t.HasNil() {
136
+ switch n.Op() {
137
+ // FIXME: maybe add support for OADDSTR?
138
+ case ir.ONIL:
139
+ ndisp = exprLiterals
140
+
141
+ case ir.OLITERAL:
142
+ if _, ok := n.(*ir.BasicLit); ok {
143
+ } else {
144
+ panic("unexpected")
145
+ }
146
+ ndisp = exprLiterals
147
+
148
+ case ir.ONAME:
149
+ nn := n.(*ir.Name)
150
+ if _, ok := ec.names[nn]; ok {
151
+ ndisp = exprSimple
152
+ ec.names[nn] = true
153
+ } else {
154
+ sv := ir.StaticValue(n)
155
+ if sv.Op() == ir.ONAME {
156
+ nn = sv.(*ir.Name)
157
+ }
158
+ if _, ok := ec.names[nn]; ok {
159
+ ndisp = exprSimple
160
+ ec.names[nn] = true
161
+ }
162
+ }
163
+
164
+ case ir.ONOT,
165
+ ir.OPLUS,
166
+ ir.ONEG:
167
+ uex := n.(*ir.UnaryExpr)
168
+ ndisp = ec.getdisp(uex.X)
169
+
170
+ case ir.OEQ,
171
+ ir.ONE,
172
+ ir.OLT,
173
+ ir.OGT,
174
+ ir.OGE,
175
+ ir.OLE:
176
+ // compare ops
177
+ x, y := binparts(n)
178
+ ndisp = ec.dispmeet(x, y)
179
+ if debugTrace&debugTraceExprClassify != 0 {
180
+ fmt.Fprintf(os.Stderr, "=-= meet(%s,%s) = %s for op=%s\n",
181
+ ec.getdisp(x), ec.getdisp(y), ec.dispmeet(x, y),
182
+ n.Op().String())
183
+ }
184
+ case ir.OLSH,
185
+ ir.ORSH,
186
+ ir.ODIV,
187
+ ir.OMOD:
188
+ x, y := binparts(n)
189
+ if ec.getdisp(y) == exprLiterals {
190
+ ndisp = ec.dispmeet(x, y)
191
+ }
192
+
193
+ case ir.OADD,
194
+ ir.OSUB,
195
+ ir.OOR,
196
+ ir.OXOR,
197
+ ir.OMUL,
198
+ ir.OAND,
199
+ ir.OANDNOT,
200
+ ir.OANDAND,
201
+ ir.OOROR:
202
+ x, y := binparts(n)
203
+ if debugTrace&debugTraceExprClassify != 0 {
204
+ fmt.Fprintf(os.Stderr, "=-= meet(%s,%s) = %s for op=%s\n",
205
+ ec.getdisp(x), ec.getdisp(y), ec.dispmeet(x, y),
206
+ n.Op().String())
207
+ }
208
+ ndisp = ec.dispmeet(x, y)
209
+ }
210
+ }
211
+
212
+ if debugTrace&debugTraceExprClassify != 0 {
213
+ fmt.Fprintf(os.Stderr, "=-= op=%s disp=%v\n", n.Op().String(),
214
+ ndisp.String())
215
+ }
216
+
217
+ ec.disposition[n] = ndisp
218
+ }
219
+
220
+ func (ec *exprClassifier) getdisp(x ir.Node) disp {
221
+ if d, ok := ec.disposition[x]; ok {
222
+ return d
223
+ } else {
224
+ panic("missing node from disp table")
225
+ }
226
+ }
227
+
228
+ // dispmeet performs a "meet" operation on the data flow states of
229
+ // node x and y (where the term "meet" is being drawn from traditional
230
+ // lattice-theoretical data flow analysis terminology).
231
+ func (ec *exprClassifier) dispmeet(x, y ir.Node) disp {
232
+ xd := ec.getdisp(x)
233
+ if xd == exprNoInfo {
234
+ return exprNoInfo
235
+ }
236
+ yd := ec.getdisp(y)
237
+ if yd == exprNoInfo {
238
+ return exprNoInfo
239
+ }
240
+ if xd == exprSimple || yd == exprSimple {
241
+ return exprSimple
242
+ }
243
+ if xd != exprLiterals || yd != exprLiterals {
244
+ panic("unexpected")
245
+ }
246
+ return exprLiterals
247
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/funcprop_string.go ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import (
8
+ "fmt"
9
+ "strings"
10
+ )
11
+
12
+ func (fp *FuncProps) String() string {
13
+ return fp.ToString("")
14
+ }
15
+
16
+ func (fp *FuncProps) ToString(prefix string) string {
17
+ var sb strings.Builder
18
+ if fp.Flags != 0 {
19
+ fmt.Fprintf(&sb, "%sFlags %s\n", prefix, fp.Flags)
20
+ }
21
+ flagSliceToSB[ParamPropBits](&sb, fp.ParamFlags,
22
+ prefix, "ParamFlags")
23
+ flagSliceToSB[ResultPropBits](&sb, fp.ResultFlags,
24
+ prefix, "ResultFlags")
25
+ return sb.String()
26
+ }
27
+
28
+ func flagSliceToSB[T interface {
29
+ ~uint32
30
+ String() string
31
+ }](sb *strings.Builder, sl []T, prefix string, tag string) {
32
+ var sb2 strings.Builder
33
+ foundnz := false
34
+ fmt.Fprintf(&sb2, "%s%s\n", prefix, tag)
35
+ for i, e := range sl {
36
+ if e != 0 {
37
+ foundnz = true
38
+ }
39
+ fmt.Fprintf(&sb2, "%s %d %s\n", prefix, i, e.String())
40
+ }
41
+ if foundnz {
42
+ sb.WriteString(sb2.String())
43
+ }
44
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/funcpropbits_string.go ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ // Code generated by "stringer -bitset -type FuncPropBits"; DO NOT EDIT.
6
+
7
+ package inlheur
8
+
9
+ import (
10
+ "bytes"
11
+ "strconv"
12
+ )
13
+
14
+ func _() {
15
+ // An "invalid array index" compiler error signifies that the constant values have changed.
16
+ // Re-run the stringer command to generate them again.
17
+ var x [1]struct{}
18
+ _ = x[FuncPropNeverReturns-1]
19
+ }
20
+
21
+ var _FuncPropBits_value = [...]uint64{
22
+ 0x1, /* FuncPropNeverReturns */
23
+ }
24
+
25
+ const _FuncPropBits_name = "FuncPropNeverReturns"
26
+
27
+ var _FuncPropBits_index = [...]uint8{0, 20}
28
+
29
+ func (i FuncPropBits) String() string {
30
+ var b bytes.Buffer
31
+
32
+ remain := uint64(i)
33
+ seen := false
34
+
35
+ for k, v := range _FuncPropBits_value {
36
+ x := _FuncPropBits_name[_FuncPropBits_index[k]:_FuncPropBits_index[k+1]]
37
+ if v == 0 {
38
+ if i == 0 {
39
+ b.WriteString(x)
40
+ return b.String()
41
+ }
42
+ continue
43
+ }
44
+ if (v & remain) == v {
45
+ remain &^= v
46
+ x := _FuncPropBits_name[_FuncPropBits_index[k]:_FuncPropBits_index[k+1]]
47
+ if seen {
48
+ b.WriteString("|")
49
+ }
50
+ seen = true
51
+ b.WriteString(x)
52
+ }
53
+ }
54
+ if remain == 0 {
55
+ return b.String()
56
+ }
57
+ return "FuncPropBits(0x" + strconv.FormatInt(int64(i), 16) + ")"
58
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/funcprops_test.go ADDED
@@ -0,0 +1,530 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import (
8
+ "bufio"
9
+ "encoding/json"
10
+ "flag"
11
+ "fmt"
12
+ "internal/testenv"
13
+ "os"
14
+ "path/filepath"
15
+ "regexp"
16
+ "strconv"
17
+ "strings"
18
+ "testing"
19
+ "time"
20
+ )
21
+
22
+ var remasterflag = flag.Bool("update-expected", false, "if true, generate updated golden results in testcases for all props tests")
23
+
24
+ func TestFuncProperties(t *testing.T) {
25
+ td := t.TempDir()
26
+ // td = "/tmp/qqq"
27
+ // os.RemoveAll(td)
28
+ // os.Mkdir(td, 0777)
29
+ testenv.MustHaveGoBuild(t)
30
+
31
+ // NOTE: this testpoint has the unfortunate characteristic that it
32
+ // relies on the installed compiler, meaning that if you make
33
+ // changes to the inline heuristics code in your working copy and
34
+ // then run the test, it will test the installed compiler and not
35
+ // your local modifications. TODO: decide whether to convert this
36
+ // to building a fresh compiler on the fly, or using some other
37
+ // scheme.
38
+
39
+ testcases := []string{"funcflags", "returns", "params",
40
+ "acrosscall", "calls", "returns2"}
41
+ for _, tc := range testcases {
42
+ dumpfile, err := gatherPropsDumpForFile(t, tc, td)
43
+ if err != nil {
44
+ t.Fatalf("dumping func props for %q: error %v", tc, err)
45
+ }
46
+ // Read in the newly generated dump.
47
+ dentries, dcsites, derr := readDump(t, dumpfile)
48
+ if derr != nil {
49
+ t.Fatalf("reading func prop dump: %v", derr)
50
+ }
51
+ if *remasterflag {
52
+ updateExpected(t, tc, dentries, dcsites)
53
+ continue
54
+ }
55
+ // Generate expected dump.
56
+ epath, egerr := genExpected(td, tc)
57
+ if egerr != nil {
58
+ t.Fatalf("generating expected func prop dump: %v", egerr)
59
+ }
60
+ // Read in the expected result entries.
61
+ eentries, ecsites, eerr := readDump(t, epath)
62
+ if eerr != nil {
63
+ t.Fatalf("reading expected func prop dump: %v", eerr)
64
+ }
65
+ // Compare new vs expected.
66
+ n := len(dentries)
67
+ eidx := 0
68
+ for i := 0; i < n; i++ {
69
+ dentry := dentries[i]
70
+ dcst := dcsites[i]
71
+ if !interestingToCompare(dentry.fname) {
72
+ continue
73
+ }
74
+ if eidx >= len(eentries) {
75
+ t.Errorf("testcase %s missing expected entry for %s, skipping", tc, dentry.fname)
76
+ continue
77
+ }
78
+ eentry := eentries[eidx]
79
+ ecst := ecsites[eidx]
80
+ eidx++
81
+ if dentry.fname != eentry.fname {
82
+ t.Errorf("got fn %q wanted %q, skipping checks",
83
+ dentry.fname, eentry.fname)
84
+ continue
85
+ }
86
+ compareEntries(t, tc, &dentry, dcst, &eentry, ecst)
87
+ }
88
+ }
89
+ }
90
+
91
+ func propBitsToString[T interface{ String() string }](sl []T) string {
92
+ var sb strings.Builder
93
+ for i, f := range sl {
94
+ fmt.Fprintf(&sb, "%d: %s\n", i, f.String())
95
+ }
96
+ return sb.String()
97
+ }
98
+
99
+ func compareEntries(t *testing.T, tc string, dentry *fnInlHeur, dcsites encodedCallSiteTab, eentry *fnInlHeur, ecsites encodedCallSiteTab) {
100
+ dfp := dentry.props
101
+ efp := eentry.props
102
+ dfn := dentry.fname
103
+
104
+ // Compare function flags.
105
+ if dfp.Flags != efp.Flags {
106
+ t.Errorf("testcase %q: Flags mismatch for %q: got %s, wanted %s",
107
+ tc, dfn, dfp.Flags.String(), efp.Flags.String())
108
+ }
109
+ // Compare returns
110
+ rgot := propBitsToString[ResultPropBits](dfp.ResultFlags)
111
+ rwant := propBitsToString[ResultPropBits](efp.ResultFlags)
112
+ if rgot != rwant {
113
+ t.Errorf("testcase %q: Results mismatch for %q: got:\n%swant:\n%s",
114
+ tc, dfn, rgot, rwant)
115
+ }
116
+ // Compare receiver + params.
117
+ pgot := propBitsToString[ParamPropBits](dfp.ParamFlags)
118
+ pwant := propBitsToString[ParamPropBits](efp.ParamFlags)
119
+ if pgot != pwant {
120
+ t.Errorf("testcase %q: Params mismatch for %q: got:\n%swant:\n%s",
121
+ tc, dfn, pgot, pwant)
122
+ }
123
+ // Compare call sites.
124
+ for k, ve := range ecsites {
125
+ if vd, ok := dcsites[k]; !ok {
126
+ t.Errorf("testcase %q missing expected callsite %q in func %q", tc, k, dfn)
127
+ continue
128
+ } else {
129
+ if vd != ve {
130
+ t.Errorf("testcase %q callsite %q in func %q: got %+v want %+v",
131
+ tc, k, dfn, vd.String(), ve.String())
132
+ }
133
+ }
134
+ }
135
+ for k := range dcsites {
136
+ if _, ok := ecsites[k]; !ok {
137
+ t.Errorf("testcase %q unexpected extra callsite %q in func %q", tc, k, dfn)
138
+ }
139
+ }
140
+ }
141
+
142
+ type dumpReader struct {
143
+ s *bufio.Scanner
144
+ t *testing.T
145
+ p string
146
+ ln int
147
+ }
148
+
149
+ // readDump reads in the contents of a dump file produced
150
+ // by the "-d=dumpinlfuncprops=..." command line flag by the Go
151
+ // compiler. It breaks the dump down into separate sections
152
+ // by function, then deserializes each func section into a
153
+ // fnInlHeur object and returns a slice of those objects.
154
+ func readDump(t *testing.T, path string) ([]fnInlHeur, []encodedCallSiteTab, error) {
155
+ content, err := os.ReadFile(path)
156
+ if err != nil {
157
+ return nil, nil, err
158
+ }
159
+ dr := &dumpReader{
160
+ s: bufio.NewScanner(strings.NewReader(string(content))),
161
+ t: t,
162
+ p: path,
163
+ ln: 1,
164
+ }
165
+ // consume header comment until preamble delimiter.
166
+ found := false
167
+ for dr.scan() {
168
+ if dr.curLine() == preambleDelimiter {
169
+ found = true
170
+ break
171
+ }
172
+ }
173
+ if !found {
174
+ return nil, nil, fmt.Errorf("malformed testcase file %s, missing preamble delimiter", path)
175
+ }
176
+ res := []fnInlHeur{}
177
+ csres := []encodedCallSiteTab{}
178
+ for {
179
+ dentry, dcst, err := dr.readEntry()
180
+ if err != nil {
181
+ t.Fatalf("reading func prop dump: %v", err)
182
+ }
183
+ if dentry.fname == "" {
184
+ break
185
+ }
186
+ res = append(res, dentry)
187
+ csres = append(csres, dcst)
188
+ }
189
+ return res, csres, nil
190
+ }
191
+
192
+ func (dr *dumpReader) scan() bool {
193
+ v := dr.s.Scan()
194
+ if v {
195
+ dr.ln++
196
+ }
197
+ return v
198
+ }
199
+
200
+ func (dr *dumpReader) curLine() string {
201
+ res := strings.TrimSpace(dr.s.Text())
202
+ if !strings.HasPrefix(res, "// ") {
203
+ dr.t.Fatalf("malformed line %s:%d, no comment: %s", dr.p, dr.ln, res)
204
+ }
205
+ return res[3:]
206
+ }
207
+
208
+ // readObjBlob reads in a series of commented lines until
209
+ // it hits a delimiter, then returns the contents of the comments.
210
+ func (dr *dumpReader) readObjBlob(delim string) (string, error) {
211
+ var sb strings.Builder
212
+ foundDelim := false
213
+ for dr.scan() {
214
+ line := dr.curLine()
215
+ if delim == line {
216
+ foundDelim = true
217
+ break
218
+ }
219
+ sb.WriteString(line + "\n")
220
+ }
221
+ if err := dr.s.Err(); err != nil {
222
+ return "", err
223
+ }
224
+ if !foundDelim {
225
+ return "", fmt.Errorf("malformed input %s, missing delimiter %q",
226
+ dr.p, delim)
227
+ }
228
+ return sb.String(), nil
229
+ }
230
+
231
+ // readEntry reads a single function's worth of material from
232
+ // a file produced by the "-d=dumpinlfuncprops=..." command line
233
+ // flag. It deserializes the json for the func properties and
234
+ // returns the resulting properties and function name. EOF is
235
+ // signaled by a nil FuncProps return (with no error
236
+ func (dr *dumpReader) readEntry() (fnInlHeur, encodedCallSiteTab, error) {
237
+ var funcInlHeur fnInlHeur
238
+ var callsites encodedCallSiteTab
239
+ if !dr.scan() {
240
+ return funcInlHeur, callsites, nil
241
+ }
242
+ // first line contains info about function: file/name/line
243
+ info := dr.curLine()
244
+ chunks := strings.Fields(info)
245
+ funcInlHeur.file = chunks[0]
246
+ funcInlHeur.fname = chunks[1]
247
+ if _, err := fmt.Sscanf(chunks[2], "%d", &funcInlHeur.line); err != nil {
248
+ return funcInlHeur, callsites, fmt.Errorf("scanning line %q: %v", info, err)
249
+ }
250
+ // consume comments until and including delimiter
251
+ for {
252
+ if !dr.scan() {
253
+ break
254
+ }
255
+ if dr.curLine() == comDelimiter {
256
+ break
257
+ }
258
+ }
259
+
260
+ // Consume JSON for encoded props.
261
+ dr.scan()
262
+ line := dr.curLine()
263
+ fp := &FuncProps{}
264
+ if err := json.Unmarshal([]byte(line), fp); err != nil {
265
+ return funcInlHeur, callsites, err
266
+ }
267
+ funcInlHeur.props = fp
268
+
269
+ // Consume callsites.
270
+ callsites = make(encodedCallSiteTab)
271
+ for dr.scan() {
272
+ line := dr.curLine()
273
+ if line == csDelimiter {
274
+ break
275
+ }
276
+ // expected format: "// callsite: <expanded pos> flagstr <desc> flagval <flags> score <score> mask <scoremask> maskstr <scoremaskstring>"
277
+ fields := strings.Fields(line)
278
+ if len(fields) != 12 {
279
+ return funcInlHeur, nil, fmt.Errorf("malformed callsite (nf=%d) %s line %d: %s", len(fields), dr.p, dr.ln, line)
280
+ }
281
+ if fields[2] != "flagstr" || fields[4] != "flagval" || fields[6] != "score" || fields[8] != "mask" || fields[10] != "maskstr" {
282
+ return funcInlHeur, nil, fmt.Errorf("malformed callsite %s line %d: %s",
283
+ dr.p, dr.ln, line)
284
+ }
285
+ tag := fields[1]
286
+ flagstr := fields[5]
287
+ flags, err := strconv.Atoi(flagstr)
288
+ if err != nil {
289
+ return funcInlHeur, nil, fmt.Errorf("bad flags val %s line %d: %q err=%v",
290
+ dr.p, dr.ln, line, err)
291
+ }
292
+ scorestr := fields[7]
293
+ score, err2 := strconv.Atoi(scorestr)
294
+ if err2 != nil {
295
+ return funcInlHeur, nil, fmt.Errorf("bad score val %s line %d: %q err=%v",
296
+ dr.p, dr.ln, line, err2)
297
+ }
298
+ maskstr := fields[9]
299
+ mask, err3 := strconv.Atoi(maskstr)
300
+ if err3 != nil {
301
+ return funcInlHeur, nil, fmt.Errorf("bad mask val %s line %d: %q err=%v",
302
+ dr.p, dr.ln, line, err3)
303
+ }
304
+ callsites[tag] = propsAndScore{
305
+ props: CSPropBits(flags),
306
+ score: score,
307
+ mask: scoreAdjustTyp(mask),
308
+ }
309
+ }
310
+
311
+ // Consume function delimiter.
312
+ dr.scan()
313
+ line = dr.curLine()
314
+ if line != fnDelimiter {
315
+ return funcInlHeur, nil, fmt.Errorf("malformed testcase file %q, missing delimiter %q", dr.p, fnDelimiter)
316
+ }
317
+
318
+ return funcInlHeur, callsites, nil
319
+ }
320
+
321
+ // gatherPropsDumpForFile builds the specified testcase 'testcase' from
322
+ // testdata/props passing the "-d=dumpinlfuncprops=..." compiler option,
323
+ // to produce a properties dump, then returns the path of the newly
324
+ // created file. NB: we can't use "go tool compile" here, since
325
+ // some of the test cases import stdlib packages (such as "os").
326
+ // This means using "go build", which is problematic since the
327
+ // Go command can potentially cache the results of the compile step,
328
+ // causing the test to fail when being run interactively. E.g.
329
+ //
330
+ // $ rm -f dump.txt
331
+ // $ go build -o foo.a -gcflags=-d=dumpinlfuncprops=dump.txt foo.go
332
+ // $ rm -f dump.txt foo.a
333
+ // $ go build -o foo.a -gcflags=-d=dumpinlfuncprops=dump.txt foo.go
334
+ // $ ls foo.a dump.txt > /dev/null
335
+ // ls : cannot access 'dump.txt': No such file or directory
336
+ // $
337
+ //
338
+ // For this reason, pick a unique filename for the dump, so as to
339
+ // defeat the caching.
340
+ func gatherPropsDumpForFile(t *testing.T, testcase string, td string) (string, error) {
341
+ t.Helper()
342
+ gopath := "testdata/props/" + testcase + ".go"
343
+ outpath := filepath.Join(td, testcase+".a")
344
+ salt := fmt.Sprintf(".p%dt%d", os.Getpid(), time.Now().UnixNano())
345
+ dumpfile := filepath.Join(td, testcase+salt+".dump.txt")
346
+ run := []string{testenv.GoToolPath(t), "build",
347
+ "-gcflags=-d=dumpinlfuncprops=" + dumpfile, "-o", outpath, gopath}
348
+ out, err := testenv.Command(t, run[0], run[1:]...).CombinedOutput()
349
+ if err != nil {
350
+ t.Logf("compile command: %+v", run)
351
+ }
352
+ if strings.TrimSpace(string(out)) != "" {
353
+ t.Logf("%s", out)
354
+ }
355
+ return dumpfile, err
356
+ }
357
+
358
+ // genExpected reads in a given Go testcase file, strips out all the
359
+ // unindented (column 0) commands, writes them out to a new file, and
360
+ // returns the path of that new file. By picking out just the comments
361
+ // from the Go file we wind up with something that resembles the
362
+ // output from a "-d=dumpinlfuncprops=..." compilation.
363
+ func genExpected(td string, testcase string) (string, error) {
364
+ epath := filepath.Join(td, testcase+".expected")
365
+ outf, err := os.OpenFile(epath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
366
+ if err != nil {
367
+ return "", err
368
+ }
369
+ gopath := "testdata/props/" + testcase + ".go"
370
+ content, err := os.ReadFile(gopath)
371
+ if err != nil {
372
+ return "", err
373
+ }
374
+ lines := strings.Split(string(content), "\n")
375
+ for _, line := range lines[3:] {
376
+ if !strings.HasPrefix(line, "// ") {
377
+ continue
378
+ }
379
+ fmt.Fprintf(outf, "%s\n", line)
380
+ }
381
+ if err := outf.Close(); err != nil {
382
+ return "", err
383
+ }
384
+ return epath, nil
385
+ }
386
+
387
+ type upexState struct {
388
+ dentries []fnInlHeur
389
+ newgolines []string
390
+ atline map[uint]uint
391
+ }
392
+
393
+ func mkUpexState(dentries []fnInlHeur) *upexState {
394
+ atline := make(map[uint]uint)
395
+ for _, e := range dentries {
396
+ atline[e.line] = atline[e.line] + 1
397
+ }
398
+ return &upexState{
399
+ dentries: dentries,
400
+ atline: atline,
401
+ }
402
+ }
403
+
404
+ // updateExpected takes a given Go testcase file X.go and writes out a
405
+ // new/updated version of the file to X.go.new, where the column-0
406
+ // "expected" comments have been updated using fresh data from
407
+ // "dentries".
408
+ //
409
+ // Writing of expected results is complicated by closures and by
410
+ // generics, where you can have multiple functions that all share the
411
+ // same starting line. Currently we combine up all the dups and
412
+ // closures into the single pre-func comment.
413
+ func updateExpected(t *testing.T, testcase string, dentries []fnInlHeur, dcsites []encodedCallSiteTab) {
414
+ nd := len(dentries)
415
+
416
+ ues := mkUpexState(dentries)
417
+
418
+ gopath := "testdata/props/" + testcase + ".go"
419
+ newgopath := "testdata/props/" + testcase + ".go.new"
420
+
421
+ // Read the existing Go file.
422
+ content, err := os.ReadFile(gopath)
423
+ if err != nil {
424
+ t.Fatalf("opening %s: %v", gopath, err)
425
+ }
426
+ golines := strings.Split(string(content), "\n")
427
+
428
+ // Preserve copyright.
429
+ ues.newgolines = append(ues.newgolines, golines[:4]...)
430
+ if !strings.HasPrefix(golines[0], "// Copyright") {
431
+ t.Fatalf("missing copyright from existing testcase")
432
+ }
433
+ golines = golines[4:]
434
+
435
+ clore := regexp.MustCompile(`.+\.func\d+[\.\d]*$`)
436
+
437
+ emitFunc := func(e *fnInlHeur, dcsites encodedCallSiteTab,
438
+ instance, atl uint) {
439
+ var sb strings.Builder
440
+ dumpFnPreamble(&sb, e, dcsites, instance, atl)
441
+ ues.newgolines = append(ues.newgolines,
442
+ strings.Split(strings.TrimSpace(sb.String()), "\n")...)
443
+ }
444
+
445
+ // Write file preamble with "DO NOT EDIT" message and such.
446
+ var sb strings.Builder
447
+ dumpFilePreamble(&sb)
448
+ ues.newgolines = append(ues.newgolines,
449
+ strings.Split(strings.TrimSpace(sb.String()), "\n")...)
450
+
451
+ // Helper to add a clump of functions to the output file.
452
+ processClump := func(idx int, emit bool) int {
453
+ // Process func itself, plus anything else defined
454
+ // on the same line
455
+ atl := ues.atline[dentries[idx].line]
456
+ for k := uint(0); k < atl; k++ {
457
+ if emit {
458
+ emitFunc(&dentries[idx], dcsites[idx], k, atl)
459
+ }
460
+ idx++
461
+ }
462
+ // now process any closures it contains
463
+ ncl := 0
464
+ for idx < nd {
465
+ nfn := dentries[idx].fname
466
+ if !clore.MatchString(nfn) {
467
+ break
468
+ }
469
+ ncl++
470
+ if emit {
471
+ emitFunc(&dentries[idx], dcsites[idx], 0, 1)
472
+ }
473
+ idx++
474
+ }
475
+ return idx
476
+ }
477
+
478
+ didx := 0
479
+ for _, line := range golines {
480
+ if strings.HasPrefix(line, "func ") {
481
+
482
+ // We have a function definition.
483
+ // Pick out the corresponding entry or entries in the dump
484
+ // and emit if interesting (or skip if not).
485
+ dentry := dentries[didx]
486
+ emit := interestingToCompare(dentry.fname)
487
+ didx = processClump(didx, emit)
488
+ }
489
+
490
+ // Consume all existing comments.
491
+ if strings.HasPrefix(line, "//") {
492
+ continue
493
+ }
494
+ ues.newgolines = append(ues.newgolines, line)
495
+ }
496
+
497
+ if didx != nd {
498
+ t.Logf("didx=%d wanted %d", didx, nd)
499
+ }
500
+
501
+ // Open new Go file and write contents.
502
+ of, err := os.OpenFile(newgopath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
503
+ if err != nil {
504
+ t.Fatalf("opening %s: %v", newgopath, err)
505
+ }
506
+ fmt.Fprintf(of, "%s", strings.Join(ues.newgolines, "\n"))
507
+ if err := of.Close(); err != nil {
508
+ t.Fatalf("closing %s: %v", newgopath, err)
509
+ }
510
+
511
+ t.Logf("update-expected: emitted updated file %s", newgopath)
512
+ t.Logf("please compare the two files, then overwrite %s with %s\n",
513
+ gopath, newgopath)
514
+ }
515
+
516
+ // interestingToCompare returns TRUE if we want to compare results
517
+ // for function 'fname'.
518
+ func interestingToCompare(fname string) bool {
519
+ if strings.HasPrefix(fname, "init.") {
520
+ return true
521
+ }
522
+ if strings.HasPrefix(fname, "T_") {
523
+ return true
524
+ }
525
+ f := strings.Split(fname, ".")
526
+ if len(f) == 2 && strings.HasPrefix(f[1], "T_") {
527
+ return true
528
+ }
529
+ return false
530
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/function_properties.go ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package 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.
32
+ type FuncProps struct {
33
+ Flags FuncPropBits
34
+ ParamFlags []ParamPropBits // slot 0 receiver if applicable
35
+ ResultFlags []ResultPropBits
36
+ }
37
+
38
+ type FuncPropBits uint32
39
+
40
+ const (
41
+ // Function always panics or invokes os.Exit() or a func that does
42
+ // likewise.
43
+ FuncPropNeverReturns FuncPropBits = 1 << iota
44
+ )
45
+
46
+ type ParamPropBits uint32
47
+
48
+ const (
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
+
82
+ type ResultPropBits uint32
83
+
84
+ const (
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
+ )
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/names.go ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import (
8
+ "cmd/compile/internal/ir"
9
+ "go/constant"
10
+ )
11
+
12
+ // nameFinder provides a set of "isXXX" query methods for clients to
13
+ // ask whether a given AST node corresponds to a function, a constant
14
+ // value, and so on. These methods use an underlying ir.ReassignOracle
15
+ // to return more precise results in cases where an "interesting"
16
+ // value is assigned to a singly-defined local temp. Example:
17
+ //
18
+ // const q = 101
19
+ // fq := func() int { return q }
20
+ // copyOfConstant := q
21
+ // copyOfFunc := f
22
+ // interestingCall(copyOfConstant, copyOfFunc)
23
+ //
24
+ // A name finder query method invoked on the arguments being passed to
25
+ // "interestingCall" will be able detect that 'copyOfConstant' always
26
+ // evaluates to a constant (even though it is in fact a PAUTO local
27
+ // variable). A given nameFinder can also operate without using
28
+ // ir.ReassignOracle (in cases where it is not practical to look
29
+ // at the entire function); in such cases queries will still work
30
+ // for explicit constant values and functions.
31
+ type nameFinder struct {
32
+ ro *ir.ReassignOracle
33
+ }
34
+
35
+ // newNameFinder returns a new nameFinder object with a reassignment
36
+ // oracle initialized based on the function fn, or if fn is nil,
37
+ // without an underlying ReassignOracle.
38
+ func newNameFinder(fn *ir.Func) *nameFinder {
39
+ var ro *ir.ReassignOracle
40
+ if fn != nil {
41
+ ro = &ir.ReassignOracle{}
42
+ ro.Init(fn)
43
+ }
44
+ return &nameFinder{ro: ro}
45
+ }
46
+
47
+ // funcName returns the *ir.Name for the func or method
48
+ // corresponding to node 'n', or nil if n can't be proven
49
+ // to contain a function value.
50
+ func (nf *nameFinder) funcName(n ir.Node) *ir.Name {
51
+ sv := n
52
+ if nf.ro != nil {
53
+ sv = nf.ro.StaticValue(n)
54
+ }
55
+ if name := ir.StaticCalleeName(sv); name != nil {
56
+ return name
57
+ }
58
+ return nil
59
+ }
60
+
61
+ // isAllocatedMem returns true if node n corresponds to a memory
62
+ // allocation expression (make, new, or equivalent).
63
+ func (nf *nameFinder) isAllocatedMem(n ir.Node) bool {
64
+ sv := n
65
+ if nf.ro != nil {
66
+ sv = nf.ro.StaticValue(n)
67
+ }
68
+ switch sv.Op() {
69
+ case ir.OMAKESLICE, ir.ONEW, ir.OPTRLIT, ir.OSLICELIT:
70
+ return true
71
+ }
72
+ return false
73
+ }
74
+
75
+ // constValue returns the underlying constant.Value for an AST node n
76
+ // if n is itself a constant value/expr, or if n is a singly assigned
77
+ // local containing constant expr/value (or nil not constant).
78
+ func (nf *nameFinder) constValue(n ir.Node) constant.Value {
79
+ sv := n
80
+ if nf.ro != nil {
81
+ sv = nf.ro.StaticValue(n)
82
+ }
83
+ if sv.Op() == ir.OLITERAL {
84
+ return sv.Val()
85
+ }
86
+ return nil
87
+ }
88
+
89
+ // isNil returns whether n is nil (or singly
90
+ // assigned local containing nil).
91
+ func (nf *nameFinder) isNil(n ir.Node) bool {
92
+ sv := n
93
+ if nf.ro != nil {
94
+ sv = nf.ro.StaticValue(n)
95
+ }
96
+ return sv.Op() == ir.ONIL
97
+ }
98
+
99
+ func (nf *nameFinder) staticValue(n ir.Node) ir.Node {
100
+ if nf.ro == nil {
101
+ return n
102
+ }
103
+ return nf.ro.StaticValue(n)
104
+ }
105
+
106
+ func (nf *nameFinder) reassigned(n *ir.Name) bool {
107
+ if nf.ro == nil {
108
+ return true
109
+ }
110
+ return nf.ro.Reassigned(n)
111
+ }
112
+
113
+ func (nf *nameFinder) isConcreteConvIface(n ir.Node) bool {
114
+ sv := n
115
+ if nf.ro != nil {
116
+ sv = nf.ro.StaticValue(n)
117
+ }
118
+ if sv.Op() != ir.OCONVIFACE {
119
+ return false
120
+ }
121
+ return !sv.(*ir.ConvExpr).X.Type().IsInterface()
122
+ }
123
+
124
+ func isSameFuncName(v1, v2 *ir.Name) bool {
125
+ // NB: there are a few corner cases where pointer equality
126
+ // doesn't work here, but this should be good enough for
127
+ // our purposes here.
128
+ return v1 == v2
129
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/parampropbits_string.go ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ // Code generated by "stringer -bitset -type ParamPropBits"; DO NOT EDIT.
6
+
7
+ package inlheur
8
+
9
+ import (
10
+ "bytes"
11
+ "strconv"
12
+ )
13
+
14
+ func _() {
15
+ // An "invalid array index" compiler error signifies that the constant values have changed.
16
+ // Re-run the stringer command to generate them again.
17
+ var x [1]struct{}
18
+ _ = x[ParamNoInfo-0]
19
+ _ = x[ParamFeedsInterfaceMethodCall-2]
20
+ _ = x[ParamMayFeedInterfaceMethodCall-4]
21
+ _ = x[ParamFeedsIndirectCall-8]
22
+ _ = x[ParamMayFeedIndirectCall-16]
23
+ _ = x[ParamFeedsIfOrSwitch-32]
24
+ _ = x[ParamMayFeedIfOrSwitch-64]
25
+ }
26
+
27
+ var _ParamPropBits_value = [...]uint64{
28
+ 0x0, /* ParamNoInfo */
29
+ 0x2, /* ParamFeedsInterfaceMethodCall */
30
+ 0x4, /* ParamMayFeedInterfaceMethodCall */
31
+ 0x8, /* ParamFeedsIndirectCall */
32
+ 0x10, /* ParamMayFeedIndirectCall */
33
+ 0x20, /* ParamFeedsIfOrSwitch */
34
+ 0x40, /* ParamMayFeedIfOrSwitch */
35
+ }
36
+
37
+ const _ParamPropBits_name = "ParamNoInfoParamFeedsInterfaceMethodCallParamMayFeedInterfaceMethodCallParamFeedsIndirectCallParamMayFeedIndirectCallParamFeedsIfOrSwitchParamMayFeedIfOrSwitch"
38
+
39
+ var _ParamPropBits_index = [...]uint8{0, 11, 40, 71, 93, 117, 137, 159}
40
+
41
+ func (i ParamPropBits) String() string {
42
+ var b bytes.Buffer
43
+
44
+ remain := uint64(i)
45
+ seen := false
46
+
47
+ for k, v := range _ParamPropBits_value {
48
+ x := _ParamPropBits_name[_ParamPropBits_index[k]:_ParamPropBits_index[k+1]]
49
+ if v == 0 {
50
+ if i == 0 {
51
+ b.WriteString(x)
52
+ return b.String()
53
+ }
54
+ continue
55
+ }
56
+ if (v & remain) == v {
57
+ remain &^= v
58
+ x := _ParamPropBits_name[_ParamPropBits_index[k]:_ParamPropBits_index[k+1]]
59
+ if seen {
60
+ b.WriteString("|")
61
+ }
62
+ seen = true
63
+ b.WriteString(x)
64
+ }
65
+ }
66
+ if remain == 0 {
67
+ return b.String()
68
+ }
69
+ return "ParamPropBits(0x" + strconv.FormatInt(int64(i), 16) + ")"
70
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/pstate_string.go ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ // Code generated by "stringer -type pstate"; DO NOT EDIT.
6
+
7
+ package inlheur
8
+
9
+ import "strconv"
10
+
11
+ func _() {
12
+ // An "invalid array index" compiler error signifies that the constant values have changed.
13
+ // Re-run the stringer command to generate them again.
14
+ var x [1]struct{}
15
+ _ = x[psNoInfo-0]
16
+ _ = x[psCallsPanic-1]
17
+ _ = x[psMayReturn-2]
18
+ _ = x[psTop-3]
19
+ }
20
+
21
+ const _pstate_name = "psNoInfopsCallsPanicpsMayReturnpsTop"
22
+
23
+ var _pstate_index = [...]uint8{0, 8, 20, 31, 36}
24
+
25
+ func (i pstate) String() string {
26
+ if i < 0 || i >= pstate(len(_pstate_index)-1) {
27
+ return "pstate(" + strconv.FormatInt(int64(i), 10) + ")"
28
+ }
29
+ return _pstate_name[_pstate_index[i]:_pstate_index[i+1]]
30
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/resultpropbits_string.go ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ // Code generated by "stringer -bitset -type ResultPropBits"; DO NOT EDIT.
6
+
7
+ package inlheur
8
+
9
+ import (
10
+ "bytes"
11
+ "strconv"
12
+ )
13
+
14
+ func _() {
15
+ // An "invalid array index" compiler error signifies that the constant values have changed.
16
+ // Re-run the stringer command to generate them again.
17
+ var x [1]struct{}
18
+ _ = x[ResultNoInfo-0]
19
+ _ = x[ResultIsAllocatedMem-2]
20
+ _ = x[ResultIsConcreteTypeConvertedToInterface-4]
21
+ _ = x[ResultAlwaysSameConstant-8]
22
+ _ = x[ResultAlwaysSameFunc-16]
23
+ _ = x[ResultAlwaysSameInlinableFunc-32]
24
+ }
25
+
26
+ var _ResultPropBits_value = [...]uint64{
27
+ 0x0, /* ResultNoInfo */
28
+ 0x2, /* ResultIsAllocatedMem */
29
+ 0x4, /* ResultIsConcreteTypeConvertedToInterface */
30
+ 0x8, /* ResultAlwaysSameConstant */
31
+ 0x10, /* ResultAlwaysSameFunc */
32
+ 0x20, /* ResultAlwaysSameInlinableFunc */
33
+ }
34
+
35
+ const _ResultPropBits_name = "ResultNoInfoResultIsAllocatedMemResultIsConcreteTypeConvertedToInterfaceResultAlwaysSameConstantResultAlwaysSameFuncResultAlwaysSameInlinableFunc"
36
+
37
+ var _ResultPropBits_index = [...]uint8{0, 12, 32, 72, 96, 116, 145}
38
+
39
+ func (i ResultPropBits) String() string {
40
+ var b bytes.Buffer
41
+
42
+ remain := uint64(i)
43
+ seen := false
44
+
45
+ for k, v := range _ResultPropBits_value {
46
+ x := _ResultPropBits_name[_ResultPropBits_index[k]:_ResultPropBits_index[k+1]]
47
+ if v == 0 {
48
+ if i == 0 {
49
+ b.WriteString(x)
50
+ return b.String()
51
+ }
52
+ continue
53
+ }
54
+ if (v & remain) == v {
55
+ remain &^= v
56
+ x := _ResultPropBits_name[_ResultPropBits_index[k]:_ResultPropBits_index[k+1]]
57
+ if seen {
58
+ b.WriteString("|")
59
+ }
60
+ seen = true
61
+ b.WriteString(x)
62
+ }
63
+ }
64
+ if remain == 0 {
65
+ return b.String()
66
+ }
67
+ return "ResultPropBits(0x" + strconv.FormatInt(int64(i), 16) + ")"
68
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/score_callresult_uses.go ADDED
@@ -0,0 +1,413 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import (
8
+ "cmd/compile/internal/ir"
9
+ "fmt"
10
+ "os"
11
+ )
12
+
13
+ // This file contains code to re-score callsites based on how the
14
+ // results of the call were used. Example:
15
+ //
16
+ // func foo() {
17
+ // x, fptr := bar()
18
+ // switch x {
19
+ // case 10: fptr = baz()
20
+ // default: blix()
21
+ // }
22
+ // fptr(100)
23
+ // }
24
+ //
25
+ // The initial scoring pass will assign a score to "bar()" based on
26
+ // various criteria, however once the first pass of scoring is done,
27
+ // we look at the flags on the result from bar, and check to see
28
+ // how those results are used. If bar() always returns the same constant
29
+ // for its first result, and if the variable receiving that result
30
+ // isn't redefined, and if that variable feeds into an if/switch
31
+ // condition, then we will try to adjust the score for "bar" (on the
32
+ // theory that if we inlined, we can constant fold / deadcode).
33
+
34
+ type resultPropAndCS struct {
35
+ defcs *CallSite
36
+ props ResultPropBits
37
+ }
38
+
39
+ type resultUseAnalyzer struct {
40
+ resultNameTab map[*ir.Name]resultPropAndCS
41
+ fn *ir.Func
42
+ cstab CallSiteTab
43
+ *condLevelTracker
44
+ }
45
+
46
+ // rescoreBasedOnCallResultUses examines how call results are used,
47
+ // and tries to update the scores of calls based on how their results
48
+ // are used in the function.
49
+ func (csa *callSiteAnalyzer) rescoreBasedOnCallResultUses(fn *ir.Func, resultNameTab map[*ir.Name]resultPropAndCS, cstab CallSiteTab) {
50
+ enableDebugTraceIfEnv()
51
+ rua := &resultUseAnalyzer{
52
+ resultNameTab: resultNameTab,
53
+ fn: fn,
54
+ cstab: cstab,
55
+ condLevelTracker: new(condLevelTracker),
56
+ }
57
+ var doNode func(ir.Node) bool
58
+ doNode = func(n ir.Node) bool {
59
+ rua.nodeVisitPre(n)
60
+ ir.DoChildren(n, doNode)
61
+ rua.nodeVisitPost(n)
62
+ return false
63
+ }
64
+ doNode(fn)
65
+ disableDebugTrace()
66
+ }
67
+
68
+ func (csa *callSiteAnalyzer) examineCallResults(cs *CallSite, resultNameTab map[*ir.Name]resultPropAndCS) map[*ir.Name]resultPropAndCS {
69
+ if debugTrace&debugTraceScoring != 0 {
70
+ fmt.Fprintf(os.Stderr, "=-= examining call results for %q\n",
71
+ EncodeCallSiteKey(cs))
72
+ }
73
+
74
+ // Invoke a helper to pick out the specific ir.Name's the results
75
+ // from this call are assigned into, e.g. "x, y := fooBar()". If
76
+ // the call is not part of an assignment statement, or if the
77
+ // variables in question are not newly defined, then we'll receive
78
+ // an empty list here.
79
+ //
80
+ names, autoTemps, props := namesDefined(cs)
81
+ if len(names) == 0 {
82
+ return resultNameTab
83
+ }
84
+
85
+ if debugTrace&debugTraceScoring != 0 {
86
+ fmt.Fprintf(os.Stderr, "=-= %d names defined\n", len(names))
87
+ }
88
+
89
+ // For each returned value, if the value has interesting
90
+ // properties (ex: always returns the same constant), and the name
91
+ // in question is never redefined, then make an entry in the
92
+ // result table for it.
93
+ const interesting = (ResultIsConcreteTypeConvertedToInterface |
94
+ ResultAlwaysSameConstant | ResultAlwaysSameInlinableFunc | ResultAlwaysSameFunc)
95
+ for idx, n := range names {
96
+ rprop := props.ResultFlags[idx]
97
+
98
+ if debugTrace&debugTraceScoring != 0 {
99
+ fmt.Fprintf(os.Stderr, "=-= props for ret %d %q: %s\n",
100
+ idx, n.Sym().Name, rprop.String())
101
+ }
102
+
103
+ if rprop&interesting == 0 {
104
+ continue
105
+ }
106
+ if csa.nameFinder.reassigned(n) {
107
+ continue
108
+ }
109
+ if resultNameTab == nil {
110
+ resultNameTab = make(map[*ir.Name]resultPropAndCS)
111
+ } else if _, ok := resultNameTab[n]; ok {
112
+ panic("should never happen")
113
+ }
114
+ entry := resultPropAndCS{
115
+ defcs: cs,
116
+ props: rprop,
117
+ }
118
+ resultNameTab[n] = entry
119
+ if autoTemps[idx] != nil {
120
+ resultNameTab[autoTemps[idx]] = entry
121
+ }
122
+ if debugTrace&debugTraceScoring != 0 {
123
+ fmt.Fprintf(os.Stderr, "=-= add resultNameTab table entry n=%v autotemp=%v props=%s\n", n, autoTemps[idx], rprop.String())
124
+ }
125
+ }
126
+ return resultNameTab
127
+ }
128
+
129
+ // namesDefined returns a list of ir.Name's corresponding to locals
130
+ // that receive the results from the call at site 'cs', plus the
131
+ // properties object for the called function. If a given result
132
+ // isn't cleanly assigned to a newly defined local, the
133
+ // slot for that result in the returned list will be nil. Example:
134
+ //
135
+ // call returned name list
136
+ //
137
+ // x := foo() [ x ]
138
+ // z, y := bar() [ nil, nil ]
139
+ // _, q := baz() [ nil, q ]
140
+ //
141
+ // In the case of a multi-return call, such as "x, y := foo()",
142
+ // the pattern we see from the front end will be a call op
143
+ // assigning to auto-temps, and then an assignment of the auto-temps
144
+ // to the user-level variables. In such cases we return
145
+ // first the user-level variable (in the first func result)
146
+ // and then the auto-temp name in the second result.
147
+ func namesDefined(cs *CallSite) ([]*ir.Name, []*ir.Name, *FuncProps) {
148
+ // If this call doesn't feed into an assignment (and of course not
149
+ // all calls do), then we don't have anything to work with here.
150
+ if cs.Assign == nil {
151
+ return nil, nil, nil
152
+ }
153
+ funcInlHeur, ok := fpmap[cs.Callee]
154
+ if !ok {
155
+ // TODO: add an assert/panic here.
156
+ return nil, nil, nil
157
+ }
158
+ if len(funcInlHeur.props.ResultFlags) == 0 {
159
+ return nil, nil, nil
160
+ }
161
+
162
+ // Single return case.
163
+ if len(funcInlHeur.props.ResultFlags) == 1 {
164
+ asgn, ok := cs.Assign.(*ir.AssignStmt)
165
+ if !ok {
166
+ return nil, nil, nil
167
+ }
168
+ // locate name being assigned
169
+ aname, ok := asgn.X.(*ir.Name)
170
+ if !ok {
171
+ return nil, nil, nil
172
+ }
173
+ return []*ir.Name{aname}, []*ir.Name{nil}, funcInlHeur.props
174
+ }
175
+
176
+ // Multi-return case
177
+ asgn, ok := cs.Assign.(*ir.AssignListStmt)
178
+ if !ok || !asgn.Def {
179
+ return nil, nil, nil
180
+ }
181
+ userVars := make([]*ir.Name, len(funcInlHeur.props.ResultFlags))
182
+ autoTemps := make([]*ir.Name, len(funcInlHeur.props.ResultFlags))
183
+ for idx, x := range asgn.Lhs {
184
+ if n, ok := x.(*ir.Name); ok {
185
+ userVars[idx] = n
186
+ r := asgn.Rhs[idx]
187
+ if r.Op() == ir.OCONVNOP {
188
+ r = r.(*ir.ConvExpr).X
189
+ }
190
+ if ir.IsAutoTmp(r) {
191
+ autoTemps[idx] = r.(*ir.Name)
192
+ }
193
+ if debugTrace&debugTraceScoring != 0 {
194
+ fmt.Fprintf(os.Stderr, "=-= multi-ret namedef uv=%v at=%v\n",
195
+ x, autoTemps[idx])
196
+ }
197
+ } else {
198
+ return nil, nil, nil
199
+ }
200
+ }
201
+ return userVars, autoTemps, funcInlHeur.props
202
+ }
203
+
204
+ func (rua *resultUseAnalyzer) nodeVisitPost(n ir.Node) {
205
+ rua.condLevelTracker.post(n)
206
+ }
207
+
208
+ func (rua *resultUseAnalyzer) nodeVisitPre(n ir.Node) {
209
+ rua.condLevelTracker.pre(n)
210
+ switch n.Op() {
211
+ case ir.OCALLINTER:
212
+ if debugTrace&debugTraceScoring != 0 {
213
+ fmt.Fprintf(os.Stderr, "=-= rescore examine iface call %v:\n", n)
214
+ }
215
+ rua.callTargetCheckResults(n)
216
+ case ir.OCALLFUNC:
217
+ if debugTrace&debugTraceScoring != 0 {
218
+ fmt.Fprintf(os.Stderr, "=-= rescore examine call %v:\n", n)
219
+ }
220
+ rua.callTargetCheckResults(n)
221
+ case ir.OIF:
222
+ ifst := n.(*ir.IfStmt)
223
+ rua.foldCheckResults(ifst.Cond)
224
+ case ir.OSWITCH:
225
+ swst := n.(*ir.SwitchStmt)
226
+ if swst.Tag != nil {
227
+ rua.foldCheckResults(swst.Tag)
228
+ }
229
+
230
+ }
231
+ }
232
+
233
+ // callTargetCheckResults examines a given call to see whether the
234
+ // callee expression is potentially an inlinable function returned
235
+ // from a potentially inlinable call. Examples:
236
+ //
237
+ // Scenario 1: named intermediate
238
+ //
239
+ // fn1 := foo() conc := bar()
240
+ // fn1("blah") conc.MyMethod()
241
+ //
242
+ // Scenario 2: returned func or concrete object feeds directly to call
243
+ //
244
+ // foo()("blah") bar().MyMethod()
245
+ //
246
+ // In the second case although at the source level the result of the
247
+ // direct call feeds right into the method call or indirect call,
248
+ // we're relying on the front end having inserted an auto-temp to
249
+ // capture the value.
250
+ func (rua *resultUseAnalyzer) callTargetCheckResults(call ir.Node) {
251
+ ce := call.(*ir.CallExpr)
252
+ rname := rua.getCallResultName(ce)
253
+ if rname == nil {
254
+ return
255
+ }
256
+ if debugTrace&debugTraceScoring != 0 {
257
+ fmt.Fprintf(os.Stderr, "=-= staticvalue returns %v:\n",
258
+ rname)
259
+ }
260
+ if rname.Class != ir.PAUTO {
261
+ return
262
+ }
263
+ switch call.Op() {
264
+ case ir.OCALLINTER:
265
+ if debugTrace&debugTraceScoring != 0 {
266
+ fmt.Fprintf(os.Stderr, "=-= in %s checking %v for cci prop:\n",
267
+ rua.fn.Sym().Name, rname)
268
+ }
269
+ if cs := rua.returnHasProp(rname, ResultIsConcreteTypeConvertedToInterface); cs != nil {
270
+
271
+ adj := returnFeedsConcreteToInterfaceCallAdj
272
+ cs.Score, cs.ScoreMask = adjustScore(adj, cs.Score, cs.ScoreMask)
273
+ }
274
+ case ir.OCALLFUNC:
275
+ if debugTrace&debugTraceScoring != 0 {
276
+ fmt.Fprintf(os.Stderr, "=-= in %s checking %v for samefunc props:\n",
277
+ rua.fn.Sym().Name, rname)
278
+ v, ok := rua.resultNameTab[rname]
279
+ if !ok {
280
+ fmt.Fprintf(os.Stderr, "=-= no entry for %v in rt\n", rname)
281
+ } else {
282
+ fmt.Fprintf(os.Stderr, "=-= props for %v: %q\n", rname, v.props.String())
283
+ }
284
+ }
285
+ if cs := rua.returnHasProp(rname, ResultAlwaysSameInlinableFunc); cs != nil {
286
+ adj := returnFeedsInlinableFuncToIndCallAdj
287
+ cs.Score, cs.ScoreMask = adjustScore(adj, cs.Score, cs.ScoreMask)
288
+ } else if cs := rua.returnHasProp(rname, ResultAlwaysSameFunc); cs != nil {
289
+ adj := returnFeedsFuncToIndCallAdj
290
+ cs.Score, cs.ScoreMask = adjustScore(adj, cs.Score, cs.ScoreMask)
291
+
292
+ }
293
+ }
294
+ }
295
+
296
+ // foldCheckResults examines the specified if/switch condition 'cond'
297
+ // to see if it refers to locals defined by a (potentially inlinable)
298
+ // function call at call site C, and if so, whether 'cond' contains
299
+ // only combinations of simple references to all of the names in
300
+ // 'names' with selected constants + operators. If these criteria are
301
+ // met, then we adjust the score for call site C to reflect the
302
+ // fact that inlining will enable deadcode and/or constant propagation.
303
+ // Note: for this heuristic to kick in, the names in question have to
304
+ // be all from the same callsite. Examples:
305
+ //
306
+ // q, r := baz() x, y := foo()
307
+ // switch q+r { a, b, c := bar()
308
+ // ... if x && y && a && b && c {
309
+ // } ...
310
+ // }
311
+ //
312
+ // For the call to "baz" above we apply a score adjustment, but not
313
+ // for the calls to "foo" or "bar".
314
+ func (rua *resultUseAnalyzer) foldCheckResults(cond ir.Node) {
315
+ namesUsed := collectNamesUsed(cond)
316
+ if len(namesUsed) == 0 {
317
+ return
318
+ }
319
+ var cs *CallSite
320
+ for _, n := range namesUsed {
321
+ rpcs, found := rua.resultNameTab[n]
322
+ if !found {
323
+ return
324
+ }
325
+ if cs != nil && rpcs.defcs != cs {
326
+ return
327
+ }
328
+ cs = rpcs.defcs
329
+ if rpcs.props&ResultAlwaysSameConstant == 0 {
330
+ return
331
+ }
332
+ }
333
+ if debugTrace&debugTraceScoring != 0 {
334
+ nls := func(nl []*ir.Name) string {
335
+ r := ""
336
+ for _, n := range nl {
337
+ r += " " + n.Sym().Name
338
+ }
339
+ return r
340
+ }
341
+ fmt.Fprintf(os.Stderr, "=-= calling ShouldFoldIfNameConstant on names={%s} cond=%v\n", nls(namesUsed), cond)
342
+ }
343
+
344
+ if !ShouldFoldIfNameConstant(cond, namesUsed) {
345
+ return
346
+ }
347
+ adj := returnFeedsConstToIfAdj
348
+ cs.Score, cs.ScoreMask = adjustScore(adj, cs.Score, cs.ScoreMask)
349
+ }
350
+
351
+ func collectNamesUsed(expr ir.Node) []*ir.Name {
352
+ res := []*ir.Name{}
353
+ ir.Visit(expr, func(n ir.Node) {
354
+ if n.Op() != ir.ONAME {
355
+ return
356
+ }
357
+ nn := n.(*ir.Name)
358
+ if nn.Class != ir.PAUTO {
359
+ return
360
+ }
361
+ res = append(res, nn)
362
+ })
363
+ return res
364
+ }
365
+
366
+ func (rua *resultUseAnalyzer) returnHasProp(name *ir.Name, prop ResultPropBits) *CallSite {
367
+ v, ok := rua.resultNameTab[name]
368
+ if !ok {
369
+ return nil
370
+ }
371
+ if v.props&prop == 0 {
372
+ return nil
373
+ }
374
+ return v.defcs
375
+ }
376
+
377
+ func (rua *resultUseAnalyzer) getCallResultName(ce *ir.CallExpr) *ir.Name {
378
+ var callTarg ir.Node
379
+ if sel, ok := ce.Fun.(*ir.SelectorExpr); ok {
380
+ // method call
381
+ callTarg = sel.X
382
+ } else if ctarg, ok := ce.Fun.(*ir.Name); ok {
383
+ // regular call
384
+ callTarg = ctarg
385
+ } else {
386
+ return nil
387
+ }
388
+ r := ir.StaticValue(callTarg)
389
+ if debugTrace&debugTraceScoring != 0 {
390
+ fmt.Fprintf(os.Stderr, "=-= staticname on %v returns %v:\n",
391
+ callTarg, r)
392
+ }
393
+ if r.Op() == ir.OCALLFUNC {
394
+ // This corresponds to the "x := foo()" case; here
395
+ // ir.StaticValue has brought us all the way back to
396
+ // the call expression itself. We need to back off to
397
+ // the name defined by the call; do this by looking up
398
+ // the callsite.
399
+ ce := r.(*ir.CallExpr)
400
+ cs, ok := rua.cstab[ce]
401
+ if !ok {
402
+ return nil
403
+ }
404
+ names, _, _ := namesDefined(cs)
405
+ if len(names) == 0 {
406
+ return nil
407
+ }
408
+ return names[0]
409
+ } else if r.Op() == ir.ONAME {
410
+ return r.(*ir.Name)
411
+ }
412
+ return nil
413
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/scoreadjusttyp_string.go ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Code generated by "stringer -bitset -type scoreAdjustTyp"; DO NOT EDIT.
2
+
3
+ package inlheur
4
+
5
+ import "strconv"
6
+ import "bytes"
7
+
8
+ func _() {
9
+ // An "invalid array index" compiler error signifies that the constant values have changed.
10
+ // Re-run the stringer command to generate them again.
11
+ var x [1]struct{}
12
+ _ = x[panicPathAdj-1]
13
+ _ = x[initFuncAdj-2]
14
+ _ = x[inLoopAdj-4]
15
+ _ = x[passConstToIfAdj-8]
16
+ _ = x[passConstToNestedIfAdj-16]
17
+ _ = x[passConcreteToItfCallAdj-32]
18
+ _ = x[passConcreteToNestedItfCallAdj-64]
19
+ _ = x[passFuncToIndCallAdj-128]
20
+ _ = x[passFuncToNestedIndCallAdj-256]
21
+ _ = x[passInlinableFuncToIndCallAdj-512]
22
+ _ = x[passInlinableFuncToNestedIndCallAdj-1024]
23
+ _ = x[returnFeedsConstToIfAdj-2048]
24
+ _ = x[returnFeedsFuncToIndCallAdj-4096]
25
+ _ = x[returnFeedsInlinableFuncToIndCallAdj-8192]
26
+ _ = x[returnFeedsConcreteToInterfaceCallAdj-16384]
27
+ }
28
+
29
+ var _scoreAdjustTyp_value = [...]uint64{
30
+ 0x1, /* panicPathAdj */
31
+ 0x2, /* initFuncAdj */
32
+ 0x4, /* inLoopAdj */
33
+ 0x8, /* passConstToIfAdj */
34
+ 0x10, /* passConstToNestedIfAdj */
35
+ 0x20, /* passConcreteToItfCallAdj */
36
+ 0x40, /* passConcreteToNestedItfCallAdj */
37
+ 0x80, /* passFuncToIndCallAdj */
38
+ 0x100, /* passFuncToNestedIndCallAdj */
39
+ 0x200, /* passInlinableFuncToIndCallAdj */
40
+ 0x400, /* passInlinableFuncToNestedIndCallAdj */
41
+ 0x800, /* returnFeedsConstToIfAdj */
42
+ 0x1000, /* returnFeedsFuncToIndCallAdj */
43
+ 0x2000, /* returnFeedsInlinableFuncToIndCallAdj */
44
+ 0x4000, /* returnFeedsConcreteToInterfaceCallAdj */
45
+ }
46
+
47
+ const _scoreAdjustTyp_name = "panicPathAdjinitFuncAdjinLoopAdjpassConstToIfAdjpassConstToNestedIfAdjpassConcreteToItfCallAdjpassConcreteToNestedItfCallAdjpassFuncToIndCallAdjpassFuncToNestedIndCallAdjpassInlinableFuncToIndCallAdjpassInlinableFuncToNestedIndCallAdjreturnFeedsConstToIfAdjreturnFeedsFuncToIndCallAdjreturnFeedsInlinableFuncToIndCallAdjreturnFeedsConcreteToInterfaceCallAdj"
48
+
49
+ var _scoreAdjustTyp_index = [...]uint16{0, 12, 23, 32, 48, 70, 94, 124, 144, 170, 199, 234, 257, 284, 320, 357}
50
+
51
+ func (i scoreAdjustTyp) String() string {
52
+ var b bytes.Buffer
53
+
54
+ remain := uint64(i)
55
+ seen := false
56
+
57
+ for k, v := range _scoreAdjustTyp_value {
58
+ x := _scoreAdjustTyp_name[_scoreAdjustTyp_index[k]:_scoreAdjustTyp_index[k+1]]
59
+ if v == 0 {
60
+ if i == 0 {
61
+ b.WriteString(x)
62
+ return b.String()
63
+ }
64
+ continue
65
+ }
66
+ if (v & remain) == v {
67
+ remain &^= v
68
+ x := _scoreAdjustTyp_name[_scoreAdjustTyp_index[k]:_scoreAdjustTyp_index[k+1]]
69
+ if seen {
70
+ b.WriteString("|")
71
+ }
72
+ seen = true
73
+ b.WriteString(x)
74
+ }
75
+ }
76
+ if remain == 0 {
77
+ return b.String()
78
+ }
79
+ return "scoreAdjustTyp(0x" + strconv.FormatInt(int64(i), 16) + ")"
80
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/scoring.go ADDED
@@ -0,0 +1,751 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import (
8
+ "cmd/compile/internal/base"
9
+ "cmd/compile/internal/ir"
10
+ "cmd/compile/internal/pgo"
11
+ "cmd/compile/internal/types"
12
+ "fmt"
13
+ "os"
14
+ "sort"
15
+ "strconv"
16
+ "strings"
17
+ )
18
+
19
+ // These constants enumerate the set of possible ways/scenarios
20
+ // in which we'll adjust the score of a given callsite.
21
+ type scoreAdjustTyp uint
22
+
23
+ // These constants capture the various ways in which the inliner's
24
+ // scoring phase can adjust a callsite score based on heuristics. They
25
+ // fall broadly into three categories:
26
+ //
27
+ // 1) adjustments based solely on the callsite context (ex: call
28
+ // appears on panic path)
29
+ //
30
+ // 2) adjustments that take into account specific interesting values
31
+ // passed at a call site (ex: passing a constant that could result in
32
+ // cprop/deadcode in the caller)
33
+ //
34
+ // 3) adjustments that take into account values returned from the call
35
+ // at a callsite (ex: call always returns the same inlinable function,
36
+ // and return value flows unmodified into an indirect call)
37
+ //
38
+ // For categories 2 and 3 above, each adjustment can have either a
39
+ // "must" version and a "may" version (but not both). Here the idea is
40
+ // that in the "must" version the value flow is unconditional: if the
41
+ // callsite executes, then the condition we're interested in (ex:
42
+ // param feeding call) is guaranteed to happen. For the "may" version,
43
+ // there may be control flow that could cause the benefit to be
44
+ // bypassed.
45
+ const (
46
+ // Category 1 adjustments (see above)
47
+ panicPathAdj scoreAdjustTyp = (1 << iota)
48
+ initFuncAdj
49
+ inLoopAdj
50
+
51
+ // Category 2 adjustments (see above).
52
+ passConstToIfAdj
53
+ passConstToNestedIfAdj
54
+ passConcreteToItfCallAdj
55
+ passConcreteToNestedItfCallAdj
56
+ passFuncToIndCallAdj
57
+ passFuncToNestedIndCallAdj
58
+ passInlinableFuncToIndCallAdj
59
+ passInlinableFuncToNestedIndCallAdj
60
+
61
+ // Category 3 adjustments.
62
+ returnFeedsConstToIfAdj
63
+ returnFeedsFuncToIndCallAdj
64
+ returnFeedsInlinableFuncToIndCallAdj
65
+ returnFeedsConcreteToInterfaceCallAdj
66
+
67
+ sentinelScoreAdj // sentinel; not a real adjustment
68
+ )
69
+
70
+ // This table records the specific values we use to adjust call
71
+ // site scores in a given scenario.
72
+ // NOTE: these numbers are chosen very arbitrarily; ideally
73
+ // we will go through some sort of turning process to decide
74
+ // what value for each one produces the best performance.
75
+
76
+ var adjValues = map[scoreAdjustTyp]int{
77
+ panicPathAdj: 40,
78
+ initFuncAdj: 20,
79
+ inLoopAdj: -5,
80
+ passConstToIfAdj: -20,
81
+ passConstToNestedIfAdj: -15,
82
+ passConcreteToItfCallAdj: -30,
83
+ passConcreteToNestedItfCallAdj: -25,
84
+ passFuncToIndCallAdj: -25,
85
+ passFuncToNestedIndCallAdj: -20,
86
+ passInlinableFuncToIndCallAdj: -45,
87
+ passInlinableFuncToNestedIndCallAdj: -40,
88
+ returnFeedsConstToIfAdj: -15,
89
+ returnFeedsFuncToIndCallAdj: -25,
90
+ returnFeedsInlinableFuncToIndCallAdj: -40,
91
+ returnFeedsConcreteToInterfaceCallAdj: -25,
92
+ }
93
+
94
+ // SetupScoreAdjustments interprets the value of the -d=inlscoreadj
95
+ // debugging option, if set. The value of this flag is expected to be
96
+ // a series of "/"-separated clauses of the form adj1:value1. Example:
97
+ // -d=inlscoreadj=inLoopAdj=0/passConstToIfAdj=-99
98
+ func SetupScoreAdjustments() {
99
+ if base.Debug.InlScoreAdj == "" {
100
+ return
101
+ }
102
+ if err := parseScoreAdj(base.Debug.InlScoreAdj); err != nil {
103
+ base.Fatalf("malformed -d=inlscoreadj argument %q: %v",
104
+ base.Debug.InlScoreAdj, err)
105
+ }
106
+ }
107
+
108
+ func adjStringToVal(s string) (scoreAdjustTyp, bool) {
109
+ for adj := scoreAdjustTyp(1); adj < sentinelScoreAdj; adj <<= 1 {
110
+ if adj.String() == s {
111
+ return adj, true
112
+ }
113
+ }
114
+ return 0, false
115
+ }
116
+
117
+ func parseScoreAdj(val string) error {
118
+ clauses := strings.Split(val, "/")
119
+ if len(clauses) == 0 {
120
+ return fmt.Errorf("no clauses")
121
+ }
122
+ for _, clause := range clauses {
123
+ elems := strings.Split(clause, ":")
124
+ if len(elems) < 2 {
125
+ return fmt.Errorf("clause %q: expected colon", clause)
126
+ }
127
+ if len(elems) != 2 {
128
+ return fmt.Errorf("clause %q has %d elements, wanted 2", clause,
129
+ len(elems))
130
+ }
131
+ adj, ok := adjStringToVal(elems[0])
132
+ if !ok {
133
+ return fmt.Errorf("clause %q: unknown adjustment", clause)
134
+ }
135
+ val, err := strconv.Atoi(elems[1])
136
+ if err != nil {
137
+ return fmt.Errorf("clause %q: malformed value: %v", clause, err)
138
+ }
139
+ adjValues[adj] = val
140
+ }
141
+ return nil
142
+ }
143
+
144
+ func adjValue(x scoreAdjustTyp) int {
145
+ if val, ok := adjValues[x]; ok {
146
+ return val
147
+ } else {
148
+ panic("internal error unregistered adjustment type")
149
+ }
150
+ }
151
+
152
+ var mayMustAdj = [...]struct{ may, must scoreAdjustTyp }{
153
+ {may: passConstToNestedIfAdj, must: passConstToIfAdj},
154
+ {may: passConcreteToNestedItfCallAdj, must: passConcreteToItfCallAdj},
155
+ {may: passFuncToNestedIndCallAdj, must: passFuncToNestedIndCallAdj},
156
+ {may: passInlinableFuncToNestedIndCallAdj, must: passInlinableFuncToIndCallAdj},
157
+ }
158
+
159
+ func isMay(x scoreAdjustTyp) bool {
160
+ return mayToMust(x) != 0
161
+ }
162
+
163
+ func isMust(x scoreAdjustTyp) bool {
164
+ return mustToMay(x) != 0
165
+ }
166
+
167
+ func mayToMust(x scoreAdjustTyp) scoreAdjustTyp {
168
+ for _, v := range mayMustAdj {
169
+ if x == v.may {
170
+ return v.must
171
+ }
172
+ }
173
+ return 0
174
+ }
175
+
176
+ func mustToMay(x scoreAdjustTyp) scoreAdjustTyp {
177
+ for _, v := range mayMustAdj {
178
+ if x == v.must {
179
+ return v.may
180
+ }
181
+ }
182
+ return 0
183
+ }
184
+
185
+ // computeCallSiteScore takes a given call site whose ir node is
186
+ // 'call' and callee function is 'callee' and with previously computed
187
+ // call site properties 'csflags', then computes a score for the
188
+ // callsite that combines the size cost of the callee with heuristics
189
+ // based on previously computed argument and function properties,
190
+ // then stores the score and the adjustment mask in the appropriate
191
+ // fields in 'cs'
192
+ func (cs *CallSite) computeCallSiteScore(csa *callSiteAnalyzer, calleeProps *FuncProps) {
193
+ callee := cs.Callee
194
+ csflags := cs.Flags
195
+ call := cs.Call
196
+
197
+ // Start with the size-based score for the callee.
198
+ score := int(callee.Inl.Cost)
199
+ var tmask scoreAdjustTyp
200
+
201
+ if debugTrace&debugTraceScoring != 0 {
202
+ fmt.Fprintf(os.Stderr, "=-= scoring call to %s at %s , initial=%d\n",
203
+ callee.Sym().Name, fmtFullPos(call.Pos()), score)
204
+ }
205
+
206
+ // First some score adjustments to discourage inlining in selected cases.
207
+ if csflags&CallSiteOnPanicPath != 0 {
208
+ score, tmask = adjustScore(panicPathAdj, score, tmask)
209
+ }
210
+ if csflags&CallSiteInInitFunc != 0 {
211
+ score, tmask = adjustScore(initFuncAdj, score, tmask)
212
+ }
213
+
214
+ // Then adjustments to encourage inlining in selected cases.
215
+ if csflags&CallSiteInLoop != 0 {
216
+ score, tmask = adjustScore(inLoopAdj, score, tmask)
217
+ }
218
+
219
+ // Stop here if no callee props.
220
+ if calleeProps == nil {
221
+ cs.Score, cs.ScoreMask = score, tmask
222
+ return
223
+ }
224
+
225
+ // Walk through the actual expressions being passed at the call.
226
+ calleeRecvrParms := callee.Type().RecvParams()
227
+ for idx := range call.Args {
228
+ // ignore blanks
229
+ if calleeRecvrParms[idx].Sym == nil ||
230
+ calleeRecvrParms[idx].Sym.IsBlank() {
231
+ continue
232
+ }
233
+ arg := call.Args[idx]
234
+ pflag := calleeProps.ParamFlags[idx]
235
+ if debugTrace&debugTraceScoring != 0 {
236
+ fmt.Fprintf(os.Stderr, "=-= arg %d of %d: val %v flags=%s\n",
237
+ idx, len(call.Args), arg, pflag.String())
238
+ }
239
+
240
+ if len(cs.ArgProps) == 0 {
241
+ continue
242
+ }
243
+ argProps := cs.ArgProps[idx]
244
+
245
+ if debugTrace&debugTraceScoring != 0 {
246
+ fmt.Fprintf(os.Stderr, "=-= arg %d props %s value %v\n",
247
+ idx, argProps.String(), arg)
248
+ }
249
+
250
+ if argProps&ActualExprConstant != 0 {
251
+ if pflag&ParamMayFeedIfOrSwitch != 0 {
252
+ score, tmask = adjustScore(passConstToNestedIfAdj, score, tmask)
253
+ }
254
+ if pflag&ParamFeedsIfOrSwitch != 0 {
255
+ score, tmask = adjustScore(passConstToIfAdj, score, tmask)
256
+ }
257
+ }
258
+
259
+ if argProps&ActualExprIsConcreteConvIface != 0 {
260
+ // FIXME: ideally here it would be nice to make a
261
+ // distinction between the inlinable case and the
262
+ // non-inlinable case, but this is hard to do. Example:
263
+ //
264
+ // type I interface { Tiny() int; Giant() }
265
+ // type Conc struct { x int }
266
+ // func (c *Conc) Tiny() int { return 42 }
267
+ // func (c *Conc) Giant() { <huge amounts of code> }
268
+ //
269
+ // func passConcToItf(c *Conc) {
270
+ // makesItfMethodCall(c)
271
+ // }
272
+ //
273
+ // In the code above, function properties will only tell
274
+ // us that 'makesItfMethodCall' invokes a method on its
275
+ // interface parameter, but we don't know whether it calls
276
+ // "Tiny" or "Giant". If we knew if called "Tiny", then in
277
+ // theory in addition to converting the interface call to
278
+ // a direct call, we could also inline (in which case
279
+ // we'd want to decrease the score even more).
280
+ //
281
+ // One thing we could do (not yet implemented) is iterate
282
+ // through all of the methods of "*Conc" that allow it to
283
+ // satisfy I, and if all are inlinable, then exploit that.
284
+ if pflag&ParamMayFeedInterfaceMethodCall != 0 {
285
+ score, tmask = adjustScore(passConcreteToNestedItfCallAdj, score, tmask)
286
+ }
287
+ if pflag&ParamFeedsInterfaceMethodCall != 0 {
288
+ score, tmask = adjustScore(passConcreteToItfCallAdj, score, tmask)
289
+ }
290
+ }
291
+
292
+ if argProps&(ActualExprIsFunc|ActualExprIsInlinableFunc) != 0 {
293
+ mayadj := passFuncToNestedIndCallAdj
294
+ mustadj := passFuncToIndCallAdj
295
+ if argProps&ActualExprIsInlinableFunc != 0 {
296
+ mayadj = passInlinableFuncToNestedIndCallAdj
297
+ mustadj = passInlinableFuncToIndCallAdj
298
+ }
299
+ if pflag&ParamMayFeedIndirectCall != 0 {
300
+ score, tmask = adjustScore(mayadj, score, tmask)
301
+ }
302
+ if pflag&ParamFeedsIndirectCall != 0 {
303
+ score, tmask = adjustScore(mustadj, score, tmask)
304
+ }
305
+ }
306
+ }
307
+
308
+ cs.Score, cs.ScoreMask = score, tmask
309
+ }
310
+
311
+ func adjustScore(typ scoreAdjustTyp, score int, mask scoreAdjustTyp) (int, scoreAdjustTyp) {
312
+
313
+ if isMust(typ) {
314
+ if mask&typ != 0 {
315
+ return score, mask
316
+ }
317
+ may := mustToMay(typ)
318
+ if mask&may != 0 {
319
+ // promote may to must, so undo may
320
+ score -= adjValue(may)
321
+ mask &^= may
322
+ }
323
+ } else if isMay(typ) {
324
+ must := mayToMust(typ)
325
+ if mask&(must|typ) != 0 {
326
+ return score, mask
327
+ }
328
+ }
329
+ if mask&typ == 0 {
330
+ if debugTrace&debugTraceScoring != 0 {
331
+ fmt.Fprintf(os.Stderr, "=-= applying adj %d for %s\n",
332
+ adjValue(typ), typ.String())
333
+ }
334
+ score += adjValue(typ)
335
+ mask |= typ
336
+ }
337
+ return score, mask
338
+ }
339
+
340
+ var resultFlagToPositiveAdj map[ResultPropBits]scoreAdjustTyp
341
+ var paramFlagToPositiveAdj map[ParamPropBits]scoreAdjustTyp
342
+
343
+ func setupFlagToAdjMaps() {
344
+ resultFlagToPositiveAdj = map[ResultPropBits]scoreAdjustTyp{
345
+ ResultIsAllocatedMem: returnFeedsConcreteToInterfaceCallAdj,
346
+ ResultAlwaysSameFunc: returnFeedsFuncToIndCallAdj,
347
+ ResultAlwaysSameConstant: returnFeedsConstToIfAdj,
348
+ }
349
+ paramFlagToPositiveAdj = map[ParamPropBits]scoreAdjustTyp{
350
+ ParamMayFeedInterfaceMethodCall: passConcreteToNestedItfCallAdj,
351
+ ParamFeedsInterfaceMethodCall: passConcreteToItfCallAdj,
352
+ ParamMayFeedIndirectCall: passInlinableFuncToNestedIndCallAdj,
353
+ ParamFeedsIndirectCall: passInlinableFuncToIndCallAdj,
354
+ }
355
+ }
356
+
357
+ // LargestNegativeScoreAdjustment tries to estimate the largest possible
358
+ // negative score adjustment that could be applied to a call of the
359
+ // function with the specified props. Example:
360
+ //
361
+ // func foo() { func bar(x int, p *int) int {
362
+ // ... if x < 0 { *p = x }
363
+ // } return 99
364
+ // }
365
+ //
366
+ // Function 'foo' above on the left has no interesting properties,
367
+ // thus as a result the most we'll adjust any call to is the value for
368
+ // "call in loop". If the calculated cost of the function is 150, and
369
+ // the in-loop adjustment is 5 (for example), then there is not much
370
+ // point treating it as inlinable. On the other hand "bar" has a param
371
+ // property (parameter "x" feeds unmodified to an "if" statement") and
372
+ // a return property (always returns same constant) meaning that a
373
+ // given call _could_ be rescored down as much as -35 points-- thus if
374
+ // the size of "bar" is 100 (for example) then there is at least a
375
+ // chance that scoring will enable inlining.
376
+ func LargestNegativeScoreAdjustment(fn *ir.Func, props *FuncProps) int {
377
+ if resultFlagToPositiveAdj == nil {
378
+ setupFlagToAdjMaps()
379
+ }
380
+ var tmask scoreAdjustTyp
381
+ score := adjValues[inLoopAdj] // any call can be in a loop
382
+ for _, pf := range props.ParamFlags {
383
+ if adj, ok := paramFlagToPositiveAdj[pf]; ok {
384
+ score, tmask = adjustScore(adj, score, tmask)
385
+ }
386
+ }
387
+ for _, rf := range props.ResultFlags {
388
+ if adj, ok := resultFlagToPositiveAdj[rf]; ok {
389
+ score, tmask = adjustScore(adj, score, tmask)
390
+ }
391
+ }
392
+
393
+ if debugTrace&debugTraceScoring != 0 {
394
+ fmt.Fprintf(os.Stderr, "=-= largestScore(%v) is %d\n",
395
+ fn, score)
396
+ }
397
+
398
+ return score
399
+ }
400
+
401
+ // LargestPositiveScoreAdjustment tries to estimate the largest possible
402
+ // positive score adjustment that could be applied to a given callsite.
403
+ // At the moment we don't have very many positive score adjustments, so
404
+ // this is just hard-coded, not table-driven.
405
+ func LargestPositiveScoreAdjustment(fn *ir.Func) int {
406
+ return adjValues[panicPathAdj] + adjValues[initFuncAdj]
407
+ }
408
+
409
+ // callSiteTab contains entries for each call in the function
410
+ // currently being processed by InlineCalls; this variable will either
411
+ // be set to 'cstabCache' below (for non-inlinable routines) or to the
412
+ // local 'cstab' entry in the fnInlHeur object for inlinable routines.
413
+ //
414
+ // NOTE: this assumes that inlining operations are happening in a serial,
415
+ // single-threaded fashion,f which is true today but probably won't hold
416
+ // in the future (for example, we might want to score the callsites
417
+ // in multiple functions in parallel); if the inliner evolves in this
418
+ // direction we'll need to come up with a different approach here.
419
+ var callSiteTab CallSiteTab
420
+
421
+ // scoreCallsCache caches a call site table and call site list between
422
+ // invocations of ScoreCalls so that we can reuse previously allocated
423
+ // storage.
424
+ var scoreCallsCache scoreCallsCacheType
425
+
426
+ type scoreCallsCacheType struct {
427
+ tab CallSiteTab
428
+ csl []*CallSite
429
+ }
430
+
431
+ // ScoreCalls assigns numeric scores to each of the callsites in
432
+ // function 'fn'; the lower the score, the more helpful we think it
433
+ // will be to inline.
434
+ //
435
+ // Unlike a lot of the other inline heuristics machinery, callsite
436
+ // scoring can't be done as part of the CanInline call for a function,
437
+ // due to fact that we may be working on a non-trivial SCC. So for
438
+ // example with this SCC:
439
+ //
440
+ // func foo(x int) { func bar(x int, f func()) {
441
+ // if x != 0 { f()
442
+ // bar(x, func(){}) foo(x-1)
443
+ // } }
444
+ // }
445
+ //
446
+ // We don't want to perform scoring for the 'foo' call in "bar" until
447
+ // after foo has been analyzed, but it's conceivable that CanInline
448
+ // might visit bar before foo for this SCC.
449
+ func ScoreCalls(fn *ir.Func) {
450
+ if len(fn.Body) == 0 {
451
+ return
452
+ }
453
+ enableDebugTraceIfEnv()
454
+
455
+ nameFinder := newNameFinder(fn)
456
+
457
+ if debugTrace&debugTraceScoring != 0 {
458
+ fmt.Fprintf(os.Stderr, "=-= ScoreCalls(%v)\n", ir.FuncName(fn))
459
+ }
460
+
461
+ // If this is an inlinable function, use the precomputed
462
+ // call site table for it. If the function wasn't an inline
463
+ // candidate, collect a callsite table for it now.
464
+ var cstab CallSiteTab
465
+ if funcInlHeur, ok := fpmap[fn]; ok {
466
+ cstab = funcInlHeur.cstab
467
+ } else {
468
+ if len(scoreCallsCache.tab) != 0 {
469
+ panic("missing call to ScoreCallsCleanup")
470
+ }
471
+ if scoreCallsCache.tab == nil {
472
+ scoreCallsCache.tab = make(CallSiteTab)
473
+ }
474
+ if debugTrace&debugTraceScoring != 0 {
475
+ fmt.Fprintf(os.Stderr, "=-= building cstab for non-inl func %s\n",
476
+ ir.FuncName(fn))
477
+ }
478
+ cstab = computeCallSiteTable(fn, fn.Body, scoreCallsCache.tab, nil, 0,
479
+ nameFinder)
480
+ }
481
+
482
+ csa := makeCallSiteAnalyzer(fn)
483
+ const doCallResults = true
484
+ csa.scoreCallsRegion(fn, fn.Body, cstab, doCallResults, nil)
485
+
486
+ disableDebugTrace()
487
+ }
488
+
489
+ // scoreCallsRegion assigns numeric scores to each of the callsites in
490
+ // region 'region' within function 'fn'. This can be called on
491
+ // an entire function, or with 'region' set to a chunk of
492
+ // code corresponding to an inlined call.
493
+ func (csa *callSiteAnalyzer) scoreCallsRegion(fn *ir.Func, region ir.Nodes, cstab CallSiteTab, doCallResults bool, ic *ir.InlinedCallExpr) {
494
+ if debugTrace&debugTraceScoring != 0 {
495
+ fmt.Fprintf(os.Stderr, "=-= scoreCallsRegion(%v, %s) len(cstab)=%d\n",
496
+ ir.FuncName(fn), region[0].Op().String(), len(cstab))
497
+ }
498
+
499
+ // Sort callsites to avoid any surprises with non deterministic
500
+ // map iteration order (this is probably not needed, but here just
501
+ // in case).
502
+ csl := scoreCallsCache.csl[:0]
503
+ for _, cs := range cstab {
504
+ csl = append(csl, cs)
505
+ }
506
+ scoreCallsCache.csl = csl[:0]
507
+ sort.Slice(csl, func(i, j int) bool {
508
+ return csl[i].ID < csl[j].ID
509
+ })
510
+
511
+ // Score each call site.
512
+ var resultNameTab map[*ir.Name]resultPropAndCS
513
+ for _, cs := range csl {
514
+ var cprops *FuncProps
515
+ fihcprops := false
516
+ desercprops := false
517
+ if funcInlHeur, ok := fpmap[cs.Callee]; ok {
518
+ cprops = funcInlHeur.props
519
+ fihcprops = true
520
+ } else if cs.Callee.Inl != nil {
521
+ cprops = DeserializeFromString(cs.Callee.Inl.Properties)
522
+ desercprops = true
523
+ } else {
524
+ if base.Debug.DumpInlFuncProps != "" {
525
+ fmt.Fprintf(os.Stderr, "=-= *** unable to score call to %s from %s\n", cs.Callee.Sym().Name, fmtFullPos(cs.Call.Pos()))
526
+ panic("should never happen")
527
+ } else {
528
+ continue
529
+ }
530
+ }
531
+ cs.computeCallSiteScore(csa, cprops)
532
+
533
+ if doCallResults {
534
+ if debugTrace&debugTraceScoring != 0 {
535
+ fmt.Fprintf(os.Stderr, "=-= examineCallResults at %s: flags=%d score=%d funcInlHeur=%v deser=%v\n", fmtFullPos(cs.Call.Pos()), cs.Flags, cs.Score, fihcprops, desercprops)
536
+ }
537
+ resultNameTab = csa.examineCallResults(cs, resultNameTab)
538
+ }
539
+
540
+ if debugTrace&debugTraceScoring != 0 {
541
+ fmt.Fprintf(os.Stderr, "=-= scoring call at %s: flags=%d score=%d funcInlHeur=%v deser=%v\n", fmtFullPos(cs.Call.Pos()), cs.Flags, cs.Score, fihcprops, desercprops)
542
+ }
543
+ }
544
+
545
+ if resultNameTab != nil {
546
+ csa.rescoreBasedOnCallResultUses(fn, resultNameTab, cstab)
547
+ }
548
+
549
+ disableDebugTrace()
550
+
551
+ if ic != nil && callSiteTab != nil {
552
+ // Integrate the calls from this cstab into the table for the caller.
553
+ if err := callSiteTab.merge(cstab); err != nil {
554
+ base.FatalfAt(ic.Pos(), "%v", err)
555
+ }
556
+ } else {
557
+ callSiteTab = cstab
558
+ }
559
+ }
560
+
561
+ // ScoreCallsCleanup resets the state of the callsite cache
562
+ // once ScoreCalls is done with a function.
563
+ func ScoreCallsCleanup() {
564
+ if base.Debug.DumpInlCallSiteScores != 0 {
565
+ if allCallSites == nil {
566
+ allCallSites = make(CallSiteTab)
567
+ }
568
+ for call, cs := range callSiteTab {
569
+ allCallSites[call] = cs
570
+ }
571
+ }
572
+ for k := range scoreCallsCache.tab {
573
+ delete(scoreCallsCache.tab, k)
574
+ }
575
+ }
576
+
577
+ // GetCallSiteScore returns the previously calculated score for call
578
+ // within fn.
579
+ func GetCallSiteScore(fn *ir.Func, call *ir.CallExpr) (int, bool) {
580
+ if funcInlHeur, ok := fpmap[fn]; ok {
581
+ if cs, ok := funcInlHeur.cstab[call]; ok {
582
+ return cs.Score, true
583
+ }
584
+ }
585
+ if cs, ok := callSiteTab[call]; ok {
586
+ return cs.Score, true
587
+ }
588
+ return 0, false
589
+ }
590
+
591
+ // BudgetExpansion returns the amount to relax/expand the base
592
+ // inlining budget when the new inliner is turned on; the inliner
593
+ // will add the returned value to the hairyness budget.
594
+ //
595
+ // Background: with the new inliner, the score for a given callsite
596
+ // can be adjusted down by some amount due to heuristics, however we
597
+ // won't know whether this is going to happen until much later after
598
+ // the CanInline call. This function returns the amount to relax the
599
+ // budget initially (to allow for a large score adjustment); later on
600
+ // in RevisitInlinability we'll look at each individual function to
601
+ // demote it if needed.
602
+ func BudgetExpansion(maxBudget int32) int32 {
603
+ if base.Debug.InlBudgetSlack != 0 {
604
+ return int32(base.Debug.InlBudgetSlack)
605
+ }
606
+ // In the default case, return maxBudget, which will effectively
607
+ // double the budget from 80 to 160; this should be good enough
608
+ // for most cases.
609
+ return maxBudget
610
+ }
611
+
612
+ var allCallSites CallSiteTab
613
+
614
+ // DumpInlCallSiteScores is invoked by the inliner if the debug flag
615
+ // "-d=dumpinlcallsitescores" is set; it dumps out a human-readable
616
+ // summary of all (potentially) inlinable callsites in the package,
617
+ // along with info on call site scoring and the adjustments made to a
618
+ // given score. Here profile is the PGO profile in use (may be
619
+ // nil), budgetCallback is a callback that can be invoked to find out
620
+ // the original pre-adjustment hairyness limit for the function, and
621
+ // inlineHotMaxBudget is the constant of the same name used in the
622
+ // inliner. Sample output lines:
623
+ //
624
+ // Score Adjustment Status Callee CallerPos ScoreFlags
625
+ // 115 40 DEMOTED cmd/compile/internal/abi.(*ABIParamAssignment).Offset expand_calls.go:1679:14|6 panicPathAdj
626
+ // 76 -5n PROMOTED runtime.persistentalloc mcheckmark.go:48:45|3 inLoopAdj
627
+ // 201 0 --- PGO unicode.DecodeRuneInString utf8.go:312:30|1
628
+ // 7 -5 --- PGO internal/abi.Name.DataChecked type.go:625:22|0 inLoopAdj
629
+ //
630
+ // In the dump above, "Score" is the final score calculated for the
631
+ // callsite, "Adjustment" is the amount added to or subtracted from
632
+ // the original hairyness estimate to form the score. "Status" shows
633
+ // whether anything changed with the site -- did the adjustment bump
634
+ // it down just below the threshold ("PROMOTED") or instead bump it
635
+ // above the threshold ("DEMOTED"); this will be blank ("---") if no
636
+ // threshold was crossed as a result of the heuristics. Note that
637
+ // "Status" also shows whether PGO was involved. "Callee" is the name
638
+ // of the function called, "CallerPos" is the position of the
639
+ // callsite, and "ScoreFlags" is a digest of the specific properties
640
+ // we used to make adjustments to callsite score via heuristics.
641
+ func DumpInlCallSiteScores(profile *pgo.Profile, budgetCallback func(fn *ir.Func, profile *pgo.Profile) (int32, bool)) {
642
+
643
+ var indirectlyDueToPromotion func(cs *CallSite) bool
644
+ indirectlyDueToPromotion = func(cs *CallSite) bool {
645
+ bud, _ := budgetCallback(cs.Callee, profile)
646
+ hairyval := cs.Callee.Inl.Cost
647
+ score := int32(cs.Score)
648
+ if hairyval > bud && score <= bud {
649
+ return true
650
+ }
651
+ if cs.parent != nil {
652
+ return indirectlyDueToPromotion(cs.parent)
653
+ }
654
+ return false
655
+ }
656
+
657
+ genstatus := func(cs *CallSite) string {
658
+ hairyval := cs.Callee.Inl.Cost
659
+ bud, isPGO := budgetCallback(cs.Callee, profile)
660
+ score := int32(cs.Score)
661
+ st := "---"
662
+ expinl := false
663
+ switch {
664
+ case hairyval <= bud && score <= bud:
665
+ // "Normal" inlined case: hairy val sufficiently low that
666
+ // it would have been inlined anyway without heuristics.
667
+ expinl = true
668
+ case hairyval > bud && score > bud:
669
+ // "Normal" not inlined case: hairy val sufficiently high
670
+ // and scoring didn't lower it.
671
+ case hairyval > bud && score <= bud:
672
+ // Promoted: we would not have inlined it before, but
673
+ // after score adjustment we decided to inline.
674
+ st = "PROMOTED"
675
+ expinl = true
676
+ case hairyval <= bud && score > bud:
677
+ // Demoted: we would have inlined it before, but after
678
+ // score adjustment we decided not to inline.
679
+ st = "DEMOTED"
680
+ }
681
+ inlined := cs.aux&csAuxInlined != 0
682
+ indprom := false
683
+ if cs.parent != nil {
684
+ indprom = indirectlyDueToPromotion(cs.parent)
685
+ }
686
+ if inlined && indprom {
687
+ st += "|INDPROM"
688
+ }
689
+ if inlined && !expinl {
690
+ st += "|[NI?]"
691
+ } else if !inlined && expinl {
692
+ st += "|[IN?]"
693
+ }
694
+ if isPGO {
695
+ st += "|PGO"
696
+ }
697
+ return st
698
+ }
699
+
700
+ if base.Debug.DumpInlCallSiteScores != 0 {
701
+ var sl []*CallSite
702
+ for _, cs := range allCallSites {
703
+ sl = append(sl, cs)
704
+ }
705
+ sort.Slice(sl, func(i, j int) bool {
706
+ if sl[i].Score != sl[j].Score {
707
+ return sl[i].Score < sl[j].Score
708
+ }
709
+ fni := ir.PkgFuncName(sl[i].Callee)
710
+ fnj := ir.PkgFuncName(sl[j].Callee)
711
+ if fni != fnj {
712
+ return fni < fnj
713
+ }
714
+ ecsi := EncodeCallSiteKey(sl[i])
715
+ ecsj := EncodeCallSiteKey(sl[j])
716
+ return ecsi < ecsj
717
+ })
718
+
719
+ mkname := func(fn *ir.Func) string {
720
+ var n string
721
+ if fn == nil || fn.Nname == nil {
722
+ return "<nil>"
723
+ }
724
+ if fn.Sym().Pkg == types.LocalPkg {
725
+ n = "·" + fn.Sym().Name
726
+ } else {
727
+ n = ir.PkgFuncName(fn)
728
+ }
729
+ // don't try to print super-long names
730
+ if len(n) <= 64 {
731
+ return n
732
+ }
733
+ return n[:32] + "..." + n[len(n)-32:len(n)]
734
+ }
735
+
736
+ if len(sl) != 0 {
737
+ fmt.Fprintf(os.Stdout, "# scores for package %s\n", types.LocalPkg.Path)
738
+ fmt.Fprintf(os.Stdout, "# Score Adjustment Status Callee CallerPos Flags ScoreFlags\n")
739
+ }
740
+ for _, cs := range sl {
741
+ hairyval := cs.Callee.Inl.Cost
742
+ adj := int32(cs.Score) - hairyval
743
+ nm := mkname(cs.Callee)
744
+ ecc := EncodeCallSiteKey(cs)
745
+ fmt.Fprintf(os.Stdout, "%d %d\t%s\t%s\t%s\t%s\n",
746
+ cs.Score, adj, genstatus(cs),
747
+ nm, ecc,
748
+ cs.ScoreMask.String())
749
+ }
750
+ }
751
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/serialize.go ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import "strings"
8
+
9
+ func (funcProps *FuncProps) SerializeToString() string {
10
+ if funcProps == nil {
11
+ return ""
12
+ }
13
+ var sb strings.Builder
14
+ writeUleb128(&sb, uint64(funcProps.Flags))
15
+ writeUleb128(&sb, uint64(len(funcProps.ParamFlags)))
16
+ for _, pf := range funcProps.ParamFlags {
17
+ writeUleb128(&sb, uint64(pf))
18
+ }
19
+ writeUleb128(&sb, uint64(len(funcProps.ResultFlags)))
20
+ for _, rf := range funcProps.ResultFlags {
21
+ writeUleb128(&sb, uint64(rf))
22
+ }
23
+ return sb.String()
24
+ }
25
+
26
+ func DeserializeFromString(s string) *FuncProps {
27
+ if len(s) == 0 {
28
+ return nil
29
+ }
30
+ var funcProps FuncProps
31
+ var v uint64
32
+ sl := []byte(s)
33
+ v, sl = readULEB128(sl)
34
+ funcProps.Flags = FuncPropBits(v)
35
+ v, sl = readULEB128(sl)
36
+ funcProps.ParamFlags = make([]ParamPropBits, v)
37
+ for i := range funcProps.ParamFlags {
38
+ v, sl = readULEB128(sl)
39
+ funcProps.ParamFlags[i] = ParamPropBits(v)
40
+ }
41
+ v, sl = readULEB128(sl)
42
+ funcProps.ResultFlags = make([]ResultPropBits, v)
43
+ for i := range funcProps.ResultFlags {
44
+ v, sl = readULEB128(sl)
45
+ funcProps.ResultFlags[i] = ResultPropBits(v)
46
+ }
47
+ return &funcProps
48
+ }
49
+
50
+ func readULEB128(sl []byte) (value uint64, rsl []byte) {
51
+ var shift uint
52
+
53
+ for {
54
+ b := sl[0]
55
+ sl = sl[1:]
56
+ value |= (uint64(b&0x7F) << shift)
57
+ if b&0x80 == 0 {
58
+ break
59
+ }
60
+ shift += 7
61
+ }
62
+ return value, sl
63
+ }
64
+
65
+ func writeUleb128(sb *strings.Builder, v uint64) {
66
+ if v < 128 {
67
+ sb.WriteByte(uint8(v))
68
+ return
69
+ }
70
+ more := true
71
+ for more {
72
+ c := uint8(v & 0x7f)
73
+ v >>= 7
74
+ more = v != 0
75
+ if more {
76
+ c |= 0x80
77
+ }
78
+ sb.WriteByte(c)
79
+ }
80
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/dumpscores.go ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package dumpscores
6
+
7
+ var G int
8
+
9
+ func inlinable(x int, f func(int) int) int {
10
+ if x != 0 {
11
+ return 1
12
+ }
13
+ G += noninl(x)
14
+ return f(x)
15
+ }
16
+
17
+ func inlinable2(x int) int {
18
+ return noninl(-x)
19
+ }
20
+
21
+ //go:noinline
22
+ func noninl(x int) int {
23
+ return x + 1
24
+ }
25
+
26
+ func tooLargeToInline(x int) int {
27
+ if x > 101 {
28
+ // Drive up the cost of inlining this func over the
29
+ // regular threshold.
30
+ return big(big(big(big(big(G + x)))))
31
+ }
32
+ if x < 100 {
33
+ // make sure this callsite is scored properly
34
+ G += inlinable(101, inlinable2)
35
+ if G == 101 {
36
+ return 0
37
+ }
38
+ panic(inlinable2(3))
39
+ }
40
+ return G
41
+ }
42
+
43
+ func big(q int) int {
44
+ return noninl(q) + noninl(-q)
45
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/props/README.txt ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ Notes on the format of the testcase files in
6
+ cmd/compile/internal/inline/inlheur/testdata/props:
7
+
8
+ - each (compilable) file contains input Go code and expected results
9
+ in the form of column-0 comments.
10
+
11
+ - functions or methods that begin with "T_" are targeted for testing,
12
+ as well as "init" functions; all other functions are ignored.
13
+
14
+ - function header comments begin with a line containing
15
+ the file name, function name, definition line, then index
16
+ and a count of the number of funcs that share that same
17
+ definition line (needed to support generics). Example:
18
+
19
+ // foo.go T_mumble 35 1 4
20
+
21
+ Here "T_mumble" is defined at line 35, and it is func 0
22
+ out of the 4 funcs that share that same line.
23
+
24
+ - function property expected results appear as comments in immediately
25
+ prior to the function. For example, here we have first the function
26
+ name ("T_feeds_if_simple"), then human-readable dump of the function
27
+ properties, as well as the JSON for the properties object, each
28
+ section separated by a "<>" delimiter.
29
+
30
+ // params.go T_feeds_if_simple 35 0 1
31
+ // RecvrParamFlags:
32
+ // 0: ParamFeedsIfOrSwitch
33
+ // <endpropsdump>
34
+ // {"Flags":0,"RecvrParamFlags":[8],"ReturnFlags":[]}
35
+ // callsite: params.go:34:10|0 "CallSiteOnPanicPath" 2
36
+ // <endcallsites>
37
+ // <endfuncpreamble>
38
+ func T_feeds_if_simple(x int) {
39
+ if x < 100 {
40
+ os.Exit(1)
41
+ }
42
+ println(x)
43
+ }
44
+
45
+ - when the test runs, it will compile the Go source file with an
46
+ option to dump out function properties, then compare the new dump
47
+ for each function with the JSON appearing in the header comment for
48
+ the function (in the example above, the JSON appears between
49
+ "<endpropsdump>" and "<endfuncpreamble>". The material prior to the
50
+ dump is simply there for human consumption, so that a developer can
51
+ easily see that "RecvrParamFlags":[8] means that the first parameter
52
+ has flag ParamFeedsIfOrSwitch.
53
+
54
+ - when making changes to the compiler (which can alter the expected
55
+ results) or edits/additions to the go code in the testcase files,
56
+ you can remaster the results by running
57
+
58
+ go test -v -count=1 .
59
+
60
+ In the trace output of this run, you'll see messages of the form
61
+
62
+ === RUN TestFuncProperties
63
+ funcprops_test.go:NNN: update-expected: emitted updated file
64
+ testdata/props/XYZ.go.new
65
+ funcprops_test.go:MMM: please compare the two files, then overwrite
66
+ testdata/props/XYZ.go with testdata/props/XYZ.go.new
67
+
68
+ at which point you can compare the old and new files by hand, then
69
+ overwrite the *.go file with the *.go.new file if you are happy with
70
+ the diffs.
71
+
72
+ - note that the remastering process will strip out any existing
73
+ column-0 (unindented) comments; if you write comments that you
74
+ want to see preserved, use "/* */" or indent them.
75
+
76
+
77
+
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/props/acrosscall.go ADDED
@@ -0,0 +1,214 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ // DO NOT EDIT (use 'go test -v -update-expected' instead.)
6
+ // See cmd/compile/internal/inline/inlheur/testdata/props/README.txt
7
+ // for more information on the format of this file.
8
+ // <endfilepreamble>
9
+ package params
10
+
11
+ // acrosscall.go T_feeds_indirect_call_via_call_toplevel 19 0 1
12
+ // ParamFlags
13
+ // 0 ParamFeedsIndirectCall
14
+ // <endpropsdump>
15
+ // {"Flags":0,"ParamFlags":[8],"ResultFlags":null}
16
+ // callsite: acrosscall.go:20:12|0 flagstr "" flagval 0 score 60 mask 0 maskstr ""
17
+ // <endcallsites>
18
+ // <endfuncpreamble>
19
+ func T_feeds_indirect_call_via_call_toplevel(f func(int)) {
20
+ callsparam(f)
21
+ }
22
+
23
+ // acrosscall.go T_feeds_indirect_call_via_call_conditional 31 0 1
24
+ // ParamFlags
25
+ // 0 ParamMayFeedIndirectCall
26
+ // <endpropsdump>
27
+ // {"Flags":0,"ParamFlags":[16],"ResultFlags":null}
28
+ // callsite: acrosscall.go:33:13|0 flagstr "" flagval 0 score 60 mask 0 maskstr ""
29
+ // <endcallsites>
30
+ // <endfuncpreamble>
31
+ func T_feeds_indirect_call_via_call_conditional(f func(int)) {
32
+ if G != 101 {
33
+ callsparam(f)
34
+ }
35
+ }
36
+
37
+ // acrosscall.go T_feeds_conditional_indirect_call_via_call_toplevel 45 0 1
38
+ // ParamFlags
39
+ // 0 ParamMayFeedIndirectCall
40
+ // <endpropsdump>
41
+ // {"Flags":0,"ParamFlags":[16],"ResultFlags":null}
42
+ // callsite: acrosscall.go:46:23|0 flagstr "" flagval 0 score 64 mask 0 maskstr ""
43
+ // <endcallsites>
44
+ // <endfuncpreamble>
45
+ func T_feeds_conditional_indirect_call_via_call_toplevel(f func(int)) {
46
+ callsparamconditional(f)
47
+ }
48
+
49
+ // acrosscall.go T_feeds_if_via_call 57 0 1
50
+ // ParamFlags
51
+ // 0 ParamFeedsIfOrSwitch
52
+ // <endpropsdump>
53
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":null}
54
+ // callsite: acrosscall.go:58:9|0 flagstr "" flagval 0 score 8 mask 0 maskstr ""
55
+ // <endcallsites>
56
+ // <endfuncpreamble>
57
+ func T_feeds_if_via_call(x int) {
58
+ feedsif(x)
59
+ }
60
+
61
+ // acrosscall.go T_feeds_if_via_call_conditional 69 0 1
62
+ // ParamFlags
63
+ // 0 ParamMayFeedIfOrSwitch
64
+ // <endpropsdump>
65
+ // {"Flags":0,"ParamFlags":[64],"ResultFlags":null}
66
+ // callsite: acrosscall.go:71:10|0 flagstr "" flagval 0 score 8 mask 0 maskstr ""
67
+ // <endcallsites>
68
+ // <endfuncpreamble>
69
+ func T_feeds_if_via_call_conditional(x int) {
70
+ if G != 101 {
71
+ feedsif(x)
72
+ }
73
+ }
74
+
75
+ // acrosscall.go T_feeds_conditional_if_via_call 83 0 1
76
+ // ParamFlags
77
+ // 0 ParamMayFeedIfOrSwitch
78
+ // <endpropsdump>
79
+ // {"Flags":0,"ParamFlags":[64],"ResultFlags":null}
80
+ // callsite: acrosscall.go:84:20|0 flagstr "" flagval 0 score 12 mask 0 maskstr ""
81
+ // <endcallsites>
82
+ // <endfuncpreamble>
83
+ func T_feeds_conditional_if_via_call(x int) {
84
+ feedsifconditional(x)
85
+ }
86
+
87
+ // acrosscall.go T_multifeeds1 97 0 1
88
+ // ParamFlags
89
+ // 0 ParamFeedsIndirectCall|ParamMayFeedIndirectCall
90
+ // 1 ParamNoInfo
91
+ // <endpropsdump>
92
+ // {"Flags":0,"ParamFlags":[24,0],"ResultFlags":null}
93
+ // callsite: acrosscall.go:98:12|0 flagstr "" flagval 0 score 60 mask 0 maskstr ""
94
+ // callsite: acrosscall.go:99:23|1 flagstr "" flagval 0 score 64 mask 0 maskstr ""
95
+ // <endcallsites>
96
+ // <endfuncpreamble>
97
+ func T_multifeeds1(f1, f2 func(int)) {
98
+ callsparam(f1)
99
+ callsparamconditional(f1)
100
+ }
101
+
102
+ // acrosscall.go T_acrosscall_returnsconstant 110 0 1
103
+ // ResultFlags
104
+ // 0 ResultAlwaysSameConstant
105
+ // <endpropsdump>
106
+ // {"Flags":0,"ParamFlags":null,"ResultFlags":[8]}
107
+ // callsite: acrosscall.go:111:24|0 flagstr "" flagval 0 score 2 mask 0 maskstr ""
108
+ // <endcallsites>
109
+ // <endfuncpreamble>
110
+ func T_acrosscall_returnsconstant() int {
111
+ return returnsconstant()
112
+ }
113
+
114
+ // acrosscall.go T_acrosscall_returnsmem 122 0 1
115
+ // ResultFlags
116
+ // 0 ResultIsAllocatedMem
117
+ // <endpropsdump>
118
+ // {"Flags":0,"ParamFlags":null,"ResultFlags":[2]}
119
+ // callsite: acrosscall.go:123:19|0 flagstr "" flagval 0 score 2 mask 0 maskstr ""
120
+ // <endcallsites>
121
+ // <endfuncpreamble>
122
+ func T_acrosscall_returnsmem() *int {
123
+ return returnsmem()
124
+ }
125
+
126
+ // acrosscall.go T_acrosscall_returnscci 134 0 1
127
+ // ResultFlags
128
+ // 0 ResultIsConcreteTypeConvertedToInterface
129
+ // <endpropsdump>
130
+ // {"Flags":0,"ParamFlags":null,"ResultFlags":[4]}
131
+ // callsite: acrosscall.go:135:19|0 flagstr "" flagval 0 score 7 mask 0 maskstr ""
132
+ // <endcallsites>
133
+ // <endfuncpreamble>
134
+ func T_acrosscall_returnscci() I {
135
+ return returnscci()
136
+ }
137
+
138
+ // acrosscall.go T_acrosscall_multiret 144 0 1
139
+ // <endpropsdump>
140
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":[0]}
141
+ // callsite: acrosscall.go:146:25|0 flagstr "" flagval 0 score 2 mask 0 maskstr ""
142
+ // <endcallsites>
143
+ // <endfuncpreamble>
144
+ func T_acrosscall_multiret(q int) int {
145
+ if q != G {
146
+ return returnsconstant()
147
+ }
148
+ return 0
149
+ }
150
+
151
+ // acrosscall.go T_acrosscall_multiret2 158 0 1
152
+ // <endpropsdump>
153
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":[0]}
154
+ // callsite: acrosscall.go:160:25|0 flagstr "" flagval 0 score 2 mask 0 maskstr ""
155
+ // callsite: acrosscall.go:162:25|1 flagstr "" flagval 0 score 2 mask 0 maskstr ""
156
+ // <endcallsites>
157
+ // <endfuncpreamble>
158
+ func T_acrosscall_multiret2(q int) int {
159
+ if q == G {
160
+ return returnsconstant()
161
+ } else {
162
+ return returnsconstant()
163
+ }
164
+ }
165
+
166
+ func callsparam(f func(int)) {
167
+ f(2)
168
+ }
169
+
170
+ func callsparamconditional(f func(int)) {
171
+ if G != 101 {
172
+ f(2)
173
+ }
174
+ }
175
+
176
+ func feedsif(x int) int {
177
+ if x != 101 {
178
+ return 42
179
+ }
180
+ return 43
181
+ }
182
+
183
+ func feedsifconditional(x int) int {
184
+ if G != 101 {
185
+ if x != 101 {
186
+ return 42
187
+ }
188
+ }
189
+ return 43
190
+ }
191
+
192
+ func returnsconstant() int {
193
+ return 42
194
+ }
195
+
196
+ func returnsmem() *int {
197
+ return new(int)
198
+ }
199
+
200
+ func returnscci() I {
201
+ var q Q
202
+ return q
203
+ }
204
+
205
+ type I interface {
206
+ Foo()
207
+ }
208
+
209
+ type Q int
210
+
211
+ func (q Q) Foo() {
212
+ }
213
+
214
+ var G int
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/props/calls.go ADDED
@@ -0,0 +1,240 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ // DO NOT EDIT (use 'go test -v -update-expected' instead.)
6
+ // See cmd/compile/internal/inline/inlheur/testdata/props/README.txt
7
+ // for more information on the format of this file.
8
+ // <endfilepreamble>
9
+ package calls
10
+
11
+ import "os"
12
+
13
+ // calls.go T_call_in_panic_arg 19 0 1
14
+ // <endpropsdump>
15
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
16
+ // callsite: calls.go:21:15|0 flagstr "CallSiteOnPanicPath" flagval 2 score 42 mask 1 maskstr "panicPathAdj"
17
+ // <endcallsites>
18
+ // <endfuncpreamble>
19
+ func T_call_in_panic_arg(x int) {
20
+ if x < G {
21
+ panic(callee(x))
22
+ }
23
+ }
24
+
25
+ // calls.go T_calls_in_loops 32 0 1
26
+ // <endpropsdump>
27
+ // {"Flags":0,"ParamFlags":[0,0],"ResultFlags":null}
28
+ // callsite: calls.go:34:9|0 flagstr "CallSiteInLoop" flagval 1 score -3 mask 4 maskstr "inLoopAdj"
29
+ // callsite: calls.go:37:9|1 flagstr "CallSiteInLoop" flagval 1 score -3 mask 4 maskstr "inLoopAdj"
30
+ // <endcallsites>
31
+ // <endfuncpreamble>
32
+ func T_calls_in_loops(x int, q []string) {
33
+ for i := 0; i < x; i++ {
34
+ callee(i)
35
+ }
36
+ for _, s := range q {
37
+ callee(len(s))
38
+ }
39
+ }
40
+
41
+ // calls.go T_calls_in_pseudo_loop 48 0 1
42
+ // <endpropsdump>
43
+ // {"Flags":0,"ParamFlags":[0,0],"ResultFlags":null}
44
+ // callsite: calls.go:50:9|0 flagstr "" flagval 0 score 2 mask 0 maskstr ""
45
+ // callsite: calls.go:54:9|1 flagstr "" flagval 0 score 2 mask 0 maskstr ""
46
+ // <endcallsites>
47
+ // <endfuncpreamble>
48
+ func T_calls_in_pseudo_loop(x int, q []string) {
49
+ for i := 0; i < x; i++ {
50
+ callee(i)
51
+ return
52
+ }
53
+ for _, s := range q {
54
+ callee(len(s))
55
+ break
56
+ }
57
+ }
58
+
59
+ // calls.go T_calls_on_panic_paths 67 0 1
60
+ // <endpropsdump>
61
+ // {"Flags":0,"ParamFlags":[0,0],"ResultFlags":null}
62
+ // callsite: calls.go:69:9|0 flagstr "CallSiteOnPanicPath" flagval 2 score 42 mask 1 maskstr "panicPathAdj"
63
+ // callsite: calls.go:73:9|1 flagstr "CallSiteOnPanicPath" flagval 2 score 42 mask 1 maskstr "panicPathAdj"
64
+ // callsite: calls.go:77:12|2 flagstr "CallSiteOnPanicPath" flagval 2 score 102 mask 1 maskstr "panicPathAdj"
65
+ // <endcallsites>
66
+ // <endfuncpreamble>
67
+ func T_calls_on_panic_paths(x int, q []string) {
68
+ if x+G == 101 {
69
+ callee(x)
70
+ panic("ouch")
71
+ }
72
+ if x < G-101 {
73
+ callee(x)
74
+ if len(q) == 0 {
75
+ G++
76
+ }
77
+ callsexit(x)
78
+ }
79
+ }
80
+
81
+ // calls.go T_calls_not_on_panic_paths 93 0 1
82
+ // ParamFlags
83
+ // 0 ParamFeedsIfOrSwitch|ParamMayFeedIfOrSwitch
84
+ // 1 ParamNoInfo
85
+ // <endpropsdump>
86
+ // {"Flags":0,"ParamFlags":[96,0],"ResultFlags":null}
87
+ // callsite: calls.go:103:9|0 flagstr "" flagval 0 score 2 mask 0 maskstr ""
88
+ // callsite: calls.go:112:9|1 flagstr "" flagval 0 score 2 mask 0 maskstr ""
89
+ // callsite: calls.go:115:9|2 flagstr "" flagval 0 score 2 mask 0 maskstr ""
90
+ // callsite: calls.go:119:12|3 flagstr "CallSiteOnPanicPath" flagval 2 score 102 mask 1 maskstr "panicPathAdj"
91
+ // <endcallsites>
92
+ // <endfuncpreamble>
93
+ func T_calls_not_on_panic_paths(x int, q []string) {
94
+ if x != G {
95
+ panic("ouch")
96
+ /* Notes: */
97
+ /* - we only look for post-dominating panic/exit, so */
98
+ /* this site will on fact not have a panicpath flag */
99
+ /* - vet will complain about this site as unreachable */
100
+ callee(x)
101
+ }
102
+ if x != G {
103
+ callee(x)
104
+ if x < 100 {
105
+ panic("ouch")
106
+ }
107
+ }
108
+ if x+G == 101 {
109
+ if x < 100 {
110
+ panic("ouch")
111
+ }
112
+ callee(x)
113
+ }
114
+ if x < -101 {
115
+ callee(x)
116
+ if len(q) == 0 {
117
+ return
118
+ }
119
+ callsexit(x)
120
+ }
121
+ }
122
+
123
+ // calls.go init.0 129 0 1
124
+ // <endpropsdump>
125
+ // {"Flags":0,"ParamFlags":null,"ResultFlags":null}
126
+ // callsite: calls.go:130:16|0 flagstr "CallSiteInInitFunc" flagval 4 score 22 mask 2 maskstr "initFuncAdj"
127
+ // <endcallsites>
128
+ // <endfuncpreamble>
129
+ func init() {
130
+ println(callee(5))
131
+ }
132
+
133
+ // calls.go T_pass_inlinable_func_to_param_feeding_indirect_call 140 0 1
134
+ // <endpropsdump>
135
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":[0]}
136
+ // callsite: calls.go:141:19|0 flagstr "" flagval 0 score 16 mask 512 maskstr "passInlinableFuncToIndCallAdj"
137
+ // callsite: calls.go:141:19|calls.go:232:10|0 flagstr "" flagval 0 score 2 mask 0 maskstr ""
138
+ // <endcallsites>
139
+ // <endfuncpreamble>
140
+ func T_pass_inlinable_func_to_param_feeding_indirect_call(x int) int {
141
+ return callsParam(x, callee)
142
+ }
143
+
144
+ // calls.go T_pass_noninlinable_func_to_param_feeding_indirect_call 150 0 1
145
+ // <endpropsdump>
146
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":[0]}
147
+ // callsite: calls.go:153:19|0 flagstr "" flagval 0 score 36 mask 128 maskstr "passFuncToIndCallAdj"
148
+ // <endcallsites>
149
+ // <endfuncpreamble>
150
+ func T_pass_noninlinable_func_to_param_feeding_indirect_call(x int) int {
151
+ // if we inline callsParam we can convert the indirect call
152
+ // to a direct call, but we can't inline it.
153
+ return callsParam(x, calleeNoInline)
154
+ }
155
+
156
+ // calls.go T_pass_inlinable_func_to_param_feeding_nested_indirect_call 165 0 1
157
+ // ParamFlags
158
+ // 0 ParamFeedsIfOrSwitch
159
+ // <endpropsdump>
160
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":[0]}
161
+ // callsite: calls.go:166:25|0 flagstr "" flagval 0 score 27 mask 1024 maskstr "passInlinableFuncToNestedIndCallAdj"
162
+ // callsite: calls.go:166:25|calls.go:237:11|0 flagstr "" flagval 0 score 2 mask 0 maskstr ""
163
+ // <endcallsites>
164
+ // <endfuncpreamble>
165
+ func T_pass_inlinable_func_to_param_feeding_nested_indirect_call(x int) int {
166
+ return callsParamNested(x, callee)
167
+ }
168
+
169
+ // calls.go T_pass_noninlinable_func_to_param_feeding_nested_indirect_call 177 0 1
170
+ // ParamFlags
171
+ // 0 ParamFeedsIfOrSwitch
172
+ // <endpropsdump>
173
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":[0]}
174
+ // callsite: calls.go:178:25|0 flagstr "" flagval 0 score 47 mask 256 maskstr "passFuncToNestedIndCallAdj"
175
+ // <endcallsites>
176
+ // <endfuncpreamble>
177
+ func T_pass_noninlinable_func_to_param_feeding_nested_indirect_call(x int) int {
178
+ return callsParamNested(x, calleeNoInline)
179
+ }
180
+
181
+ // calls.go T_call_scoring_in_noninlinable_func 195 0 1
182
+ // <endpropsdump>
183
+ // {"Flags":0,"ParamFlags":[0,0],"ResultFlags":[0]}
184
+ // callsite: calls.go:209:14|0 flagstr "CallSiteOnPanicPath" flagval 2 score 42 mask 1 maskstr "panicPathAdj"
185
+ // callsite: calls.go:210:15|1 flagstr "CallSiteOnPanicPath" flagval 2 score 42 mask 1 maskstr "panicPathAdj"
186
+ // callsite: calls.go:212:19|2 flagstr "" flagval 0 score 16 mask 512 maskstr "passInlinableFuncToIndCallAdj"
187
+ // callsite: calls.go:212:19|calls.go:232:10|0 flagstr "" flagval 0 score 4 mask 0 maskstr ""
188
+ // <endcallsites>
189
+ // <endfuncpreamble>
190
+ // calls.go T_call_scoring_in_noninlinable_func.func1 212 0 1
191
+ // <endpropsdump>
192
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":[0]}
193
+ // <endcallsites>
194
+ // <endfuncpreamble>
195
+ func T_call_scoring_in_noninlinable_func(x int, sl []int) int {
196
+ if x == 101 {
197
+ // Drive up the cost of inlining this funcfunc over the
198
+ // regular threshold.
199
+ for i := 0; i < 10; i++ {
200
+ for j := 0; j < i; j++ {
201
+ sl = append(sl, append(sl, append(sl, append(sl, x)...)...)...)
202
+ sl = append(sl, sl[0], sl[1], sl[2])
203
+ x += calleeNoInline(x)
204
+ }
205
+ }
206
+ }
207
+ if x < 100 {
208
+ // make sure this callsite is scored properly
209
+ G += callee(101)
210
+ panic(callee(x))
211
+ }
212
+ return callsParam(x, func(y int) int { return y + x })
213
+ }
214
+
215
+ var G int
216
+
217
+ func callee(x int) int {
218
+ return x
219
+ }
220
+
221
+ func calleeNoInline(x int) int {
222
+ defer func() { G++ }()
223
+ return x
224
+ }
225
+
226
+ func callsexit(x int) {
227
+ println(x)
228
+ os.Exit(x)
229
+ }
230
+
231
+ func callsParam(x int, f func(int) int) int {
232
+ return f(x)
233
+ }
234
+
235
+ func callsParamNested(x int, f func(int) int) int {
236
+ if x < 0 {
237
+ return f(x)
238
+ }
239
+ return 0
240
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/props/funcflags.go ADDED
@@ -0,0 +1,341 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ // DO NOT EDIT (use 'go test -v -update-expected' instead.)
6
+ // See cmd/compile/internal/inline/inlheur/testdata/props/README.txt
7
+ // for more information on the format of this file.
8
+ // <endfilepreamble>
9
+
10
+ package funcflags
11
+
12
+ import "os"
13
+
14
+ // funcflags.go T_simple 20 0 1
15
+ // Flags FuncPropNeverReturns
16
+ // <endpropsdump>
17
+ // {"Flags":1,"ParamFlags":null,"ResultFlags":null}
18
+ // <endcallsites>
19
+ // <endfuncpreamble>
20
+ func T_simple() {
21
+ panic("bad")
22
+ }
23
+
24
+ // funcflags.go T_nested 32 0 1
25
+ // Flags FuncPropNeverReturns
26
+ // ParamFlags
27
+ // 0 ParamFeedsIfOrSwitch
28
+ // <endpropsdump>
29
+ // {"Flags":1,"ParamFlags":[32],"ResultFlags":null}
30
+ // <endcallsites>
31
+ // <endfuncpreamble>
32
+ func T_nested(x int) {
33
+ if x < 10 {
34
+ panic("bad")
35
+ } else {
36
+ panic("good")
37
+ }
38
+ }
39
+
40
+ // funcflags.go T_block1 46 0 1
41
+ // Flags FuncPropNeverReturns
42
+ // <endpropsdump>
43
+ // {"Flags":1,"ParamFlags":[0],"ResultFlags":null}
44
+ // <endcallsites>
45
+ // <endfuncpreamble>
46
+ func T_block1(x int) {
47
+ panic("bad")
48
+ if x < 10 {
49
+ return
50
+ }
51
+ }
52
+
53
+ // funcflags.go T_block2 60 0 1
54
+ // ParamFlags
55
+ // 0 ParamFeedsIfOrSwitch
56
+ // <endpropsdump>
57
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":null}
58
+ // <endcallsites>
59
+ // <endfuncpreamble>
60
+ func T_block2(x int) {
61
+ if x < 10 {
62
+ return
63
+ }
64
+ panic("bad")
65
+ }
66
+
67
+ // funcflags.go T_switches1 75 0 1
68
+ // Flags FuncPropNeverReturns
69
+ // ParamFlags
70
+ // 0 ParamFeedsIfOrSwitch
71
+ // <endpropsdump>
72
+ // {"Flags":1,"ParamFlags":[32],"ResultFlags":null}
73
+ // <endcallsites>
74
+ // <endfuncpreamble>
75
+ func T_switches1(x int) {
76
+ switch x {
77
+ case 1:
78
+ panic("one")
79
+ case 2:
80
+ panic("two")
81
+ }
82
+ panic("whatev")
83
+ }
84
+
85
+ // funcflags.go T_switches1a 92 0 1
86
+ // ParamFlags
87
+ // 0 ParamFeedsIfOrSwitch
88
+ // <endpropsdump>
89
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":null}
90
+ // <endcallsites>
91
+ // <endfuncpreamble>
92
+ func T_switches1a(x int) {
93
+ switch x {
94
+ case 2:
95
+ panic("two")
96
+ }
97
+ }
98
+
99
+ // funcflags.go T_switches2 106 0 1
100
+ // ParamFlags
101
+ // 0 ParamFeedsIfOrSwitch
102
+ // <endpropsdump>
103
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":null}
104
+ // <endcallsites>
105
+ // <endfuncpreamble>
106
+ func T_switches2(x int) {
107
+ switch x {
108
+ case 1:
109
+ panic("one")
110
+ case 2:
111
+ panic("two")
112
+ default:
113
+ return
114
+ }
115
+ panic("whatev")
116
+ }
117
+
118
+ // funcflags.go T_switches3 123 0 1
119
+ // <endpropsdump>
120
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
121
+ // <endcallsites>
122
+ // <endfuncpreamble>
123
+ func T_switches3(x interface{}) {
124
+ switch x.(type) {
125
+ case bool:
126
+ panic("one")
127
+ case float32:
128
+ panic("two")
129
+ }
130
+ }
131
+
132
+ // funcflags.go T_switches4 138 0 1
133
+ // Flags FuncPropNeverReturns
134
+ // <endpropsdump>
135
+ // {"Flags":1,"ParamFlags":[0],"ResultFlags":null}
136
+ // <endcallsites>
137
+ // <endfuncpreamble>
138
+ func T_switches4(x int) {
139
+ switch x {
140
+ case 1:
141
+ x++
142
+ fallthrough
143
+ case 2:
144
+ panic("two")
145
+ fallthrough
146
+ default:
147
+ panic("bad")
148
+ }
149
+ panic("whatev")
150
+ }
151
+
152
+ // funcflags.go T_recov 157 0 1
153
+ // <endpropsdump>
154
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
155
+ // <endcallsites>
156
+ // <endfuncpreamble>
157
+ func T_recov(x int) {
158
+ if x := recover(); x != nil {
159
+ panic(x)
160
+ }
161
+ }
162
+
163
+ // funcflags.go T_forloops1 169 0 1
164
+ // Flags FuncPropNeverReturns
165
+ // <endpropsdump>
166
+ // {"Flags":1,"ParamFlags":[0],"ResultFlags":null}
167
+ // <endcallsites>
168
+ // <endfuncpreamble>
169
+ func T_forloops1(x int) {
170
+ for {
171
+ panic("wokketa")
172
+ }
173
+ }
174
+
175
+ // funcflags.go T_forloops2 180 0 1
176
+ // <endpropsdump>
177
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
178
+ // <endcallsites>
179
+ // <endfuncpreamble>
180
+ func T_forloops2(x int) {
181
+ for {
182
+ println("blah")
183
+ if true {
184
+ break
185
+ }
186
+ panic("warg")
187
+ }
188
+ }
189
+
190
+ // funcflags.go T_forloops3 195 0 1
191
+ // <endpropsdump>
192
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
193
+ // <endcallsites>
194
+ // <endfuncpreamble>
195
+ func T_forloops3(x int) {
196
+ for i := 0; i < 101; i++ {
197
+ println("blah")
198
+ if true {
199
+ continue
200
+ }
201
+ panic("plark")
202
+ }
203
+ for i := range [10]int{} {
204
+ println(i)
205
+ panic("plark")
206
+ }
207
+ panic("whatev")
208
+ }
209
+
210
+ // funcflags.go T_hasgotos 215 0 1
211
+ // <endpropsdump>
212
+ // {"Flags":0,"ParamFlags":[0,0],"ResultFlags":null}
213
+ // <endcallsites>
214
+ // <endfuncpreamble>
215
+ func T_hasgotos(x int, y int) {
216
+ {
217
+ xx := x
218
+ panic("bad")
219
+ lab1:
220
+ goto lab2
221
+ lab2:
222
+ if false {
223
+ goto lab1
224
+ } else {
225
+ goto lab4
226
+ }
227
+ lab4:
228
+ if xx < y {
229
+ lab3:
230
+ if false {
231
+ goto lab3
232
+ }
233
+ }
234
+ println(9)
235
+ }
236
+ }
237
+
238
+ // funcflags.go T_break_with_label 246 0 1
239
+ // ParamFlags
240
+ // 0 ParamMayFeedIfOrSwitch
241
+ // 1 ParamNoInfo
242
+ // <endpropsdump>
243
+ // {"Flags":0,"ParamFlags":[64,0],"ResultFlags":null}
244
+ // <endcallsites>
245
+ // <endfuncpreamble>
246
+ func T_break_with_label(x int, y int) {
247
+ // presence of break with label should pessimize this func
248
+ // (similar to goto).
249
+ panic("bad")
250
+ lab1:
251
+ for {
252
+ println("blah")
253
+ if x < 0 {
254
+ break lab1
255
+ }
256
+ panic("hubba")
257
+ }
258
+ }
259
+
260
+ // funcflags.go T_callsexit 268 0 1
261
+ // Flags FuncPropNeverReturns
262
+ // ParamFlags
263
+ // 0 ParamFeedsIfOrSwitch
264
+ // <endpropsdump>
265
+ // {"Flags":1,"ParamFlags":[32],"ResultFlags":null}
266
+ // <endcallsites>
267
+ // <endfuncpreamble>
268
+ func T_callsexit(x int) {
269
+ if x < 0 {
270
+ os.Exit(1)
271
+ }
272
+ os.Exit(2)
273
+ }
274
+
275
+ // funcflags.go T_exitinexpr 281 0 1
276
+ // <endpropsdump>
277
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
278
+ // callsite: funcflags.go:286:18|0 flagstr "CallSiteOnPanicPath" flagval 2 score 102 mask 1 maskstr "panicPathAdj"
279
+ // <endcallsites>
280
+ // <endfuncpreamble>
281
+ func T_exitinexpr(x int) {
282
+ // This function does indeed unconditionally call exit, since the
283
+ // first thing it does is invoke exprcallsexit, however from the
284
+ // perspective of this function, the call is not at the statement
285
+ // level, so we'll wind up missing it.
286
+ if exprcallsexit(x) < 0 {
287
+ println("foo")
288
+ }
289
+ }
290
+
291
+ // funcflags.go T_select_noreturn 297 0 1
292
+ // Flags FuncPropNeverReturns
293
+ // <endpropsdump>
294
+ // {"Flags":1,"ParamFlags":[0,0,0],"ResultFlags":null}
295
+ // <endcallsites>
296
+ // <endfuncpreamble>
297
+ func T_select_noreturn(chi chan int, chf chan float32, p *int) {
298
+ rv := 0
299
+ select {
300
+ case i := <-chi:
301
+ rv = i
302
+ case f := <-chf:
303
+ rv = int(f)
304
+ }
305
+ *p = rv
306
+ panic("bad")
307
+ }
308
+
309
+ // funcflags.go T_select_mayreturn 314 0 1
310
+ // <endpropsdump>
311
+ // {"Flags":0,"ParamFlags":[0,0,0],"ResultFlags":[0]}
312
+ // <endcallsites>
313
+ // <endfuncpreamble>
314
+ func T_select_mayreturn(chi chan int, chf chan float32, p *int) int {
315
+ rv := 0
316
+ select {
317
+ case i := <-chi:
318
+ rv = i
319
+ return i
320
+ case f := <-chf:
321
+ rv = int(f)
322
+ }
323
+ *p = rv
324
+ panic("bad")
325
+ }
326
+
327
+ // funcflags.go T_calls_callsexit 334 0 1
328
+ // Flags FuncPropNeverReturns
329
+ // <endpropsdump>
330
+ // {"Flags":1,"ParamFlags":[0],"ResultFlags":null}
331
+ // callsite: funcflags.go:335:15|0 flagstr "CallSiteOnPanicPath" flagval 2 score 102 mask 1 maskstr "panicPathAdj"
332
+ // <endcallsites>
333
+ // <endfuncpreamble>
334
+ func T_calls_callsexit(x int) {
335
+ exprcallsexit(x)
336
+ }
337
+
338
+ func exprcallsexit(x int) int {
339
+ os.Exit(x)
340
+ return x
341
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/props/params.go ADDED
@@ -0,0 +1,367 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ // DO NOT EDIT (use 'go test -v -update-expected' instead.)
6
+ // See cmd/compile/internal/inline/inlheur/testdata/props/README.txt
7
+ // for more information on the format of this file.
8
+ // <endfilepreamble>
9
+ package params
10
+
11
+ import "os"
12
+
13
+ // params.go T_feeds_if_simple 20 0 1
14
+ // ParamFlags
15
+ // 0 ParamFeedsIfOrSwitch
16
+ // <endpropsdump>
17
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":null}
18
+ // <endcallsites>
19
+ // <endfuncpreamble>
20
+ func T_feeds_if_simple(x int) {
21
+ if x < 100 {
22
+ os.Exit(1)
23
+ }
24
+ println(x)
25
+ }
26
+
27
+ // params.go T_feeds_if_nested 35 0 1
28
+ // ParamFlags
29
+ // 0 ParamMayFeedIfOrSwitch
30
+ // 1 ParamFeedsIfOrSwitch
31
+ // <endpropsdump>
32
+ // {"Flags":0,"ParamFlags":[64,32],"ResultFlags":null}
33
+ // <endcallsites>
34
+ // <endfuncpreamble>
35
+ func T_feeds_if_nested(x, y int) {
36
+ if y != 0 {
37
+ if x < 100 {
38
+ os.Exit(1)
39
+ }
40
+ }
41
+ println(x)
42
+ }
43
+
44
+ // params.go T_feeds_if_pointer 51 0 1
45
+ // ParamFlags
46
+ // 0 ParamFeedsIfOrSwitch
47
+ // <endpropsdump>
48
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":null}
49
+ // <endcallsites>
50
+ // <endfuncpreamble>
51
+ func T_feeds_if_pointer(xp *int) {
52
+ if xp != nil {
53
+ os.Exit(1)
54
+ }
55
+ println(xp)
56
+ }
57
+
58
+ // params.go T.T_feeds_if_simple_method 66 0 1
59
+ // ParamFlags
60
+ // 0 ParamFeedsIfOrSwitch
61
+ // 1 ParamFeedsIfOrSwitch
62
+ // <endpropsdump>
63
+ // {"Flags":0,"ParamFlags":[32,32],"ResultFlags":null}
64
+ // <endcallsites>
65
+ // <endfuncpreamble>
66
+ func (r T) T_feeds_if_simple_method(x int) {
67
+ if x < 100 {
68
+ os.Exit(1)
69
+ }
70
+ if r != 99 {
71
+ os.Exit(2)
72
+ }
73
+ println(x)
74
+ }
75
+
76
+ // params.go T_feeds_if_blanks 86 0 1
77
+ // ParamFlags
78
+ // 0 ParamNoInfo
79
+ // 1 ParamFeedsIfOrSwitch
80
+ // 2 ParamNoInfo
81
+ // 3 ParamNoInfo
82
+ // <endpropsdump>
83
+ // {"Flags":0,"ParamFlags":[0,32,0,0],"ResultFlags":null}
84
+ // <endcallsites>
85
+ // <endfuncpreamble>
86
+ func T_feeds_if_blanks(_ string, x int, _ bool, _ bool) {
87
+ // blanks ignored; from a props perspective "x" is param 0
88
+ if x < 100 {
89
+ os.Exit(1)
90
+ }
91
+ println(x)
92
+ }
93
+
94
+ // params.go T_feeds_if_with_copy 101 0 1
95
+ // ParamFlags
96
+ // 0 ParamFeedsIfOrSwitch
97
+ // <endpropsdump>
98
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":null}
99
+ // <endcallsites>
100
+ // <endfuncpreamble>
101
+ func T_feeds_if_with_copy(x int) {
102
+ // simple copy here -- we get this case
103
+ xx := x
104
+ if xx < 100 {
105
+ os.Exit(1)
106
+ }
107
+ println(x)
108
+ }
109
+
110
+ // params.go T_feeds_if_with_copy_expr 115 0 1
111
+ // <endpropsdump>
112
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
113
+ // <endcallsites>
114
+ // <endfuncpreamble>
115
+ func T_feeds_if_with_copy_expr(x int) {
116
+ // this case (copy of expression) currently not handled.
117
+ xx := x < 100
118
+ if xx {
119
+ os.Exit(1)
120
+ }
121
+ println(x)
122
+ }
123
+
124
+ // params.go T_feeds_switch 131 0 1
125
+ // ParamFlags
126
+ // 0 ParamFeedsIfOrSwitch
127
+ // <endpropsdump>
128
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":null}
129
+ // <endcallsites>
130
+ // <endfuncpreamble>
131
+ func T_feeds_switch(x int) {
132
+ switch x {
133
+ case 101:
134
+ println(101)
135
+ case 202:
136
+ panic("bad")
137
+ }
138
+ println(x)
139
+ }
140
+
141
+ // params.go T_feeds_if_toocomplex 146 0 1
142
+ // <endpropsdump>
143
+ // {"Flags":0,"ParamFlags":[0,0],"ResultFlags":null}
144
+ // <endcallsites>
145
+ // <endfuncpreamble>
146
+ func T_feeds_if_toocomplex(x int, y int) {
147
+ // not handled at the moment; we only look for cases where
148
+ // an "if" or "switch" can be simplified based on a single
149
+ // constant param, not a combination of constant params.
150
+ if x < y {
151
+ panic("bad")
152
+ }
153
+ println(x + y)
154
+ }
155
+
156
+ // params.go T_feeds_if_redefined 161 0 1
157
+ // <endpropsdump>
158
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
159
+ // <endcallsites>
160
+ // <endfuncpreamble>
161
+ func T_feeds_if_redefined(x int) {
162
+ if x < G {
163
+ x++
164
+ }
165
+ if x == 101 {
166
+ panic("bad")
167
+ }
168
+ }
169
+
170
+ // params.go T_feeds_if_redefined2 175 0 1
171
+ // <endpropsdump>
172
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
173
+ // <endcallsites>
174
+ // <endfuncpreamble>
175
+ func T_feeds_if_redefined2(x int) {
176
+ // this currently classifies "x" as "no info", since the analysis we
177
+ // use to check for reassignments/redefinitions is not flow-sensitive,
178
+ // but we could probably catch this case with better analysis or
179
+ // high-level SSA.
180
+ if x == 101 {
181
+ panic("bad")
182
+ }
183
+ if x < G {
184
+ x++
185
+ }
186
+ }
187
+
188
+ // params.go T_feeds_multi_if 196 0 1
189
+ // ParamFlags
190
+ // 0 ParamFeedsIfOrSwitch
191
+ // 1 ParamNoInfo
192
+ // <endpropsdump>
193
+ // {"Flags":0,"ParamFlags":[32,0],"ResultFlags":null}
194
+ // <endcallsites>
195
+ // <endfuncpreamble>
196
+ func T_feeds_multi_if(x int, y int) {
197
+ // Here we have one "if" that is too complex (x < y) but one that is
198
+ // simple enough. Currently we enable the heuristic for this. It's
199
+ // possible to imagine this being a bad thing if the function in
200
+ // question is sufficiently large, but if it's too large we probably
201
+ // can't inline it anyhow.
202
+ if x < y {
203
+ panic("bad")
204
+ }
205
+ if x < 10 {
206
+ panic("whatev")
207
+ }
208
+ println(x + y)
209
+ }
210
+
211
+ // params.go T_feeds_if_redefined_indirectwrite 216 0 1
212
+ // <endpropsdump>
213
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
214
+ // <endcallsites>
215
+ // <endfuncpreamble>
216
+ func T_feeds_if_redefined_indirectwrite(x int) {
217
+ ax := &x
218
+ if G != 2 {
219
+ *ax = G
220
+ }
221
+ if x == 101 {
222
+ panic("bad")
223
+ }
224
+ }
225
+
226
+ // params.go T_feeds_if_redefined_indirectwrite_copy 231 0 1
227
+ // <endpropsdump>
228
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
229
+ // <endcallsites>
230
+ // <endfuncpreamble>
231
+ func T_feeds_if_redefined_indirectwrite_copy(x int) {
232
+ // we don't catch this case, "x" is marked as no info,
233
+ // since we're conservative about redefinitions.
234
+ ax := &x
235
+ cx := x
236
+ if G != 2 {
237
+ *ax = G
238
+ }
239
+ if cx == 101 {
240
+ panic("bad")
241
+ }
242
+ }
243
+
244
+ // params.go T_feeds_if_expr1 251 0 1
245
+ // ParamFlags
246
+ // 0 ParamFeedsIfOrSwitch
247
+ // <endpropsdump>
248
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":null}
249
+ // <endcallsites>
250
+ // <endfuncpreamble>
251
+ func T_feeds_if_expr1(x int) {
252
+ if x == 101 || x == 102 || x&0xf == 0 {
253
+ panic("bad")
254
+ }
255
+ }
256
+
257
+ // params.go T_feeds_if_expr2 262 0 1
258
+ // <endpropsdump>
259
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
260
+ // <endcallsites>
261
+ // <endfuncpreamble>
262
+ func T_feeds_if_expr2(x int) {
263
+ if (x*x)-(x+x)%x == 101 || x&0xf == 0 {
264
+ panic("bad")
265
+ }
266
+ }
267
+
268
+ // params.go T_feeds_if_expr3 273 0 1
269
+ // <endpropsdump>
270
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
271
+ // <endcallsites>
272
+ // <endfuncpreamble>
273
+ func T_feeds_if_expr3(x int) {
274
+ if x-(x&0x1)^378 > (1 - G) {
275
+ panic("bad")
276
+ }
277
+ }
278
+
279
+ // params.go T_feeds_if_shift_may_panic 284 0 1
280
+ // <endpropsdump>
281
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":[0]}
282
+ // <endcallsites>
283
+ // <endfuncpreamble>
284
+ func T_feeds_if_shift_may_panic(x int) *int {
285
+ // here if "x" is a constant like 2, we could simplify the "if",
286
+ // but if we were to pass in a negative value for "x" we can't
287
+ // fold the condition due to the need to panic on negative shift.
288
+ if 1<<x > 1024 {
289
+ return nil
290
+ }
291
+ return &G
292
+ }
293
+
294
+ // params.go T_feeds_if_maybe_divide_by_zero 299 0 1
295
+ // <endpropsdump>
296
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
297
+ // <endcallsites>
298
+ // <endfuncpreamble>
299
+ func T_feeds_if_maybe_divide_by_zero(x int) {
300
+ if 99/x == 3 {
301
+ return
302
+ }
303
+ println("blarg")
304
+ }
305
+
306
+ // params.go T_feeds_indcall 313 0 1
307
+ // ParamFlags
308
+ // 0 ParamMayFeedIndirectCall
309
+ // <endpropsdump>
310
+ // {"Flags":0,"ParamFlags":[16],"ResultFlags":null}
311
+ // <endcallsites>
312
+ // <endfuncpreamble>
313
+ func T_feeds_indcall(x func()) {
314
+ if G != 20 {
315
+ x()
316
+ }
317
+ }
318
+
319
+ // params.go T_feeds_indcall_and_if 326 0 1
320
+ // ParamFlags
321
+ // 0 ParamMayFeedIndirectCall|ParamFeedsIfOrSwitch
322
+ // <endpropsdump>
323
+ // {"Flags":0,"ParamFlags":[48],"ResultFlags":null}
324
+ // <endcallsites>
325
+ // <endfuncpreamble>
326
+ func T_feeds_indcall_and_if(x func()) {
327
+ if x != nil {
328
+ x()
329
+ }
330
+ }
331
+
332
+ // params.go T_feeds_indcall_with_copy 339 0 1
333
+ // ParamFlags
334
+ // 0 ParamFeedsIndirectCall
335
+ // <endpropsdump>
336
+ // {"Flags":0,"ParamFlags":[8],"ResultFlags":null}
337
+ // <endcallsites>
338
+ // <endfuncpreamble>
339
+ func T_feeds_indcall_with_copy(x func()) {
340
+ xx := x
341
+ if G < 10 {
342
+ G--
343
+ }
344
+ xx()
345
+ }
346
+
347
+ // params.go T_feeds_interface_method_call 354 0 1
348
+ // ParamFlags
349
+ // 0 ParamFeedsInterfaceMethodCall
350
+ // <endpropsdump>
351
+ // {"Flags":0,"ParamFlags":[2],"ResultFlags":null}
352
+ // <endcallsites>
353
+ // <endfuncpreamble>
354
+ func T_feeds_interface_method_call(i I) {
355
+ i.Blarg()
356
+ }
357
+
358
+ var G int
359
+
360
+ type T int
361
+
362
+ type I interface {
363
+ Blarg()
364
+ }
365
+
366
+ func (r T) Blarg() {
367
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/props/returns.go ADDED
@@ -0,0 +1,370 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ // DO NOT EDIT (use 'go test -v -update-expected' instead.)
6
+ // See cmd/compile/internal/inline/inlheur/testdata/props/README.txt
7
+ // for more information on the format of this file.
8
+ // <endfilepreamble>
9
+
10
+ package returns1
11
+
12
+ import "unsafe"
13
+
14
+ // returns.go T_simple_allocmem 21 0 1
15
+ // ResultFlags
16
+ // 0 ResultIsAllocatedMem
17
+ // <endpropsdump>
18
+ // {"Flags":0,"ParamFlags":null,"ResultFlags":[2]}
19
+ // <endcallsites>
20
+ // <endfuncpreamble>
21
+ func T_simple_allocmem() *Bar {
22
+ return &Bar{}
23
+ }
24
+
25
+ // returns.go T_allocmem_two_returns 34 0 1
26
+ // ParamFlags
27
+ // 0 ParamFeedsIfOrSwitch
28
+ // ResultFlags
29
+ // 0 ResultIsAllocatedMem
30
+ // <endpropsdump>
31
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":[2]}
32
+ // <endcallsites>
33
+ // <endfuncpreamble>
34
+ func T_allocmem_two_returns(x int) *Bar {
35
+ // multiple returns
36
+ if x < 0 {
37
+ return new(Bar)
38
+ } else {
39
+ return &Bar{x: 2}
40
+ }
41
+ }
42
+
43
+ // returns.go T_allocmem_three_returns 52 0 1
44
+ // ParamFlags
45
+ // 0 ParamFeedsIfOrSwitch
46
+ // ResultFlags
47
+ // 0 ResultIsAllocatedMem
48
+ // <endpropsdump>
49
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":[2]}
50
+ // <endcallsites>
51
+ // <endfuncpreamble>
52
+ func T_allocmem_three_returns(x int) []*Bar {
53
+ // more multiple returns
54
+ switch x {
55
+ case 10, 11, 12:
56
+ return make([]*Bar, 10)
57
+ case 13:
58
+ fallthrough
59
+ case 15:
60
+ return []*Bar{&Bar{x: 15}}
61
+ }
62
+ return make([]*Bar, 0, 10)
63
+ }
64
+
65
+ // returns.go T_return_nil 72 0 1
66
+ // ResultFlags
67
+ // 0 ResultAlwaysSameConstant
68
+ // <endpropsdump>
69
+ // {"Flags":0,"ParamFlags":null,"ResultFlags":[8]}
70
+ // <endcallsites>
71
+ // <endfuncpreamble>
72
+ func T_return_nil() *Bar {
73
+ // simple case: no alloc
74
+ return nil
75
+ }
76
+
77
+ // returns.go T_multi_return_nil 84 0 1
78
+ // ResultFlags
79
+ // 0 ResultAlwaysSameConstant
80
+ // <endpropsdump>
81
+ // {"Flags":0,"ParamFlags":[0,0],"ResultFlags":[8]}
82
+ // <endcallsites>
83
+ // <endfuncpreamble>
84
+ func T_multi_return_nil(x, y bool) *Bar {
85
+ if x && y {
86
+ return nil
87
+ }
88
+ return nil
89
+ }
90
+
91
+ // returns.go T_multi_return_nil_anomoly 98 0 1
92
+ // ResultFlags
93
+ // 0 ResultIsConcreteTypeConvertedToInterface
94
+ // <endpropsdump>
95
+ // {"Flags":0,"ParamFlags":[0,0],"ResultFlags":[4]}
96
+ // <endcallsites>
97
+ // <endfuncpreamble>
98
+ func T_multi_return_nil_anomoly(x, y bool) Itf {
99
+ if x && y {
100
+ var qnil *Q
101
+ return qnil
102
+ }
103
+ var barnil *Bar
104
+ return barnil
105
+ }
106
+
107
+ // returns.go T_multi_return_some_nil 112 0 1
108
+ // <endpropsdump>
109
+ // {"Flags":0,"ParamFlags":[0,0],"ResultFlags":[0]}
110
+ // <endcallsites>
111
+ // <endfuncpreamble>
112
+ func T_multi_return_some_nil(x, y bool) *Bar {
113
+ if x && y {
114
+ return nil
115
+ } else {
116
+ return &GB
117
+ }
118
+ }
119
+
120
+ // returns.go T_mixed_returns 127 0 1
121
+ // ParamFlags
122
+ // 0 ParamFeedsIfOrSwitch
123
+ // <endpropsdump>
124
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":[0]}
125
+ // <endcallsites>
126
+ // <endfuncpreamble>
127
+ func T_mixed_returns(x int) *Bar {
128
+ // mix of alloc and non-alloc
129
+ if x < 0 {
130
+ return new(Bar)
131
+ } else {
132
+ return &GB
133
+ }
134
+ }
135
+
136
+ // returns.go T_mixed_returns_slice 143 0 1
137
+ // ParamFlags
138
+ // 0 ParamFeedsIfOrSwitch
139
+ // <endpropsdump>
140
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":[0]}
141
+ // <endcallsites>
142
+ // <endfuncpreamble>
143
+ func T_mixed_returns_slice(x int) []*Bar {
144
+ // mix of alloc and non-alloc
145
+ switch x {
146
+ case 10, 11, 12:
147
+ return make([]*Bar, 10)
148
+ case 13:
149
+ fallthrough
150
+ case 15:
151
+ return []*Bar{&Bar{x: 15}}
152
+ }
153
+ ba := [...]*Bar{&GB, &GB}
154
+ return ba[:]
155
+ }
156
+
157
+ // returns.go T_maps_and_channels 167 0 1
158
+ // ResultFlags
159
+ // 0 ResultNoInfo
160
+ // 1 ResultNoInfo
161
+ // 2 ResultNoInfo
162
+ // 3 ResultAlwaysSameConstant
163
+ // <endpropsdump>
164
+ // {"Flags":0,"ParamFlags":[0,0],"ResultFlags":[0,0,0,8]}
165
+ // <endcallsites>
166
+ // <endfuncpreamble>
167
+ func T_maps_and_channels(x int, b bool) (bool, map[int]int, chan bool, unsafe.Pointer) {
168
+ // maps and channels
169
+ return b, make(map[int]int), make(chan bool), nil
170
+ }
171
+
172
+ // returns.go T_assignment_to_named_returns 179 0 1
173
+ // ParamFlags
174
+ // 0 ParamFeedsIfOrSwitch
175
+ // <endpropsdump>
176
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":[0,0]}
177
+ // <endcallsites>
178
+ // <endfuncpreamble>
179
+ func T_assignment_to_named_returns(x int) (r1 *uint64, r2 *uint64) {
180
+ // assignments to named returns and then "return" not supported
181
+ r1 = new(uint64)
182
+ if x < 1 {
183
+ *r1 = 2
184
+ }
185
+ r2 = new(uint64)
186
+ return
187
+ }
188
+
189
+ // returns.go T_named_returns_but_return_explicit_values 199 0 1
190
+ // ParamFlags
191
+ // 0 ParamFeedsIfOrSwitch
192
+ // ResultFlags
193
+ // 0 ResultIsAllocatedMem
194
+ // 1 ResultIsAllocatedMem
195
+ // <endpropsdump>
196
+ // {"Flags":0,"ParamFlags":[32],"ResultFlags":[2,2]}
197
+ // <endcallsites>
198
+ // <endfuncpreamble>
199
+ func T_named_returns_but_return_explicit_values(x int) (r1 *uint64, r2 *uint64) {
200
+ // named returns ok if all returns are non-empty
201
+ rx1 := new(uint64)
202
+ if x < 1 {
203
+ *rx1 = 2
204
+ }
205
+ rx2 := new(uint64)
206
+ return rx1, rx2
207
+ }
208
+
209
+ // returns.go T_return_concrete_type_to_itf 216 0 1
210
+ // ResultFlags
211
+ // 0 ResultIsConcreteTypeConvertedToInterface
212
+ // <endpropsdump>
213
+ // {"Flags":0,"ParamFlags":[0,0],"ResultFlags":[4]}
214
+ // <endcallsites>
215
+ // <endfuncpreamble>
216
+ func T_return_concrete_type_to_itf(x, y int) Itf {
217
+ return &Bar{}
218
+ }
219
+
220
+ // returns.go T_return_concrete_type_to_itfwith_copy 227 0 1
221
+ // ResultFlags
222
+ // 0 ResultIsConcreteTypeConvertedToInterface
223
+ // <endpropsdump>
224
+ // {"Flags":0,"ParamFlags":[0,0],"ResultFlags":[4]}
225
+ // <endcallsites>
226
+ // <endfuncpreamble>
227
+ func T_return_concrete_type_to_itfwith_copy(x, y int) Itf {
228
+ b := &Bar{}
229
+ println("whee")
230
+ return b
231
+ }
232
+
233
+ // returns.go T_return_concrete_type_to_itf_mixed 238 0 1
234
+ // <endpropsdump>
235
+ // {"Flags":0,"ParamFlags":[0,0],"ResultFlags":[0]}
236
+ // <endcallsites>
237
+ // <endfuncpreamble>
238
+ func T_return_concrete_type_to_itf_mixed(x, y int) Itf {
239
+ if x < y {
240
+ b := &Bar{}
241
+ return b
242
+ }
243
+ return nil
244
+ }
245
+
246
+ // returns.go T_return_same_func 253 0 1
247
+ // ResultFlags
248
+ // 0 ResultAlwaysSameInlinableFunc
249
+ // <endpropsdump>
250
+ // {"Flags":0,"ParamFlags":null,"ResultFlags":[32]}
251
+ // <endcallsites>
252
+ // <endfuncpreamble>
253
+ func T_return_same_func() func(int) int {
254
+ if G < 10 {
255
+ return foo
256
+ } else {
257
+ return foo
258
+ }
259
+ }
260
+
261
+ // returns.go T_return_different_funcs 266 0 1
262
+ // <endpropsdump>
263
+ // {"Flags":0,"ParamFlags":null,"ResultFlags":[0]}
264
+ // <endcallsites>
265
+ // <endfuncpreamble>
266
+ func T_return_different_funcs() func(int) int {
267
+ if G != 10 {
268
+ return foo
269
+ } else {
270
+ return bar
271
+ }
272
+ }
273
+
274
+ // returns.go T_return_same_closure 286 0 1
275
+ // ResultFlags
276
+ // 0 ResultAlwaysSameInlinableFunc
277
+ // <endpropsdump>
278
+ // {"Flags":0,"ParamFlags":null,"ResultFlags":[32]}
279
+ // <endcallsites>
280
+ // <endfuncpreamble>
281
+ // returns.go T_return_same_closure.func1 287 0 1
282
+ // <endpropsdump>
283
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":[0]}
284
+ // <endcallsites>
285
+ // <endfuncpreamble>
286
+ func T_return_same_closure() func(int) int {
287
+ p := func(q int) int { return q }
288
+ if G < 10 {
289
+ return p
290
+ } else {
291
+ return p
292
+ }
293
+ }
294
+
295
+ // returns.go T_return_different_closures 312 0 1
296
+ // <endpropsdump>
297
+ // {"Flags":0,"ParamFlags":null,"ResultFlags":[0]}
298
+ // <endcallsites>
299
+ // <endfuncpreamble>
300
+ // returns.go T_return_different_closures.func1 313 0 1
301
+ // <endpropsdump>
302
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":[0]}
303
+ // <endcallsites>
304
+ // <endfuncpreamble>
305
+ // returns.go T_return_different_closures.func2 317 0 1
306
+ // ResultFlags
307
+ // 0 ResultAlwaysSameConstant
308
+ // <endpropsdump>
309
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":[8]}
310
+ // <endcallsites>
311
+ // <endfuncpreamble>
312
+ func T_return_different_closures() func(int) int {
313
+ p := func(q int) int { return q }
314
+ if G < 10 {
315
+ return p
316
+ } else {
317
+ return func(q int) int { return 101 }
318
+ }
319
+ }
320
+
321
+ // returns.go T_return_noninlinable 339 0 1
322
+ // ResultFlags
323
+ // 0 ResultAlwaysSameFunc
324
+ // <endpropsdump>
325
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":[16]}
326
+ // <endcallsites>
327
+ // <endfuncpreamble>
328
+ // returns.go T_return_noninlinable.func1 340 0 1
329
+ // <endpropsdump>
330
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":[0]}
331
+ // callsite: returns.go:343:4|0 flagstr "" flagval 0 score 4 mask 0 maskstr ""
332
+ // <endcallsites>
333
+ // <endfuncpreamble>
334
+ // returns.go T_return_noninlinable.func1.1 341 0 1
335
+ // <endpropsdump>
336
+ // {"Flags":0,"ParamFlags":null,"ResultFlags":null}
337
+ // <endcallsites>
338
+ // <endfuncpreamble>
339
+ func T_return_noninlinable(x int) func(int) int {
340
+ noti := func(q int) int {
341
+ defer func() {
342
+ println(q + x)
343
+ }()
344
+ return q
345
+ }
346
+ return noti
347
+ }
348
+
349
+ type Bar struct {
350
+ x int
351
+ y string
352
+ }
353
+
354
+ func (b *Bar) Plark() {
355
+ }
356
+
357
+ type Q int
358
+
359
+ func (q *Q) Plark() {
360
+ }
361
+
362
+ func foo(x int) int { return x }
363
+ func bar(x int) int { return -x }
364
+
365
+ var G int
366
+ var GB Bar
367
+
368
+ type Itf interface {
369
+ Plark()
370
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/testdata/props/returns2.go ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ // DO NOT EDIT (use 'go test -v -update-expected' instead.)
6
+ // See cmd/compile/internal/inline/inlheur/testdata/props/README.txt
7
+ // for more information on the format of this file.
8
+ // <endfilepreamble>
9
+
10
+ package returns2
11
+
12
+ // returns2.go T_return_feeds_iface_call 18 0 1
13
+ // <endpropsdump>
14
+ // {"Flags":0,"ParamFlags":null,"ResultFlags":null}
15
+ // callsite: returns2.go:19:13|0 flagstr "" flagval 0 score 1 mask 16384 maskstr "returnFeedsConcreteToInterfaceCallAdj"
16
+ // <endcallsites>
17
+ // <endfuncpreamble>
18
+ func T_return_feeds_iface_call() {
19
+ b := newBar(10)
20
+ b.Plark()
21
+ }
22
+
23
+ // returns2.go T_multi_return_feeds_iface_call 29 0 1
24
+ // <endpropsdump>
25
+ // {"Flags":0,"ParamFlags":null,"ResultFlags":null}
26
+ // callsite: returns2.go:30:20|0 flagstr "" flagval 0 score 3 mask 16384 maskstr "returnFeedsConcreteToInterfaceCallAdj"
27
+ // <endcallsites>
28
+ // <endfuncpreamble>
29
+ func T_multi_return_feeds_iface_call() {
30
+ _, b, _ := newBar2(10)
31
+ b.Plark()
32
+ }
33
+
34
+ // returns2.go T_returned_inlinable_func_feeds_indirect_call 41 0 1
35
+ // <endpropsdump>
36
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
37
+ // callsite: returns2.go:42:18|0 flagstr "" flagval 0 score -51 mask 8200 maskstr "passConstToIfAdj|returnFeedsInlinableFuncToIndCallAdj"
38
+ // callsite: returns2.go:44:20|1 flagstr "" flagval 0 score -23 mask 8192 maskstr "returnFeedsInlinableFuncToIndCallAdj"
39
+ // <endcallsites>
40
+ // <endfuncpreamble>
41
+ func T_returned_inlinable_func_feeds_indirect_call(q int) {
42
+ f := returnsFunc(10)
43
+ f(q)
44
+ f2 := returnsFunc2()
45
+ f2(q)
46
+ }
47
+
48
+ // returns2.go T_returned_noninlineable_func_feeds_indirect_call 54 0 1
49
+ // <endpropsdump>
50
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
51
+ // callsite: returns2.go:55:30|0 flagstr "" flagval 0 score -23 mask 4096 maskstr "returnFeedsFuncToIndCallAdj"
52
+ // <endcallsites>
53
+ // <endfuncpreamble>
54
+ func T_returned_noninlineable_func_feeds_indirect_call(q int) {
55
+ f := returnsNonInlinableFunc()
56
+ f(q)
57
+ }
58
+
59
+ // returns2.go T_multi_return_feeds_indirect_call 65 0 1
60
+ // <endpropsdump>
61
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":null}
62
+ // callsite: returns2.go:66:29|0 flagstr "" flagval 0 score -21 mask 8192 maskstr "returnFeedsInlinableFuncToIndCallAdj"
63
+ // <endcallsites>
64
+ // <endfuncpreamble>
65
+ func T_multi_return_feeds_indirect_call(q int) {
66
+ _, f, _ := multiReturnsFunc()
67
+ f(q)
68
+ }
69
+
70
+ // returns2.go T_return_feeds_ifswitch 76 0 1
71
+ // <endpropsdump>
72
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":[0]}
73
+ // callsite: returns2.go:77:14|0 flagstr "" flagval 0 score 10 mask 2048 maskstr "returnFeedsConstToIfAdj"
74
+ // <endcallsites>
75
+ // <endfuncpreamble>
76
+ func T_return_feeds_ifswitch(q int) int {
77
+ x := meaning(q)
78
+ if x < 42 {
79
+ switch x {
80
+ case 42:
81
+ return 1
82
+ }
83
+ }
84
+ return 0
85
+ }
86
+
87
+ // returns2.go T_multi_return_feeds_ifswitch 93 0 1
88
+ // <endpropsdump>
89
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":[0]}
90
+ // callsite: returns2.go:94:21|0 flagstr "" flagval 0 score 9 mask 2048 maskstr "returnFeedsConstToIfAdj"
91
+ // <endcallsites>
92
+ // <endfuncpreamble>
93
+ func T_multi_return_feeds_ifswitch(q int) int {
94
+ x, y, z := meanings(q)
95
+ if x < y {
96
+ switch x {
97
+ case 42:
98
+ return z
99
+ }
100
+ }
101
+ return 0
102
+ }
103
+
104
+ // returns2.go T_two_calls_feed_ifswitch 111 0 1
105
+ // <endpropsdump>
106
+ // {"Flags":0,"ParamFlags":[0],"ResultFlags":[0]}
107
+ // callsite: returns2.go:115:14|0 flagstr "" flagval 0 score 25 mask 0 maskstr ""
108
+ // callsite: returns2.go:116:14|1 flagstr "" flagval 0 score 25 mask 0 maskstr ""
109
+ // <endcallsites>
110
+ // <endfuncpreamble>
111
+ func T_two_calls_feed_ifswitch(q int) int {
112
+ // This case we don't handle; for the heuristic to kick in,
113
+ // all names in a given if/switch cond have to come from the
114
+ // same callsite
115
+ x := meaning(q)
116
+ y := meaning(-q)
117
+ if x < y {
118
+ switch x + y {
119
+ case 42:
120
+ return 1
121
+ }
122
+ }
123
+ return 0
124
+ }
125
+
126
+ // returns2.go T_chained_indirect_call 132 0 1
127
+ // <endpropsdump>
128
+ // {"Flags":0,"ParamFlags":[0,0],"ResultFlags":null}
129
+ // callsite: returns2.go:135:18|0 flagstr "" flagval 0 score -31 mask 8192 maskstr "returnFeedsInlinableFuncToIndCallAdj"
130
+ // <endcallsites>
131
+ // <endfuncpreamble>
132
+ func T_chained_indirect_call(x, y int) {
133
+ // Here 'returnsFunc' returns an inlinable func that feeds
134
+ // directly into a call (no named intermediate).
135
+ G += returnsFunc(x - y)(x + y)
136
+ }
137
+
138
+ // returns2.go T_chained_conc_iface_call 144 0 1
139
+ // <endpropsdump>
140
+ // {"Flags":0,"ParamFlags":[0,0],"ResultFlags":null}
141
+ // callsite: returns2.go:148:8|0 flagstr "" flagval 0 score 1 mask 16384 maskstr "returnFeedsConcreteToInterfaceCallAdj"
142
+ // <endcallsites>
143
+ // <endfuncpreamble>
144
+ func T_chained_conc_iface_call(x, y int) {
145
+ // Similar to the case above, return from call returning concrete type
146
+ // feeds directly into interface call. Note that only the first
147
+ // iface call is interesting here.
148
+ newBar(10).Plark().Plark()
149
+ }
150
+
151
+ func returnsFunc(x int) func(int) int {
152
+ if x < 0 {
153
+ G++
154
+ }
155
+ return adder
156
+ }
157
+
158
+ func returnsFunc2() func(int) int {
159
+ return func(x int) int {
160
+ return adder(x)
161
+ }
162
+ }
163
+
164
+ func returnsNonInlinableFunc() func(int) int {
165
+ return adderNoInline
166
+ }
167
+
168
+ func multiReturnsFunc() (int, func(int) int, int) {
169
+ return 42, func(x int) int { G++; return 1 }, -42
170
+ }
171
+
172
+ func adder(x int) int {
173
+ G += 1
174
+ return G
175
+ }
176
+
177
+ func adderNoInline(x int) int {
178
+ defer func() { G += x }()
179
+ G += 1
180
+ return G
181
+ }
182
+
183
+ func meaning(q int) int {
184
+ r := 0
185
+ for i := 0; i < 42; i++ {
186
+ r += q
187
+ }
188
+ G += r
189
+ return 42
190
+ }
191
+
192
+ func meanings(q int) (int, int, int) {
193
+ r := 0
194
+ for i := 0; i < 42; i++ {
195
+ r += q
196
+ }
197
+ return 42, 43, r
198
+ }
199
+
200
+ type Bar struct {
201
+ x int
202
+ y string
203
+ }
204
+
205
+ func (b *Bar) Plark() Itf {
206
+ return b
207
+ }
208
+
209
+ type Itf interface {
210
+ Plark() Itf
211
+ }
212
+
213
+ func newBar(x int) Itf {
214
+ s := 0
215
+ for i := 0; i < x; i++ {
216
+ s += i
217
+ }
218
+ return &Bar{
219
+ x: s,
220
+ }
221
+ }
222
+
223
+ func newBar2(x int) (int, Itf, bool) {
224
+ s := 0
225
+ for i := 0; i < x; i++ {
226
+ s += i
227
+ }
228
+ return 0, &Bar{x: s}, false
229
+ }
230
+
231
+ var G int
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/texpr_classify_test.go ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import (
8
+ "cmd/compile/internal/ir"
9
+ "cmd/compile/internal/typecheck"
10
+ "cmd/compile/internal/types"
11
+ "cmd/internal/src"
12
+ "go/constant"
13
+ "testing"
14
+ )
15
+
16
+ var pos src.XPos
17
+ var local *types.Pkg
18
+ var f *ir.Func
19
+
20
+ func init() {
21
+ types.PtrSize = 8
22
+ types.RegSize = 8
23
+ types.MaxWidth = 1 << 50
24
+ typecheck.InitUniverse()
25
+ local = types.NewPkg("", "")
26
+ fsym := &types.Sym{
27
+ Pkg: types.NewPkg("my/import/path", "path"),
28
+ Name: "function",
29
+ }
30
+ f = ir.NewFunc(src.NoXPos, src.NoXPos, fsym, nil)
31
+ }
32
+
33
+ type state struct {
34
+ ntab map[string]*ir.Name
35
+ }
36
+
37
+ func mkstate() *state {
38
+ return &state{
39
+ ntab: make(map[string]*ir.Name),
40
+ }
41
+ }
42
+
43
+ func bin(x ir.Node, op ir.Op, y ir.Node) ir.Node {
44
+ return ir.NewBinaryExpr(pos, op, x, y)
45
+ }
46
+
47
+ func conv(x ir.Node, t *types.Type) ir.Node {
48
+ return ir.NewConvExpr(pos, ir.OCONV, t, x)
49
+ }
50
+
51
+ func logical(x ir.Node, op ir.Op, y ir.Node) ir.Node {
52
+ return ir.NewLogicalExpr(pos, op, x, y)
53
+ }
54
+
55
+ func un(op ir.Op, x ir.Node) ir.Node {
56
+ return ir.NewUnaryExpr(pos, op, x)
57
+ }
58
+
59
+ func liti(i int64) ir.Node {
60
+ return ir.NewBasicLit(pos, types.Types[types.TINT64], constant.MakeInt64(i))
61
+ }
62
+
63
+ func lits(s string) ir.Node {
64
+ return ir.NewBasicLit(pos, types.Types[types.TSTRING], constant.MakeString(s))
65
+ }
66
+
67
+ func (s *state) nm(name string, t *types.Type) *ir.Name {
68
+ if n, ok := s.ntab[name]; ok {
69
+ if n.Type() != t {
70
+ panic("bad")
71
+ }
72
+ return n
73
+ }
74
+ sym := local.Lookup(name)
75
+ nn := ir.NewNameAt(pos, sym, t)
76
+ s.ntab[name] = nn
77
+ return nn
78
+ }
79
+
80
+ func (s *state) nmi64(name string) *ir.Name {
81
+ return s.nm(name, types.Types[types.TINT64])
82
+ }
83
+
84
+ func (s *state) nms(name string) *ir.Name {
85
+ return s.nm(name, types.Types[types.TSTRING])
86
+ }
87
+
88
+ func TestClassifyIntegerCompare(t *testing.T) {
89
+
90
+ // (n < 10 || n > 100) && (n >= 12 || n <= 99 || n != 101)
91
+ s := mkstate()
92
+ nn := s.nmi64("n")
93
+ nlt10 := bin(nn, ir.OLT, liti(10)) // n < 10
94
+ ngt100 := bin(nn, ir.OGT, liti(100)) // n > 100
95
+ nge12 := bin(nn, ir.OGE, liti(12)) // n >= 12
96
+ nle99 := bin(nn, ir.OLE, liti(99)) // n < 10
97
+ nne101 := bin(nn, ir.ONE, liti(101)) // n != 101
98
+ noror1 := logical(nlt10, ir.OOROR, ngt100) // n < 10 || n > 100
99
+ noror2 := logical(nge12, ir.OOROR, nle99) // n >= 12 || n <= 99
100
+ noror3 := logical(noror2, ir.OOROR, nne101)
101
+ nandand := typecheck.Expr(logical(noror1, ir.OANDAND, noror3))
102
+
103
+ wantv := true
104
+ v := ShouldFoldIfNameConstant(nandand, []*ir.Name{nn})
105
+ if v != wantv {
106
+ t.Errorf("wanted shouldfold(%v) %v, got %v", nandand, wantv, v)
107
+ }
108
+ }
109
+
110
+ func TestClassifyStringCompare(t *testing.T) {
111
+
112
+ // s != "foo" && s < "ooblek" && s > "plarkish"
113
+ s := mkstate()
114
+ nn := s.nms("s")
115
+ snefoo := bin(nn, ir.ONE, lits("foo")) // s != "foo"
116
+ sltoob := bin(nn, ir.OLT, lits("ooblek")) // s < "ooblek"
117
+ sgtpk := bin(nn, ir.OGT, lits("plarkish")) // s > "plarkish"
118
+ nandand := logical(snefoo, ir.OANDAND, sltoob)
119
+ top := typecheck.Expr(logical(nandand, ir.OANDAND, sgtpk))
120
+
121
+ wantv := true
122
+ v := ShouldFoldIfNameConstant(top, []*ir.Name{nn})
123
+ if v != wantv {
124
+ t.Errorf("wanted shouldfold(%v) %v, got %v", top, wantv, v)
125
+ }
126
+ }
127
+
128
+ func TestClassifyIntegerArith(t *testing.T) {
129
+ // n+1 ^ n-3 * n/2 + n<<9 + n>>2 - n&^7
130
+
131
+ s := mkstate()
132
+ nn := s.nmi64("n")
133
+ np1 := bin(nn, ir.OADD, liti(1)) // n+1
134
+ nm3 := bin(nn, ir.OSUB, liti(3)) // n-3
135
+ nd2 := bin(nn, ir.ODIV, liti(2)) // n/2
136
+ nls9 := bin(nn, ir.OLSH, liti(9)) // n<<9
137
+ nrs2 := bin(nn, ir.ORSH, liti(2)) // n>>2
138
+ nan7 := bin(nn, ir.OANDNOT, liti(7)) // n&^7
139
+ c1xor := bin(np1, ir.OXOR, nm3)
140
+ c2mul := bin(c1xor, ir.OMUL, nd2)
141
+ c3add := bin(c2mul, ir.OADD, nls9)
142
+ c4add := bin(c3add, ir.OADD, nrs2)
143
+ c5sub := bin(c4add, ir.OSUB, nan7)
144
+ top := typecheck.Expr(c5sub)
145
+
146
+ wantv := true
147
+ v := ShouldFoldIfNameConstant(top, []*ir.Name{nn})
148
+ if v != wantv {
149
+ t.Errorf("wanted shouldfold(%v) %v, got %v", top, wantv, v)
150
+ }
151
+ }
152
+
153
+ func TestClassifyAssortedShifts(t *testing.T) {
154
+
155
+ s := mkstate()
156
+ nn := s.nmi64("n")
157
+ badcases := []ir.Node{
158
+ bin(liti(3), ir.OLSH, nn), // 3<<n
159
+ bin(liti(7), ir.ORSH, nn), // 7>>n
160
+ }
161
+ for _, bc := range badcases {
162
+ wantv := false
163
+ v := ShouldFoldIfNameConstant(typecheck.Expr(bc), []*ir.Name{nn})
164
+ if v != wantv {
165
+ t.Errorf("wanted shouldfold(%v) %v, got %v", bc, wantv, v)
166
+ }
167
+ }
168
+ }
169
+
170
+ func TestClassifyFloat(t *testing.T) {
171
+ // float32(n) + float32(10)
172
+ s := mkstate()
173
+ nn := s.nm("n", types.Types[types.TUINT32])
174
+ f1 := conv(nn, types.Types[types.TFLOAT32])
175
+ f2 := conv(liti(10), types.Types[types.TFLOAT32])
176
+ add := bin(f1, ir.OADD, f2)
177
+
178
+ wantv := false
179
+ v := ShouldFoldIfNameConstant(typecheck.Expr(add), []*ir.Name{nn})
180
+ if v != wantv {
181
+ t.Errorf("wanted shouldfold(%v) %v, got %v", add, wantv, v)
182
+ }
183
+ }
184
+
185
+ func TestMultipleNamesAllUsed(t *testing.T) {
186
+ // n != 101 && m < 2
187
+ s := mkstate()
188
+ nn := s.nmi64("n")
189
+ nm := s.nmi64("m")
190
+ nne101 := bin(nn, ir.ONE, liti(101)) // n != 101
191
+ mlt2 := bin(nm, ir.OLT, liti(2)) // m < 2
192
+ nandand := typecheck.Expr(logical(nne101, ir.OANDAND, mlt2))
193
+
194
+ // all names used
195
+ wantv := true
196
+ v := ShouldFoldIfNameConstant(nandand, []*ir.Name{nn, nm})
197
+ if v != wantv {
198
+ t.Errorf("wanted shouldfold(%v) %v, got %v", nandand, wantv, v)
199
+ }
200
+
201
+ // not all names used
202
+ wantv = false
203
+ v = ShouldFoldIfNameConstant(nne101, []*ir.Name{nn, nm})
204
+ if v != wantv {
205
+ t.Errorf("wanted shouldfold(%v) %v, got %v", nne101, wantv, v)
206
+ }
207
+
208
+ // other names used.
209
+ np := s.nmi64("p")
210
+ pne0 := bin(np, ir.ONE, liti(101)) // p != 0
211
+ noror := logical(nandand, ir.OOROR, pne0)
212
+ wantv = false
213
+ v = ShouldFoldIfNameConstant(noror, []*ir.Name{nn, nm})
214
+ if v != wantv {
215
+ t.Errorf("wanted shouldfold(%v) %v, got %v", noror, wantv, v)
216
+ }
217
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/trace_off.go ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ //go:build !debugtrace
6
+
7
+ package inlheur
8
+
9
+ const debugTrace = 0
10
+
11
+ func enableDebugTrace(x int) {
12
+ }
13
+
14
+ func enableDebugTraceIfEnv() {
15
+ }
16
+
17
+ func disableDebugTrace() {
18
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/trace_on.go ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ //go:build debugtrace
6
+
7
+ package inlheur
8
+
9
+ import (
10
+ "os"
11
+ "strconv"
12
+ )
13
+
14
+ var debugTrace = 0
15
+
16
+ func enableDebugTrace(x int) {
17
+ debugTrace = x
18
+ }
19
+
20
+ func enableDebugTraceIfEnv() {
21
+ v := os.Getenv("DEBUG_TRACE_INLHEUR")
22
+ if v == "" {
23
+ return
24
+ }
25
+ if v[0] == '*' {
26
+ if !UnitTesting() {
27
+ return
28
+ }
29
+ v = v[1:]
30
+ }
31
+ i, err := strconv.Atoi(v)
32
+ if err != nil {
33
+ return
34
+ }
35
+ debugTrace = i
36
+ }
37
+
38
+ func disableDebugTrace() {
39
+ debugTrace = 0
40
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/inlheur/tserial_test.go ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package inlheur
6
+
7
+ import "testing"
8
+
9
+ func fpeq(fp1, fp2 FuncProps) bool {
10
+ if fp1.Flags != fp2.Flags {
11
+ return false
12
+ }
13
+ if len(fp1.ParamFlags) != len(fp2.ParamFlags) {
14
+ return false
15
+ }
16
+ for i := range fp1.ParamFlags {
17
+ if fp1.ParamFlags[i] != fp2.ParamFlags[i] {
18
+ return false
19
+ }
20
+ }
21
+ if len(fp1.ResultFlags) != len(fp2.ResultFlags) {
22
+ return false
23
+ }
24
+ for i := range fp1.ResultFlags {
25
+ if fp1.ResultFlags[i] != fp2.ResultFlags[i] {
26
+ return false
27
+ }
28
+ }
29
+ return true
30
+ }
31
+
32
+ func TestSerDeser(t *testing.T) {
33
+ testcases := []FuncProps{
34
+ FuncProps{},
35
+ FuncProps{
36
+ Flags: 0xfffff,
37
+ },
38
+ FuncProps{
39
+ Flags: 1,
40
+ ResultFlags: []ResultPropBits{ResultAlwaysSameConstant},
41
+ },
42
+ FuncProps{
43
+ Flags: 1,
44
+ ParamFlags: []ParamPropBits{0x99, 0xaa, 0xfffff},
45
+ ResultFlags: []ResultPropBits{0xfeedface},
46
+ },
47
+ }
48
+
49
+ for k, tc := range testcases {
50
+ s := tc.SerializeToString()
51
+ fp := DeserializeFromString(s)
52
+ got := fp.String()
53
+ want := tc.String()
54
+ if !fpeq(*fp, tc) {
55
+ t.Errorf("eq check failed for test %d: got:\n%s\nwant:\n%s\n", k, got, want)
56
+ }
57
+ }
58
+
59
+ var nilt *FuncProps
60
+ ns := nilt.SerializeToString()
61
+ nfp := DeserializeFromString(ns)
62
+ if len(ns) != 0 || nfp != nil {
63
+ t.Errorf("nil serialize/deserialize failed")
64
+ }
65
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/inline/interleaved/interleaved.go ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ // Package interleaved implements the interleaved devirtualization and
6
+ // inlining pass.
7
+ package interleaved
8
+
9
+ import (
10
+ "cmd/compile/internal/base"
11
+ "cmd/compile/internal/devirtualize"
12
+ "cmd/compile/internal/inline"
13
+ "cmd/compile/internal/inline/inlheur"
14
+ "cmd/compile/internal/ir"
15
+ "cmd/compile/internal/pgo"
16
+ "cmd/compile/internal/typecheck"
17
+ "fmt"
18
+ )
19
+
20
+ // DevirtualizeAndInlinePackage interleaves devirtualization and inlining on
21
+ // all functions within pkg.
22
+ func DevirtualizeAndInlinePackage(pkg *ir.Package, profile *pgo.Profile) {
23
+ if profile != nil && base.Debug.PGODevirtualize > 0 {
24
+ // TODO(mdempsky): Integrate into DevirtualizeAndInlineFunc below.
25
+ ir.VisitFuncsBottomUp(typecheck.Target.Funcs, func(list []*ir.Func, recursive bool) {
26
+ for _, fn := range list {
27
+ devirtualize.ProfileGuided(fn, profile)
28
+ }
29
+ })
30
+ ir.CurFunc = nil
31
+ }
32
+
33
+ if base.Flag.LowerL != 0 {
34
+ inlheur.SetupScoreAdjustments()
35
+ }
36
+
37
+ var inlProfile *pgo.Profile // copy of profile for inlining
38
+ if base.Debug.PGOInline != 0 {
39
+ inlProfile = profile
40
+ }
41
+ if inlProfile != nil {
42
+ inline.PGOInlinePrologue(inlProfile, pkg.Funcs)
43
+ }
44
+
45
+ ir.VisitFuncsBottomUp(pkg.Funcs, func(funcs []*ir.Func, recursive bool) {
46
+ // We visit functions within an SCC in fairly arbitrary order,
47
+ // so by computing inlinability for all functions in the SCC
48
+ // before performing any inlining, the results are less
49
+ // sensitive to the order within the SCC (see #58905 for an
50
+ // example).
51
+
52
+ // First compute inlinability for all functions in the SCC ...
53
+ inline.CanInlineSCC(funcs, recursive, inlProfile)
54
+
55
+ // ... then make a second pass to do devirtualization and inlining
56
+ // of calls.
57
+ for _, fn := range funcs {
58
+ DevirtualizeAndInlineFunc(fn, inlProfile)
59
+ }
60
+ })
61
+
62
+ if base.Flag.LowerL != 0 {
63
+ // Perform a garbage collection of hidden closures functions that
64
+ // are no longer reachable from top-level functions following
65
+ // inlining. See #59404 and #59638 for more context.
66
+ inline.GarbageCollectUnreferencedHiddenClosures()
67
+
68
+ if base.Debug.DumpInlFuncProps != "" {
69
+ inlheur.DumpFuncProps(nil, base.Debug.DumpInlFuncProps)
70
+ }
71
+ if inlheur.Enabled() {
72
+ inline.PostProcessCallSites(inlProfile)
73
+ inlheur.TearDown()
74
+ }
75
+ }
76
+ }
77
+
78
+ // DevirtualizeAndInlineFunc interleaves devirtualization and inlining
79
+ // on a single function.
80
+ func DevirtualizeAndInlineFunc(fn *ir.Func, profile *pgo.Profile) {
81
+ ir.WithFunc(fn, func() {
82
+ if base.Flag.LowerL != 0 {
83
+ if inlheur.Enabled() && !fn.Wrapper() {
84
+ inlheur.ScoreCalls(fn)
85
+ defer inlheur.ScoreCallsCleanup()
86
+ }
87
+ if base.Debug.DumpInlFuncProps != "" && !fn.Wrapper() {
88
+ inlheur.DumpFuncProps(fn, base.Debug.DumpInlFuncProps)
89
+ }
90
+ }
91
+
92
+ bigCaller := base.Flag.LowerL != 0 && inline.IsBigFunc(fn)
93
+ if bigCaller && base.Flag.LowerM > 1 {
94
+ fmt.Printf("%v: function %v considered 'big'; reducing max cost of inlinees\n", ir.Line(fn), fn)
95
+ }
96
+
97
+ // Walk fn's body and apply devirtualization and inlining.
98
+ var inlCalls []*ir.InlinedCallExpr
99
+ var edit func(ir.Node) ir.Node
100
+ edit = func(n ir.Node) ir.Node {
101
+ switch n := n.(type) {
102
+ case *ir.TailCallStmt:
103
+ n.Call.NoInline = true // can't inline yet
104
+ }
105
+
106
+ ir.EditChildren(n, edit)
107
+
108
+ if call, ok := n.(*ir.CallExpr); ok {
109
+ devirtualize.StaticCall(call)
110
+
111
+ if inlCall := inline.TryInlineCall(fn, call, bigCaller, profile); inlCall != nil {
112
+ inlCalls = append(inlCalls, inlCall)
113
+ n = inlCall
114
+ }
115
+ }
116
+
117
+ return n
118
+ }
119
+ ir.EditChildren(fn, edit)
120
+
121
+ // If we inlined any calls, we want to recursively visit their
122
+ // bodies for further devirtualization and inlining. However, we
123
+ // need to wait until *after* the original function body has been
124
+ // expanded, or else inlCallee can have false positives (e.g.,
125
+ // #54632).
126
+ for len(inlCalls) > 0 {
127
+ call := inlCalls[0]
128
+ inlCalls = inlCalls[1:]
129
+ ir.EditChildren(call, edit)
130
+ }
131
+ })
132
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/loopvar/testdata/for_complicated_esc_address.go ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package main
6
+
7
+ import (
8
+ "fmt"
9
+ "os"
10
+ )
11
+
12
+ func main() {
13
+ ss, sa := shared(23)
14
+ ps, pa := private(23)
15
+ es, ea := experiment(23)
16
+
17
+ fmt.Printf("shared s, a; private, s, a; experiment s, a = %d, %d; %d, %d; %d, %d\n", ss, sa, ps, pa, es, ea)
18
+
19
+ if ss != ps || ss != es || ea != pa || sa == pa {
20
+ os.Exit(11)
21
+ } else {
22
+ fmt.Println("PASS")
23
+ }
24
+ }
25
+
26
+ func experiment(x int) (int, int) {
27
+ sum := 0
28
+ var is []*int
29
+ for i := x; i != 1; i = i / 2 {
30
+ for j := 0; j < 10; j++ {
31
+ if i == j { // 10 skips
32
+ continue
33
+ }
34
+ sum++
35
+ }
36
+ i = i*3 + 1
37
+ if i&1 == 0 {
38
+ is = append(is, &i)
39
+ for i&2 == 0 {
40
+ i = i >> 1
41
+ }
42
+ } else {
43
+ i = i + i
44
+ }
45
+ }
46
+
47
+ asum := 0
48
+ for _, pi := range is {
49
+ asum += *pi
50
+ }
51
+
52
+ return sum, asum
53
+ }
54
+
55
+ func private(x int) (int, int) {
56
+ sum := 0
57
+ var is []*int
58
+ I := x
59
+ for ; I != 1; I = I / 2 {
60
+ i := I
61
+ for j := 0; j < 10; j++ {
62
+ if i == j { // 10 skips
63
+ I = i
64
+ continue
65
+ }
66
+ sum++
67
+ }
68
+ i = i*3 + 1
69
+ if i&1 == 0 {
70
+ is = append(is, &i)
71
+ for i&2 == 0 {
72
+ i = i >> 1
73
+ }
74
+ } else {
75
+ i = i + i
76
+ }
77
+ I = i
78
+ }
79
+
80
+ asum := 0
81
+ for _, pi := range is {
82
+ asum += *pi
83
+ }
84
+
85
+ return sum, asum
86
+ }
87
+
88
+ func shared(x int) (int, int) {
89
+ sum := 0
90
+ var is []*int
91
+ i := x
92
+ for ; i != 1; i = i / 2 {
93
+ for j := 0; j < 10; j++ {
94
+ if i == j { // 10 skips
95
+ continue
96
+ }
97
+ sum++
98
+ }
99
+ i = i*3 + 1
100
+ if i&1 == 0 {
101
+ is = append(is, &i)
102
+ for i&2 == 0 {
103
+ i = i >> 1
104
+ }
105
+ } else {
106
+ i = i + i
107
+ }
108
+ }
109
+
110
+ asum := 0
111
+ for _, pi := range is {
112
+ asum += *pi
113
+ }
114
+ return sum, asum
115
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/loopvar/testdata/for_esc_address.go ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package main
6
+
7
+ import (
8
+ "fmt"
9
+ "os"
10
+ )
11
+
12
+ func main() {
13
+ sum := 0
14
+ var is []*int
15
+ for i := 0; i < 10; i++ {
16
+ for j := 0; j < 10; j++ {
17
+ if i == j { // 10 skips
18
+ continue
19
+ }
20
+ sum++
21
+ }
22
+ if i&1 == 0 {
23
+ is = append(is, &i)
24
+ }
25
+ }
26
+
27
+ bug := false
28
+ if sum != 100-10 {
29
+ fmt.Printf("wrong sum, expected %d, saw %d\n", 90, sum)
30
+ bug = true
31
+ }
32
+ sum = 0
33
+ for _, pi := range is {
34
+ sum += *pi
35
+ }
36
+ if sum != 2+4+6+8 {
37
+ fmt.Printf("wrong sum, expected %d, saw %d\n", 20, sum)
38
+ bug = true
39
+ }
40
+ if !bug {
41
+ fmt.Printf("PASS\n")
42
+ } else {
43
+ os.Exit(11)
44
+ }
45
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/loopvar/testdata/for_esc_closure.go ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package main
6
+
7
+ import (
8
+ "fmt"
9
+ "os"
10
+ )
11
+
12
+ var is []func() int
13
+
14
+ func main() {
15
+ sum := 0
16
+ for i := 0; i < 10; i++ {
17
+ for j := 0; j < 10; j++ {
18
+ if i == j { // 10 skips
19
+ continue
20
+ }
21
+ sum++
22
+ }
23
+ if i&1 == 0 {
24
+ is = append(is, func() int {
25
+ if i%17 == 15 {
26
+ i++
27
+ }
28
+ return i
29
+ })
30
+ }
31
+ }
32
+
33
+ bug := false
34
+ if sum != 100-10 {
35
+ fmt.Printf("wrong sum, expected %d, saw %d\n", 90, sum)
36
+ bug = true
37
+ }
38
+ sum = 0
39
+ for _, f := range is {
40
+ sum += f()
41
+ }
42
+ if sum != 2+4+6+8 {
43
+ fmt.Printf("wrong sum, expected %d, saw %d\n", 20, sum)
44
+ bug = true
45
+ }
46
+ if !bug {
47
+ fmt.Printf("PASS\n")
48
+ } else {
49
+ os.Exit(11)
50
+ }
51
+ }
platform/dbops/binaries/go/go/src/cmd/compile/internal/loopvar/testdata/for_esc_method.go ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
5
+ package main
6
+
7
+ import (
8
+ "fmt"
9
+ "os"
10
+ )
11
+
12
+ type I int
13
+
14
+ func (x *I) method() int {
15
+ return int(*x)
16
+ }
17
+
18
+ func main() {
19
+ sum := 0
20
+ var is []func() int
21
+ for i := I(0); int(i) < 10; i++ {
22
+ for j := 0; j < 10; j++ {
23
+ if int(i) == j { // 10 skips
24
+ continue
25
+ }
26
+ sum++
27
+ }
28
+ if i&1 == 0 {
29
+ is = append(is, i.method)
30
+ }
31
+ }
32
+
33
+ bug := false
34
+ if sum != 100-10 {
35
+ fmt.Printf("wrong sum, expected %d, saw %d\n", 90, sum)
36
+ bug = true
37
+ }
38
+ sum = 0
39
+ for _, m := range is {
40
+ sum += m()
41
+ }
42
+ if sum != 2+4+6+8 {
43
+ fmt.Printf("wrong sum, expected %d, saw %d\n", 20, sum)
44
+ bug = true
45
+ }
46
+ if !bug {
47
+ fmt.Printf("PASS\n")
48
+ } else {
49
+ os.Exit(11)
50
+ }
51
+ }