[PATCH] D156499: [InstCombine] Fold abs of known sign operand when source is sub
Allen zhong via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Thu Jul 27 19:35:38 PDT 2023
Allen created this revision.
Allen added reviewers: bcl5980, peterwaller-arm, RKSimon, goldstein.w.n, nikic.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
Allen requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
abs(x-y) --> x-y where x >= y
abs(x-y) --> y-x where x <= y
https://reviews.llvm.org/D156499
Files:
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/test/Transforms/InstCombine/abs-intrinsic.ll
Index: llvm/test/Transforms/InstCombine/abs-intrinsic.ll
===================================================================
--- llvm/test/Transforms/InstCombine/abs-intrinsic.ll
+++ llvm/test/Transforms/InstCombine/abs-intrinsic.ll
@@ -452,6 +452,33 @@
ret i32 %r
}
+; https://alive2.llvm.org/ce/z/VSumU5
+define i32 @sub_abs_sge(i32 %x, i32 %y) {
+; CHECK-LABEL: @sub_abs_sge(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP_NOT:%.*]] = icmp slt i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: br i1 [[CMP_NOT]], label [[COND_END:%.*]], label [[COND_TRUE:%.*]]
+; CHECK: cond.true:
+; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X]], [[Y]]
+; CHECK-NEXT: br label [[COND_END]]
+; CHECK: cond.end:
+; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[SUB]], [[COND_TRUE]] ], [ 0, [[ENTRY:%.*]] ]
+; CHECK-NEXT: ret i32 [[R]]
+;
+entry:
+ %cmp = icmp sge i32 %x, %y
+ br i1 %cmp, label %cond.true, label %cond.end
+
+cond.true:
+ %sub = sub nsw i32 %x, %y
+ %0 = call i32 @llvm.abs.i32(i32 %sub, i1 true)
+ br label %cond.end
+
+cond.end:
+ %r = phi i32 [ %0, %cond.true ], [ 0, %entry ]
+ ret i32 %r
+}
+
define i32 @sub_abs_lt(i32 %x, i32 %y) {
; CHECK-LABEL: @sub_abs_lt(
; CHECK-NEXT: entry:
@@ -478,6 +505,33 @@
ret i32 %r
}
+; https://alive2.llvm.org/ce/z/9wQo6G
+define i32 @sub_abs_sle(i32 %x, i32 %y) {
+; CHECK-LABEL: @sub_abs_sle(
+; CHECK-NEXT: entry:
+; CHECK-NEXT: [[CMP_NOT:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
+; CHECK-NEXT: br i1 [[CMP_NOT]], label [[COND_END:%.*]], label [[COND_TRUE:%.*]]
+; CHECK: cond.true:
+; CHECK-NEXT: [[SUB_NEG:%.*]] = sub i32 [[Y]], [[X]]
+; CHECK-NEXT: br label [[COND_END]]
+; CHECK: cond.end:
+; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[SUB_NEG]], [[COND_TRUE]] ], [ 0, [[ENTRY:%.*]] ]
+; CHECK-NEXT: ret i32 [[R]]
+;
+entry:
+ %cmp = icmp sle i32 %x, %y
+ br i1 %cmp, label %cond.true, label %cond.end
+
+cond.true:
+ %sub = sub nsw i32 %x, %y
+ %0 = call i32 @llvm.abs.i32(i32 %sub, i1 true)
+ br label %cond.end
+
+cond.end:
+ %r = phi i32 [ %0, %cond.true ], [ 0, %entry ]
+ ret i32 %r
+}
+
define i32 @sub_abs_lt_min_not_poison(i32 %x, i32 %y) {
; CHECK-LABEL: @sub_abs_lt_min_not_poison(
; CHECK-NEXT: entry:
Index: llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1024,7 +1024,13 @@
Value *X, *Y;
if (match(Op, m_NSWSub(m_Value(X), m_Value(Y))))
- return isImpliedByDomCondition(ICmpInst::ICMP_SLT, X, Y, CxtI, DL);
+ if (std::optional<bool> Known =
+ isImpliedByDomCondition(ICmpInst::ICMP_SLT, X, Y, CxtI, DL))
+ return Known;
+ if (match(Op, m_NSWSub(m_Value(X), m_Value(Y))))
+ if (std::optional<bool> Known =
+ isImpliedByDomCondition(ICmpInst::ICMP_SLE, X, Y, CxtI, DL))
+ return Known;
return isImpliedByDomCondition(
ICmpInst::ICMP_SLT, Op, Constant::getNullValue(Op->getType()), CxtI, DL);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156499.545002.patch
Type: text/x-patch
Size: 3070 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230728/11a21fed/attachment.bin>
More information about the llvm-commits
mailing list