[InstCombine] canonicalize trunc + insert as bitcast + shuffle, part 1
authorSanjay Patel <spatel@rotateright.com>
Wed, 30 Nov 2022 18:04:46 +0000 (13:04 -0500)
committerSanjay Patel <spatel@rotateright.com>
Wed, 30 Nov 2022 18:22:04 +0000 (13:22 -0500)
commita4c466766db77cd1fb42d7f98f32bb87a3d38829
treef038ce98f5115ebda15a7743f342cb68345a3bb0
parentb647d8f95d194e15d0335a644947c650f97041df
[InstCombine] canonicalize trunc + insert as bitcast + shuffle, part 1

This is the main patch for converting a truncated scalar that is
inserted into a vector to bitcast+shuffle. We could go either way
on patterns like this, but this direction will allow collapsing a
pair of these sequences on the motivating example from issue

The patch is split into 3 parts to make it easier to see the
progression of tests diffs. We allow inserting/shuffling into a
different size vector for flexibility, so there are several test
variations. The length-changing is handled by shortening/padding
the shuffle mask with undef elements.

In part 1, handle the basic pattern:
inselt undef, (trunc T), IndexC --> shuffle (bitcast T), IdentityMask

Proof for the endian-dependency behaving as expected:
https://alive2.llvm.org/ce/z/BsA7yC

The TODO items for handling shifts and insert into an arbitrary base
vector value are implemented as follow-ups.

Differential Revision: https://reviews.llvm.org/D138872
llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
llvm/test/Transforms/InstCombine/insert-trunc.ll
llvm/test/Transforms/InstCombine/vec_phi_extract-inseltpoison.ll
llvm/test/Transforms/InstCombine/vec_phi_extract.ll
llvm/test/Transforms/LoopVectorize/ARM/pointer_iv.ll
llvm/test/Transforms/PhaseOrdering/X86/vec-load-combine.ll