Skip to content

Commit

Permalink
Fixed canonicalization bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
RealNeGate committed Jan 15, 2023
1 parent 4f1c61c commit 4c5c4e8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/tb/opt/canonical.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,21 @@ static bool inst_combine(TB_Function* f) {
TB_Node* a = &f->nodes[n->cmp.a];
TB_Node* b = &f->nodes[n->cmp.b];

// (cmpeq (cmpeq a 0) 0) => a
if (n->type == TB_CMP_EQ && tb_node_is_constant_zero(f, n->cmp.b) &&
a->type == TB_CMP_EQ && tb_node_is_constant_zero(f, a->cmp.b)) {
// (cmpeq (cmpeq a 0) 0) => (cmpeq a 0)
OPTIMIZER_LOG(r, "removed redundant comparisons");

n->type = TB_PASS;
n->pass.value = a->cmp.a;
} else if (n->type == TB_CMP_NE && tb_node_is_constant_zero(f, n->cmp.b) &&
a->type == TB_CMP_EQ && tb_node_is_constant_zero(f, a->cmp.b)) {
// (cmpeq (cmpeq a 0) 0) => (cmpne a 0)
OPTIMIZER_LOG(r, "removed redundant comparisons");

n->type = TB_PASS;
n->pass.value = a->cmp.a;
a->type = TB_CMP_NE;
} else {
// Sometimes we promote some types up when we don't need to
// (cmp (sxt/zxt A) (int B))
Expand Down
1 change: 0 additions & 1 deletion src/tb/opt/fold.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ static bool is_associative(TB_NodeTypeEnum type) {
switch (type) {
case TB_ADD: case TB_MUL:
case TB_AND: case TB_XOR: case TB_OR:
case TB_CMP_NE: case TB_CMP_EQ:
return true;

default:
Expand Down

0 comments on commit 4c5c4e8

Please sign in to comment.