Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6199,6 +6199,19 @@ SDValue DAGCombiner::visitIMINMAX(SDNode *N) {
SDLoc(N), VT, N0, N1))
return SD;

// (umin (sub a, b) a) -> (usubo a, b); (select usubo.1, a, usubo.0)
{
Comment thread
ckoparkar marked this conversation as resolved.
Outdated
SDValue A, B;
if (sd_match(N, m_UMin(m_Sub(m_Value(A), m_Value(B)), m_Deferred(A)))) {
if (TLI.isOperationLegalOrCustom(ISD::USUBO, VT)) {
Comment thread
ckoparkar marked this conversation as resolved.
Outdated
Comment thread
ckoparkar marked this conversation as resolved.
Outdated
EVT SETCCT = getSetCCResultType(VT);
Comment thread
ckoparkar marked this conversation as resolved.
Outdated
SDVTList VTs = DAG.getVTList(VT, SETCCT);
SDValue USO = DAG.getNode(ISD::USUBO, DL, VTs, A, B);
return DAG.getSelect(DL, VT, USO.getValue(1), A, USO.getValue(0));
}
Comment thread
ckoparkar marked this conversation as resolved.
Outdated
}
}

Comment thread
ckoparkar marked this conversation as resolved.
Outdated
// Simplify the operands using demanded-bits information.
Comment thread
ckoparkar marked this conversation as resolved.
Outdated
if (SimplifyDemandedBits(SDValue(N, 0)))
return SDValue(N, 0);
Comment thread
ckoparkar marked this conversation as resolved.
Outdated
Expand Down
14 changes: 14 additions & 0 deletions llvm/test/CodeGen/AArch64/underflow-compare-fold.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
; RUN: llc < %s -mtriple=aarch64 | FileCheck %s

; GitHub issue #161036

define i64 @underflow_compare_fold(i64 %a, i64 %b) {
Comment thread
ckoparkar marked this conversation as resolved.
Outdated
; CHECK-LABEL: underflow_compare_fold
; CHECK: // %bb.0:
; CHECK-NEXT: subs x8, x0, x1
; CHECK-NEXT: csel x0, x0, x8, lo
; CHECK-NEXT: ret
%sub = sub i64 %a, %b
%cond = tail call i64 @llvm.umin.i64(i64 %sub, i64 %a)
ret i64 %cond
}
Comment thread
ckoparkar marked this conversation as resolved.
Outdated
15 changes: 15 additions & 0 deletions llvm/test/CodeGen/X86/underflow-compare-fold.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; RUN: llc < %s -mtriple=x86_64 | FileCheck %s

; GitHub issue #161036

define i64 @underflow_compare_fold(i64 %a, i64 %b) {
; CHECK-LABEL: underflow_compare_fold
; CHECK-LABEL: %bb.0
; CHECK-NEXT: movq %rdi, %rax
; CHECK-NEXT: subq %rsi, %rax
; CHECK-NEXT: cmovbq %rdi, %rax
; CHECK-NEXT: retq
%sub = sub i64 %a, %b
%cond = tail call i64 @llvm.umin.i64(i64 %sub, i64 %a)
ret i64 %cond
}
Loading