[PATCH] D74095: [ConstantFold][SVE] Fold bitcast into splat value for splat vector.

Huihui Zhang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 5 15:40:00 PST 2020


huihuiz created this revision.
huihuiz added reviewers: sdesmalen, efriedma, apazos.
huihuiz added a project: LLVM.
Herald added subscribers: psnobl, rkruppe, hiraditya, tschuett.
huihuiz added a parent revision: D71389: [ConstantFold][SVE] Fix constant folding for bitcast..

Split from D71389 <https://reviews.llvm.org/D71389>, as it depends on D71637 <https://reviews.llvm.org/D71637>.

I am not 100% confident about the general impact of D71637 <https://reviews.llvm.org/D71637>, if we
support constexpr in all OneOps_match, TwoOps_match and ThreeOps_match.

Split first to unblock D71389 <https://reviews.llvm.org/D71389>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D74095

Files:
  llvm/lib/IR/ConstantFold.cpp
  llvm/test/Analysis/ConstantFolding/bitcast.ll


Index: llvm/test/Analysis/ConstantFolding/bitcast.ll
===================================================================
--- llvm/test/Analysis/ConstantFolding/bitcast.ll
+++ llvm/test/Analysis/ConstantFolding/bitcast.ll
@@ -3,7 +3,7 @@
 
 define <vscale x 4 x float> @bitcast_scalable_constant() {
 ; CHECK-LABEL: @bitcast_scalable_constant(
-; CHECK-NEXT:    ret <vscale x 4 x float> bitcast (<vscale x 4 x i32> shufflevector (<vscale x 4 x i32> insertelement (<vscale x 4 x i32> undef, i32 1, i32 0), <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer) to <vscale x 4 x float>)
+; CHECK-NEXT:    ret <vscale x 4 x float> shufflevector (<vscale x 4 x float> insertelement (<vscale x 4 x float> undef, float 0x36A0000000000000, i32 0), <vscale x 4 x float> undef, <vscale x 4 x i32> zeroinitializer)
 ;
   %i1 = insertelement <vscale x 4 x i32> undef, i32 1, i32 0
   %i2 = shufflevector <vscale x 4 x i32> %i1, <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer
Index: llvm/lib/IR/ConstantFold.cpp
===================================================================
--- llvm/lib/IR/ConstantFold.cpp
+++ llvm/lib/IR/ConstantFold.cpp
@@ -568,6 +568,29 @@
       if (isAllNull)
         // This is casting one pointer type to another, always BitCast
         return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
+    } else if (CE->getType()->isVectorTy() && DestTy->isVectorTy() &&
+               CE->getType()->getVectorElementCount() ==
+                   DestTy->getVectorElementCount() &&
+               CE->getOpcode() == Instruction::ShuffleVector &&
+               opc == Instruction::BitCast) {
+      // For splat vector, fold bitcast to splat value.
+      // BitCast(ShuffleVector(InsertElement(C1, SplatV, Zero), C2, Zero)) to NewType
+      // into
+      // ShuffleVector(InsertElement(C1, BitCast(SplatV) to NewType, Zero), C2, Zero)
+      Constant *SplatV, *C1, *C2, *ZeroIdx, *ZeroMask;
+      if (match(CE, m_ShuffleVector(
+                        m_InsertElement(
+                            m_Constant(C1), m_Constant(SplatV),
+                            m_CombineAnd(m_Zero(), m_Constant(ZeroIdx))),
+                        m_Constant(C2),
+                        m_CombineAnd(m_Zero(), m_Constant(ZeroMask))))) {
+        auto *CastedSplatV =
+            ConstantExpr::getCast(opc, SplatV, DestTy->getScalarType());
+        return ConstantExpr::getShuffleVector(
+            ConstantExpr::getInsertElement(
+                ConstantExpr::getCast(opc, C1, DestTy), CastedSplatV, ZeroIdx),
+            ConstantExpr::getCast(opc, C2, DestTy), ZeroMask);
+      }
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74095.242767.patch
Type: text/x-patch
Size: 2641 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200205/3f388733/attachment.bin>


More information about the llvm-commits mailing list