Skip to content

Commit

Permalink
[TLI] Fold ~X >/< ~Y --> Y >/< X
Browse files Browse the repository at this point in the history
Fixes: #61120

Signed-off-by: Jun Zhang <jun@junz.org>

Differential Revision: https://reviews.llvm.org/D146512
  • Loading branch information
junaire committed Mar 23, 2023
1 parent 1c420cd commit b3e12be
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 113 deletions.
17 changes: 17 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
Expand Up @@ -4974,6 +4974,23 @@ SDValue TargetLowering::SimplifySetCC(EVT VT, SDValue N0, SDValue N1,
return DAG.getSetCC(dl, VT, N0, N1, NewCond);
}

// ~X > ~Y --> Y > X
// ~X < ~Y --> Y < X
// ~X < C --> X > ~C
// ~X > C --> X < ~C
if ((isSignedIntSetCC(Cond) || isUnsignedIntSetCC(Cond)) &&
N0.getValueType().isInteger()) {
if (isBitwiseNot(N0)) {
if (isBitwiseNot(N1))
return DAG.getSetCC(dl, VT, N1.getOperand(0), N0.getOperand(0), Cond);

if (DAG.isConstantIntBuildVectorOrConstantInt(N1)) {
SDValue Not = DAG.getNOT(dl, N1, OpVT);
return DAG.getSetCC(dl, VT, Not, N0.getOperand(0), Cond);
}
}
}

if ((Cond == ISD::SETEQ || Cond == ISD::SETNE) &&
N0.getValueType().isInteger()) {
if (N0.getOpcode() == ISD::ADD || N0.getOpcode() == ISD::SUB ||
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/CodeGen/X86/cmov.ll
Expand Up @@ -213,10 +213,10 @@ define i64 @test8(i64 %0, i64 %1, i64 %2) {
define i32 @smin(i32 %x) {
; CHECK-LABEL: smin:
; CHECK: # %bb.0:
; CHECK-NEXT: notl %edi
; CHECK-NEXT: testl %edi, %edi
; CHECK-NEXT: notl %edi
; CHECK-NEXT: movl $-1, %eax
; CHECK-NEXT: cmovsl %edi, %eax
; CHECK-NEXT: cmovnsl %edi, %eax
; CHECK-NEXT: retq
%not_x = xor i32 %x, -1
%1 = icmp slt i32 %not_x, -1
Expand Down

0 comments on commit b3e12be

Please sign in to comment.