Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cranelift/egraphs: more select/bitselect optimizations #6214

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cranelift/codegen/src/opts/selects.isle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@
(rule (simplify (select ty _ x x)) x)
(rule (simplify (bitselect ty _ x x)) x)

;; bmask of a truthy scalar integer creates a mask that causes bitselect to
;; select values fully from one input or the other, same as select.
(rule (simplify (bitselect ty (bmask ty c) x y)) (select ty c x y))

;; bmask and negation both preserve truthiness, so they can be removed from any
;; instruction that only considers truthiness.
(rule (simplify (bmask ty (bmask ty c))) (bmask ty c))
(rule (simplify (bmask ty (ineg ty c))) (bmask ty c))
(rule (simplify (select ty (bmask ty c) x y)) (select ty c x y))
(rule (simplify (select ty (ineg ty c) x y)) (select ty c x y))
(rule (simplify (select_spectre_guard ty (bmask ty c) x y)) (select_spectre_guard ty c x y))
(rule (simplify (select_spectre_guard ty (ineg ty c) x y)) (select_spectre_guard ty c x y))

;; bitwise-not of bmask inverts truthiness, swapping branches
(rule (simplify (select ty (bnot ty (bmask ty c)) x y)) (select ty c y x))
(rule (simplify (select_spectre_guard ty (bnot ty (bmask ty c)) x y)) (select_spectre_guard ty c y x))
(rule (simplify (bitselect ty (bnot ty c) x y)) (bitselect ty c y x))

;; Transform select-of-icmp into {u,s}{min,max} instructions where possible.
(rule (simplify (select ty (sgt _ x y) x y)) (smax ty x y))
(rule (simplify (select ty (sge _ x y) x y)) (smax ty x y))
Expand Down