Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
22 changes: 22 additions & 0 deletions llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6199,6 +6199,28 @@ 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)
//
// IR:
// %sub = sub %a, %b
// %cond = umin %sub, %a
// ->
// %usubo = usubo %a, %b
// %overflow = extractvalue %usubo, 1
// %sub = extractvalue %usubo, 0
// %cond = select %overflow, %a, %sub
if (N0.getOpcode() == ISD::SUB) {
SDValue A, B, C;
if (sd_match(N, m_UMin(m_Sub(m_Value(A), m_Value(B)), m_Value(C)))) {
EVT AVT = A.getValueType();
if (A == C && TLI.isOperationLegalOrCustom(ISD::USUBO, AVT)) {
SDVTList VTs = DAG.getVTList(AVT, MVT::i1);
SDValue USO = DAG.getNode(ISD::USUBO, DL, VTs, A, B);
return DAG.getSelect(DL, VT, USO.getValue(1), A, USO.getValue(0));
}
}
}

// Simplify the operands using demanded-bits information.
if (SimplifyDemandedBits(SDValue(N, 0)))
return SDValue(N, 0);
Expand Down
16 changes: 16 additions & 0 deletions llvm/test/CodeGen/X86/underflow-compare-fold.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
; RUN: llc < %s -mtriple=x86_64 | FileCheck %s

; GitHub issue #161036

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