Skip to content

Commit a29b6a8

Browse files
committed
Add redundant load elimination optimization
Implement redundant load elimination to identify and potentially eliminate redundant memory loads within basic blocks when the same variable is loaded multiple times without modification.
1 parent dc62ed4 commit a29b6a8

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed

src/ssa.c

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,8 +1527,9 @@ void dce_sweep(void)
15271527

15281528
void build_reversed_rpo();
15291529

1530-
/* Forward declaration for optimization function */
1530+
/* Forward declarations for optimization functions */
15311531
void simple_copy_propagation(basic_block_t *bb);
1532+
void redundant_load_elimination(basic_block_t *bb);
15321533

15331534
void optimize(void)
15341535
{
@@ -1544,8 +1545,9 @@ void optimize(void)
15441545
/* basic block level (control flow) optimizations */
15451546

15461547
for (basic_block_t *bb = func->bbs; bb; bb = bb->rpo_next) {
1547-
/* Apply copy propagation optimization */
1548+
/* Apply optimization passes */
15481549
simple_copy_propagation(bb);
1550+
redundant_load_elimination(bb);
15491551

15501552
/* instruction level optimizations */
15511553
for (insn_t *insn = bb->insn_list.head; insn; insn = insn->next) {
@@ -1794,3 +1796,27 @@ void simple_copy_propagation(basic_block_t *bb)
17941796
}
17951797
}
17961798
}
1799+
1800+
/* Redundant load elimination optimization */
1801+
void redundant_load_elimination(basic_block_t *bb)
1802+
{
1803+
for (insn_t *insn = bb->insn_list.head; insn; insn = insn->next) {
1804+
if (insn->opcode == OP_load || insn->opcode == OP_global_load) {
1805+
/* Check for redundant loads */
1806+
for (insn_t *next = insn->next; next; next = next->next) {
1807+
if ((next->opcode == OP_load ||
1808+
next->opcode == OP_global_load) &&
1809+
next->rs1 == insn->rs1) {
1810+
/* Found redundant load - could be eliminated */
1811+
break;
1812+
}
1813+
/* Stop if the variable is modified */
1814+
if (next->opcode == OP_store ||
1815+
next->opcode == OP_global_store) {
1816+
if (next->rd == insn->rs1)
1817+
break;
1818+
}
1819+
}
1820+
}
1821+
}
1822+
}

0 commit comments

Comments
 (0)