Debian Codegraph @

crates/wasm-mutate/src/mutators/peephole/rules.rs

raw
1 //! Rewriting rules for the peephole mutator.
2 //!
3 //! New rewriting rules should be declared inside the
4 //! [`get_rules`](/src/wasm_mutate/mutators/peephole/rules.rs.html#17) function.
5
6 use egg::{Id, Rewrite, Subst, rewrite};
7
8 use crate::{WasmMutate, module::PrimitiveTypeInfo};
9
10 use super::{
11 EG, PeepholeMutator,
12 eggsy::{analysis::PeepholeMutationAnalysis, lang::Lang},
13 };
14
15 impl PeepholeMutator {
16 /// Returns the rewriting rules.
17 ///
18 /// Define new rules here for the peephole mutator.
19 pub fn get_rules(&self, config: &WasmMutate) -> Vec<Rewrite<Lang, PeepholeMutationAnalysis>> {
20 let mut rules = vec![];
21
22 macro_rules! rewrite {
23 ($name:tt; $lhs:tt => $rhs:tt $(if $cond:expr)*) => {
24 self.add_rewrite(&mut rules, $name, $lhs, $rhs, rewrite!(@cond $($cond)*));
25 };
26 ($name:tt; $lhs:tt <=> $rhs:tt $(if $cond:expr)*) => {
27 self.add_bidirectional_rewrite(&mut rules, $name, $lhs, $rhs, rewrite!(@cond $($cond)*));
28 };
29
30 (@cond $($cond:expr)*) => (&[$($cond),*]);
31 }
32
33 //// Various identities.
34 if config.reduce {
35 // NB: these only go one way when we are reducing.
36 rewrite!("i32.or--1"; "(i32.or ?x i32.const.-1)" => "i32.const.-1");
37 rewrite!("i64.or--1"; "(i64.or ?x i64.const.-1)" => "i64.const.-1");
38 rewrite!("i32.or-x-x"; "(i32.or ?x ?x)" => "?x");
39 rewrite!("i64.or-x-x"; "(i64.or ?x ?x)" => "?x");
40 rewrite!("i32.and-x-x"; "(i32.and ?x ?x)" => "?x");
41 rewrite!("i64.and-x-x"; "(i64.and ?x ?x)" => "?x");
42 rewrite!("select-same-branches"; "(select ?y ?y ?x)" => "?y");
43 rewrite!("i32.sub-0"; "(i32.sub ?x i32.const.0)" => "?x");
44 rewrite!("i64.sub-0"; "(i64.sub ?x i64.const.0)" => "?x");
45 rewrite!("i32.mul-x-1"; "(i32.mul ?x i32.const.1)" => "?x");
46 rewrite!("i64.mul-x-1"; "(i64.mul ?x i64.const.1)" => "?x");
47 rewrite!("f32.mul-x-1"; "(f32.mul ?x f32.const.1,0)" => "?x");
48 rewrite!("f64.mul-x-1"; "(f64.mul ?x f64.const.1,0)" => "?x");
49 rewrite!("i32.add-x-0"; "(i32.add ?x i32.const.0)" => "?x");
50 rewrite!("i64.add-x-0"; "(i64.add ?x i64.const.0)" => "?x");
51 rewrite!("f32-add-x-0"; "(f32.add ?x f32.const.0,0)" => "?x");
52 rewrite!("f64.add-x-0"; "(f64.add ?x f64.const.0,0)" => "?x");
53 rewrite!("i32.xor-x-0"; "(i32.xor ?x i32.const.0)" => "?x");
54 rewrite!("i64.xor-x-0"; "(i64.xor ?x i64.const.0)" => "?x");
55 rewrite!("i32.eq-x-0"; "(i32.eq ?x i32.const.0)" => "(i32.eqz ?x)");
56 rewrite!("i64.eq-x-0"; "(i64.eq ?x i64.const.0)" => "(i64.eqz ?x)");
57 rewrite!("i32.shl-by-0"; "(i32.shl ?x i32.const.0)" => "?x");
58 rewrite!("i64.shl-by-0"; "(i64.shl ?x i64.const.0)" => "?x");
59 rewrite!("i32.shr_u-by-0"; "(i32.shr_u ?x i32.const.0)" => "?x");
60 rewrite!("i64.shr_u-by-0"; "(i64.shr_u ?x i64.const.0)" => "?x");
61 rewrite!("i32.shr_s-by-0"; "(i32.shr_s ?x i32.const.0)" => "?x");
62 rewrite!("i64.shr_s-by-0"; "(i64.shr_s ?x i64.const.0)" => "?x");
63 } else {
64 rewrite!("i32.or--1"; "(i32.or ?x i32.const.-1)" => "i32.const.-1");
65 rewrite!("i64.or--1"; "(i64.or ?x i64.const.-1)" => "i64.const.-1");
66
67 rewrite!(
68 "i32.or-x-x";
69 "(i32.or ?x ?x)" <=> "?x"
70 if self.is_type("?x", PrimitiveTypeInfo::I32)
71 );
72 rewrite!(
73 "i64.or-x-x";
74 "(i64.or ?x ?x)" <=> "?x"
75 if self.is_type("?x", PrimitiveTypeInfo::I64)
76 );
77
78 rewrite!(
79 "i32.and-x-x";
80 "(i32.and ?x ?x)" <=> "?x"
81 if self.is_type("?x", PrimitiveTypeInfo::I32)
82 );
83 rewrite!(
84 "i64.and-x-x";
85 "(i64.and ?x ?x)" <=> "?x"
86 if self.is_type("?x", PrimitiveTypeInfo::I64)
87 );
88
89 rewrite!("select-same-branches"; "(select ?y ?y ?x)" => "?y");
90
91 rewrite!(
92 "i32.sub-0";
93 "(i32.sub ?x i32.const.0)" <=> "?x"
94 if self.is_type("?x", PrimitiveTypeInfo::I32)
95 );
96 rewrite!(
97 "i64.sub-0";
98 "(i64.sub ?x i64.const.0)" <=> "?x"
99 if self.is_type("?x", PrimitiveTypeInfo::I64)
100 );
101
102 rewrite!(
103 "i32.mul-x-1";
104 "?x" <=> "(i32.mul ?x i32.const.1)"
105 if self.is_type("?x", PrimitiveTypeInfo::I32)
106 );
107 rewrite!(
108 "i64.mul-x-1";
109 "?x" <=> "(i64.mul ?x i64.const.1)"
110 if self.is_type("?x", PrimitiveTypeInfo::I64)
111 );
112
113 rewrite!(
114 "f32.mul-x-1";
115 "?x" <=> "(f32.mul ?x f32.const.1,0)"
116 if self.is_type("?x", PrimitiveTypeInfo::F32)
117 );
118 rewrite!(
119 "f64.mul-x-1";
120 "?x" <=> "(f64.mul ?x f64.const.1,0)"
121 if self.is_type("?x", PrimitiveTypeInfo::F64)
122 );
123
124 rewrite!(
125 "i32.add-x-0";
126 "?x" <=> "(i32.add ?x i32.const.0)"
127 if self.is_type("?x", PrimitiveTypeInfo::I32)
128 );
129 rewrite!(
130 "i64.add-x-0";
131 "?x" <=> "(i64.add ?x i64.const.0)"
132 if self.is_type("?x", PrimitiveTypeInfo::I64)
133 );
134 rewrite!(
135 "f32-add-x-0";
136 "?x" <=> "(f32.add ?x f32.const.0,0)"
137 if self.is_type("?x", PrimitiveTypeInfo::F32)
138 );
139 rewrite!(
140 "f64.add-x-0";
141 "?x" <=> "(f64.add ?x f64.const.0,0)"
142 if self.is_type("?x", PrimitiveTypeInfo::F64)
143 );
144
145 rewrite!(
146 "i32.xor-x-0";
147 "?x" <=> "(i32.xor ?x i32.const.0)"
148 if self.is_type("?x", PrimitiveTypeInfo::I32)
149 );
150 rewrite!(
151 "i64.xor-x-0";
152 "?x" <=> "(i64.xor ?x i64.const.0)"
153 if self.is_type("?x", PrimitiveTypeInfo::I64)
154 );
155
156 rewrite!(
157 "i32.eq-x-0";
158 "(i32.eq ?x i32.const.0)" <=> "(i32.eqz ?x)"
159 if self.is_type("?x", PrimitiveTypeInfo::I32)
160 );
161 rewrite!(
162 "i64.eq-x-0";
163 "(i64.eq ?x i64.const.0)" <=> "(i64.eqz ?x)"
164 if self.is_type("?x", PrimitiveTypeInfo::I64)
165 );
166
167 rewrite!(
168 "i32.shl-by-0";
169 "(i32.shl ?x i32.const.0)" <=> "?x"
170 if self.is_type("?x", PrimitiveTypeInfo::I32)
171 );
172 rewrite!(
173 "i64.shl-by-0";
174 "(i64.shl ?x i64.const.0)" <=> "?x"
175 if self.is_type("?x", PrimitiveTypeInfo::I64)
176 );
177
178 rewrite!(
179 "i32.shr_u-by-0";
180 "(i32.shr_u ?x i32.const.0)" <=> "?x"
181 if self.is_type("?x", PrimitiveTypeInfo::I32)
182 );
183 rewrite!(
184 "i64.shr_u-by-0";
185 "(i64.shr_u ?x i64.const.0)" <=> "?x"
186 if self.is_type("?x", PrimitiveTypeInfo::I64)
187 );
188
189 rewrite!(
190 "i32.shr_s-by-0";
191 "(i32.shr_s ?x i32.const.0)" <=> "?x"
192 if self.is_type("?x", PrimitiveTypeInfo::I32)
193 );
194 rewrite!(
195 "i64.shr_s-by-0";
196 "(i64.shr_s ?x i64.const.0)" <=> "?x"
197 if self.is_type("?x", PrimitiveTypeInfo::I64)
198 );
199 }
200
201 // A bunch of commutativity rules.
202 //
203 // Even though these don't reduce code size themselves, they can help
204 // other rules apply, so we add them even when we aren't just reducing.
205
206 rewrite!("i32.add-commutes"; "(i32.add ?x ?y)" <=> "(i32.add ?y ?x)");
207 rewrite!("i64.add-commutes"; "(i64.add ?x ?y)" <=> "(i64.add ?y ?x)");
208 rewrite!("f32.add-commutes"; "(f32.add ?x ?y)" <=> "(f32.add ?y ?x)");
209 rewrite!("f64.add-commutes"; "(f64.add ?x ?y)" <=> "(f64.add ?y ?x)");
210
211 rewrite!("i32.mul-commutes"; "(i32.mul ?x ?y)" <=> "(i32.mul ?y ?x)" );
212 rewrite!("i64.mul-commutes"; "(i64.mul ?x ?y)" <=> "(i64.mul ?y ?x)" );
213 rewrite!("f32.mul-commutes"; "(f32.mul ?x ?y)" <=> "(f32.mul ?y ?x)" );
214 rewrite!("f64.mul-commutes"; "(f64.mul ?x ?y)" <=> "(f64.mul ?y ?x)" );
215
216 rewrite!("i32.and-commutes"; "(i32.and ?x ?y)" <=> "(i32.and ?y ?x)");
217 rewrite!("i64.and-commutes"; "(i64.and ?x ?y)" <=> "(i64.and ?y ?x)");
218
219 rewrite!("i32.or-commutes"; "(i32.or ?x ?y)" <=> "(i32.or ?y ?x)");
220 rewrite!("i64.or-commutes"; "(i64.or ?x ?y)" <=> "(i64.or ?y ?x)");
221
222 rewrite!("i32.xor-commutes"; "(i32.xor ?x ?y)" <=> "(i32.xor ?y ?x)");
223 rewrite!("i64.xor-commutes"; "(i64.xor ?x ?y)" <=> "(i64.xor ?y ?x)");
224
225 rewrite!("i32.eq-commutes"; "(i32.eq ?x ?y)" <=> "(i32.eq ?y ?x)");
226 rewrite!("i64.eq-commutes"; "(i64.eq ?x ?y)" <=> "(i64.eq ?y ?x)");
227
228 // A bunch of associativity rules.
229 //
230 // Even though these don't reduce code size themselves, they can help
231 // other rules apply, so we add them even when we aren't just reducing.
232
233 rewrite!(
234 "i32.mul-associates";
235 "(i32.mul ?x (i32.mul ?y ?z))" <=> "(i32.mul (i32.mul ?x ?y) ?z)"
236 );
237 rewrite!(
238 "i64.mul-associates";
239 "(i64.mul ?x (i64.mul ?y ?z))" <=> "(i64.mul (i64.mul ?x ?y) ?z)"
240 );
241
242 rewrite!(
243 "i32.add-associates";
244 "(i32.add ?x (i32.add ?y ?z))" <=> "(i32.add (i32.add ?x ?y) ?z)"
245 );
246 rewrite!(
247 "i64.add-associates";
248 "(i64.add ?x (i64.add ?y ?z))" <=> "(i64.add (i64.add ?x ?y) ?z)"
249 );
250
251 rewrite!(
252 "i32.and-associates";
253 "(i32.and ?x (i32.and ?y ?z))" <=> "(i32.and (i32.and ?x ?y) ?z)"
254 );
255 rewrite!(
256 "i64.and-associates";
257 "(i64.and ?x (i64.and ?y ?z))" <=> "(i64.and (i64.and ?x ?y) ?z)"
258 );
259
260 rewrite!(
261 "i32.or-associates";
262 "(i32.or ?x (i32.or ?y ?z))" <=> "(i32.or (i32.or ?x ?y) ?z)"
263 );
264 rewrite!(
265 "i64.or-associates";
266 "(i64.or ?x (i64.or ?y ?z))" <=> "(i64.or (i64.or ?x ?y) ?z)"
267 );
268
269 rewrite!(
270 "i32.xor-associates";
271 "(i32.xor ?x (i32.xor ?y ?z))" <=> "(i32.xor (i32.xor ?x ?y) ?z)"
272 );
273 rewrite!(
274 "i64.xor-associates";
275 "(i64.xor ?x (i64.xor ?y ?z))" <=> "(i64.xor (i64.xor ?x ?y) ?z)"
276 );
277
278 rewrite!(
279 "i32.eq-associates";
280 "(i32.eq ?x (i32.eq ?y ?z))" <=> "(i32.eq (i32.eq ?x ?y) ?z)"
281 );
282 rewrite!(
283 "i64.eq-associates";
284 "(i64.eq ?x (i64.eq ?y ?z))" <=> "(i64.eq (i64.eq ?x ?y) ?z)"
285 );
286
287 // Undoing `x * 2 ==> x << 1` strength reduction, etc...
288 if !config.reduce {
289 rewrite!("i32.mul-by-2"; "(i32.shl ?x i32.const.1)" <=> "(i32.mul ?x i32.const.2)");
290 rewrite!("i64.mul-by-2"; "(i64.shl ?x i64.const.1)" <=> "(i64.mul ?x i64.const.2)");
291 rewrite!("i32.mul-by-4"; "(i32.shl ?x i32.const.2)" <=> "(i32.mul ?x i32.const.4)");
292 rewrite!("i64.mul-by-4"; "(i64.shl ?x i64.const.2)" <=> "(i64.mul ?x i64.const.4)");
293 rewrite!("i32.mul-by-8"; "(i32.shl ?x i32.const.3)" <=> "(i32.mul ?x i32.const.8)");
294 rewrite!("i64.mul-by-8"; "(i64.shl ?x i64.const.3)" <=> "(i64.mul ?x i64.const.8)");
295 }
296
297 // Invert a `select` condition and swap its consequent and alternative.
298 if !config.reduce {
299 rewrite!("select-invert"; "(select ?x ?y ?z)" <=> "(select ?y ?x (i32.eqz ?z))");
300 }
301
302 // Convert `x + x` into `x * 2`.
303 if !config.reduce {
304 rewrite!("i32.add-x-x"; "(i32.add ?x ?x)" <=> "(i32.mul ?x i32.const.2)");
305 rewrite!("i64.add-x-x"; "(i64.add ?x ?x)" <=> "(i64.mul ?x i64.const.2)");
306 }
307
308 // Mess with dropped subexpressions.
309 if !config.reduce && !config.preserve_semantics {
310 rewrite!(
311 "i32.drop-x";
312 "(drop ?x)" => "(drop i32.rand)"
313 if self.is_type("?x", PrimitiveTypeInfo::I32)
314 );
315 rewrite!(
316 "i64.drop-x";
317 "(drop ?x)" => "(drop i64.rand)"
318 if self.is_type("?x", PrimitiveTypeInfo::I64)
319 );
320 rewrite!(
321 "f32.drop-x";
322 "(drop ?x)" => "(drop f32.const.0,0)"
323 if self.is_type("?x", PrimitiveTypeInfo::F32)
324 );
325 rewrite!(
326 "f64.drop-x";
327 "(drop ?x)" => "(drop f32.const.0,0)"
328 if self.is_type("?x", PrimitiveTypeInfo::F64)
329 );
330 }
331
332 // Insert some stack-neutral sub-expressions.
333 if !config.reduce {
334 rewrite!("container-nop-x"; "?x" => "(container nop ?x)");
335 rewrite!("container-x-nop"; "?x" => "(container ?x nop)");
336 rewrite!("container-drop-i32.rand-x"; "?x" => "(container (drop i32.rand) ?x)");
337 rewrite!("container-drop-i64.rand-x"; "?x" => "(container (drop i64.rand) ?x)");
338 rewrite!("container-x-drop-i32.rand"; "?x" => "(container ?x (drop i32.rand))");
339 rewrite!("container-x-drop-i64.rand"; "?x" => "(container ?x (drop i64.rand))");
340 }
341
342 // Spill expressions to a new global and then use the global's value.
343 if !config.reduce {
344 let max_globals = 100_000;
345 rewrite!(
346 "i32.use_of_global";
347 "?x" => "(i32.use_of_global ?x)"
348 if self.is_type("?x", PrimitiveTypeInfo::I32)
349 if self.global_count_less_than(config, max_globals)
350 );
351 rewrite!(
352 "i64.use_of_global";
353 "?x" => "(i64.use_of_global ?x)"
354 if self.is_type("?x", PrimitiveTypeInfo::I64)
355 if self.global_count_less_than(config, max_globals)
356 );
357 rewrite!(
358 "f32.use_of_global";
359 "?x" => "(f32.use_of_global ?x)"
360 if self.is_type("?x", PrimitiveTypeInfo::F32)
361 if self.global_count_less_than(config, max_globals)
362 );
363 rewrite!(
364 "f64.use_of_global";
365 "?x" => "(f64.use_of_global ?x)"
366 if self.is_type("?x", PrimitiveTypeInfo::F64)
367 if self.global_count_less_than(config, max_globals)
368 );
369 }
370
371 // Unfolding constants.
372 if !config.reduce {
373 rewrite!(
374 "i32.unfold";
375 "?x" => "(i32.unfold ?x)"
376 if self.is_const("?x")
377 if self.is_type("?x", PrimitiveTypeInfo::I32)
378 );
379 rewrite!(
380 "i64.unfold";
381 "?x" => "(i64.unfold ?x)"
382 if self.is_const("?x")
383 if self.is_type("?x", PrimitiveTypeInfo::I64)
384 );
385 }
386
387 // If we aren't preserving semantics, then go wild with mutations. Only
388 // thing we need to preserve is that we are emitting valid, well-typed
389 // Wasm.
390 if !config.preserve_semantics {
391 // Replace an expression with either zero, one, or a random constant.
392 rewrite!(
393 "replace-with-i32-1";
394 "?x" => "i32.const.1"
395 if self.is_type("?x", PrimitiveTypeInfo::I32)
396 );
397 rewrite!(
398 "replace-with-i64-1";
399 "?x" => "i64.const.1"
400 if self.is_type("?x", PrimitiveTypeInfo::I64)
401 );
402 rewrite!(
403 "replace-with-f32-1.0";
404 "?x" => "f32.const.1,0"
405 if self.is_type("?x", PrimitiveTypeInfo::F32)
406 );
407 rewrite!(
408 "replace-with-f64-1.0";
409 "?x" => "f64.const.1,0"
410 if self.is_type("?x", PrimitiveTypeInfo::F64)
411 );
412 rewrite!(
413 "replace-with-i32-0";
414 "?x" => "i32.const.0"
415 if self.is_type("?x", PrimitiveTypeInfo::I32)
416 );
417 rewrite!(
418 "replace-with-i64-0";
419 "?x" => "i64.const.0"
420 if self.is_type("?x", PrimitiveTypeInfo::I64)
421 );
422 rewrite!(
423 "replace-with-f32-0";
424 "?x" => "f32.const.0,0"
425 if self.is_type("?x", PrimitiveTypeInfo::F32)
426 );
427 rewrite!(
428 "replace-with-f64-0";
429 "?x" => "f64.const.0,0"
430 if self.is_type("?x", PrimitiveTypeInfo::F64)
431 );
432 rewrite!(
433 "replace-with-v128-0";
434 "?x" => "v128.const.0"
435 if self.is_type("?x", PrimitiveTypeInfo::V128)
436 );
437 rewrite!(
438 "replace-with-i32.rand";
439 "?x" => "i32.rand"
440 if self.is_type("?x", PrimitiveTypeInfo::I32)
441 );
442 rewrite!(
443 "replace-with-i64.rand";
444 "?x" => "i64.rand"
445 if self.is_type("?x", PrimitiveTypeInfo::I64)
446 );
447 rewrite!(
448 "replace-with-f32.rand";
449 "?x" => "f32.rand"
450 if self.is_type("?x", PrimitiveTypeInfo::F32)
451 );
452 rewrite!(
453 "replace-with-f64.rand";
454 "?x" => "f64.rand"
455 if self.is_type("?x", PrimitiveTypeInfo::F64)
456 );
457 rewrite!(
458 "replace-with-ref-null-func";
459 "?x" => "ref.null.func"
460 if self.is_type("?x", PrimitiveTypeInfo::FuncRef)
461 );
462 rewrite!(
463 "replace-with-ref-null-extern";
464 "?x" => "ref.null.extern"
465 if self.is_type("?x", PrimitiveTypeInfo::ExternRef)
466 );
467 // Try to outright delete some instructions and containers to
468 // help make input even smaller in wasm-shrink cases.
469 rewrite!(
470 "remove-drop";
471 "(drop ?x)" => "(container)"
472 );
473 rewrite!(
474 "remove-nop";
475 "nop" => "(container)"
476 );
477 rewrite!(
478 "remove-global.set.0";
479 "(global.set.0 ?x)" => "(container)"
480 );
481 rewrite!(
482 "remove-global.set.1";
483 "(global.set.1 ?x)" => "(container)"
484 );
485 rewrite!(
486 "remove-elem.drop.0";
487 "(elem.drop.0)" => "(container)"
488 );
489 rewrite!(
490 "remove-elem.drop.1";
491 "(elem.drop.1)" => "(container)"
492 );
493 rewrite!(
494 "remove-data.drop.0";
495 "(data.drop.0)" => "(container)"
496 );
497 rewrite!(
498 "remove-data.drop.1";
499 "(data.drop.1)" => "(container)"
500 );
501
502 if !config.reduce {
503 // `x <=> x + 1`
504 rewrite!(
505 "i32.add-1";
506 "?x" <=> "(i32.add ?x i32.const.1)"
507 if self.is_type("?x", PrimitiveTypeInfo::I32)
508 );
509 rewrite!(
510 "i64.add-1";
511 "?x" <=> "(i64.add ?x i64.const.1)"
512 if self.is_type("?x", PrimitiveTypeInfo::I64)
513 );
514
515 // `x <=> x - 1`
516 rewrite!(
517 "i32.sub-1";
518 "?x" <=> "(i32.sub ?x i32.const.1)"
519 if self.is_type("?x", PrimitiveTypeInfo::I32)
520 );
521 rewrite!(
522 "i64.sub-1";
523 "?x" <=> "(i64.sub ?x i64.const.1)"
524 if self.is_type("?x", PrimitiveTypeInfo::I64)
525 );
526
527 // `x <=> x & 1`
528 rewrite!(
529 "i32.and-1";
530 "?x" <=> "(i32.and ?x i32.const.1)"
531 if self.is_type("?x", PrimitiveTypeInfo::I32)
532 );
533 rewrite!(
534 "i64.and-1";
535 "?x" <=> "(i64.and ?x i64.const.1)"
536 if self.is_type("?x", PrimitiveTypeInfo::I64)
537 );
538
539 // `x <=> x | 1`
540 rewrite!(
541 "i32.or-1";
542 "?x" <=> "(i32.or ?x i32.const.1)"
543 if self.is_type("?x", PrimitiveTypeInfo::I32)
544 );
545 rewrite!(
546 "i64.or-1";
547 "?x" <=> "(i64.or ?x i64.const.1)"
548 if self.is_type("?x", PrimitiveTypeInfo::I64)
549 );
550
551 // `x <=> x ^ 1`
552 rewrite!(
553 "i32.xor-1";
554 "?x" <=> "(i32.xor ?x i32.const.1)"
555 if self.is_type("?x", PrimitiveTypeInfo::I32)
556 );
557 rewrite!(
558 "i64.xor-1";
559 "?x" <=> "(i64.xor ?x i64.const.1)"
560 if self.is_type("?x", PrimitiveTypeInfo::I64)
561 );
562
563 // `x <=> x << 1`
564 rewrite!(
565 "i32.shl-1";
566 "?x" <=> "(i32.shl ?x i32.const.1)"
567 if self.is_type("?x", PrimitiveTypeInfo::I32)
568 );
569 rewrite!(
570 "i64.shl-1";
571 "?x" <=> "(i64.shl ?x i64.const.1)"
572 if self.is_type("?x", PrimitiveTypeInfo::I64)
573 );
574
575 // `x <=> x >> 1`
576 rewrite!(
577 "i32.shr_u-1";
578 "?x" <=> "(i32.shr_u ?x i32.const.1)"
579 if self.is_type("?x", PrimitiveTypeInfo::I32)
580 );
581 rewrite!(
582 "i64.shr_u-1";
583 "?x" <=> "(i64.shr_u ?x i64.const.1)"
584 if self.is_type("?x", PrimitiveTypeInfo::I64)
585 );
586 rewrite!(
587 "i32.shr_s-1";
588 "?x" <=> "(i32.shr_s ?x i32.const.1)"
589 if self.is_type("?x", PrimitiveTypeInfo::I32)
590 );
591 rewrite!(
592 "i64.shr_s-1";
593 "?x" <=> "(i64.shr_s ?x i64.const.1)"
594 if self.is_type("?x", PrimitiveTypeInfo::I64)
595 );
596 }
597
598 for i in 0..20 {
599 let name = format!("remove-local.set{i}");
600 let lhs = format!("(local.set.{i} ?x)");
601 self.add_rewrite(&mut rules, &name, &lhs, "nop", &[]);
602
603 let name = format!("remove-local.tee{i}");
604 let lhs = format!("(local.tee.{i} ?x)");
605 self.add_rewrite(&mut rules, &name, &lhs, "?x", &[]);
606 }
607 }
608
609 rules
610 }
611
612 fn is_type(&self, var: &str, ty: PrimitiveTypeInfo) -> Condition {
613 Condition::Type(var.parse().unwrap(), ty)
614 }
615
616 fn is_const(&self, var: &str) -> Condition {
617 Condition::Const(var.parse().unwrap())
618 }
619
620 /// Condition that check for number of module globals
621 fn global_count_less_than(&self, config: &WasmMutate, allowed: usize) -> Condition {
622 let count = config.info().get_global_count();
623 Condition::Bool(count < allowed)
624 }
625
626 fn add_bidirectional_rewrite(
627 &self,
628 rules: &mut Vec<Rewrite<Lang, PeepholeMutationAnalysis>>,
629 name: &str,
630 lhs: &str,
631 rhs: &str,
632 cond: &[Condition],
633 ) {
634 self.add_rewrite(rules, name, lhs, rhs, cond);
635 self.add_rewrite(rules, &format!("{name}-rev"), rhs, lhs, cond);
636 }
637
638 fn add_rewrite(
639 &self,
640 rules: &mut Vec<Rewrite<Lang, PeepholeMutationAnalysis>>,
641 name: &str,
642 lhs: &str,
643 rhs: &str,
644 cond: &[Condition],
645 ) {
646 let name = name.to_string();
647 let long_name = format!("{lhs} => {rhs}");
648 let lhs = lhs.parse::<egg::Pattern<_>>().unwrap();
649 let rhs = rhs.parse::<egg::Pattern<_>>().unwrap();
650 rules.push(match cond.len() {
651 0 => Rewrite::new(name, long_name, lhs, rhs).unwrap(),
652 1 => Rewrite::new(
653 name,
654 long_name,
655 lhs,
656 egg::ConditionalApplier {
657 condition: cond[0].clone(),
658 applier: rhs,
659 },
660 )
661 .unwrap(),
662 2 => Rewrite::new(
663 name,
664 long_name,
665 lhs,
666 egg::ConditionalApplier {
667 condition: cond[0].clone(),
668 applier: egg::ConditionalApplier {
669 condition: cond[1].clone(),
670 applier: rhs,
671 },
672 },
673 )
674 .unwrap(),
675 _ => unimplemented!(),
676 });
677 }
678 }
679
680 #[derive(Clone)]
681 enum Condition {
682 Type(egg::Var, PrimitiveTypeInfo),
683 Const(egg::Var),
684 Bool(bool),
685 }
686
687 impl egg::Condition<Lang, PeepholeMutationAnalysis> for Condition {
688 fn check(&self, egraph: &mut EG, _eclass: Id, subst: &Subst) -> bool {
689 match *self {
690 Condition::Type(var, t) => {
691 let eclass = &egraph[subst[var]];
692 match &eclass.data {
693 Some(d) => d.tpe == t,
694 None => false,
695 }
696 }
697 Condition::Const(var) => {
698 let eclass = &egraph[subst[var]];
699 if eclass.nodes.len() == 1 {
700 let node = &eclass.nodes[0];
701 match node {
702 Lang::I32(_) => true,
703 Lang::I64(_) => true,
704 Lang::F32(_) => true,
705 Lang::F64(_) => true,
706 _ => false,
707 }
708 } else {
709 false
710 }
711 }
712 Condition::Bool(b) => b,
713 }
714 }
715 }