Skip to content

Commit 81fa621

Browse files
committed
Add constant folding for unary operators
Optimize unary operations on constants at parse time: - Logical NOT (!x) when x is constant - Bitwise NOT (~x) when x is constant - Negation (-x) when x is constant This reduces the number of instructions generated and enables further optimizations in later passes.
1 parent 13f90a9 commit 81fa621

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

src/parser.c

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,18 +1750,40 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
17501750
read_expr_operand(parent, bb);
17511751

17521752
rs1 = opstack_pop();
1753-
vd = require_var(parent);
1754-
gen_name_to(vd->var_name);
1755-
opstack_push(vd);
1756-
add_insn(parent, *bb, OP_log_not, vd, rs1, NULL, 0, NULL);
1753+
1754+
/* Constant folding for logical NOT */
1755+
if (rs1 && rs1->is_const && !rs1->ptr_level && !rs1->is_global) {
1756+
vd = require_var(parent);
1757+
gen_name_to(vd->var_name);
1758+
vd->is_const = true;
1759+
vd->init_val = !rs1->init_val;
1760+
opstack_push(vd);
1761+
add_insn(parent, *bb, OP_load_constant, vd, NULL, NULL, 0, NULL);
1762+
} else {
1763+
vd = require_var(parent);
1764+
gen_name_to(vd->var_name);
1765+
opstack_push(vd);
1766+
add_insn(parent, *bb, OP_log_not, vd, rs1, NULL, 0, NULL);
1767+
}
17571768
} else if (lex_accept(T_bit_not)) {
17581769
read_expr_operand(parent, bb);
17591770

17601771
rs1 = opstack_pop();
1761-
vd = require_var(parent);
1762-
gen_name_to(vd->var_name);
1763-
opstack_push(vd);
1764-
add_insn(parent, *bb, OP_bit_not, vd, rs1, NULL, 0, NULL);
1772+
1773+
/* Constant folding for bitwise NOT */
1774+
if (rs1 && rs1->is_const && !rs1->ptr_level && !rs1->is_global) {
1775+
vd = require_var(parent);
1776+
gen_name_to(vd->var_name);
1777+
vd->is_const = true;
1778+
vd->init_val = ~rs1->init_val;
1779+
opstack_push(vd);
1780+
add_insn(parent, *bb, OP_load_constant, vd, NULL, NULL, 0, NULL);
1781+
} else {
1782+
vd = require_var(parent);
1783+
gen_name_to(vd->var_name);
1784+
opstack_push(vd);
1785+
add_insn(parent, *bb, OP_bit_not, vd, rs1, NULL, 0, NULL);
1786+
}
17651787
} else if (lex_accept(T_ampersand)) {
17661788
handle_address_of_operator(parent, bb);
17671789
} else if (lex_accept(T_asterisk)) {
@@ -2179,10 +2201,22 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
21792201

21802202
if (is_neg) {
21812203
rs1 = opstack_pop();
2182-
vd = require_var(parent);
2183-
gen_name_to(vd->var_name);
2184-
opstack_push(vd);
2185-
add_insn(parent, *bb, OP_negate, vd, rs1, NULL, 0, NULL);
2204+
2205+
/* Constant folding for negation */
2206+
if (rs1 && rs1->is_const && !rs1->ptr_level && !rs1->is_global) {
2207+
vd = require_var(parent);
2208+
gen_name_to(vd->var_name);
2209+
vd->is_const = true;
2210+
vd->init_val = -rs1->init_val;
2211+
opstack_push(vd);
2212+
add_insn(parent, *bb, OP_load_constant, vd, NULL, NULL, 0,
2213+
NULL);
2214+
} else {
2215+
vd = require_var(parent);
2216+
gen_name_to(vd->var_name);
2217+
opstack_push(vd);
2218+
add_insn(parent, *bb, OP_negate, vd, rs1, NULL, 0, NULL);
2219+
}
21862220
}
21872221
}
21882222
}

0 commit comments

Comments
 (0)