diff options
| author | WormHeamer | 2025-08-04 01:18:46 -0400 |
|---|---|---|
| committer | WormHeamer | 2025-08-04 01:18:46 -0400 |
| commit | ff64edc488c5b7b44dfecfecc03fb4cc26c4da19 (patch) | |
| tree | c81f267b7524748e699c962ebbcd8b364aa1f2be | |
| parent | e47ae0caca1d27057c016d904e2dc15a30310b56 (diff) | |
use lisp acronyms to express peephole optimizations
| -rw-r--r-- | ir.c | 55 | ||||
| -rw-r--r-- | test.lang | 4 |
2 files changed, 28 insertions, 31 deletions
@@ -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)); } } @@ -9,5 +9,7 @@ // also single-line now proc main(a i64, b i64) { - return (4 + (3 * (a + 2 + b + 4) * 5) + 8) < 3 + a := a + 1 + b := b + 2 + return a * b } |
