Skip to content

Commit aadd0b9

Browse files
committed
Enhance constant folding for unary operations
This adds compile-time constant folding for unary operators (!, ~, and unary -) to reduce runtime operations. When these operators are applied to compile-time constants, the result is computed during compilation and replaced with a constant load instruction.
1 parent 06021f6 commit aadd0b9

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

src/parser.c

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,18 +1745,38 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
17451745
read_expr_operand(parent, bb);
17461746

17471747
rs1 = opstack_pop();
1748-
vd = require_var(parent);
1749-
gen_name_to(vd->var_name);
1750-
opstack_push(vd);
1751-
add_insn(parent, *bb, OP_log_not, vd, rs1, NULL, 0, NULL);
1748+
1749+
/* Constant folding for logical NOT */
1750+
if (rs1 && rs1->init_val && !rs1->is_ptr && !rs1->is_global) {
1751+
vd = require_var(parent);
1752+
gen_name_to(vd->var_name);
1753+
vd->init_val = !rs1->init_val;
1754+
opstack_push(vd);
1755+
add_insn(parent, *bb, OP_load_constant, vd, NULL, NULL, 0, NULL);
1756+
} else {
1757+
vd = require_var(parent);
1758+
gen_name_to(vd->var_name);
1759+
opstack_push(vd);
1760+
add_insn(parent, *bb, OP_log_not, vd, rs1, NULL, 0, NULL);
1761+
}
17521762
} else if (lex_accept(T_bit_not)) {
17531763
read_expr_operand(parent, bb);
17541764

17551765
rs1 = opstack_pop();
1756-
vd = require_var(parent);
1757-
gen_name_to(vd->var_name);
1758-
opstack_push(vd);
1759-
add_insn(parent, *bb, OP_bit_not, vd, rs1, NULL, 0, NULL);
1766+
1767+
/* Constant folding for bitwise NOT */
1768+
if (rs1 && rs1->init_val && !rs1->is_ptr && !rs1->is_global) {
1769+
vd = require_var(parent);
1770+
gen_name_to(vd->var_name);
1771+
vd->init_val = ~rs1->init_val;
1772+
opstack_push(vd);
1773+
add_insn(parent, *bb, OP_load_constant, vd, NULL, NULL, 0, NULL);
1774+
} else {
1775+
vd = require_var(parent);
1776+
gen_name_to(vd->var_name);
1777+
opstack_push(vd);
1778+
add_insn(parent, *bb, OP_bit_not, vd, rs1, NULL, 0, NULL);
1779+
}
17601780
} else if (lex_accept(T_ampersand)) {
17611781
handle_address_of_operator(parent, bb);
17621782
} else if (lex_accept(T_asterisk)) {
@@ -2174,10 +2194,21 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
21742194

21752195
if (is_neg) {
21762196
rs1 = opstack_pop();
2177-
vd = require_var(parent);
2178-
gen_name_to(vd->var_name);
2179-
opstack_push(vd);
2180-
add_insn(parent, *bb, OP_negate, vd, rs1, NULL, 0, NULL);
2197+
2198+
/* Constant folding for negation */
2199+
if (rs1 && rs1->init_val && !rs1->is_ptr && !rs1->is_global) {
2200+
vd = require_var(parent);
2201+
gen_name_to(vd->var_name);
2202+
vd->init_val = -rs1->init_val;
2203+
opstack_push(vd);
2204+
add_insn(parent, *bb, OP_load_constant, vd, NULL, NULL, 0,
2205+
NULL);
2206+
} else {
2207+
vd = require_var(parent);
2208+
gen_name_to(vd->var_name);
2209+
opstack_push(vd);
2210+
add_insn(parent, *bb, OP_negate, vd, rs1, NULL, 0, NULL);
2211+
}
21812212
}
21822213
}
21832214
}

0 commit comments

Comments
 (0)