Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
rihi committed Apr 3, 2024
1 parent d9b4578 commit 55cdbd0
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1283,3 +1283,32 @@ def test_common_subexpression_elimination_correct_place():
Assignment(Variable("d", ssa_label=4), BinaryOperation(OperationType.plus, [Variable("e", ssa_label=2), replacement0])),
Assignment(Variable("f", ssa_label=1), BinaryOperation(OperationType.minus, [Variable("g", ssa_label=4), replacement1])),
]


def test_common_subexpression_elimination_correct_place2():
"""Check that the instruction is inserted at the correct position"""
expr0 = BinaryOperation(OperationType.multiply, [Variable("a", Integer.int32_t()), Constant(2, Integer.int32_t())])
expr1 = BinaryOperation(OperationType.plus, [expr0.copy(), Constant(5, Integer.int32_t())])

cfg = ControlFlowGraph()
cfg.add_node(
node := BasicBlock(
0,
instructions=[
Assignment(ListOperation([]), Call(FunctionSymbol("func", 0), [expr0.copy(), expr1.copy()])),
Assignment(ListOperation([]), Call(FunctionSymbol("func", 0), [expr1.copy()]))
],
)
)
_run_cse(cfg, _generate_options(threshold=2))

replacement0 = Variable("c0", Integer.int32_t(), ssa_label=0)
replacement1 = Variable("c1", Integer.int32_t(), ssa_label=0)
expr_new = BinaryOperation(OperationType.plus, [replacement1.copy(), Constant(5, Integer.int32_t())])

assert node.instructions == [
Assignment(replacement1.copy(), expr0.copy()),
Assignment(replacement0.copy(), expr_new.copy()),
Assignment(ListOperation([]), Call(FunctionSymbol("func", 0), [replacement1.copy(), replacement0.copy()])),
Assignment(ListOperation([]), Call(FunctionSymbol("func", 0), [replacement0.copy()])),
]

0 comments on commit 55cdbd0

Please sign in to comment.