Remove the Cranelift vselect instruction (#5918)

* Remove the Cranelift `vselect` instruction

This instruction is documented as selecting lanes based on the "truthy"
value of the condition lane, but the current status of the
implementation of this instruction is:

* x64 - uses the high bit for `f32x4` and `f64x2` and otherwise uses the
  high bit of each byte doing a byte-wise lane select rather than
  whatever the controlling type is.

* AArch64 - this is the same as `bitselect` which is a bit-wise
  selection rather than a lane-wise selection.

* s390x - this is the same as AArch64, a bit-wise selection rather than
  lane-wise.

* interpreter - the interpreter implements the documented semantics of
  selecting based on "truthy" values.

Coupled with the status of the implementation is the fact that this
instruction is not used by WebAssembly SIMD today either. The only use
of this instruction in Cranelift is the nan-canonicalization pass. By
moving nan-canonicalization to `bitselect`, since that has the desired
semantics, there's no longer any need for `vselect`.

Given this situation this commit subsqeuently removes `vselect` and all
usage of it throughout Cranelift.

Closes #5917

* Review comments

* Bring back vselect opts as bitselect opts

* Clean up vselect usage in the interpreter

* Move bitcast in nan canonicalization

* Add a comment about float optimization
This commit is contained in:
Alex Crichton
2023-03-07 18:42:05 -06:00
committed by GitHub
parent fc45ccc125
commit 07518dfd36
14 changed files with 163 additions and 333 deletions

View File

@@ -454,56 +454,56 @@
(select ty (icmp _ (IntCC.UnsignedGreaterThanOrEqual) x y) y x))
(umin ty x y))
;; Transform vselect-of-icmp into {u,s}{min,max} instructions where possible.
;; Transform bitselect-of-icmp into {u,s}{min,max} instructions where possible.
(rule (simplify
(vselect ty (icmp _ (IntCC.SignedGreaterThan) x y) x y))
(bitselect ty (icmp _ (IntCC.SignedGreaterThan) x y) x y))
(smax ty x y))
(rule (simplify
(vselect ty (icmp _ (IntCC.SignedGreaterThanOrEqual) x y) x y))
(bitselect ty (icmp _ (IntCC.SignedGreaterThanOrEqual) x y) x y))
(smax ty x y))
(rule (simplify
(vselect ty (icmp _ (IntCC.UnsignedGreaterThan) x y) x y))
(bitselect ty (icmp _ (IntCC.UnsignedGreaterThan) x y) x y))
(umax ty x y))
(rule (simplify
(vselect ty (icmp _ (IntCC.UnsignedGreaterThanOrEqual) x y) x y))
(bitselect ty (icmp _ (IntCC.UnsignedGreaterThanOrEqual) x y) x y))
(umax ty x y))
(rule (simplify
(vselect ty (icmp _ (IntCC.SignedLessThan) x y) x y))
(bitselect ty (icmp _ (IntCC.SignedLessThan) x y) x y))
(smin ty x y))
(rule (simplify
(vselect ty (icmp _ (IntCC.SignedLessThanOrEqual) x y) x y))
(bitselect ty (icmp _ (IntCC.SignedLessThanOrEqual) x y) x y))
(smin ty x y))
(rule (simplify
(vselect ty (icmp _ (IntCC.UnsignedLessThan) x y) x y))
(bitselect ty (icmp _ (IntCC.UnsignedLessThan) x y) x y))
(umin ty x y))
(rule (simplify
(vselect ty (icmp _ (IntCC.UnsignedLessThanOrEqual) x y) x y))
(bitselect ty (icmp _ (IntCC.UnsignedLessThanOrEqual) x y) x y))
(umin ty x y))
;; These are the same rules as above, but when the operands for select are swapped
(rule (simplify
(vselect ty (icmp _ (IntCC.SignedLessThan) x y) y x))
(bitselect ty (icmp _ (IntCC.SignedLessThan) x y) y x))
(smax ty x y))
(rule (simplify
(vselect ty (icmp _ (IntCC.SignedLessThanOrEqual) x y) y x))
(bitselect ty (icmp _ (IntCC.SignedLessThanOrEqual) x y) y x))
(smax ty x y))
(rule (simplify
(vselect ty (icmp _ (IntCC.UnsignedLessThan) x y) y x))
(bitselect ty (icmp _ (IntCC.UnsignedLessThan) x y) y x))
(umax ty x y))
(rule (simplify
(vselect ty (icmp _ (IntCC.UnsignedLessThanOrEqual) x y) y x))
(bitselect ty (icmp _ (IntCC.UnsignedLessThanOrEqual) x y) y x))
(umax ty x y))
(rule (simplify
(vselect ty (icmp _ (IntCC.SignedGreaterThan) x y) y x))
(bitselect ty (icmp _ (IntCC.SignedGreaterThan) x y) y x))
(smin ty x y))
(rule (simplify
(vselect ty (icmp _ (IntCC.SignedGreaterThanOrEqual) x y) y x))
(bitselect ty (icmp _ (IntCC.SignedGreaterThanOrEqual) x y) y x))
(smin ty x y))
(rule (simplify
(vselect ty (icmp _ (IntCC.UnsignedGreaterThan) x y) y x))
(bitselect ty (icmp _ (IntCC.UnsignedGreaterThan) x y) y x))
(umin ty x y))
(rule (simplify
(vselect ty (icmp _ (IntCC.UnsignedGreaterThanOrEqual) x y) y x))
(bitselect ty (icmp _ (IntCC.UnsignedGreaterThanOrEqual) x y) y x))
(umin ty x y))
;; For floats convert fcmp lt into pseudo_min and gt into pseudo_max
@@ -520,13 +520,9 @@
(select ty (fcmp _ (FloatCC.GreaterThan) x y) x y))
(fmax_pseudo ty x y))
;; Do the same for vectors
(rule (simplify
(vselect ty (fcmp _ (FloatCC.LessThan) x y) x y))
(fmin_pseudo ty x y))
(rule (simplify
(vselect ty (fcmp _ (FloatCC.GreaterThan) x y) x y))
(fmax_pseudo ty x y))
;; TODO: perform this same optimization to `f{min,max}_pseudo` for vectors
;; with the `bitselect` instruction, but the pattern is a bit more complicated
;; due to most bitselects-over-floats having bitcasts.
;; If both of the multiplied arguments to an `fma` are negated then remove
;; both of them since they cancel out.