forked from sampsyo/bril
-
Notifications
You must be signed in to change notification settings - Fork 0
/
df.py
169 lines (138 loc) · 4.29 KB
/
df.py
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import sys
import json
from collections import namedtuple
from form_blocks import form_blocks
import cfg
# A single dataflow analysis consists of these part:
# - forward: True for forward, False for backward.
# - init: An initial value (bottom or top of the latice).
# - merge: Take a list of values and produce a single value.
# - transfer: The transfer function.
Analysis = namedtuple('Analysis', ['forward', 'init', 'merge', 'transfer'])
def union(sets):
out = set()
for s in sets:
out.update(s)
return out
def df_worklist(blocks, analysis):
"""The worklist algorithm for iterating a data flow analysis to a
fixed point.
"""
preds, succs = cfg.edges(blocks)
# Switch between directions.
if analysis.forward:
first_block = list(blocks.keys())[0] # Entry.
in_edges = preds
out_edges = succs
else:
first_block = list(blocks.keys())[-1] # Exit.
in_edges = succs
out_edges = preds
# Initialize.
in_ = {first_block: analysis.init}
out = {node: analysis.init for node in blocks}
# Iterate.
worklist = list(blocks.keys())
while worklist:
node = worklist.pop(0)
inval = analysis.merge(out[n] for n in in_edges[node])
in_[node] = inval
outval = analysis.transfer(blocks[node], inval)
if outval != out[node]:
out[node] = outval
worklist += out_edges[node]
if analysis.forward:
return in_, out
else:
return out, in_
def fmt(val):
"""Guess a good way to format a data flow value. (Works for sets and
dicts, at least.)
"""
if isinstance(val, set):
if val:
return ', '.join(v for v in sorted(val))
else:
return '∅'
elif isinstance(val, dict):
if val:
return ', '.join('{}: {}'.format(k, v)
for k, v in sorted(val.items()))
else:
return '∅'
else:
return str(val)
def run_df(bril, analysis):
for func in bril['functions']:
# Form the CFG.
blocks = cfg.block_map(form_blocks(func['instrs']))
cfg.add_terminators(blocks)
in_, out = df_worklist(blocks, analysis)
for block in blocks:
print('{}:'.format(block))
print(' in: ', fmt(in_[block]))
print(' out:', fmt(out[block]))
def gen(block):
"""Variables that are written in the block.
"""
return {i['dest'] for i in block if 'dest' in i}
def use(block):
"""Variables that are read before they are written in the block.
"""
defined = set() # Locally defined.
used = set()
for i in block:
used.update(v for v in i.get('args', []) if v not in defined)
if 'dest' in i:
defined.add(i['dest'])
return used
def cprop_transfer(block, in_vals):
out_vals = dict(in_vals)
for instr in block:
if 'dest' in instr:
if instr['op'] == 'const':
out_vals[instr['dest']] = instr['value']
else:
out_vals[instr['dest']] = '?'
return out_vals
def cprop_merge(vals_list):
out_vals = {}
for vals in vals_list:
for name, val in vals.items():
if val == '?':
out_vals[name] = '?'
else:
if name in out_vals:
if out_vals[name] != val:
out_vals[name] = '?'
else:
out_vals[name] = val
return out_vals
ANALYSES = {
# A really really basic analysis that just accumulates all the
# currently-defined variables.
'defined': Analysis(
True,
init=set(),
merge=union,
transfer=lambda block, in_: in_.union(gen(block)),
),
# Live variable analysis: the variables that are both defined at a
# given point and might be read along some path in the future.
'live': Analysis(
False,
init=set(),
merge=union,
transfer=lambda block, out: use(block).union(out - gen(block)),
),
# A simple constant propagation pass.
'cprop': Analysis(
True,
init={},
merge=cprop_merge,
transfer=cprop_transfer,
),
}
if __name__ == '__main__':
bril = json.load(sys.stdin)
run_df(bril, ANALYSES[sys.argv[1]])