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 @@ -833,6 +833,12 @@ ConstantInt *CIdx = dyn_cast(Idx); if (!CIdx) return nullptr; + // Should not iterate over scalable vector, as num element is known at + // runtime. + VectorType *ValTy = cast(Val->getType()); + if (ValTy->isScalable()) + return nullptr; + unsigned NumElts = Val->getType()->getVectorNumElements(); if (CIdx->uge(NumElts)) return UndefValue::get(Val->getType()); diff --git a/llvm/test/Analysis/ConstantFolding/insertelement.ll b/llvm/test/Analysis/ConstantFolding/insertelement.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Analysis/ConstantFolding/insertelement.ll @@ -0,0 +1,19 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -instcombine -S | FileCheck %s + + +define <4 x i32> @insertelement_fixedlength_constant() { +; CHECK-LABEL: @insertelement_fixedlength_constant( +; CHECK-NEXT: ret <4 x i32> +; + %i = insertelement <4 x i32> undef, i32 1, i32 0 + ret <4 x i32> %i +} + +define @insertelement_scalable_constant() { +; CHECK-LABEL: @insertelement_scalable_constant( +; CHECK-NEXT: ret insertelement ( undef, i32 1, i32 0) +; + %i = insertelement undef, i32 1, i32 0 + ret %i +}