Skip to content

Commit 8301e5d

Browse files
committed
[X86] Implement canceling out of XOR with sub nodes
This can happen when comparing the two bit positions.
1 parent 21089ef commit 8301e5d

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49009,6 +49009,28 @@ static SDValue combineSetCCMOVMSK(SDValue EFLAGS, X86::CondCode &CC,
4900949009
static SDValue combineSetCCEFLAGS(SDValue EFLAGS, X86::CondCode &CC,
4901049010
SelectionDAG &DAG,
4901149011
const X86Subtarget &Subtarget) {
49012+
// For EQ/NE, rewrite SUB(xor A,B, xor C,B) -> SUB(A,C) and
49013+
// SUB(add A,K, add C,K) -> SUB(A,C)
49014+
// Only safe if the arithmetic result is unused, since we only
49015+
// care about flags for EQ/NE.
49016+
49017+
// TODO: Can we do this for other comparions like 31 ^ A > 31 ^ B ?
49018+
if ((CC == X86::COND_E || CC == X86::COND_NE) &&
49019+
EFLAGS.getOpcode() == X86ISD::SUB &&
49020+
!EFLAGS.getNode()->hasAnyUseOfValue(0)) {
49021+
using namespace llvm::SDPatternMatch;
49022+
SDValue LHS = EFLAGS.getOperand(0);
49023+
SDValue RHS = EFLAGS.getOperand(1);
49024+
SDValue A, B, C;
49025+
49026+
// (xor A, B) - (xor C, B) -> A - C
49027+
if (sd_match(LHS, m_Xor(m_Value(A), m_Value(B))) &&
49028+
sd_match(RHS, m_Xor(m_Value(C), m_Specific(B)))) {
49029+
DAG.UpdateNodeOperands(EFLAGS.getNode(), A, C);
49030+
return SDValue(EFLAGS.getNode(), 1);
49031+
}
49032+
}
49033+
4901249034
if (CC == X86::COND_B)
4901349035
if (SDValue Flags = combineCarryThroughADD(EFLAGS, DAG))
4901449036
return Flags;

llvm/test/CodeGen/X86/ctlz.ll

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1339,9 +1339,7 @@ define i32 @bsr_eq(i32 %a, i32 %b) {
13391339
; X86-LABEL: bsr_eq:
13401340
; X86: # %bb.0:
13411341
; X86-NEXT: bsrl {{[0-9]+}}(%esp), %ecx
1342-
; X86-NEXT: xorl $31, %ecx
13431342
; X86-NEXT: bsrl {{[0-9]+}}(%esp), %edx
1344-
; X86-NEXT: xorl $31, %edx
13451343
; X86-NEXT: xorl %eax, %eax
13461344
; X86-NEXT: cmpl %edx, %ecx
13471345
; X86-NEXT: sete %al
@@ -1350,9 +1348,7 @@ define i32 @bsr_eq(i32 %a, i32 %b) {
13501348
; X64-LABEL: bsr_eq:
13511349
; X64: # %bb.0:
13521350
; X64-NEXT: bsrl %edi, %ecx
1353-
; X64-NEXT: xorl $31, %ecx
13541351
; X64-NEXT: bsrl %esi, %edx
1355-
; X64-NEXT: xorl $31, %edx
13561352
; X64-NEXT: xorl %eax, %eax
13571353
; X64-NEXT: cmpl %edx, %ecx
13581354
; X64-NEXT: sete %al

0 commit comments

Comments
 (0)