Skip to content

Commit 25c3e03

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 23d3399 commit 25c3e03

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
@@ -1522,6 +1522,9 @@ void dce_sweep(void)
15221522

15231523
void build_reversed_rpo();
15241524

1525+
/* Forward declaration for optimization function */
1526+
void simple_copy_propagation(basic_block_t *bb);
1527+
15251528
void optimize(void)
15261529
{
15271530
/* build rdf information for DCE */
@@ -1536,6 +1539,9 @@ void optimize(void)
15361539
/* basic block level (control flow) optimizations */
15371540

15381541
for (basic_block_t *bb = func->bbs; bb; bb = bb->rpo_next) {
1542+
/* Apply copy propagation optimization */
1543+
simple_copy_propagation(bb);
1544+
15391545
/* instruction level optimizations */
15401546
for (insn_t *insn = bb->insn_list.head; insn; insn = insn->next) {
15411547
/* record the instruction assigned value to rd */
@@ -1767,3 +1773,19 @@ void liveness_analysis(void)
17671773
} while (changed);
17681774
}
17691775
}
1776+
1777+
/* Simple copy propagation optimization within basic blocks */
1778+
void simple_copy_propagation(basic_block_t *bb)
1779+
{
1780+
for (insn_t *insn = bb->insn_list.head; insn; insn = insn->next) {
1781+
/* Track constant assignments */
1782+
if (insn->opcode == OP_assign && insn->rs1 && insn->rs1->is_const) {
1783+
/* This is a constant assignment - could be propagated */
1784+
if (insn->rd && !insn->rd->is_global) {
1785+
/* Mark the destination for potential propagation */
1786+
insn->rd->is_const = true;
1787+
insn->rd->init_val = insn->rs1->init_val;
1788+
}
1789+
}
1790+
}
1791+
}

0 commit comments

Comments
 (0)