From f4f2706572b1851d902fd569d8fb75bc42c8c82b Mon Sep 17 00:00:00 2001 From: Huihui Zhang Date: Thu, 12 Mar 2020 15:59:14 -0700 Subject: [PATCH] [ConstantFold][SVE] Fix constant folding for scalable vector compare instruction. Summary: Do not iterate on scalable vector. Also do not return constant scalable vector from ConstantInt::get(). Fix result type by using getElementCount() instead of getNumElements(). Reviewers: sdesmalen, efriedma, apazos, huntergr, willlovett Reviewed By: efriedma Subscribers: tschuett, hiraditya, rkruppe, psnobl, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D73753 --- llvm/lib/IR/ConstantFold.cpp | 7 +++- llvm/lib/IR/Constants.cpp | 4 +-- llvm/test/Analysis/ConstantFolding/vscale.ll | 48 ++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index 0da027f..dc78c55 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -1840,7 +1840,7 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, 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()); @@ -1971,6 +1971,11 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, 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 index eb0e589..c609add 100644 --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -2175,7 +2175,7 @@ Constant *ConstantExpr::getICmp(unsigned short pred, Constant *LHS, 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); @@ -2200,7 +2200,7 @@ Constant *ConstantExpr::getFCmp(unsigned short pred, Constant *LHS, 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 index 1616c70..f00e74b 100644 --- a/llvm/test/Analysis/ConstantFolding/vscale.ll +++ b/llvm/test/Analysis/ConstantFolding/vscale.ll @@ -235,3 +235,51 @@ define @call() { %r = call @llvm.sadd.sat.nxv16i8( undef, 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 +} -- 2.7.4