[PATCH] D71389: [ConstantFold][SVE] Fix constant folding for bitcast.

Huihui Zhang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 17 17:38:55 PST 2019


huihuiz updated this revision to Diff 234432.
huihuiz added a comment.

Add special case handling:

For splat vector, fold bitcast to splat value.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D71389

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


Index: llvm/test/Analysis/ConstantFolding/bitcast.ll
===================================================================
--- /dev/null
+++ llvm/test/Analysis/ConstantFolding/bitcast.ll
@@ -0,0 +1,12 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -constprop -S -verify | FileCheck %s
+
+define <vscale x 4 x float> @bitcast_scalable_constant() {
+; CHECK-LABEL: @bitcast_scalable_constant(
+; 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
+  %i3 = bitcast <vscale x 4 x i32> %i2 to <vscale x 4 x float>
+  ret <vscale x 4 x float> %i3
+}
Index: llvm/lib/IR/ConstantFold.cpp
===================================================================
--- llvm/lib/IR/ConstantFold.cpp
+++ llvm/lib/IR/ConstantFold.cpp
@@ -47,6 +47,11 @@
   if (CV->isAllOnesValue()) return Constant::getAllOnesValue(DstTy);
   if (CV->isNullValue()) return Constant::getNullValue(DstTy);
 
+  // Do not iterate on scalable vector. The num of elements is unknown at
+  // compile-time.
+  if (DstTy->isScalable())
+    return nullptr;
+
   // If this cast changes element count then we can't handle it here:
   // doing so requires endianness information.  This should be handled by
   // Analysis/ConstantFolding.cpp
@@ -563,6 +568,28 @@
       if (isAllNull)
         // This is casting one pointer type to another, always BitCast
         return ConstantExpr::getPointerCast(CE->getOperand(0), DestTy);
+    } else if (CE->getOpcode() == Instruction::ShuffleVector &&
+               opc == Instruction::BitCast) {
+      // For splat vector, fold bitcast to splat value.
+      // BitCast(ShuffleVector(InsertElement(Undef, C, Zero), V, Zero)) to NewType
+      // into
+      // ShuffleVector(InsertElement(Undef, BitCast(C) to NewType, Zero), V, Zero)
+      Constant *SplatV;
+      if (match(CE,
+                m_ShuffleVector(
+                    m_InsertElement(m_Undef(), m_Constant(SplatV), m_Zero()),
+                    m_Value(), m_Zero()))) {
+        auto NewCast =
+            ConstantExpr::getCast(opc, SplatV, DestTy->getScalarType());
+        auto InsertElem = CE->getOperand(0);
+        auto NewInsertElem = ConstantExpr::getInsertElement(
+            UndefValue::get(DestTy), NewCast,
+            cast<Constant>(InsertElem->getOperand(2)));
+        return ConstantExpr::getShuffleVector(
+            NewInsertElem,
+            ConstantExpr::getCast(opc, CE->getOperand(1), DestTy),
+            CE->getOperand(2));
+      }
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71389.234432.patch
Type: text/x-patch
Size: 2836 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191218/14b3a00d/attachment.bin>


More information about the llvm-commits mailing list