[llvm] r267351 - [X86][InstCombine] Tidyup PSHUFB -> shufflevector conversion to helper function. NFCI.

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 24 10:00:35 PDT 2016


Author: rksimon
Date: Sun Apr 24 12:00:34 2016
New Revision: 267351

URL: http://llvm.org/viewvc/llvm-project?rev=267351&view=rev
Log:
[X86][InstCombine] Tidyup PSHUFB -> shufflevector conversion to helper function. NFCI.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp?rev=267351&r1=267350&r2=267351&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp Sun Apr 24 12:00:34 2016
@@ -590,6 +590,46 @@ static Value *simplifyX86insertq(Intrins
   return nullptr;
 }
 
+/// Attempt to convert pshufb* to shufflevector if the mask is constant.
+static Value *simplifyX86pshufb(const IntrinsicInst &II,
+                                InstCombiner::BuilderTy &Builder) {
+  auto *V = II.getArgOperand(1);
+  auto *VTy = cast<VectorType>(V->getType());
+  unsigned NumElts = VTy->getNumElements();
+  assert((NumElts == 16 || NumElts == 32) &&
+         "Unexpected number of elements in shuffle mask!");
+  // Initialize the resulting shuffle mask to all zeroes.
+  uint32_t Indexes[32] = {0};
+
+  if (auto *Mask = dyn_cast<ConstantDataVector>(V)) {
+    // Each byte in the shuffle control mask forms an index to permute the
+    // corresponding byte in the destination operand.
+    for (unsigned I = 0; I < NumElts; ++I) {
+      int8_t Index = Mask->getElementAsInteger(I);
+      // If the most significant bit (bit[7]) of each byte of the shuffle
+      // control mask is set, then zero is written in the result byte.
+      // The zero vector is in the right-hand side of the resulting
+      // shufflevector.
+
+      // The value of each index is the least significant 4 bits of the
+      // shuffle control byte.
+      Indexes[I] = (Index < 0) ? NumElts : Index & 0xF;
+    }
+  } else if (!isa<ConstantAggregateZero>(V))
+    return nullptr;
+
+  // The value of each index for the high 128-bit lane is the least
+  // significant 4 bits of the respective shuffle control byte.
+  for (unsigned I = 16; I < NumElts; ++I)
+    Indexes[I] += I & 0xF0;
+
+  auto ShuffleMask =
+      ConstantDataVector::get(V->getContext(), makeArrayRef(Indexes, NumElts));
+  auto V1 = II.getArgOperand(0);
+  auto V2 = Constant::getNullValue(II.getType());
+  return Builder.CreateShuffleVector(V1, V2, ShuffleMask);
+}
+
 /// The shuffle mask for a perm2*128 selects any two halves of two 256-bit
 /// source vectors, unless a zero bit is set. If a zero bit is set,
 /// then ignore that half of the mask and clear that half of the vector.
@@ -1587,45 +1627,10 @@ Instruction *InstCombiner::visitCallInst
   }
 
   case Intrinsic::x86_ssse3_pshuf_b_128:
-  case Intrinsic::x86_avx2_pshuf_b: {
-    // Turn pshufb(V1,mask) -> shuffle(V1,Zero,mask) if mask is a constant.
-    auto *V = II->getArgOperand(1);
-    auto *VTy = cast<VectorType>(V->getType());
-    unsigned NumElts = VTy->getNumElements();
-    assert((NumElts == 16 || NumElts == 32) &&
-           "Unexpected number of elements in shuffle mask!");
-    // Initialize the resulting shuffle mask to all zeroes.
-    uint32_t Indexes[32] = {0};
-
-    if (auto *Mask = dyn_cast<ConstantDataVector>(V)) {
-      // Each byte in the shuffle control mask forms an index to permute the
-      // corresponding byte in the destination operand.
-      for (unsigned I = 0; I < NumElts; ++I) {
-        int8_t Index = Mask->getElementAsInteger(I);
-        // If the most significant bit (bit[7]) of each byte of the shuffle
-        // control mask is set, then zero is written in the result byte.
-        // The zero vector is in the right-hand side of the resulting
-        // shufflevector.
-
-        // The value of each index is the least significant 4 bits of the
-        // shuffle control byte.
-        Indexes[I] = (Index < 0) ? NumElts : Index & 0xF;
-      }
-    } else if (!isa<ConstantAggregateZero>(V))
-      break;
-
-    // The value of each index for the high 128-bit lane is the least
-    // significant 4 bits of the respective shuffle control byte.
-    for (unsigned I = 16; I < NumElts; ++I)
-      Indexes[I] += I & 0xF0;
-
-    auto NewC = ConstantDataVector::get(V->getContext(),
-                                        makeArrayRef(Indexes, NumElts));
-    auto V1 = II->getArgOperand(0);
-    auto V2 = Constant::getNullValue(II->getType());
-    auto Shuffle = Builder->CreateShuffleVector(V1, V2, NewC);
-    return replaceInstUsesWith(CI, Shuffle);
-  }
+  case Intrinsic::x86_avx2_pshuf_b:
+    if (Value *V = simplifyX86pshufb(*II, *Builder))
+      return replaceInstUsesWith(*II, V);
+    break;
 
   case Intrinsic::x86_avx_vpermilvar_ps:
   case Intrinsic::x86_avx_vpermilvar_ps_256:
@@ -2030,7 +2035,7 @@ Instruction *InstCombiner::visitCallInst
       if (isa<ConstantPointerNull>(DerivedPtr))
         // Use null-pointer of gc_relocate's type to replace it.
         return replaceInstUsesWith(*II, ConstantPointerNull::get(PT));
-      
+
       // isKnownNonNull -> nonnull attribute
       if (isKnownNonNullAt(DerivedPtr, II, DT, TLI))
         II->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);




More information about the llvm-commits mailing list