diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -5734,16 +5734,19 @@ return nullptr; } case Intrinsic::experimental_vector_insert: { + Value *Vec = Call->getArgOperand(0); Value *SubVec = Call->getArgOperand(1); Value *Idx = Call->getArgOperand(2); Type *ReturnType = F->getReturnType(); - // (insert_vector _, (extract_vector X, 0), 0) -> X + // (insert_vector Y, (extract_vector X, 0), 0) -> X + // where: Y is X, or Y is undef unsigned IdxN = cast(Idx)->getZExtValue(); Value *X = nullptr; if (match(SubVec, m_Intrinsic( m_Value(X), m_Zero())) && - IdxN == 0 && X->getType() == ReturnType) + (Q.isUndefValue(Vec) || Vec == X) && IdxN == 0 && + X->getType() == ReturnType) return X; return nullptr; diff --git a/llvm/test/Transforms/InstSimplify/insert-vector.ll b/llvm/test/Transforms/InstSimplify/insert-vector.ll --- a/llvm/test/Transforms/InstSimplify/insert-vector.ll +++ b/llvm/test/Transforms/InstSimplify/insert-vector.ll @@ -10,8 +10,8 @@ ret %inserted } -define @non_redundant_extract_insert_chain( %pg, %x) { -; CHECK-LABEL: @non_redundant_extract_insert_chain( +define @non_redundant_extract_insert_chain_0( %pg, %x) { +; CHECK-LABEL: @non_redundant_extract_insert_chain_0( ; CHECK-NEXT: [[EXTRACTED:%.*]] = call <32 x i8> @llvm.experimental.vector.extract.v32i8.nxv32i8( [[X:%.*]], i64 0) ; CHECK-NEXT: [[INSERTED:%.*]] = call @llvm.experimental.vector.insert.nxv16i8.v32i8( undef, <32 x i8> [[EXTRACTED]], i64 0) ; CHECK-NEXT: ret [[INSERTED]] @@ -21,6 +21,17 @@ ret %inserted } +define @non_redundant_extract_insert_chain_1( %pg, %x, %y) { +; CHECK-LABEL: @non_redundant_extract_insert_chain_1( +; CHECK-NEXT: [[EXTRACTED:%.*]] = call <32 x i8> @llvm.experimental.vector.extract.v32i8.nxv16i8( [[X:%.*]], i64 0) +; CHECK-NEXT: [[INSERTED:%.*]] = call @llvm.experimental.vector.insert.nxv16i8.v32i8( [[Y:%.*]], <32 x i8> [[EXTRACTED]], i64 0) +; CHECK-NEXT: ret [[INSERTED]] +; + %extracted = call <32 x i8> @llvm.experimental.vector.extract.v32i8.nxv16i8( %x, i64 0) + %inserted = call @llvm.experimental.vector.insert.nxv16i8.v32i8( %y, <32 x i8> %extracted, i64 0) + ret %inserted +} + declare <32 x i8> @llvm.experimental.vector.extract.v32i8.nxv16i8(, i64) declare <32 x i8> @llvm.experimental.vector.extract.v32i8.nxv32i8(, i64) declare @llvm.experimental.vector.insert.nxv16i8.v32i8(, <32 x i8>, i64)