[PATCH] D31509: [InstCombine] Combine vector shuffles if the same operand can be reused

Keno Fischer via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 30 14:19:11 PDT 2017


loladiro created this revision.

I noticed some spurious vector shuffle chains in my code that in theory could be folded
away completely, but InstCombine refused to do so, because it didn't like forming the intermediate
masks. I see that the last time a relaxation was attempted here (https://reviews.llvm.org/rL180802), it was reverted because
there was no consensus as to whether this was the right place to do that. CCing folks that were
involved in that discussion here. Note this isn't quite the same change, but it's in a similar vein.


https://reviews.llvm.org/D31509

Files:
  lib/Transforms/InstCombine/InstCombineVectorOps.cpp
  test/Transforms/InstCombine/vec_shuffle.ll


Index: test/Transforms/InstCombine/vec_shuffle.ll
===================================================================
--- test/Transforms/InstCombine/vec_shuffle.ll
+++ test/Transforms/InstCombine/vec_shuffle.ll
@@ -463,3 +463,18 @@
   %1 = shufflevector <4 x i32*> %A, <4 x i32*> undef, <2 x i32> <i32 0, i32 1>
   ret <2 x i32*> %1
 }
+
+define <8 x double> @shuffle_after_wide_load(double* %ptr) #0 {
+  %a = bitcast double* %ptr to <8 x double>*
+  %1 = load <8 x double>, <8 x double>* %a, align 16
+; CHECK-LABEL: @shuffle_after_wide_load
+; CHECK-NOT: shufflevector
+  %2 = shufflevector <8 x double> %1, <8 x double> undef, <2 x i32> <i32 0, i32 1>
+  %3 = shufflevector <8 x double> %1, <8 x double> undef, <2 x i32> <i32 2, i32 3>
+  %4 = shufflevector <8 x double> %1, <8 x double> undef, <2 x i32> <i32 4, i32 5>
+  %5 = shufflevector <8 x double> %1, <8 x double> undef, <2 x i32> <i32 6, i32 7>
+  %s1 = shufflevector <2 x double> %2, <2 x double> %3, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %s2 = shufflevector <2 x double> %4, <2 x double> %5, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
+  %s3 = shufflevector <4 x double> %s1, <4 x double> %s2, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
+  ret <8 x double> %s3
+}
Index: lib/Transforms/InstCombine/InstCombineVectorOps.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineVectorOps.cpp
+++ lib/Transforms/InstCombine/InstCombineVectorOps.cpp
@@ -1309,6 +1309,9 @@
   // one instruction, which we know is safe.  This is good for things like
   // turning: (splat(splat)) -> splat, or
   // merge(V[0..n], V[n+1..2n]) -> V[0..2n]
+  // As an exception to the above, we always allow the case v1==v2, which lets
+  // us combine to `x=shuffle(v1,undef,newMask)` since targets should generally
+  // be good enough at single-vector shuffles.
   ShuffleVectorInst* LHSShuffle = dyn_cast<ShuffleVectorInst>(LHS);
   ShuffleVectorInst* RHSShuffle = dyn_cast<ShuffleVectorInst>(RHS);
   if (LHSShuffle)
@@ -1434,7 +1437,8 @@
 
   // If the result mask is equal to one of the original shuffle masks,
   // or is a splat, do the replacement.
-  if (isSplat || newMask == LHSMask || newMask == RHSMask || newMask == Mask) {
+  if (isSplat || newMask == LHSMask || newMask == RHSMask || newMask == Mask ||
+      !newRHS) {
     SmallVector<Constant*, 16> Elts;
     for (unsigned i = 0, e = newMask.size(); i != e; ++i) {
       if (newMask[i] < 0) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31509.93541.patch
Type: text/x-patch
Size: 2490 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170330/8df72f42/attachment.bin>


More information about the llvm-commits mailing list