Repository navigation
Expand file tree
/
Copy pathstub.go
More file actions
97 lines (90 loc) · 2.75 KB
/
Copy pathstub.go
File metadata and controls
97 lines (90 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package irutil
import (
"honnef.co/go/tools/go/ir"
)
// IsStub reports whether a function is a stub. A function is
// considered a stub if it has no instructions or if all it does is
// return a fixed value (either an actual constant or a new allocation).
func IsStub(fn *ir.Function) bool {
if fn.Source() == nil {
// External functions have to be assumed to not be stubs.
return false
}
for _, b := range fn.Blocks {
for _, instr := range b.Instrs {
switch instr.(type) {
case *ir.Panic:
// panic is a stub if it only uses constants
case *ir.Return:
// return is a stub if it only uses constants
case *ir.Jump:
// if there are no disallowed instructions, then we're
// only jumping to the exit block (or possibly
// somewhere else that's stubby?)
case *ir.MakeInterface:
// this can only be wrapping Const and HeapAlloc, which is fine.
case *ir.Alloc:
// allocating is fine as long as all we do with the allocation
// is return it (possibly in an interface value)
default:
// all other instructions are assumed to do actual work
return false
}
}
}
return true
}
// IsTrivial reports whether a function is trivial. A function is
// considered trivial if it is a stub, only returns a global variable, or calls
// another trivial function.
func IsTrivial(fn *ir.Function) bool {
return isTrivial(fn, nil)
}
func isTrivial(fn *ir.Function, seen map[*ir.Function]struct{}) bool {
if fn == nil || fn.Source() == nil {
// External functions have to be assumed to be nontrivial.
return false
}
if _, ok := seen[fn]; ok {
// Mutual recursion is not trivial
return false
}
// We delay adding to seen until a call, to avoid creating garbage in the
// common case.
for _, b := range fn.Blocks {
for _, instr := range b.Instrs {
switch instr := instr.(type) {
case *ir.Panic:
// panic is a stub if it only uses constants
case *ir.Return:
// return is a stub if it only uses constants
case *ir.Jump:
// if there are no disallowed instructions, then we're
// only jumping to the exit block (or possibly
// somewhere else that's stubby?)
case *ir.MakeInterface:
// this can only be wrapping allowed instructions
case *ir.Alloc:
// allocating is fine as long as all we do with the allocation
// is return it (possibly in an interface value)
case *ir.Load:
if _, ok := instr.X.(*ir.Global); !ok {
return false
}
case *ir.Call:
if seen == nil {
seen = make(map[*ir.Function]struct{})
}
seen[fn] = struct{}{}
callee := instr.Call.StaticCallee()
if callee == nil || !isTrivial(callee, seen) {
return false
}
default:
// all other instructions are assumed to do actual work
return false
}
}
}
return true
}