[llvm] c31ac81 - [InstSimplify] Fold (insertelement Splat(C), C, X) -> Splat(C) (#102315)

via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 7 07:39:13 PDT 2024


Author: Benjamin Kramer
Date: 2024-08-07T16:39:08+02:00
New Revision: c31ac810910ac87531de636ea508abec6e29e8ff

URL: https://github.com/llvm/llvm-project/commit/c31ac810910ac87531de636ea508abec6e29e8ff
DIFF: https://github.com/llvm/llvm-project/commit/c31ac810910ac87531de636ea508abec6e29e8ff.diff

LOG: [InstSimplify] Fold (insertelement Splat(C), C, X) -> Splat(C) (#102315)

The index doesn't matter here.

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstSimplify/insertelement.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 12a3193e63755..c4c174977c475 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5181,6 +5181,10 @@ Value *llvm::simplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx,
       (Q.isUndefValue(Val) && isGuaranteedNotToBePoison(Vec)))
     return Vec;
 
+  // Inserting the splatted value into a constant splat does nothing.
+  if (VecC && ValC && VecC->getSplatValue() == ValC)
+    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

diff  --git a/llvm/test/Transforms/InstSimplify/insertelement.ll b/llvm/test/Transforms/InstSimplify/insertelement.ll
index 55fab36ddca15..3fe8b8331a40c 100644
--- a/llvm/test/Transforms/InstSimplify/insertelement.ll
+++ b/llvm/test/Transforms/InstSimplify/insertelement.ll
@@ -119,3 +119,11 @@ unreachable_infloop:
   %bogus = insertelement <2 x i64> %bogus, i64 undef, i32 1
   br label %unreachable_infloop
 }
+
+define <4 x i32> @insert_into_splat(i32 %index) {
+; CHECK-LABEL: @insert_into_splat(
+; CHECK-NEXT:    ret <4 x i32> <i32 3, i32 3, i32 3, i32 3>
+;
+  %I = insertelement <4 x i32> <i32 3, i32 3, i32 3, i32 3>, i32 3, i32 %index
+  ret <4 x i32> %I
+}


        


More information about the llvm-commits mailing list