Skip to content

Commit bca3eec

Browse files
committed
Add dead store elimination optimization
Implement basic dead store elimination that identifies stores that are immediately overwritten within the same basic block before any loads occur. The optimization scans store and global_store instructions and tracks: - Whether a load occurs before another store to the same location - If another store overwrites the value before it is used
1 parent a29b6a8 commit bca3eec

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/defs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ struct var {
316316
bool is_ternary_ret;
317317
bool is_logical_ret;
318318
bool is_const; /* whether a constant representaion or not */
319+
bool is_dead; /* whether this variable is dead (never used) */
319320
};
320321

321322
typedef struct {

src/ssa.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,7 @@ void build_reversed_rpo();
15301530
/* Forward declarations for optimization functions */
15311531
void simple_copy_propagation(basic_block_t *bb);
15321532
void redundant_load_elimination(basic_block_t *bb);
1533+
void dead_store_elimination(basic_block_t *bb);
15331534

15341535
void optimize(void)
15351536
{
@@ -1548,6 +1549,7 @@ void optimize(void)
15481549
/* Apply optimization passes */
15491550
simple_copy_propagation(bb);
15501551
redundant_load_elimination(bb);
1552+
dead_store_elimination(bb);
15511553

15521554
/* instruction level optimizations */
15531555
for (insn_t *insn = bb->insn_list.head; insn; insn = insn->next) {
@@ -1820,3 +1822,38 @@ void redundant_load_elimination(basic_block_t *bb)
18201822
}
18211823
}
18221824
}
1825+
1826+
1827+
/* Dead store elimination - remove stores that are never used */
1828+
void dead_store_elimination(basic_block_t *bb)
1829+
{
1830+
for (insn_t *insn = bb->insn_list.head; insn; insn = insn->next) {
1831+
if (insn->opcode == OP_store || insn->opcode == OP_global_store) {
1832+
if (!insn->rd)
1833+
continue;
1834+
1835+
/* Check if this store is immediately overwritten */
1836+
bool is_dead = true;
1837+
for (insn_t *next = insn->next; next; next = next->next) {
1838+
/* If we find a load before another store, it's not dead */
1839+
if ((next->opcode == OP_load ||
1840+
next->opcode == OP_global_load) &&
1841+
next->rs1 == insn->rd) {
1842+
is_dead = false;
1843+
break;
1844+
}
1845+
/* If we find another store to the same location, this store is
1846+
* dead */
1847+
if ((next->opcode == OP_store ||
1848+
next->opcode == OP_global_store) &&
1849+
next->rd == insn->rd) {
1850+
break;
1851+
}
1852+
}
1853+
1854+
if (is_dead && insn->rd) {
1855+
insn->rd->is_dead = true;
1856+
}
1857+
}
1858+
}
1859+
}

0 commit comments

Comments
 (0)