Skip to content

Commit 926e731

Browse files
committed
[InstCombine] fold usub.with.overflow to icmp when there's no use of the math value
https://alive2.llvm.org/ce/z/UE48FH This is part of solving issue #56926.
1 parent 8800b11 commit 926e731

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -3281,6 +3281,10 @@ InstCombinerImpl::foldExtractOfOverflowIntrinsic(ExtractValueInst &EV) {
32813281

32823282
assert(*EV.idx_begin() == 1 && "Unexpected extract index for overflow inst");
32833283

3284+
// (usub LHS, RHS) overflows when LHS is unsigned-less-than RHS.
3285+
if (OvID == Intrinsic::usub_with_overflow)
3286+
return new ICmpInst(ICmpInst::ICMP_ULT, WO->getLHS(), WO->getRHS());
3287+
32843288
// If only the overflow result is used, and the right hand side is a
32853289
// constant (or constant splat), we can remove the intrinsic by directly
32863290
// checking for overflow.

llvm/test/Transforms/InstCombine/usubo.ll

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ declare { i8, i1 } @llvm.usub.with.overflow.i8(i8, i8)
66

77
define i1 @test_generic(i64 %a, i64 %b) {
88
; CHECK-LABEL: @test_generic(
9-
; CHECK-NEXT: [[RES:%.*]] = tail call { i64, i1 } @llvm.usub.with.overflow.i64(i64 [[A:%.*]], i64 [[B:%.*]])
10-
; CHECK-NEXT: [[OVERFLOW:%.*]] = extractvalue { i64, i1 } [[RES]], 1
9+
; CHECK-NEXT: [[OVERFLOW:%.*]] = icmp ult i64 [[A:%.*]], [[B:%.*]]
1110
; CHECK-NEXT: ret i1 [[OVERFLOW]]
1211
;
1312
%res = tail call { i64, i1 } @llvm.usub.with.overflow.i64(i64 %a, i64 %b)

0 commit comments

Comments
 (0)