From 8869a98e82552ef698112df840575693780802a4 Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Fri, 24 May 2019 00:13:58 +0000 Subject: [PATCH] [InstSimplify] fold insertelement-of-extractelement This was partly handled in InstCombine (only the constant index case), so delete that and zap it more generally in InstSimplify. llvm-svn: 361576 --- llvm/lib/Analysis/InstructionSimplify.cpp | 6 ++++++ llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp | 5 ----- llvm/test/Transforms/InstSimplify/insertelement.ll | 8 ++------ 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 6e421dc..1f8245d 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -4016,6 +4016,12 @@ Value *llvm::SimplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx, if (isa(Val)) return Vec; + // If we are extracting a value from a vector, then inserting it into the same + // place, that's the input vector: + // insertelt Vec, (extractelt Vec, Idx), Idx --> Vec + if (match(Val, m_ExtractElement(m_Specific(Vec), m_Specific(Idx)))) + return Vec; + return nullptr; } diff --git a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp index c3fd612..3085693 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp @@ -884,11 +884,6 @@ Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { if (match(IdxOp, m_ConstantInt(InsertedIdx)) && match(ScalarOp, m_ExtractElement(m_Value(ExtVecOp), m_ConstantInt(ExtractedIdx)))) { - // If we are extracting a value from a vector, then inserting it right - // back into the same place, just use the input vector. - if (ExtVecOp == VecOp && ExtractedIdx == InsertedIdx) - return replaceInstUsesWith(IE, VecOp); - // TODO: Looking at the user(s) to determine if this insert is a // fold-to-shuffle opportunity does not match the usual instcombine // constraints. We should decide if the transform is worthy based only diff --git a/llvm/test/Transforms/InstSimplify/insertelement.ll b/llvm/test/Transforms/InstSimplify/insertelement.ll index 6e78ea7..e487eeb 100644 --- a/llvm/test/Transforms/InstSimplify/insertelement.ll +++ b/llvm/test/Transforms/InstSimplify/insertelement.ll @@ -52,9 +52,7 @@ define <4 x i32> @PR1286(<4 x i32> %A) { define <8 x i8> @extract_insert_same_vec_and_index(<8 x i8> %in) { ; CHECK-LABEL: @extract_insert_same_vec_and_index( -; CHECK-NEXT: [[VAL:%.*]] = extractelement <8 x i8> [[IN:%.*]], i32 5 -; CHECK-NEXT: [[VEC:%.*]] = insertelement <8 x i8> [[IN]], i8 [[VAL]], i32 5 -; CHECK-NEXT: ret <8 x i8> [[VEC]] +; CHECK-NEXT: ret <8 x i8> [[IN:%.*]] ; %val = extractelement <8 x i8> %in, i32 5 %vec = insertelement <8 x i8> %in, i8 %val, i32 5 @@ -63,9 +61,7 @@ define <8 x i8> @extract_insert_same_vec_and_index(<8 x i8> %in) { define <8 x i8> @extract_insert_same_vec_and_index2(<8 x i8> %in, i32 %index) { ; CHECK-LABEL: @extract_insert_same_vec_and_index2( -; CHECK-NEXT: [[VAL:%.*]] = extractelement <8 x i8> [[IN:%.*]], i32 [[INDEX:%.*]] -; CHECK-NEXT: [[VEC:%.*]] = insertelement <8 x i8> [[IN]], i8 [[VAL]], i32 [[INDEX]] -; CHECK-NEXT: ret <8 x i8> [[VEC]] +; CHECK-NEXT: ret <8 x i8> [[IN:%.*]] ; %val = extractelement <8 x i8> %in, i32 %index %vec = insertelement <8 x i8> %in, i8 %val, i32 %index -- 2.7.4