annotate python/interferencegraph.py @ 279:2ccd57b1d78c

Fix register allocator to do burn2 OK
author Windel Bouwman
date Sat, 12 Oct 2013 09:56:23 +0200
parents 9fca39eebe50
children 02385f62f250
rev   line source
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
1 import graph
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
2
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
3
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
4 class InterferenceGraphNode(graph.Node):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
5 def __init__(self, g, varname):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
6 super().__init__(g)
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
7 self.temps = [varname]
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
8 self.moves = set()
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
9 self.color = None
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
10
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
11 def __repr__(self):
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
12 return '{}({})'.format(self.temps, self.color)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
13
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
14
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
15 class InterferenceGraph(graph.Graph):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
16 """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
17 Interference graph.
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
18 """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
19 def __init__(self, flowgraph):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
20 """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
21 Create a new interference graph from a flowgraph
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
22 """
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
23 super().__init__()
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
24 # Calculate liveness in CFG:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
25 ###
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
26 # Liveness:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
27 # in[n] = use[n] UNION (out[n] - def[n])
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
28 # out[n] = for s in n.succ in union in[s]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
29 ###
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
30 for n in flowgraph.nodes:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
31 n.live_in = set()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
32 n.live_out = set()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
33
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
34 # Dataflow fixed point iteration:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
35 change = True
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
36 while change:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
37 change = False
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
38 for n in flowgraph.nodes:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
39 _in = n.live_in
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
40 _out = n.live_out
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
41 n.live_in = n.uses | (n.live_out - n.defs)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
42 if n.Succ:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
43 n.live_out = set.union(*(s.live_in for s in n.Succ))
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
44 else:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
45 n.live_out = set()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
46 change = change or (_in != n.live_in) or (_out != n.live_out)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
47
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
48 # Construct interference graph:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
49 for n in flowgraph.nodes:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
50 for tmp in n.live_out:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
51 n1 = self.getNode(tmp)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
52 for tmp2 in (n.live_out - {tmp}):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
53 n2 = self.getNode(tmp2)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
54 self.addEdge(n1, n2)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
55
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
56 def to_dot(self, f):
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
57 """ Generate graphviz dot representation """
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
58 for node in self.nodes:
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
59 print('{} [label="{}" shape=box3d];'.format(id(node), node), file=f)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
60 for n, m in self.edges:
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
61 print('{} -> {};'.format(id(n), id(m)), file=f)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
62
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
63 def to_txt(self):
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
64 for node in self.nodes:
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
65 print('{} interferes: {}'.format(node, node.Adjecent))
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
66
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
67 def getNode(self, tmp):
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
68 # Linear search
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
69 # TODO: can be improved for speed!
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
70 for n in self.nodes:
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
71 if tmp in n.temps:
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
72 return n
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
73 print('new one!')
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
74 n = InterferenceGraphNode(self, tmp)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
75 self.addNode(n)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
76 return n
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
77
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
78 def Combine(self, n, m):
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
79 """ Combine n and m into n """
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
80 n.temps.extend(m.temps)
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
81 n.moves.update(m.moves)
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
82 # Reroute all edges:
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
83 e1 = [e for e in self.edges if e[0] is m]
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
84 e2 = [e for e in self.edges if e[1] is m]
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
85 for e in e1:
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
86 self.edges.remove(e)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
87 self.addEdge(n, e[1])
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
88 for e in e2:
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
89 self.edges.remove(e)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
90 self.addEdge(n, e[0])
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
91 # Remove node m:
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
92 self.delNode(m)
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
93