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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! Constant evaluation and deduplication.
use super::*;
// ────────────────────────────────────────────────────────────────
// Phase 1A: Constant folding
// ────────────────────────────────────────────────────────────────
/// Constant folding: evaluate operations with constant operands at compile time.
pub(super) fn constant_fold(func: &mut MFunction) {
// Build def map: VReg → LoadImm value
let mut consts: HashMap<VReg, u64> = HashMap::default();
for block in &func.blocks {
for inst in &block.insts {
if let MInst::LoadImm { dst, value } = inst {
consts.insert(*dst, *value);
}
}
}
if consts.is_empty() {
return;
}
let mut changed = true;
while changed {
changed = false;
for block in &mut func.blocks {
for inst in &mut block.insts {
let folded = match inst {
// Binary reg-reg with both constant
MInst::Add { dst, lhs, rhs } => {
fold_bin(&consts, *dst, *lhs, *rhs, u64::wrapping_add)
}
MInst::Add32 { dst, lhs, rhs } => {
fold_bin32(&consts, *dst, *lhs, *rhs, u32::wrapping_add)
}
MInst::Sub { dst, lhs, rhs } => {
fold_bin(&consts, *dst, *lhs, *rhs, u64::wrapping_sub)
}
MInst::Sub32 { dst, lhs, rhs } => {
fold_bin32(&consts, *dst, *lhs, *rhs, u32::wrapping_sub)
}
MInst::Mul { dst, lhs, rhs } => {
fold_bin(&consts, *dst, *lhs, *rhs, u64::wrapping_mul)
}
MInst::Mul32 { dst, lhs, rhs } => {
fold_bin32(&consts, *dst, *lhs, *rhs, u32::wrapping_mul)
}
MInst::MulImm { dst, src, imm } => consts
.get(src)
.map(|value| (*dst, value.wrapping_mul(*imm as u64))),
MInst::MulImm32 { dst, src, imm } => consts
.get(src)
.map(|value| (*dst, u64::from((*value as u32).wrapping_mul(*imm as u32)))),
MInst::And { dst, lhs, rhs } => {
fold_bin(&consts, *dst, *lhs, *rhs, |a, b| a & b)
}
MInst::And32 { dst, lhs, rhs } => {
fold_bin32(&consts, *dst, *lhs, *rhs, |a, b| a & b)
}
MInst::Or { dst, lhs, rhs } => {
fold_bin(&consts, *dst, *lhs, *rhs, |a, b| a | b)
}
MInst::Or32 { dst, lhs, rhs } => {
fold_bin32(&consts, *dst, *lhs, *rhs, |a, b| a | b)
}
MInst::Xor { dst, lhs, rhs } => {
fold_bin(&consts, *dst, *lhs, *rhs, |a, b| a ^ b)
}
MInst::Xor32 { dst, lhs, rhs } => {
fold_bin32(&consts, *dst, *lhs, *rhs, |a, b| a ^ b)
}
MInst::Shr { dst, lhs, rhs } => {
fold_bin(
&consts,
*dst,
*lhs,
*rhs,
|a, b| {
if b >= 64 { 0 } else { a >> b }
},
)
}
MInst::Shl { dst, lhs, rhs } => {
fold_bin(
&consts,
*dst,
*lhs,
*rhs,
|a, b| {
if b >= 64 { 0 } else { a << b }
},
)
}
MInst::Sar { dst, lhs, rhs } => fold_bin(&consts, *dst, *lhs, *rhs, |a, b| {
if b >= 64 {
((a as i64) >> 63) as u64
} else {
((a as i64) >> b) as u64
}
}),
// Binary imm with constant src
MInst::AndImm { dst, src, imm } => consts.get(src).map(|&v| (*dst, v & *imm)),
MInst::AndImm32 { dst, src, imm } => consts
.get(src)
.map(|&v| (*dst, u64::from((v as u32) & *imm))),
MInst::OrImm { dst, src, imm } => consts.get(src).map(|&v| (*dst, v | *imm)),
MInst::ShrImm { dst, src, imm } => consts
.get(src)
.map(|&v| (*dst, if *imm >= 64 { 0 } else { v >> *imm })),
MInst::ShlImm { dst, src, imm } => consts
.get(src)
.map(|&v| (*dst, if *imm >= 64 { 0 } else { v << *imm })),
MInst::SarImm { dst, src, imm } => consts.get(src).map(|&v| {
(
*dst,
if *imm >= 64 {
((v as i64) >> 63) as u64
} else {
((v as i64) >> *imm) as u64
},
)
}),
// Unary with constant src
MInst::BitNot { dst, src } => consts.get(src).map(|&v| (*dst, !v)),
MInst::Neg { dst, src } => consts.get(src).map(|&v| (*dst, v.wrapping_neg())),
MInst::Popcnt { dst, src } => {
consts.get(src).map(|&v| (*dst, v.count_ones() as u64))
}
MInst::Bsf { dst, src } => consts
.get(src)
.and_then(|&v| (v != 0).then_some((*dst, v.trailing_zeros() as u64))),
MInst::Bsr { dst, src } => consts
.get(src)
.and_then(|&v| (v != 0).then_some((*dst, 63 - v.leading_zeros() as u64))),
MInst::BsrOr {
dst,
src,
zero_value,
} => consts.get(src).map(|&v| {
(
*dst,
if v == 0 {
*zero_value as u64
} else {
63 - v.leading_zeros() as u64
},
)
}),
// Comparison with both constant
MInst::Cmp {
dst,
lhs,
rhs,
kind,
} => {
if let (Some(&l), Some(&r)) = (consts.get(lhs), consts.get(rhs)) {
let result = match kind {
CmpKind::Eq => l == r,
CmpKind::Ne => l != r,
CmpKind::LtU => l < r,
CmpKind::LeU => l <= r,
CmpKind::GtU => l > r,
CmpKind::GeU => l >= r,
CmpKind::LtS => (l as i64) < (r as i64),
CmpKind::LeS => (l as i64) <= (r as i64),
CmpKind::GtS => (l as i64) > (r as i64),
CmpKind::GeS => (l as i64) >= (r as i64),
};
Some((*dst, result as u64))
} else {
None
}
}
_ => None,
};
if let Some((dst, value)) = folded {
*inst = MInst::LoadImm { dst, value };
consts.insert(dst, value);
changed = true;
}
}
}
}
}
fn fold_bin(
consts: &HashMap<VReg, u64>,
dst: VReg,
lhs: VReg,
rhs: VReg,
op: impl Fn(u64, u64) -> u64,
) -> Option<(VReg, u64)> {
if let (Some(&l), Some(&r)) = (consts.get(&lhs), consts.get(&rhs)) {
Some((dst, op(l, r)))
} else {
None
}
}
fn fold_bin32(
consts: &HashMap<VReg, u64>,
dst: VReg,
lhs: VReg,
rhs: VReg,
op: impl Fn(u32, u32) -> u32,
) -> Option<(VReg, u64)> {
if let (Some(&l), Some(&r)) = (consts.get(&lhs), consts.get(&rhs)) {
Some((dst, u64::from(op(l as u32, r as u32))))
} else {
None
}
}
/// Constant deduplication: merge LoadImm instructions with the same value
/// into a single VReg. Reduces register pressure and instruction count.
pub(super) fn constant_dedup(func: &mut MFunction) {
let mut aliases: HashMap<VReg, VReg> = HashMap::default();
// Map from constant value → canonical VReg
let mut const_map: HashMap<u64, VReg> = HashMap::default();
for block in &func.blocks {
const_map.clear(); // per-block to avoid cross-block live range extension
for inst in &block.insts {
if let MInst::LoadImm { dst, value } = inst {
if let Some(&canonical) = const_map.get(value) {
aliases.insert(*dst, canonical);
} else {
const_map.insert(*value, *dst);
}
}
}
}
if aliases.is_empty() {
return;
}
// Apply aliases
for block in &mut func.blocks {
for inst in &mut block.insts {
let current_uses = inst.uses();
for u in current_uses {
if let Some(&target) = aliases.get(&u) {
inst.rewrite_use(u, target);
}
}
}
for phi in &mut block.phis {
for (_, src) in &mut phi.sources {
if let Some(&a) = aliases.get(src) {
*src = a;
}
}
}
}
// Remove duplicated LoadImm
for block in &mut func.blocks {
block.insts.retain(|inst| {
if let MInst::LoadImm { dst, .. } = inst {
!aliases.contains_key(dst)
} else {
true
}
});
}
}