Skip to content

Commit

Permalink
[InstSimplify] Fold (insertelement Splat(C), C, X) -> Splat(C) (llvm#…
Browse files Browse the repository at this point in the history
…102315)

The index doesn't matter here.
  • Loading branch information
d0k committed Aug 7, 2024
1 parent 06a808c commit c31ac81
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions llvm/test/Transforms/InstSimplify/insertelement.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c31ac81

Please sign in to comment.