aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/IR/ConstantFold.cpp
diff options
context:
space:
mode:
authorLuke Lau <luke@igalia.com>2025-04-03 16:24:56 +0100
committerGitHub <noreply@github.com>2025-04-03 16:24:56 +0100
commit79435de8a51a3df4b74f858a604b7ff56b342ae7 (patch)
treefad9c782512eb7a170bcf4d58085385fc469ad56 /llvm/lib/IR/ConstantFold.cpp
parent2080334574a88a7ac4102007b56809cd0a19b905 (diff)
downloadllvm-79435de8a51a3df4b74f858a604b7ff56b342ae7.zip
llvm-79435de8a51a3df4b74f858a604b7ff56b342ae7.tar.gz
llvm-79435de8a51a3df4b74f858a604b7ff56b342ae7.tar.bz2
[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
Diffstat (limited to 'llvm/lib/IR/ConstantFold.cpp')
-rw-r--r--llvm/lib/IR/ConstantFold.cpp9
1 files changed, 5 insertions, 4 deletions
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<ConstantVector>(V) || isa<ConstantDataVector>(V)) &&
- DestTy->isVectorTy() &&
- cast<FixedVectorType>(DestTy)->getNumElements() ==
- cast<FixedVectorType>(V->getType())->getNumElements()) {
+ if (DestTy->isVectorTy() && V->getType()->isVectorTy() &&
+ cast<VectorType>(DestTy)->getElementCount() ==
+ cast<VectorType>(V->getType())->getElementCount()) {
VectorType *DestVecTy = cast<VectorType>(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<VectorType>(DestTy)->getElementCount(), Res);
}
+ if (isa<ScalableVectorType>(DestTy))
+ return nullptr;
SmallVector<Constant *, 16> res;
Type *Ty = IntegerType::get(V->getContext(), 32);
for (unsigned i = 0,