Skip to content

Commit 17b4cac

Browse files
authored
[DAG] isSplatValue - only treat binop splats shared undef elements as undef (#135597)
#134602 demonstrated an issue where an AND node always had at least one demanded UNDEF element in either operand, and incorrectly reported this an all-undef result - despite the other element being 0 (so would correctly fold to 0). This fix only assumes a binops splats element is undefined if both operands are undef. Fixes #134602
1 parent fceb9ce commit 17b4cac

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3002,9 +3002,12 @@ bool SelectionDAG::isSplatValue(SDValue V, const APInt &DemandedElts,
30023002
APInt UndefLHS, UndefRHS;
30033003
SDValue LHS = V.getOperand(0);
30043004
SDValue RHS = V.getOperand(1);
3005+
// Only propagate common undef elts for both operands, otherwise we might
3006+
// fail to handle binop-specific undef handling.
3007+
// e.g. (and undef, 0) -> 0 etc.
30053008
if (isSplatValue(LHS, DemandedElts, UndefLHS, Depth + 1) &&
30063009
isSplatValue(RHS, DemandedElts, UndefRHS, Depth + 1)) {
3007-
UndefElts = UndefLHS | UndefRHS;
3010+
UndefElts = UndefLHS & UndefRHS;
30083011
return true;
30093012
}
30103013
return false;

llvm/test/CodeGen/RISCV/rvv/fixed-vectors-buildvec-of-binop.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,7 @@ define void @buggy(i32 %0) #0 {
452452
; RV64-NEXT: vsetivli zero, 4, e32, m1, ta, ma
453453
; RV64-NEXT: vmv.v.x v8, a0
454454
; RV64-NEXT: vor.vi v8, v8, 1
455-
; RV64-NEXT: vrgather.vi v9, v8, 0
456-
; RV64-NEXT: vse32.v v9, (zero)
455+
; RV64-NEXT: vse32.v v8, (zero)
457456
; RV64-NEXT: ret
458457
entry:
459458
%mul.us.us.i.3 = shl i32 %0, 1

llvm/test/CodeGen/X86/pr134602.ll

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
; RUN: llc < %s -mtriple=i686-- | FileCheck %s --check-prefix=X86
33
; RUN: llc < %s -mtriple=x86_64-- | FileCheck %s --check-prefix=X64
44

5-
; FIXME: incorrect vector codegen due to bad handling of splats of binops containing undefs
5+
; Test for incorrect vector codegen due to bad handling of splats of binops containing undefs
66
define i32 @PR134602(i16 %a0) {
77
; X86-LABEL: PR134602:
88
; X86: # %bb.0:
@@ -14,7 +14,16 @@ define i32 @PR134602(i16 %a0) {
1414
;
1515
; X64-LABEL: PR134602:
1616
; X64: # %bb.0:
17-
; X64-NEXT: xorl %eax, %eax
17+
; X64-NEXT: movzwl %di, %eax
18+
; X64-NEXT: movd %eax, %xmm0
19+
; X64-NEXT: por {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm0
20+
; X64-NEXT: pshuflw {{.*#+}} xmm1 = xmm0[2,2,2,2,4,5,6,7]
21+
; X64-NEXT: paddw %xmm0, %xmm1
22+
; X64-NEXT: movdqa %xmm1, %xmm0
23+
; X64-NEXT: psrld $16, %xmm0
24+
; X64-NEXT: paddw %xmm1, %xmm0
25+
; X64-NEXT: movd %xmm0, %eax
26+
; X64-NEXT: cwtl
1827
; X64-NEXT: retq
1928
%splat= insertelement <4 x i16> zeroinitializer, i16 %a0, i64 0
2029
%mul = mul <4 x i16> %splat, <i16 1, i16 1, i16 0, i16 0>

0 commit comments

Comments
 (0)