Skip to content

Commit

Permalink
Optimize ADD(x, x) into MUL(x, 2), instead of MUL(x,2) -> ADD(x, x)
Browse files Browse the repository at this point in the history
Back-ends emit ADD for MUL(_, 2) anyway.
  • Loading branch information
dstogov committed Dec 27, 2024
1 parent 82ee409 commit 36a2349
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
23 changes: 13 additions & 10 deletions ir_fold.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down Expand Up @@ -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)
{
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion tests/007.irt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions tests/014.irt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
11 changes: 6 additions & 5 deletions tests/023.irt
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 36a2349

Please sign in to comment.