-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathssa.go
152 lines (128 loc) · 2.84 KB
/
ssa.go
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package analysisutil
import (
"golang.org/x/tools/go/ssa"
)
// IfInstr returns *ssa.If which is contained in the block b.
// If the block b has not any if instruction, IfInstr returns nil.
func IfInstr(b *ssa.BasicBlock) *ssa.If {
if len(b.Instrs) == 0 {
return nil
}
ifinstr, ok := b.Instrs[len(b.Instrs)-1].(*ssa.If)
if !ok {
return nil
}
return ifinstr
}
// Phi returns phi values which are contained in the block b.
func Phi(b *ssa.BasicBlock) []*ssa.Phi {
var phis []*ssa.Phi
for _, instr := range b.Instrs {
if phi, ok := instr.(*ssa.Phi); ok {
phis = append(phis, phi)
} else {
// no more phi
break
}
}
return phis
}
// Returns returns a slice of *ssa.Return in the function.
func Returns(v ssa.Value) []*ssa.Return {
var fn *ssa.Function
switch v := v.(type) {
case *ssa.Function:
fn = v
case *ssa.MakeClosure:
return Returns(v.Fn)
default:
return nil
}
var rets []*ssa.Return
done := map[*ssa.BasicBlock]bool{}
for _, b := range fn.Blocks {
rets = append(rets, returnsInBlock(b, done)...)
}
return rets
}
func returnsInBlock(b *ssa.BasicBlock, done map[*ssa.BasicBlock]bool) (rets []*ssa.Return) {
if done[b] {
return nil
}
done[b] = true
if b.Index != 0 && len(b.Preds) == 0 {
return nil
}
if len(b.Instrs) != 0 {
switch instr := b.Instrs[len(b.Instrs)-1].(type) {
case *ssa.Return:
rets = append(rets, instr)
}
}
for _, s := range b.Succs {
rets = append(rets, returnsInBlock(s, done)...)
}
return rets
}
// BinOp returns binary operator values which are contained in the block b.
func BinOp(b *ssa.BasicBlock) []*ssa.BinOp {
var binops []*ssa.BinOp
for _, instr := range b.Instrs {
if binop, ok := instr.(*ssa.BinOp); ok {
binops = append(binops, binop)
}
}
return binops
}
// Used returns an instruction which uses the value in the instructions.
func Used(v ssa.Value, instrs []ssa.Instruction) ssa.Instruction {
if len(instrs) == 0 || v.Referrers() == nil {
return nil
}
for _, instr := range instrs {
if used := usedInInstr(v, instr); used != nil {
return used
}
}
return nil
}
func usedInInstr(v ssa.Value, instr ssa.Instruction) ssa.Instruction {
switch instr := instr.(type) {
case *ssa.MakeClosure:
return usedInClosure(v, instr)
default:
operands := instr.Operands(nil)
for _, x := range operands {
if x != nil && *x == v {
return instr
}
}
}
switch v := v.(type) {
case *ssa.UnOp:
return usedInInstr(v.X, instr)
}
return nil
}
func usedInClosure(v ssa.Value, instr *ssa.MakeClosure) ssa.Instruction {
fn, _ := instr.Fn.(*ssa.Function)
if fn == nil {
return nil
}
var fv *ssa.FreeVar
for i := range instr.Bindings {
if instr.Bindings[i] == v {
fv = fn.FreeVars[i]
break
}
}
if fv == nil {
return nil
}
for _, b := range fn.Blocks {
if used := Used(fv, b.Instrs); used != nil {
return used
}
}
return nil
}