Skip to content

Commit 5486e00

Browse files
committed
[InstSimplify] remove poison-unsafe insertelement of undef value
PR45481: https://bugs.llvm.org/show_bug.cgi?id=45481 SDAG has an identical transform to this, so there's little chance of any real-world impact. OTOH, that means we are effectively sweeping the bug out of sight because poison exists in codegen too.
1 parent 4f1e9a1 commit 5486e00

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4302,11 +4302,6 @@ Value *llvm::SimplifyInsertElementInst(Value *Vec, Value *Val, Value *Idx,
43024302
if (isa<UndefValue>(Idx))
43034303
return UndefValue::get(Vec->getType());
43044304

4305-
// Inserting an undef scalar? Assume it is the same value as the existing
4306-
// vector element.
4307-
if (isa<UndefValue>(Val))
4308-
return Vec;
4309-
43104305
// If we are extracting a value from a vector, then inserting it into the same
43114306
// place, that's the input vector:
43124307
// insertelt Vec, (extractelt Vec, Idx), Idx --> Vec

llvm/test/Transforms/InstCombine/extractelement.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ define <4 x double> @invalid_extractelement(<2 x double> %a, <4 x double> %b, do
319319
; ANY-NEXT: [[T4:%.*]] = shufflevector <4 x double> [[B:%.*]], <4 x double> [[TMP1]], <4 x i32> <i32 undef, i32 1, i32 4, i32 3>
320320
; ANY-NEXT: [[E:%.*]] = extractelement <4 x double> [[B]], i32 1
321321
; ANY-NEXT: store double [[E]], double* [[P:%.*]], align 8
322-
; ANY-NEXT: ret <4 x double> [[T4]]
322+
; ANY-NEXT: [[R:%.*]] = insertelement <4 x double> [[T4]], double undef, i64 0
323+
; ANY-NEXT: ret <4 x double> [[R]]
323324
;
324325
%t3 = extractelement <2 x double> %a, i32 0
325326
%t4 = insertelement <4 x double> %b, double %t3, i32 2

llvm/test/Transforms/InstSimplify/insertelement.ll

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,26 @@ define <4 x i32> @test5(<4 x i32> %A) {
4242
ret <4 x i32> %I
4343
}
4444

45+
; The undef may be replacing a poison value, so it is not safe to just return 'A'.
46+
4547
define <4 x i32> @PR1286(<4 x i32> %A) {
4648
; CHECK-LABEL: @PR1286(
47-
; CHECK-NEXT: ret <4 x i32> [[A:%.*]]
49+
; CHECK-NEXT: [[B:%.*]] = insertelement <4 x i32> [[A:%.*]], i32 undef, i32 1
50+
; CHECK-NEXT: ret <4 x i32> [[B]]
4851
;
4952
%B = insertelement <4 x i32> %A, i32 undef, i32 1
5053
ret <4 x i32> %B
5154
}
5255

56+
define <2 x i32> @undef_into_constant_vector_with_variable_index(<2 x i32> %A, i32 %Index) {
57+
; CHECK-LABEL: @undef_into_constant_vector_with_variable_index(
58+
; CHECK-NEXT: [[B:%.*]] = insertelement <2 x i32> <i32 42, i32 -42>, i32 undef, i32 [[INDEX:%.*]]
59+
; CHECK-NEXT: ret <2 x i32> [[B]]
60+
;
61+
%B = insertelement <2 x i32> <i32 42, i32 -42>, i32 undef, i32 %Index
62+
ret <2 x i32> %B
63+
}
64+
5365
define <8 x i8> @extract_insert_same_vec_and_index(<8 x i8> %in) {
5466
; CHECK-LABEL: @extract_insert_same_vec_and_index(
5567
; CHECK-NEXT: ret <8 x i8> [[IN:%.*]]

0 commit comments

Comments
 (0)