Expand let elimination to work on some simple expression types regardless of use count

This commit is contained in:
Emi Simpson 2024-03-17 10:19:50 -04:00
parent 67a7bbc821
commit 2479dbd9a6
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847

5
opt.py
View file

@ -10,8 +10,9 @@ Optimization: TypeAlias = Callable[[Expression], Option[Expression]]
def eliminate_single_let(expr: Expression) -> Option[Expression]:
match expr:
case LetBinding(lhs, rhs, body):
if count_uses(lhs, body) <= 1:
# RHS is used at most once i nthe body
rhs_is_simple = isinstance(rhs, Int | Variable | Builtin)
if count_uses(lhs, body) <= 1 or rhs_is_simple:
# RHS is used at most once i nthe body or replication wouldnt be costly
if count_uses(lhs, rhs) > 0:
# RHS is recursive
if not isinstance(body, Variable):