Mercurial > lcfOS
annotate python/interferencegraph.py @ 278:9fca39eebe50
First implementation of regalloc with coalsesc
author | Windel Bouwman |
---|---|
date | Sun, 29 Sep 2013 14:08:15 +0200 |
parents | 046017431c6a |
children | 2ccd57b1d78c |
rev | line source |
---|---|
277 | 1 import graph |
2 | |
3 | |
4 class InterferenceGraphNode(graph.Node): | |
5 def __init__(self, g, varname): | |
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() |
277 | 9 |
10 def __repr__(self): | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
11 return 'IGN<{}>'.format(self.temps) |
277 | 12 |
13 | |
14 class InterferenceGraph(graph.Graph): | |
15 """ | |
16 Interference graph. | |
17 """ | |
18 def __init__(self, flowgraph): | |
19 """ | |
20 Create a new interference graph from a flowgraph | |
21 """ | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
22 super().__init__() |
277 | 23 # Calculate liveness in CFG: |
24 ### | |
25 # Liveness: | |
26 # in[n] = use[n] UNION (out[n] - def[n]) | |
27 # out[n] = for s in n.succ in union in[s] | |
28 ### | |
29 for n in flowgraph.nodes: | |
30 n.live_in = set() | |
31 n.live_out = set() | |
32 | |
33 # Dataflow fixed point iteration: | |
34 change = True | |
35 while change: | |
36 change = False | |
37 for n in flowgraph.nodes: | |
38 _in = n.live_in | |
39 _out = n.live_out | |
40 n.live_in = n.uses | (n.live_out - n.defs) | |
41 if n.Succ: | |
42 n.live_out = set.union(*(s.live_in for s in n.Succ)) | |
43 else: | |
44 n.live_out = set() | |
45 change = change or (_in != n.live_in) or (_out != n.live_out) | |
46 | |
47 # Construct interference graph: | |
48 for n in flowgraph.nodes: | |
49 for tmp in n.live_out: | |
50 n1 = self.getNode(tmp) | |
51 for tmp2 in (n.live_out - {tmp}): | |
52 n2 = self.getNode(tmp2) | |
53 self.addEdge(n1, n2) | |
54 | |
55 def getNode(self, tmp): | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
56 # Linear search |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
57 # TODO: can be improved for speed! |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
58 for n in self.nodes: |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
59 if tmp in n.temps: |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
60 return n |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
61 n = InterferenceGraphNode(self, tmp) |
277 | 62 self.addNode(n) |
63 return n | |
64 | |
278
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
65 def Combine(self, n, m): |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
66 """ Combine n and m into n """ |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
67 n.temps.extend(m.temps) |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
68 n.moves.update(m.moves) |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
69 for t in m.Adjecent: |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
70 self.addEdge(n, t) |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
71 self.delNode(m) |
9fca39eebe50
First implementation of regalloc with coalsesc
Windel Bouwman
parents:
277
diff
changeset
|
72 |