From 70e5f31e4d5fa050447bb81b64f9573d421187e0 Mon Sep 17 00:00:00 2001 From: WormHeamer Date: Sat, 2 Aug 2025 03:21:04 -0400 Subject: more peepholing --- main.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index 2642fac..3ffd6cb 100644 --- a/main.c +++ b/main.c @@ -263,15 +263,26 @@ Node *node_peephole(Node *n, Proc *p, Lexer *l) { default: break; } } else { - if (node_op_communative(n->type) - && in[0]->type == N_LIT + /* TODO: figure out to do peepholes recursively, without fucking up the graph or having to clone everything */ + if (node_op_communative(n->type)) { + /* transformations to help encourage constant folding */ + if (in[0]->type == N_LIT && in[1]->type == n->type && in[1]->in.data[0]->type != N_LIT && in[1]->in.data[1]->type == N_LIT) { - /* op(lit, op(X, lit)) -> op(X, op(lit, lit)) */ - Node *tmp = in[1]->in.data[0]; - in[1]->in.data[0] = in[0]; - in[0] = tmp; + /* op(lit, op(X, lit)) -> op(X, op(lit, lit)) */ + Node *tmp = in[1]->in.data[0]; + in[1]->in.data[0] = in[0]; + in[0] = tmp; + } else if (in[0]->type == n->type + && in[0]->in.data[0]->type != N_LIT + && in[0]->in.data[1]->type == N_LIT + && in[1]->type != N_LIT) { + /* op(op(X, lit), Y) -> op(op(X, Y), lit) */ + Node *tmp = in[0]->in.data[1]; + in[0]->in.data[1] = in[1]; + in[1] = tmp; + } } } -- cgit v1.2.3