Skip to content

Commit 54d663d

Browse files
committed
[DAG] SimplifyDemandedBits - if we're only demanding the signbit, a SMIN/SMAX node can be simplified to a OR/AND node respectively.
Alive2: https://alive2.llvm.org/ce/z/MehvFB
1 parent 4772c66 commit 54d663d

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,6 +1688,22 @@ bool TargetLowering::SimplifyDemandedBits(
16881688
Known.Zero.setBitsFrom(1);
16891689
break;
16901690
}
1691+
case ISD::SMIN: {
1692+
SDValue Op0 = Op.getOperand(0);
1693+
SDValue Op1 = Op.getOperand(1);
1694+
// If we're only wanting the signbit, then we can simplify to OR node.
1695+
if (OriginalDemandedBits.isSignMask())
1696+
return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::OR, dl, VT, Op0, Op1));
1697+
break;
1698+
}
1699+
case ISD::SMAX: {
1700+
SDValue Op0 = Op.getOperand(0);
1701+
SDValue Op1 = Op.getOperand(1);
1702+
// If we're only wanting the signbit, then we can simplify to AND node.
1703+
if (OriginalDemandedBits.isSignMask())
1704+
return TLO.CombineTo(Op, TLO.DAG.getNode(ISD::AND, dl, VT, Op0, Op1));
1705+
break;
1706+
}
16911707
case ISD::SHL: {
16921708
SDValue Op0 = Op.getOperand(0);
16931709
SDValue Op1 = Op.getOperand(1);

llvm/test/CodeGen/X86/smax.ll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -692,9 +692,7 @@ define i64 @test_signbits_i64(i64 %a, i64 %b) nounwind {
692692
; X86-LABEL: test_signbits_i64:
693693
; X86: # %bb.0:
694694
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
695-
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
696-
; X86-NEXT: cmpl %eax, %ecx
697-
; X86-NEXT: cmovgl %ecx, %eax
695+
; X86-NEXT: andl {{[0-9]+}}(%esp), %eax
698696
; X86-NEXT: movl %eax, %edx
699697
; X86-NEXT: sarl $31, %edx
700698
; X86-NEXT: retl
@@ -709,8 +707,7 @@ define i128 @test_signbits_i128(i128 %a, i128 %b) nounwind {
709707
; X64: # %bb.0:
710708
; X64-NEXT: movq %rcx, %rax
711709
; X64-NEXT: sarq $28, %rax
712-
; X64-NEXT: cmpq %rax, %rsi
713-
; X64-NEXT: cmovgq %rsi, %rax
710+
; X64-NEXT: andq %rsi, %rax
714711
; X64-NEXT: movq %rax, %rdx
715712
; X64-NEXT: sarq $63, %rdx
716713
; X64-NEXT: retq

llvm/test/CodeGen/X86/smin.ll

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -693,9 +693,7 @@ define i64 @test_signbits_i64(i64 %a, i64 %b) nounwind {
693693
; X86-LABEL: test_signbits_i64:
694694
; X86: # %bb.0:
695695
; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
696-
; X86-NEXT: movl {{[0-9]+}}(%esp), %ecx
697-
; X86-NEXT: cmpl %eax, %ecx
698-
; X86-NEXT: cmovll %ecx, %eax
696+
; X86-NEXT: orl {{[0-9]+}}(%esp), %eax
699697
; X86-NEXT: movl %eax, %edx
700698
; X86-NEXT: sarl $31, %edx
701699
; X86-NEXT: retl
@@ -710,8 +708,7 @@ define i128 @test_signbits_i128(i128 %a, i128 %b) nounwind {
710708
; X64: # %bb.0:
711709
; X64-NEXT: movq %rcx, %rax
712710
; X64-NEXT: sarq $28, %rax
713-
; X64-NEXT: cmpq %rax, %rsi
714-
; X64-NEXT: cmovlq %rsi, %rax
711+
; X64-NEXT: orq %rsi, %rax
715712
; X64-NEXT: movq %rax, %rdx
716713
; X64-NEXT: sarq $63, %rdx
717714
; X64-NEXT: retq

0 commit comments

Comments
 (0)