perf(optimiser): collapse conditionals with identical branches
3 files changed, +6 −1
- src/Examples.thy modified +4 −0
- src/Model.thy modified +1 −1
- tests/native.py modified +1 −0
src/Examples.thy modified
| @@ -47,6 +47,10 @@ lemma dead_branch: | ||
| 47 | 47 | "optimise (Cond (Literal 0) (Input X) (Input Y)) = Input Y" |
| 48 | 48 | by eval |
| 49 | 49 | |
| 50 | +lemma equal_branches: | |
| 51 | + "optimise (Cond (Input T) (Input X) (Input X)) = Input X" | |
| 52 | + by eval | |
| 53 | + | |
| 50 | 54 | lemma literal_code: |
| 51 | 55 | "executable (Literal 42) = [72,184,42,0,0,0,0,0,0,0,80,88,195]" |
| 52 | 56 | by eval |
src/Model.thy modified
| @@ -57,7 +57,7 @@ fun fold_binary :: "operator \<Rightarrow> expression \<Rightarrow> expression \ | ||
| 57 | 57 | |
| 58 | 58 | fun fold_cond :: "expression \<Rightarrow> expression \<Rightarrow> expression \<Rightarrow> expression" where |
| 59 | 59 | "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)" | |
| 61 | 61 | |
| 62 | 62 | fun optimise :: "expression \<Rightarrow> expression" where |
| 63 | 63 | "optimise (Binary op a b) = fold_binary op (optimise a) (optimise b)" |
tests/native.py modified
| @@ -68,6 +68,7 @@ def main(): | ||
| 68 | 68 | cases += [(op, "x", 0) for op in ("+", "-", "or", "xor", "shl", "shr")] |
| 69 | 69 | cases += [("*", "x", 1), ("*", 1, "x"), ("and", "x", MASK)] |
| 70 | 70 | cases += [("*", "x", 0), ("and", "x", 0), ("shl", 0, "y"), ("shr", 0, "y"), ("or", "x", MASK)] |
| 71 | + cases += [("if", "x", ("+", "y", "t"), ("+", "y", "t"))] | |
| 71 | 72 | cases += [("let", "a", ("+", "x", 3), ("+", "y", ("let", "b", "t", ("*", "a", "b")))), |
| 72 | 73 | ("let", "x", "y", ("if", "t", ("+", "x", 1), ("-", "x", 1)))] |
| 73 | 74 | cases += [generate(rng, 5, ["x", "y", "t"]) for _ in range(117)] |