Skip to content

Commit 8816b36

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 a9317c3 commit 8816b36

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
@@ -313,6 +313,7 @@ struct var {
313313
bool is_ternary_ret;
314314
bool is_logical_ret;
315315
bool is_const; /* whether a constant representaion or not */
316+
bool is_dead; /* whether this variable is dead (never used) */
316317
};
317318

318319
typedef struct {

src/ssa.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,7 @@ void build_reversed_rpo();
15251525
/* Forward declarations for optimization functions */
15261526
void simple_copy_propagation(basic_block_t *bb);
15271527
void redundant_load_elimination(basic_block_t *bb);
1528+
void dead_store_elimination(basic_block_t *bb);
15281529

15291530
void optimize(void)
15301531
{
@@ -1543,6 +1544,7 @@ void optimize(void)
15431544
/* Apply optimization passes */
15441545
simple_copy_propagation(bb);
15451546
redundant_load_elimination(bb);
1547+
dead_store_elimination(bb);
15461548

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

0 commit comments

Comments
 (0)