Skip to content

Commit 60d830e

Browse files
committed
[InstCombine] Fold umax(X, C) + -C into usub.sat(X, C)
1 parent 6e8cebe commit 60d830e

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,12 @@ Instruction *InstCombinerImpl::foldAddWithConstant(BinaryOperator &Add) {
994994
}
995995
}
996996

997+
// umax(X, C) + -C --> usub.sat(X, C)
998+
if (match(Op0, m_OneUse(m_UMax(m_Value(X), m_SpecificInt(-*C)))))
999+
return replaceInstUsesWith(
1000+
Add, Builder.CreateBinaryIntrinsic(
1001+
Intrinsic::usub_sat, X, ConstantInt::get(Add.getType(), -*C)));
1002+
9971003
// Fold (add (zext (add X, -1)), 1) -> (zext X) if X is non-zero.
9981004
// TODO: There's a general form for any constant on the outer add.
9991005
if (C->isOne()) {

llvm/test/Transforms/InstCombine/saturating-add-sub.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,8 +2319,7 @@ define i32 @uadd_sat_via_add_swapped_cmp_select_nonstrict(i32 %x, i32 %y) {
23192319

23202320
define i8 @fold_add_umax_to_usub(i8 %a) {
23212321
; CHECK-LABEL: @fold_add_umax_to_usub(
2322-
; CHECK-NEXT: [[UMAX:%.*]] = call i8 @llvm.umax.i8(i8 [[A:%.*]], i8 10)
2323-
; CHECK-NEXT: [[SEL:%.*]] = add i8 [[UMAX]], -10
2322+
; CHECK-NEXT: [[SEL:%.*]] = call i8 @llvm.usub.sat.i8(i8 [[A:%.*]], i8 10)
23242323
; CHECK-NEXT: ret i8 [[SEL]]
23252324
;
23262325
%umax = call i8 @llvm.umax.i8(i8 %a, i8 10)

0 commit comments

Comments
 (0)