| 1 |
from __future__ import print_function |
| 2 |
|
| 3 |
import sys |
| 4 |
from rpython.flowspace.model import Variable |
| 5 |
from rpython.tool.algo.color import DependencyGraph |
| 6 |
from rpython.tool.algo.unionfind import UnionFind |
| 7 |
|
| 8 |
def perform_register_allocation(graph, consider_var, ListOfKind=()): |
| 9 |
"""Perform register allocation for the Variables of the given 'kind' |
| 10 |
in the 'graph'.""" |
| 11 |
regalloc = RegAllocator(graph, consider_var, ListOfKind) |
| 12 |
regalloc.make_dependencies() |
| 13 |
regalloc.coalesce_variables() |
| 14 |
regalloc.find_node_coloring() |
| 15 |
return regalloc |
| 16 |
|
| 17 |
|
| 18 |
class RegAllocator(object): |
| 19 |
DEBUG_REGALLOC = False |
| 20 |
|
| 21 |
def __init__(self, graph, consider_var, ListOfKind): |
| 22 |
self.graph = graph |
| 23 |
self.consider_var = consider_var |
| 24 |
self.ListOfKind = ListOfKind |
| 25 |
|
| 26 |
def make_dependencies(self): |
| 27 |
dg = DependencyGraph() |
| 28 |
for block in self.graph.iterblocks(): |
| 29 |
|
| 30 |
die_at = dict.fromkeys(block.inputargs, 0) |
| 31 |
for i, op in enumerate(block.operations): |
| 32 |
for v in op.args: |
| 33 |
if isinstance(v, Variable): |
| 34 |
die_at[v] = i |
| 35 |
elif isinstance(v, self.ListOfKind): |
| 36 |
for v1 in v: |
| 37 |
if isinstance(v1, Variable): |
| 38 |
die_at[v1] = i |
| 39 |
if op.result is not None: |
| 40 |
die_at[op.result] = i + 1 |
| 41 |
if isinstance(block.exitswitch, tuple): |
| 42 |
for x in block.exitswitch: |
| 43 |
die_at.pop(x, None) |
| 44 |
else: |
| 45 |
die_at.pop(block.exitswitch, None) |
| 46 |
for link in block.exits: |
| 47 |
for v in link.args: |
| 48 |
die_at.pop(v, None) |
| 49 |
die_at = [(value, key) for (key, value) in die_at.items()] |
| 50 |
die_at.sort() |
| 51 |
die_at.append((sys.maxint,)) |
| 52 |
|
| 53 |
|
| 54 |
livevars = [v for v in block.inputargs |
| 55 |
if self.consider_var(v)] |
| 56 |
|
| 57 |
for i, v in enumerate(livevars): |
| 58 |
dg.add_node(v) |
| 59 |
for j in range(i): |
| 60 |
dg.add_edge(livevars[j], v) |
| 61 |
livevars = set(livevars) |
| 62 |
die_index = 0 |
| 63 |
for i, op in enumerate(block.operations): |
| 64 |
while die_at[die_index][0] == i: |
| 65 |
try: |
| 66 |
livevars.remove(die_at[die_index][1]) |
| 67 |
except KeyError: |
| 68 |
pass |
| 69 |
die_index += 1 |
| 70 |
if (op.result is not None and |
| 71 |
self.consider_var(op.result)): |
| 72 |
dg.add_node(op.result) |
| 73 |
for v in livevars: |
| 74 |
if self.consider_var(v): |
| 75 |
dg.add_edge(v, op.result) |
| 76 |
livevars.add(op.result) |
| 77 |
self._depgraph = dg |
| 78 |
|
| 79 |
def coalesce_variables(self): |
| 80 |
self._unionfind = UnionFind() |
| 81 |
pendingblocks = list(self.graph.iterblocks()) |
| 82 |
while pendingblocks: |
| 83 |
block = pendingblocks.pop() |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
for link in block.exits: |
| 91 |
if link.last_exception is not None: |
| 92 |
self._depgraph.add_node(link.last_exception) |
| 93 |
if link.last_exc_value is not None: |
| 94 |
self._depgraph.add_node(link.last_exc_value) |
| 95 |
for i, v in enumerate(link.args): |
| 96 |
self._try_coalesce(v, link.target.inputargs[i]) |
| 97 |
|
| 98 |
def _try_coalesce(self, v, w): |
| 99 |
if isinstance(v, Variable) and self.consider_var(v) \ |
| 100 |
and self.consider_var(w): |
| 101 |
dg = self._depgraph |
| 102 |
uf = self._unionfind |
| 103 |
v0 = uf.find_rep(v) |
| 104 |
w0 = uf.find_rep(w) |
| 105 |
if v0 is not w0 and v0 not in dg.neighbours[w0]: |
| 106 |
_, rep, _ = uf.union(v0, w0) |
| 107 |
assert uf.find_rep(v0) is uf.find_rep(w0) is rep |
| 108 |
if rep is v0: |
| 109 |
dg.coalesce(w0, v0) |
| 110 |
else: |
| 111 |
assert rep is w0 |
| 112 |
dg.coalesce(v0, w0) |
| 113 |
|
| 114 |
def find_node_coloring(self): |
| 115 |
self._coloring = self._depgraph.find_node_coloring() |
| 116 |
if self.DEBUG_REGALLOC: |
| 117 |
for block in self.graph.iterblocks(): |
| 118 |
print(block) |
| 119 |
for v in block.getvariables(): |
| 120 |
print('\t', v, '\t', self.getcolor(v)) |
| 121 |
|
| 122 |
def find_num_colors(self): |
| 123 |
if self._coloring: |
| 124 |
numcolors = max(self._coloring.values()) + 1 |
| 125 |
else: |
| 126 |
numcolors = 0 |
| 127 |
self.numcolors = numcolors |
| 128 |
|
| 129 |
def getcolor(self, v): |
| 130 |
return self._coloring[self._unionfind.find_rep(v)] |
| 131 |
|
| 132 |
def checkcolor(self, v, color): |
| 133 |
try: |
| 134 |
return self.getcolor(v) == color |
| 135 |
except KeyError: |
| 136 |
return False |
| 137 |
|
| 138 |
def swapcolors(self, col1, col2): |
| 139 |
for key, value in self._coloring.items(): |
| 140 |
if value == col1: |
| 141 |
self._coloring[key] = col2 |
| 142 |
elif value == col2: |
| 143 |
self._coloring[key] = col1 |