From 0298b32e38a53a9cd39fadce8acbde12e3d6b187 Mon Sep 17 00:00:00 2001 From: Jakub Doka Date: Mon, 21 Oct 2024 17:29:11 +0200 Subject: [PATCH] sniping a peephole --- lang/src/son.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lang/src/son.rs b/lang/src/son.rs index 2ea5842..b0a8999 100644 --- a/lang/src/son.rs +++ b/lang/src/son.rs @@ -305,6 +305,19 @@ impl Nodes { return Some(self.new_node(ty, K::BinOp { op: T::Mul }, [ctrl, rhs, new_rhs])); } + if op == T::Sub + && self[lhs].kind == (K::BinOp { op: T::Add }) + && let K::CInt { value: a } = self[rhs].kind + && let K::CInt { value: b } = self[self[lhs].inputs[2]].kind + { + let new_rhs = self.new_node_nop(ty, K::CInt { value: b - a }, [ctrl]); + return Some(self.new_node(ty, K::BinOp { op: T::Add }, [ + ctrl, + self[lhs].inputs[1], + new_rhs, + ])); + } + if op == T::Sub && self[lhs].kind == (K::BinOp { op }) { // (a - b) - c => a - (b + c) let &[_, a, b] = self[lhs].inputs.as_slice() else { unreachable!() };