[llvm-branch-commits] [llvm] 2182561 - [ConstantFold] Remove unnecessary BitCastConstantVector() (NFCI)

Nikita Popov via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Nov 3 03:23:56 PDT 2023


Author: Nikita Popov
Date: 2023-11-03T11:04:58+01:00
New Revision: 2182561b7ba675ca87356c02474eecb6ecfaa23f

URL: https://github.com/llvm/llvm-project/commit/2182561b7ba675ca87356c02474eecb6ecfaa23f
DIFF: https://github.com/llvm/llvm-project/commit/2182561b7ba675ca87356c02474eecb6ecfaa23f.diff

LOG: [ConstantFold] Remove unnecessary BitCastConstantVector() (NFCI)

ConstantFoldCastInstruction() already has generic code to perform
lane-wise casts for vectors. There is no need to repeate it
specifically for bitcasts.

Added: 
    

Modified: 
    llvm/lib/IR/ConstantFold.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 81691a5f69aa0a6..ada921bc2dea4db 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -37,45 +37,6 @@ using namespace llvm::PatternMatch;
 //                ConstantFold*Instruction Implementations
 //===----------------------------------------------------------------------===//
 
-/// Convert the specified vector Constant node to the specified vector type.
-/// At this point, we know that the elements of the input vector constant are
-/// all simple integer or FP values.
-static Constant *BitCastConstantVector(Constant *CV, VectorType *DstTy) {
-
-  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 (isa<ScalableVectorType>(DstTy))
-    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
-  unsigned NumElts = cast<FixedVectorType>(DstTy)->getNumElements();
-  if (NumElts != cast<FixedVectorType>(CV->getType())->getNumElements())
-    return nullptr;
-
-  Type *DstEltTy = DstTy->getElementType();
-  // Fast path for splatted constants.
-  if (Constant *Splat = CV->getSplatValue()) {
-    return ConstantVector::getSplat(DstTy->getElementCount(),
-                                    ConstantExpr::getBitCast(Splat, DstEltTy));
-  }
-
-  SmallVector<Constant*, 16> Result;
-  Type *Ty = IntegerType::get(CV->getContext(), 32);
-  for (unsigned i = 0; i != NumElts; ++i) {
-    Constant *C =
-      ConstantExpr::getExtractElement(CV, ConstantInt::get(Ty, i));
-    C = ConstantExpr::getBitCast(C, DstEltTy);
-    Result.push_back(C);
-  }
-
-  return ConstantVector::get(Result);
-}
-
 /// This function determines which opcode to use to fold two constant cast
 /// expressions together. It uses CastInst::isEliminableCastPair to determine
 /// the opcode. Consequently its just a wrapper around that function.
@@ -114,24 +75,12 @@ static Constant *FoldBitCast(Constant *V, Type *DestTy) {
   // Handle casts from one vector constant to another.  We know that the src
   // and dest type have the same size (otherwise its an illegal cast).
   if (VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) {
-    if (VectorType *SrcTy = dyn_cast<VectorType>(V->getType())) {
-      assert(DestPTy->getPrimitiveSizeInBits() ==
-                 SrcTy->getPrimitiveSizeInBits() &&
-             "Not cast between same sized vectors!");
-      SrcTy = nullptr;
-      // First, check for null.  Undef is already handled.
-      if (isa<ConstantAggregateZero>(V))
-        return Constant::getNullValue(DestTy);
-
-      // Handle ConstantVector and ConstantAggregateVector.
-      return BitCastConstantVector(V, DestPTy);
-    }
-
     // Canonicalize scalar-to-vector bitcasts into vector-to-vector bitcasts
     // This allows for other simplifications (although some of them
     // can only be handled by Analysis/ConstantFolding.cpp).
     if (isa<ConstantInt>(V) || isa<ConstantFP>(V))
       return ConstantExpr::getBitCast(ConstantVector::get(V), DestPTy);
+    return nullptr;
   }
 
   // Finally, implement bitcast folding now.   The code below doesn't handle


        


More information about the llvm-branch-commits mailing list