From c0166f78f9f9954320b52ef3be258b97410ef15c Mon Sep 17 00:00:00 2001 From: Karl Meakin Date: Tue, 11 Apr 2023 23:41:19 +0100 Subject: [PATCH] ISLE: simplify select/bitselect when both choices are the same (#6141) --- cranelift/codegen/src/opts/selects.isle | 4 ++++ cranelift/filetests/filetests/egraph/select.clif | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/cranelift/codegen/src/opts/selects.isle b/cranelift/codegen/src/opts/selects.isle index c8fbc09a90..1fcc6c8d64 100644 --- a/cranelift/codegen/src/opts/selects.isle +++ b/cranelift/codegen/src/opts/selects.isle @@ -1,5 +1,9 @@ ;; `select`/`bitselect`-related rewrites +;; remove select when both choices are the same +(rule (simplify (select ty _ x x)) x) +(rule (simplify (bitselect ty _ x x)) 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)) diff --git a/cranelift/filetests/filetests/egraph/select.clif b/cranelift/filetests/filetests/egraph/select.clif index e8a225b4de..0e7a2be288 100644 --- a/cranelift/filetests/filetests/egraph/select.clif +++ b/cranelift/filetests/filetests/egraph/select.clif @@ -5,6 +5,20 @@ target aarch64 target s390x target riscv64 +function %select_self(i8, i32) -> i32 { +block0(v0: i8, v1: i32): + v2 = select v0, v1, v1 + return v2 + ; check: return v1 +} + +function %bitselect_self(i32, i32) -> i32 { +block0(v0: i32, v1: i32): + v2 = bitselect v0, v1, v1 + return v2 + ; check: return v1 +} + function %select_sgt_to_smax(i32, i32) -> i32 { block0(v0: i32, v1: i32): v2 = icmp sgt v0, v1