| 1 |
from rpython.flowspace.model import Variable, mkentrymap |
| 2 |
from rpython.tool.algo.unionfind import UnionFind |
| 3 |
|
| 4 |
class DataFlowFamilyBuilder: |
| 5 |
"""Follow the flow of the data in the graph. Builds a UnionFind grouping |
| 6 |
all the variables by families: each family contains exactly one variable |
| 7 |
where a value is stored into -- either by an operation or a merge -- and |
| 8 |
all following variables where the value is just passed unmerged into the |
| 9 |
next block. |
| 10 |
""" |
| 11 |
|
| 12 |
def __init__(self, graph): |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
opportunities = [] |
| 18 |
opportunities_with_const = [] |
| 19 |
entrymap = mkentrymap(graph) |
| 20 |
del entrymap[graph.startblock] |
| 21 |
for block, links in entrymap.items(): |
| 22 |
assert links |
| 23 |
for n, inputvar in enumerate(block.inputargs): |
| 24 |
vars = [block, inputvar] |
| 25 |
put_in = opportunities |
| 26 |
for link in links: |
| 27 |
var = link.args[n] |
| 28 |
if not isinstance(var, Variable): |
| 29 |
put_in = opportunities_with_const |
| 30 |
vars.append(var) |
| 31 |
|
| 32 |
|
| 33 |
put_in.append(vars) |
| 34 |
self.opportunities = opportunities |
| 35 |
self.opportunities_with_const = opportunities_with_const |
| 36 |
self.variable_families = UnionFind() |
| 37 |
|
| 38 |
def complete(self): |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
variable_families = self.variable_families |
| 45 |
any_progress_at_all = False |
| 46 |
progress = True |
| 47 |
while progress: |
| 48 |
progress = False |
| 49 |
pending_opportunities = [] |
| 50 |
for vars in self.opportunities: |
| 51 |
repvars = [variable_families.find_rep(v1) for v1 in vars[1:]] |
| 52 |
repvars_without_duplicates = dict.fromkeys(repvars) |
| 53 |
count = len(repvars_without_duplicates) |
| 54 |
if count > 2: |
| 55 |
|
| 56 |
pending_opportunities.append(vars[:1] + repvars) |
| 57 |
elif count == 2: |
| 58 |
|
| 59 |
variable_families.union(*repvars_without_duplicates) |
| 60 |
progress = True |
| 61 |
self.opportunities = pending_opportunities |
| 62 |
any_progress_at_all |= progress |
| 63 |
return any_progress_at_all |
| 64 |
|
| 65 |
def merge_identical_phi_nodes(self): |
| 66 |
variable_families = self.variable_families |
| 67 |
any_progress_at_all = False |
| 68 |
progress = True |
| 69 |
while progress: |
| 70 |
progress = False |
| 71 |
block_phi_nodes = {} |
| 72 |
for vars in self.opportunities + self.opportunities_with_const: |
| 73 |
block, blockvar = vars[:2] |
| 74 |
linksvars = vars[2:] |
| 75 |
linksvars = [variable_families.find_rep(v) for v in linksvars] |
| 76 |
phi_node = (block,) + tuple(linksvars) |
| 77 |
if phi_node in block_phi_nodes: |
| 78 |
|
| 79 |
|
| 80 |
blockvar1 = block_phi_nodes[phi_node] |
| 81 |
if variable_families.union(blockvar1, blockvar)[0]: |
| 82 |
progress = True |
| 83 |
else: |
| 84 |
block_phi_nodes[phi_node] = blockvar |
| 85 |
any_progress_at_all |= progress |
| 86 |
return any_progress_at_all |
| 87 |
|
| 88 |
def get_variable_families(self): |
| 89 |
self.complete() |
| 90 |
return self.variable_families |
| 91 |
|
| 92 |
|
| 93 |
def SSI_to_SSA(graph): |
| 94 |
"""Rename the variables in a flow graph as much as possible without |
| 95 |
violating the SSA rule. 'SSI' means that each Variable in a flow graph is |
| 96 |
defined only once in the whole graph; all our graphs are SSI. This |
| 97 |
function does not break that rule, but changes the 'name' of some |
| 98 |
Variables to give them the same 'name' as other Variables. The result |
| 99 |
looks like an SSA graph. 'SSA' means that each var name appears as the |
| 100 |
result of an operation only once in the whole graph, but it can be |
| 101 |
passed to other blocks across links. |
| 102 |
""" |
| 103 |
variable_families = DataFlowFamilyBuilder(graph).get_variable_families() |
| 104 |
|
| 105 |
for v in variable_families.keys(): |
| 106 |
v1 = variable_families.find_rep(v) |
| 107 |
if v1 != v: |
| 108 |
v.set_name_from(v1) |
| 109 |
|
| 110 |
|
| 111 |
variables_by_name = {} |
| 112 |
for block in graph.iterblocks(): |
| 113 |
vars = [op.result for op in block.operations] |
| 114 |
for link in block.exits: |
| 115 |
vars += link.getextravars() |
| 116 |
assert len(dict.fromkeys([v.name for v in vars])) == len(vars), ( |
| 117 |
"duplicate variable name in %r" % (block,)) |
| 118 |
for v in vars: |
| 119 |
variables_by_name.setdefault(v.name, []).append(v) |
| 120 |
|
| 121 |
for vname, vlist in variables_by_name.items(): |
| 122 |
vct = [getattr(v, 'concretetype', None) for v in vlist] |
| 123 |
assert vct == vct[:1] * len(vct), ( |
| 124 |
"variables called %s have mixed concretetypes: %r" % (vname, vct)) |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
def variables_created_in(block): |
| 129 |
result = set(block.inputargs) |
| 130 |
for op in block.operations: |
| 131 |
result.add(op.result) |
| 132 |
return result |
| 133 |
|
| 134 |
|
| 135 |
def SSA_to_SSI(graph, annotator=None): |
| 136 |
"""Turn a number of blocks belonging to a flow graph into valid (i.e. SSI) |
| 137 |
form, assuming that they are only in SSA form (i.e. they can use each |
| 138 |
other's variables directly, without having to pass and rename them along |
| 139 |
links). |
| 140 |
""" |
| 141 |
entrymap = mkentrymap(graph) |
| 142 |
del entrymap[graph.startblock] |
| 143 |
builder = DataFlowFamilyBuilder(graph) |
| 144 |
variable_families = builder.get_variable_families() |
| 145 |
del builder |
| 146 |
|
| 147 |
pending = [] |
| 148 |
for block in graph.iterblocks(): |
| 149 |
if block not in entrymap: |
| 150 |
continue |
| 151 |
variables_created = variables_created_in(block) |
| 152 |
seen = set(variables_created) |
| 153 |
variables_used = [] |
| 154 |
def record_used_var(v): |
| 155 |
if v not in seen: |
| 156 |
variables_used.append(v) |
| 157 |
seen.add(v) |
| 158 |
for op in block.operations: |
| 159 |
for arg in op.args: |
| 160 |
record_used_var(arg) |
| 161 |
record_used_var(block.exitswitch) |
| 162 |
for link in block.exits: |
| 163 |
for arg in link.args: |
| 164 |
record_used_var(arg) |
| 165 |
|
| 166 |
for v in variables_used: |
| 167 |
if (isinstance(v, Variable) and |
| 168 |
v._name not in ('last_exception_', 'last_exc_value_')): |
| 169 |
pending.append((block, v)) |
| 170 |
|
| 171 |
while pending: |
| 172 |
block, v = pending.pop() |
| 173 |
v_rep = variable_families.find_rep(v) |
| 174 |
variables_created = variables_created_in(block) |
| 175 |
if v in variables_created: |
| 176 |
continue |
| 177 |
for w in variables_created: |
| 178 |
w_rep = variable_families.find_rep(w) |
| 179 |
if v_rep is w_rep: |
| 180 |
|
| 181 |
block.renamevariables({v: w}) |
| 182 |
break |
| 183 |
else: |
| 184 |
|
| 185 |
try: |
| 186 |
links = entrymap[block] |
| 187 |
except KeyError: |
| 188 |
raise Exception("SSA_to_SSI failed: no way to give a value to" |
| 189 |
" %r in %r" % (v, block)) |
| 190 |
w = v.copy() |
| 191 |
variable_families.union(v, w) |
| 192 |
block.renamevariables({v: w}) |
| 193 |
block.inputargs.append(w) |
| 194 |
for link in links: |
| 195 |
link.args.append(v) |
| 196 |
pending.append((link.prevblock, v)) |