Skip to content

Commit

Permalink
add optimizer rules for exp
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed May 6, 2022
1 parent f999143 commit 287b82d
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions vyper/ir/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def _is_int(node: IRnode) -> bool:
"sdiv": (evm_div, "/", SIGNED),
"mod": (evm_mod, "%", UNSIGNED),
"smod": (evm_mod, "%", SIGNED),
"exp": (operator.pow, "**", UNSIGNED),
"eq": (operator.eq, "==", UNSIGNED),
"ne": (operator.ne, "!=", UNSIGNED),
"lt": (operator.lt, "<", UNSIGNED),
Expand Down Expand Up @@ -165,6 +166,20 @@ def _conservative_eq(x, y):
new_val = "sub"
new_args = [0, args[0]]

elif binop == "exp":
# n ** 0 == (n == 0)
if _int(args[1]) == 0:
new_val = "iszero"
new_args = [args[0]]
# n ** 1 == n
if _int(args[1]) == 1:
new_val = args[0].value
new_args = args[0].args
# 0 ** n == 0; 1 ** n == 1
if _int(args[0]) in (0, 1):
new_val = _int(args[0])
new_args = []

# maybe OK:
# elif binop == "div" and _int(args[1], UNSIGNED) == MAX_UINT256:
# # (div x (2**256 - 1)) == (eq x (2**256 - 1))
Expand Down

0 comments on commit 287b82d

Please sign in to comment.