Skip to content

Commit 3dfdb4d

Browse files
diggerlinamy-kwan
andauthored
[SelectionDAG] Propagate poison in getNode with two operands if the input is poison. (#135387)
Propagation to poison in function `SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,SDValue N1, SDValue N2, const SDNodeFlags Flags) ` if one of the input is poison. The patch also revert the test cases llvm/test/CodeGen/X86/pr119158.ll llvm/test/CodeGen/X86/half.ll which are mentioned in #125883 (comment) --------- Co-authored-by: Amy Kwan <amy.kwan1@ibm.com>
1 parent 4041791 commit 3dfdb4d

File tree

4 files changed

+29
-41
lines changed

4 files changed

+29
-41
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7578,15 +7578,18 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
75787578
} else {
75797579
switch (Opcode) {
75807580
case ISD::SUB:
7581-
return getUNDEF(VT); // fold op(undef, arg2) -> undef
7581+
// fold op(undef, arg2) -> undef, fold op(poison, arg2) ->poison.
7582+
return N1.getOpcode() == ISD::POISON ? getPOISON(VT) : getUNDEF(VT);
75827583
case ISD::SIGN_EXTEND_INREG:
75837584
case ISD::UDIV:
75847585
case ISD::SDIV:
75857586
case ISD::UREM:
75867587
case ISD::SREM:
75877588
case ISD::SSUBSAT:
75887589
case ISD::USUBSAT:
7589-
return getConstant(0, DL, VT); // fold op(undef, arg2) -> 0
7590+
// fold op(undef, arg2) -> 0, fold op(poison, arg2) -> poison.
7591+
return N1.getOpcode() == ISD::POISON ? getPOISON(VT)
7592+
: getConstant(0, DL, VT);
75907593
}
75917594
}
75927595
}
@@ -7606,16 +7609,22 @@ SDValue SelectionDAG::getNode(unsigned Opcode, const SDLoc &DL, EVT VT,
76067609
case ISD::SDIV:
76077610
case ISD::UREM:
76087611
case ISD::SREM:
7609-
return getUNDEF(VT); // fold op(arg1, undef) -> undef
7612+
// fold op(arg1, undef) -> undef, fold op(arg1, poison) -> poison.
7613+
return N2.getOpcode() == ISD::POISON ? getPOISON(VT) : getUNDEF(VT);
76107614
case ISD::MUL:
76117615
case ISD::AND:
76127616
case ISD::SSUBSAT:
76137617
case ISD::USUBSAT:
7614-
return getConstant(0, DL, VT); // fold op(arg1, undef) -> 0
7618+
// fold op(arg1, undef) -> 0, fold op(arg1, poison) -> poison.
7619+
return N2.getOpcode() == ISD::POISON ? getPOISON(VT)
7620+
: getConstant(0, DL, VT);
76157621
case ISD::OR:
76167622
case ISD::SADDSAT:
76177623
case ISD::UADDSAT:
7618-
return getAllOnesConstant(DL, VT);
7624+
// fold op(arg1, undef) -> an all-ones constant, fold op(arg1, poison) ->
7625+
// poison.
7626+
return N2.getOpcode() == ISD::POISON ? getPOISON(VT)
7627+
: getAllOnesConstant(DL, VT);
76197628
}
76207629
}
76217630

