Debian Codegraph @

rpython/translator/backendopt/test/test_ssa.py

raw symbols SCIP
1 from rpython.translator.backendopt.ssa import *
2 from rpython.translator.translator import TranslationContext
3 from rpython.flowspace.model import (
4 Block, Link, Variable, Constant, SpaceOperation, FunctionGraph)
5
6
7 def test_data_flow_families():
8 def snippet_fn(xx, yy):
9 while yy > 0:
10 if 0 < xx:
11 yy = yy - xx
12 else:
13 yy = yy + xx
14 return yy
15 t = TranslationContext()
16 graph = t.buildflowgraph(snippet_fn)
17 operations = []
18 for block in graph.iterblocks():
19 operations += block.operations
20
21 variable_families = DataFlowFamilyBuilder(graph).get_variable_families()
22
23 # we expect to find xx only once:
24 v_xx = variable_families.find_rep(graph.getargs()[0])
25 found = 0
26 for op in operations:
27 if op.opname in ('add', 'sub', 'lt'):
28 assert variable_families.find_rep(op.args[1]) == v_xx
29 found += 1
30 assert found == 3
31
32
33 def test_SSI_to_SSA():
34 def snippet_fn(v1, v2, v3):
35 if v1: # v4 = is_true(v1)
36 while v3: # v5 = is_true(v3)
37 pass
38 passed_over = 0
39 else:
40 v6 = snippet_fn(v3, v2, v1) # v6 = simple_call(v3, v2, v1)
41 passed_over = v6
42 v7 = passed_over # v7 = inputarg
43 return v7+v1 # v8 = add(v7, v1)
44
45 t = TranslationContext()
46 graph = t.buildflowgraph(snippet_fn)
47 SSI_to_SSA(graph)
48 allvars = []
49 for block in graph.iterblocks():
50 allvars += [v.name for v in block.getvariables()]
51 # see comments above for where the 8 remaining variables are expected to be
52 assert len(dict.fromkeys(allvars)) == 8
53
54
55 def test_SSA_to_SSI():
56 c = Variable('c')
57 x = Variable('x')
58 y = Variable('y')
59 b1 = Block([c])
60 b2 = Block([x])
61 b3 = Block([])
62
63 graph = FunctionGraph('x', b1)
64 b2.operations.append(SpaceOperation('add', [x, c], y))
65 b2.exitswitch = y
66
67 b1.closeblock(Link([Constant(0)], b2))
68 b2.closeblock(Link([y], b2), Link([], b3))
69 b3.closeblock(Link([y, c], graph.exceptblock))
70 SSA_to_SSI(graph)
71
72 assert len(b1.inputargs) == 1
73 assert len(b2.inputargs) == 2
74 assert len(b3.inputargs) == 2
75
76 assert b2.inputargs == b2.operations[0].args
77 assert len(b1.exits[0].args) == 2
78 assert b1.exits[0].args[1] is c
79 assert len(b2.exits[0].args) == 2
80 assert b2.exits[0].args == [y, b2.inputargs[1]]
81 assert len(b2.exits[1].args) == 2
82 assert len(b3.exits[0].args) == 2
83
84 index = b3.inputargs.index(b3.exits[0].args[0])
85 assert b2.exits[1].args[index] is b2.operations[0].result
86
87 index = b3.inputargs.index(b3.exits[0].args[1])
88 assert b2.exits[1].args[index] is b2.inputargs[1]
89
90
91 def test_SSA_to_SSI_2():
92 x = Variable('x')
93 y = Variable('y')
94 z = Variable('z')
95 b1 = Block([x])
96 b2 = Block([y])
97 b3 = Block([])
98
99 b3.operations.append(SpaceOperation('hello', [y], z))
100 b1.closeblock(Link([x], b2), Link([], b3))
101 graph = FunctionGraph('x', b1)
102 SSA_to_SSI(graph)
103
104 assert b1.inputargs == [x]
105 assert b2.inputargs == [y]
106 assert b3.inputargs == [b3.operations[0].args[0]]
107 assert b1.exits[0].args == [x]
108 assert b1.exits[1].args == [x]