]> granicus.if.org Git - llvm/commitdiff
[InstSimplify] insertelement V, undef, ? --> V
authorSanjay Patel <spatel@rotateright.com>
Thu, 23 May 2019 21:49:47 +0000 (21:49 +0000)
committerSanjay Patel <spatel@rotateright.com>
Thu, 23 May 2019 21:49:47 +0000 (21:49 +0000)
This was part of InstCombine, but it's better placed in
InstSimplify. InstCombine also had an unreachable but weaker
fold for insertelement with undef index, so that is deleted.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@361559 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/InstructionSimplify.cpp
lib/Transforms/InstCombine/InstCombineVectorOps.cpp
test/Transforms/InstCombine/vec_insertelt.ll [deleted file]
test/Transforms/InstSimplify/insertelement.ll

index b71841a1607dc15b3165c715bdffe347892a5e18..6e421dcaa737f84acb678502ac1695f9f239f40c 100644 (file)
@@ -4011,6 +4011,11 @@ Value *llvm::SimplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx,
   if (isa<UndefValue>(Idx))
     return UndefValue::get(Vec->getType());
 
+  // Inserting an undef scalar? Assume it is the same value as the existing
+  // vector element.
+  if (isa<UndefValue>(Val))
+    return Vec;
+
   return nullptr;
 }
 
index c2ea0733a48e439b500ff7a586237d31821ffab1..44130d3246b67043feec406c7b591fa24c50c6a5 100644 (file)
@@ -863,10 +863,6 @@ Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) {
           VecOp, ScalarOp, IdxOp, SQ.getWithInstruction(&IE)))
     return replaceInstUsesWith(IE, V);
 
-  // Inserting an undef or into an undefined place, remove this.
-  if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
-    replaceInstUsesWith(IE, VecOp);
-
   // If the vector and scalar are both bitcast from the same element type, do
   // the insert in that source type followed by bitcast.
   Value *VecSrc, *ScalarSrc;
diff --git a/test/Transforms/InstCombine/vec_insertelt.ll b/test/Transforms/InstCombine/vec_insertelt.ll
deleted file mode 100644 (file)
index 3b94920..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-; RUN: opt < %s -instcombine -S | FileCheck %s
-; CHECK: ret <4 x i32> %A
-
-; PR1286
-define <4 x i32> @test1(<4 x i32> %A) {
-       %B = insertelement <4 x i32> %A, i32 undef, i32 1
-       ret <4 x i32> %B
-}
index 3524f2145acb098d632183ba7af6c98ed0a53472..c7db869d056d2aff01a7739a689d1bc8fe78902f 100644 (file)
@@ -1,31 +1,51 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt -S -instsimplify < %s | FileCheck %s
 
 define <4 x i32> @test1(<4 x i32> %A) {
+; CHECK-LABEL: @test1(
+; CHECK-NEXT:    ret <4 x i32> undef
+;
   %I = insertelement <4 x i32> %A, i32 5, i64 4294967296
-  ; CHECK: ret <4 x i32> undef
   ret <4 x i32> %I
 }
 
 define <4 x i32> @test2(<4 x i32> %A) {
+; CHECK-LABEL: @test2(
+; CHECK-NEXT:    ret <4 x i32> undef
+;
   %I = insertelement <4 x i32> %A, i32 5, i64 4
-  ; CHECK: ret <4 x i32> undef
   ret <4 x i32> %I
 }
 
 define <4 x i32> @test3(<4 x i32> %A) {
+; CHECK-LABEL: @test3(
+; CHECK-NEXT:    [[I:%.*]] = insertelement <4 x i32> [[A:%.*]], i32 5, i64 1
+; CHECK-NEXT:    ret <4 x i32> [[I]]
+;
   %I = insertelement <4 x i32> %A, i32 5, i64 1
-  ; CHECK: ret <4 x i32> %I
   ret <4 x i32> %I
 }
 
 define <4 x i32> @test4(<4 x i32> %A) {
+; CHECK-LABEL: @test4(
+; CHECK-NEXT:    ret <4 x i32> undef
+;
   %I = insertelement <4 x i32> %A, i32 5, i128 100
-  ; CHECK: ret <4 x i32> undef
   ret <4 x i32> %I
 }
 
 define <4 x i32> @test5(<4 x i32> %A) {
+; CHECK-LABEL: @test5(
+; CHECK-NEXT:    ret <4 x i32> undef
+;
   %I = insertelement <4 x i32> %A, i32 5, i64 undef
-  ; CHECK: ret <4 x i32> undef
   ret <4 x i32> %I
 }
+
+define <4 x i32> @PR1286(<4 x i32> %A) {
+; CHECK-LABEL: @PR1286(
+; CHECK-NEXT:    ret <4 x i32> [[A:%.*]]
+;
+  %B = insertelement <4 x i32> %A, i32 undef, i32 1
+  ret <4 x i32> %B
+}