llvm/test/CodeGen/X86/half.ll

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,8 +1991,8 @@ define void @pr63114() {
19911991
; CHECK-LIBCALL-LABEL: pr63114:
19921992
; CHECK-LIBCALL: # %bb.0:
19931993
; CHECK-LIBCALL-NEXT: movdqu (%rax), %xmm4
1994-
; CHECK-LIBCALL-NEXT: pshufhw {{.*#+}} xmm0 = xmm4[0,1,2,3,4,5,7,7]
1995-
; CHECK-LIBCALL-NEXT: pshufd {{.*#+}} xmm0 = xmm0[0,2,2,3]
1994+
; CHECK-LIBCALL-NEXT: pshuflw {{.*#+}} xmm0 = xmm4[0,1,3,3,4,5,6,7]
1995+
; CHECK-LIBCALL-NEXT: pshufd {{.*#+}} xmm0 = xmm0[0,0,2,1]
19961996
; CHECK-LIBCALL-NEXT: movdqa {{.*#+}} xmm1 = [65535,65535,65535,0,65535,65535,65535,65535]
19971997
; CHECK-LIBCALL-NEXT: pand %xmm1, %xmm0
19981998
; CHECK-LIBCALL-NEXT: movq {{.*#+}} xmm2 = [0,0,0,15360,0,0,0,0]
@@ -2001,8 +2001,8 @@ define void @pr63114() {
20012001
; CHECK-LIBCALL-NEXT: pand %xmm3, %xmm0
20022002
; CHECK-LIBCALL-NEXT: movdqa {{.*#+}} xmm5 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60]
20032003
; CHECK-LIBCALL-NEXT: por %xmm5, %xmm0
2004-
; CHECK-LIBCALL-NEXT: pshuflw {{.*#+}} xmm6 = xmm4[0,1,3,3,4,5,6,7]
2005-
; CHECK-LIBCALL-NEXT: pshufd {{.*#+}} xmm6 = xmm6[0,0,2,1]
2004+
; CHECK-LIBCALL-NEXT: pshufhw {{.*#+}} xmm6 = xmm4[0,1,2,3,4,5,7,7]
2005+
; CHECK-LIBCALL-NEXT: pshufd {{.*#+}} xmm6 = xmm6[0,2,2,3]
20062006
; CHECK-LIBCALL-NEXT: pand %xmm1, %xmm6
20072007
; CHECK-LIBCALL-NEXT: por %xmm2, %xmm6
20082008
; CHECK-LIBCALL-NEXT: pand %xmm3, %xmm6
@@ -2020,8 +2020,8 @@ define void @pr63114() {
20202020
; CHECK-LIBCALL-NEXT: por %xmm5, %xmm7
20212021
; CHECK-LIBCALL-NEXT: movdqu %xmm7, 0
20222022
; CHECK-LIBCALL-NEXT: movdqu %xmm4, 32
2023-
; CHECK-LIBCALL-NEXT: movdqu %xmm6, 16
2024-
; CHECK-LIBCALL-NEXT: movdqu %xmm0, 48
2023+
; CHECK-LIBCALL-NEXT: movdqu %xmm6, 48
2024+
; CHECK-LIBCALL-NEXT: movdqu %xmm0, 16
20252025
; CHECK-LIBCALL-NEXT: retq
20262026
;
20272027
; BWON-F16C-LABEL: pr63114:
@@ -2056,8 +2056,8 @@ define void @pr63114() {
20562056
; CHECK-I686-LABEL: pr63114:
20572057
; CHECK-I686: # %bb.0:
20582058
; CHECK-I686-NEXT: movdqu (%eax), %xmm6
2059-
; CHECK-I686-NEXT: pshufhw {{.*#+}} xmm0 = xmm6[0,1,2,3,4,5,7,7]
2060-
; CHECK-I686-NEXT: pshufd {{.*#+}} xmm0 = xmm0[0,2,2,3]
2059+
; CHECK-I686-NEXT: pshuflw {{.*#+}} xmm0 = xmm6[0,1,3,3,4,5,6,7]
2060+
; CHECK-I686-NEXT: pshufd {{.*#+}} xmm0 = xmm0[0,0,2,1]
20612061
; CHECK-I686-NEXT: movdqa {{.*#+}} xmm1 = [65535,65535,65535,0,65535,65535,65535,65535]
20622062
; CHECK-I686-NEXT: pand %xmm1, %xmm0
20632063
; CHECK-I686-NEXT: movq {{.*#+}} xmm2 = [0,0,0,15360,0,0,0,0]
@@ -2066,8 +2066,8 @@ define void @pr63114() {
20662066
; CHECK-I686-NEXT: pand %xmm3, %xmm0
20672067
; CHECK-I686-NEXT: movdqa {{.*#+}} xmm4 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,60]
20682068
; CHECK-I686-NEXT: por %xmm4, %xmm0
2069-
; CHECK-I686-NEXT: pshuflw {{.*#+}} xmm5 = xmm6[0,1,3,3,4,5,6,7]
2070-
; CHECK-I686-NEXT: pshufd {{.*#+}} xmm5 = xmm5[0,0,2,1]
2069+
; CHECK-I686-NEXT: pshufhw {{.*#+}} xmm5 = xmm6[0,1,2,3,4,5,7,7]
2070+
; CHECK-I686-NEXT: pshufd {{.*#+}} xmm5 = xmm5[0,2,2,3]
20712071
; CHECK-I686-NEXT: pand %xmm1, %xmm5
20722072
; CHECK-I686-NEXT: por %xmm2, %xmm5
20732073
; CHECK-I686-NEXT: pand %xmm3, %xmm5
@@ -2085,8 +2085,8 @@ define void @pr63114() {
20852085
; CHECK-I686-NEXT: por %xmm4, %xmm7
20862086
; CHECK-I686-NEXT: movdqu %xmm7, 0
20872087
; CHECK-I686-NEXT: movdqu %xmm6, 32
2088-
; CHECK-I686-NEXT: movdqu %xmm5, 16
2089-
; CHECK-I686-NEXT: movdqu %xmm0, 48
2088+
; CHECK-I686-NEXT: movdqu %xmm5, 48
2089+
; CHECK-I686-NEXT: movdqu %xmm0, 16
20902090
; CHECK-I686-NEXT: retl
20912091
%1 = load <24 x half>, ptr poison, align 2
20922092
%2 = shufflevector <24 x half> %1, <24 x half> poison, <8 x i32> <i32 2, i32 5, i32 8, i32 11, i32 14, i32 17, i32 20, i32 23>

llvm/test/CodeGen/X86/poison-ops.ll

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ define <4 x i32> @sub_poison_lhs_vec(<4 x i32> %x) {
6868
define i32 @mul_poison_rhs(i32 %x) {
6969
; CHECK-LABEL: mul_poison_rhs:
7070
; CHECK: # %bb.0:
71-
; CHECK-NEXT: xorl %eax, %eax
7271
; CHECK-NEXT: retq
7372
%r = mul i32 %x, poison
7473
ret i32 %r
@@ -77,7 +76,6 @@ define i32 @mul_poison_rhs(i32 %x) {
7776
define <4 x i32> @mul_poison_rhs_vec(<4 x i32> %x) {
7877
; CHECK-LABEL: mul_poison_rhs_vec:
7978
; CHECK: # %bb.0:
80-
; CHECK-NEXT: xorps %xmm0, %xmm0
8179
; CHECK-NEXT: retq
8280
%r = mul <4 x i32> %x, poison
8381
ret <4 x i32> %r
@@ -86,7 +84,6 @@ define <4 x i32> @mul_poison_rhs_vec(<4 x i32> %x) {
8684
define i32 @mul_poison_lhs(i32 %x) {
8785
; CHECK-LABEL: mul_poison_lhs:
8886
; CHECK: # %bb.0:
89-
; CHECK-NEXT: xorl %eax, %eax
9087
; CHECK-NEXT: retq
9188
%r = mul i32 poison, %x
9289
ret i32 %r
@@ -95,7 +92,6 @@ define i32 @mul_poison_lhs(i32 %x) {
9592
define <4 x i32> @mul_poison_lhs_vec(<4 x i32> %x) {
9693
; CHECK-LABEL: mul_poison_lhs_vec:
9794
; CHECK: # %bb.0:
98-
; CHECK-NEXT: xorps %xmm0, %xmm0
9995
; CHECK-NEXT: retq
10096
%r = mul <4 x i32> poison, %x
10197
ret <4 x i32> %r
@@ -120,7 +116,6 @@ define <4 x i32> @sdiv_poison_rhs_vec(<4 x i32> %x) {
120116
define i32 @sdiv_poison_lhs(i32 %x) {
121117
; CHECK-LABEL: sdiv_poison_lhs:
122118
; CHECK: # %bb.0:
123-
; CHECK-NEXT: xorl %eax, %eax
124119
; CHECK-NEXT: retq
125120
%r = sdiv i32 poison, %x
126121
ret i32 %r
@@ -129,7 +124,6 @@ define i32 @sdiv_poison_lhs(i32 %x) {
129124
define <4 x i32> @sdiv_poison_lhs_vec(<4 x i32> %x) {
130125
; CHECK-LABEL: sdiv_poison_lhs_vec:
131126
; CHECK: # %bb.0:
132-
; CHECK-NEXT: xorps %xmm0, %xmm0
133127
; CHECK-NEXT: retq
134128
%r = sdiv <4 x i32> poison, %x
135129
ret <4 x i32> %r
@@ -154,7 +148,6 @@ define <4 x i32> @udiv_poison_rhs_vec(<4 x i32> %x) {
154148
define i32 @udiv_poison_lhs(i32 %x) {
155149
; CHECK-LABEL: udiv_poison_lhs:
156150
; CHECK: # %bb.0:
157-
; CHECK-NEXT: xorl %eax, %eax
158151
; CHECK-NEXT: retq
159152
%r = udiv i32 poison, %x
160153
ret i32 %r
@@ -163,7 +156,6 @@ define i32 @udiv_poison_lhs(i32 %x) {
163156
define <4 x i32> @udiv_poison_lhs_vec(<4 x i32> %x) {
164157
; CHECK-LABEL: udiv_poison_lhs_vec:
165158
; CHECK: # %bb.0:
166-
; CHECK-NEXT: xorps %xmm0, %xmm0
167159
; CHECK-NEXT: retq
168160
%r = udiv <4 x i32> poison, %x
169161
ret <4 x i32> %r
@@ -188,7 +180,6 @@ define <4 x i32> @srem_poison_rhs_vec(<4 x i32> %x) {
188180
define i32 @srem_poison_lhs(i32 %x) {
189181
; CHECK-LABEL: srem_poison_lhs:
190182
; CHECK: # %bb.0:
191-
; CHECK-NEXT: xorl %eax, %eax
192183
; CHECK-NEXT: retq
193184
%r = srem i32 poison, %x
194185
ret i32 %r
@@ -197,7 +188,6 @@ define i32 @srem_poison_lhs(i32 %x) {
197188
define <4 x i32> @srem_poison_lhs_vec(<4 x i32> %x) {
198189
; CHECK-LABEL: srem_poison_lhs_vec:
199190
; CHECK: # %bb.0:
200-
; CHECK-NEXT: xorps %xmm0, %xmm0
201191
; CHECK-NEXT: retq
202192
%r = srem <4 x i32> poison, %x
203193
ret <4 x i32> %r
@@ -222,7 +212,6 @@ define <4 x i32> @urem_poison_rhs_vec(<4 x i32> %x) {
222212
define i32 @urem_poison_lhs(i32 %x) {
223213
; CHECK-LABEL: urem_poison_lhs:
224214
; CHECK: # %bb.0:
225-
; CHECK-NEXT: xorl %eax, %eax
226215
; CHECK-NEXT: retq
227216
%r = urem i32 poison, %x
228217
ret i32 %r
@@ -231,7 +220,6 @@ define i32 @urem_poison_lhs(i32 %x) {
231220
define <4 x i32> @urem_poison_lhs_vec(<4 x i32> %x) {
232221
; CHECK-LABEL: urem_poison_lhs_vec:
233222
; CHECK: # %bb.0:
234-
; CHECK-NEXT: xorps %xmm0, %xmm0
235223
; CHECK-NEXT: retq
236224
%r = urem <4 x i32> poison, %x
237225
ret <4 x i32> %r
@@ -342,7 +330,6 @@ define <4 x i32> @shl_poison_lhs_vec(<4 x i32> %x) {
342330
define i32 @and_poison_rhs(i32 %x) {
343331
; CHECK-LABEL: and_poison_rhs:
344332
; CHECK: # %bb.0:
345-
; CHECK-NEXT: xorl %eax, %eax
346333
; CHECK-NEXT: retq
347334
%r = and i32 %x, poison
348335
ret i32 %r
@@ -351,7 +338,6 @@ define i32 @and_poison_rhs(i32 %x) {
351338
define <4 x i32> @and_poison_rhs_vec(<4 x i32> %x) {
352339
; CHECK-LABEL: and_poison_rhs_vec:
353340
; CHECK: # %bb.0:
354-
; CHECK-NEXT: xorps %xmm0, %xmm0
355341
; CHECK-NEXT: retq
356342
%r = and <4 x i32> %x, poison
357343
ret <4 x i32> %r
@@ -360,7 +346,6 @@ define <4 x i32> @and_poison_rhs_vec(<4 x i32> %x) {
360346
define i32 @and_poison_lhs(i32 %x) {
361347
; CHECK-LABEL: and_poison_lhs:
362348
; CHECK: # %bb.0:
363-
; CHECK-NEXT: xorl %eax, %eax
364349
; CHECK-NEXT: retq
365350
%r = and i32 poison, %x
366351
ret i32 %r
@@ -369,7 +354,6 @@ define i32 @and_poison_lhs(i32 %x) {
369354
define <4 x i32> @and_poison_lhs_vec(<4 x i32> %x) {
370355
; CHECK-LABEL: and_poison_lhs_vec:
371356
; CHECK: # %bb.0:
372-
; CHECK-NEXT: xorps %xmm0, %xmm0
373357
; CHECK-NEXT: retq
374358
%r = and <4 x i32> poison, %x
375359
ret <4 x i32> %r
@@ -378,7 +362,6 @@ define <4 x i32> @and_poison_lhs_vec(<4 x i32> %x) {
378362
define i32 @or_poison_rhs(i32 %x) {
379363
; CHECK-LABEL: or_poison_rhs:
380364
; CHECK: # %bb.0:
381-
; CHECK-NEXT: movl $-1, %eax
382365
; CHECK-NEXT: retq
383366
%r = or i32 %x, poison
384367
ret i32 %r
@@ -387,7 +370,6 @@ define i32 @or_poison_rhs(i32 %x) {
387370
define <4 x i32> @or_poison_rhs_vec(<4 x i32> %x) {
388371
; CHECK-LABEL: or_poison_rhs_vec:
389372
; CHECK: # %bb.0:
390-
; CHECK-NEXT: pcmpeqd %xmm0, %xmm0
391373
; CHECK-NEXT: retq
392374
%r = or <4 x i32> %x, poison
393375
ret <4 x i32> %r
@@ -396,7 +378,6 @@ define <4 x i32> @or_poison_rhs_vec(<4 x i32> %x) {
396378
define i32 @or_poison_lhs(i32 %x) {
397379
; CHECK-LABEL: or_poison_lhs:
398380
; CHECK: # %bb.0:
399-
; CHECK-NEXT: movl $-1, %eax
400381
; CHECK-NEXT: retq
401382
%r = or i32 poison, %x
402383
ret i32 %r
@@ -405,7 +386,6 @@ define i32 @or_poison_lhs(i32 %x) {
405386
define <4 x i32> @or_poison_lhs_vec(<4 x i32> %x) {
406387
; CHECK-LABEL: or_poison_lhs_vec:
407388
; CHECK: # %bb.0:
408-
; CHECK-NEXT: pcmpeqd %xmm0, %xmm0
409389
; CHECK-NEXT: retq
410390
%r = or <4 x i32> poison, %x
411391
ret <4 x i32> %r

llvm/test/CodeGen/X86/pr119158.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@ define dso_local void @foo() #1 {
55
; CHECK-LABEL: foo:
66
; CHECK: # %bb.0: # %newFuncRoot
77
; CHECK-NEXT: vpmovzxbd {{.*#+}} ymm0 = mem[0],zero,zero,zero,mem[1],zero,zero,zero,mem[2],zero,zero,zero,mem[3],zero,zero,zero,mem[4],zero,zero,zero,mem[5],zero,zero,zero,mem[6],zero,zero,zero,mem[7],zero,zero,zero
8-
; CHECK-NEXT: vpbroadcastd {{.*#+}} ymm1 = [18,0,18,0,18,0,18,0,18,0,18,0,18,0,18,0]
9-
; CHECK-NEXT: vpmaddwd %ymm1, %ymm0, %ymm0
10-
; CHECK-NEXT: vpaddd {{\.?LCPI[0-9]+_[0-9]+}}(%rip){1to8}, %ymm0, %ymm0
11-
; CHECK-NEXT: vpsrld $7, %ymm0, %ymm0
8+
; CHECK-NEXT: vpbroadcastd {{.*#+}} ymm1 = [64,64,64,64,64,64,64,64]
9+
; CHECK-NEXT: vpdpwssd {{\.?LCPI[0-9]+_[0-9]+}}(%rip){1to8}, %ymm0, %ymm1
10+
; CHECK-NEXT: vpsrld $7, %ymm1, %ymm0
1211
; CHECK-NEXT: vpackusdw %ymm0, %ymm0, %ymm0
1312
; CHECK-NEXT: vpermq {{.*#+}} ymm0 = ymm0[0,2,1,3]
1413
; CHECK-NEXT: vmovdqu %ymm0, (%rax)

0 commit comments

Comments
 (0)