Debian Codegraph @

rpython/translator/backendopt/innerloop.py

raw symbols SCIP
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 # list of Links making the cycle,
14 # starting from one of the exits of headblock
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 # Heuristic (thanks Stakkars for the idea):
22 # to find the "best" cycle to pick,
23 #
24 # * look for variables that don't change over the whole cycle
25 #
26 # * cycles with _more_ of them are _inside_ cycles with less of them,
27 # because any variable that doesn't change over an outer loop will
28 # not change over an inner loop either, and on the other hand the
29 # outer loop is likely to use and modify variables that remain
30 # constant over the inner loop.
31 #
32 # * if the numbers are the same, fall back to a more arbitrary
33 # measure: loops involving less blocks may be more important
34 # to optimize
35 #
36 # * in a cycle, which block is the head of the loop? Somewhat
37 # arbitrarily we pick the first Bool-switching block that has
38 # two exits. The "first" means the one closest to the
39 # startblock of the graph.
40 #
41 startdistance = {} # {block: distance-from-startblock}
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 # find the headblock
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 # count the variables that remain constant across the cycle,
81 # detected as having its SSA family present across all blocks.
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 # ok, found in this block
95 else:
96 break # not found in this block, fail
97 else:
98 # found in all blocks, this variable is a loop constant
99 num_loop_constants += 1
100
101 # smaller keys are "better"
102 key = (-num_loop_constants, # maximize num_loop_constants
103 len(cycle)) # minimize len(cycle)
104 loops.append((key, loop))
105
106 loops.sort()
107
108 # returns 'loops' without overlapping blocks
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 # overlapping
115 else:
116 # non-overlapping
117 result.append(loop)
118 for link in loop.links:
119 blocks_seen[link.target] = True
120 return result