| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from rpython.rtyper.lltypesystem import lltype |
| 5 |
|
| 6 |
|
| 7 |
relevant_ops = ["direct_call", "indirect_call", "malloc", "malloc_varsize"] |
| 8 |
|
| 9 |
def filter_for_ptr(arg): |
| 10 |
return isinstance(arg.concretetype, lltype.Ptr) |
| 11 |
|
| 12 |
def filter_for_nongcptr(arg): |
| 13 |
return isinstance(arg.concretetype, lltype.Ptr) and not arg.concretetype._needsgc() |
| 14 |
|
| 15 |
def relevant_gcvars_block(block, filter=filter_for_ptr): |
| 16 |
result = [] |
| 17 |
def filter_ptr(args): |
| 18 |
return [arg for arg in args if filter(arg)] |
| 19 |
def live_vars_before(index): |
| 20 |
if index == 0: |
| 21 |
return set(filter_ptr(block.inputargs)) |
| 22 |
op = block.operations[index - 1] |
| 23 |
result = live_vars_before(index - 1).union(filter_ptr(op.args + [op.result])) |
| 24 |
return result |
| 25 |
def live_vars_after(index): |
| 26 |
if index == len(block.operations) - 1: |
| 27 |
result = set() |
| 28 |
for exit in block.exits: |
| 29 |
result = result.union(filter_ptr(exit.args)) |
| 30 |
return result |
| 31 |
op = block.operations[index + 1] |
| 32 |
result = live_vars_after(index + 1).union(filter_ptr(op.args + [op.result])) |
| 33 |
return result |
| 34 |
for i, op in enumerate(block.operations): |
| 35 |
if op.opname not in relevant_ops: |
| 36 |
continue |
| 37 |
live_before = live_vars_before(i) |
| 38 |
live_after = live_vars_after(i) |
| 39 |
result.append(len(live_before.intersection(live_after))) |
| 40 |
return result |
| 41 |
|
| 42 |
def relevant_gcvars_graph(graph, filter=filter_for_ptr): |
| 43 |
result = [] |
| 44 |
for block in graph.iterblocks(): |
| 45 |
result += relevant_gcvars_block(block, filter) |
| 46 |
return result |
| 47 |
|
| 48 |
def relevant_gcvars(t, filter=filter_for_ptr): |
| 49 |
result = [] |
| 50 |
for graph in t.graphs: |
| 51 |
result.extend(relevant_gcvars_graph(graph, filter)) |
| 52 |
return result |
| 53 |
|