diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -1836,7 +1836,7 @@ Type *ResultTy; if (VectorType *VT = dyn_cast(C1->getType())) ResultTy = VectorType::get(Type::getInt1Ty(C1->getContext()), - VT->getNumElements()); + VT->getElementCount()); else ResultTy = Type::getInt1Ty(C1->getContext()); @@ -1967,6 +1967,11 @@ R==APFloat::cmpEqual); } } else if (C1->getType()->isVectorTy()) { + // Do not iterate on scalable vector. The number of elements is unknown at + // compile-time. + if (C1->getType()->getVectorIsScalable()) + return nullptr; + // If we can constant fold the comparison of each element, constant fold // the whole vector comparison. SmallVector ResElts; diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -2176,7 +2176,7 @@ Type *ResultTy = Type::getInt1Ty(LHS->getContext()); if (VectorType *VT = dyn_cast(LHS->getType())) - ResultTy = VectorType::get(ResultTy, VT->getNumElements()); + ResultTy = VectorType::get(ResultTy, VT->getElementCount()); LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl; return pImpl->ExprConstants.getOrCreate(ResultTy, Key); @@ -2201,7 +2201,7 @@ Type *ResultTy = Type::getInt1Ty(LHS->getContext()); if (VectorType *VT = dyn_cast(LHS->getType())) - ResultTy = VectorType::get(ResultTy, VT->getNumElements()); + ResultTy = VectorType::get(ResultTy, VT->getElementCount()); LLVMContextImpl *pImpl = LHS->getType()->getContext().pImpl; return pImpl->ExprConstants.getOrCreate(ResultTy, Key); diff --git a/llvm/test/Analysis/ConstantFolding/vscale.ll b/llvm/test/Analysis/ConstantFolding/vscale.ll --- a/llvm/test/Analysis/ConstantFolding/vscale.ll +++ b/llvm/test/Analysis/ConstantFolding/vscale.ll @@ -210,3 +210,51 @@ %r = select undef, zeroinitializer, undef ret %r } + +define @icmp_undef() { +; CHECK-LABEL: @icmp_undef( +; CHECK-NEXT: ret undef +; + %r = icmp eq undef, undef + ret %r +} + +define @icmp_zero() { +; CHECK-LABEL: @icmp_zero( +; CHECK-NEXT: ret icmp eq ( zeroinitializer, zeroinitializer) +; + %r = icmp eq zeroinitializer, zeroinitializer + ret %r +} + +define @fcmp_true() { +; CHECK-LABEL: @fcmp_true( +; CHECK-NEXT: ret shufflevector ( insertelement ( undef, i1 true, i32 0), undef, zeroinitializer) +; + %r = fcmp true undef, undef + ret %r +} + +define @fcmp_false() { +; CHECK-LABEL: @fcmp_false( +; CHECK-NEXT: ret zeroinitializer +; + %r = fcmp false undef, undef + ret %r +} + +define @fcmp_undef() { +; CHECK-LABEL: @fcmp_undef( +; CHECK-NEXT: ret undef +; + %r = icmp ne undef, undef + ret %r +} + +define @fcmp_not_equality() { +; CHECK-LABEL: @fcmp_not_equality( +; CHECK-NEXT: ret shufflevector ( insertelement ( undef, i1 true, i32 0), undef, zeroinitializer) +; + %r = icmp ule undef, zeroinitializer + ret %r +}