Author: Nikita Popov Date: 2023-11-03T11:39:58+01:00 New Revision: d49a893cdbea0dd6f8fde7dc9f321b2e0d169bba
URL: https://github.com/llvm/llvm-project/commit/d49a893cdbea0dd6f8fde7dc9f321b2e0d169bba DIFF: https://github.com/llvm/llvm-project/commit/d49a893cdbea0dd6f8fde7dc9f321b2e0d169bba.diff LOG: Reapply [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. However, we do need to keep the special case for vectors of -1, which is not handled elsewhere. Added: Modified: llvm/lib/IR/ConstantFold.cpp Removed: ################################################################################ diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index a263cdcf4008a2a..d51e9c67592eb95 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,15 @@ 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); - } + if (V->isAllOnesValue()) + return Constant::getAllOnesValue(DestTy); // 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; } // Handle integral constant input. _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits