| 1 |
package irutil |
| 2 |
|
| 3 |
import ( |
| 4 |
"go/types" |
| 5 |
"slices" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"honnef.co/go/tools/go/ir" |
| 9 |
"honnef.co/go/tools/go/types/typeutil" |
| 10 |
) |
| 11 |
|
| 12 |
func Reachable(from, to *ir.BasicBlock) bool { |
| 13 |
if from == to { |
| 14 |
return true |
| 15 |
} |
| 16 |
if from.Dominates(to) { |
| 17 |
return true |
| 18 |
} |
| 19 |
|
| 20 |
found := false |
| 21 |
Walk(from, func(b *ir.BasicBlock) bool { |
| 22 |
if b == to { |
| 23 |
found = true |
| 24 |
return false |
| 25 |
} |
| 26 |
return true |
| 27 |
}) |
| 28 |
return found |
| 29 |
} |
| 30 |
|
| 31 |
func Walk(b *ir.BasicBlock, fn func(*ir.BasicBlock) bool) { |
| 32 |
seen := map[*ir.BasicBlock]bool{} |
| 33 |
wl := []*ir.BasicBlock{b} |
| 34 |
for len(wl) > 0 { |
| 35 |
b := wl[len(wl)-1] |
| 36 |
wl = wl[:len(wl)-1] |
| 37 |
if seen[b] { |
| 38 |
continue |
| 39 |
} |
| 40 |
seen[b] = true |
| 41 |
if !fn(b) { |
| 42 |
continue |
| 43 |
} |
| 44 |
wl = append(wl, b.Succs...) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func Vararg(x *ir.Slice) ([]ir.Value, bool) { |
| 49 |
var out []ir.Value |
| 50 |
alloc, ok := ir.Unwrap(x.X).(*ir.Alloc) |
| 51 |
if !ok { |
| 52 |
return nil, false |
| 53 |
} |
| 54 |
var checkAlloc func(alloc ir.Value) bool |
| 55 |
checkAlloc = func(alloc ir.Value) bool { |
| 56 |
for _, ref := range *alloc.Referrers() { |
| 57 |
if ref == x { |
| 58 |
continue |
| 59 |
} |
| 60 |
if ref.Block() != x.Block() { |
| 61 |
return false |
| 62 |
} |
| 63 |
switch ref := ref.(type) { |
| 64 |
case *ir.IndexAddr: |
| 65 |
idx := ref |
| 66 |
if len(*idx.Referrers()) != 1 { |
| 67 |
return false |
| 68 |
} |
| 69 |
store, ok := (*idx.Referrers())[0].(*ir.Store) |
| 70 |
if !ok { |
| 71 |
return false |
| 72 |
} |
| 73 |
out = append(out, store.Val) |
| 74 |
case *ir.Copy: |
| 75 |
if !checkAlloc(ref) { |
| 76 |
return false |
| 77 |
} |
| 78 |
default: |
| 79 |
return false |
| 80 |
} |
| 81 |
} |
| 82 |
return true |
| 83 |
} |
| 84 |
if !checkAlloc(alloc) { |
| 85 |
return nil, false |
| 86 |
} |
| 87 |
return out, true |
| 88 |
} |
| 89 |
|
| 90 |
func CallName(call *ir.CallCommon) string { |
| 91 |
if call.IsInvoke() { |
| 92 |
return "" |
| 93 |
} |
| 94 |
switch v := call.Value.(type) { |
| 95 |
case *ir.Function: |
| 96 |
fn, ok := v.Object().(*types.Func) |
| 97 |
if !ok { |
| 98 |
return "" |
| 99 |
} |
| 100 |
return typeutil.FuncName(fn) |
| 101 |
case *ir.Builtin: |
| 102 |
return v.Name() |
| 103 |
} |
| 104 |
return "" |
| 105 |
} |
| 106 |
|
| 107 |
func IsCallTo(call *ir.CallCommon, name string) bool { return CallName(call) == name } |
| 108 |
|
| 109 |
func IsCallToAny(call *ir.CallCommon, names ...string) bool { |
| 110 |
q := CallName(call) |
| 111 |
return slices.Contains(names, q) |
| 112 |
} |
| 113 |
|
| 114 |
func FilterDebug(instr []ir.Instruction) []ir.Instruction { |
| 115 |
var out []ir.Instruction |
| 116 |
for _, ins := range instr { |
| 117 |
if _, ok := ins.(*ir.DebugRef); !ok { |
| 118 |
out = append(out, ins) |
| 119 |
} |
| 120 |
} |
| 121 |
return out |
| 122 |
} |
| 123 |
|
| 124 |
func IsExample(fn *ir.Function) bool { |
| 125 |
if !strings.HasPrefix(fn.Name(), "Example") { |
| 126 |
return false |
| 127 |
} |
| 128 |
f := fn.Prog.Fset.File(fn.Pos()) |
| 129 |
if f == nil { |
| 130 |
return false |
| 131 |
} |
| 132 |
return strings.HasSuffix(f.Name(), "_test.go") |
| 133 |
} |
| 134 |
|
| 135 |
// Flatten recursively returns the underlying value of an ir.Sigma or |
| 136 |
// ir.Phi node. If all edges in an ir.Phi node are the same (after |
| 137 |
// flattening), the flattened edge will get returned. If flattening is |
| 138 |
// not possible, nil is returned. |
| 139 |
func Flatten(v ir.Value) ir.Value { |
| 140 |
failed := false |
| 141 |
seen := map[ir.Value]struct{}{} |
| 142 |
var out ir.Value |
| 143 |
var dfs func(v ir.Value) |
| 144 |
dfs = func(v ir.Value) { |
| 145 |
if failed { |
| 146 |
return |
| 147 |
} |
| 148 |
if _, ok := seen[v]; ok { |
| 149 |
return |
| 150 |
} |
| 151 |
seen[v] = struct{}{} |
| 152 |
|
| 153 |
switch v := v.(type) { |
| 154 |
case *ir.Sigma: |
| 155 |
dfs(v.X) |
| 156 |
case *ir.Phi: |
| 157 |
for _, e := range v.Edges { |
| 158 |
dfs(e) |
| 159 |
} |
| 160 |
default: |
| 161 |
if out == nil { |
| 162 |
out = v |
| 163 |
} else if out != v { |
| 164 |
failed = true |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
dfs(v) |
| 169 |
|
| 170 |
if failed { |
| 171 |
return nil |
| 172 |
} |
| 173 |
return out |
| 174 |
} |