Fix fusion of IF(_, CMP(AND(_, _) 0))

This commit is contained in:
Dmitry Stogov 2023-03-28 19:03:06 +03:00
parent 2940f9e281
commit e4b618ad00
5 changed files with 117 additions and 3 deletions

View File

@ -1634,12 +1634,12 @@ store_int:
} else { } else {
ir_match_fuse_load(ctx, op1_insn->op2, bb); ir_match_fuse_load(ctx, op1_insn->op2, bb);
} }
if (op1_insn->op == IR_AND && ctx->use_lists[insn->op1].count == 1) { if (op1_insn->op == IR_AND && ctx->use_lists[op2_insn->op1].count == 1) {
if (IR_IS_CONST_REF(op1_insn->op2)) { if (IR_IS_CONST_REF(op1_insn->op2)) {
ir_match_fuse_load(ctx, op1_insn->op1, bb); ir_match_fuse_load(ctx, op1_insn->op1, bb);
} }
ctx->rules[op2_insn->op1] = IR_TEST_INT; ctx->rules[op2_insn->op1] = IR_SKIP_TEST_INT;
ctx->rules[insn->op2] = IR_SKIP_CMP_INT; ctx->rules[insn->op2] = IR_SKIP;
return IR_TEST_AND_BRANCH_INT; return IR_TEST_AND_BRANCH_INT;
} else { } else {
ctx->rules[op2_insn->op1] = IR_BINOP_INT; ctx->rules[op2_insn->op1] = IR_BINOP_INT;

29
tests/x86/test_003.irt Normal file
View File

@ -0,0 +1,29 @@
--TEST--
003: IF(EQ(AND(_,_), 0)) -> TEST
--TARGET--
x86
--ARGS--
-S
--CODE--
{
int32_t c1 = 0;
int32_t c2 = 3;
l_1 = START(ret);
int32_t x = PARAM(l_1, "x", 1);
int32_t d_1 = AND(x, c2);
bool d_2 = EQ(d_1, c1);
l_2 = IF(l_1, d_2);
l_3 = IF_TRUE(l_2);
ret1 = RETURN(l_3, c1);
l_5 = IF_FALSE(l_2);
ret = RETURN(l_5, c2, ret1);
}
--EXPECT--
test:
testl $3, 4(%esp)
jne .L1
xorl %eax, %eax
retl
.L1:
movl $3, %eax
retl

28
tests/x86/test_004.irt Normal file
View File

@ -0,0 +1,28 @@
--TEST--
004: IF(AND(_,_)) -> TEST
--TARGET--
x86
--ARGS--
-S
--CODE--
{
int32_t c1 = 0;
int32_t c2 = 3;
l_1 = START(ret);
int32_t x = PARAM(l_1, "x", 1);
int32_t d_1 = AND(x, c2);
l_2 = IF(l_1, d_1);
l_3 = IF_TRUE(l_2);
ret1 = RETURN(l_3, c1);
l_5 = IF_FALSE(l_2);
ret = RETURN(l_5, c2, ret1);
}
--EXPECT--
test:
testl $3, 4(%esp)
je .L1
xorl %eax, %eax
retl
.L1:
movl $3, %eax
retl

29
tests/x86_64/test_003.irt Normal file
View File

@ -0,0 +1,29 @@
--TEST--
003: IF(EQ(AND(_,_), 0)) -> TEST
--TARGET--
x86_64
--ARGS--
-S
--CODE--
{
int32_t c1 = 0;
int32_t c2 = 3;
l_1 = START(ret);
int32_t x = PARAM(l_1, "x", 1);
int32_t d_1 = AND(x, c2);
bool d_2 = EQ(d_1, c1);
l_2 = IF(l_1, d_2);
l_3 = IF_TRUE(l_2);
ret1 = RETURN(l_3, c1);
l_5 = IF_FALSE(l_2);
ret = RETURN(l_5, c2, ret1);
}
--EXPECT--
test:
testl $3, %edi
jne .L1
xorl %eax, %eax
retq
.L1:
movl $3, %eax
retq

28
tests/x86_64/test_004.irt Normal file
View File

@ -0,0 +1,28 @@
--TEST--
004: IF(AND(_,_)) -> TEST
--TARGET--
x86_64
--ARGS--
-S
--CODE--
{
int32_t c1 = 0;
int32_t c2 = 3;
l_1 = START(ret);
int32_t x = PARAM(l_1, "x", 1);
int32_t d_1 = AND(x, c2);
l_2 = IF(l_1, d_1);
l_3 = IF_TRUE(l_2);
ret1 = RETURN(l_3, c1);
l_5 = IF_FALSE(l_2);
ret = RETURN(l_5, c2, ret1);
}
--EXPECT--
test:
testl $3, %edi
je .L1
xorl %eax, %eax
retq
.L1:
movl $3, %eax
retq