1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/// NeoVM-specific peephole optimization: removes redundant stack operations
fn neovm_peephole_optimize(block: &mut ir::BasicBlock) {
let mut optimized = Vec::with_capacity(block.instructions.len());
let mut i = 0;
while i < block.instructions.len() {
// Pattern: PUSH x followed by DROP → remove both
if i + 1 < block.instructions.len() {
if matches!(&block.instructions[i], ir::Instruction::PushLiteral(_))
&& matches!(&block.instructions[i + 1], ir::Instruction::Drop(_))
{
i += 2;
continue;
}
// Pattern: LoadLocal x followed by DROP → remove both
if matches!(&block.instructions[i], ir::Instruction::LoadLocal(_))
&& matches!(&block.instructions[i + 1], ir::Instruction::Drop(_))
{
i += 2;
continue;
}
// Pattern: DUP followed by DROP → remove both
if matches!(&block.instructions[i], ir::Instruction::Dup)
&& matches!(&block.instructions[i + 1], ir::Instruction::Drop(_))
{
i += 2;
continue;
}
}
// Pattern: StoreLocal x, LoadLocal x → DUP, StoreLocal x
if i + 1 < block.instructions.len() {
if let (
ir::Instruction::StoreLocal(store_idx),
ir::Instruction::LoadLocal(load_idx),
) = (&block.instructions[i], &block.instructions[i + 1]) {
if store_idx == load_idx {
optimized.push(ir::Instruction::Dup);
optimized.push(ir::Instruction::StoreLocal(*store_idx));
i += 2;
continue;
}
}
}
// Pattern: SWAP followed by SWAP → remove both
if i + 1 < block.instructions.len()
&& matches!(&block.instructions[i], ir::Instruction::Swap)
&& matches!(&block.instructions[i + 1], ir::Instruction::Swap)
{
i += 2;
continue;
}
optimized.push(block.instructions[i].clone());
i += 1;
}
block.instructions = optimized;
}
/// NeoVM-specific: simplify identity operations (x + 0, x * 1, x & MAX, etc.)
fn neovm_simplify_identity_ops(block: &mut ir::BasicBlock) {
use num_bigint::BigInt;
use num_traits::Zero;
let mut optimized = Vec::with_capacity(block.instructions.len());
let mut i = 0;
while i < block.instructions.len() {
// Pattern: PUSH 0, BinaryOp::Add → remove (x + 0 = x)
if i + 1 < block.instructions.len() {
if let ir::Instruction::PushLiteral(ir::LiteralValue::Integer(val)) =
&block.instructions[i]
{
if val.is_zero() {
if let ir::Instruction::BinaryOp(ir::BinaryOperator::Add) =
&block.instructions[i + 1]
{
// Skip both instructions, identity operation
i += 2;
continue;
}
if let ir::Instruction::BinaryOp(ir::BinaryOperator::Sub) =
&block.instructions[i + 1]
{
// x - 0 = x, skip both
i += 2;
continue;
}
if let ir::Instruction::BinaryOp(ir::BinaryOperator::BitOr) =
&block.instructions[i + 1]
{
// x | 0 = x, skip both
i += 2;
continue;
}
if let ir::Instruction::BinaryOp(ir::BinaryOperator::BitXor) =
&block.instructions[i + 1]
{
// x ^ 0 = x, skip both
i += 2;
continue;
}
}
// Pattern: PUSH 1, MUL → identity (x * 1 = x)
if *val == BigInt::from(1) {
if let ir::Instruction::BinaryOp(ir::BinaryOperator::Mul) =
&block.instructions[i + 1]
{
i += 2;
continue;
}
if let ir::Instruction::BinaryOp(ir::BinaryOperator::Div) =
&block.instructions[i + 1]
{
// x / 1 = x
i += 2;
continue;
}
}
}
}
optimized.push(block.instructions[i].clone());
i += 1;
}
block.instructions = optimized;
}
/// NeoVM-specific: optimize boolean patterns
fn neovm_bool_optimize(block: &mut ir::BasicBlock) {
let mut optimized = Vec::with_capacity(block.instructions.len());
let mut i = 0;
while i < block.instructions.len() {
// M-BC2 fix — REMOVED the `PUSH true, EQ → identity` rewrite. For a
// non-boolean operand (e.g. NeoVM Integer 5), `(5 == true)` must
// evaluate to `false`, but the rewrite left `5` on the stack —
// corrupting any downstream consumer expecting a 0/1 boolean. The
// frontend guarantees Solidity `bool` operands for `==`, but Yul /
// assembly-injected values or a future type-inference miss could
// reach here with a non-bool, so the rewrite was unsound. The EQ
// instruction is cheap; correctness wins.
// Pattern: PUSH false, NE → identity for booleans (x != false = x)
if i + 1 < block.instructions.len() {
if let ir::Instruction::PushLiteral(ir::LiteralValue::Boolean(false)) =
&block.instructions[i]
{
if let ir::Instruction::BinaryOp(ir::BinaryOperator::Ne) =
&block.instructions[i + 1]
{
// x != false → x (skip both)
i += 2;
continue;
}
}
// Pattern: PUSH true, NE → converts to negation (x != true = !x)
// Keep this pattern as-is since we don't have a simple NOT instruction
// The codegen will handle it appropriately
}
// Pattern: BitwiseNot followed by BitwiseNot → identity (removes both)
if i + 1 < block.instructions.len()
&& matches!(&block.instructions[i], ir::Instruction::BitwiseNot)
&& matches!(&block.instructions[i + 1], ir::Instruction::BitwiseNot)
{
i += 2;
continue;
}
optimized.push(block.instructions[i].clone());
i += 1;
}
block.instructions = optimized;
}