~james/remy

perf(optimiser): fold neutral arithmetic and bitwise operations

commit
0c936dff3ac8cddb8c0c8a477151e4a03d4c40b6 browse tree
parent
974b16e2aca2567ecf0d3f566f3287566f49c530
author
james <j@cpl0.zip>
signature
Verified ~james signed with a key registered by ~james
date
2026-09-05 11:00 UTC

3 files changed, +17 −2

src/Examples.thy modified

@@ -27,6 +27,14 @@ lemma constant_fold:
2727 "optimise (Binary Add (Literal (-1)) (Literal 1)) = Literal 0"
2828 by eval
2929
30+lemma neutral_arithmetic:
31+ "optimise (Binary Mul (Binary Add (Input X) (Literal 0)) (Literal 1)) = Input X"
32+ by eval
33+
34+lemma neutral_bits:
35+ "optimise (Binary And (Binary Xor (Input Y) (Literal 0)) (Literal (-1))) = Input Y"
36+ by eval
37+
3038 lemma dead_branch:
3139 "optimise (Cond (Literal 0) (Input X) (Input Y)) = Input Y"
3240 by eval

src/Model.thy modified

@@ -44,7 +44,12 @@ fun scoped :: "nat \<Rightarrow> expression \<Rightarrow> bool" where
4444
4545 fun fold_binary :: "operator \<Rightarrow> expression \<Rightarrow> expression \<Rightarrow> expression" where
4646 "fold_binary op (Literal a) (Literal b) = Literal (operate op a b)"
47-| "fold_binary op a b = Binary op a b"
47+| "fold_binary op a b = (if b = Literal 0 \<and> op \<in> {Add,Sub,Or,Xor,Shl,Shr} then a
48+ else if a = Literal 0 \<and> op \<in> {Add,Or,Xor} then b
49+ else if b = Literal 1 \<and> op = Mul then a
50+ else if a = Literal 1 \<and> op = Mul then b
51+ else if b = Literal (-1) \<and> op = And then a
52+ else if a = Literal (-1) \<and> op = And then b else Binary op a b)"
4853
4954 fun fold_cond :: "expression \<Rightarrow> expression \<Rightarrow> expression \<Rightarrow> expression" where
5055 "fold_cond (Literal c) a b = (if c = 0 then b else a)"
@@ -58,7 +63,7 @@ fun optimise :: "expression \<Rightarrow> expression" where
5863
5964 lemma fold_binary_value:
6065 "evaluate (fold_binary op a b) env inputs = operate op (evaluate a env inputs) (evaluate b env inputs)"
61- by (cases a; cases b) auto
66+ by (cases op; cases a; cases b) auto
6267
6368 lemma fold_cond_value:
6469 "evaluate (fold_cond c a b) env inputs = (if evaluate c env inputs = 0 then evaluate b env inputs else evaluate a env inputs)"

tests/native.py modified

@@ -65,6 +65,8 @@ def main():
6565 raise RuntimeError("native differential tests require an x86-64 Linux host")
6666 rng = random.Random(0x52454D59)
6767 cases = [(op, "x", "y") for op in OPERATORS]
68+ cases += [(op, "x", 0) for op in ("+", "-", "or", "xor", "shl", "shr")]
69+ cases += [("*", "x", 1), ("*", 1, "x"), ("and", "x", MASK)]
6870 cases += [("let", "a", ("+", "x", 3), ("+", "y", ("let", "b", "t", ("*", "a", "b")))),
6971 ("let", "x", "y", ("if", "t", ("+", "x", 1), ("-", "x", 1)))]
7072 cases += [generate(rng, 5, ["x", "y", "t"]) for _ in range(117)]