x64: Add a smattering of lowerings for shuffle specializations (#5930)

* x64: Add lowerings for `punpck{h,l}wd`

Add some special cases for `shuffle` for more specialized x86
instructions.

* x64: Add `shuffle` lowerings for `pshufd`

This commit adds special-cased lowerings for the x64 `shuffle`
instruction when the `pshufd` instruction alone is necessary. This is
possible when the shuffle immediate permutes 32-bit values within one of
the vector inputs of the `shuffle` instruction, but not both.

* x64: Add shuffle lowerings for `punpck{h,l}{q,}dq`

This adds specific permutations for some x86 instructions which
specifically interleave high/low bytes for 32 and 64-bit values. This
corresponds to the preexisting specific lowerings for interleaving 8 and
16-bit values.

* x64: Add `shuffle` lowerings for `shufps`

This commit adds targeted lowerings for the `shuffle` instruction that
match the pattern that `shufps` supports. The `shufps` instruction
selects two elements from the first vector and two elements from the
second vector which means while it's not generally applicable it should
still be more useful than the catch-all lowering of `shuffle`.

* x64: Add shuffle support for `pshuf{l,h}w`

This commit adds special lowering cases for these instructions which
permute 16-bit values within a 128-bit value either within the upper or
lower half of the 128-bit value.

* x64: Specialize `shuffle` with an all-zeros immediate

Instead of loading the all-zeros immediate from a rip-relative address
at the end of the function instead generate a zero with a `pxor`
instruction and then use `pshufb` to do the broadcast.

* Review comments
This commit is contained in:
Alex Crichton
2023-03-09 16:58:19 -06:00
committed by GitHub
parent 8a2bf29444
commit 1c3a1bda6c
10 changed files with 1332 additions and 10 deletions

View File

@@ -585,9 +585,86 @@ macro_rules! isle_lower_prelude_methods {
.collect();
self.lower_ctx.gen_return(rets);
}
/// Attempts to interpret the shuffle immediate `imm` as a shuffle of
/// 32-bit lanes, returning four integers, each of which is less than 8,
/// which represents a permutation of 32-bit lanes as specified by
/// `imm`.
///
/// For example the shuffle immediate
///
/// `0 1 2 3 8 9 10 11 16 17 18 19 24 25 26 27`
///
/// would return `Some((0, 2, 4, 6))`.
fn shuffle32_from_imm(&mut self, imm: Immediate) -> Option<(u8, u8, u8, u8)> {
use crate::machinst::isle::shuffle_imm_as_le_lane_idx;
let bytes = self.lower_ctx.get_immediate_data(imm).as_slice();
Some((
shuffle_imm_as_le_lane_idx(4, &bytes[0..4])?,
shuffle_imm_as_le_lane_idx(4, &bytes[4..8])?,
shuffle_imm_as_le_lane_idx(4, &bytes[8..12])?,
shuffle_imm_as_le_lane_idx(4, &bytes[12..16])?,
))
}
/// Same as `shuffle32_from_imm`, but for 16-bit lane shuffles.
fn shuffle16_from_imm(
&mut self,
imm: Immediate,
) -> Option<(u8, u8, u8, u8, u8, u8, u8, u8)> {
use crate::machinst::isle::shuffle_imm_as_le_lane_idx;
let bytes = self.lower_ctx.get_immediate_data(imm).as_slice();
Some((
shuffle_imm_as_le_lane_idx(2, &bytes[0..2])?,
shuffle_imm_as_le_lane_idx(2, &bytes[2..4])?,
shuffle_imm_as_le_lane_idx(2, &bytes[4..6])?,
shuffle_imm_as_le_lane_idx(2, &bytes[6..8])?,
shuffle_imm_as_le_lane_idx(2, &bytes[8..10])?,
shuffle_imm_as_le_lane_idx(2, &bytes[10..12])?,
shuffle_imm_as_le_lane_idx(2, &bytes[12..14])?,
shuffle_imm_as_le_lane_idx(2, &bytes[14..16])?,
))
}
};
}
/// Returns the `size`-byte lane referred to by the shuffle immediate specified
/// in `bytes`.
///
/// This helper is used by `shuffleNN_from_imm` above and is used to interpret a
/// byte-based shuffle as a higher-level shuffle of bigger lanes. This will see
/// if the `bytes` specified, which must have `size` length, specifies a lane in
/// vectors aligned to a `size`-byte boundary.
///
/// Returns `None` if `bytes` doesn't specify a `size`-byte lane aligned
/// appropriately, or returns `Some(n)` where `n` is the index of the lane being
/// shuffled.
pub fn shuffle_imm_as_le_lane_idx(size: u8, bytes: &[u8]) -> Option<u8> {
assert_eq!(bytes.len(), usize::from(size));
// The first index in `bytes` must be aligned to a `size` boundary for the
// bytes to be a valid specifier for a lane of `size` bytes.
if bytes[0] % size != 0 {
return None;
}
// Afterwards the bytes must all be one larger than the prior to specify a
// contiguous sequence of bytes that's being shuffled. Basically `bytes`
// must refer to the entire `size`-byte lane, in little-endian order.
for i in 0..size - 1 {
let idx = usize::from(i);
if bytes[idx] + 1 != bytes[idx + 1] {
return None;
}
}
// All of the `bytes` are in-order, meaning that this is a valid shuffle
// immediate to specify a lane of `size` bytes. The index, when viewed as
// `size`-byte immediates, will be the first byte divided by the byte size.
Some(bytes[0] / size)
}
/// Helpers specifically for machines that use ABICaller.
#[macro_export]
#[doc(hidden)]