Skip to content

Commit 0de3fef

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 45353ac commit 0de3fef

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
@@ -1750,18 +1750,38 @@ 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->init_val && !rs1->ptr_level && !rs1->is_global) {
1756+
vd = require_var(parent);
1757+
gen_name_to(vd->var_name);
1758+
vd->init_val = !rs1->init_val;
1759+
opstack_push(vd);
1760+
add_insn(parent, *bb, OP_load_constant, vd, NULL, NULL, 0, NULL);
1761+
} else {
1762+
vd = require_var(parent);
1763+
gen_name_to(vd->var_name);
1764+
opstack_push(vd);
1765+
add_insn(parent, *bb, OP_log_not, vd, rs1, NULL, 0, NULL);
1766+
}
17571767
} else if (lex_accept(T_bit_not)) {
17581768
read_expr_operand(parent, bb);
17591769

17601770
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);
1771+
1772+
/* Constant folding for bitwise NOT */
1773+
if (rs1 && rs1->init_val && !rs1->ptr_level && !rs1->is_global) {
1774+
vd = require_var(parent);
1775+
gen_name_to(vd->var_name);
1776+
vd->init_val = ~rs1->init_val;
1777+
opstack_push(vd);
1778+
add_insn(parent, *bb, OP_load_constant, vd, NULL, NULL, 0, NULL);
1779+
} else {
1780+
vd = require_var(parent);
1781+
gen_name_to(vd->var_name);
1782+
opstack_push(vd);
1783+
add_insn(parent, *bb, OP_bit_not, vd, rs1, NULL, 0, NULL);
1784+
}
17651785
} else if (lex_accept(T_ampersand)) {
17661786
handle_address_of_operator(parent, bb);
17671787
} else if (lex_accept(T_asterisk)) {
@@ -2179,10 +2199,21 @@ void read_expr_operand(block_t *parent, basic_block_t **bb)
21792199

21802200
if (is_neg) {
21812201
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);
2202+
2203+
/* Constant folding for negation */
2204+
if (rs1 && rs1->init_val && !rs1->ptr_level && !rs1->is_global) {
2205+
vd = require_var(parent);
2206+
gen_name_to(vd->var_name);
2207+
vd->init_val = -rs1->init_val;
2208+
opstack_push(vd);
2209+
add_insn(parent, *bb, OP_load_constant, vd, NULL, NULL, 0,
2210+
NULL);
2211+
} else {
2212+
vd = require_var(parent);
2213+
gen_name_to(vd->var_name);
2214+
opstack_push(vd);
2215+
add_insn(parent, *bb, OP_negate, vd, rs1, NULL, 0, NULL);
2216+
}
21862217
}
21872218
}
21882219
}

0 commit comments

Comments
 (0)