diff options
author | Nikita Popov <npopov@redhat.com> | 2023-11-03 11:04:12 +0100 |
---|---|---|
committer | Nikita Popov <npopov@redhat.com> | 2023-11-03 11:39:58 +0100 |
commit | d49a893cdbea0dd6f8fde7dc9f321b2e0d169bba (patch) | |
tree | fa6e0e902365b6d1bdea0e8bcdc2a60ea4aa5994 /llvm/lib/IR/ConstantFold.cpp | |
parent | 4be8a7bda55c5b50832b773b204f75cd26c5979d (diff) | |
download | llvm-d49a893cdbea0dd6f8fde7dc9f321b2e0d169bba.zip llvm-d49a893cdbea0dd6f8fde7dc9f321b2e0d169bba.tar.gz llvm-d49a893cdbea0dd6f8fde7dc9f321b2e0d169bba.tar.bz2 |
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.
Diffstat (limited to 'llvm/lib/IR/ConstantFold.cpp')
-rw-r--r-- | llvm/lib/IR/ConstantFold.cpp | 54 |
1 files changed, 3 insertions, 51 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index a263cdc..d51e9c6 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. |