Skip to content

Commit de7cee2

Browse files
committed
[X86] getBT - attempt to peek through aext(and(trunc(x),c)) mask/modulo
Ideally we'd fold this with generic DAGCombiner, but that only works for !isTruncateFree cases - we might be able to adapt IsDesirableToPromoteOp to find truncated src ops in the future, but for now just use this peephole. Noticed in Issue #55138
1 parent 39dd297 commit de7cee2

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22975,8 +22975,19 @@ static SDValue getBT(SDValue Src, SDValue BitNo, const SDLoc &DL, SelectionDAG &
2297522975

2297622976
// If the operand types disagree, extend the shift amount to match. Since
2297722977
// BT ignores high bits (like shifts) we can use anyextend.
22978-
if (Src.getValueType() != BitNo.getValueType())
22979-
BitNo = DAG.getNode(ISD::ANY_EXTEND, DL, Src.getValueType(), BitNo);
22978+
if (Src.getValueType() != BitNo.getValueType()) {
22979+
// Peek through a mask/modulo operation.
22980+
// TODO: DAGCombine fails to do this as it just checks isTruncateFree, but
22981+
// we probably need a better IsDesirableToPromoteOp to handle this as well.
22982+
if (BitNo.getOpcode() == ISD::AND && BitNo->hasOneUse())
22983+
BitNo = DAG.getNode(ISD::AND, DL, Src.getValueType(),
22984+
DAG.getNode(ISD::ANY_EXTEND, DL, Src.getValueType(),
22985+
BitNo.getOperand(0)),
22986+
DAG.getNode(ISD::ANY_EXTEND, DL, Src.getValueType(),
22987+
BitNo.getOperand(1)));
22988+
else
22989+
BitNo = DAG.getNode(ISD::ANY_EXTEND, DL, Src.getValueType(), BitNo);
22990+
}
2298022991

2298122992
return DAG.getNode(X86ISD::BT, DL, MVT::i32, Src, BitNo);
2298222993
}

llvm/test/CodeGen/X86/setcc.ll

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,8 @@ define i16 @shift_and(i16 %a) {
311311
define i32 @PR55138(i32 %x) {
312312
; X86-LABEL: PR55138:
313313
; X86: ## %bb.0:
314-
; X86-NEXT: movb {{[0-9]+}}(%esp), %al
315-
; X86-NEXT: andb $15, %al
316-
; X86-NEXT: movzbl %al, %ecx
314+
; X86-NEXT: movzbl {{[0-9]+}}(%esp), %ecx
315+
; X86-NEXT: andl $15, %ecx
317316
; X86-NEXT: movl $27030, %edx ## imm = 0x6996
318317
; X86-NEXT: xorl %eax, %eax
319318
; X86-NEXT: btl %ecx, %edx
@@ -322,11 +321,10 @@ define i32 @PR55138(i32 %x) {
322321
;
323322
; X64-LABEL: PR55138:
324323
; X64: ## %bb.0:
325-
; X64-NEXT: andb $15, %dil
326-
; X64-NEXT: movzbl %dil, %ecx
327-
; X64-NEXT: movl $27030, %edx ## imm = 0x6996
324+
; X64-NEXT: andl $15, %edi
325+
; X64-NEXT: movl $27030, %ecx ## imm = 0x6996
328326
; X64-NEXT: xorl %eax, %eax
329-
; X64-NEXT: btl %ecx, %edx
327+
; X64-NEXT: btl %edi, %ecx
330328
; X64-NEXT: setb %al
331329
; X64-NEXT: retq
332330
%urem = and i32 %x, 15

0 commit comments

Comments
 (0)