Debian Codegraph @

rpython/tool/algo/test/test_regalloc.py

raw symbols SCIP
1 from rpython.rtyper.test.test_llinterp import gengraph
2 from rpython.rtyper.lltypesystem import lltype
3 from rpython.tool.algo.regalloc import perform_register_allocation
4 from rpython.flowspace.model import Variable
5 from rpython.conftest import option
6
7
8 def is_int(v):
9 return v.concretetype == lltype.Signed
10
11 def check_valid(graph, regalloc, consider_var):
12 if getattr(option, 'view', False):
13 graph.show()
14 num_renamings = 0
15 for block in graph.iterblocks():
16 inputs = [v for v in block.inputargs if consider_var(v)]
17 colors = [regalloc.getcolor(v) for v in inputs]
18 print inputs, ':', colors
19 assert len(inputs) == len(set(colors))
20 in_use = dict(zip(colors, inputs))
21 for op in block.operations:
22 for v in op.args:
23 if isinstance(v, Variable) and consider_var(v):
24 assert in_use[regalloc.getcolor(v)] is v
25 if consider_var(op.result):
26 in_use[regalloc.getcolor(op.result)] = op.result
27 for link in block.exits:
28 for i, v in enumerate(link.args):
29 if consider_var(v):
30 assert in_use[regalloc.getcolor(v)] is v
31 w = link.target.inputargs[i]
32 if regalloc.getcolor(v) is not regalloc.getcolor(w):
33 print '\trenaming %s:%d -> %s:%d' % (
34 v, regalloc.getcolor(v), w, regalloc.getcolor(w))
35 num_renamings += 1
36 return num_renamings
37
38
39 def test_loop_1():
40 def f(a, b):
41 while a > 0:
42 b += a
43 a -= 1
44 return b
45 t, rtyper, graph = gengraph(f, [int, int], viewbefore=False)
46 regalloc = perform_register_allocation(graph, is_int)
47 num_renamings = check_valid(graph, regalloc, is_int)
48 assert num_renamings == 0
49
50 def test_loop_2():
51 def f(a, b):
52 while a > 0:
53 b += a
54 if b < 10:
55 a, b = b, a
56 a -= 1
57 return b
58 t, rtyper, graph = gengraph(f, [int, int], viewbefore=False)
59 regalloc = perform_register_allocation(graph, is_int)
60 num_renamings = check_valid(graph, regalloc, is_int)
61 assert num_renamings == 2