| 1 |
""" |
| 2 |
This is optional support code for backends: it finds which cycles |
| 3 |
in a graph are likely to correspond to source-level 'inner loops'. |
| 4 |
""" |
| 5 |
from rpython.tool.algo import graphlib |
| 6 |
from rpython.flowspace.model import Variable |
| 7 |
from rpython.translator.backendopt.ssa import DataFlowFamilyBuilder |
| 8 |
|
| 9 |
|
| 10 |
class Loop: |
| 11 |
def __init__(self, headblock, links): |
| 12 |
self.headblock = headblock |
| 13 |
self.links = links |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
def find_inner_loops(graph, check_exitswitch_type=None): |
| 18 |
"""Enumerate what look like the innermost loops of the graph. |
| 19 |
Returns a list of non-overlapping Loop() instances. |
| 20 |
""" |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
startdistance = {} |
| 42 |
pending = [graph.startblock] |
| 43 |
edge_list = [] |
| 44 |
dist = 0 |
| 45 |
while pending: |
| 46 |
newblocks = [] |
| 47 |
for block in pending: |
| 48 |
if block not in startdistance: |
| 49 |
startdistance[block] = dist |
| 50 |
for link in block.exits: |
| 51 |
newblocks.append(link.target) |
| 52 |
edge = graphlib.Edge(block, link.target) |
| 53 |
edge.link = link |
| 54 |
edge_list.append(edge) |
| 55 |
dist += 1 |
| 56 |
pending = newblocks |
| 57 |
|
| 58 |
vertices = startdistance |
| 59 |
edges = graphlib.make_edge_dict(edge_list) |
| 60 |
cycles = graphlib.all_cycles(graph.startblock, vertices, edges) |
| 61 |
loops = [] |
| 62 |
variable_families = None |
| 63 |
|
| 64 |
for cycle in cycles: |
| 65 |
|
| 66 |
candidates = [] |
| 67 |
for i in range(len(cycle)): |
| 68 |
block = cycle[i].source |
| 69 |
v = block.exitswitch |
| 70 |
if isinstance(v, Variable) and len(block.exits) == 2: |
| 71 |
if getattr(v, 'concretetype', None) is check_exitswitch_type: |
| 72 |
dist = startdistance[block] |
| 73 |
candidates.append((dist, i)) |
| 74 |
if not candidates: |
| 75 |
continue |
| 76 |
_, i = min(candidates) |
| 77 |
links = [edge.link for edge in cycle[i:] + cycle[:i]] |
| 78 |
loop = Loop(cycle[i].source, links) |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
if variable_families is None: |
| 83 |
dffb = DataFlowFamilyBuilder(graph) |
| 84 |
variable_families = dffb.get_variable_families() |
| 85 |
|
| 86 |
num_loop_constants = 0 |
| 87 |
for v in loop.headblock.inputargs: |
| 88 |
v = variable_families.find_rep(v) |
| 89 |
for link in loop.links: |
| 90 |
block1 = link.target |
| 91 |
for v1 in block1.inputargs: |
| 92 |
v1 = variable_families.find_rep(v1) |
| 93 |
if v1 is v: |
| 94 |
break |
| 95 |
else: |
| 96 |
break |
| 97 |
else: |
| 98 |
|
| 99 |
num_loop_constants += 1 |
| 100 |
|
| 101 |
|
| 102 |
key = (-num_loop_constants, |
| 103 |
len(cycle)) |
| 104 |
loops.append((key, loop)) |
| 105 |
|
| 106 |
loops.sort() |
| 107 |
|
| 108 |
|
| 109 |
result = [] |
| 110 |
blocks_seen = {} |
| 111 |
for key, loop in loops: |
| 112 |
for link in loop.links: |
| 113 |
if link.target in blocks_seen: |
| 114 |
break |
| 115 |
else: |
| 116 |
|
| 117 |
result.append(loop) |
| 118 |
for link in loop.links: |
| 119 |
blocks_seen[link.target] = True |
| 120 |
return result |