Skip to content

Commit 4d4e903

Browse files
authored
[Cranelift] add simplification rules (#12937)
1 parent e5b127c commit 4d4e903

3 files changed

Lines changed: 519 additions & 3 deletions

File tree

cranelift/codegen/src/opts/bitops.isle

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,4 +579,121 @@
579579
(rule (simplify (bxor ty (band ty x (bnot ty y)) (bnot ty x))) (bnot ty (band ty x y)))
580580
(rule (simplify (bxor ty (bnot ty x) (band ty x (bnot ty y)))) (bnot ty (band ty x y)))
581581
(rule (simplify (bxor ty (band ty (bnot ty y) x) (bnot ty x))) (bnot ty (band ty x y)))
582-
(rule (simplify (bxor ty (bnot ty x) (band ty (bnot ty y) x))) (bnot ty (band ty x y)))
582+
(rule (simplify (bxor ty (bnot ty x) (band ty (bnot ty y) x))) (bnot ty (band ty x y)))
583+
584+
;; (~x & y) ^ x --> x | y
585+
(rule (simplify (bxor ty (band ty (bnot ty x) y) x)) (bor ty x y))
586+
(rule (simplify (bxor ty x (band ty (bnot ty x) y))) (bor ty x y))
587+
(rule (simplify (bxor ty (band ty y (bnot ty x)) x)) (bor ty x y))
588+
(rule (simplify (bxor ty x (band ty y (bnot ty x)))) (bor ty x y))
589+
590+
;; (x | y) & ~(x ^ y) --> x & y
591+
(rule (simplify (band ty (bor ty x y) (bnot ty (bxor ty x y)))) (band ty x y))
592+
(rule (simplify (band ty (bnot ty (bxor ty x y)) (bor ty x y))) (band ty x y))
593+
(rule (simplify (band ty (bor ty x y) (bnot ty (bxor ty y x)))) (band ty x y))
594+
(rule (simplify (band ty (bnot ty (bxor ty y x)) (bor ty x y))) (band ty x y))
595+
(rule (simplify (band ty (bor ty y x) (bnot ty (bxor ty x y)))) (band ty x y))
596+
(rule (simplify (band ty (bnot ty (bxor ty x y)) (bor ty y x))) (band ty x y))
597+
(rule (simplify (band ty (bor ty y x) (bnot ty (bxor ty y x)))) (band ty x y))
598+
(rule (simplify (band ty (bnot ty (bxor ty y x)) (bor ty y x))) (band ty x y))
599+
600+
;; x | ~(x ^ y) --> x | ~y
601+
(rule (simplify (bor ty x (bnot ty (bxor ty x y)))) (bor ty x (bnot ty y)))
602+
(rule (simplify (bor ty (bnot ty (bxor ty x y)) x)) (bor ty x (bnot ty y)))
603+
(rule (simplify (bor ty x (bnot ty (bxor ty y x)))) (bor ty x (bnot ty y)))
604+
(rule (simplify (bor ty (bnot ty (bxor ty y x)) x)) (bor ty x (bnot ty y)))
605+
606+
;; x | ((~x) ^ y) --> x | ~y
607+
(rule (simplify (bor ty x (bxor ty (bnot ty x) y))) (bor ty x (bnot ty y)))
608+
(rule (simplify (bor ty (bxor ty (bnot ty x) y) x)) (bor ty x (bnot ty y)))
609+
(rule (simplify (bor ty x (bxor ty y (bnot ty x)))) (bor ty x (bnot ty y)))
610+
(rule (simplify (bor ty (bxor ty y (bnot ty x)) x)) (bor ty x (bnot ty y)))
611+
612+
;; x & ~(x ^ y) --> x & y
613+
(rule (simplify (band ty x (bnot ty (bxor ty x y)))) (band ty x y))
614+
(rule (simplify (band ty (bnot ty (bxor ty x y)) x)) (band ty x y))
615+
(rule (simplify (band ty x (bnot ty (bxor ty y x)))) (band ty x y))
616+
(rule (simplify (band ty (bnot ty (bxor ty y x)) x)) (band ty x y))
617+
618+
;; x & ((~x) ^ y) --> x & y
619+
(rule (simplify (band ty x (bxor ty (bnot ty x) y))) (band ty x y))
620+
(rule (simplify (band ty (bxor ty (bnot ty x) y) x)) (band ty x y))
621+
(rule (simplify (band ty x (bxor ty y (bnot ty x)))) (band ty x y))
622+
(rule (simplify (band ty (bxor ty y (bnot ty x)) x)) (band ty x y))
623+
624+
;; (x | y) | (x ^ y) --> (x | y)
625+
(rule (simplify (bor ty (bor ty x y) (bxor ty x y))) (bor ty x y))
626+
(rule (simplify (bor ty (bxor ty x y) (bor ty x y))) (bor ty x y))
627+
(rule (simplify (bor ty (bor ty x y) (bxor ty y x))) (bor ty x y))
628+
(rule (simplify (bor ty (bxor ty y x) (bor ty x y))) (bor ty x y))
629+
(rule (simplify (bor ty (bor ty y x) (bxor ty x y))) (bor ty x y))
630+
(rule (simplify (bor ty (bxor ty x y) (bor ty y x))) (bor ty x y))
631+
(rule (simplify (bor ty (bor ty y x) (bxor ty y x))) (bor ty x y))
632+
(rule (simplify (bor ty (bxor ty y x) (bor ty y x))) (bor ty x y))
633+
634+
;; (x ^ y) & (x ^ (y ^ z)) --> (x ^ y) & ~z
635+
(rule (simplify (band ty (bxor ty x y) (bxor ty (bxor ty y z) x))) (band ty (bxor ty x y) (bnot ty z)))
636+
(rule (simplify (band ty (bxor ty (bxor ty y z) x) (bxor ty x y))) (band ty (bxor ty x y) (bnot ty z)))
637+
(rule (simplify (band ty (bxor ty x y) (bxor ty x (bxor ty y z)))) (band ty (bxor ty x y) (bnot ty z)))
638+
(rule (simplify (band ty (bxor ty x (bxor ty y z)) (bxor ty x y))) (band ty (bxor ty x y) (bnot ty z)))
639+
(rule (simplify (band ty (bxor ty x y) (bxor ty (bxor ty z y) x))) (band ty (bxor ty x y) (bnot ty z)))
640+
(rule (simplify (band ty (bxor ty (bxor ty z y) x) (bxor ty x y))) (band ty (bxor ty x y) (bnot ty z)))
641+
(rule (simplify (band ty (bxor ty x y) (bxor ty x (bxor ty z y)))) (band ty (bxor ty x y) (bnot ty z)))
642+
(rule (simplify (band ty (bxor ty x (bxor ty z y)) (bxor ty x y))) (band ty (bxor ty x y) (bnot ty z)))
643+
(rule (simplify (band ty (bxor ty y x) (bxor ty (bxor ty y z) x))) (band ty (bxor ty x y) (bnot ty z)))
644+
(rule (simplify (band ty (bxor ty (bxor ty y z) x) (bxor ty y x))) (band ty (bxor ty x y) (bnot ty z)))
645+
(rule (simplify (band ty (bxor ty y x) (bxor ty x (bxor ty y z)))) (band ty (bxor ty x y) (bnot ty z)))
646+
(rule (simplify (band ty (bxor ty x (bxor ty y z)) (bxor ty y x))) (band ty (bxor ty x y) (bnot ty z)))
647+
(rule (simplify (band ty (bxor ty y x) (bxor ty (bxor ty z y) x))) (band ty (bxor ty x y) (bnot ty z)))
648+
(rule (simplify (band ty (bxor ty (bxor ty z y) x) (bxor ty y x))) (band ty (bxor ty x y) (bnot ty z)))
649+
(rule (simplify (band ty (bxor ty y x) (bxor ty x (bxor ty z y)))) (band ty (bxor ty x y) (bnot ty z)))
650+
(rule (simplify (band ty (bxor ty x (bxor ty z y)) (bxor ty y x))) (band ty (bxor ty x y) (bnot ty z)))
651+
652+
;; (~x & y) ^ z --> (x & y) ^ (z ^ y)
653+
(rule (simplify (bxor ty (band ty (bnot ty x) y) z)) (bxor ty (band ty x y) (bxor ty z y)))
654+
(rule (simplify (bxor ty z (band ty (bnot ty x) y))) (bxor ty (band ty x y) (bxor ty z y)))
655+
(rule (simplify (bxor ty (band ty y (bnot ty x)) z)) (bxor ty (band ty x y) (bxor ty z y)))
656+
(rule (simplify (bxor ty z (band ty y (bnot ty x)))) (bxor ty (band ty x y) (bxor ty z y)))
657+
658+
;; ~x - ~y --> y - x
659+
(rule (simplify (isub ty (bnot ty x) (bnot ty y))) (isub ty y x))
660+
661+
;; (~x & y) | ~(x | y) --> ~x
662+
(rule (simplify (bor ty (band ty (bnot ty x) y) (bnot ty (bor ty x y)))) (bnot ty x))
663+
(rule (simplify (bor ty (bnot ty (bor ty x y)) (band ty (bnot ty x) y))) (bnot ty x))
664+
(rule (simplify (bor ty (band ty (bnot ty x) y) (bnot ty (bor ty y x)))) (bnot ty x))
665+
(rule (simplify (bor ty (bnot ty (bor ty y x)) (band ty (bnot ty x) y))) (bnot ty x))
666+
(rule (simplify (bor ty (band ty y (bnot ty x)) (bnot ty (bor ty x y)))) (bnot ty x))
667+
(rule (simplify (bor ty (bnot ty (bor ty x y)) (band ty y (bnot ty x)))) (bnot ty x))
668+
(rule (simplify (bor ty (band ty y (bnot ty x)) (bnot ty (bor ty y x)))) (bnot ty x))
669+
(rule (simplify (bor ty (bnot ty (bor ty y x)) (band ty y (bnot ty x)))) (bnot ty x))
670+
671+
;; (~x | y) & ~(x & y) --> ~x
672+
(rule (simplify (band ty (bor ty (bnot ty x) y) (bnot ty (band ty x y)))) (bnot ty x))
673+
(rule (simplify (band ty (bnot ty (band ty x y)) (bor ty (bnot ty x) y))) (bnot ty x))
674+
(rule (simplify (band ty (bor ty (bnot ty x) y) (bnot ty (band ty y x)))) (bnot ty x))
675+
(rule (simplify (band ty (bnot ty (band ty y x)) (bor ty (bnot ty x) y))) (bnot ty x))
676+
(rule (simplify (band ty (bor ty y (bnot ty x)) (bnot ty (band ty x y)))) (bnot ty x))
677+
(rule (simplify (band ty (bnot ty (band ty x y)) (bor ty y (bnot ty x)))) (bnot ty x))
678+
(rule (simplify (band ty (bor ty y (bnot ty x)) (bnot ty (band ty y x)))) (bnot ty x))
679+
(rule (simplify (band ty (bnot ty (band ty y x)) (bor ty y (bnot ty x)))) (bnot ty x))
680+
681+
;; (x & y) | ~(x | y) --> ~(x ^ y)
682+
(rule (simplify (bor ty (band ty x y) (bnot ty (bor ty x y)))) (bnot ty (bxor ty x y)))
683+
(rule (simplify (bor ty (bnot ty (bor ty x y)) (band ty x y))) (bnot ty (bxor ty x y)))
684+
(rule (simplify (bor ty (band ty x y) (bnot ty (bor ty y x)))) (bnot ty (bxor ty x y)))
685+
(rule (simplify (bor ty (bnot ty (bor ty y x)) (band ty x y))) (bnot ty (bxor ty x y)))
686+
(rule (simplify (bor ty (band ty y x) (bnot ty (bor ty x y)))) (bnot ty (bxor ty x y)))
687+
(rule (simplify (bor ty (bnot ty (bor ty x y)) (band ty y x))) (bnot ty (bxor ty x y)))
688+
(rule (simplify (bor ty (band ty y x) (bnot ty (bor ty y x)))) (bnot ty (bxor ty x y)))
689+
(rule (simplify (bor ty (bnot ty (bor ty y x)) (band ty y x))) (bnot ty (bxor ty x y)))
690+
691+
;; (~x | y) ^ (x ^ y) --> x | ~y
692+
(rule (simplify (bxor ty (bor ty (bnot ty x) y) (bxor ty x y))) (bor ty x (bnot ty y)))
693+
(rule (simplify (bxor ty (bxor ty x y) (bor ty (bnot ty x) y))) (bor ty x (bnot ty y)))
694+
(rule (simplify (bxor ty (bor ty (bnot ty x) y) (bxor ty y x))) (bor ty x (bnot ty y)))
695+
(rule (simplify (bxor ty (bxor ty y x) (bor ty (bnot ty x) y))) (bor ty x (bnot ty y)))
696+
(rule (simplify (bxor ty (bor ty y (bnot ty x)) (bxor ty x y))) (bor ty x (bnot ty y)))
697+
(rule (simplify (bxor ty (bxor ty x y) (bor ty y (bnot ty x)))) (bor ty x (bnot ty y)))
698+
(rule (simplify (bxor ty (bor ty y (bnot ty x)) (bxor ty y x))) (bor ty x (bnot ty y)))
699+
(rule (simplify (bxor ty (bxor ty y x) (bor ty y (bnot ty x)))) (bor ty x (bnot ty y)))

cranelift/filetests/filetests/egraph/fold-bitops.clif

Lines changed: 228 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,234 @@ block0(v0: i32, v1: i32):
263263

264264
; function %test_bxor_band_bnot_to_bnot_band(i32, i32) -> i32 fast {
265265
; block0(v0: i32, v1: i32):
266+
; v8 = band v1, v0
267+
; v12 = bnot v8
268+
; return v12
269+
; }
270+
271+
;; (~x & y) ^ x --> x | y
272+
function %test_bxor_band_bnot_x(i32, i32) -> i32 fast {
273+
block0(v0: i32, v1: i32):
274+
v2 = bnot v0
275+
v3 = band v2, v1
276+
v4 = bxor v3, v0
277+
return v4
278+
}
279+
280+
; function %test_bxor_band_bnot_x(i32, i32) -> i32 fast {
281+
; block0(v0: i32, v1: i32):
282+
; v5 = bor v0, v1
283+
; return v5
284+
; }
285+
286+
;; (x | y) & ~(x ^ y) --> x & y
287+
function %test_band_bor_bnot_bxor(i32, i32) -> i32 fast {
288+
block0(v0: i32, v1: i32):
289+
v2 = bor v0, v1
290+
v3 = bxor v0, v1
291+
v4 = bnot v3
292+
v5 = band v2, v4
293+
return v5
294+
}
295+
296+
; function %test_band_bor_bnot_bxor(i32, i32) -> i32 fast {
297+
; block0(v0: i32, v1: i32):
298+
; v7 = band v1, v0
299+
; return v7
300+
; }
301+
302+
;; x | ~(x ^ y) --> x | ~y
303+
function %test_bor_bnot_bxor(i32, i32) -> i32 fast {
304+
block0(v0: i32, v1: i32):
305+
v2 = bxor v0, v1
306+
v3 = bnot v2
307+
v4 = bor v0, v3
308+
return v4
309+
}
310+
311+
; function %test_bor_bnot_bxor(i32, i32) -> i32 fast {
312+
; block0(v0: i32, v1: i32):
313+
; v5 = bnot v1
314+
; v6 = bor v0, v5
315+
; return v6
316+
; }
317+
318+
;; x | ((~x) ^ y) --> x | ~y
319+
function %test_bor_bxor_bnot_x(i32, i32) -> i32 fast {
320+
block0(v0: i32, v1: i32):
321+
v2 = bnot v0
322+
v3 = bxor v2, v1
323+
v4 = bor v0, v3
324+
return v4
325+
}
326+
327+
; function %test_bor_bxor_bnot_x(i32, i32) -> i32 fast {
328+
; block0(v0: i32, v1: i32):
329+
; v5 = bnot v1
330+
; v6 = bor v0, v5
331+
; return v6
332+
; }
333+
334+
;; x & ~(x ^ y) --> x & y
335+
function %test_band_bnot_bxor(i32, i32) -> i32 fast {
336+
block0(v0: i32, v1: i32):
337+
v2 = bxor v0, v1
338+
v3 = bnot v2
339+
v4 = band v0, v3
340+
return v4
341+
}
342+
343+
; function %test_band_bnot_bxor(i32, i32) -> i32 fast {
344+
; block0(v0: i32, v1: i32):
345+
; v5 = band v0, v1
346+
; return v5
347+
; }
348+
349+
;; x & ((~x) ^ y) --> x & y
350+
function %test_band_bxor_bnot_x(i32, i32) -> i32 fast {
351+
block0(v0: i32, v1: i32):
352+
v2 = bnot v0
353+
v3 = bxor v2, v1
354+
v4 = band v0, v3
355+
return v4
356+
}
357+
358+
; function %test_band_bxor_bnot_x(i32, i32) -> i32 fast {
359+
; block0(v0: i32, v1: i32):
360+
; v5 = band v0, v1
361+
; return v5
362+
; }
363+
364+
;; (x | y) | (x ^ y) --> (x | y)
365+
function %test_bor_bor_bxor_same(i32, i32) -> i32 fast {
366+
block0(v0: i32, v1: i32):
367+
v2 = bor v0, v1
368+
v3 = bxor v0, v1
369+
v4 = bor v2, v3
370+
return v4
371+
}
372+
373+
; function %test_bor_bor_bxor_same(i32, i32) -> i32 fast {
374+
; block0(v0: i32, v1: i32):
375+
; v5 = bor v1, v0
376+
; return v5
377+
; }
378+
379+
;; (x ^ y) & ((y ^ z) ^ x) --> (x ^ y) & ~z
380+
function %test_band_bxor_bxor_bnot(i32, i32, i32) -> i32 fast {
381+
block0(v0: i32, v1: i32, v2: i32):
382+
v3 = bxor v0, v1
383+
v4 = bxor v1, v2
384+
v5 = bxor v4, v0
385+
v6 = band v3, v5
386+
return v6
387+
}
388+
389+
; function %test_band_bxor_bxor_bnot(i32, i32, i32) -> i32 fast {
390+
; block0(v0: i32, v1: i32, v2: i32):
391+
; v3 = bxor v0, v1
392+
; v7 = bnot v2
393+
; v8 = band v3, v7
394+
; return v8
395+
; }
396+
397+
;; (~x & y) ^ z --> (x & y) ^ (z ^ y)
398+
function %test_bxor_band_bnot_general(i32, i32, i32) -> i32 fast {
399+
block0(v0: i32, v1: i32, v2: i32):
400+
v3 = bnot v0
401+
v4 = band v3, v1
402+
v5 = bxor v4, v2
403+
return v5
404+
}
405+
406+
; function %test_bxor_band_bnot_general(i32, i32, i32) -> i32 fast {
407+
; block0(v0: i32, v1: i32, v2: i32):
266408
; v6 = band v0, v1
267-
; v7 = bnot v6
409+
; v7 = bxor v2, v1
410+
; v8 = bxor v6, v7
411+
; return v8
412+
; }
413+
414+
;; ~x - ~y --> y - x
415+
function %test_isub_bnot_bnot(i32, i32) -> i32 fast {
416+
block0(v0: i32, v1: i32):
417+
v2 = bnot v0
418+
v3 = bnot v1
419+
v4 = isub v2, v3
420+
return v4
421+
}
422+
423+
; function %test_isub_bnot_bnot(i32, i32) -> i32 fast {
424+
; block0(v0: i32, v1: i32):
425+
; v5 = isub v1, v0
426+
; return v5
427+
; }
428+
429+
;; (~x & y) | ~(x | y) --> ~x
430+
function %test_bor_band_bnot_bor_to_bnot(i32, i32) -> i32 fast {
431+
block0(v0: i32, v1: i32):
432+
v2 = bnot v0
433+
v3 = band v2, v1
434+
v4 = bor v0, v1
435+
v5 = bnot v4
436+
v6 = bor v3, v5
437+
return v6
438+
}
439+
440+
; function %test_bor_band_bnot_bor_to_bnot(i32, i32) -> i32 fast {
441+
; block0(v0: i32, v1: i32):
442+
; v2 = bnot v0
443+
; return v2
444+
; }
445+
446+
;; (~x | y) & ~(x & y) --> ~x
447+
function %test_band_bor_bnot_band_to_bnot(i32, i32) -> i32 fast {
448+
block0(v0: i32, v1: i32):
449+
v2 = bnot v0
450+
v3 = bor v2, v1
451+
v4 = band v0, v1
452+
v5 = bnot v4
453+
v6 = band v3, v5
454+
return v6
455+
}
456+
457+
; function %test_band_bor_bnot_band_to_bnot(i32, i32) -> i32 fast {
458+
; block0(v0: i32, v1: i32):
459+
; v2 = bnot v0
460+
; return v2
461+
; }
462+
463+
;; (x & y) | ~(x | y) --> ~(x ^ y)
464+
function %test_bor_band_bnot_bor_to_bnot_bxor(i32, i32) -> i32 fast {
465+
block0(v0: i32, v1: i32):
466+
v2 = band v0, v1
467+
v3 = bor v0, v1
468+
v4 = bnot v3
469+
v5 = bor v2, v4
470+
return v5
471+
}
472+
473+
; function %test_bor_band_bnot_bor_to_bnot_bxor(i32, i32) -> i32 fast {
474+
; block0(v0: i32, v1: i32):
475+
; v8 = bxor v1, v0
476+
; v9 = bnot v8
477+
; return v9
478+
; }
479+
480+
;; (~x | y) ^ (x ^ y) --> x | ~y
481+
function %test_bxor_bor_bnot_bxor(i32, i32) -> i32 fast {
482+
block0(v0: i32, v1: i32):
483+
v2 = bnot v0
484+
v3 = bor v2, v1
485+
v4 = bxor v0, v1
486+
v5 = bxor v3, v4
487+
return v5
488+
}
489+
490+
; function %test_bxor_bor_bnot_bxor(i32, i32) -> i32 fast {
491+
; block0(v0: i32, v1: i32):
492+
; v6 = bnot v1
493+
; v7 = bor v0, v6
268494
; return v7
269495
; }
496+

0 commit comments

Comments
 (0)