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<ConstantVector>(Cond)) {
-    SmallVector<Constant*, 16> 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<Constant>(CondV->getOperand(i));
-      if (V1Element == V2Element) {
-        V = V1Element;
-      } else if (isa<UndefValue>(Cond)) {
-        V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
-      } else {
-        if (!isa<ConstantInt>(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<ConstantVector>(Cond)) {
+      SmallVector<Constant *, 16> 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<Constant>(CondV->getOperand(i));
+        if (V1Element == V2Element) {
+          V = V1Element;
+        } else if (isa<UndefValue>(Cond)) {
+          V = isa<UndefValue>(V1Element) ? V1Element : V2Element;
+        } else {
+          if (!isa<ConstantInt>(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<UndefValue>(Cond)) {
     if (isa<UndefValue>(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 <vscale x 4 x i32> undef, undef
   ret <vscale x 4 x i32> %r
 }
+
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; Other Operations
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+define <vscale x 4 x i32> @select() {
+; CHECK-LABEL: @select(
+; CHECK-NEXT:    ret <vscale x 4 x i32> undef
+;
+  %r = select <vscale x 4 x i1> undef, <vscale x 4 x i32> zeroinitializer, <vscale x 4 x i32> undef
+  ret <vscale x 4 x i32> %r
+}