-
-
Notifications
You must be signed in to change notification settings - Fork 816
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
105654c
commit 120c093
Showing
1 changed file
with
46 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,53 @@ | ||
from tests.venom_utils import assert_ctx_eq, parse_venom | ||
from vyper.venom.analysis import IRAnalysesCache | ||
from vyper.venom.basicblock import IRBasicBlock, IRLabel | ||
from vyper.venom.context import IRContext | ||
from vyper.venom.passes import MakeSSA | ||
|
||
|
||
def test_phi_case(): | ||
ctx = IRContext() | ||
fn = ctx.create_function("_global") | ||
|
||
bb = fn.get_basic_block() | ||
|
||
bb_cont = IRBasicBlock(IRLabel("condition"), fn) | ||
bb_then = IRBasicBlock(IRLabel("then"), fn) | ||
bb_else = IRBasicBlock(IRLabel("else"), fn) | ||
bb_if_exit = IRBasicBlock(IRLabel("if_exit"), fn) | ||
fn.append_basic_block(bb_cont) | ||
fn.append_basic_block(bb_then) | ||
fn.append_basic_block(bb_else) | ||
fn.append_basic_block(bb_if_exit) | ||
|
||
v = bb.append_instruction("mload", 64) | ||
bb_cont.append_instruction("jnz", v, bb_then.label, bb_else.label) | ||
def _check_pre_post(pre, post): | ||
ctx = parse_venom(pre) | ||
for fn in ctx.functions.values(): | ||
ac = IRAnalysesCache(fn) | ||
MakeSSA(ac, fn).run_pass() | ||
assert_ctx_eq(ctx, parse_venom(post)) | ||
|
||
bb_if_exit.append_instruction("add", v, 1, ret=v) | ||
bb_if_exit.append_instruction("jmp", bb_cont.label) | ||
|
||
bb_then.append_instruction("assert", bb_then.append_instruction("mload", 96)) | ||
bb_then.append_instruction("jmp", bb_if_exit.label) | ||
bb_else.append_instruction("jmp", bb_if_exit.label) | ||
|
||
bb.append_instruction("jmp", bb_cont.label) | ||
|
||
ac = IRAnalysesCache(fn) | ||
MakeSSA(ac, fn).run_pass() | ||
|
||
condition_block = fn.get_basic_block("condition") | ||
assert len(condition_block.instructions) == 2 | ||
def test_phi_case(): | ||
pre = """ | ||
function loop { | ||
main: | ||
%v = mload 64 | ||
jmp @test | ||
test: | ||
jnz %v, @then, @else | ||
then: | ||
%t = mload 96 | ||
assert %t | ||
jmp @if_exit | ||
else: | ||
jmp @if_exit | ||
if_exit: | ||
%v = add %v, 1 | ||
jmp @test | ||
} | ||
""" | ||
post = """ | ||
function loop { | ||
main: | ||
%v = mload 64 | ||
jmp @test | ||
test: | ||
%v:1 = phi @main, %v, @if_exit, %v:2 | ||
jnz %v:1, @then, @else | ||
then: | ||
%t = mload 96 | ||
assert %t | ||
jmp @if_exit | ||
else: | ||
jmp @if_exit | ||
if_exit: | ||
%v:2 = add %v:1, 1 | ||
jmp @test | ||
} | ||
""" | ||
_check_pre_post(pre, post) | ||
|
||
phi_inst = condition_block.instructions[0] | ||
assert phi_inst.opcode == "phi" | ||
assert phi_inst.operands[0].name == "_global" | ||
assert phi_inst.operands[1].name == "%1" | ||
assert phi_inst.operands[2].name == "if_exit" | ||
assert phi_inst.operands[3].name == "%1" | ||
assert phi_inst.output.name == "%1" | ||
assert phi_inst.output.value != phi_inst.operands[1].value | ||
assert phi_inst.output.value != phi_inst.operands[3].value |