Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fold_divmod mwcc inner add fix #265

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions m2c/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,6 @@ def fold_divmod(original_expr: BinaryOp) -> BinaryOp:
expr = left_expr
left_expr = early_unwrap_ints(expr.left)
right_expr = early_unwrap_ints(expr.right)
# Normalize MULT_HI(N, x) to MULT_HI(x, N)
if isinstance(left_expr, Literal) and not isinstance(right_expr, Literal):
left_expr, right_expr = right_expr, left_expr

# Remove inner addition: (MULT_HI(x, N) + x) >> M --> MULT_HI(x, N) >> M
# MULT_HI performs signed multiplication, so the `+ x` acts as setting the 32nd bit
Expand All @@ -706,12 +703,19 @@ def fold_divmod(original_expr: BinaryOp) -> BinaryOp:
isinstance(left_expr, BinaryOp)
and left_expr.op == "MULT_HI"
and expr.op == "+"
and early_unwrap_ints(left_expr.left) == right_expr
and (
right_expr == early_unwrap_ints(left_expr.left)
or right_expr == early_unwrap_ints(left_expr.right)
)
):
expr = left_expr
left_expr = early_unwrap_ints(expr.left)
right_expr = early_unwrap_ints(expr.right)

# Normalize MULT_HI(N, x) to MULT_HI(x, N)
if isinstance(left_expr, Literal) and not isinstance(right_expr, Literal):
left_expr, right_expr = right_expr, left_expr

# Shift on the LHS of the mul: MULT_HI(x >> M, N) --> MULT_HI(x, N) >> M
if (
expr.op in mult_high_ops
Expand Down
Loading
Loading