From 79435de8a51a3df4b74f858a604b7ff56b342ae7 Mon Sep 17 00:00:00 2001 From: Luke Lau Date: Thu, 3 Apr 2025 16:24:56 +0100 Subject: [ConstantFold] Support scalable constant splats in ConstantFoldCastInstruction (#133207) Previously only fixed vector splats were handled. This adds supports for scalable vectors too by allowing ConstantExpr splats. We need to add the extra V->getType()->isVectorTy() check because a ConstantExpr might be a scalar to vector bitcast. By allowing ConstantExprs this also allow fixed vector ConstantExprs to be folded, which causes the diffs in llvm/test/Analysis/ValueTracking/known-bits-from-operator-constexpr.ll and llvm/test/Transforms/InstSimplify/ConstProp/cast-vector.ll. I can remove them from this PR if reviewers would prefer. Fixes #132922 --- llvm/lib/IR/ConstantFold.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'llvm/lib/IR/ConstantFold.cpp') diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index b577f69..7e5fda2 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -160,10 +160,9 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, // If the cast operand is a constant vector, perform the cast by // operating on each element. In the cast of bitcasts, the element // count may be mismatched; don't attempt to handle that here. - if ((isa(V) || isa(V)) && - DestTy->isVectorTy() && - cast(DestTy)->getNumElements() == - cast(V->getType())->getNumElements()) { + if (DestTy->isVectorTy() && V->getType()->isVectorTy() && + cast(DestTy)->getElementCount() == + cast(V->getType())->getElementCount()) { VectorType *DestVecTy = cast(DestTy); Type *DstEltTy = DestVecTy->getElementType(); // Fast path for splatted constants. @@ -174,6 +173,8 @@ Constant *llvm::ConstantFoldCastInstruction(unsigned opc, Constant *V, return ConstantVector::getSplat( cast(DestTy)->getElementCount(), Res); } + if (isa(DestTy)) + return nullptr; SmallVector res; Type *Ty = IntegerType::get(V->getContext(), 32); for (unsigned i = 0, -- cgit v1.1