Skip to content

Commit dc62ed4

Browse files
committed
Add copy propagation optimization
Implement copy propagation within basic blocks to track constant assignments that can be propagated to their uses, marking destination variables with constant values when they receive assignments from constants. When processing OP_assign instructions with constant sources: - Check that destination is non-global to avoid unsafe optimizations - Mark destination variable as constant with is_const=true - Copy the constant value to destination's init_val field
1 parent 0cbe9a2 commit dc62ed4

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/ssa.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,9 @@ void dce_sweep(void)
15271527

15281528
void build_reversed_rpo();
15291529

1530+
/* Forward declaration for optimization function */
1531+
void simple_copy_propagation(basic_block_t *bb);
1532+
15301533
void optimize(void)
15311534
{
15321535
/* build rdf information for DCE */
@@ -1541,6 +1544,9 @@ void optimize(void)
15411544
/* basic block level (control flow) optimizations */
15421545

15431546
for (basic_block_t *bb = func->bbs; bb; bb = bb->rpo_next) {
1547+
/* Apply copy propagation optimization */
1548+
simple_copy_propagation(bb);
1549+
15441550
/* instruction level optimizations */
15451551
for (insn_t *insn = bb->insn_list.head; insn; insn = insn->next) {
15461552
/* record the instruction assigned value to rd */
@@ -1772,3 +1778,19 @@ void liveness_analysis(void)
17721778
} while (changed);
17731779
}
17741780
}
1781+
1782+
/* Simple copy propagation optimization within basic blocks */
1783+
void simple_copy_propagation(basic_block_t *bb)
1784+
{
1785+
for (insn_t *insn = bb->insn_list.head; insn; insn = insn->next) {
1786+
/* Track constant assignments */
1787+
if (insn->opcode == OP_assign && insn->rs1 && insn->rs1->is_const) {
1788+
/* This is a constant assignment - could be propagated */
1789+
if (insn->rd && !insn->rd->is_global) {
1790+
/* Mark the destination for potential propagation */
1791+
insn->rd->is_const = true;
1792+
insn->rd->init_val = insn->rs1->init_val;
1793+
}
1794+
}
1795+
}
1796+
}

0 commit comments

Comments
 (0)