diff options
| -rw-r--r-- | main.c | 23 |
1 files changed, 17 insertions, 6 deletions
@@ -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; + } } } |
