Optimization: Collapse consecutive constant additions

This commit is contained in:
Emi Simpson 2024-03-17 09:09:04 -04:00
parent 37c809f5c0
commit a2b68cc73e
Signed by: Emi
GPG key ID: A12F2C2FFDC3D847

8
opt.py
View file

@ -29,6 +29,12 @@ def eliminate_single_let(expr: Expression) -> Option[Expression]:
case _:
return None
def collapse_constant_additions(expr: Expression) -> Option[Expression]:
match expr:
case Application(Builtin(Builtin.BB_PLUS_CONST(x)), Application(Builtin(Builtin.BB_PLUS_CONST(y)), val)):
return Some(Application(Builtin(Builtin.BB_PLUS_CONST(x+y)), val))
return None
def apply_opts(optimizations: Collection[Optimization], expression: Expression) -> Tuple[Expression, int]:
count: int
optimized_expr: Expression
@ -77,4 +83,4 @@ def optimize_to_fixpoint(optimizations: Collection[Optimization], expression: Ex
return optimize_to_fixpoint(optimizations, optimized_expression)
raise Exception('Unreachable')
all_optimizations: Sequence[Optimization] = (eliminate_single_let,)
all_optimizations: Sequence[Optimization] = (eliminate_single_let, collapse_constant_additions)