[InstSimplify] fold insertelement-of-extractelement
authorSanjay Patel <spatel@rotateright.com>
Fri, 24 May 2019 00:13:58 +0000 (00:13 +0000)
committerSanjay Patel <spatel@rotateright.com>
Fri, 24 May 2019 00:13:58 +0000 (00:13 +0000)
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
llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp
llvm/test/Transforms/InstSimplify/insertelement.ll

index 6e421dc..1f8245d 100644 (file)
@@ -4016,6 +4016,12 @@ Value *llvm::SimplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx,
   if (isa<UndefValue>(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;
 }
 
index c3fd612..3085693 100644 (file)
@@ -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
index 6e78ea7..e487eeb 100644 (file)
@@ -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