diff --git a/ir_fold.h b/ir_fold.h index b23ea832..38ea059a 100644 --- a/ir_fold.h +++ b/ir_fold.h @@ -1808,10 +1808,6 @@ IR_FOLD(MUL(_, C_ADDR)) IR_FOLD_COPY(op2); } else if (op2_insn->val.u64 == 1) { IR_FOLD_COPY(op1); - } else if (op2_insn->val.u64 == 2 && IR_OPT_TYPE(opt) != IR_ADDR) { - opt = IR_ADD | (opt & IR_OPT_TYPE_MASK); - op2 = op1; - IR_FOLD_RESTART; } IR_FOLD_NEXT; } @@ -1827,11 +1823,6 @@ IR_FOLD(MUL(_, C_I64)) } else if (op2_insn->val.i64 == 1) { /* a * 1 => a */ IR_FOLD_COPY(op1); - } else if (op2_insn->val.i64 == 2) { - /* a * 2 => a + a */ - opt = IR_ADD | (opt & IR_OPT_TYPE_MASK); - op2 = op1; - IR_FOLD_RESTART; } else if (op2_insn->val.i64 == -1) { /* a * -1 => -a */ opt = IR_NEG | (opt & IR_OPT_TYPE_MASK); @@ -2907,7 +2898,6 @@ IR_FOLD(ADD(SHR, SHL)) /* Swap operands (move lower ref to op2) for better CSE */ -IR_FOLD(ADD(_, _)) IR_FOLD(MUL(_, _)) IR_FOLD_NAMED(swap_ops) { @@ -2929,6 +2919,19 @@ IR_FOLD(MUL_OV(_, _)) IR_FOLD_EMIT; } +IR_FOLD(ADD(_, _)) +{ + if (IR_IS_TYPE_INT(IR_OPT_TYPE(opt)) && op1 == op2) { + /* a + a => a * 2 */ + IR_ASSERT(!IR_IS_CONST_REF(op1)); + val.u64 = 2; + opt = IR_MUL | (opt & IR_OPT_TYPE_MASK); + op2 = ir_const(ctx, val, IR_OPT_TYPE(opt)); + IR_FOLD_RESTART; + } + IR_FOLD_DO_NAMED(swap_ops); +} + IR_FOLD(SUB(_, _)) { if (IR_IS_TYPE_INT(IR_OPT_TYPE(opt)) && op1 == op2) { diff --git a/tests/007.irt b/tests/007.irt index d1b98ebf..49228e70 100644 --- a/tests/007.irt +++ b/tests/007.irt @@ -42,7 +42,7 @@ int32_t d_5 = PHI(l_4, c_7, d_7); int32_t d_6 = ADD(d_2, c_6); int32_t d_7 = ADD(d_6, d_5); - int32_t d_8 = ADD(d_7, d_7); + int32_t d_8 = MUL(d_7, c_5); bool d_9 = LT(d_7, c_4); l_10 = IF(l_4, d_9); l_11 = IF_TRUE(l_10); diff --git a/tests/014.irt b/tests/014.irt index 96fa8f87..f9432a59 100644 --- a/tests/014.irt +++ b/tests/014.irt @@ -30,20 +30,21 @@ bool c_2 = 0; bool c_3 = 1; int32_t c_4 = 10; - int32_t c_5 = 1; - int32_t c_6 = 0; + int32_t c_5 = 2; + int32_t c_6 = 1; + int32_t c_7 = 0; l_1 = START(l_14); int32_t d_2 = PARAM(l_1, "x", 0); - int32_t d_3 = ADD(d_2, c_5); + int32_t d_3 = ADD(d_2, c_6); l_4 = END(l_1); l_5 = LOOP_BEGIN(l_4, l_11); - int32_t d_6 = PHI(l_5, c_6, d_7); + int32_t d_6 = PHI(l_5, c_7, d_7); int32_t d_7 = ADD(d_6, d_3); bool d_8 = LT(d_7, c_4); l_9 = IF(l_5, d_8); l_10 = IF_TRUE(l_9); l_11 = LOOP_END(l_10); l_12 = IF_FALSE(l_9); - int32_t d_13 = ADD(d_7, d_7); + int32_t d_13 = MUL(d_7, c_5); l_14 = RETURN(l_12, d_13); } diff --git a/tests/023.irt b/tests/023.irt index 60b213bb..653a6555 100644 --- a/tests/023.irt +++ b/tests/023.irt @@ -31,20 +31,21 @@ bool c_2 = 0; bool c_3 = 1; int32_t c_4 = 10; - int32_t c_5 = 1; - int32_t c_6 = 0; + int32_t c_5 = 2; + int32_t c_6 = 1; + int32_t c_7 = 0; l_1 = START(l_14); int32_t d_2 = PARAM(l_1, "x", 0); - int32_t d_3 = ADD(d_2, c_5); + int32_t d_3 = ADD(d_2, c_6); l_4 = END(l_1); l_5 = LOOP_BEGIN(l_4, l_11); - int32_t d_6 = PHI(l_5, c_6, d_7); + int32_t d_6 = PHI(l_5, c_7, d_7); int32_t d_7 = ADD(d_6, d_3); bool d_8 = LT(d_7, c_4); l_9 = IF(l_5, d_8); l_10 = IF_TRUE(l_9); l_11 = LOOP_END(l_10); l_12 = IF_FALSE(l_9); - int32_t d_13 = ADD(d_7, d_7); + int32_t d_13 = MUL(d_7, c_5); l_14 = RETURN(l_12, d_13); }