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 @@ -863,7 +863,6 @@ if (auto *CAZ = dyn_cast(Val)) return CAZ->getElementValue(CIdx->getZExtValue()); } - return nullptr; } return Val->getAggregateElement(CIdx); diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -409,9 +409,19 @@ if (const UndefValue *UV = dyn_cast(this)) return Elt < UV->getNumElements() ? UV->getElementValue(Elt) : nullptr; - if (const ConstantDataSequential *CDS =dyn_cast(this)) + if (const ConstantDataSequential *CDS = + dyn_cast(this)) return Elt < CDS->getNumElements() ? CDS->getElementAsConstant(Elt) : nullptr; + + if (const auto *IE = dyn_cast(this)) { + if (const auto *IEIdx = dyn_cast(IE->getOperand(2))) { + if (IEIdx->equalsInt(Elt)) { + if (auto *IEValue = dyn_cast(IE->getOperand(1))) + return IEValue; + } + } + } return nullptr; } 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) {