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

via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 7 07:10:27 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Benjamin Kramer (d0k)

<details>
<summary>Changes</summary>

The index doesn't matter here.

---
Full diff: https://github.com/llvm/llvm-project/pull/102315.diff


2 Files Affected:

- (modified) llvm/lib/Analysis/InstructionSimplify.cpp (+4) 
- (modified) llvm/test/Transforms/InstSimplify/insertelement.ll (+8) 


``````````diff
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
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/102315


More information about the llvm-commits mailing list