[PATCH] D71488: [InstSimplify] fold splat of inserted constant to vector constant

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Dec 15 06:36:59 PST 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rG6080387f136a: [InstSimplify] fold splat of inserted constant to vector constant (authored by spatel).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D71488/new/

https://reviews.llvm.org/D71488

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstCombine/insert-extract-shuffle.ll
  llvm/test/Transforms/InstSimplify/shufflevector.ll


Index: llvm/test/Transforms/InstSimplify/shufflevector.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/shufflevector.ll
+++ llvm/test/Transforms/InstSimplify/shufflevector.ll
@@ -250,9 +250,7 @@
 
 define <5 x i8> @splat_inserted_constant(<4 x i8> %x) {
 ; CHECK-LABEL: @splat_inserted_constant(
-; CHECK-NEXT:    [[INS3:%.*]] = insertelement <4 x i8> [[X:%.*]], i8 42, i64 3
-; CHECK-NEXT:    [[SPLAT5:%.*]] = shufflevector <4 x i8> [[INS3]], <4 x i8> undef, <5 x i32> <i32 3, i32 3, i32 3, i32 3, i32 3>
-; CHECK-NEXT:    ret <5 x i8> [[SPLAT5]]
+; CHECK-NEXT:    ret <5 x i8> <i8 42, i8 42, i8 42, i8 42, i8 42>
 ;
   %ins3 = insertelement <4 x i8> %x, i8 42, i64 3
   %splat5 = shufflevector <4 x i8> %ins3, <4 x i8> undef, <5 x i32> <i32 3, i32 3, i32 3, i32 3, i32 3>
@@ -261,9 +259,7 @@
 
 define <4 x float> @splat_inserted_constant_undef_elt(<4 x float> %x) {
 ; CHECK-LABEL: @splat_inserted_constant_undef_elt(
-; CHECK-NEXT:    [[INS1:%.*]] = insertelement <4 x float> [[X:%.*]], float 1.200000e+01, i32 1
-; CHECK-NEXT:    [[SPLAT1:%.*]] = shufflevector <4 x float> [[INS1]], <4 x float> undef, <4 x i32> <i32 1, i32 1, i32 undef, i32 1>
-; CHECK-NEXT:    ret <4 x float> [[SPLAT1]]
+; CHECK-NEXT:    ret <4 x float> <float 1.200000e+01, float 1.200000e+01, float undef, float 1.200000e+01>
 ;
   %ins1 = insertelement <4 x float> %x, float 12.0, i32 1
   %splat1 = shufflevector <4 x float> %ins1, <4 x float> undef, <4 x i32> <i32 1, i32 1, i32 undef, i32 1>
@@ -272,9 +268,7 @@
 
 define <2 x i8> @splat_inserted_constant_not_canonical(<3 x i8> %x, <3 x i8> %y) {
 ; CHECK-LABEL: @splat_inserted_constant_not_canonical(
-; CHECK-NEXT:    [[INS2:%.*]] = insertelement <3 x i8> [[X:%.*]], i8 23, i7 2
-; CHECK-NEXT:    [[SPLAT2:%.*]] = shufflevector <3 x i8> [[Y:%.*]], <3 x i8> [[INS2]], <2 x i32> <i32 undef, i32 5>
-; CHECK-NEXT:    ret <2 x i8> [[SPLAT2]]
+; CHECK-NEXT:    ret <2 x i8> <i8 undef, i8 23>
 ;
   %ins2 = insertelement <3 x i8> %x, i8 23, i7 2
   %splat2 = shufflevector <3 x i8> %y, <3 x i8> %ins2, <2 x i32> <i32 undef, i32 5>
Index: llvm/test/Transforms/InstCombine/insert-extract-shuffle.ll
===================================================================
--- llvm/test/Transforms/InstCombine/insert-extract-shuffle.ll
+++ llvm/test/Transforms/InstCombine/insert-extract-shuffle.ll
@@ -725,8 +725,7 @@
 define <4 x float> @splat_constant(<4 x float> %x) {
 ; CHECK-LABEL: @splat_constant(
 ; CHECK-NEXT:    [[INS3:%.*]] = insertelement <4 x float> [[X:%.*]], float 3.000000e+00, i32 3
-; CHECK-NEXT:    [[SPLAT3:%.*]] = shufflevector <4 x float> [[INS3]], <4 x float> undef, <4 x i32> <i32 3, i32 3, i32 3, i32 3>
-; CHECK-NEXT:    [[R:%.*]] = fadd <4 x float> [[INS3]], [[SPLAT3]]
+; CHECK-NEXT:    [[R:%.*]] = fadd <4 x float> [[INS3]], <float 3.000000e+00, float 3.000000e+00, float 3.000000e+00, float 3.000000e+00>
 ; CHECK-NEXT:    ret <4 x float> [[R]]
 ;
   %ins3 = insertelement <4 x float> %x, float 3.0, i32 3
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4452,6 +4452,30 @@
     ShuffleVectorInst::commuteShuffleMask(Indices, InVecNumElts);
   }
 
+  // A splat of an inserted scalar constant becomes a vector constant:
+  // shuf (inselt ?, C, IndexC), undef, <IndexC, IndexC...> --> <C, C...>
+  // NOTE: We may have commuted above, so analyze the updated Indices, not the
+  //       original mask constant.
+  Constant *C;
+  ConstantInt *IndexC;
+  if (match(Op0, m_InsertElement(m_Value(), m_Constant(C),
+                                 m_ConstantInt(IndexC)))) {
+    // Match a splat shuffle mask of the insert index allowing undef elements.
+    int InsertIndex = IndexC->getZExtValue();
+    if (all_of(Indices, [InsertIndex](int MaskElt) {
+          return MaskElt == InsertIndex || MaskElt == -1;
+        })) {
+      assert(isa<UndefValue>(Op1) && "Expected undef operand 1 for splat");
+
+      // Shuffle mask undefs become undefined constant result elements.
+      SmallVector<Constant *, 16> VecC(MaskNumElts, C);
+      for (unsigned i = 0; i != MaskNumElts; ++i)
+        if (Indices[i] == -1)
+          VecC[i] = UndefValue::get(C->getType());
+      return ConstantVector::get(VecC);
+    }
+  }
+
   // A shuffle of a splat is always the splat itself. Legal if the shuffle's
   // value type is same as the input vectors' type.
   if (auto *OpShuf = dyn_cast<ShuffleVectorInst>(Op0))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71488.233965.patch
Type: text/x-patch
Size: 4590 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191215/395de6ac/attachment.bin>


More information about the llvm-commits mailing list