[DAG] isKnownToBeAPowerOfTwo - add missing DemandedElts handling to ISD::TRUNCATE and hidden m_Neg pattern (#190190)

Use MaskedVectorIsZero to match X & -X pattern when only DemandedElts
match the negation pattern

Fixes #181654 (properly)
This commit is contained in:
Simon Pilgrim 2026-04-03 13:03:33 +01:00 committed by GitHub
parent f1d167123c
commit 15ed4f6c49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 10 deletions

View File

@ -4775,8 +4775,10 @@ bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val,
// If x != 0:
// x & -x -> non-zero pow2
// so if we find the pattern return whether we know `x` is non-zero.
SDValue X;
if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))))
SDValue X, Z;
if (sd_match(Val, m_And(m_Value(X), m_Neg(m_Deferred(X)))) ||
(sd_match(Val, m_And(m_Value(X), m_Sub(m_Value(Z), m_Deferred(X)))) &&
MaskedVectorIsZero(Z, DemandedElts, Depth + 1)))
return OrZero || isKnownNeverZero(X, DemandedElts, Depth);
break;
}
@ -4803,10 +4805,10 @@ bool SelectionDAG::isKnownToBeAPowerOfTwo(SDValue Val,
Depth + 1);
}
case ISD::TRUNCATE: {
return (OrZero || isKnownNeverZero(Val, Depth)) &&
isKnownToBeAPowerOfTwo(Val.getOperand(0), OrZero, Depth + 1);
}
case ISD::TRUNCATE:
return (OrZero || isKnownNeverZero(Val, DemandedElts, Depth)) &&
isKnownToBeAPowerOfTwo(Val.getOperand(0), DemandedElts, OrZero,
Depth + 1);
case ISD::ROTL:
case ISD::ROTR:

View File

@ -1373,14 +1373,13 @@ define i8 @pow2_trunc_vec(i8 %x8, <4 x i32> %a, ptr %p) {
; CHECK-NEXT: movdqa {{.*#+}} xmm1 = [0,4294967295,4294967295,4294967295]
; CHECK-NEXT: psubd %xmm0, %xmm1
; CHECK-NEXT: pand %xmm0, %xmm1
; CHECK-NEXT: movd %xmm1, %ecx
; CHECK-NEXT: movd %xmm1, %eax
; CHECK-NEXT: pand {{\.?LCPI[0-9]+_[0-9]+}}(%rip), %xmm1
; CHECK-NEXT: packuswb %xmm1, %xmm1
; CHECK-NEXT: packuswb %xmm1, %xmm1
; CHECK-NEXT: movd %xmm1, (%rsi)
; CHECK-NEXT: movzbl %dil, %eax
; CHECK-NEXT: divb %cl
; CHECK-NEXT: movzbl %ah, %eax
; CHECK-NEXT: decb %al
; CHECK-NEXT: andb %dil, %al
; CHECK-NEXT: # kill: def $al killed $al killed $eax
; CHECK-NEXT: retq
%a.neg = sub <4 x i32> <i32 0, i32 -1, i32 -1, i32 -1>, %a