From ff64edc488c5b7b44dfecfecc03fb4cc26c4da19 Mon Sep 17 00:00:00 2001 From: WormHeamer Date: Mon, 4 Aug 2025 01:18:46 -0400 Subject: use lisp acronyms to express peephole optimizations --- ir.c | 55 +++++++++++++++++++++++++------------------------------ 1 file changed, 25 insertions(+), 30 deletions(-) (limited to 'ir.c') diff --git a/ir.c b/ir.c index 4e7f84e..ff61c7a 100644 --- a/ir.c +++ b/ir.c @@ -311,6 +311,13 @@ void node_peephole_in(Node *n, int idx, Proc *p, Lexer *l) { } #define NODE(...) node_peephole(node_new(p, __VA_ARGS__), p, l) +#define OP(...) NODE(n->type, __VA_ARGS__) +#define CAR(n) n->in.data[0] +#define CDR(n) n->in.data[1] +#define CAAR(n) CAR(CAR(n)) +#define CADR(n) CDR(CAR(n)) +#define CDAR(n) CAR(CDR(n)) +#define CDDR(n) CDR(CDR(n)) /* needs lexer for error reporting */ Node *node_idealize(Node *n, Proc *p, Lexer *l) { @@ -328,57 +335,45 @@ Node *node_idealize(Node *n, Proc *p, Lexer *l) { } } - Node **in = n->in.data; /* 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 */ /* the overall trend is to move them rightwards */ - if (in[0]->type == N_LIT && in[1]->type != N_LIT) { + if (CAR(n)->type == N_LIT && CDR(n)->type != N_LIT) { fprintf(stderr, "op(lit, X) -> op(X, lit)\n"); - return NODE(n->type, in[1], in[0]); + return OP(CDR(n), CAR(n)); } - if (in[1]->type == n->type && in[0]->type != n->type) { + if (CDR(n)->type == n->type && CAR(n)->type != n->type) { fprintf(stderr, "op(X, op(Y, Z)) -> op(op(Y, Z), X)\n"); - return NODE(n->type, in[1], in[0]); + return OP(CDR(n), CAR(n)); } - if (in[0]->type == n->type - && in[1]->type == n->type - && in[1]->in.data[1]->type == N_LIT) { + if (CAR(n)->type == n->type + && CDR(n)->type == n->type + && CDDR(n)->type == N_LIT) { fprintf(stderr, "op(op(X, Y), op(Z, lit)) -> op(op(X, op(Y, Z)), lit)\n"); - return NODE(n->type, - NODE(n->type, - in[0]->in.data[0], - NODE(n->type, - in[0]->in.data[1], - in[1]->in.data[0])), - in[1]->in.data[1]); + return OP(OP(CAAR(n), OP(CADR(n), CDAR(n))), CDDR(n)); } - if (in[1]->type == N_LIT - && in[0]->type == n->type - && in[0]->in.data[0]->type != N_LIT - && in[0]->in.data[1]->type == N_LIT) { + if (CDR(n)->type == N_LIT + && CAR(n)->type == n->type + && CAAR(n)->type != N_LIT + && CADR(n)->type == N_LIT) { fprintf(stderr, "op(op(X, lit), lit) -> op(X, op(lit, lit))\n"); - return NODE(n->type, - in[0]->in.data[0], - NODE(n->type, in[0]->in.data[1], in[1])); + return OP(CAAR(n), OP(CADR(n), CDR(n))); } /* op(op(X, lit), Y) -> op(op(X, Y), lit) */ - 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) { + if (CAR(n)->type == n->type + && CAAR(n)->type != N_LIT + && CADR(n)->type == N_LIT + && CDR(n)->type != N_LIT) { fprintf(stderr, "op(op(X, lit), Y) -> op(op(X, Y), lit)\n"); - - return NODE(n->type, - NODE(n->type, in[0]->in.data[0], in[1]), - in[0]->in.data[1]); + return OP(OP(CAAR(n), CDR(n)), CADR(n)); } } -- cgit v1.2.3