diff options
| author | WormHeamer | 2025-08-05 01:44:20 -0400 |
|---|---|---|
| committer | WormHeamer | 2025-08-05 01:44:20 -0400 |
| commit | 2069733522922fc45cc87da0327e6dfcda74f1b2 (patch) | |
| tree | b3af34d102c262c84b37e37b73481f9fa2f111dc /ir.c | |
| parent | def1b9179981081f67ed654f65ef22a55f6cdcb3 (diff) | |
correction! equality is communative, but not _associative_
so constants can still be shuffled from left to right, not(X) from
right to left, but the regrouping optimizations don't apply
Diffstat (limited to 'ir.c')
| -rw-r--r-- | ir.c | 22 |
1 files changed, 16 insertions, 6 deletions
@@ -245,6 +245,14 @@ Node *node_new_lit_bool(Proc *p, int b) { } static inline int node_op_communative(NodeType t) { + NodeType ops[] = { N_OP_ADD, N_OP_MUL, N_OP_AND, N_OP_XOR, N_OP_OR, N_CMP_EQL, N_CMP_NEQ }; + for (unsigned i = 0; i < sizeof ops / sizeof *ops; i++) { + if (ops[i] == t) return 1; + } + return 0; +} + +static inline int node_op_associative(NodeType t) { NodeType ops[] = { N_OP_ADD, N_OP_MUL, N_OP_AND, N_OP_XOR, N_OP_OR }; for (unsigned i = 0; i < sizeof ops / sizeof *ops; i++) { if (ops[i] == t) return 1; @@ -487,6 +495,14 @@ Node *node_idealize(Node *n, Proc *p, Lexer *l) { /* op(lit, X) -> op(X, lit) */ if (T(CAR(n), N_LIT) && !T(CDR(n), N_LIT)) return OP(CDR(n), CAR(n)); + /* op(X, not(Y)) -> op(not(Y), X) */ + /* shuffle not left to avoid conflict w/ literals */ + if (T(CDR(n), N_OP_NOT) && !T(CAR(n), N_OP_NOT)) { + return NODE(n->type, CDR(n), CAR(n)); + } + } + + if (node_op_associative(n->type)) { /* op(X, op(Y,Z)) -> op(op(Y,Z), X) */ /*if (!T(CAR(n), n->type) && T(CDR(n), n->type) && C(CAR(n), CDAR(n))) return OP(CDR(n), CAR(n));*/ @@ -510,12 +526,6 @@ Node *node_idealize(Node *n, Proc *p, Lexer *l) { && C(CADR(n), CDR(n))) { return OP(OP(CAAR(n), CDR(n)), CADR(n)); } - - /* op(X, not(Y)) -> op(not(Y), X) */ - /* shuffle not left to avoid conflict w/ literals */ - if (T(CDR(n), N_OP_NOT) && !T(CAR(n), N_OP_NOT)) { - return NODE(n->type, CDR(n), CAR(n)); - } } /* optimize based on situations where the input is partly known (e.g. |
