Skip to content

Commit 88b493a

Browse files
committed
[ValueTracking] improve undef/poison analysis for constant vectors
Differential Revision: https://reviews.llvm.org/D76702
1 parent b8dab9b commit 88b493a

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4610,9 +4610,22 @@ bool llvm::isGuaranteedNotToBeUndefOrPoison(const Value *V,
46104610
// TODO: Some instructions are guaranteed to return neither undef
46114611
// nor poison if their arguments are not poison/undef.
46124612

4613-
// TODO: Deal with other Constant subclasses.
4614-
if (isa<ConstantInt>(V) || isa<GlobalVariable>(V))
4615-
return true;
4613+
if (auto *C = dyn_cast<Constant>(V)) {
4614+
// TODO: We can analyze ConstExpr by opcode to determine if there is any
4615+
// possibility of poison.
4616+
if (isa<UndefValue>(C) || isa<ConstantExpr>(C))
4617+
return false;
4618+
4619+
// TODO: Add ConstantFP and pointers.
4620+
if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) )
4621+
return true;
4622+
4623+
if (C->getType()->isVectorTy())
4624+
return !C->containsUndefElement() && !C->containsConstantExpression();
4625+
4626+
// TODO: Recursively analyze aggregates or other constants.
4627+
return false;
4628+
}
46164629

46174630
if (auto PN = dyn_cast<PHINode>(V)) {
46184631
if (llvm::all_of(PN->incoming_values(), [](const Use &U) {

llvm/test/Transforms/InstSimplify/freeze.ll

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ define i32 @make_const() {
1919
ret i32 %x
2020
}
2121

22+
; TODO: This is not poison.
23+
2224
define float @make_const2() {
2325
; CHECK-LABEL: @make_const2(
2426
; CHECK-NEXT: [[X:%.*]] = freeze float 1.000000e+01
@@ -38,6 +40,8 @@ define i32* @make_const_glb() {
3840
ret i32* %k
3941
}
4042

43+
; TODO: This is not poison.
44+
4145
define i32()* @make_const_fn() {
4246
; CHECK-LABEL: @make_const_fn(
4347
; CHECK-NEXT: [[K:%.*]] = freeze i32 ()* @make_const
@@ -47,6 +51,8 @@ define i32()* @make_const_fn() {
4751
ret i32()* %k
4852
}
4953

54+
; TODO: This is not poison.
55+
5056
define i32* @make_const_null() {
5157
; CHECK-LABEL: @make_const_null(
5258
; CHECK-NEXT: [[K:%.*]] = freeze i32* null
@@ -58,31 +64,30 @@ define i32* @make_const_null() {
5864

5965
define <2 x i32> @constvector() {
6066
; CHECK-LABEL: @constvector(
61-
; CHECK-NEXT: [[X:%.*]] = freeze <2 x i32> <i32 0, i32 1>
62-
; CHECK-NEXT: ret <2 x i32> [[X]]
67+
; CHECK-NEXT: ret <2 x i32> <i32 0, i32 1>
6368
;
6469
%x = freeze <2 x i32> <i32 0, i32 1>
6570
ret <2 x i32> %x
6671
}
6772

6873
define <3 x i5> @constvector_weird() {
6974
; CHECK-LABEL: @constvector_weird(
70-
; CHECK-NEXT: [[X:%.*]] = freeze <3 x i5> <i5 0, i5 1, i5 10>
71-
; CHECK-NEXT: ret <3 x i5> [[X]]
75+
; CHECK-NEXT: ret <3 x i5> <i5 0, i5 1, i5 10>
7276
;
7377
%x = freeze <3 x i5> <i5 0, i5 1, i5 42>
7478
ret <3 x i5> %x
7579
}
7680

7781
define <2 x float> @constvector_FP() {
7882
; CHECK-LABEL: @constvector_FP(
79-
; CHECK-NEXT: [[X:%.*]] = freeze <2 x float> <float 0.000000e+00, float 1.000000e+00>
80-
; CHECK-NEXT: ret <2 x float> [[X]]
83+
; CHECK-NEXT: ret <2 x float> <float 0.000000e+00, float 1.000000e+00>
8184
;
8285
%x = freeze <2 x float> <float 0.0, float 1.0>
8386
ret <2 x float> %x
8487
}
8588

89+
; Negative test
90+
8691
define <2 x i32> @constvector_noopt() {
8792
; CHECK-LABEL: @constvector_noopt(
8893
; CHECK-NEXT: [[X:%.*]] = freeze <2 x i32> <i32 0, i32 undef>
@@ -92,6 +97,8 @@ define <2 x i32> @constvector_noopt() {
9297
ret <2 x i32> %x
9398
}
9499

100+
; Negative test
101+
95102
define <3 x i5> @constvector_weird_noopt() {
96103
; CHECK-LABEL: @constvector_weird_noopt(
97104
; CHECK-NEXT: [[X:%.*]] = freeze <3 x i5> <i5 0, i5 undef, i5 10>
@@ -101,6 +108,8 @@ define <3 x i5> @constvector_weird_noopt() {
101108
ret <3 x i5> %x
102109
}
103110

111+
; Negative test
112+
104113
define <2 x float> @constvector_FP_noopt() {
105114
; CHECK-LABEL: @constvector_FP_noopt(
106115
; CHECK-NEXT: [[X:%.*]] = freeze <2 x float> <float 0.000000e+00, float undef>
@@ -112,6 +121,8 @@ define <2 x float> @constvector_FP_noopt() {
112121

113122
@g = external global i16, align 1
114123

124+
; Negative test
125+
115126
define float @constant_expr() {
116127
; CHECK-LABEL: @constant_expr(
117128
; CHECK-NEXT: [[R:%.*]] = freeze float bitcast (i32 ptrtoint (i16* @g to i32) to float)
@@ -121,6 +132,8 @@ define float @constant_expr() {
121132
ret float %r
122133
}
123134

135+
; Negative test
136+
124137
define <2 x i31> @vector_element_constant_expr() {
125138
; CHECK-LABEL: @vector_element_constant_expr(
126139
; CHECK-NEXT: [[R:%.*]] = freeze <2 x i31> <i31 34, i31 ptrtoint (i16* @g to i31)>

0 commit comments

Comments
 (0)