diff --git a/examples/test/ssa_roundtrip/argwrite.bril b/examples/test/ssa_roundtrip/argwrite.bril new file mode 100644 index 000000000..30a0cc17f --- /dev/null +++ b/examples/test/ssa_roundtrip/argwrite.bril @@ -0,0 +1,9 @@ +# ARGS: 3 +@main(a: int) { + cond: bool = const true; + br cond .here .there; +.here: + a: int = const 5; +.there: + print a; +} diff --git a/examples/test/ssa_roundtrip/argwrite.out b/examples/test/ssa_roundtrip/argwrite.out new file mode 100644 index 000000000..7ed6ff82d --- /dev/null +++ b/examples/test/ssa_roundtrip/argwrite.out @@ -0,0 +1 @@ +5 diff --git a/examples/test/ssa_roundtrip/if-const.bril b/examples/test/ssa_roundtrip/if-const.bril new file mode 100644 index 000000000..d7151e83f --- /dev/null +++ b/examples/test/ssa_roundtrip/if-const.bril @@ -0,0 +1,14 @@ +@main() { + cond: bool = const true; + br cond .true .false; +.true: + a: int = const 0; + jmp .zexit; +.false: + b: int = const 1; + jmp .zexit; +# zexit to trigger a bug in to_ssa.py that depends on +# the order that basic blocks get renamed. +.zexit: + print a; +} diff --git a/examples/test/ssa_roundtrip/if-const.out b/examples/test/ssa_roundtrip/if-const.out new file mode 100644 index 000000000..573541ac9 --- /dev/null +++ b/examples/test/ssa_roundtrip/if-const.out @@ -0,0 +1 @@ +0 diff --git a/examples/test/ssa_roundtrip/if-ssa.bril.BROKEN b/examples/test/ssa_roundtrip/if-ssa.bril.BROKEN new file mode 100644 index 000000000..e6eca9aac --- /dev/null +++ b/examples/test/ssa_roundtrip/if-ssa.bril.BROKEN @@ -0,0 +1,17 @@ +# ARGS: true +@main(cond: bool) { +.entry: + a.1: int = const 47; + br cond .left .right; +.left: + a.2: int = add a.1 a.1; + jmp .zexit; +.right: + a.3: int = mul a.1 a.1; + jmp .zexit; +# zexit to trigger a bug in to_ssa.py that depends on +# the order that basic blocks get renamed. +.zexit: + a.4: int = phi .left a.2 .right a.3; + print a.4; +} \ No newline at end of file diff --git a/examples/test/ssa_roundtrip/if-ssa.out b/examples/test/ssa_roundtrip/if-ssa.out new file mode 100644 index 000000000..49541f721 --- /dev/null +++ b/examples/test/ssa_roundtrip/if-ssa.out @@ -0,0 +1 @@ +94 diff --git a/examples/test/ssa_roundtrip/if.bril b/examples/test/ssa_roundtrip/if.bril new file mode 100644 index 000000000..37c33fa4b --- /dev/null +++ b/examples/test/ssa_roundtrip/if.bril @@ -0,0 +1,14 @@ +# ARGS: false +@main(cond: bool) { +.entry: + a: int = const 47; + br cond .left .right; +.left: + a: int = add a a; + jmp .exit; +.right: + a: int = mul a a; + jmp .exit; +.exit: + print a; +} diff --git a/examples/test/ssa_roundtrip/if.out b/examples/test/ssa_roundtrip/if.out new file mode 100644 index 000000000..f3346818e --- /dev/null +++ b/examples/test/ssa_roundtrip/if.out @@ -0,0 +1 @@ +2209 diff --git a/examples/test/ssa_roundtrip/loop-branch.bril.BROKEN b/examples/test/ssa_roundtrip/loop-branch.bril.BROKEN new file mode 100644 index 000000000..ad872e515 --- /dev/null +++ b/examples/test/ssa_roundtrip/loop-branch.bril.BROKEN @@ -0,0 +1,24 @@ +@func(): int { + n: int = const 5; + ret n; +} + +@loop(infinite: bool, print: bool) { +.entry: +.loop.header: + br infinite .loop.body .loop.end; +.loop.body: + br print .loop.print .loop.next; +.loop.print: + v: int = call @func; + print v; +.loop.next: + jmp .loop.header; +.loop.end: +} + +@main() { + infinite: bool = const false; + print: bool = const true; + call @loop infinite print; +} diff --git a/examples/test/ssa_roundtrip/loop-branch.out b/examples/test/ssa_roundtrip/loop-branch.out new file mode 100644 index 000000000..e69de29bb diff --git a/examples/test/ssa_roundtrip/loop.bril b/examples/test/ssa_roundtrip/loop.bril new file mode 100644 index 000000000..777c69169 --- /dev/null +++ b/examples/test/ssa_roundtrip/loop.bril @@ -0,0 +1,14 @@ +@main { +.entry: + i: int = const 1; + jmp .loop; +.loop: + max: int = const 10; + cond: bool = lt i max; + br cond .body .exit; +.body: + i: int = add i i; + jmp .loop; +.exit: + print i; +} diff --git a/examples/test/ssa_roundtrip/loop.out b/examples/test/ssa_roundtrip/loop.out new file mode 100644 index 000000000..b6a7d89c6 --- /dev/null +++ b/examples/test/ssa_roundtrip/loop.out @@ -0,0 +1 @@ +16 diff --git a/examples/test/ssa_roundtrip/selfloop.bril b/examples/test/ssa_roundtrip/selfloop.bril new file mode 100644 index 000000000..dec694e7d --- /dev/null +++ b/examples/test/ssa_roundtrip/selfloop.bril @@ -0,0 +1,14 @@ +@main { +.entry: + one: int = const 1; + zero: int = const 0; + x: int = const 5; +.loop: + x: int = sub x one; + done: bool = eq x zero; +.br: + br done .exit .loop; +.exit: + print x; + ret; +} diff --git a/examples/test/ssa_roundtrip/selfloop.out b/examples/test/ssa_roundtrip/selfloop.out new file mode 100644 index 000000000..573541ac9 --- /dev/null +++ b/examples/test/ssa_roundtrip/selfloop.out @@ -0,0 +1 @@ +0 diff --git a/examples/test/ssa_roundtrip/turnt.toml b/examples/test/ssa_roundtrip/turnt.toml new file mode 100644 index 000000000..282aeb09f --- /dev/null +++ b/examples/test/ssa_roundtrip/turnt.toml @@ -0,0 +1 @@ +command = "bril2json < {filename} | python ../../to_ssa.py | python ../../from_ssa.py | python ../../tdce.py | brili {args}" diff --git a/examples/test/ssa_roundtrip/while.bril b/examples/test/ssa_roundtrip/while.bril new file mode 100644 index 000000000..c6e5ac248 --- /dev/null +++ b/examples/test/ssa_roundtrip/while.bril @@ -0,0 +1,13 @@ +# ARGS: 5 +@main(a: int) { +.while.cond: + zero: int = const 0; + is_term: bool = eq a zero; + br is_term .while.finish .while.body; +.while.body: + one: int = const 1; + a: int = sub a one; + jmp .while.cond; +.while.finish: + print a; +} diff --git a/examples/test/ssa_roundtrip/while.out b/examples/test/ssa_roundtrip/while.out new file mode 100644 index 000000000..573541ac9 --- /dev/null +++ b/examples/test/ssa_roundtrip/while.out @@ -0,0 +1 @@ +0 diff --git a/examples/test/to_ssa/if-const.out b/examples/test/to_ssa/if-const.out index 84b19da84..d3578a70b 100644 --- a/examples/test/to_ssa/if-const.out +++ b/examples/test/to_ssa/if-const.out @@ -9,7 +9,7 @@ b.0: int = const 1; jmp .zexit; .zexit: - b.1: int = phi b.0 b.0 .false .true; + b.1: int = phi b.0 __undefined .false .true; a.1: int = phi __undefined a.0 .false .true; print a.1; ret; diff --git a/examples/test/to_ssa/if-ssa.out b/examples/test/to_ssa/if-ssa.out index c9d635ace..e8fee00e7 100644 --- a/examples/test/to_ssa/if-ssa.out +++ b/examples/test/to_ssa/if-ssa.out @@ -10,7 +10,7 @@ jmp .zexit; .zexit: a.3.1: int = phi __undefined a.3.0 .left .right; - a.2.1: int = phi a.2.0 a.2.0 .left .right; + a.2.1: int = phi a.2.0 __undefined .left .right; a.4.0: int = phi a.2.1 a.3.1 .left .right; print a.4.0; ret; diff --git a/examples/test/to_ssa/loop-branch.bril b/examples/test/to_ssa/loop-branch.bril new file mode 100644 index 000000000..ad872e515 --- /dev/null +++ b/examples/test/to_ssa/loop-branch.bril @@ -0,0 +1,24 @@ +@func(): int { + n: int = const 5; + ret n; +} + +@loop(infinite: bool, print: bool) { +.entry: +.loop.header: + br infinite .loop.body .loop.end; +.loop.body: + br print .loop.print .loop.next; +.loop.print: + v: int = call @func; + print v; +.loop.next: + jmp .loop.header; +.loop.end: +} + +@main() { + infinite: bool = const false; + print: bool = const true; + call @loop infinite print; +} diff --git a/examples/test/to_ssa/loop-branch.out b/examples/test/to_ssa/loop-branch.out new file mode 100644 index 000000000..c183019c2 --- /dev/null +++ b/examples/test/to_ssa/loop-branch.out @@ -0,0 +1,30 @@ +@func: int { +.b1: + n.0: int = const 5; + ret n.0; +} +@loop(infinite: bool, print: bool) { +.entry: + jmp .loop.header; +.loop.header: + v.0: int = phi __undefined v.1 .entry .loop.next; + br infinite .loop.body .loop.end; +.loop.body: + br print .loop.print .loop.next; +.loop.print: + v.2: int = call @func; + print v.2; + jmp .loop.next; +.loop.next: + v.1: int = phi v.0 v.2 .loop.body .loop.print; + jmp .loop.header; +.loop.end: + ret; +} +@main { +.b1: + infinite.0: bool = const false; + print.0: bool = const true; + call @loop infinite.0 print.0; + ret; +} diff --git a/examples/to_ssa.py b/examples/to_ssa.py index 093c96cb3..62de7b3be 100644 --- a/examples/to_ssa.py +++ b/examples/to_ssa.py @@ -4,7 +4,7 @@ from cfg import block_map, successors, add_terminators, add_entry, reassemble from form_blocks import form_blocks -from dom import get_dom, dom_fronts, dom_tree, map_inv +from dom import get_dom, dom_fronts, dom_tree def def_blocks(blocks): @@ -83,6 +83,7 @@ def _rename(block): _rename(b) # Restore stacks. + stack.clear() stack.update(old_stack) entry = list(blocks.keys())[0] @@ -91,7 +92,6 @@ def _rename(block): return phi_args, phi_dests - def insert_phis(blocks, phi_args, phi_dests, types): for block, instrs in blocks.items(): for dest, pairs in sorted(phi_args[block].items()): @@ -121,7 +121,6 @@ def func_to_ssa(func): add_entry(blocks) add_terminators(blocks) succ = {name: successors(block[-1]) for name, block in blocks.items()} - pred = map_inv(succ) dom = get_dom(succ, list(blocks.keys())[0]) df = dom_fronts(dom, succ)