Skip to content

Commit 189e471

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 6b6dd4d commit 189e471

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
@@ -1705,18 +1705,38 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
17051705
read_expr_operand(parent, bb);
17061706

17071707
rs1 = opstack_pop();
1708-
vd = require_var(parent);
1709-
gen_name_to(vd->var_name);
1710-
opstack_push(vd);
1711-
add_insn(parent, *bb, OP_log_not, vd, rs1, NULL, 0, NULL);
1708+
1709+
/* Constant folding for logical NOT */
1710+
if (rs1 && rs1->init_val && !rs1->is_ptr && !rs1->is_global) {
1711+
vd = require_var(parent);
1712+
gen_name_to(vd->var_name);
1713+
vd->init_val = !rs1->init_val;
1714+
opstack_push(vd);
1715+
add_insn(parent, *bb, OP_load_constant, vd, NULL, NULL, 0, NULL);
1716+
} else {
1717+
vd = require_var(parent);
1718+
gen_name_to(vd->var_name);
1719+
opstack_push(vd);
1720+
add_insn(parent, *bb, OP_log_not, vd, rs1, NULL, 0, NULL);
1721+
}
17121722
} else if (lex_accept(T_bit_not)) {
17131723
read_expr_operand(parent, bb);
17141724

17151725
rs1 = opstack_pop();
1716-
vd = require_var(parent);
1717-
gen_name_to(vd->var_name);
1718-
opstack_push(vd);
1719-
add_insn(parent, *bb, OP_bit_not, vd, rs1, NULL, 0, NULL);
1726+
1727+
/* Constant folding for bitwise NOT */
1728+
if (rs1 && rs1->init_val && !rs1->is_ptr && !rs1->is_global) {
1729+
vd = require_var(parent);
1730+
gen_name_to(vd->var_name);
1731+
vd->init_val = ~rs1->init_val;
1732+
opstack_push(vd);
1733+
add_insn(parent, *bb, OP_load_constant, vd, NULL, NULL, 0, NULL);
1734+
} else {
1735+
vd = require_var(parent);
1736+
gen_name_to(vd->var_name);
1737+
opstack_push(vd);
1738+
add_insn(parent, *bb, OP_bit_not, vd, rs1, NULL, 0, NULL);
1739+
}
17201740
} else if (lex_accept(T_ampersand)) {
17211741
handle_address_of_operator(parent, bb);
17221742
} else if (lex_accept(T_asterisk)) {
@@ -2134,10 +2154,21 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
21342154

21352155
if (is_neg) {
21362156
rs1 = opstack_pop();
2137-
vd = require_var(parent);
2138-
gen_name_to(vd->var_name);
2139-
opstack_push(vd);
2140-
add_insn(parent, *bb, OP_negate, vd, rs1, NULL, 0, NULL);
2157+
2158+
/* Constant folding for negation */
2159+
if (rs1 && rs1->init_val && !rs1->is_ptr && !rs1->is_global) {
2160+
vd = require_var(parent);
2161+
gen_name_to(vd->var_name);
2162+
vd->init_val = -rs1->init_val;
2163+
opstack_push(vd);
2164+
add_insn(parent, *bb, OP_load_constant, vd, NULL, NULL, 0,
2165+
NULL);
2166+
} else {
2167+
vd = require_var(parent);
2168+
gen_name_to(vd->var_name);
2169+
opstack_push(vd);
2170+
add_insn(parent, *bb, OP_negate, vd, rs1, NULL, 0, NULL);
2171+
}
21412172
}
21422173
}
21432174
}

0 commit comments

Comments
 (0)