-
Notifications
You must be signed in to change notification settings - Fork 189
/
to_ssa.py
182 lines (146 loc) · 5.86 KB
/
to_ssa.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
170
171
172
173
174
175
176
177
178
179
180
181
182
import json
import sys
from collections import defaultdict
from cfg import block_map, successors, add_terminators, add_entry, reassemble
from form_blocks import form_blocks
from dom import get_dom, dom_fronts, dom_tree, map_inv
def def_blocks(blocks):
"""Get a map from variable names to defining blocks.
"""
out = defaultdict(set)
for name, block in blocks.items():
for instr in block:
if 'dest' in instr:
out[instr['dest']].add(name)
return dict(out)
def get_phis(blocks, df, defs):
"""Find where to insert phi-nodes in the blocks.
Produce a map from block names to variable names that need phi-nodes
in those blocks. (We will need to generate names and actually insert
instructions later.)
"""
phis = {b: set() for b in blocks}
for v, v_defs in defs.items():
v_defs_list = list(v_defs)
for d in v_defs_list:
for block in df[d]:
# Add a phi-node...
if v not in phis[block]:
# ..unless we already did.
phis[block].add(v)
if block not in v_defs_list:
v_defs_list.append(block)
return phis
def ssa_rename(blocks, phis, succ, domtree, args):
stack = defaultdict(list, {v: [v] for v in args})
phi_args = {b: {p: [] for p in phis[b]} for b in blocks}
phi_dests = {b: {p: None for p in phis[b]} for b in blocks}
counters = defaultdict(int)
def _push_fresh(var):
fresh = '{}.{}'.format(var, counters[var])
counters[var] += 1
stack[var].insert(0, fresh)
return fresh
def _rename(block):
# Save stacks.
old_stack = {k: list(v) for k, v in stack.items()}
# Rename phi-node destinations.
for p in phis[block]:
phi_dests[block][p] = _push_fresh(p)
for instr in blocks[block]:
# Rename arguments in normal instructions.
if 'args' in instr:
new_args = [stack[arg][0] for arg in instr['args']]
instr['args'] = new_args
# Rename destinations.
if 'dest' in instr:
instr['dest'] = _push_fresh(instr['dest'])
# Rename phi-node arguments (in successors).
for s in succ[block]:
for p in phis[s]:
if stack[p]:
phi_args[s][p].append((block, stack[p][0]))
# Recursive calls.
for b in sorted(domtree[block]):
_rename(b)
# Restore stacks.
stack.update(old_stack)
entry = list(blocks.keys())[0]
_rename(entry)
return phi_args, phi_dests
def prune_phis(pred, phi_args, phi_dests):
"""Prune possibly-undefined phi-nodes.
Ordinary phi insertion will create phi-nodes that are "partially
undefined" because they represent a convergence of paths where the
variable is defined along some but not all paths. These phi-nodes
are useless because it is illegal to read from the result. And they
can confuse the out-of-SSA pass because it creates nonsensical
copies. This algorithm iteratively eliminates such phi-nodes,
propagating through to eliminate consumer phi-nodes until
convergence.
"""
# We build up a set of new names (phi destinations) to prune, and we
# iterate until this set stops growing.
old_prune_len = -1
prune = set()
while len(prune) != old_prune_len:
old_prune_len = len(prune)
# Look at each phi.
for block, args in phi_args.items():
dests = phi_dests[block]
for v, v_args in args.items():
# How many non-pruned arguments does this phi have?
live_args = [a for _, a in v_args if a not in prune]
if len(live_args) < len(pred[block]):
# Prune phis with insufficient arguments.
prune.add(dests[v])
# Actually delete all phis with pruned destinations.
for block, args in phi_args.items():
dests = phi_dests[block]
to_del = {v for v, d in dests.items() if d in prune}
for v in to_del:
del args[v]
del dests[v]
def insert_phis(blocks, phi_args, phi_dests, types):
for block, instrs in blocks.items():
for dest, pairs in sorted(phi_args[block].items()):
phi = {
'op': 'phi',
'dest': phi_dests[block][dest],
'type': types[dest],
'labels': [p[0] for p in pairs],
'args': [p[1] for p in pairs],
}
instrs.insert(0, phi)
def get_types(func):
# Silly way to get the type of variables. (According to the Bril
# spec, well-formed programs must use only a single type for every
# variable within a given function.)
types = {arg['name']: arg['type'] for arg in func.get('args', [])}
for instr in func['instrs']:
if 'dest' in instr:
types[instr['dest']] = instr['type']
return types
def func_to_ssa(func):
blocks = block_map(form_blocks(func['instrs']))
add_entry(blocks)
add_terminators(blocks)
succ = {name: successors(block[-1]) for name, block in blocks.items()}
pred = map_inv(succ)
dom = get_dom(succ, list(blocks.keys())[0])
df = dom_fronts(dom, succ)
defs = def_blocks(blocks)
types = get_types(func)
arg_names = {a['name'] for a in func['args']} if 'args' in func else set()
phis = get_phis(blocks, df, defs)
phi_args, phi_dests = ssa_rename(blocks, phis, succ, dom_tree(dom),
arg_names)
prune_phis(pred, phi_args, phi_dests)
insert_phis(blocks, phi_args, phi_dests, types)
func['instrs'] = reassemble(blocks)
def to_ssa(bril):
for func in bril['functions']:
func_to_ssa(func)
return bril
if __name__ == '__main__':
print(json.dumps(to_ssa(json.load(sys.stdin)), indent=2, sort_keys=True))