Skip to content

Commit 8d9c224

Browse files
committed
[SelectionDAG] Teach GetDemandedBits to look at the known zeros of the LHS when handling ISD::AND
If the LHS has known zeros, then the RHS immediate mask might have been simplified to remove those bits. This patch adds a call to computeKnownBits to get the known zeroes to handle that possibility. I left an early out to skip the call if all of the demanded bits are set in the mask. Differential Revision: https://reviews.llvm.org/D58464 llvm-svn: 354514
1 parent c3b496d commit 8d9c224

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,9 +2102,13 @@ SDValue SelectionDAG::GetDemandedBits(SDValue V, const APInt &Mask) {
21022102
break;
21032103
case ISD::AND: {
21042104
// X & -1 -> X (ignoring bits which aren't demanded).
2105-
ConstantSDNode *AndVal = isConstOrConstSplat(V.getOperand(1));
2106-
if (AndVal && Mask.isSubsetOf(AndVal->getAPIntValue()))
2107-
return V.getOperand(0);
2105+
// Also handle the case where masked out bits in X are known to be zero.
2106+
if (ConstantSDNode *RHSC = isConstOrConstSplat(V.getOperand(1))) {
2107+
const APInt &AndVal = RHSC->getAPIntValue();
2108+
if (Mask.isSubsetOf(AndVal) ||
2109+
Mask.isSubsetOf(computeKnownBits(V.getOperand(0)).Zero | AndVal))
2110+
return V.getOperand(0);
2111+
}
21082112
break;
21092113
}
21102114
case ISD::ANY_EXTEND: {

llvm/test/CodeGen/X86/bt.ll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,6 @@ define zeroext i1 @demanded_with_known_zeroes(i32 %bit, i32 %bits) {
11531153
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
11541154
; X86-NEXT: movb {{[0-9]+}}(%esp), %cl
11551155
; X86-NEXT: shlb $2, %cl
1156-
; X86-NEXT: andb $28, %cl
11571156
; X86-NEXT: movzbl %cl, %ecx
11581157
; X86-NEXT: btl %ecx, %eax
11591158
; X86-NEXT: setb %al
@@ -1162,7 +1161,6 @@ define zeroext i1 @demanded_with_known_zeroes(i32 %bit, i32 %bits) {
11621161
; X64-LABEL: demanded_with_known_zeroes:
11631162
; X64: # %bb.0: # %entry
11641163
; X64-NEXT: shlb $2, %dil
1165-
; X64-NEXT: andb $28, %dil
11661164
; X64-NEXT: movzbl %dil, %eax
11671165
; X64-NEXT: btl %eax, %esi
11681166
; X64-NEXT: setb %al

0 commit comments

Comments
 (0)