This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] try to turn shuffle into insertelement
ClosedPublic

Authored by spatel on Oct 22 2018, 9:14 AM.

Details

Summary

shuffle (insert ?, Scalar, IndexC), V1, Mask --> insert V1, Scalar, IndexC'

The motivating case is at least a couple of steps away: I noticed that SLPVectorizer does not analyze shuffles as well as sequences of insert/extract in PR34724:
https://bugs.llvm.org/show_bug.cgi?id=34724
...so SLP may fail to vectorize when source code has shuffles to start with or instcombine has converted insert/extract to shuffles.

Independent of that, an insertelement is always a simpler op for IR analysis vs. a shuffle, so we should transform to insert when possible.

I don't think there's any codegen concern here - if a target can't insert a scalar directly to some fixed element in a vector (x86?), then this should get expanded to the insert+shuffle that we started with.

Diff Detail

Event Timeline

spatel created this revision.Oct 22 2018, 9:14 AM
RKSimon accepted this revision.Oct 30 2018, 4:29 AM

LGTM with one minor

lib/Transforms/InstCombine/InstCombineVectorOps.cpp
1579

Worth returning an ConstantInt*/nullptr instead? Mainly to remove the external IndexC being used for the match at the start of the lambda.

This revision is now accepted and ready to land.Oct 30 2018, 4:29 AM
spatel added inline comments.Oct 30 2018, 7:55 AM
lib/Transforms/InstCombine/InstCombineVectorOps.cpp
1579

We can make this more like a souped-up 'match', so pass in both of the values that we want to capture:

auto isShufflingScalarIntoOp1 = [&](Value *&Scalar, ConstantInt *&IndexC) {
This revision was automatically updated to reflect the committed changes.