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 @@ -736,32 +736,37 @@ if (Cond->isNullValue()) return V2; if (Cond->isAllOnesValue()) return V1; - // If the condition is a vector constant, fold the result elementwise. - if (ConstantVector *CondV = dyn_cast(Cond)) { - SmallVector Result; - Type *Ty = IntegerType::get(CondV->getContext(), 32); - for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e;++i){ - Constant *V; - Constant *V1Element = ConstantExpr::getExtractElement(V1, - ConstantInt::get(Ty, i)); - Constant *V2Element = ConstantExpr::getExtractElement(V2, - ConstantInt::get(Ty, i)); - auto *Cond = cast(CondV->getOperand(i)); - if (V1Element == V2Element) { - V = V1Element; - } else if (isa(Cond)) { - V = isa(V1Element) ? V1Element : V2Element; - } else { - if (!isa(Cond)) break; - V = Cond->isNullValue() ? V2Element : V1Element; + // Do not iterate on scalable vector. The number of elements is unknown at + // compile-time. + if (!V1->getType()->getVectorIsScalable()) + // If the condition is a vector constant, fold the result elementwise. + if (ConstantVector *CondV = dyn_cast(Cond)) { + SmallVector Result; + Type *Ty = IntegerType::get(CondV->getContext(), 32); + for (unsigned i = 0, e = V1->getType()->getVectorNumElements(); i != e; + ++i) { + Constant *V; + Constant *V1Element = + ConstantExpr::getExtractElement(V1, ConstantInt::get(Ty, i)); + Constant *V2Element = + ConstantExpr::getExtractElement(V2, ConstantInt::get(Ty, i)); + auto *Cond = cast(CondV->getOperand(i)); + if (V1Element == V2Element) { + V = V1Element; + } else if (isa(Cond)) { + V = isa(V1Element) ? V1Element : V2Element; + } else { + if (!isa(Cond)) + break; + V = Cond->isNullValue() ? V2Element : V1Element; + } + Result.push_back(V); } - Result.push_back(V); - } - // If we were able to build the vector, return it. - if (Result.size() == V1->getType()->getVectorNumElements()) - return ConstantVector::get(Result); - } + // If we were able to build the vector, return it. + if (Result.size() == V1->getType()->getVectorNumElements()) + return ConstantVector::get(Result); + } if (isa(Cond)) { if (isa(V1)) return V1; 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 @@ -153,3 +153,15 @@ %r = xor undef, undef ret %r } + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Other Operations +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +define @select() { +; CHECK-LABEL: @select( +; CHECK-NEXT: ret undef +; + %r = select undef, zeroinitializer, undef + ret %r +}