Conversation
Member
|
@llvm/pr-subscribers-llvm-transforms Author: Nathan Corbyn (cofibrant) ChangesThis patch implements the following two peephole optimisations:
See the following Alive2 proofs: 1 and 2. Full diff: https://github.com/llvm/llvm-project/pull/176148.diff 2 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
index d536cb752d562..e511099302aaa 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
@@ -4288,6 +4288,40 @@ Instruction *InstCombinerImpl::foldICmpIntrinsicWithConstant(ICmpInst &Cmp,
II->getArgOperand(1));
}
break;
+ case Intrinsic::abs: {
+ if (!Cmp.getOperand(0)->hasOneUse())
+ return nullptr;
+
+ Value *X = II->getArgOperand(0);
+ APInt IntMinIsPoison =
+ dyn_cast<ConstantInt>(II->getArgOperand(1))->getValue();
+
+ // abs(X) u> K --> K >= 0 ? `X + K u> 2 * K` : `false`
+ if (Pred == CmpInst::ICMP_UGT) {
+ if (C.isNegative()) {
+ Cmp.replaceAllUsesWith(ConstantInt::getFalse(Ty));
+ return nullptr;
+ }
+
+ return new ICmpInst(ICmpInst::ICMP_UGT,
+ Builder.CreateAdd(X, ConstantInt::get(Ty, C)),
+ ConstantInt::get(Ty, 2 * C));
+ }
+
+ // abs(X) u< K --> K >= 1 ? `X + (K - 1) u<= 2 * (K - 1)` : K != 0
+ if (Pred == CmpInst::ICMP_ULT) {
+ if (C.slt(1)) {
+ Cmp.replaceAllUsesWith(ConstantInt::getBool(Ty, !C.isZero()));
+ return nullptr;
+ }
+
+ return new ICmpInst(ICmpInst::ICMP_ULE,
+ Builder.CreateAdd(X, ConstantInt::get(Ty, C - 1)),
+ ConstantInt::get(Ty, 2 * (C - 1)));
+ }
+
+ break;
+ }
default:
break;
}
diff --git a/llvm/test/Transforms/InstCombine/abs-intrinsic.ll b/llvm/test/Transforms/InstCombine/abs-intrinsic.ll
index 763d82652dd5d..aaac71d5a002a 100644
--- a/llvm/test/Transforms/InstCombine/abs-intrinsic.ll
+++ b/llvm/test/Transforms/InstCombine/abs-intrinsic.ll
@@ -997,3 +997,211 @@ define i8 @abs_diff_sle_y_x(i8 %x, i8 %y) {
%cond = select i1 %cmp, i8 %sub, i8 %sub1
ret i8 %cond
}
+
+define i1 @abs_cmp_ule_no_poison(i32 %x) {
+; CHECK-LABEL: @abs_cmp_ule_no_poison(
+; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[X:%.*]], 31
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[TMP1]], 63
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 0)
+ %cmp = icmp ule i32 %x.abs, 31
+ ret i1 %cmp
+}
+
+define i1 @abs_cmp_ule_no_poison_multiuse(i32 %x) {
+; CHECK-LABEL: @abs_cmp_ule_no_poison_multiuse(
+; CHECK-NEXT: [[X_ABS:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 false)
+; CHECK-NEXT: call void @use(i32 [[X_ABS]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X_ABS]], 32
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 0)
+ call void @use(i32 %x.abs)
+ %cmp = icmp ule i32 %x.abs, 31
+ ret i1 %cmp
+}
+
+define i1 @abs_cmp_ule_poison(i32 %x) {
+; CHECK-LABEL: @abs_cmp_ule_poison(
+; CHECK-NEXT: [[X_ABS_FREEZE:%.*]] = freeze i32 [[X:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[X_ABS_FREEZE]], 31
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[TMP1]], 63
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 1)
+ %x.abs.freeze = freeze i32 %x.abs
+ %cmp = icmp ule i32 %x.abs.freeze, 31
+ ret i1 %cmp
+}
+
+define i1 @abs_cmp_ule_poison_multiuse(i32 %x) {
+; CHECK-LABEL: @abs_cmp_ule_poison_multiuse(
+; CHECK-NEXT: [[X_FR:%.*]] = freeze i32 [[X:%.*]]
+; CHECK-NEXT: [[X_ABS_FREEZE:%.*]] = call i32 @llvm.abs.i32(i32 [[X_FR]], i1 false)
+; CHECK-NEXT: call void @use(i32 [[X_ABS_FREEZE]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X_ABS_FREEZE]], 32
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 1)
+ %x.abs.freeze = freeze i32 %x.abs
+ call void @use(i32 %x.abs.freeze)
+ %cmp = icmp ule i32 %x.abs.freeze, 31
+ ret i1 %cmp
+}
+
+define i1 @abs_cmp_ult_no_poison(i32 %x) {
+; CHECK-LABEL: @abs_cmp_ult_no_poison(
+; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[X:%.*]], 31
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[TMP1]], 63
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 0)
+ %cmp = icmp ult i32 %x.abs, 32
+ ret i1 %cmp
+}
+
+define i1 @abs_cmp_ult_no_poison_multiuse(i32 %x) {
+; CHECK-LABEL: @abs_cmp_ult_no_poison_multiuse(
+; CHECK-NEXT: [[X_ABS:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 false)
+; CHECK-NEXT: call void @use(i32 [[X_ABS]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X_ABS]], 32
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 0)
+ call void @use(i32 %x.abs)
+ %cmp = icmp ult i32 %x.abs, 32
+ ret i1 %cmp
+}
+
+define i1 @abs_cmp_ult_poison(i32 %x) {
+; CHECK-LABEL: @abs_cmp_ult_poison(
+; CHECK-NEXT: [[X_FR:%.*]] = freeze i32 [[X:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[X_FR]], 31
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[TMP1]], 63
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 1)
+ %x.abs.freeze = freeze i32 %x.abs
+ %cmp = icmp ult i32 %x.abs.freeze, 32
+ ret i1 %cmp
+}
+
+define i1 @abs_cmp_ult_poison_multiuse(i32 %x) {
+; CHECK-LABEL: @abs_cmp_ult_poison_multiuse(
+; CHECK-NEXT: [[X_FR:%.*]] = freeze i32 [[X:%.*]]
+; CHECK-NEXT: [[X_ABS_FREEZE:%.*]] = call i32 @llvm.abs.i32(i32 [[X_FR]], i1 false)
+; CHECK-NEXT: call void @use(i32 [[X_ABS_FREEZE]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X_ABS_FREEZE]], 32
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 1)
+ %x.abs.freeze = freeze i32 %x.abs
+ call void @use(i32 %x.abs.freeze)
+ %cmp = icmp ult i32 %x.abs.freeze, 32
+ ret i1 %cmp
+}
+
+define i1 @abs_cmp_uge_no_poison(i32 %x) {
+; CHECK-LABEL: @abs_cmp_uge_no_poison(
+; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[X:%.*]], -31
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[TMP1]], -61
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 0)
+ %cmp = icmp ugt i32 %x.abs, 30
+ ret i1 %cmp
+}
+
+define i1 @abs_cmp_uge_no_poison_multiuse(i32 %x) {
+; CHECK-LABEL: @abs_cmp_uge_no_poison_multiuse(
+; CHECK-NEXT: [[X_ABS:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 false)
+; CHECK-NEXT: call void @use(i32 [[X_ABS]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[X_ABS]], 30
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 0)
+ call void @use(i32 %x.abs)
+ %cmp = icmp ugt i32 %x.abs, 30
+ ret i1 %cmp
+}
+
+define i1 @abs_cmp_uge_poison(i32 %x) {
+; CHECK-LABEL: @abs_cmp_uge_poison(
+; CHECK-NEXT: [[X_FR:%.*]] = freeze i32 [[X:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[X_FR]], -31
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[TMP1]], -61
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 1)
+ %x.abs.freeze = freeze i32 %x.abs
+ %cmp = icmp ugt i32 %x.abs.freeze, 30
+ ret i1 %cmp
+}
+
+define i1 @abs_cmp_uge_poison_multiuse(i32 %x) {
+; CHECK-LABEL: @abs_cmp_uge_poison_multiuse(
+; CHECK-NEXT: [[X_FR:%.*]] = freeze i32 [[X:%.*]]
+; CHECK-NEXT: [[X_ABS_FREEZE:%.*]] = call i32 @llvm.abs.i32(i32 [[X_FR]], i1 false)
+; CHECK-NEXT: call void @use(i32 [[X_ABS_FREEZE]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[X_ABS_FREEZE]], 30
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 1)
+ %x.abs.freeze = freeze i32 %x.abs
+ call void @use(i32 %x.abs.freeze)
+ %cmp = icmp ugt i32 %x.abs.freeze, 30
+ ret i1 %cmp
+}
+
+define i1 @abs_cmp_ugt_no_poison(i32 %x) {
+; CHECK-LABEL: @abs_cmp_ugt_no_poison(
+; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[X:%.*]], -32
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[TMP1]], -63
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 0)
+ %cmp = icmp ugt i32 %x.abs, 31
+ ret i1 %cmp
+}
+
+define i1 @abs_cmp_ugt_no_poison_multiuse(i32 %x) {
+; CHECK-LABEL: @abs_cmp_ugt_no_poison_multiuse(
+; CHECK-NEXT: [[X_ABS:%.*]] = call i32 @llvm.abs.i32(i32 [[X:%.*]], i1 false)
+; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[X_ABS]], 31
+; CHECK-NEXT: call void @use(i32 [[X_ABS]])
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 0)
+ %cmp = icmp ugt i32 %x.abs, 31
+ call void @use(i32 %x.abs)
+ ret i1 %cmp
+}
+
+define i1 @abs_cmp_ugt_poison(i32 %x) {
+; CHECK-LABEL: @abs_cmp_ugt_poison(
+; CHECK-NEXT: [[X:%.*]] = freeze i32 [[X1:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = add i32 [[X]], -32
+; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[TMP1]], -63
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 1)
+ %x.abs.freeze = freeze i32 %x.abs
+ %cmp = icmp ugt i32 %x.abs.freeze, 31
+ ret i1 %cmp
+}
+
+define i1 @abs_cmp_ugt_poison_multiuse(i32 %x) {
+; CHECK-LABEL: @abs_cmp_ugt_poison_multiuse(
+; CHECK-NEXT: [[X_FR:%.*]] = freeze i32 [[X:%.*]]
+; CHECK-NEXT: [[X_ABS_FREEZE:%.*]] = call i32 @llvm.abs.i32(i32 [[X_FR]], i1 false)
+; CHECK-NEXT: call void @use(i32 [[X_ABS_FREEZE]])
+; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i32 [[X_ABS_FREEZE]], 31
+; CHECK-NEXT: ret i1 [[CMP]]
+;
+ %x.abs = call i32 @llvm.abs.i32(i32 %x, i1 1)
+ %x.abs.freeze = freeze i32 %x.abs
+ call void @use(i32 %x.abs.freeze)
+ %cmp = icmp ugt i32 %x.abs.freeze, 31
+ ret i1 %cmp
+}
|
16ee280 to
23759cc
Compare
🐧 Linux x64 Test Results
✅ The build succeeded and all tests passed. |
23759cc to
de7fbd2
Compare
dtcxzyw
reviewed
Jan 19, 2026
fhahn
reviewed
Jan 26, 2026
Contributor
Author
|
Ping |
honeygoyal
pushed a commit
to honeygoyal/llvm-project
that referenced
this pull request
Jan 30, 2026
…to constant (llvm#176148) This patch implements the following two peephole optimisations: 1. ``` abs(X) u> K --> K >= 0 ? `X + K u> 2 * K` : `false` ```; 2. If `abs(INT_MIN)` is `poison`, ```abs(X) u< K --> K >= 1 ? `X + (K - 1) u<= 2 * (K - 1)` : K != 0```. See the following Alive2 proofs: [1](https://alive2.llvm.org/ce/z/J2SRSv) and [2](https://alive2.llvm.org/ce/z/tfxTrU).
SavchenkoValeriy
pushed a commit
to swiftlang/llvm-project
that referenced
this pull request
Feb 2, 2026
…to constant (llvm#176148) This patch implements the following two peephole optimisations: 1. ``` abs(X) u> K --> K >= 0 ? `X + K u> 2 * K` : `false` ```; 2. If `abs(INT_MIN)` is `poison`, ```abs(X) u< K --> K >= 1 ? `X + (K - 1) u<= 2 * (K - 1)` : K != 0```. See the following Alive2 proofs: [1](https://alive2.llvm.org/ce/z/J2SRSv) and [2](https://alive2.llvm.org/ce/z/tfxTrU).
sshrestha-aa
pushed a commit
to sshrestha-aa/llvm-project
that referenced
this pull request
Feb 4, 2026
…to constant (llvm#176148) This patch implements the following two peephole optimisations: 1. ``` abs(X) u> K --> K >= 0 ? `X + K u> 2 * K` : `false` ```; 2. If `abs(INT_MIN)` is `poison`, ```abs(X) u< K --> K >= 1 ? `X + (K - 1) u<= 2 * (K - 1)` : K != 0```. See the following Alive2 proofs: [1](https://alive2.llvm.org/ce/z/J2SRSv) and [2](https://alive2.llvm.org/ce/z/tfxTrU).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This patch implements the following two peephole optimisations:
abs(X) u> K --> K >= 0 ? `X + K u> 2 * K` : `false`;abs(INT_MIN)ispoison,abs(X) u< K --> K >= 1 ? `X + (K - 1) u<= 2 * (K - 1)` : K != 0.See the following Alive2 proofs: 1 and 2.