From 287b82de0fd00091b1bd55ee83415a7759b17d7e Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Fri, 6 May 2022 11:17:50 +0200 Subject: [PATCH] add optimizer rules for `exp` --- vyper/ir/optimizer.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/vyper/ir/optimizer.py b/vyper/ir/optimizer.py index 0fec39b6e0f..baedc5c37f2 100644 --- a/vyper/ir/optimizer.py +++ b/vyper/ir/optimizer.py @@ -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), @@ -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))