annotate python/ppci/codegen/interferencegraph.py @ 394:988f3fb861e4

c3 code generator rewrite
author Windel Bouwman
date Thu, 22 May 2014 08:14:12 +0200
parents 39bf68bf1891
children
rev   line source
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 314
diff changeset
1 import logging
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 280
diff changeset
2 from .graph import Graph, Node
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
3
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
4
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 280
diff changeset
5 class InterferenceGraphNode(Node):
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
6 def __init__(self, g, varname):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
7 super().__init__(g)
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
8 self.temps = [varname]
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
9 self.moves = set()
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
10 self.color = None
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
11
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
12 def __repr__(self):
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
13 return '{}({})'.format(self.temps, self.color)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
14
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 337
diff changeset
15 def __gt__(self, other):
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 337
diff changeset
16 return str(self.temps) > str(other.temps)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
17
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 280
diff changeset
18 class InterferenceGraph(Graph):
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
19 """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
20 Interference graph.
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
21 """
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
22 def __init__(self, flowgraph):
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 280
diff changeset
23 """ Create a new interference graph from a flowgraph """
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
24 super().__init__()
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 314
diff changeset
25 self.logger = logging.getLogger('interferencegraph')
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
26 # Calculate liveness in CFG:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
27 ###
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
28 # Liveness:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
29 # in[n] = use[n] UNION (out[n] - def[n])
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
30 # out[n] = for s in n.succ in union in[s]
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
31 ###
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
32 for n in flowgraph.nodes:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
33 n.live_in = set()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
34 n.live_out = set()
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
35
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 314
diff changeset
36 # Sort flowgraph nodes backwards:
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 314
diff changeset
37 cfg_nodes = list(flowgraph.nodes)
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 314
diff changeset
38 self.logger.debug('CFG nodes: {}'.format(cfg_nodes))
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 314
diff changeset
39 cfg_nodes.sort(reverse=True)
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 314
diff changeset
40
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
41 # Dataflow fixed point iteration:
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 314
diff changeset
42 n_iterations = 0
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
43 change = True
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
44 while change:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
45 change = False
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 314
diff changeset
46 for n in cfg_nodes:
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
47 _in = n.live_in
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
48 _out = n.live_out
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
49 n.live_in = n.uses | (n.live_out - n.defs)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
50 if n.Succ:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
51 n.live_out = set.union(*(s.live_in for s in n.Succ))
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
52 else:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
53 n.live_out = set()
313
04cf4d26a3bc Added constant function
Windel Bouwman
parents: 301
diff changeset
54 n.live_out = n.live_out | n.defs
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
55 change = change or (_in != n.live_in) or (_out != n.live_out)
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 314
diff changeset
56 n_iterations += 1
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
57
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 314
diff changeset
58 self.logger.debug('Iterations: {} * {}'.format(n_iterations, len(cfg_nodes)))
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
59 # Construct interference graph:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
60 for n in flowgraph.nodes:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
61 for tmp in n.live_out:
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
62 n1 = self.getNode(tmp)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
63 for tmp2 in (n.live_out - {tmp}):
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
64 n2 = self.getNode(tmp2)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
65 self.addEdge(n1, n2)
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
66
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
67 def to_dot(self, f):
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
68 """ Generate graphviz dot representation """
296
9417caea2eb3 Directorized some backend files
Windel Bouwman
parents: 280
diff changeset
69 for n in self.nodes:
314
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
70 print(' {} [label="{}" shape=box3d];'.format(id(n), n), file=f)
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
71 for n, m in self.edges:
314
38f5f298ce0e Add log for interference graph
Windel Bouwman
parents: 313
diff changeset
72 print(' {} -> {};'.format(id(n), id(m)), file=f)
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
73
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
74 def to_txt(self):
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
75 for node in self.nodes:
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
76 print('{} interferes: {}'.format(node, node.Adjecent))
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
77
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
78 def getNode(self, tmp):
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
79 # Linear search
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
80 # TODO: can be improved for speed!
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
81 for n in self.nodes:
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
82 if tmp in n.temps:
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
83 return n
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
84 n = InterferenceGraphNode(self, tmp)
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 314
diff changeset
85 self.add_node(n)
277
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
86 return n
046017431c6a Started register allocator
Windel Bouwman
parents:
diff changeset
87
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
88 def Combine(self, n, m):
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
89 """ Combine n and m into n """
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
90 n.temps.extend(m.temps)
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
91 n.moves.update(m.moves)
279
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
92 # Reroute all edges:
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
93 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
94 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
95 for e in e1:
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
96 self.edges.remove(e)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
97 self.addEdge(n, e[1])
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
98 for e in e2:
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
99 self.edges.remove(e)
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
100 self.addEdge(n, e[0])
2ccd57b1d78c Fix register allocator to do burn2 OK
Windel Bouwman
parents: 278
diff changeset
101 # Remove node m:
278
9fca39eebe50 First implementation of regalloc with coalsesc
Windel Bouwman
parents: 277
diff changeset
102 self.delNode(m)