Skip to content

Commit

Permalink
add some more arithmetic ops
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Feb 15, 2022
1 parent 81baa6a commit 4e71f13
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions vyper/lll/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,39 @@ def apply_general_optimizations(node: LLLnode) -> LLLnode:
argz = []
value = ceil32(t.value)

elif node.value == "add" and get_int_at(argz, 0) == 0:
value = argz[1].value
annotation = argz[1].annotation
argz = argz[1].args
# swap args in add and mul if one is a literal so
# some of these optimizations are easier to write
elif node.value in {"add", "mul"} and int_at(argz, 0):
argz = [argz[1], argz[0]]
if node.value == "add" and get_int_at(argz, 1) == 0:
value = argz[1].value
annotation = argz[1].annotation
argz = argz[1].args
elif node.value == "mul" and get_int_at(argz, 1) == 0:
value = 0
annotation = argz[1].annotation
argz = []
elif node.value == "mul" and get_int_at(argz, 1) == 1:
value = argz[1].value
annotation = argz[1].annotation
argz = argz[1].args

elif node.value == "add" and get_int_at(argz, 1) == 0:
elif node.value in {"div", "sdiv"} and get_int_at(argz, 1) == 1:
value = argz[0].value
annotation = argz[0].annotation
argz = argz[0].args

elif node.value in {"div", "sdiv", "smod", "mod"} and get_int_at(argz, 1) == 0:
value = 0
annotation = argz[0].annotation
argz = argz[0].args

# TODO: rules for
# - mul by 0, 1, -1, power of two
# - div/sdiv by 0, 1, -1, power of two
# - mod/smod by 0, 1
# - shl/shr/sar
# - lt/gt/le/ge/ne

elif (
node.value in ("clamp", "uclamp")
Expand Down

0 comments on commit 4e71f13

Please sign in to comment.