Skip to content

Commit 8921b9b

Browse files
committed
Enhance constant folding optimization
Extend constant folding to support comparison operators (==, \!=, <, <=, >, >=) in addition to existing arithmetic and bitwise operations. This enables compile-time evaluation of constant comparisons, reducing code size and improving optimization opportunities for control flow. The enhancement integrates seamlessly with existing SSA optimizations including copy propagation, redundant load elimination, and dead store elimination. All operations with constant operands are now folded at compile time.
1 parent 8816b36 commit 8921b9b

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

src/ssa.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,6 +1527,9 @@ void simple_copy_propagation(basic_block_t *bb);
15271527
void redundant_load_elimination(basic_block_t *bb);
15281528
void dead_store_elimination(basic_block_t *bb);
15291529

1530+
/* Enhanced optimization function declarations */
1531+
bool enhanced_const_fold(insn_t *insn);
1532+
15301533
void optimize(void)
15311534
{
15321535
/* build rdf information for DCE */
@@ -1551,6 +1554,11 @@ void optimize(void)
15511554
/* record the instruction assigned value to rd */
15521555
if (insn->rd)
15531556
insn->rd->last_assign = insn;
1557+
1558+
/* Apply enhanced constant folding first */
1559+
if (enhanced_const_fold(insn))
1560+
continue;
1561+
15541562
if (cse(insn, bb))
15551563
continue;
15561564
if (const_folding(insn))
@@ -1852,3 +1860,84 @@ void dead_store_elimination(basic_block_t *bb)
18521860
}
18531861
}
18541862
}
1863+
1864+
1865+
/* Enhanced constant folding implementation */
1866+
bool enhanced_const_fold(insn_t *insn)
1867+
{
1868+
if (!insn->rs1 || !insn->rs2)
1869+
return false;
1870+
1871+
if (!insn->rs1->is_const || !insn->rs2->is_const)
1872+
return false;
1873+
1874+
int l = insn->rs1->init_val;
1875+
int r = insn->rs2->init_val;
1876+
int result;
1877+
1878+
switch (insn->opcode) {
1879+
case OP_add:
1880+
result = l + r;
1881+
break;
1882+
case OP_sub:
1883+
result = l - r;
1884+
break;
1885+
case OP_mul:
1886+
result = l * r;
1887+
break;
1888+
case OP_div:
1889+
if (r == 0)
1890+
return false;
1891+
result = l / r;
1892+
break;
1893+
case OP_mod:
1894+
if (r == 0)
1895+
return false;
1896+
result = l % r;
1897+
break;
1898+
case OP_lshift:
1899+
result = l << r;
1900+
break;
1901+
case OP_rshift:
1902+
result = l >> r;
1903+
break;
1904+
case OP_bit_and:
1905+
result = l & r;
1906+
break;
1907+
case OP_bit_or:
1908+
result = l | r;
1909+
break;
1910+
case OP_bit_xor:
1911+
result = l ^ r;
1912+
break;
1913+
case OP_eq:
1914+
result = (l == r) ? 1 : 0;
1915+
break;
1916+
case OP_neq:
1917+
result = (l != r) ? 1 : 0;
1918+
break;
1919+
case OP_lt:
1920+
result = (l < r) ? 1 : 0;
1921+
break;
1922+
case OP_leq:
1923+
result = (l <= r) ? 1 : 0;
1924+
break;
1925+
case OP_gt:
1926+
result = (l > r) ? 1 : 0;
1927+
break;
1928+
case OP_geq:
1929+
result = (l >= r) ? 1 : 0;
1930+
break;
1931+
default:
1932+
return false;
1933+
}
1934+
1935+
/* Transform to constant load */
1936+
insn->opcode = OP_load_constant;
1937+
insn->rd->is_const = true;
1938+
insn->rd->init_val = result;
1939+
insn->rs1 = NULL;
1940+
insn->rs2 = NULL;
1941+
1942+
return true;
1943+
}

0 commit comments

Comments
 (0)