1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
from rpython.translator.translator import TranslationContext
from rpython.translator.backendopt.innerloop import find_inner_loops
from rpython.conftest import option
def test_simple_loop():
def snippet_fn(x, y):
while y > 0:
y -= x
return y
t = TranslationContext()
graph = t.buildflowgraph(snippet_fn)
if option.view:
t.view()
loops = find_inner_loops(graph)
assert len(loops) == 1
loop = loops[0]
assert loop.headblock.operations[0].opname == 'gt'
assert len(loop.links) == 2
assert loop.links[0] in loop.headblock.exits
assert loop.links[1] in loop.links[0].target.exits
assert loop.links[1].target is loop.headblock
def test_two_loops():
def snippet_fn(x, y):
while y > 0:
y -= x
while y < 0:
y += x
return y
t = TranslationContext()
graph = t.buildflowgraph(snippet_fn)
if option.view:
t.view()
loops = find_inner_loops(graph)
assert len(loops) == 2
assert loops[0].headblock is not loops[1].headblock
for loop in loops:
assert loop.headblock.operations[0].opname in ('gt', 'lt')
assert len(loop.links) == 2
assert loop.links[0] in loop.headblock.exits
assert loop.links[1] in loop.links[0].target.exits
assert loop.links[1].target is loop.headblock
def test_nested_loops():
def snippet_fn(x, z):
y = 0
while y <= 10:
while z < y:
z += y
y += 1
return z
t = TranslationContext()
graph = t.buildflowgraph(snippet_fn)
if option.view:
t.view()
loops = find_inner_loops(graph)
assert len(loops) == 1
loop = loops[0]
assert loop.headblock.operations[0].opname == 'lt'
assert len(loop.links) == 2
assert loop.links[0] in loop.headblock.exits
assert loop.links[1] in loop.links[0].target.exits
assert loop.links[1].target is loop.headblock
|