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 @@ -853,6 +853,13 @@ } return CE->getWithOperands(Ops, ValVTy->getElementType(), false, Ops[0]->getType()->getPointerElementType()); + } else if (CE->getOpcode() == Instruction::InsertElement) { + if (const auto *IEIdx = dyn_cast(CE->getOperand(2))) { + if (IEIdx->equalsInt(CIdx->getZExtValue())) { + if (auto *IEValue = dyn_cast(CE->getOperand(1))) + return IEValue; + } + } } } diff --git a/llvm/test/Transforms/InstSimplify/vscale.ll b/llvm/test/Transforms/InstSimplify/vscale.ll --- a/llvm/test/Transforms/InstSimplify/vscale.ll +++ b/llvm/test/Transforms/InstSimplify/vscale.ll @@ -95,6 +95,14 @@ ret i32 %r } +define i32 @insert_extract_element_same_vec_idx_3() { +; CHECK-LABEL: @insert_extract_element_same_vec_idx_3( +; CHECK-NEXT: ret i32 1 +; + %r = extractelement insertelement ( undef, i32 1, i64 4), i64 4 + ret i32 %r +} + ; more complicated expressions define @cmp_le_smax_always_true( %x) {