~james/remy

perf(optimiser): collapse conditionals with identical branches

commit
a08d052613b63f659b70120516674bcf96f4684d browse tree
parent
3e3f52cd4b5a7e127a8a6834b895a5537dcf5b47
author
james <j@cpl0.zip>
signature
Verified ~james signed with a key registered by ~james
date
2026-09-10 11:00 UTC

3 files changed, +6 −1

src/Examples.thy modified

@@ -47,6 +47,10 @@ lemma dead_branch:
4747 "optimise (Cond (Literal 0) (Input X) (Input Y)) = Input Y"
4848 by eval
4949
50+lemma equal_branches:
51+ "optimise (Cond (Input T) (Input X) (Input X)) = Input X"
52+ by eval
53+
5054 lemma literal_code:
5155 "executable (Literal 42) = [72,184,42,0,0,0,0,0,0,0,80,88,195]"
5256 by eval

src/Model.thy modified

@@ -57,7 +57,7 @@ fun fold_binary :: "operator \<Rightarrow> expression \<Rightarrow> expression \
5757
5858 fun fold_cond :: "expression \<Rightarrow> expression \<Rightarrow> expression \<Rightarrow> expression" where
5959 "fold_cond (Literal c) a b = (if c = 0 then b else a)"
60-| "fold_cond c a b = Cond c a b"
60+| "fold_cond c a b = (if a = b then a else Cond c a b)"
6161
6262 fun optimise :: "expression \<Rightarrow> expression" where
6363 "optimise (Binary op a b) = fold_binary op (optimise a) (optimise b)"

tests/native.py modified

@@ -68,6 +68,7 @@ def main():
6868 cases += [(op, "x", 0) for op in ("+", "-", "or", "xor", "shl", "shr")]
6969 cases += [("*", "x", 1), ("*", 1, "x"), ("and", "x", MASK)]
7070 cases += [("*", "x", 0), ("and", "x", 0), ("shl", 0, "y"), ("shr", 0, "y"), ("or", "x", MASK)]
71+ cases += [("if", "x", ("+", "y", "t"), ("+", "y", "t"))]
7172 cases += [("let", "a", ("+", "x", 3), ("+", "y", ("let", "b", "t", ("*", "a", "b")))),
7273 ("let", "x", "y", ("if", "t", ("+", "x", 1), ("-", "x", 1)))]
7374 cases += [generate(rng, 5, ["x", "y", "t"]) for _ in range(117)]