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

Benjamin Kramer via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 7 07:09:50 PDT 2024


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

The index doesn't matter here.

>From 7a44b5d6c62062978d2d6203dc56a0ab8ffc4070 Mon Sep 17 00:00:00 2001
From: Benjamin Kramer <benny.kra at googlemail.com>
Date: Wed, 7 Aug 2024 16:04:10 +0200
Subject: [PATCH] [InstSimplify] Fold (insertelement Splat(C), C, X) ->
 Splat(C)

The index doesn't matter here.
---
 llvm/lib/Analysis/InstructionSimplify.cpp          | 4 ++++
 llvm/test/Transforms/InstSimplify/insertelement.ll | 8 ++++++++
 2 files changed, 12 insertions(+)

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